@automata-network/pom-js-sdk
Advanced tools
Comparing version 0.0.77 to 0.0.78
@@ -27,2 +27,6 @@ import * as _automata_network_pom_socket from '@automata-network/pom-socket'; | ||
declare function getCallbackBrowser(): CallbackBrowser | undefined; | ||
declare function canOpenCallbackBrowser(options: { | ||
identityStatus?: IdentityStatusResult; | ||
browser: CallbackBrowser; | ||
}): boolean; | ||
@@ -173,2 +177,2 @@ declare enum ChainId { | ||
export { type AttestDeviceOptions, type AttestDeviceResult, CallbackBrowser, ChainId, type ChainIdOptions, type ConnectionQuery, type DeepLinkGenerationRequest, type DeepLinkQuery, ERR_GET_IDENTITY_STATUS_TIMEOUT, ERR_IDENTITY_IS_EMPTY, ERR_INVALID_DEEP_LINK, ERR_INVALID_DEEP_LINK_PARAMETERS, ERR_OPERATION_CANCELLED, ERR_SOCKET_IS_CONNECTING, type GetIdentityOnChainOptions, type GetIdentityStatusOptions, IdentityStatus, type IdentityStatusPayload, type IdentityStatusResult, type MessageResult, POMClinet, POMOperation, POMPlatform, type QrcodeGenerationRequest, type RenderQrcodeData, type SignMessageOptions, type SignatureOptions, type SignatureResult, bundleIds, decodeMessageData, encodeMessageData, getCallbackBrowser, pomAppDeepLinkProtocol }; | ||
export { type AttestDeviceOptions, type AttestDeviceResult, CallbackBrowser, ChainId, type ChainIdOptions, type ConnectionQuery, type DeepLinkGenerationRequest, type DeepLinkQuery, ERR_GET_IDENTITY_STATUS_TIMEOUT, ERR_IDENTITY_IS_EMPTY, ERR_INVALID_DEEP_LINK, ERR_INVALID_DEEP_LINK_PARAMETERS, ERR_OPERATION_CANCELLED, ERR_SOCKET_IS_CONNECTING, type GetIdentityOnChainOptions, type GetIdentityStatusOptions, IdentityStatus, type IdentityStatusPayload, type IdentityStatusResult, type MessageResult, POMClinet, POMOperation, POMPlatform, type QrcodeGenerationRequest, type RenderQrcodeData, type SignMessageOptions, type SignatureOptions, type SignatureResult, bundleIds, canOpenCallbackBrowser, decodeMessageData, encodeMessageData, getCallbackBrowser, pomAppDeepLinkProtocol }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e,r,a,o,t=require("@automata-network/pom-socket"),s=require("qrcode"),i=require("./util.js");exports.ChainId=void 0,(e=exports.ChainId||(exports.ChainId={})).LineaSepolia="0xe705",e.AutomataTestnet="0x1555e3",e.AutomataMainnet="0x10000",exports.POMPlatform=void 0,(r=exports.POMPlatform||(exports.POMPlatform={})).Android="android",r.iOS="iOS",r.macOS="macOS",exports.IdentityStatus=void 0,(a=exports.IdentityStatus||(exports.IdentityStatus={})).None="none",a.Generated="generated",a.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-universal-link.ata.network",d="https:",l="pom:",p=new Error("deep link is invalid"),m=new Error("deep link parameters is invalid"),S=new Error("socket is connecting"),h=new Error("get identity status timeout"),O=new Error("identity is empty");class w{pomSocket;constructor(...[e,r]){this.pomSocket=t.getSocket(e,r)}static generateDeepLink(e){const{sessionId:r,dhSharedPublicNumber:a,hkdfSalt:o,callbackBrowser:t,callbackDeepLinkUrl:s}=e,n=i.isDevice.macOS(),p=i.isBrowser.Safari(),m=new URL(`${n&&!p?l:d}//${c}`);return m.hash="download",r&&m.searchParams.set("id",r),a&&m.searchParams.set("dh",a),o&&m.searchParams.set("s",o),t&&m.searchParams.set("cb",t),s&&m.searchParams.set("cu",s),m.toString()}static parseDeepLinkQuery(e){const r=new URL(e);if(r.hostname!==c||r.protocol!==d&&r.protocol!==l)throw p;const a=r.searchParams.get("id"),o=r.searchParams.get("dh"),t=r.searchParams.get("s"),s=r.searchParams.get("cu"),i=r.searchParams.get("cb");if(null!=a&&"string"!=typeof a||null!=o&&"string"!=typeof o||null!=t&&"string"!=typeof t||null!=s&&"string"!=typeof s||null!=i&&"string"!=typeof i)throw m;return{id:a,dh:o,s:t,cu:s,cb:i}}static async generateQrcode(e,r){const{sessionId:a,dhSharedPublicNumber:o,hkdfSalt:t}=e,i=[o?{data:o.toUpperCase(),mode:"alphanumeric"}:void 0,t?{data:t,mode:"alphanumeric"}:void 0,a?{data:a}:void 0];return await s.toDataURL(i.filter((e=>null!=e)),{errorCorrectionLevel:r?.level||"L"})}static parseQrcode(e){const r=e.slice(0,128).toLowerCase(),a=e.slice(128,160);return{id:e.slice(160),dh:r,s:a}}async sign(e){const{chainId:r,caller:a,message:o,qrcodeLevel:s,onRenderQrcode:n,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;let l=this.pomSocket.getSocketStatus();if(l===t.SocketStatus.Connecting)throw S;l===t.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:s,onRenderQrcode:n});const p=await this.getIdentityStatus({chainId:r}).promise;let m;if(p.status!==exports.IdentityStatus.None&&p.status!==exports.IdentityStatus.Generated||(m=await this.attestDevice({chainId:r,caller:a}).promise),p.status===exports.IdentityStatus.None||p.status===exports.IdentityStatus.Generated){if(null==m)throw O;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:a,message:o}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:r,onRenderQrcode:a}=e,o=await this.connectSocket(),t=w.generateDeepLink(o);a({deepLink:t,imageBase64:await w.generateQrcode(o,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 a;return{promise:new Promise(((e,o)=>{a=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.GetIdentityStatus,data:{chainId:r},resolver:e,rejecter:o})})),cancel:a}}attestDevice(e){let r;return{promise:new Promise(((a,o)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.AttestDevice,data:e,resolver:a,rejecter:o})})),cancel:r}}signMessage(e){let r;return{promise:new Promise(((a,o)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.SignMessage,data:e,resolver:a,rejecter:o})})),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.getCallbackBrowser=i.getCallbackBrowser,exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=h,exports.ERR_IDENTITY_IS_EMPTY=O,exports.ERR_INVALID_DEEP_LINK=p,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=m,exports.ERR_SOCKET_IS_CONNECTING=S,exports.POMClinet=w,exports.bundleIds=n,exports.pomAppDeepLinkProtocol=l; | ||
"use strict";var e,r,a,o,t=require("@automata-network/pom-socket"),s=require("qrcode"),i=require("./util.js");exports.ChainId=void 0,(e=exports.ChainId||(exports.ChainId={})).LineaSepolia="0xe705",e.AutomataTestnet="0x1555e3",e.AutomataMainnet="0x10000",exports.POMPlatform=void 0,(r=exports.POMPlatform||(exports.POMPlatform={})).Android="android",r.iOS="iOS",r.macOS="macOS",exports.IdentityStatus=void 0,(a=exports.IdentityStatus||(exports.IdentityStatus={})).None="none",a.Generated="generated",a.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-universal-link.ata.network",d="https:",l="pom:",p=new Error("deep link is invalid"),m=new Error("deep link parameters is invalid"),S=new Error("socket is connecting"),h=new Error("get identity status timeout"),O=new Error("identity is empty");class w{pomSocket;constructor(...[e,r]){this.pomSocket=t.getSocket(e,r)}static generateDeepLink(e){const{sessionId:r,dhSharedPublicNumber:a,hkdfSalt:o,callbackBrowser:t,callbackDeepLinkUrl:s}=e,n=i.isDevice.macOS(),p=i.isBrowser.Safari(),m=new URL(`${n&&!p?l:d}//${c}`);return m.hash="download",r&&m.searchParams.set("id",r),a&&m.searchParams.set("dh",a),o&&m.searchParams.set("s",o),t&&m.searchParams.set("cb",t),s&&m.searchParams.set("cu",s),m.toString()}static parseDeepLinkQuery(e){const r=new URL(e);if(r.hostname!==c||r.protocol!==d&&r.protocol!==l)throw p;const a=r.searchParams.get("id"),o=r.searchParams.get("dh"),t=r.searchParams.get("s"),s=r.searchParams.get("cu"),i=r.searchParams.get("cb");if(null!=a&&"string"!=typeof a||null!=o&&"string"!=typeof o||null!=t&&"string"!=typeof t||null!=s&&"string"!=typeof s||null!=i&&"string"!=typeof i)throw m;return{id:a,dh:o,s:t,cu:s,cb:i}}static async generateQrcode(e,r){const{sessionId:a,dhSharedPublicNumber:o,hkdfSalt:t}=e,i=[o?{data:o.toUpperCase(),mode:"alphanumeric"}:void 0,t?{data:t,mode:"alphanumeric"}:void 0,a?{data:a}:void 0];return await s.toDataURL(i.filter((e=>null!=e)),{errorCorrectionLevel:r?.level||"L"})}static parseQrcode(e){const r=e.slice(0,128).toLowerCase(),a=e.slice(128,160);return{id:e.slice(160),dh:r,s:a}}async sign(e){const{chainId:r,caller:a,message:o,qrcodeLevel:s,onRenderQrcode:n,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;let l=this.pomSocket.getSocketStatus();if(l===t.SocketStatus.Connecting)throw S;l===t.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:s,onRenderQrcode:n});const p=await this.getIdentityStatus({chainId:r}).promise;let m;if(p.status!==exports.IdentityStatus.None&&p.status!==exports.IdentityStatus.Generated||(m=await this.attestDevice({chainId:r,caller:a}).promise),p.status===exports.IdentityStatus.None||p.status===exports.IdentityStatus.Generated){if(null==m)throw O;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:a,message:o}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:r,onRenderQrcode:a}=e,o=await this.connectSocket(),t=w.generateDeepLink(o);a({deepLink:t,imageBase64:await w.generateQrcode(o,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 a;return{promise:new Promise(((e,o)=>{a=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.GetIdentityStatus,data:{chainId:r},resolver:e,rejecter:o})})),cancel:a}}attestDevice(e){let r;return{promise:new Promise(((a,o)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.AttestDevice,data:e,resolver:a,rejecter:o})})),cancel:r}}signMessage(e){let r;return{promise:new Promise(((a,o)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.SignMessage,data:e,resolver:a,rejecter:o})})),cancel:r}}}exports.ERR_OPERATION_CANCELLED=i.ERR_OPERATION_CANCELLED,Object.defineProperty(exports,"POMOperation",{enumerable:!0,get:function(){return i.POMOperation}}),exports.canOpenCallbackBrowser=i.canOpenCallbackBrowser,exports.decodeMessageData=i.decodeMessageData,exports.encodeMessageData=i.encodeMessageData,exports.getCallbackBrowser=i.getCallbackBrowser,exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=h,exports.ERR_IDENTITY_IS_EMPTY=O,exports.ERR_INVALID_DEEP_LINK=p,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=m,exports.ERR_SOCKET_IS_CONNECTING=S,exports.POMClinet=w,exports.bundleIds=n,exports.pomAppDeepLinkProtocol=l; | ||
//# sourceMappingURL=index.js.map |
import { POMSocket } from '@automata-network/pom-socket'; | ||
import { CallbackBrowser } from '.'; | ||
import { CallbackBrowser, IdentityStatusResult } from '.'; | ||
export declare enum POMOperation { | ||
@@ -60,1 +60,5 @@ GetIdentityStatus = "getIdentityStatus", | ||
export declare function getCallbackBrowser(): CallbackBrowser | undefined; | ||
export declare function canOpenCallbackBrowser(options: { | ||
identityStatus?: IdentityStatusResult; | ||
browser: CallbackBrowser; | ||
}): boolean; |
@@ -1,2 +0,2 @@ | ||
"use strict";var r,e=require("@automata-network/pom-socket"),a=require("./index.js");exports.POMOperation=void 0,(r=exports.POMOperation||(exports.POMOperation={})).GetIdentityStatus="getIdentityStatus",r.AttestDevice="attestDevice",r.SignMessage="signMessage";const t=new Error("operation is cancelled"),o=new Error("need retry");function n(r){const{id:a=e.createRandomString(6),error:t}=r;return{id:a,data:JSON.stringify({id:a,success:null==t,error:t,...r})}}function i(r){return JSON.parse(r)}const s={Android:function(){return navigator.userAgent.match(/Android/i)},macOS:function(){return navigator.userAgent.match(/macintosh|macintel|macppc|mac68k|macos/i)},iOS:function(){return navigator.userAgent.match(/iPhone|iPad|iPod/i)},any:function(){return s.Android()||s.macOS()||s.iOS()}},c={Chrome:function(){return navigator.userAgent.match(/Chrome/i)},Safari:function(){return navigator.vendor.match(/apple/i)&&!navigator.userAgent.match(/crios/i)&&!navigator.userAgent.match(/fxios/i)&&!navigator.userAgent.match(/Opera|OPT\//)},Edge:function(){return navigator.userAgent.match(/Edg/i)},Firefox:function(){return navigator.userAgent.match(/Firefox/i)},Opera:function(){return navigator.userAgent.match(/Opera/i)},Brave:function(){return navigator.userAgent.match(/Brave/i)},Aloha:function(){return navigator.userAgent.match(/Aloha/i)},any:function(){return c.Chrome()||c.Safari()||c.Edge()||c.Firefox()||c.Opera()||c.Brave()||c.Aloha()}};exports.ERR_NEED_RETRY=o,exports.ERR_OPERATION_CANCELLED=t,exports.decodeMessageData=i,exports.encodeMessageData=n,exports.getCallbackBrowser=function(){if(s.Android()){if(c.Chrome())return a.CallbackBrowser.androidChrome;if(c.Edge())return a.CallbackBrowser.androidEdge;if(c.Firefox())return a.CallbackBrowser.androidFirefox;if(c.Opera())return a.CallbackBrowser.androidOpera;if(c.Brave())return a.CallbackBrowser.androidBrave;if(c.Aloha())return a.CallbackBrowser.androidAloha}else if(s.iOS()){if(c.Chrome())return a.CallbackBrowser.iOSChrome;if(c.Safari())return a.CallbackBrowser.iOSSafari;if(c.Edge())return a.CallbackBrowser.iOSEdge;if(c.Firefox())return a.CallbackBrowser.iOSFirefox;if(c.Opera())return a.CallbackBrowser.iOSOpera;if(c.Brave())return a.CallbackBrowser.iOSBrave;if(c.Aloha())return a.CallbackBrowser.iOSAloha}else if(s.macOS()){if(c.Chrome())return a.CallbackBrowser.macOSChrome;if(c.Safari())return a.CallbackBrowser.macOSSafari;if(c.Edge())return a.CallbackBrowser.macOSEdge;if(c.Firefox())return a.CallbackBrowser.macOSFirefox;if(c.Opera())return a.CallbackBrowser.macOSOpera;if(c.Brave())return a.CallbackBrowser.macOSBrave;if(c.Aloha())return a.CallbackBrowser.macOSAloha}},exports.isBrowser=c,exports.isDevice=s,exports.runWithErrorRetry=async function(r,e){const{signal:a,maxRetry:t=5,throttleInterval:n=3e3,backoffStrategyFactor:i,timeoutError:s}=e||{};let c=0,u=!0,f=!1;for(a?.addEventListener("abort",(function r(){f=!0,a?.removeEventListener("abort",r)}));u&&!f;){u=!1;try{return await r({retryCount:c})}catch(r){if(r===o&&c<t&&!f){c+=1,u=!0,await new Promise((r=>{setTimeout((()=>{r()}),n*(i?c*i:1))}));continue}throw r}}throw s||new Error("runWithErrorRetry no result")},exports.sendMessageAndWaitForResponse=function(r){const{socket:e,op:a,data:o,resolver:s,rejecter:c}=r,u=n({op:a,data:o});function f(r){e.off(l),c(r)}const l={success(r){try{const t=i(r);t.id===u.id&&t.op===a&&(t.success?(e.off(l),s(t.data)):f(new Error(t.error||"Unknown error")))}catch(r){f(r)}},error:f};return e.on(l),e.send(u.data),()=>{f(t)}}; | ||
"use strict";var r,e=require("@automata-network/pom-socket"),a=require("./index.js");exports.POMOperation=void 0,(r=exports.POMOperation||(exports.POMOperation={})).GetIdentityStatus="getIdentityStatus",r.AttestDevice="attestDevice",r.SignMessage="signMessage";const t=new Error("operation is cancelled"),o=new Error("need retry");function n(r){const{id:a=e.createRandomString(6),error:t}=r;return{id:a,data:JSON.stringify({id:a,success:null==t,error:t,...r})}}function i(r){return JSON.parse(r)}const s={Android:function(){return navigator.userAgent.match(/Android/i)},macOS:function(){return navigator.userAgent.match(/macintosh|macintel|macppc|mac68k|macos/i)},iOS:function(){return navigator.userAgent.match(/iPhone|iPad|iPod/i)},any:function(){return s.Android()||s.macOS()||s.iOS()}},c={Chrome:function(){return navigator.userAgent.match(/Chrome/i)},Safari:function(){return navigator.vendor.match(/apple/i)&&!navigator.userAgent.match(/crios/i)&&!navigator.userAgent.match(/fxios/i)&&!navigator.userAgent.match(/Opera|OPT\//)},Edge:function(){return navigator.userAgent.match(/Edg/i)},Firefox:function(){return navigator.userAgent.match(/Firefox/i)},Opera:function(){return navigator.userAgent.match(/Opera/i)},Brave:function(){return navigator.userAgent.match(/Brave/i)},Aloha:function(){return navigator.userAgent.match(/Aloha/i)},any:function(){return c.Chrome()||c.Safari()||c.Edge()||c.Firefox()||c.Opera()||c.Brave()||c.Aloha()}};const u=Object.values(a.CallbackBrowser).filter((r=>r.startsWith("iOS"))),l=Object.values(a.CallbackBrowser).filter((r=>r.startsWith("android"))),f=Object.values(a.CallbackBrowser).filter((r=>r.startsWith("macOS")));exports.ERR_NEED_RETRY=o,exports.ERR_OPERATION_CANCELLED=t,exports.canOpenCallbackBrowser=function(r){const{identityStatus:e,browser:t}=r;return e?.platform===a.POMPlatform.iOS&&u.includes(t)||e?.platform===a.POMPlatform.Android&&l.includes(t)||e?.platform===a.POMPlatform.macOS&&f.includes(t)},exports.decodeMessageData=i,exports.encodeMessageData=n,exports.getCallbackBrowser=function(){if(s.Android()){if(c.Chrome())return a.CallbackBrowser.androidChrome;if(c.Edge())return a.CallbackBrowser.androidEdge;if(c.Firefox())return a.CallbackBrowser.androidFirefox;if(c.Opera())return a.CallbackBrowser.androidOpera;if(c.Brave())return a.CallbackBrowser.androidBrave;if(c.Aloha())return a.CallbackBrowser.androidAloha}else if(s.iOS()){if(c.Chrome())return a.CallbackBrowser.iOSChrome;if(c.Safari())return a.CallbackBrowser.iOSSafari;if(c.Edge())return a.CallbackBrowser.iOSEdge;if(c.Firefox())return a.CallbackBrowser.iOSFirefox;if(c.Opera())return a.CallbackBrowser.iOSOpera;if(c.Brave())return a.CallbackBrowser.iOSBrave;if(c.Aloha())return a.CallbackBrowser.iOSAloha}else if(s.macOS()){if(c.Chrome())return a.CallbackBrowser.macOSChrome;if(c.Safari())return a.CallbackBrowser.macOSSafari;if(c.Edge())return a.CallbackBrowser.macOSEdge;if(c.Firefox())return a.CallbackBrowser.macOSFirefox;if(c.Opera())return a.CallbackBrowser.macOSOpera;if(c.Brave())return a.CallbackBrowser.macOSBrave;if(c.Aloha())return a.CallbackBrowser.macOSAloha}},exports.isBrowser=c,exports.isDevice=s,exports.runWithErrorRetry=async function(r,e){const{signal:a,maxRetry:t=5,throttleInterval:n=3e3,backoffStrategyFactor:i,timeoutError:s}=e||{};let c=0,u=!0,l=!1;for(a?.addEventListener("abort",(function r(){l=!0,a?.removeEventListener("abort",r)}));u&&!l;){u=!1;try{return await r({retryCount:c})}catch(r){if(r===o&&c<t&&!l){c+=1,u=!0,await new Promise((r=>{setTimeout((()=>{r()}),n*(i?c*i:1))}));continue}throw r}}throw s||new Error("runWithErrorRetry no result")},exports.sendMessageAndWaitForResponse=function(r){const{socket:e,op:a,data:o,resolver:s,rejecter:c}=r,u=n({op:a,data:o});function l(r){e.off(f),c(r)}const f={success(r){try{const t=i(r);t.id===u.id&&t.op===a&&(t.success?(e.off(f),s(t.data)):l(new Error(t.error||"Unknown error")))}catch(r){l(r)}},error:l};return e.on(f),e.send(u.data),()=>{l(t)}}; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@automata-network/pom-js-sdk", | ||
"version": "0.0.77", | ||
"version": "0.0.78", | ||
"description": "A socket clinet for automata's Proof Of Machinehood", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -23,2 +23,3 @@ import { | ||
getCallbackBrowser, | ||
canOpenCallbackBrowser, | ||
} from './util'; | ||
@@ -25,0 +26,0 @@ |
import {POMSocket, createRandomString} from '@automata-network/pom-socket'; | ||
import {CallbackBrowser} from '.'; | ||
import {CallbackBrowser, IdentityStatusResult, POMPlatform} from '.'; | ||
@@ -275,1 +275,27 @@ export enum POMOperation { | ||
} | ||
const iOSBrowsers = Object.values(CallbackBrowser).filter(item => | ||
item.startsWith('iOS'), | ||
); | ||
const androidBrowsers = Object.values(CallbackBrowser).filter(item => | ||
item.startsWith('android'), | ||
); | ||
const macOSBrowsers = Object.values(CallbackBrowser).filter(item => | ||
item.startsWith('macOS'), | ||
); | ||
export function canOpenCallbackBrowser(options: { | ||
identityStatus?: IdentityStatusResult; | ||
browser: CallbackBrowser; | ||
}) { | ||
const {identityStatus, browser} = options; | ||
return ( | ||
(identityStatus?.platform === POMPlatform.iOS && | ||
iOSBrowsers.includes(browser)) || | ||
(identityStatus?.platform === POMPlatform.Android && | ||
androidBrowsers.includes(browser)) || | ||
(identityStatus?.platform === POMPlatform.macOS && | ||
macOSBrowsers.includes(browser)) | ||
); | ||
} |
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
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
71940
982