@automata-network/pom-js-sdk
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -9,2 +9,17 @@ import { POMSocket, getSocket, SocketConnectionResult } from '@automata-network/pom-socket'; | ||
declare const ERR_OPERATION_CANCELLED: Error; | ||
declare function encodeMessageData<T>(options: { | ||
op: POMOperation; | ||
data: T; | ||
id?: string; | ||
}): { | ||
id: string; | ||
data: string; | ||
}; | ||
declare function decodeMessageData<R>(data: string): { | ||
id: string; | ||
op: POMOperation; | ||
data: R; | ||
success: boolean; | ||
error?: string; | ||
}; | ||
@@ -94,2 +109,2 @@ declare enum POMPlatform { | ||
export { type AttestDeviceData, 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 }; | ||
export { type AttestDeviceData, 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"),s=require("qs"),r=require("@nuintun/qrcode"),o=require("./util.js");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 i="pom://pomrn.ata.network/connect",a=new Error("deep link is invalid"),c=new Error("deep link parameters is invalid"),d=new Error("socket is connecting"),p=new Error("get identity status timeout"),u=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:r}=e,o={id:t,dh:n,s:r};return`${i}?${s.stringify(o)}`}static parseDeepLinkQuery(e){const[t,n]=e.split("?");if(t!==i)throw a;const r=s.parse(n);if("string"!=typeof r.id||"string"!=typeof r.dh||"string"!=typeof r.s||""===r.id||""===r.dh||""===r.s)throw c;return s.parse(n)}static generateQrcode(e,t){return new r.Encoder({level:t?.level||"M"}).encode(new r.Byte(e)).toDataURL()}async sign(e){const{chainId:t,message:s,qrcodeLevel:r,onRenderQrcode:i,onGetIdentityOnChain:a,onRegisterIdentity:c}=e;if(this.pomSocket.status===n.SocketStatus.Connecting)throw d;this.pomSocket.status===n.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:r,onRenderQrcode:i});const p=await this.getIdentityStatus({chainId:t,onGetIdentityOnChain:a}).promise;let S;if(p.status===exports.IdentityStatus.None?S=await this.attestDevice({chainId:t}).promise:p.status!==exports.IdentityStatus.Generated&&p.status!==exports.IdentityStatus.Registered||(S=p.payload),null==S)throw u;p.status!==exports.IdentityStatus.None&&p.status!==exports.IdentityStatus.Generated||(await c(S),await o.runWithErrorRetry((()=>{const e=a({chainId:t,identity:S.identity});if(!e)throw o.ERR_NEED_RETRY;return e})));return{signature:await this.signMessage({chainId:t,message:s}).promise,...S}}async connectSocketAndWait(e){try{const{qrcodeLevel:t,onRenderQrcode:n}=e,s=await this.connectSocket(),r=S.generateDeepLink(s);n({deepLink:r,imageBase64:S.generateQrcode(r,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 s;return{promise:new Promise(((e,r)=>{s=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.GetIdentityStatus,data:{chainId:t},resolver:s=>{s?n({chainId:t,identity:s.identity}).then((t=>{e({status:t?exports.IdentityStatus.Registered:exports.IdentityStatus.Generated,payload:s})})).catch((e=>{r(e)})):e({status:exports.IdentityStatus.None})},rejecter:r})})),cancel:s}}attestDevice(e){let t;return{promise:new Promise(((n,s)=>{t=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.AttestDevice,data:e,resolver:n,rejecter:s})})),cancel:t}}signMessage(e){let t;return{promise:new Promise(((n,s)=>{t=o.sendMessageAndWaitForResponse({socket:this.pomSocket,op:o.POMOperation.SignMessage,data:e,resolver:n,rejecter:s})})),cancel:t}}}exports.ERR_OPERATION_CANCELLED=o.ERR_OPERATION_CANCELLED,Object.defineProperty(exports,"POMOperation",{enumerable:!0,get:function(){return o.POMOperation}}),exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=p,exports.ERR_IDENTITY_IS_EMPTY=u,exports.ERR_INVALID_DEEP_LINK=a,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=c,exports.ERR_SOCKET_IS_CONNECTING=d,exports.POMClinet=S; | ||
"use strict";var e,t,n=require("@automata-network/pom-socket"),s=require("qs"),o=require("@nuintun/qrcode"),r=require("./util.js");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 i="pom://pomrn.ata.network/connect",a=new Error("deep link is invalid"),c=new Error("deep link parameters is invalid"),d=new Error("socket is connecting"),p=new Error("get identity status timeout"),u=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:o}=e,r={id:t,dh:n,s:o};return`${i}?${s.stringify(r)}`}static parseDeepLinkQuery(e){const[t,n]=e.split("?");if(t!==i)throw a;const o=s.parse(n);if("string"!=typeof o.id||"string"!=typeof o.dh||"string"!=typeof o.s||""===o.id||""===o.dh||""===o.s)throw c;return s.parse(n)}static generateQrcode(e,t){return new o.Encoder({level:t?.level||"M"}).encode(new o.Byte(e)).toDataURL()}async sign(e){const{chainId:t,message:s,qrcodeLevel:o,onRenderQrcode:i,onGetIdentityOnChain:a,onRegisterIdentity:c}=e;if(this.pomSocket.status===n.SocketStatus.Connecting)throw d;this.pomSocket.status===n.SocketStatus.Disconnected&&await this.connectSocketAndWait({qrcodeLevel:o,onRenderQrcode:i});const p=await this.getIdentityStatus({chainId:t,onGetIdentityOnChain:a}).promise;let S;if(p.status===exports.IdentityStatus.None?S=await this.attestDevice({chainId:t}).promise:p.status!==exports.IdentityStatus.Generated&&p.status!==exports.IdentityStatus.Registered||(S=p.payload),null==S)throw u;p.status!==exports.IdentityStatus.None&&p.status!==exports.IdentityStatus.Generated||(await c(S),await r.runWithErrorRetry((()=>{const e=a({chainId:t,identity:S.identity});if(!e)throw r.ERR_NEED_RETRY;return e})));return{signature:await this.signMessage({chainId:t,message:s}).promise,...S}}async connectSocketAndWait(e){try{const{qrcodeLevel:t,onRenderQrcode:n}=e,s=await this.connectSocket(),o=S.generateDeepLink(s);n({deepLink:o,imageBase64:S.generateQrcode(o,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 s;return{promise:new Promise(((e,o)=>{s=r.sendMessageAndWaitForResponse({socket:this.pomSocket,op:r.POMOperation.GetIdentityStatus,data:{chainId:t},resolver:s=>{s?n({chainId:t,identity:s.identity}).then((t=>{e({status:t?exports.IdentityStatus.Registered:exports.IdentityStatus.Generated,payload:s})})).catch((e=>{o(e)})):e({status:exports.IdentityStatus.None})},rejecter:o})})),cancel:s}}attestDevice(e){let t;return{promise:new Promise(((n,s)=>{t=r.sendMessageAndWaitForResponse({socket:this.pomSocket,op:r.POMOperation.AttestDevice,data:e,resolver:n,rejecter:s})})),cancel:t}}signMessage(e){let t;return{promise:new Promise(((n,s)=>{t=r.sendMessageAndWaitForResponse({socket:this.pomSocket,op:r.POMOperation.SignMessage,data:e,resolver:n,rejecter:s})})),cancel:t}}}exports.ERR_OPERATION_CANCELLED=r.ERR_OPERATION_CANCELLED,Object.defineProperty(exports,"POMOperation",{enumerable:!0,get:function(){return r.POMOperation}}),exports.decodeMessageData=r.decodeMessageData,exports.encodeMessageData=r.encodeMessageData,exports.ERR_GET_IDENTITY_STATUS_TIMEOUT=p,exports.ERR_IDENTITY_IS_EMPTY=u,exports.ERR_INVALID_DEEP_LINK=a,exports.ERR_INVALID_DEEP_LINK_PARAMETERS=c,exports.ERR_SOCKET_IS_CONNECTING=d,exports.POMClinet=S; | ||
//# sourceMappingURL=index.js.map |
@@ -19,2 +19,3 @@ import { POMSocket } from '@automata-network/pom-socket'; | ||
data: T; | ||
id?: string; | ||
}): { | ||
@@ -21,0 +22,0 @@ id: string; |
@@ -1,2 +0,2 @@ | ||
"use strict";var t,e=require("@automata-network/pom-socket");exports.POMOperation=void 0,(t=exports.POMOperation||(exports.POMOperation={})).GetIdentityStatus="getIdentityStatus",t.AttestDevice="attestDevice",t.SignMessage="signMessage";const r=new Error("operation is cancelled"),o=new Error("need retry");function n(t){const r=e.createRandomString(6);return{id:r,data:JSON.stringify({id:r,...t})}}function s(t){return JSON.parse(t)}exports.ERR_NEED_RETRY=o,exports.ERR_OPERATION_CANCELLED=r,exports.decodeMessageData=s,exports.encodeMessageData=n,exports.runWithErrorRetry=async function(t,e){const{signal:r,maxRetry:n=5,throttleInterval:s=3e3,backoffStrategyFactor:a,timeoutError:i}=e||{};let c=0,u=!0,d=!1;for(r?.addEventListener("abort",(function t(){d=!0,r?.removeEventListener("abort",t)}));u&&!d;){u=!1;try{return await t({retryCount:c})}catch(t){if(t===o&&c<n&&!d){c+=1,u=!0,await new Promise((t=>{setTimeout((()=>{t()}),s*(a?c*a:1))}));continue}throw t}}throw i||new Error("runWithErrorRetry no result")},exports.sendMessageAndWaitForResponse=function(t){const{socket:e,op:o,data:a,resolver:i,rejecter:c}=t,u=n({op:o,data:a});function d(t){e.off(p),c(t)}const p={success(t){try{const r=s(t);r.id===u.id&&r.op===o&&(r.success?(e.off(p),i(r.data)):d(new Error(r.error||"Unknown error")))}catch(t){d(t)}},error:d};return e.on(p),e.send(u.data),()=>{d(r)}}; | ||
"use strict";var t,e=require("@automata-network/pom-socket");exports.POMOperation=void 0,(t=exports.POMOperation||(exports.POMOperation={})).GetIdentityStatus="getIdentityStatus",t.AttestDevice="attestDevice",t.SignMessage="signMessage";const r=new Error("operation is cancelled"),o=new Error("need retry");function n(t){const{id:r=e.createRandomString(6)}=t;return{id:r,data:JSON.stringify({id:r,...t})}}function s(t){return JSON.parse(t)}exports.ERR_NEED_RETRY=o,exports.ERR_OPERATION_CANCELLED=r,exports.decodeMessageData=s,exports.encodeMessageData=n,exports.runWithErrorRetry=async function(t,e){const{signal:r,maxRetry:n=5,throttleInterval:s=3e3,backoffStrategyFactor:a,timeoutError:i}=e||{};let c=0,d=!0,u=!1;for(r?.addEventListener("abort",(function t(){u=!0,r?.removeEventListener("abort",t)}));d&&!u;){d=!1;try{return await t({retryCount:c})}catch(t){if(t===o&&c<n&&!u){c+=1,d=!0,await new Promise((t=>{setTimeout((()=>{t()}),s*(a?c*a:1))}));continue}throw t}}throw i||new Error("runWithErrorRetry no result")},exports.sendMessageAndWaitForResponse=function(t){const{socket:e,op:o,data:a,resolver:i,rejecter:c}=t,d=n({op:o,data:a});function u(t){e.off(p),c(t)}const p={success(t){try{const r=s(t);r.id===d.id&&r.op===o&&(r.success?(e.off(p),i(r.data)):u(new Error(r.error||"Unknown error")))}catch(t){u(t)}},error:u};return e.on(p),e.send(d.data),()=>{u(r)}}; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@automata-network/pom-js-sdk", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A socket clinet for automata's Proof Of Machinehood", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -17,3 +17,8 @@ import { | ||
export {POMOperation, ERR_OPERATION_CANCELLED} from './util'; | ||
export { | ||
POMOperation, | ||
ERR_OPERATION_CANCELLED, | ||
decodeMessageData, | ||
encodeMessageData, | ||
} from './util'; | ||
@@ -20,0 +25,0 @@ export enum POMPlatform { |
@@ -62,4 +62,8 @@ import {POMSocket, createRandomString} from '@automata-network/pom-socket'; | ||
export function encodeMessageData<T>(options: {op: POMOperation; data: T}) { | ||
const id = createRandomString(6); | ||
export function encodeMessageData<T>(options: { | ||
op: POMOperation; | ||
data: T; | ||
id?: string; | ||
}) { | ||
const {id = createRandomString(6)} = options; | ||
@@ -66,0 +70,0 @@ return {id, data: JSON.stringify({id, ...options})}; |
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
41134
608