@automata-network/pom-js-sdk
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -42,3 +42,3 @@ import { POMSocket, getSocket, SocketConnectionResult } from '@automata-network/pom-socket'; | ||
} | ||
interface AttestDeviceData { | ||
interface AttestDeviceResult { | ||
platform: POMPlatform; | ||
@@ -57,3 +57,2 @@ chainId: ChainId; | ||
status: IdentityStatus; | ||
payload?: AttestDeviceData; | ||
} | ||
@@ -76,3 +75,3 @@ interface MessageResult<T> { | ||
onGetIdentityOnChain: (options: GetIdentityOnChainOptions) => Promise<boolean>; | ||
onRegisterIdentity: (data: AttestDeviceData) => Promise<void>; | ||
onRegisterIdentity: (data: AttestDeviceResult) => Promise<void>; | ||
} | ||
@@ -89,3 +88,2 @@ interface ChainIdOptions { | ||
interface GetIdentityStatusOptions extends ChainIdOptions { | ||
onGetIdentityOnChain: (options: GetIdentityOnChainOptions) => Promise<boolean>; | ||
} | ||
@@ -115,6 +113,6 @@ interface SignMessageOptions extends ChainIdOptions { | ||
getIdentityStatus(options: GetIdentityStatusOptions): MessageResult<IdentityStatusResult>; | ||
attestDevice(options: AttestDeviceOptions): MessageResult<AttestDeviceData>; | ||
signMessage(options: SignMessageOptions): MessageResult<string>; | ||
attestDevice(options: AttestDeviceOptions): MessageResult<AttestDeviceResult>; | ||
signMessage(options: SignMessageOptions): MessageResult<SignatureResult>; | ||
} | ||
export { type AttestDeviceData, type AttestDeviceOptions, 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 IdentityStatusResult, type MessageResult, POMClinet, POMOperation, POMPlatform, type RenderQrcodeData, type SignMessageOptions, type SignatureOptions, type SignatureResult, decodeMessageData, encodeMessageData }; | ||
export { type AttestDeviceOptions, type AttestDeviceResult, 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 IdentityStatusResult, type MessageResult, POMClinet, POMOperation, POMPlatform, type RenderQrcodeData, type SignMessageOptions, type SignatureOptions, type SignatureResult, decodeMessageData, encodeMessageData }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e,t,n=require("@automata-network/pom-socket"),r=require("qs"),s=require("@nuintun/qrcode"),o=require("./util.js"),i=require("@automata-network/pom-web3");exports.POMPlatform=void 0,(e=exports.POMPlatform||(exports.POMPlatform={})).Android="android",e.iOS="iOS",e.macOS="macOS",exports.IdentityStatus=void 0,(t=exports.IdentityStatus||(exports.IdentityStatus={})).None="none",t.Generated="generated",t.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"),u=new Error("get identity status timeout"),S=new Error("identity is empty");class E{pomSocket;constructor(...[e,t]){this.pomSocket=n.getSocket(e,t)}static generateDeepLink(e){const{sessionId:t,dhSharedPublicNumber:n,hkdfSalt:s}=e,o={id:t,dh:n,s:s};return`${a}?${r.stringify(o)}`}static parseDeepLinkQuery(e){const[t,n]=e.split("?");if(t!==a)throw c;const s=r.parse(n);if("string"!=typeof s.id||"string"!=typeof s.dh||"string"!=typeof s.s||""===s.id||""===s.dh||""===s.s)throw d;return r.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:r,message:s,qrcodeLevel:i,onRenderQrcode:a,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;if(this.pomSocket.status===n.SocketStatus.Connecting)throw p;this.pomSocket.status===n.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:i,onRenderQrcode:a});const u=await this.getIdentityStatus({chainId:t,onGetIdentityOnChain:c}).promise;let E;if(u.status===exports.IdentityStatus.None?E=await this.attestDevice({chainId:t,caller:r}).promise:u.status!==exports.IdentityStatus.Generated&&u.status!==exports.IdentityStatus.Registered||(E=u.payload),null==E)throw S;u.status!==exports.IdentityStatus.None&&u.status!==exports.IdentityStatus.Generated||(await d(E),await o.runWithErrorRetry((()=>{const e=c({chainId:t,identity:E.identity});if(!e)throw o.ERR_NEED_RETRY;return e})));return{signature:await this.signMessage({chainId:t,caller:r,message:s}).promise,...E}}async connectSocketAndWait(e){try{const{qrcodeLevel:t,onRenderQrcode:n}=e,r=await this.connectSocket(),s=E.generateDeepLink(r);n({deepLink:s,imageBase64:E.generateQrcode(s,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,onGetIdentityOnChain:n}=e;let r;return{promise:new Promise(((e,s)=>{r=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.GetIdentityStatus,data:{chainId:t},resolver:r=>{r?n({chainId:t,identity:r.identity}).then((t=>{e({status:t?exports.IdentityStatus.Registered:exports.IdentityStatus.Generated,payload:r})})).catch((e=>{s(e)})):e({status:exports.IdentityStatus.None})},rejecter:s})})),cancel:r}}attestDevice(e){let t;return{promise:new Promise(((n,r)=>{t=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.AttestDevice,data:e,resolver:n,rejecter:r})})),cancel:t}}signMessage(e){let t;return{promise:new Promise(((n,r)=>{t=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.SignMessage,data:e,resolver:n,rejecter:r})})),cancel:t}}}exports.ERR_OPERATION_CANCELLED=o.ERR_OPERATION_CANCELLED,Object.defineProperty(exports,"POMOperation",{enumerable:!0,get:function(){return o.POMOperation}}),exports.decodeMessageData=o.decodeMessageData,exports.encodeMessageData=o.encodeMessageData,Object.defineProperty(exports,"ChainId",{enumerable:!0,get:function(){return i.ChainId}}),exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=u,exports.ERR_IDENTITY_IS_EMPTY=S,exports.ERR_INVALID_DEEP_LINK=c,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=d,exports.ERR_SOCKET_IS_CONNECTING=p,exports.POMClinet=E; | ||
"use strict";var e,t,n=require("@automata-network/pom-socket"),r=require("qs"),s=require("@nuintun/qrcode"),o=require("./util.js"),i=require("@automata-network/pom-web3");exports.POMPlatform=void 0,(e=exports.POMPlatform||(exports.POMPlatform={})).Android="android",e.iOS="iOS",e.macOS="macOS",exports.IdentityStatus=void 0,(t=exports.IdentityStatus||(exports.IdentityStatus={})).None="none",t.Generated="generated",t.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"),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:n,hkdfSalt:s}=e,o={id:t,dh:n,s:s};return`${a}?${r.stringify(o)}`}static parseDeepLinkQuery(e){const[t,n]=e.split("?");if(t!==a)throw c;const s=r.parse(n);if("string"!=typeof s.id||"string"!=typeof s.dh||"string"!=typeof s.s||""===s.id||""===s.dh||""===s.s)throw d;return r.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:r,message:s,qrcodeLevel:i,onRenderQrcode:a,onGetIdentityOnChain:c,onRegisterIdentity:d}=e;if(this.pomSocket.status===n.SocketStatus.Connecting)throw p;this.pomSocket.status===n.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:i,onRenderQrcode:a});const u=await this.getIdentityStatus({chainId:t}).promise;let S;if(u.status!==exports.IdentityStatus.None&&u.status!==exports.IdentityStatus.Generated||(S=await this.attestDevice({chainId:t,caller:r}).promise),u.status===exports.IdentityStatus.None||u.status===exports.IdentityStatus.Generated){if(null==S)throw E;await d(S),await o.runWithErrorRetry((()=>{const e=c({chainId:t,identity:S.identity});if(!e)throw o.ERR_NEED_RETRY;return e}))}return await this.signMessage({chainId:t,caller:r,message:s}).promise}async connectSocketAndWait(e){try{const{qrcodeLevel:t,onRenderQrcode:n}=e,r=await this.connectSocket(),s=S.generateDeepLink(r);n({deepLink:s,imageBase64:S.generateQrcode(s,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 n;return{promise:new Promise(((e,r)=>{n=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.GetIdentityStatus,data:{chainId:t},resolver:t=>{e(t?{status:t.status}:{status:exports.IdentityStatus.None})},rejecter:r})})),cancel:n}}attestDevice(e){let t;return{promise:new Promise(((n,r)=>{t=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.AttestDevice,data:e,resolver:n,rejecter:r})})),cancel:t}}signMessage(e){let t;return{promise:new Promise(((n,r)=>{t=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.SignMessage,data:e,resolver:n,rejecter:r})})),cancel:t}}}exports.ERR_OPERATION_CANCELLED=o.ERR_OPERATION_CANCELLED,Object.defineProperty(exports,"POMOperation",{enumerable:!0,get:function(){return o.POMOperation}}),exports.decodeMessageData=o.decodeMessageData,exports.encodeMessageData=o.encodeMessageData,Object.defineProperty(exports,"ChainId",{enumerable:!0,get:function(){return i.ChainId}}),exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=u,exports.ERR_IDENTITY_IS_EMPTY=E,exports.ERR_INVALID_DEEP_LINK=c,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=d,exports.ERR_SOCKET_IS_CONNECTING=p,exports.POMClinet=S; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@automata-network/pom-js-sdk", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "A socket clinet for automata's Proof Of Machinehood", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
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
40481
603