@automata-network/pom-js-sdk
Advanced tools
Comparing version 0.0.76 to 0.0.77
@@ -147,2 +147,3 @@ import * as _automata_network_pom_socket from '@automata-network/pom-socket'; | ||
} | ||
declare const pomAppDeepLinkProtocol = "pom:"; | ||
declare const ERR_INVALID_DEEP_LINK: Error; | ||
@@ -172,2 +173,2 @@ declare const ERR_INVALID_DEEP_LINK_PARAMETERS: Error; | ||
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 }; | ||
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 }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e,r,a,t,o=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,(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 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=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"),h=new Error("identity is empty");class O{pomSocket;constructor(...[e,r]){this.pomSocket=o.getSocket(e,r)}static generateDeepLink(e){const{sessionId:r,dhSharedPublicNumber:a,hkdfSalt:t,callbackBrowser:o,callbackDeepLinkUrl:s}=e,i=new URL(`${d}//${c}`);return i.hash="download",r&&i.searchParams.set("id",r),a&&i.searchParams.set("dh",a),t&&i.searchParams.set("s",t),o&&i.searchParams.set("cb",o),s&&i.searchParams.set("cu",s),i.toString()}static parseDeepLinkQuery(e){const r=new URL(e);if(r.hostname!==c||r.protocol!==d)throw l;const a=r.searchParams.get("id"),t=r.searchParams.get("dh"),o=r.searchParams.get("s"),s=r.searchParams.get("cu"),i=r.searchParams.get("cb");if(null!=a&&"string"!=typeof a||null!=t&&"string"!=typeof t||null!=o&&"string"!=typeof o||null!=s&&"string"!=typeof s||null!=i&&"string"!=typeof i)throw p;return{id:a,dh:t,s:o,cu:s,cb:i}}static async generateQrcode(e,r){const{sessionId:a,dhSharedPublicNumber:t,hkdfSalt:o}=e,i=[t?{data:t.toUpperCase(),mode:"alphanumeric"}:void 0,o?{data:o,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:t,qrcodeLevel:s,onRenderQrcode:n,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;let l=this.pomSocket.getSocketStatus();if(l===o.SocketStatus.Connecting)throw m;l===o.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:s,onRenderQrcode:n});const p=await this.getIdentityStatus({chainId:r}).promise;let S;if(p.status!==exports.IdentityStatus.None&&p.status!==exports.IdentityStatus.Generated||(S=await this.attestDevice({chainId:r,caller:a}).promise),p.status===exports.IdentityStatus.None||p.status===exports.IdentityStatus.Generated){if(null==S)throw h;await d(S),await i.runWithErrorRetry((()=>{const e=c({chainId:r,identity:S.identity});if(!e)throw i.ERR_NEED_RETRY;return e}))}return await this.signMessage({chainId:r,caller:a,message:t}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:r,onRenderQrcode:a}=e,t=await this.connectSocket(),o=O.generateDeepLink(t);a({deepLink:o,imageBase64:await O.generateQrcode(t,r?{level:r}:void 0)}),await this.waitForConnectedEvent(),await this.waitForKeyExchangeEvent()}catch(r){if(r!==o.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,t)=>{a=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.GetIdentityStatus,data:{chainId:r},resolver:e,rejecter:t})})),cancel:a}}attestDevice(e){let r;return{promise:new Promise(((a,t)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.AttestDevice,data:e,resolver:a,rejecter:t})})),cancel:r}}signMessage(e){let r;return{promise:new Promise(((a,t)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.SignMessage,data:e,resolver:a,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.getCallbackBrowser=i.getCallbackBrowser,exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=S,exports.ERR_IDENTITY_IS_EMPTY=h,exports.ERR_INVALID_DEEP_LINK=l,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=p,exports.ERR_SOCKET_IS_CONNECTING=m,exports.POMClinet=O,exports.bundleIds=n; | ||
"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; | ||
//# sourceMappingURL=index.js.map |
@@ -43,2 +43,18 @@ import { POMSocket } from '@automata-network/pom-socket'; | ||
export declare function setTimer(callback: () => void | Promise<void>, throttleInterval: number, backoffStrategyFactor?: number): () => void; | ||
export declare const isDevice: { | ||
Android: () => RegExpMatchArray | null; | ||
macOS: () => RegExpMatchArray | null; | ||
iOS: () => RegExpMatchArray | null; | ||
any: () => RegExpMatchArray | null; | ||
}; | ||
export declare const isBrowser: { | ||
Chrome: () => RegExpMatchArray | null; | ||
Safari: () => boolean | null; | ||
Edge: () => RegExpMatchArray | null; | ||
Firefox: () => RegExpMatchArray | null; | ||
Opera: () => RegExpMatchArray | null; | ||
Brave: () => RegExpMatchArray | null; | ||
Aloha: () => RegExpMatchArray | null; | ||
any: () => true | RegExpMatchArray | null; | ||
}; | ||
export declare function getCallbackBrowser(): CallbackBrowser | undefined; |
@@ -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"),n=new Error("need retry");function o(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)}exports.ERR_NEED_RETRY=n,exports.ERR_OPERATION_CANCELLED=t,exports.decodeMessageData=i,exports.encodeMessageData=o,exports.getCallbackBrowser=function(){const r={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 r.Android()||r.macOS()||r.iOS()}},e={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 e.Chrome()||e.Safari()||e.Edge()||e.Firefox()||e.Opera()||e.Brave()||e.Aloha()}};if(r.Android()){if(e.Chrome())return a.CallbackBrowser.androidChrome;if(e.Edge())return a.CallbackBrowser.androidEdge;if(e.Firefox())return a.CallbackBrowser.androidFirefox;if(e.Opera())return a.CallbackBrowser.androidOpera;if(e.Brave())return a.CallbackBrowser.androidBrave;if(e.Aloha())return a.CallbackBrowser.androidAloha}else if(r.iOS()){if(e.Chrome())return a.CallbackBrowser.iOSChrome;if(e.Safari())return a.CallbackBrowser.iOSSafari;if(e.Edge())return a.CallbackBrowser.iOSEdge;if(e.Firefox())return a.CallbackBrowser.iOSFirefox;if(e.Opera())return a.CallbackBrowser.iOSOpera;if(e.Brave())return a.CallbackBrowser.iOSBrave;if(e.Aloha())return a.CallbackBrowser.iOSAloha}else if(r.macOS()){if(e.Chrome())return a.CallbackBrowser.macOSChrome;if(e.Safari())return a.CallbackBrowser.macOSSafari;if(e.Edge())return a.CallbackBrowser.macOSEdge;if(e.Firefox())return a.CallbackBrowser.macOSFirefox;if(e.Opera())return a.CallbackBrowser.macOSOpera;if(e.Brave())return a.CallbackBrowser.macOSBrave;if(e.Aloha())return a.CallbackBrowser.macOSAloha}},exports.runWithErrorRetry=async function(r,e){const{signal:a,maxRetry:t=5,throttleInterval:o=3e3,backoffStrategyFactor:i,timeoutError:c}=e||{};let s=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:s})}catch(r){if(r===n&&s<t&&!f){s+=1,u=!0,await new Promise((r=>{setTimeout((()=>{r()}),o*(i?s*i:1))}));continue}throw r}}throw c||new Error("runWithErrorRetry no result")},exports.sendMessageAndWaitForResponse=function(r){const{socket:e,op:a,data:n,resolver:c,rejecter:s}=r,u=o({op:a,data:n});function f(r){e.off(l),s(r)}const l={success(r){try{const t=i(r);t.id===u.id&&t.op===a&&(t.success?(e.off(l),c(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()}};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)}}; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@automata-network/pom-js-sdk", | ||
"version": "0.0.76", | ||
"version": "0.0.77", | ||
"description": "A socket clinet for automata's Proof Of Machinehood", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -13,2 +13,4 @@ import { | ||
ERR_NEED_RETRY, | ||
isDevice, | ||
isBrowser, | ||
} from './util'; | ||
@@ -194,2 +196,3 @@ | ||
const pomAppHash = 'download'; | ||
export const pomAppDeepLinkProtocol = 'pom:'; | ||
@@ -224,4 +227,11 @@ export const ERR_INVALID_DEEP_LINK = new Error('deep link is invalid'); | ||
const url = new URL(`${pomAppProtocol}//${pomAppHost}`); | ||
const isMac = isDevice.macOS(); | ||
const isSafari = isBrowser.Safari(); | ||
const url = new URL( | ||
`${ | ||
isMac && !isSafari ? pomAppDeepLinkProtocol : pomAppProtocol | ||
}//${pomAppHost}`, | ||
); | ||
url.hash = pomAppHash; | ||
@@ -255,3 +265,7 @@ | ||
if (url.hostname !== pomAppHost || url.protocol !== pomAppProtocol) { | ||
if ( | ||
url.hostname !== pomAppHost || | ||
(url.protocol !== pomAppProtocol && | ||
url.protocol !== pomAppDeepLinkProtocol) | ||
) { | ||
throw ERR_INVALID_DEEP_LINK; | ||
@@ -258,0 +272,0 @@ } |
111
src/util.ts
@@ -171,59 +171,58 @@ import {POMSocket, createRandomString} from '@automata-network/pom-socket'; | ||
export const isDevice = { | ||
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 isDevice.Android() || isDevice.macOS() || isDevice.iOS(); | ||
}, | ||
}; | ||
export const isBrowser = { | ||
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 ( | ||
isBrowser.Chrome() || | ||
isBrowser.Safari() || | ||
isBrowser.Edge() || | ||
isBrowser.Firefox() || | ||
isBrowser.Opera() || | ||
isBrowser.Brave() || | ||
isBrowser.Aloha() | ||
); | ||
}, | ||
}; | ||
export function getCallbackBrowser() { | ||
const isDevice = { | ||
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 isDevice.Android() || isDevice.macOS() || isDevice.iOS(); | ||
}, | ||
}; | ||
const isBrowser = { | ||
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 ( | ||
isBrowser.Chrome() || | ||
isBrowser.Safari() || | ||
isBrowser.Edge() || | ||
isBrowser.Firefox() || | ||
isBrowser.Opera() || | ||
isBrowser.Brave() || | ||
isBrowser.Aloha() | ||
); | ||
}, | ||
}; | ||
if (isDevice.Android()) { | ||
@@ -230,0 +229,0 @@ if (isBrowser.Chrome()) { |
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
68700
945