@automata-network/pom-js-sdk
Advanced tools
Comparing version 0.0.27 to 0.0.28
@@ -28,3 +28,2 @@ import * as _automata_network_pom_socket from '@automata-network/pom-socket'; | ||
declare enum ChainId { | ||
Ethereum = "0x1", | ||
LineaSepolia = "0xe705" | ||
@@ -31,0 +30,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict";var e,r,o,t,a=require("@automata-network/pom-socket"),s=require("qs"),i=require("@nuintun/qrcode"),n=require("./util.js");exports.ChainId=void 0,(e=exports.ChainId||(exports.ChainId={})).Ethereum="0x1",e.LineaSepolia="0xe705",exports.POMPlatform=void 0,(r=exports.POMPlatform||(exports.POMPlatform={})).Android="android",r.iOS="iOS",r.macOS="macOS",exports.IdentityStatus=void 0,(o=exports.IdentityStatus||(exports.IdentityStatus={})).None="none",o.Generated="generated",o.Registered="registered",exports.CallbackBrowser=void 0,(t=exports.CallbackBrowser||(exports.CallbackBrowser={})).iOSChrome="iOSChrome",t.iOSSafari="iOSSafari",t.iOSEdge="iOSEdge",t.iOSFirefox="iOSFirefox",t.iOSOpera="iOSOpera",t.iOSBrave="iOSBrave",t.iOSAloha="iOSAloha",t.androidChrome="androidChrome",t.androidEdge="androidEdge",t.androidFirefox="androidFirefox",t.androidOpera="androidOpera",t.androidBrave="androidBrave",t.androidAloha="androidAloha",t.macOSChrome="macOSChrome",t.macOSSafari="macOSSafari",t.macOSEdge="macOSEdge",t.macOSFirefox="macOSFirefox",t.macOSOpera="macOSOpera",t.macOSBrave="macOSBrave",t.macOSAloha="macOSAloha";const c={macOS:{[exports.CallbackBrowser.macOSChrome]:"com.google.Chrome",[exports.CallbackBrowser.macOSSafari]:"com.apple.safari",[exports.CallbackBrowser.macOSFirefox]:"org.mozilla.firefox",[exports.CallbackBrowser.macOSEdge]:"com.microsoft.edgemac",[exports.CallbackBrowser.macOSBrave]:"com.brave.Browser",[exports.CallbackBrowser.macOSOpera]:"com.operasoftware.Opera",[exports.CallbackBrowser.macOSAloha]:"com.alohabrowser.alohabrowser"},iOS:{[exports.CallbackBrowser.iOSChrome]:"com.google.chrome.ios",[exports.CallbackBrowser.iOSSafari]:"com.apple.mobilesafari",[exports.CallbackBrowser.iOSFirefox]:"org.mozilla.ios.Firefox",[exports.CallbackBrowser.iOSEdge]:"com.microsoft.msedge",[exports.CallbackBrowser.iOSBrave]:"com.brave.ios.browser",[exports.CallbackBrowser.iOSOpera]:"com.opera.OperaTouch",[exports.CallbackBrowser.iOSAloha]:"com.alohabrowser.alohabrowser"},android:{[exports.CallbackBrowser.androidChrome]:"com.android.chrome",[exports.CallbackBrowser.androidFirefox]:"org.mozilla.firefox",[exports.CallbackBrowser.androidEdge]:"com.microsoft.emmx",[exports.CallbackBrowser.androidBrave]:"com.brave.browser",[exports.CallbackBrowser.androidOpera]:"com.opera.browser",[exports.CallbackBrowser.androidAloha]:"com.aloha.browser"}},d="pom://pomrn.ata.network/connect",l=new Error("deep link is invalid"),p=new Error("deep link parameters is invalid"),m=new Error("socket is connecting"),S=new Error("get identity status timeout"),O=new Error("identity is empty");class h{pomSocket;constructor(...[e,r]){this.pomSocket=a.getSocket(e,r)}static generateDeepLink(e){const{sessionId:r,dhSharedPublicNumber:o,hkdfSalt:t,callbackBrowser:a,callbackDeepLinkUrl:i}=e,n={id:r,dh:o,s:t,cbBrowser:a,cbUrl:i};return`${d}?${s.stringify(n)}`}static parseDeepLinkQuery(e){const[r,o]=e.split("?");if(r!==d)throw l;const t=s.parse(o);if(null!=t.id&&"string"!=typeof t.id||null!=t.dh&&"string"!=typeof t.dh||null!=t.s&&"string"!=typeof t.s||null!=t.cbUrl&&"string"!=typeof t.cbUrl||null!=t.cbBrowser&&"string"!=typeof t.cbBrowser)throw p;return t}static generateQrcode(e,r){return new i.Encoder({level:r?.level||"M"}).encode(new i.Byte(e)).toDataURL()}async sign(e){const{chainId:r,caller:o,message:t,qrcodeLevel:s,onRenderQrcode:i,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;if(this.pomSocket.status===a.SocketStatus.Connecting)throw m;this.pomSocket.status===a.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:s,onRenderQrcode:i});const l=await this.getIdentityStatus({chainId:r}).promise;let p;if(l.status!==exports.IdentityStatus.None&&l.status!==exports.IdentityStatus.Generated||(p=await this.attestDevice({chainId:r,caller:o}).promise),l.status===exports.IdentityStatus.None||l.status===exports.IdentityStatus.Generated){if(null==p)throw O;await d(p),await n.runWithErrorRetry((()=>{const e=c({chainId:r,identity:p.identity});if(!e)throw n.ERR_NEED_RETRY;return e}))}return await this.signMessage({chainId:r,caller:o,message:t}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:r,onRenderQrcode:o}=e,t=await this.connectSocket(),a=h.generateDeepLink(t);o({deepLink:a,imageBase64:h.generateQrcode(a,r?{level:r}:void 0)}),await this.waitForConnectedEvent(),await this.waitForKeyExchangeEvent()}catch(r){if(r!==a.ERR_SESSION_IS_EXPIRED)throw r;this.connectSocketAndWait(e)}}async connectSocket(){return this.pomSocket.connect()}async waitForConnectedEvent(){return this.pomSocket.waitForConnectedEvent()}async waitForKeyExchangeEvent(){return this.pomSocket.waitForKeyExchangeEvent()}getIdentityStatus(e){const{chainId:r}=e;let o;return{promise:new Promise(((e,t)=>{o=n.sendMessageAndWaitForResponse({socket:this.pomSocket,op:n.POMOperation.GetIdentityStatus,data:{chainId:r},resolver:e,rejecter:t})})),cancel:o}}attestDevice(e){let r;return{promise:new Promise(((o,t)=>{r=n.sendMessageAndWaitForResponse({socket:this.pomSocket,op:n.POMOperation.AttestDevice,data:e,resolver:o,rejecter:t})})),cancel:r}}signMessage(e){let r;return{promise:new Promise(((o,t)=>{r=n.sendMessageAndWaitForResponse({socket:this.pomSocket,op:n.POMOperation.SignMessage,data:e,resolver:o,rejecter:t})})),cancel:r}}}exports.ERR_OPERATION_CANCELLED=n.ERR_OPERATION_CANCELLED,Object.defineProperty(exports,"POMOperation",{enumerable:!0,get:function(){return n.POMOperation}}),exports.decodeMessageData=n.decodeMessageData,exports.encodeMessageData=n.encodeMessageData,exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=S,exports.ERR_IDENTITY_IS_EMPTY=O,exports.ERR_INVALID_DEEP_LINK=l,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=p,exports.ERR_SOCKET_IS_CONNECTING=m,exports.POMClinet=h,exports.bundleIds=c; | ||
"use strict";var e,r,o,t=require("@automata-network/pom-socket"),a=require("qs"),s=require("@nuintun/qrcode"),i=require("./util.js");exports.ChainId=void 0,(exports.ChainId||(exports.ChainId={})).LineaSepolia="0xe705",exports.POMPlatform=void 0,(e=exports.POMPlatform||(exports.POMPlatform={})).Android="android",e.iOS="iOS",e.macOS="macOS",exports.IdentityStatus=void 0,(r=exports.IdentityStatus||(exports.IdentityStatus={})).None="none",r.Generated="generated",r.Registered="registered",exports.CallbackBrowser=void 0,(o=exports.CallbackBrowser||(exports.CallbackBrowser={})).iOSChrome="iOSChrome",o.iOSSafari="iOSSafari",o.iOSEdge="iOSEdge",o.iOSFirefox="iOSFirefox",o.iOSOpera="iOSOpera",o.iOSBrave="iOSBrave",o.iOSAloha="iOSAloha",o.androidChrome="androidChrome",o.androidEdge="androidEdge",o.androidFirefox="androidFirefox",o.androidOpera="androidOpera",o.androidBrave="androidBrave",o.androidAloha="androidAloha",o.macOSChrome="macOSChrome",o.macOSSafari="macOSSafari",o.macOSEdge="macOSEdge",o.macOSFirefox="macOSFirefox",o.macOSOpera="macOSOpera",o.macOSBrave="macOSBrave",o.macOSAloha="macOSAloha";const n={macOS:{[exports.CallbackBrowser.macOSChrome]:"com.google.Chrome",[exports.CallbackBrowser.macOSSafari]:"com.apple.safari",[exports.CallbackBrowser.macOSFirefox]:"org.mozilla.firefox",[exports.CallbackBrowser.macOSEdge]:"com.microsoft.edgemac",[exports.CallbackBrowser.macOSBrave]:"com.brave.Browser",[exports.CallbackBrowser.macOSOpera]:"com.operasoftware.Opera",[exports.CallbackBrowser.macOSAloha]:"com.alohabrowser.alohabrowser"},iOS:{[exports.CallbackBrowser.iOSChrome]:"com.google.chrome.ios",[exports.CallbackBrowser.iOSSafari]:"com.apple.mobilesafari",[exports.CallbackBrowser.iOSFirefox]:"org.mozilla.ios.Firefox",[exports.CallbackBrowser.iOSEdge]:"com.microsoft.msedge",[exports.CallbackBrowser.iOSBrave]:"com.brave.ios.browser",[exports.CallbackBrowser.iOSOpera]:"com.opera.OperaTouch",[exports.CallbackBrowser.iOSAloha]:"com.alohabrowser.alohabrowser"},android:{[exports.CallbackBrowser.androidChrome]:"com.android.chrome",[exports.CallbackBrowser.androidFirefox]:"org.mozilla.firefox",[exports.CallbackBrowser.androidEdge]:"com.microsoft.emmx",[exports.CallbackBrowser.androidBrave]:"com.brave.browser",[exports.CallbackBrowser.androidOpera]:"com.opera.browser",[exports.CallbackBrowser.androidAloha]:"com.aloha.browser"}},c="pom://pomrn.ata.network/connect",d=new Error("deep link is invalid"),l=new Error("deep link parameters is invalid"),p=new Error("socket is connecting"),m=new Error("get identity status timeout"),S=new Error("identity is empty");class O{pomSocket;constructor(...[e,r]){this.pomSocket=t.getSocket(e,r)}static generateDeepLink(e){const{sessionId:r,dhSharedPublicNumber:o,hkdfSalt:t,callbackBrowser:s,callbackDeepLinkUrl:i}=e,n={id:r,dh:o,s:t,cbBrowser:s,cbUrl:i};return`${c}?${a.stringify(n)}`}static parseDeepLinkQuery(e){const[r,o]=e.split("?");if(r!==c)throw d;const t=a.parse(o);if(null!=t.id&&"string"!=typeof t.id||null!=t.dh&&"string"!=typeof t.dh||null!=t.s&&"string"!=typeof t.s||null!=t.cbUrl&&"string"!=typeof t.cbUrl||null!=t.cbBrowser&&"string"!=typeof t.cbBrowser)throw l;return t}static generateQrcode(e,r){return new s.Encoder({level:r?.level||"M"}).encode(new s.Byte(e)).toDataURL()}async sign(e){const{chainId:r,caller:o,message:a,qrcodeLevel:s,onRenderQrcode:n,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;if(this.pomSocket.status===t.SocketStatus.Connecting)throw p;this.pomSocket.status===t.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:s,onRenderQrcode:n});const l=await this.getIdentityStatus({chainId:r}).promise;let m;if(l.status!==exports.IdentityStatus.None&&l.status!==exports.IdentityStatus.Generated||(m=await this.attestDevice({chainId:r,caller:o}).promise),l.status===exports.IdentityStatus.None||l.status===exports.IdentityStatus.Generated){if(null==m)throw S;await d(m),await i.runWithErrorRetry((()=>{const e=c({chainId:r,identity:m.identity});if(!e)throw i.ERR_NEED_RETRY;return e}))}return await this.signMessage({chainId:r,caller:o,message:a}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:r,onRenderQrcode:o}=e,t=await this.connectSocket(),a=O.generateDeepLink(t);o({deepLink:a,imageBase64:O.generateQrcode(a,r?{level:r}:void 0)}),await this.waitForConnectedEvent(),await this.waitForKeyExchangeEvent()}catch(r){if(r!==t.ERR_SESSION_IS_EXPIRED)throw r;this.connectSocketAndWait(e)}}async connectSocket(){return this.pomSocket.connect()}async waitForConnectedEvent(){return this.pomSocket.waitForConnectedEvent()}async waitForKeyExchangeEvent(){return this.pomSocket.waitForKeyExchangeEvent()}getIdentityStatus(e){const{chainId:r}=e;let o;return{promise:new Promise(((e,t)=>{o=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.GetIdentityStatus,data:{chainId:r},resolver:e,rejecter:t})})),cancel:o}}attestDevice(e){let r;return{promise:new Promise(((o,t)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.AttestDevice,data:e,resolver:o,rejecter:t})})),cancel:r}}signMessage(e){let r;return{promise:new Promise(((o,t)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.SignMessage,data:e,resolver:o,rejecter:t})})),cancel:r}}}exports.ERR_OPERATION_CANCELLED=i.ERR_OPERATION_CANCELLED,Object.defineProperty(exports,"POMOperation",{enumerable:!0,get:function(){return i.POMOperation}}),exports.decodeMessageData=i.decodeMessageData,exports.encodeMessageData=i.encodeMessageData,exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=m,exports.ERR_IDENTITY_IS_EMPTY=S,exports.ERR_INVALID_DEEP_LINK=d,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=l,exports.ERR_SOCKET_IS_CONNECTING=p,exports.POMClinet=O,exports.bundleIds=n; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@automata-network/pom-js-sdk", | ||
"version": "0.0.27", | ||
"version": "0.0.28", | ||
"description": "A socket clinet for automata's Proof Of Machinehood", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -24,3 +24,2 @@ import { | ||
export enum ChainId { | ||
Ethereum = '0x1', | ||
LineaSepolia = '0xe705', | ||
@@ -27,0 +26,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
51097
739