@automata-network/pom-js-sdk
Advanced tools
Comparing version 0.0.24 to 0.0.25
@@ -1,2 +0,3 @@ | ||
import { POMSocket, getSocket, SocketConnectionResult } from '@automata-network/pom-socket'; | ||
import * as _automata_network_pom_socket from '@automata-network/pom-socket'; | ||
import { POMSocket, getSocket } from '@automata-network/pom-socket'; | ||
@@ -71,7 +72,11 @@ declare enum POMOperation { | ||
} | ||
interface DeepLinkQuery { | ||
id: string; | ||
dh: string; | ||
s: string; | ||
interface QrcodeCallbackQuery { | ||
cbUrl?: string; | ||
cbBrowser?: string; | ||
} | ||
interface DeepLinkQuery extends QrcodeCallbackQuery { | ||
id?: string; | ||
dh?: string; | ||
s?: string; | ||
} | ||
interface SignatureOptions { | ||
@@ -101,2 +106,17 @@ chainId: ChainId; | ||
} | ||
declare enum CallbackBrowser { | ||
Chrome = "chrome", | ||
Safari = "safari", | ||
Edge = "edge", | ||
Firefox = "firefox", | ||
Opera = "opera", | ||
Brave = "brave" | ||
} | ||
interface QrcodeGenerationRequest { | ||
sessionId?: string; | ||
dhSharedPublicNumber?: string; | ||
hkdfSalt?: string; | ||
callbackDeepLinkUrl?: string; | ||
callbackBrowser?: CallbackBrowser; | ||
} | ||
declare const ERR_INVALID_DEEP_LINK: Error; | ||
@@ -110,3 +130,3 @@ declare const ERR_INVALID_DEEP_LINK_PARAMETERS: Error; | ||
constructor(...[socketUrl, options]: Parameters<typeof getSocket>); | ||
static generateDeepLink(options: SocketConnectionResult): string; | ||
static generateDeepLink(options: QrcodeGenerationRequest): string; | ||
static parseDeepLinkQuery(deepLink: string): DeepLinkQuery; | ||
@@ -118,3 +138,3 @@ static generateQrcode(deepLink: string, options?: { | ||
private connectSocketAndWait; | ||
connectSocket(): Promise<SocketConnectionResult>; | ||
connectSocket(): Promise<_automata_network_pom_socket.SocketConnectionResult>; | ||
waitForConnectedEvent(): Promise<void>; | ||
@@ -127,2 +147,2 @@ waitForKeyExchangeEvent(): Promise<void>; | ||
export { type AttestDeviceOptions, type AttestDeviceResult, ChainId, type ChainIdOptions, 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 RenderQrcodeData, type SignMessageOptions, type SignatureOptions, type SignatureResult, decodeMessageData, encodeMessageData }; | ||
export { type AttestDeviceOptions, type AttestDeviceResult, CallbackBrowser, ChainId, type ChainIdOptions, 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 QrcodeCallbackQuery, type QrcodeGenerationRequest, type RenderQrcodeData, type SignMessageOptions, type SignatureOptions, type SignatureResult, decodeMessageData, encodeMessageData }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e,t,n,r=require("@automata-network/pom-socket"),o=require("qs"),s=require("@nuintun/qrcode"),i=require("./util.js");exports.ChainId=void 0,(e=exports.ChainId||(exports.ChainId={})).Ethereum="0x1",e.LineaSepolia="0xe705",exports.POMPlatform=void 0,(t=exports.POMPlatform||(exports.POMPlatform={})).Android="android",t.iOS="iOS",t.macOS="macOS",exports.IdentityStatus=void 0,(n=exports.IdentityStatus||(exports.IdentityStatus={})).None="none",n.Generated="generated",n.Registered="registered";const a="pom://pomrn.ata.network/connect",c=new Error("deep link is invalid"),d=new Error("deep link parameters is invalid"),p=new Error("socket is connecting"),E=new Error("get identity status timeout"),u=new Error("identity is empty");class S{pomSocket;constructor(...[e,t]){this.pomSocket=r.getSocket(e,t)}static generateDeepLink(e){const{sessionId:t,dhSharedPublicNumber:n,hkdfSalt:r}=e,s={id:t,dh:n,s:r};return`${a}?${o.stringify(s)}`}static parseDeepLinkQuery(e){const[t,n]=e.split("?");if(t!==a)throw c;const r=o.parse(n);if("string"!=typeof r.id||"string"!=typeof r.dh||"string"!=typeof r.s||""===r.id||""===r.dh||""===r.s)throw d;return o.parse(n)}static generateQrcode(e,t){return new s.Encoder({level:t?.level||"M"}).encode(new s.Byte(e)).toDataURL()}async sign(e){const{chainId:t,caller:n,message:o,qrcodeLevel:s,onRenderQrcode:a,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;if(this.pomSocket.status===r.SocketStatus.Connecting)throw p;this.pomSocket.status===r.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:s,onRenderQrcode:a});const E=await this.getIdentityStatus({chainId:t}).promise;let S;if(E.status!==exports.IdentityStatus.None&&E.status!==exports.IdentityStatus.Generated||(S=await this.attestDevice({chainId:t,caller:n}).promise),E.status===exports.IdentityStatus.None||E.status===exports.IdentityStatus.Generated){if(null==S)throw u;await d(S),await i.runWithErrorRetry((()=>{const e=c({chainId:t,identity:S.identity});if(!e)throw i.ERR_NEED_RETRY;return e}))}return await this.signMessage({chainId:t,caller:n,message:o}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:t,onRenderQrcode:n}=e,r=await this.connectSocket(),o=S.generateDeepLink(r);n({deepLink:o,imageBase64:S.generateQrcode(o,t?{level:t}:void 0)}),await this.waitForConnectedEvent(),await this.waitForKeyExchangeEvent()}catch(t){if(t!==r.ERR_SESSION_IS_EXPIRED)throw t;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:t}=e;let n;return{promise:new Promise(((e,r)=>{n=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.GetIdentityStatus,data:{chainId:t},resolver:e,rejecter:r})})),cancel:n}}attestDevice(e){let t;return{promise:new Promise(((n,r)=>{t=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.AttestDevice,data:e,resolver:n,rejecter:r})})),cancel:t}}signMessage(e){let t;return{promise:new Promise(((n,r)=>{t=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.SignMessage,data:e,resolver:n,rejecter:r})})),cancel:t}}}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=E,exports.ERR_IDENTITY_IS_EMPTY=u,exports.ERR_INVALID_DEEP_LINK=c,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=d,exports.ERR_SOCKET_IS_CONNECTING=p,exports.POMClinet=S; | ||
"use strict";var e,t,r,o,n=require("@automata-network/pom-socket"),s=require("qs"),a=require("@nuintun/qrcode"),i=require("./util.js");exports.ChainId=void 0,(e=exports.ChainId||(exports.ChainId={})).Ethereum="0x1",e.LineaSepolia="0xe705",exports.POMPlatform=void 0,(t=exports.POMPlatform||(exports.POMPlatform={})).Android="android",t.iOS="iOS",t.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={})).Chrome="chrome",o.Safari="safari",o.Edge="edge",o.Firefox="firefox",o.Opera="opera",o.Brave="brave";const c="pom://pomrn.ata.network/connect",d=new Error("deep link is invalid"),p=new Error("deep link parameters is invalid"),l=new Error("socket is connecting"),u=new Error("get identity status timeout"),E=new Error("identity is empty");class S{pomSocket;constructor(...[e,t]){this.pomSocket=n.getSocket(e,t)}static generateDeepLink(e){const{sessionId:t,dhSharedPublicNumber:r,hkdfSalt:o,callbackBrowser:n,callbackDeepLinkUrl:a}=e,i={id:t,dh:r,s:o,cbBrowser:n,cbUrl:a};return`${c}?${s.stringify(i)}`}static parseDeepLinkQuery(e){const[t,r]=e.split("?");if(t!==c)throw d;const o=s.parse(r);if(null!=o.id&&"string"!=typeof o.id||null!=o.dh&&"string"!=typeof o.dh||null!=o.s&&"string"!=typeof o.s||null!=o.cbUrl&&"string"!=typeof o.cbUrl||null!=o.cbBrowser&&"string"!=typeof o.cbBrowser)throw p;return o}static generateQrcode(e,t){return new a.Encoder({level:t?.level||"M"}).encode(new a.Byte(e)).toDataURL()}async sign(e){const{chainId:t,caller:r,message:o,qrcodeLevel:s,onRenderQrcode:a,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;if(this.pomSocket.status===n.SocketStatus.Connecting)throw l;this.pomSocket.status===n.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:s,onRenderQrcode:a});const p=await this.getIdentityStatus({chainId:t}).promise;let u;if(p.status!==exports.IdentityStatus.None&&p.status!==exports.IdentityStatus.Generated||(u=await this.attestDevice({chainId:t,caller:r}).promise),p.status===exports.IdentityStatus.None||p.status===exports.IdentityStatus.Generated){if(null==u)throw E;await d(u),await i.runWithErrorRetry((()=>{const e=c({chainId:t,identity:u.identity});if(!e)throw i.ERR_NEED_RETRY;return e}))}return await this.signMessage({chainId:t,caller:r,message:o}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:t,onRenderQrcode:r}=e,o=await this.connectSocket(),n=S.generateDeepLink(o);r({deepLink:n,imageBase64:S.generateQrcode(n,t?{level:t}:void 0)}),await this.waitForConnectedEvent(),await this.waitForKeyExchangeEvent()}catch(t){if(t!==n.ERR_SESSION_IS_EXPIRED)throw t;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:t}=e;let r;return{promise:new Promise(((e,o)=>{r=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.GetIdentityStatus,data:{chainId:t},resolver:e,rejecter:o})})),cancel:r}}attestDevice(e){let t;return{promise:new Promise(((r,o)=>{t=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.AttestDevice,data:e,resolver:r,rejecter:o})})),cancel:t}}signMessage(e){let t;return{promise:new Promise(((r,o)=>{t=i.sendMessageAndWaitForResponse({socket:this.pomSocket,op:i.POMOperation.SignMessage,data:e,resolver:r,rejecter:o})})),cancel:t}}}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=u,exports.ERR_IDENTITY_IS_EMPTY=E,exports.ERR_INVALID_DEEP_LINK=d,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=p,exports.ERR_SOCKET_IS_CONNECTING=l,exports.POMClinet=S; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@automata-network/pom-js-sdk", | ||
"version": "0.0.24", | ||
"version": "0.0.25", | ||
"description": "A socket clinet for automata's Proof Of Machinehood", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { | ||
ERR_SESSION_IS_EXPIRED, | ||
POMSocket, | ||
SocketConnectionResult, | ||
SocketStatus, | ||
@@ -78,8 +77,13 @@ getSocket, | ||
export interface DeepLinkQuery { | ||
id: string; | ||
dh: string; | ||
s: string; | ||
export interface QrcodeCallbackQuery { | ||
cbUrl?: string; | ||
cbBrowser?: string; | ||
} | ||
export interface DeepLinkQuery extends QrcodeCallbackQuery { | ||
id?: string; | ||
dh?: string; | ||
s?: string; | ||
} | ||
export interface SignatureOptions { | ||
@@ -116,2 +120,19 @@ chainId: ChainId; | ||
export enum CallbackBrowser { | ||
Chrome = 'chrome', | ||
Safari = 'safari', | ||
Edge = 'edge', | ||
Firefox = 'firefox', | ||
Opera = 'opera', | ||
Brave = 'brave', | ||
} | ||
export interface QrcodeGenerationRequest { | ||
sessionId?: string; | ||
dhSharedPublicNumber?: string; | ||
hkdfSalt?: string; | ||
callbackDeepLinkUrl?: string; | ||
callbackBrowser?: CallbackBrowser; | ||
} | ||
const pomAppBasePath = `pom://pomrn.ata.network/connect`; | ||
@@ -138,4 +159,11 @@ | ||
static generateDeepLink(options: SocketConnectionResult): string { | ||
const {sessionId, dhSharedPublicNumber, hkdfSalt} = options; | ||
static generateDeepLink(options: QrcodeGenerationRequest): string { | ||
const { | ||
sessionId, | ||
dhSharedPublicNumber, | ||
hkdfSalt, | ||
callbackBrowser, | ||
callbackDeepLinkUrl, | ||
} = options; | ||
const query: DeepLinkQuery = { | ||
@@ -145,2 +173,4 @@ id: sessionId, | ||
s: hkdfSalt, | ||
cbBrowser: callbackBrowser, | ||
cbUrl: callbackDeepLinkUrl, | ||
}; | ||
@@ -161,8 +191,7 @@ | ||
if ( | ||
typeof parsedQs.id !== 'string' || | ||
typeof parsedQs.dh !== 'string' || | ||
typeof parsedQs.s !== 'string' || | ||
parsedQs.id === '' || | ||
parsedQs.dh === '' || | ||
parsedQs.s === '' | ||
(parsedQs.id != null && typeof parsedQs.id !== 'string') || | ||
(parsedQs.dh != null && typeof parsedQs.dh !== 'string') || | ||
(parsedQs.s != null && typeof parsedQs.s !== 'string') || | ||
(parsedQs.cbUrl != null && typeof parsedQs.cbUrl !== 'string') || | ||
(parsedQs.cbBrowser != null && typeof parsedQs.cbBrowser !== 'string') | ||
) { | ||
@@ -172,3 +201,3 @@ throw ERR_INVALID_DEEP_LINK_PARAMETERS; | ||
return parse(query) as unknown as DeepLinkQuery; | ||
return parsedQs as unknown as DeepLinkQuery; | ||
} | ||
@@ -175,0 +204,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
43413
661