@nimiq/hub-api
Advanced tools
Comparing version 1.1.1 to 1.2.0-beta.0
@@ -40,3 +40,3 @@ import { PostMessageRpcClient, RedirectRpcClient } from '@nimiq/rpc'; | ||
const state = Object.assign({}, this._localState, { __command: command }); | ||
client.callAndSaveLocalState(this._returnUrl, state, command, true, ...args); | ||
client.callAndSaveLocalState(this._returnUrl, state, command, true, ...(await Promise.all(args))); | ||
} | ||
@@ -55,3 +55,3 @@ } | ||
try { | ||
return await client.call(command, ...args); | ||
return await client.call(command, ...(await Promise.all(args))); | ||
} | ||
@@ -96,3 +96,3 @@ catch (e) { | ||
} | ||
return await this._client.call(command, ...args); | ||
return await this._client.call(command, ...(await Promise.all(args))); | ||
} | ||
@@ -116,3 +116,2 @@ async createIFrame(endpoint) { | ||
RequestType["LIST"] = "list"; | ||
RequestType["LIST_CASHLINKS"] = "list-cashlinks"; | ||
RequestType["MIGRATE"] = "migrate"; | ||
@@ -131,3 +130,2 @@ RequestType["CHECKOUT"] = "checkout"; | ||
RequestType["CHOOSE_ADDRESS"] = "choose-address"; | ||
RequestType["CASHLINK"] = "cashlink"; | ||
})(RequestType || (RequestType = {})); | ||
@@ -171,5 +169,2 @@ | ||
*/ | ||
createCashlink(request, requestBehavior = this._defaultBehavior) { | ||
return this._request(requestBehavior, RequestType.CASHLINK, [request]); | ||
} | ||
checkout(request, requestBehavior = this._defaultBehavior) { | ||
@@ -225,8 +220,2 @@ return this._request(requestBehavior, RequestType.CHECKOUT, [request]); | ||
} | ||
/** | ||
* Only accessible in iframe from Nimiq domains. | ||
*/ | ||
cashlinks(requestBehavior = this._iframeBehavior) { | ||
return this._request(requestBehavior, RequestType.LIST_CASHLINKS, []); | ||
} | ||
// END API | ||
@@ -233,0 +222,0 @@ /* PRIVATE METHODS */ |
@@ -44,3 +44,3 @@ (function (global, factory) { | ||
const state = Object.assign({}, this._localState, { __command: command }); | ||
client.callAndSaveLocalState(this._returnUrl, state, command, true, ...args); | ||
client.callAndSaveLocalState(this._returnUrl, state, command, true, ...(await Promise.all(args))); | ||
} | ||
@@ -59,3 +59,3 @@ } | ||
try { | ||
return await client.call(command, ...args); | ||
return await client.call(command, ...(await Promise.all(args))); | ||
} | ||
@@ -100,3 +100,3 @@ catch (e) { | ||
} | ||
return await this._client.call(command, ...args); | ||
return await this._client.call(command, ...(await Promise.all(args))); | ||
} | ||
@@ -120,3 +120,2 @@ async createIFrame(endpoint) { | ||
RequestType["LIST"] = "list"; | ||
RequestType["LIST_CASHLINKS"] = "list-cashlinks"; | ||
RequestType["MIGRATE"] = "migrate"; | ||
@@ -135,3 +134,2 @@ RequestType["CHECKOUT"] = "checkout"; | ||
RequestType["CHOOSE_ADDRESS"] = "choose-address"; | ||
RequestType["CASHLINK"] = "cashlink"; | ||
})(RequestType || (RequestType = {})); | ||
@@ -175,5 +173,2 @@ | ||
*/ | ||
createCashlink(request, requestBehavior = this._defaultBehavior) { | ||
return this._request(requestBehavior, RequestType.CASHLINK, [request]); | ||
} | ||
checkout(request, requestBehavior = this._defaultBehavior) { | ||
@@ -229,8 +224,2 @@ return this._request(requestBehavior, RequestType.CHECKOUT, [request]); | ||
} | ||
/** | ||
* Only accessible in iframe from Nimiq domains. | ||
*/ | ||
cashlinks(requestBehavior = this._iframeBehavior) { | ||
return this._request(requestBehavior, RequestType.LIST_CASHLINKS, []); | ||
} | ||
// END API | ||
@@ -237,0 +226,0 @@ /* PRIVATE METHODS */ |
import { RequestBehavior, RedirectRequestBehavior, BehaviorType } from './RequestBehavior'; | ||
import { RequestType } from '../src/lib/RequestTypes'; | ||
import { BasicRequest, SimpleRequest, OnboardRequest, CheckoutRequest, SignTransactionRequest, RenameRequest, SignMessageRequest, ExportRequest, ResultByRequestType, Account, Address, SignedTransaction, SimpleResult, ExportResult, SignedMessage, CashlinkRequest, Cashlink } from '../src/lib/PublicRequestTypes'; | ||
import { BasicRequest, SimpleRequest, OnboardRequest, CheckoutRequest, SignTransactionRequest, RenameRequest, SignMessageRequest, ExportRequest, ResultByRequestType, Account, Address, SignedTransaction, SimpleResult, ExportResult, SignedMessage } from '../src/lib/PublicRequestTypes'; | ||
export default class HubApi<DB extends BehaviorType = BehaviorType.POPUP> { | ||
@@ -19,7 +19,6 @@ static readonly RequestType: typeof RequestType; | ||
*/ | ||
createCashlink<B extends BehaviorType = DB>(request: CashlinkRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Cashlink>; | ||
checkout<B extends BehaviorType = DB>(request: CheckoutRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction>; | ||
chooseAddress<B extends BehaviorType = DB>(request: BasicRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Address>; | ||
signTransaction<B extends BehaviorType = DB>(request: SignTransactionRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction>; | ||
signMessage<B extends BehaviorType = DB>(request: SignMessageRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedMessage>; | ||
checkout<B extends BehaviorType = DB>(request: Promise<CheckoutRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction>; | ||
chooseAddress<B extends BehaviorType = DB>(request: Promise<BasicRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Address>; | ||
signTransaction<B extends BehaviorType = DB>(request: Promise<SignTransactionRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction>; | ||
signMessage<B extends BehaviorType = DB>(request: Promise<SignMessageRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedMessage>; | ||
/** | ||
@@ -30,10 +29,10 @@ * Account Management | ||
*/ | ||
onboard<B extends BehaviorType = DB>(request: OnboardRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
signup<B extends BehaviorType = DB>(request: BasicRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
login<B extends BehaviorType = DB>(request: BasicRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
logout<B extends BehaviorType = DB>(request: SimpleRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SimpleResult>; | ||
export<B extends BehaviorType = DB>(request: ExportRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : ExportResult>; | ||
changePassword<B extends BehaviorType = DB>(request: SimpleRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SimpleResult>; | ||
addAddress<B extends BehaviorType = DB>(request: SimpleRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Address>; | ||
rename<B extends BehaviorType = DB>(request: RenameRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account>; | ||
onboard<B extends BehaviorType = DB>(request: Promise<OnboardRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
signup<B extends BehaviorType = DB>(request: Promise<BasicRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
login<B extends BehaviorType = DB>(request: Promise<BasicRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
logout<B extends BehaviorType = DB>(request: Promise<SimpleRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SimpleResult>; | ||
export<B extends BehaviorType = DB>(request: Promise<ExportRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : ExportResult>; | ||
changePassword<B extends BehaviorType = DB>(request: Promise<SimpleRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SimpleResult>; | ||
addAddress<B extends BehaviorType = DB>(request: Promise<SimpleRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Address>; | ||
rename<B extends BehaviorType = DB>(request: Promise<RenameRequest>, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account>; | ||
migrate<B extends BehaviorType = DB>(requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
@@ -44,7 +43,3 @@ /** | ||
list<B extends BehaviorType = DB>(requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Account[]>; | ||
/** | ||
* Only accessible in iframe from Nimiq domains. | ||
*/ | ||
cashlinks<B extends BehaviorType = DB>(requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : Cashlink[]>; | ||
private _request; | ||
} |
@@ -7,3 +7,3 @@ import { RequestType } from '../src/lib/RequestTypes'; | ||
constructor(type: B); | ||
request<R extends RequestType>(endpoint: string, command: R, args: any[]): Promise<B extends BehaviorType.REDIRECT ? void : ResultByRequestType<R>>; | ||
request<R extends RequestType>(endpoint: string, command: R, args: Array<Promise<any>>): Promise<B extends BehaviorType.REDIRECT ? void : ResultByRequestType<R>>; | ||
} | ||
@@ -20,3 +20,3 @@ export declare enum BehaviorType { | ||
constructor(returnUrl?: string, localState?: any); | ||
request<R extends RequestType>(endpoint: string, command: R, args: any[]): Promise<void>; | ||
request<R extends RequestType>(endpoint: string, command: R, args: Array<Promise<any>>): Promise<void>; | ||
} | ||
@@ -27,3 +27,3 @@ export declare class PopupRequestBehavior extends RequestBehavior<BehaviorType.POPUP> { | ||
constructor(options?: string); | ||
request<R extends RequestType>(endpoint: string, command: R, args: any[]): Promise<ResultByRequestType<R>>; | ||
request<R extends RequestType>(endpoint: string, command: R, args: Array<Promise<any>>): Promise<ResultByRequestType<R>>; | ||
createPopup(url: string): Window; | ||
@@ -36,4 +36,4 @@ } | ||
constructor(); | ||
request<R extends RequestType>(endpoint: string, command: R, args: any[]): Promise<ResultByRequestType<R>>; | ||
request<R extends RequestType>(endpoint: string, command: R, args: Array<Promise<any>>): Promise<ResultByRequestType<R>>; | ||
createIFrame(endpoint: string): Promise<HTMLIFrameElement>; | ||
} |
import { WalletType } from './WalletInfo'; | ||
import { RequestType } from './RequestTypes'; | ||
export declare enum CashlinkState { | ||
UNKNOWN = -1, | ||
UNCHARGED = 0, | ||
CHARGING = 1, | ||
UNCLAIMED = 2, | ||
CLAIMING = 3, | ||
CLAIMED = 4 | ||
} | ||
export interface BasicRequest { | ||
@@ -118,16 +110,4 @@ appName: string; | ||
} | ||
export interface Cashlink { | ||
address: string; | ||
message: string | Uint8Array; | ||
status: CashlinkState; | ||
sender: string; | ||
recipient?: string; | ||
} | ||
export interface CashlinkRequest extends BasicRequest { | ||
senderAddress?: string; | ||
senderBalance?: number; | ||
cashlinkAddress?: string; | ||
} | ||
export declare type RpcRequest = SignTransactionRequest | CashlinkRequest | CheckoutRequest | BasicRequest | SimpleRequest | OnboardRequest | RenameRequest | SignMessageRequest | ExportRequest; | ||
export declare type RpcResult = SignedTransaction | Account | Account[] | SimpleResult | Address | Cashlink | SignedMessage | ExportResult; | ||
export declare type ResultByRequestType<T> = T extends RequestType.RENAME ? Account : T extends RequestType.ONBOARD | RequestType.SIGNUP | RequestType.LOGIN | RequestType.MIGRATE | RequestType.LIST ? Account[] : T extends RequestType.LIST_CASHLINKS ? Cashlink[] : T extends RequestType.CHOOSE_ADDRESS | RequestType.ADD_ADDRESS ? Address : T extends RequestType.SIGN_TRANSACTION | RequestType.CHECKOUT ? SignedTransaction : T extends RequestType.SIGN_MESSAGE ? SignedMessage : T extends RequestType.LOGOUT | RequestType.CHANGE_PASSWORD ? SimpleResult : T extends RequestType.EXPORT ? ExportResult : T extends RequestType.CASHLINK ? Cashlink : never; | ||
export declare type RpcRequest = SignTransactionRequest | CheckoutRequest | BasicRequest | SimpleRequest | OnboardRequest | RenameRequest | SignMessageRequest | ExportRequest; | ||
export declare type RpcResult = SignedTransaction | Account | Account[] | SimpleResult | Address | SignedMessage | ExportResult; | ||
export declare type ResultByRequestType<T> = T extends RequestType.RENAME ? Account : T extends RequestType.ONBOARD | RequestType.SIGNUP | RequestType.LOGIN | RequestType.MIGRATE | RequestType.LIST ? Account[] : T extends RequestType.CHOOSE_ADDRESS | RequestType.ADD_ADDRESS ? Address : T extends RequestType.SIGN_TRANSACTION | RequestType.CHECKOUT ? SignedTransaction : T extends RequestType.SIGN_MESSAGE ? SignedMessage : T extends RequestType.LOGOUT | RequestType.CHANGE_PASSWORD ? SimpleResult : T extends RequestType.EXPORT ? ExportResult : never; |
export declare enum RequestType { | ||
LIST = "list", | ||
LIST_CASHLINKS = "list-cashlinks", | ||
MIGRATE = "migrate", | ||
@@ -16,4 +15,3 @@ CHECKOUT = "checkout", | ||
RENAME = "rename", | ||
CHOOSE_ADDRESS = "choose-address", | ||
CASHLINK = "cashlink" | ||
CHOOSE_ADDRESS = "choose-address" | ||
} | ||
@@ -63,7 +61,2 @@ export interface ParsedBasicRequest { | ||
} | ||
export interface ParsedCashlinkRequest extends ParsedBasicRequest { | ||
senderAddress?: Nimiq.Address; | ||
senderBalance?: number; | ||
cashlinkAddress?: Nimiq.Address; | ||
} | ||
export declare type ParsedRpcRequest = ParsedSignTransactionRequest | ParsedCashlinkRequest | ParsedCheckoutRequest | ParsedBasicRequest | ParsedSimpleRequest | ParsedOnboardRequest | ParsedRenameRequest | ParsedSignMessageRequest | ParsedExportRequest; | ||
export declare type ParsedRpcRequest = ParsedSignTransactionRequest | ParsedCheckoutRequest | ParsedBasicRequest | ParsedSimpleRequest | ParsedOnboardRequest | ParsedRenameRequest | ParsedSignMessageRequest | ParsedExportRequest; |
@@ -1,1 +0,1 @@ | ||
class e{static byteLength(t){const[s,r]=e._getLengths(t);return e._byteLength(s,r)}static decode(t){e._initRevLookup();const[s,r]=e._getLengths(t),n=new Uint8Array(e._byteLength(s,r));let i=0;const o=r>0?s-4:s;let a=0;for(;a<o;a+=4){const s=e._revLookup[t.charCodeAt(a)]<<18|e._revLookup[t.charCodeAt(a+1)]<<12|e._revLookup[t.charCodeAt(a+2)]<<6|e._revLookup[t.charCodeAt(a+3)];n[i++]=s>>16&255,n[i++]=s>>8&255,n[i++]=255&s}if(2===r){const s=e._revLookup[t.charCodeAt(a)]<<2|e._revLookup[t.charCodeAt(a+1)]>>4;n[i++]=255&s}if(1===r){const s=e._revLookup[t.charCodeAt(a)]<<10|e._revLookup[t.charCodeAt(a+1)]<<4|e._revLookup[t.charCodeAt(a+2)]>>2;n[i++]=s>>8&255,n[i]=255&s}return n}static encode(t){const s=t.length,r=s%3,n=[];for(let i=0,o=s-r;i<o;i+=16383)n.push(e._encodeChunk(t,i,i+16383>o?o:i+16383));if(1===r){const r=t[s-1];n.push(e._lookup[r>>2]+e._lookup[r<<4&63]+"==")}else if(2===r){const r=(t[s-2]<<8)+t[s-1];n.push(e._lookup[r>>10]+e._lookup[r>>4&63]+e._lookup[r<<2&63]+"=")}return n.join("")}static encodeUrl(t){return e.encode(t).replace(/\//g,"_").replace(/\+/g,"-").replace(/=/g,".")}static decodeUrl(t){return e.decode(t.replace(/_/g,"/").replace(/-/g,"+").replace(/\./g,"="))}static _initRevLookup(){if(0===e._revLookup.length){e._revLookup=[];for(let t=0,s=e._lookup.length;t<s;t++)e._revLookup[e._lookup.charCodeAt(t)]=t;e._revLookup["-".charCodeAt(0)]=62,e._revLookup["_".charCodeAt(0)]=63}}static _getLengths(e){const t=e.length;if(t%4>0)throw new Error("Invalid string. Length must be a multiple of 4");let s=e.indexOf("=");return-1===s&&(s=t),[s,s===t?0:4-s%4]}static _byteLength(e,t){return 3*(e+t)/4-t}static _tripletToBase64(t){return e._lookup[t>>18&63]+e._lookup[t>>12&63]+e._lookup[t>>6&63]+e._lookup[63&t]}static _encodeChunk(t,s,r){const n=[];for(let i=s;i<r;i+=3){const s=(t[i]<<16&16711680)+(t[i+1]<<8&65280)+(255&t[i+2]);n.push(e._tripletToBase64(s))}return n.join("")}}var t,s;e._lookup="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",e._revLookup=[],function(e){e[e.UINT8_ARRAY=0]="UINT8_ARRAY"}(t||(t={}));class r{static stringify(e){return JSON.stringify(e,r._jsonifyType)}static parse(e){return JSON.parse(e,r._parseType)}static _parseType(s,n){if(n&&n.hasOwnProperty&&n.hasOwnProperty(r.TYPE_SYMBOL)&&n.hasOwnProperty(r.VALUE_SYMBOL))switch(n[r.TYPE_SYMBOL]){case t.UINT8_ARRAY:return e.decode(n[r.VALUE_SYMBOL])}return n}static _jsonifyType(s,n){return n instanceof Uint8Array?r._typedObject(t.UINT8_ARRAY,e.encode(n)):n}static _typedObject(e,t){const s={};return s[r.TYPE_SYMBOL]=e,s[r.VALUE_SYMBOL]=t,s}}r.TYPE_SYMBOL="__",r.VALUE_SYMBOL="v";class n{static generateRandomId(){const e=new Uint32Array(1);return crypto.getRandomValues(e),e[0]}}!function(e){e.OK="ok",e.ERROR="error"}(s||(s={}));const i="<postMessage>";class o{constructor(e=!0){this._store=e?window.sessionStorage:null,this._validIds=new Map,e&&this._restoreIds()}static _decodeIds(e){const t=r.parse(e),s=new Map;for(const e of Object.keys(t)){const r=parseInt(e,10);s.set(isNaN(r)?e:r,t[e])}return s}has(e){return this._validIds.has(e)}getCommand(e){const t=this._validIds.get(e);return t?t[0]:null}getState(e){const t=this._validIds.get(e);return t?t[1]:null}add(e,t,s=null){this._validIds.set(e,[t,s]),this._storeIds()}remove(e){this._validIds.delete(e),this._storeIds()}clear(){this._validIds.clear(),this._store&&this._store.removeItem(o.KEY)}_encodeIds(){const e=Object.create(null);for(const[t,s]of this._validIds)e[t]=s;return r.stringify(e)}_restoreIds(){const e=this._store.getItem(o.KEY);e&&(this._validIds=o._decodeIds(e))}_storeIds(){this._store&&this._store.setItem(o.KEY,this._encodeIds())}}o.KEY="rpcRequests";class a{static receiveRedirectCommand(e){const t=new URL(e.href);if(!document.referrer)return null;const s=new URL(document.referrer),n=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),n.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("command"))return null;const l=o.get("command");if(o.delete("command"),!o.has("returnURL"))return null;const h=o.get("returnURL");o.delete("returnURL");const d=h===i&&(window.opener||window.parent);if(!d&&new URL(h).origin!==s.origin)return null;let u=[];if(o.has("args"))try{u=r.parse(o.get("args"))}catch(e){}return u=Array.isArray(u)?u:[],o.delete("args"),t.search=n.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:s.origin,data:{id:c,command:l,args:u},returnURL:h,source:d?window.opener||window.parent:null}}static receiveRedirectResponse(e){const t=new URL(e.href);if(!document.referrer)return null;const n=new URL(document.referrer),i=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),i.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("status"))return null;const l=o.get("status")===s.OK?s.OK:s.ERROR;if(o.delete("status"),!o.has("result"))return null;const h=r.parse(o.get("result"));return o.delete("result"),t.search=i.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:n.origin,data:{id:c,status:l,result:h}}}static prepareRedirectReply(e,t,s){const n=new URL(e.returnURL),i=new URLSearchParams(n.hash.substring(1));return i.set("id",e.id.toString()),i.set("status",t),i.set("result",r.stringify(s)),n.hash=i.toString(),n.href}static prepareRedirectInvocation(e,t,s,n,i){const o=new URL(e),a=new URLSearchParams(o.hash.substring(1));return a.set("id",t.toString()),a.set("returnURL",s),a.set("command",n),Array.isArray(i)&&a.set("args",r.stringify(i)),o.hash=a.toString(),o.href}static _setUrlFragment(e,t){t.toString().endsWith("=")?e.hash=t.toString().slice(0,-1):e.hash=t.toString()}}a.URL_SEARCHPARAM_NAME="rpcId";class c{constructor(e,t=!1){this._allowedOrigin=e,this._waitingRequests=new o(t),this._responseHandlers=new Map,this._preserveRequests=!1}onResponse(e,t,s){this._responseHandlers.set(e,{resolve:t,reject:s})}_receive(e){if(!e.data||!e.data.status||!e.data.id||"*"!==this._allowedOrigin&&e.origin!==this._allowedOrigin)return!1;const t=e.data,r=this._getCallback(t.id),n=this._waitingRequests.getState(t.id);if(r){if(this._preserveRequests||(this._waitingRequests.remove(t.id),this._responseHandlers.delete(t.id)),console.debug("RpcClient RECEIVE",t),t.status===s.OK)r.resolve(t.result,t.id,n);else if(t.status===s.ERROR){const e=new Error(t.result.message);t.result.stack&&(e.stack=t.result.stack),t.result.name&&(e.name=t.result.name),r.reject(e,t.id,n)}return!0}return console.warn("Unknown RPC response:",t),!1}_getCallback(e){if(this._responseHandlers.has(e))return this._responseHandlers.get(e);{const t=this._waitingRequests.getCommand(e);if(t)return this._responseHandlers.get(t)}}}class l extends c{constructor(e,t){super(t),this._serverCloseCheckInterval=-1,this._target=e,this._connectionState=0,this._receiveListener=this._receive.bind(this)}async init(){2!==this._connectionState&&(await this._connect(),window.addEventListener("message",this._receiveListener),-1===this._serverCloseCheckInterval&&(this._serverCloseCheckInterval=window.setInterval(()=>this._checkIfServerClosed(),300)))}async call(e,...t){return this._call({command:e,args:t,id:n.generateRandomId()})}close(){this._connectionState=0,window.removeEventListener("message",this._receiveListener),window.clearInterval(this._serverCloseCheckInterval),this._serverCloseCheckInterval=-1;for(const[e,{reject:t}]of this._responseHandlers){const s=this._waitingRequests.getState(e);t("Connection was closed","number"==typeof e?e:void 0,s)}this._waitingRequests.clear(),this._responseHandlers.clear(),this._target&&this._target.closed&&(this._target=null)}_receive(e){return e.source===this._target&&super._receive(e)}async _call(e){if(!this._target||this._target.closed)throw new Error("Connection was closed.");if(2!==this._connectionState)throw new Error("Client is not connected, call init first");return new Promise((t,s)=>{this._responseHandlers.set(e.id,{resolve:t,reject:s}),this._waitingRequests.add(e.id,e.command),console.debug("RpcClient REQUEST",e.command,e.args),this._target.postMessage(e,this._allowedOrigin)})}_connect(){if(2!==this._connectionState)return this._connectionState=1,new Promise((e,t)=>{const r=t=>{const{source:n,origin:i,data:o}=t;if(n===this._target&&o.status===s.OK&&"pong"===o.result&&1===o.id&&("*"===this._allowedOrigin||i===this._allowedOrigin)){if(o.result.stack){const e=new Error(o.result.message);e.stack=o.result.stack,o.result.name&&(e.name=o.result.name),console.error(e)}window.removeEventListener("message",r),this._connectionState=2,console.log("RpcClient: Connection established"),e(!0)}};window.addEventListener("message",r);const n=()=>{if(2!==this._connectionState){if(0===this._connectionState||this._checkIfServerClosed())return window.removeEventListener("message",r),void t(new Error("Connection was closed"));try{this._target.postMessage({command:"ping",id:1},this._allowedOrigin)}catch(e){console.error(`postMessage failed: ${e}`)}window.setTimeout(n,100)}};window.setTimeout(n,100)})}_checkIfServerClosed(){return!(this._target&&!this._target.closed)&&(this.close(),!0)}}class h extends c{constructor(e,t,s=!0){super(t,!0),this._target=e,this._preserveRequests=s}async init(){const e=a.receiveRedirectResponse(window.location);if(e)return void this._receive(e);if(this._rejectOnBack())return;const t=new URLSearchParams(window.location.search);if(t.has(a.URL_SEARCHPARAM_NAME)){const e=window.sessionStorage.getItem(`response-${t.get(a.URL_SEARCHPARAM_NAME)}`);if(e)return void this._receive(r.parse(e),!1)}}close(){}call(e,t,s=!1,...r){this.callAndSaveLocalState(e,null,t,s,...r)}callAndSaveLocalState(e,t,s,r=!1,...i){const o=n.generateRandomId(),c=a.prepareRedirectInvocation(this._target,o,e,s,i);this._waitingRequests.add(o,s,t),r&&history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:o}),""),console.debug("RpcClient REQUEST",s,i),window.location.href=c}_receive(e,t=!0){const s=super._receive(e);return s&&t&&window.sessionStorage.setItem(`response-${e.data.id}`,r.stringify(e)),s}_rejectOnBack(){if(!history.state||!history.state.rpcBackRejectionId)return!1;const e=history.state.rpcBackRejectionId;history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:null}),"");const t=this._getCallback(e),s=this._waitingRequests.getState(e);if(t){this._preserveRequests||(this._waitingRequests.remove(e),this._responseHandlers.delete(e)),console.debug("RpcClient BACK");const r=new Error("Request aborted");return t.reject(r,e,s),!0}return!1}}class d{constructor(e){this._type=e}static getAllowedOrigin(e){return new URL(e).origin}async request(e,t,s){throw new Error("Not implemented")}}var u,_;!function(e){e[e.REDIRECT=0]="REDIRECT",e[e.POPUP=1]="POPUP",e[e.IFRAME=2]="IFRAME"}(u||(u={}));class g extends d{static withLocalState(e){return new g(void 0,e)}constructor(e,t){super(u.REDIRECT);const s=window.location;if(this._returnUrl=e||`${s.origin}${s.pathname}`,this._localState=t||{},void 0!==this._localState.__command)throw new Error("Invalid localState: Property '__command' is reserved")}async request(e,t,s){const r=d.getAllowedOrigin(e),n=new h(e,r);await n.init();const i=Object.assign({},this._localState,{__command:t});n.callAndSaveLocalState(this._returnUrl,i,t,!0,...s)}}class p extends d{constructor(e=p.DEFAULT_OPTIONS){super(u.POPUP),this._options=e}async request(e,t,s){const r=d.getAllowedOrigin(e),n=this.createPopup(e),i=new l(n,r);await i.init();try{return await i.call(t,...s)}catch(e){throw e}finally{i.close(),n.close()}}createPopup(e){const t=window.open(e,"NimiqAccounts",this._options);if(!t)throw new Error("Failed to open popup");return t}}p.DEFAULT_OPTIONS="";class w extends d{constructor(){super(u.IFRAME),this._iframe=null,this._client=null}async request(e,t,s){if(this._iframe&&this._iframe.src!==`${e}${w.IFRAME_PATH_SUFFIX}`)throw new Error("Hub iframe is already opened with another endpoint");const r=d.getAllowedOrigin(e);if(this._iframe||(this._iframe=await this.createIFrame(e)),!this._iframe.contentWindow)throw new Error(`IFrame contentWindow is ${typeof this._iframe.contentWindow}`);return this._client||(this._client=new l(this._iframe.contentWindow,r),await this._client.init()),await this._client.call(t,...s)}async createIFrame(e){return new Promise((t,s)=>{const r=document.createElement("iframe");r.name="NimiqAccountsIFrame",r.style.display="none",document.body.appendChild(r),r.src=`${e}${w.IFRAME_PATH_SUFFIX}`,r.onload=(()=>t(r)),r.onerror=s})}}w.IFRAME_PATH_SUFFIX="/iframe.html",function(e){e.LIST="list",e.LIST_CASHLINKS="list-cashlinks",e.MIGRATE="migrate",e.CHECKOUT="checkout",e.SIGN_MESSAGE="sign-message",e.SIGN_TRANSACTION="sign-transaction",e.ONBOARD="onboard",e.SIGNUP="signup",e.LOGIN="login",e.EXPORT="export",e.CHANGE_PASSWORD="change-password",e.LOGOUT="logout",e.ADD_ADDRESS="add-address",e.RENAME="rename",e.CHOOSE_ADDRESS="choose-address",e.CASHLINK="cashlink"}(_||(_={}));class R{constructor(e=R.DEFAULT_ENDPOINT,t){this._endpoint=e,this._defaultBehavior=t||new p(`left=${window.innerWidth/2-400},top=75,width=800,height=850,location=yes,dependent=yes`),this._iframeBehavior=new w,this._redirectClient=new h("",d.getAllowedOrigin(this._endpoint))}static get DEFAULT_ENDPOINT(){const e=location.origin.split(".");switch(e.shift(),e.join(".")){case"nimiq.com":return"https://hub.nimiq.com";case"nimiq-testnet.com":return"https://hub.nimiq-testnet.com";default:return"http://localhost:8080"}}checkRedirectResponse(){return this._redirectClient.init()}on(e,t,s){this._redirectClient.onResponse(e,(e,s,r)=>t(e,r),(e,t,r)=>{s&&s(e,r)})}createCashlink(e,t=this._defaultBehavior){return this._request(t,_.CASHLINK,[e])}checkout(e,t=this._defaultBehavior){return this._request(t,_.CHECKOUT,[e])}chooseAddress(e,t=this._defaultBehavior){return this._request(t,_.CHOOSE_ADDRESS,[e])}signTransaction(e,t=this._defaultBehavior){return this._request(t,_.SIGN_TRANSACTION,[e])}signMessage(e,t=this._defaultBehavior){return this._request(t,_.SIGN_MESSAGE,[e])}onboard(e,t=this._defaultBehavior){return this._request(t,_.ONBOARD,[e])}signup(e,t=this._defaultBehavior){return this._request(t,_.SIGNUP,[e])}login(e,t=this._defaultBehavior){return this._request(t,_.LOGIN,[e])}logout(e,t=this._defaultBehavior){return this._request(t,_.LOGOUT,[e])}export(e,t=this._defaultBehavior){return this._request(t,_.EXPORT,[e])}changePassword(e,t=this._defaultBehavior){return this._request(t,_.CHANGE_PASSWORD,[e])}addAddress(e,t=this._defaultBehavior){return this._request(t,_.ADD_ADDRESS,[e])}rename(e,t=this._defaultBehavior){return this._request(t,_.RENAME,[e])}migrate(e=this._defaultBehavior){return this._request(e,_.MIGRATE,[{appName:"Account list"}])}list(e=this._iframeBehavior){return this._request(e,_.LIST,[])}cashlinks(e=this._iframeBehavior){return this._request(e,_.LIST_CASHLINKS,[])}_request(e,t,s){return e.request(this._endpoint,t,s)}}R.RequestType=_,R.RedirectRequestBehavior=g,R.MSG_PREFIX="Nimiq Signed Message:\n";export default R; | ||
class e{static byteLength(t){const[s,r]=e._getLengths(t);return e._byteLength(s,r)}static decode(t){e._initRevLookup();const[s,r]=e._getLengths(t),n=new Uint8Array(e._byteLength(s,r));let i=0;const o=r>0?s-4:s;let a=0;for(;a<o;a+=4){const s=e._revLookup[t.charCodeAt(a)]<<18|e._revLookup[t.charCodeAt(a+1)]<<12|e._revLookup[t.charCodeAt(a+2)]<<6|e._revLookup[t.charCodeAt(a+3)];n[i++]=s>>16&255,n[i++]=s>>8&255,n[i++]=255&s}if(2===r){const s=e._revLookup[t.charCodeAt(a)]<<2|e._revLookup[t.charCodeAt(a+1)]>>4;n[i++]=255&s}if(1===r){const s=e._revLookup[t.charCodeAt(a)]<<10|e._revLookup[t.charCodeAt(a+1)]<<4|e._revLookup[t.charCodeAt(a+2)]>>2;n[i++]=s>>8&255,n[i]=255&s}return n}static encode(t){const s=t.length,r=s%3,n=[];for(let i=0,o=s-r;i<o;i+=16383)n.push(e._encodeChunk(t,i,i+16383>o?o:i+16383));if(1===r){const r=t[s-1];n.push(e._lookup[r>>2]+e._lookup[r<<4&63]+"==")}else if(2===r){const r=(t[s-2]<<8)+t[s-1];n.push(e._lookup[r>>10]+e._lookup[r>>4&63]+e._lookup[r<<2&63]+"=")}return n.join("")}static encodeUrl(t){return e.encode(t).replace(/\//g,"_").replace(/\+/g,"-").replace(/=/g,".")}static decodeUrl(t){return e.decode(t.replace(/_/g,"/").replace(/-/g,"+").replace(/\./g,"="))}static _initRevLookup(){if(0===e._revLookup.length){e._revLookup=[];for(let t=0,s=e._lookup.length;t<s;t++)e._revLookup[e._lookup.charCodeAt(t)]=t;e._revLookup["-".charCodeAt(0)]=62,e._revLookup["_".charCodeAt(0)]=63}}static _getLengths(e){const t=e.length;if(t%4>0)throw new Error("Invalid string. Length must be a multiple of 4");let s=e.indexOf("=");return-1===s&&(s=t),[s,s===t?0:4-s%4]}static _byteLength(e,t){return 3*(e+t)/4-t}static _tripletToBase64(t){return e._lookup[t>>18&63]+e._lookup[t>>12&63]+e._lookup[t>>6&63]+e._lookup[63&t]}static _encodeChunk(t,s,r){const n=[];for(let i=s;i<r;i+=3){const s=(t[i]<<16&16711680)+(t[i+1]<<8&65280)+(255&t[i+2]);n.push(e._tripletToBase64(s))}return n.join("")}}var t,s;e._lookup="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",e._revLookup=[],function(e){e[e.UINT8_ARRAY=0]="UINT8_ARRAY"}(t||(t={}));class r{static stringify(e){return JSON.stringify(e,r._jsonifyType)}static parse(e){return JSON.parse(e,r._parseType)}static _parseType(s,n){if(n&&n.hasOwnProperty&&n.hasOwnProperty(r.TYPE_SYMBOL)&&n.hasOwnProperty(r.VALUE_SYMBOL))switch(n[r.TYPE_SYMBOL]){case t.UINT8_ARRAY:return e.decode(n[r.VALUE_SYMBOL])}return n}static _jsonifyType(s,n){return n instanceof Uint8Array?r._typedObject(t.UINT8_ARRAY,e.encode(n)):n}static _typedObject(e,t){const s={};return s[r.TYPE_SYMBOL]=e,s[r.VALUE_SYMBOL]=t,s}}r.TYPE_SYMBOL="__",r.VALUE_SYMBOL="v";class n{static generateRandomId(){const e=new Uint32Array(1);return crypto.getRandomValues(e),e[0]}}!function(e){e.OK="ok",e.ERROR="error"}(s||(s={}));const i="<postMessage>";class o{constructor(e=!0){this._store=e?window.sessionStorage:null,this._validIds=new Map,e&&this._restoreIds()}static _decodeIds(e){const t=r.parse(e),s=new Map;for(const e of Object.keys(t)){const r=parseInt(e,10);s.set(isNaN(r)?e:r,t[e])}return s}has(e){return this._validIds.has(e)}getCommand(e){const t=this._validIds.get(e);return t?t[0]:null}getState(e){const t=this._validIds.get(e);return t?t[1]:null}add(e,t,s=null){this._validIds.set(e,[t,s]),this._storeIds()}remove(e){this._validIds.delete(e),this._storeIds()}clear(){this._validIds.clear(),this._store&&this._store.removeItem(o.KEY)}_encodeIds(){const e=Object.create(null);for(const[t,s]of this._validIds)e[t]=s;return r.stringify(e)}_restoreIds(){const e=this._store.getItem(o.KEY);e&&(this._validIds=o._decodeIds(e))}_storeIds(){this._store&&this._store.setItem(o.KEY,this._encodeIds())}}o.KEY="rpcRequests";class a{static receiveRedirectCommand(e){const t=new URL(e.href);if(!document.referrer)return null;const s=new URL(document.referrer),n=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),n.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("command"))return null;const l=o.get("command");if(o.delete("command"),!o.has("returnURL"))return null;const h=o.get("returnURL");o.delete("returnURL");const d=h===i&&(window.opener||window.parent);if(!d&&new URL(h).origin!==s.origin)return null;let u=[];if(o.has("args"))try{u=r.parse(o.get("args"))}catch(e){}return u=Array.isArray(u)?u:[],o.delete("args"),t.search=n.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:s.origin,data:{id:c,command:l,args:u},returnURL:h,source:d?window.opener||window.parent:null}}static receiveRedirectResponse(e){const t=new URL(e.href);if(!document.referrer)return null;const n=new URL(document.referrer),i=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),i.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("status"))return null;const l=o.get("status")===s.OK?s.OK:s.ERROR;if(o.delete("status"),!o.has("result"))return null;const h=r.parse(o.get("result"));return o.delete("result"),t.search=i.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:n.origin,data:{id:c,status:l,result:h}}}static prepareRedirectReply(e,t,s){const n=new URL(e.returnURL),i=new URLSearchParams(n.hash.substring(1));return i.set("id",e.id.toString()),i.set("status",t),i.set("result",r.stringify(s)),n.hash=i.toString(),n.href}static prepareRedirectInvocation(e,t,s,n,i){const o=new URL(e),a=new URLSearchParams(o.hash.substring(1));return a.set("id",t.toString()),a.set("returnURL",s),a.set("command",n),Array.isArray(i)&&a.set("args",r.stringify(i)),o.hash=a.toString(),o.href}static _setUrlFragment(e,t){t.toString().endsWith("=")?e.hash=t.toString().slice(0,-1):e.hash=t.toString()}}a.URL_SEARCHPARAM_NAME="rpcId";class c{constructor(e,t=!1){this._allowedOrigin=e,this._waitingRequests=new o(t),this._responseHandlers=new Map,this._preserveRequests=!1}onResponse(e,t,s){this._responseHandlers.set(e,{resolve:t,reject:s})}_receive(e){if(!e.data||!e.data.status||!e.data.id||"*"!==this._allowedOrigin&&e.origin!==this._allowedOrigin)return!1;const t=e.data,r=this._getCallback(t.id),n=this._waitingRequests.getState(t.id);if(r){if(this._preserveRequests||(this._waitingRequests.remove(t.id),this._responseHandlers.delete(t.id)),console.debug("RpcClient RECEIVE",t),t.status===s.OK)r.resolve(t.result,t.id,n);else if(t.status===s.ERROR){const e=new Error(t.result.message);t.result.stack&&(e.stack=t.result.stack),t.result.name&&(e.name=t.result.name),r.reject(e,t.id,n)}return!0}return console.warn("Unknown RPC response:",t),!1}_getCallback(e){if(this._responseHandlers.has(e))return this._responseHandlers.get(e);{const t=this._waitingRequests.getCommand(e);if(t)return this._responseHandlers.get(t)}}}class l extends c{constructor(e,t){super(t),this._serverCloseCheckInterval=-1,this._target=e,this._connectionState=0,this._receiveListener=this._receive.bind(this)}async init(){2!==this._connectionState&&(await this._connect(),window.addEventListener("message",this._receiveListener),-1===this._serverCloseCheckInterval&&(this._serverCloseCheckInterval=window.setInterval(()=>this._checkIfServerClosed(),300)))}async call(e,...t){return this._call({command:e,args:t,id:n.generateRandomId()})}close(){this._connectionState=0,window.removeEventListener("message",this._receiveListener),window.clearInterval(this._serverCloseCheckInterval),this._serverCloseCheckInterval=-1;for(const[e,{reject:t}]of this._responseHandlers){const s=this._waitingRequests.getState(e);t("Connection was closed","number"==typeof e?e:void 0,s)}this._waitingRequests.clear(),this._responseHandlers.clear(),this._target&&this._target.closed&&(this._target=null)}_receive(e){return e.source===this._target&&super._receive(e)}async _call(e){if(!this._target||this._target.closed)throw new Error("Connection was closed.");if(2!==this._connectionState)throw new Error("Client is not connected, call init first");return new Promise((t,s)=>{this._responseHandlers.set(e.id,{resolve:t,reject:s}),this._waitingRequests.add(e.id,e.command),console.debug("RpcClient REQUEST",e.command,e.args),this._target.postMessage(e,this._allowedOrigin)})}_connect(){if(2!==this._connectionState)return this._connectionState=1,new Promise((e,t)=>{const r=t=>{const{source:n,origin:i,data:o}=t;if(n===this._target&&o.status===s.OK&&"pong"===o.result&&1===o.id&&("*"===this._allowedOrigin||i===this._allowedOrigin)){if(o.result.stack){const e=new Error(o.result.message);e.stack=o.result.stack,o.result.name&&(e.name=o.result.name),console.error(e)}window.removeEventListener("message",r),this._connectionState=2,console.log("RpcClient: Connection established"),e(!0)}};window.addEventListener("message",r);const n=()=>{if(2!==this._connectionState){if(0===this._connectionState||this._checkIfServerClosed())return window.removeEventListener("message",r),void t(new Error("Connection was closed"));try{this._target.postMessage({command:"ping",id:1},this._allowedOrigin)}catch(e){console.error(`postMessage failed: ${e}`)}window.setTimeout(n,100)}};window.setTimeout(n,100)})}_checkIfServerClosed(){return!(this._target&&!this._target.closed)&&(this.close(),!0)}}class h extends c{constructor(e,t,s=!0){super(t,!0),this._target=e,this._preserveRequests=s}async init(){const e=a.receiveRedirectResponse(window.location);if(e)return void this._receive(e);if(this._rejectOnBack())return;const t=new URLSearchParams(window.location.search);if(t.has(a.URL_SEARCHPARAM_NAME)){const e=window.sessionStorage.getItem(`response-${t.get(a.URL_SEARCHPARAM_NAME)}`);if(e)return void this._receive(r.parse(e),!1)}}close(){}call(e,t,s=!1,...r){this.callAndSaveLocalState(e,null,t,s,...r)}callAndSaveLocalState(e,t,s,r=!1,...i){const o=n.generateRandomId(),c=a.prepareRedirectInvocation(this._target,o,e,s,i);this._waitingRequests.add(o,s,t),r&&history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:o}),""),console.debug("RpcClient REQUEST",s,i),window.location.href=c}_receive(e,t=!0){const s=super._receive(e);return s&&t&&window.sessionStorage.setItem(`response-${e.data.id}`,r.stringify(e)),s}_rejectOnBack(){if(!history.state||!history.state.rpcBackRejectionId)return!1;const e=history.state.rpcBackRejectionId;history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:null}),"");const t=this._getCallback(e),s=this._waitingRequests.getState(e);if(t){this._preserveRequests||(this._waitingRequests.remove(e),this._responseHandlers.delete(e)),console.debug("RpcClient BACK");const r=new Error("Request aborted");return t.reject(r,e,s),!0}return!1}}class d{constructor(e){this._type=e}static getAllowedOrigin(e){return new URL(e).origin}async request(e,t,s){throw new Error("Not implemented")}}var u,_;!function(e){e[e.REDIRECT=0]="REDIRECT",e[e.POPUP=1]="POPUP",e[e.IFRAME=2]="IFRAME"}(u||(u={}));class g extends d{static withLocalState(e){return new g(void 0,e)}constructor(e,t){super(u.REDIRECT);const s=window.location;if(this._returnUrl=e||`${s.origin}${s.pathname}`,this._localState=t||{},void 0!==this._localState.__command)throw new Error("Invalid localState: Property '__command' is reserved")}async request(e,t,s){const r=d.getAllowedOrigin(e),n=new h(e,r);await n.init();const i=Object.assign({},this._localState,{__command:t});n.callAndSaveLocalState(this._returnUrl,i,t,!0,...await Promise.all(s))}}class p extends d{constructor(e=p.DEFAULT_OPTIONS){super(u.POPUP),this._options=e}async request(e,t,s){const r=d.getAllowedOrigin(e),n=this.createPopup(e),i=new l(n,r);await i.init();try{return await i.call(t,...await Promise.all(s))}catch(e){throw e}finally{i.close(),n.close()}}createPopup(e){const t=window.open(e,"NimiqAccounts",this._options);if(!t)throw new Error("Failed to open popup");return t}}p.DEFAULT_OPTIONS="";class w extends d{constructor(){super(u.IFRAME),this._iframe=null,this._client=null}async request(e,t,s){if(this._iframe&&this._iframe.src!==`${e}${w.IFRAME_PATH_SUFFIX}`)throw new Error("Hub iframe is already opened with another endpoint");const r=d.getAllowedOrigin(e);if(this._iframe||(this._iframe=await this.createIFrame(e)),!this._iframe.contentWindow)throw new Error(`IFrame contentWindow is ${typeof this._iframe.contentWindow}`);return this._client||(this._client=new l(this._iframe.contentWindow,r),await this._client.init()),await this._client.call(t,...await Promise.all(s))}async createIFrame(e){return new Promise((t,s)=>{const r=document.createElement("iframe");r.name="NimiqAccountsIFrame",r.style.display="none",document.body.appendChild(r),r.src=`${e}${w.IFRAME_PATH_SUFFIX}`,r.onload=(()=>t(r)),r.onerror=s})}}w.IFRAME_PATH_SUFFIX="/iframe.html",function(e){e.LIST="list",e.MIGRATE="migrate",e.CHECKOUT="checkout",e.SIGN_MESSAGE="sign-message",e.SIGN_TRANSACTION="sign-transaction",e.ONBOARD="onboard",e.SIGNUP="signup",e.LOGIN="login",e.EXPORT="export",e.CHANGE_PASSWORD="change-password",e.LOGOUT="logout",e.ADD_ADDRESS="add-address",e.RENAME="rename",e.CHOOSE_ADDRESS="choose-address"}(_||(_={}));class R{constructor(e=R.DEFAULT_ENDPOINT,t){this._endpoint=e,this._defaultBehavior=t||new p(`left=${window.innerWidth/2-400},top=75,width=800,height=850,location=yes,dependent=yes`),this._iframeBehavior=new w,this._redirectClient=new h("",d.getAllowedOrigin(this._endpoint))}static get DEFAULT_ENDPOINT(){const e=location.origin.split(".");switch(e.shift(),e.join(".")){case"nimiq.com":return"https://hub.nimiq.com";case"nimiq-testnet.com":return"https://hub.nimiq-testnet.com";default:return"http://localhost:8080"}}checkRedirectResponse(){return this._redirectClient.init()}on(e,t,s){this._redirectClient.onResponse(e,(e,s,r)=>t(e,r),(e,t,r)=>{s&&s(e,r)})}checkout(e,t=this._defaultBehavior){return this._request(t,_.CHECKOUT,[e])}chooseAddress(e,t=this._defaultBehavior){return this._request(t,_.CHOOSE_ADDRESS,[e])}signTransaction(e,t=this._defaultBehavior){return this._request(t,_.SIGN_TRANSACTION,[e])}signMessage(e,t=this._defaultBehavior){return this._request(t,_.SIGN_MESSAGE,[e])}onboard(e,t=this._defaultBehavior){return this._request(t,_.ONBOARD,[e])}signup(e,t=this._defaultBehavior){return this._request(t,_.SIGNUP,[e])}login(e,t=this._defaultBehavior){return this._request(t,_.LOGIN,[e])}logout(e,t=this._defaultBehavior){return this._request(t,_.LOGOUT,[e])}export(e,t=this._defaultBehavior){return this._request(t,_.EXPORT,[e])}changePassword(e,t=this._defaultBehavior){return this._request(t,_.CHANGE_PASSWORD,[e])}addAddress(e,t=this._defaultBehavior){return this._request(t,_.ADD_ADDRESS,[e])}rename(e,t=this._defaultBehavior){return this._request(t,_.RENAME,[e])}migrate(e=this._defaultBehavior){return this._request(e,_.MIGRATE,[{appName:"Account list"}])}list(e=this._iframeBehavior){return this._request(e,_.LIST,[])}_request(e,t,s){return e.request(this._endpoint,t,s)}}R.RequestType=_,R.RedirectRequestBehavior=g,R.MSG_PREFIX="Nimiq Signed Message:\n";export default R; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).HubApi=t()}(this,function(){"use strict";class e{static byteLength(t){const[s,r]=e._getLengths(t);return e._byteLength(s,r)}static decode(t){e._initRevLookup();const[s,r]=e._getLengths(t),n=new Uint8Array(e._byteLength(s,r));let i=0;const o=r>0?s-4:s;let a=0;for(;a<o;a+=4){const s=e._revLookup[t.charCodeAt(a)]<<18|e._revLookup[t.charCodeAt(a+1)]<<12|e._revLookup[t.charCodeAt(a+2)]<<6|e._revLookup[t.charCodeAt(a+3)];n[i++]=s>>16&255,n[i++]=s>>8&255,n[i++]=255&s}if(2===r){const s=e._revLookup[t.charCodeAt(a)]<<2|e._revLookup[t.charCodeAt(a+1)]>>4;n[i++]=255&s}if(1===r){const s=e._revLookup[t.charCodeAt(a)]<<10|e._revLookup[t.charCodeAt(a+1)]<<4|e._revLookup[t.charCodeAt(a+2)]>>2;n[i++]=s>>8&255,n[i]=255&s}return n}static encode(t){const s=t.length,r=s%3,n=[];for(let i=0,o=s-r;i<o;i+=16383)n.push(e._encodeChunk(t,i,i+16383>o?o:i+16383));if(1===r){const r=t[s-1];n.push(e._lookup[r>>2]+e._lookup[r<<4&63]+"==")}else if(2===r){const r=(t[s-2]<<8)+t[s-1];n.push(e._lookup[r>>10]+e._lookup[r>>4&63]+e._lookup[r<<2&63]+"=")}return n.join("")}static encodeUrl(t){return e.encode(t).replace(/\//g,"_").replace(/\+/g,"-").replace(/=/g,".")}static decodeUrl(t){return e.decode(t.replace(/_/g,"/").replace(/-/g,"+").replace(/\./g,"="))}static _initRevLookup(){if(0===e._revLookup.length){e._revLookup=[];for(let t=0,s=e._lookup.length;t<s;t++)e._revLookup[e._lookup.charCodeAt(t)]=t;e._revLookup["-".charCodeAt(0)]=62,e._revLookup["_".charCodeAt(0)]=63}}static _getLengths(e){const t=e.length;if(t%4>0)throw new Error("Invalid string. Length must be a multiple of 4");let s=e.indexOf("=");return-1===s&&(s=t),[s,s===t?0:4-s%4]}static _byteLength(e,t){return 3*(e+t)/4-t}static _tripletToBase64(t){return e._lookup[t>>18&63]+e._lookup[t>>12&63]+e._lookup[t>>6&63]+e._lookup[63&t]}static _encodeChunk(t,s,r){const n=[];for(let i=s;i<r;i+=3){const s=(t[i]<<16&16711680)+(t[i+1]<<8&65280)+(255&t[i+2]);n.push(e._tripletToBase64(s))}return n.join("")}}var t,s;e._lookup="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",e._revLookup=[],function(e){e[e.UINT8_ARRAY=0]="UINT8_ARRAY"}(t||(t={}));class r{static stringify(e){return JSON.stringify(e,r._jsonifyType)}static parse(e){return JSON.parse(e,r._parseType)}static _parseType(s,n){if(n&&n.hasOwnProperty&&n.hasOwnProperty(r.TYPE_SYMBOL)&&n.hasOwnProperty(r.VALUE_SYMBOL))switch(n[r.TYPE_SYMBOL]){case t.UINT8_ARRAY:return e.decode(n[r.VALUE_SYMBOL])}return n}static _jsonifyType(s,n){return n instanceof Uint8Array?r._typedObject(t.UINT8_ARRAY,e.encode(n)):n}static _typedObject(e,t){const s={};return s[r.TYPE_SYMBOL]=e,s[r.VALUE_SYMBOL]=t,s}}r.TYPE_SYMBOL="__",r.VALUE_SYMBOL="v";class n{static generateRandomId(){const e=new Uint32Array(1);return crypto.getRandomValues(e),e[0]}}!function(e){e.OK="ok",e.ERROR="error"}(s||(s={}));const i="<postMessage>";class o{constructor(e=!0){this._store=e?window.sessionStorage:null,this._validIds=new Map,e&&this._restoreIds()}static _decodeIds(e){const t=r.parse(e),s=new Map;for(const e of Object.keys(t)){const r=parseInt(e,10);s.set(isNaN(r)?e:r,t[e])}return s}has(e){return this._validIds.has(e)}getCommand(e){const t=this._validIds.get(e);return t?t[0]:null}getState(e){const t=this._validIds.get(e);return t?t[1]:null}add(e,t,s=null){this._validIds.set(e,[t,s]),this._storeIds()}remove(e){this._validIds.delete(e),this._storeIds()}clear(){this._validIds.clear(),this._store&&this._store.removeItem(o.KEY)}_encodeIds(){const e=Object.create(null);for(const[t,s]of this._validIds)e[t]=s;return r.stringify(e)}_restoreIds(){const e=this._store.getItem(o.KEY);e&&(this._validIds=o._decodeIds(e))}_storeIds(){this._store&&this._store.setItem(o.KEY,this._encodeIds())}}o.KEY="rpcRequests";class a{static receiveRedirectCommand(e){const t=new URL(e.href);if(!document.referrer)return null;const s=new URL(document.referrer),n=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),n.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("command"))return null;const l=o.get("command");if(o.delete("command"),!o.has("returnURL"))return null;const h=o.get("returnURL");o.delete("returnURL");const d=h===i&&(window.opener||window.parent);if(!d&&new URL(h).origin!==s.origin)return null;let u=[];if(o.has("args"))try{u=r.parse(o.get("args"))}catch(e){}return u=Array.isArray(u)?u:[],o.delete("args"),t.search=n.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:s.origin,data:{id:c,command:l,args:u},returnURL:h,source:d?window.opener||window.parent:null}}static receiveRedirectResponse(e){const t=new URL(e.href);if(!document.referrer)return null;const n=new URL(document.referrer),i=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),i.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("status"))return null;const l=o.get("status")===s.OK?s.OK:s.ERROR;if(o.delete("status"),!o.has("result"))return null;const h=r.parse(o.get("result"));return o.delete("result"),t.search=i.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:n.origin,data:{id:c,status:l,result:h}}}static prepareRedirectReply(e,t,s){const n=new URL(e.returnURL),i=new URLSearchParams(n.hash.substring(1));return i.set("id",e.id.toString()),i.set("status",t),i.set("result",r.stringify(s)),n.hash=i.toString(),n.href}static prepareRedirectInvocation(e,t,s,n,i){const o=new URL(e),a=new URLSearchParams(o.hash.substring(1));return a.set("id",t.toString()),a.set("returnURL",s),a.set("command",n),Array.isArray(i)&&a.set("args",r.stringify(i)),o.hash=a.toString(),o.href}static _setUrlFragment(e,t){t.toString().endsWith("=")?e.hash=t.toString().slice(0,-1):e.hash=t.toString()}}a.URL_SEARCHPARAM_NAME="rpcId";class c{constructor(e,t=!1){this._allowedOrigin=e,this._waitingRequests=new o(t),this._responseHandlers=new Map,this._preserveRequests=!1}onResponse(e,t,s){this._responseHandlers.set(e,{resolve:t,reject:s})}_receive(e){if(!e.data||!e.data.status||!e.data.id||"*"!==this._allowedOrigin&&e.origin!==this._allowedOrigin)return!1;const t=e.data,r=this._getCallback(t.id),n=this._waitingRequests.getState(t.id);if(r){if(this._preserveRequests||(this._waitingRequests.remove(t.id),this._responseHandlers.delete(t.id)),console.debug("RpcClient RECEIVE",t),t.status===s.OK)r.resolve(t.result,t.id,n);else if(t.status===s.ERROR){const e=new Error(t.result.message);t.result.stack&&(e.stack=t.result.stack),t.result.name&&(e.name=t.result.name),r.reject(e,t.id,n)}return!0}return console.warn("Unknown RPC response:",t),!1}_getCallback(e){if(this._responseHandlers.has(e))return this._responseHandlers.get(e);{const t=this._waitingRequests.getCommand(e);if(t)return this._responseHandlers.get(t)}}}class l extends c{constructor(e,t){super(t),this._serverCloseCheckInterval=-1,this._target=e,this._connectionState=0,this._receiveListener=this._receive.bind(this)}async init(){2!==this._connectionState&&(await this._connect(),window.addEventListener("message",this._receiveListener),-1===this._serverCloseCheckInterval&&(this._serverCloseCheckInterval=window.setInterval(()=>this._checkIfServerClosed(),300)))}async call(e,...t){return this._call({command:e,args:t,id:n.generateRandomId()})}close(){this._connectionState=0,window.removeEventListener("message",this._receiveListener),window.clearInterval(this._serverCloseCheckInterval),this._serverCloseCheckInterval=-1;for(const[e,{reject:t}]of this._responseHandlers){const s=this._waitingRequests.getState(e);t("Connection was closed","number"==typeof e?e:void 0,s)}this._waitingRequests.clear(),this._responseHandlers.clear(),this._target&&this._target.closed&&(this._target=null)}_receive(e){return e.source===this._target&&super._receive(e)}async _call(e){if(!this._target||this._target.closed)throw new Error("Connection was closed.");if(2!==this._connectionState)throw new Error("Client is not connected, call init first");return new Promise((t,s)=>{this._responseHandlers.set(e.id,{resolve:t,reject:s}),this._waitingRequests.add(e.id,e.command),console.debug("RpcClient REQUEST",e.command,e.args),this._target.postMessage(e,this._allowedOrigin)})}_connect(){if(2!==this._connectionState)return this._connectionState=1,new Promise((e,t)=>{const r=t=>{const{source:n,origin:i,data:o}=t;if(n===this._target&&o.status===s.OK&&"pong"===o.result&&1===o.id&&("*"===this._allowedOrigin||i===this._allowedOrigin)){if(o.result.stack){const e=new Error(o.result.message);e.stack=o.result.stack,o.result.name&&(e.name=o.result.name),console.error(e)}window.removeEventListener("message",r),this._connectionState=2,console.log("RpcClient: Connection established"),e(!0)}};window.addEventListener("message",r);const n=()=>{if(2!==this._connectionState){if(0===this._connectionState||this._checkIfServerClosed())return window.removeEventListener("message",r),void t(new Error("Connection was closed"));try{this._target.postMessage({command:"ping",id:1},this._allowedOrigin)}catch(e){console.error(`postMessage failed: ${e}`)}window.setTimeout(n,100)}};window.setTimeout(n,100)})}_checkIfServerClosed(){return!(this._target&&!this._target.closed)&&(this.close(),!0)}}class h extends c{constructor(e,t,s=!0){super(t,!0),this._target=e,this._preserveRequests=s}async init(){const e=a.receiveRedirectResponse(window.location);if(e)return void this._receive(e);if(this._rejectOnBack())return;const t=new URLSearchParams(window.location.search);if(t.has(a.URL_SEARCHPARAM_NAME)){const e=window.sessionStorage.getItem(`response-${t.get(a.URL_SEARCHPARAM_NAME)}`);if(e)return void this._receive(r.parse(e),!1)}}close(){}call(e,t,s=!1,...r){this.callAndSaveLocalState(e,null,t,s,...r)}callAndSaveLocalState(e,t,s,r=!1,...i){const o=n.generateRandomId(),c=a.prepareRedirectInvocation(this._target,o,e,s,i);this._waitingRequests.add(o,s,t),r&&history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:o}),""),console.debug("RpcClient REQUEST",s,i),window.location.href=c}_receive(e,t=!0){const s=super._receive(e);return s&&t&&window.sessionStorage.setItem(`response-${e.data.id}`,r.stringify(e)),s}_rejectOnBack(){if(!history.state||!history.state.rpcBackRejectionId)return!1;const e=history.state.rpcBackRejectionId;history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:null}),"");const t=this._getCallback(e),s=this._waitingRequests.getState(e);if(t){this._preserveRequests||(this._waitingRequests.remove(e),this._responseHandlers.delete(e)),console.debug("RpcClient BACK");const r=new Error("Request aborted");return t.reject(r,e,s),!0}return!1}}class d{constructor(e){this._type=e}static getAllowedOrigin(e){return new URL(e).origin}async request(e,t,s){throw new Error("Not implemented")}}var u,_;!function(e){e[e.REDIRECT=0]="REDIRECT",e[e.POPUP=1]="POPUP",e[e.IFRAME=2]="IFRAME"}(u||(u={}));class g extends d{static withLocalState(e){return new g(void 0,e)}constructor(e,t){super(u.REDIRECT);const s=window.location;if(this._returnUrl=e||`${s.origin}${s.pathname}`,this._localState=t||{},void 0!==this._localState.__command)throw new Error("Invalid localState: Property '__command' is reserved")}async request(e,t,s){const r=d.getAllowedOrigin(e),n=new h(e,r);await n.init();const i=Object.assign({},this._localState,{__command:t});n.callAndSaveLocalState(this._returnUrl,i,t,!0,...s)}}class p extends d{constructor(e=p.DEFAULT_OPTIONS){super(u.POPUP),this._options=e}async request(e,t,s){const r=d.getAllowedOrigin(e),n=this.createPopup(e),i=new l(n,r);await i.init();try{return await i.call(t,...s)}catch(e){throw e}finally{i.close(),n.close()}}createPopup(e){const t=window.open(e,"NimiqAccounts",this._options);if(!t)throw new Error("Failed to open popup");return t}}p.DEFAULT_OPTIONS="";class w extends d{constructor(){super(u.IFRAME),this._iframe=null,this._client=null}async request(e,t,s){if(this._iframe&&this._iframe.src!==`${e}${w.IFRAME_PATH_SUFFIX}`)throw new Error("Hub iframe is already opened with another endpoint");const r=d.getAllowedOrigin(e);if(this._iframe||(this._iframe=await this.createIFrame(e)),!this._iframe.contentWindow)throw new Error(`IFrame contentWindow is ${typeof this._iframe.contentWindow}`);return this._client||(this._client=new l(this._iframe.contentWindow,r),await this._client.init()),await this._client.call(t,...s)}async createIFrame(e){return new Promise((t,s)=>{const r=document.createElement("iframe");r.name="NimiqAccountsIFrame",r.style.display="none",document.body.appendChild(r),r.src=`${e}${w.IFRAME_PATH_SUFFIX}`,r.onload=(()=>t(r)),r.onerror=s})}}w.IFRAME_PATH_SUFFIX="/iframe.html",function(e){e.LIST="list",e.LIST_CASHLINKS="list-cashlinks",e.MIGRATE="migrate",e.CHECKOUT="checkout",e.SIGN_MESSAGE="sign-message",e.SIGN_TRANSACTION="sign-transaction",e.ONBOARD="onboard",e.SIGNUP="signup",e.LOGIN="login",e.EXPORT="export",e.CHANGE_PASSWORD="change-password",e.LOGOUT="logout",e.ADD_ADDRESS="add-address",e.RENAME="rename",e.CHOOSE_ADDRESS="choose-address",e.CASHLINK="cashlink"}(_||(_={}));class R{constructor(e=R.DEFAULT_ENDPOINT,t){this._endpoint=e,this._defaultBehavior=t||new p(`left=${window.innerWidth/2-400},top=75,width=800,height=850,location=yes,dependent=yes`),this._iframeBehavior=new w,this._redirectClient=new h("",d.getAllowedOrigin(this._endpoint))}static get DEFAULT_ENDPOINT(){const e=location.origin.split(".");switch(e.shift(),e.join(".")){case"nimiq.com":return"https://hub.nimiq.com";case"nimiq-testnet.com":return"https://hub.nimiq-testnet.com";default:return"http://localhost:8080"}}checkRedirectResponse(){return this._redirectClient.init()}on(e,t,s){this._redirectClient.onResponse(e,(e,s,r)=>t(e,r),(e,t,r)=>{s&&s(e,r)})}createCashlink(e,t=this._defaultBehavior){return this._request(t,_.CASHLINK,[e])}checkout(e,t=this._defaultBehavior){return this._request(t,_.CHECKOUT,[e])}chooseAddress(e,t=this._defaultBehavior){return this._request(t,_.CHOOSE_ADDRESS,[e])}signTransaction(e,t=this._defaultBehavior){return this._request(t,_.SIGN_TRANSACTION,[e])}signMessage(e,t=this._defaultBehavior){return this._request(t,_.SIGN_MESSAGE,[e])}onboard(e,t=this._defaultBehavior){return this._request(t,_.ONBOARD,[e])}signup(e,t=this._defaultBehavior){return this._request(t,_.SIGNUP,[e])}login(e,t=this._defaultBehavior){return this._request(t,_.LOGIN,[e])}logout(e,t=this._defaultBehavior){return this._request(t,_.LOGOUT,[e])}export(e,t=this._defaultBehavior){return this._request(t,_.EXPORT,[e])}changePassword(e,t=this._defaultBehavior){return this._request(t,_.CHANGE_PASSWORD,[e])}addAddress(e,t=this._defaultBehavior){return this._request(t,_.ADD_ADDRESS,[e])}rename(e,t=this._defaultBehavior){return this._request(t,_.RENAME,[e])}migrate(e=this._defaultBehavior){return this._request(e,_.MIGRATE,[{appName:"Account list"}])}list(e=this._iframeBehavior){return this._request(e,_.LIST,[])}cashlinks(e=this._iframeBehavior){return this._request(e,_.LIST_CASHLINKS,[])}_request(e,t,s){return e.request(this._endpoint,t,s)}}return R.RequestType=_,R.RedirectRequestBehavior=g,R.MSG_PREFIX="Nimiq Signed Message:\n",R}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).HubApi=t()}(this,function(){"use strict";class e{static byteLength(t){const[s,r]=e._getLengths(t);return e._byteLength(s,r)}static decode(t){e._initRevLookup();const[s,r]=e._getLengths(t),n=new Uint8Array(e._byteLength(s,r));let i=0;const o=r>0?s-4:s;let a=0;for(;a<o;a+=4){const s=e._revLookup[t.charCodeAt(a)]<<18|e._revLookup[t.charCodeAt(a+1)]<<12|e._revLookup[t.charCodeAt(a+2)]<<6|e._revLookup[t.charCodeAt(a+3)];n[i++]=s>>16&255,n[i++]=s>>8&255,n[i++]=255&s}if(2===r){const s=e._revLookup[t.charCodeAt(a)]<<2|e._revLookup[t.charCodeAt(a+1)]>>4;n[i++]=255&s}if(1===r){const s=e._revLookup[t.charCodeAt(a)]<<10|e._revLookup[t.charCodeAt(a+1)]<<4|e._revLookup[t.charCodeAt(a+2)]>>2;n[i++]=s>>8&255,n[i]=255&s}return n}static encode(t){const s=t.length,r=s%3,n=[];for(let i=0,o=s-r;i<o;i+=16383)n.push(e._encodeChunk(t,i,i+16383>o?o:i+16383));if(1===r){const r=t[s-1];n.push(e._lookup[r>>2]+e._lookup[r<<4&63]+"==")}else if(2===r){const r=(t[s-2]<<8)+t[s-1];n.push(e._lookup[r>>10]+e._lookup[r>>4&63]+e._lookup[r<<2&63]+"=")}return n.join("")}static encodeUrl(t){return e.encode(t).replace(/\//g,"_").replace(/\+/g,"-").replace(/=/g,".")}static decodeUrl(t){return e.decode(t.replace(/_/g,"/").replace(/-/g,"+").replace(/\./g,"="))}static _initRevLookup(){if(0===e._revLookup.length){e._revLookup=[];for(let t=0,s=e._lookup.length;t<s;t++)e._revLookup[e._lookup.charCodeAt(t)]=t;e._revLookup["-".charCodeAt(0)]=62,e._revLookup["_".charCodeAt(0)]=63}}static _getLengths(e){const t=e.length;if(t%4>0)throw new Error("Invalid string. Length must be a multiple of 4");let s=e.indexOf("=");return-1===s&&(s=t),[s,s===t?0:4-s%4]}static _byteLength(e,t){return 3*(e+t)/4-t}static _tripletToBase64(t){return e._lookup[t>>18&63]+e._lookup[t>>12&63]+e._lookup[t>>6&63]+e._lookup[63&t]}static _encodeChunk(t,s,r){const n=[];for(let i=s;i<r;i+=3){const s=(t[i]<<16&16711680)+(t[i+1]<<8&65280)+(255&t[i+2]);n.push(e._tripletToBase64(s))}return n.join("")}}var t,s;e._lookup="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",e._revLookup=[],function(e){e[e.UINT8_ARRAY=0]="UINT8_ARRAY"}(t||(t={}));class r{static stringify(e){return JSON.stringify(e,r._jsonifyType)}static parse(e){return JSON.parse(e,r._parseType)}static _parseType(s,n){if(n&&n.hasOwnProperty&&n.hasOwnProperty(r.TYPE_SYMBOL)&&n.hasOwnProperty(r.VALUE_SYMBOL))switch(n[r.TYPE_SYMBOL]){case t.UINT8_ARRAY:return e.decode(n[r.VALUE_SYMBOL])}return n}static _jsonifyType(s,n){return n instanceof Uint8Array?r._typedObject(t.UINT8_ARRAY,e.encode(n)):n}static _typedObject(e,t){const s={};return s[r.TYPE_SYMBOL]=e,s[r.VALUE_SYMBOL]=t,s}}r.TYPE_SYMBOL="__",r.VALUE_SYMBOL="v";class n{static generateRandomId(){const e=new Uint32Array(1);return crypto.getRandomValues(e),e[0]}}!function(e){e.OK="ok",e.ERROR="error"}(s||(s={}));const i="<postMessage>";class o{constructor(e=!0){this._store=e?window.sessionStorage:null,this._validIds=new Map,e&&this._restoreIds()}static _decodeIds(e){const t=r.parse(e),s=new Map;for(const e of Object.keys(t)){const r=parseInt(e,10);s.set(isNaN(r)?e:r,t[e])}return s}has(e){return this._validIds.has(e)}getCommand(e){const t=this._validIds.get(e);return t?t[0]:null}getState(e){const t=this._validIds.get(e);return t?t[1]:null}add(e,t,s=null){this._validIds.set(e,[t,s]),this._storeIds()}remove(e){this._validIds.delete(e),this._storeIds()}clear(){this._validIds.clear(),this._store&&this._store.removeItem(o.KEY)}_encodeIds(){const e=Object.create(null);for(const[t,s]of this._validIds)e[t]=s;return r.stringify(e)}_restoreIds(){const e=this._store.getItem(o.KEY);e&&(this._validIds=o._decodeIds(e))}_storeIds(){this._store&&this._store.setItem(o.KEY,this._encodeIds())}}o.KEY="rpcRequests";class a{static receiveRedirectCommand(e){const t=new URL(e.href);if(!document.referrer)return null;const s=new URL(document.referrer),n=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),n.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("command"))return null;const l=o.get("command");if(o.delete("command"),!o.has("returnURL"))return null;const h=o.get("returnURL");o.delete("returnURL");const d=h===i&&(window.opener||window.parent);if(!d&&new URL(h).origin!==s.origin)return null;let u=[];if(o.has("args"))try{u=r.parse(o.get("args"))}catch(e){}return u=Array.isArray(u)?u:[],o.delete("args"),t.search=n.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:s.origin,data:{id:c,command:l,args:u},returnURL:h,source:d?window.opener||window.parent:null}}static receiveRedirectResponse(e){const t=new URL(e.href);if(!document.referrer)return null;const n=new URL(document.referrer),i=new URLSearchParams(t.search),o=new URLSearchParams(t.hash.substring(1));if(!o.has("id"))return null;const c=parseInt(o.get("id"),10);if(o.delete("id"),i.set(a.URL_SEARCHPARAM_NAME,c.toString()),!o.has("status"))return null;const l=o.get("status")===s.OK?s.OK:s.ERROR;if(o.delete("status"),!o.has("result"))return null;const h=r.parse(o.get("result"));return o.delete("result"),t.search=i.toString(),this._setUrlFragment(t,o),history.replaceState(history.state,"",t.href),{origin:n.origin,data:{id:c,status:l,result:h}}}static prepareRedirectReply(e,t,s){const n=new URL(e.returnURL),i=new URLSearchParams(n.hash.substring(1));return i.set("id",e.id.toString()),i.set("status",t),i.set("result",r.stringify(s)),n.hash=i.toString(),n.href}static prepareRedirectInvocation(e,t,s,n,i){const o=new URL(e),a=new URLSearchParams(o.hash.substring(1));return a.set("id",t.toString()),a.set("returnURL",s),a.set("command",n),Array.isArray(i)&&a.set("args",r.stringify(i)),o.hash=a.toString(),o.href}static _setUrlFragment(e,t){t.toString().endsWith("=")?e.hash=t.toString().slice(0,-1):e.hash=t.toString()}}a.URL_SEARCHPARAM_NAME="rpcId";class c{constructor(e,t=!1){this._allowedOrigin=e,this._waitingRequests=new o(t),this._responseHandlers=new Map,this._preserveRequests=!1}onResponse(e,t,s){this._responseHandlers.set(e,{resolve:t,reject:s})}_receive(e){if(!e.data||!e.data.status||!e.data.id||"*"!==this._allowedOrigin&&e.origin!==this._allowedOrigin)return!1;const t=e.data,r=this._getCallback(t.id),n=this._waitingRequests.getState(t.id);if(r){if(this._preserveRequests||(this._waitingRequests.remove(t.id),this._responseHandlers.delete(t.id)),console.debug("RpcClient RECEIVE",t),t.status===s.OK)r.resolve(t.result,t.id,n);else if(t.status===s.ERROR){const e=new Error(t.result.message);t.result.stack&&(e.stack=t.result.stack),t.result.name&&(e.name=t.result.name),r.reject(e,t.id,n)}return!0}return console.warn("Unknown RPC response:",t),!1}_getCallback(e){if(this._responseHandlers.has(e))return this._responseHandlers.get(e);{const t=this._waitingRequests.getCommand(e);if(t)return this._responseHandlers.get(t)}}}class l extends c{constructor(e,t){super(t),this._serverCloseCheckInterval=-1,this._target=e,this._connectionState=0,this._receiveListener=this._receive.bind(this)}async init(){2!==this._connectionState&&(await this._connect(),window.addEventListener("message",this._receiveListener),-1===this._serverCloseCheckInterval&&(this._serverCloseCheckInterval=window.setInterval(()=>this._checkIfServerClosed(),300)))}async call(e,...t){return this._call({command:e,args:t,id:n.generateRandomId()})}close(){this._connectionState=0,window.removeEventListener("message",this._receiveListener),window.clearInterval(this._serverCloseCheckInterval),this._serverCloseCheckInterval=-1;for(const[e,{reject:t}]of this._responseHandlers){const s=this._waitingRequests.getState(e);t("Connection was closed","number"==typeof e?e:void 0,s)}this._waitingRequests.clear(),this._responseHandlers.clear(),this._target&&this._target.closed&&(this._target=null)}_receive(e){return e.source===this._target&&super._receive(e)}async _call(e){if(!this._target||this._target.closed)throw new Error("Connection was closed.");if(2!==this._connectionState)throw new Error("Client is not connected, call init first");return new Promise((t,s)=>{this._responseHandlers.set(e.id,{resolve:t,reject:s}),this._waitingRequests.add(e.id,e.command),console.debug("RpcClient REQUEST",e.command,e.args),this._target.postMessage(e,this._allowedOrigin)})}_connect(){if(2!==this._connectionState)return this._connectionState=1,new Promise((e,t)=>{const r=t=>{const{source:n,origin:i,data:o}=t;if(n===this._target&&o.status===s.OK&&"pong"===o.result&&1===o.id&&("*"===this._allowedOrigin||i===this._allowedOrigin)){if(o.result.stack){const e=new Error(o.result.message);e.stack=o.result.stack,o.result.name&&(e.name=o.result.name),console.error(e)}window.removeEventListener("message",r),this._connectionState=2,console.log("RpcClient: Connection established"),e(!0)}};window.addEventListener("message",r);const n=()=>{if(2!==this._connectionState){if(0===this._connectionState||this._checkIfServerClosed())return window.removeEventListener("message",r),void t(new Error("Connection was closed"));try{this._target.postMessage({command:"ping",id:1},this._allowedOrigin)}catch(e){console.error(`postMessage failed: ${e}`)}window.setTimeout(n,100)}};window.setTimeout(n,100)})}_checkIfServerClosed(){return!(this._target&&!this._target.closed)&&(this.close(),!0)}}class h extends c{constructor(e,t,s=!0){super(t,!0),this._target=e,this._preserveRequests=s}async init(){const e=a.receiveRedirectResponse(window.location);if(e)return void this._receive(e);if(this._rejectOnBack())return;const t=new URLSearchParams(window.location.search);if(t.has(a.URL_SEARCHPARAM_NAME)){const e=window.sessionStorage.getItem(`response-${t.get(a.URL_SEARCHPARAM_NAME)}`);if(e)return void this._receive(r.parse(e),!1)}}close(){}call(e,t,s=!1,...r){this.callAndSaveLocalState(e,null,t,s,...r)}callAndSaveLocalState(e,t,s,r=!1,...i){const o=n.generateRandomId(),c=a.prepareRedirectInvocation(this._target,o,e,s,i);this._waitingRequests.add(o,s,t),r&&history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:o}),""),console.debug("RpcClient REQUEST",s,i),window.location.href=c}_receive(e,t=!0){const s=super._receive(e);return s&&t&&window.sessionStorage.setItem(`response-${e.data.id}`,r.stringify(e)),s}_rejectOnBack(){if(!history.state||!history.state.rpcBackRejectionId)return!1;const e=history.state.rpcBackRejectionId;history.replaceState(Object.assign({},history.state,{rpcBackRejectionId:null}),"");const t=this._getCallback(e),s=this._waitingRequests.getState(e);if(t){this._preserveRequests||(this._waitingRequests.remove(e),this._responseHandlers.delete(e)),console.debug("RpcClient BACK");const r=new Error("Request aborted");return t.reject(r,e,s),!0}return!1}}class d{constructor(e){this._type=e}static getAllowedOrigin(e){return new URL(e).origin}async request(e,t,s){throw new Error("Not implemented")}}var u,_;!function(e){e[e.REDIRECT=0]="REDIRECT",e[e.POPUP=1]="POPUP",e[e.IFRAME=2]="IFRAME"}(u||(u={}));class g extends d{static withLocalState(e){return new g(void 0,e)}constructor(e,t){super(u.REDIRECT);const s=window.location;if(this._returnUrl=e||`${s.origin}${s.pathname}`,this._localState=t||{},void 0!==this._localState.__command)throw new Error("Invalid localState: Property '__command' is reserved")}async request(e,t,s){const r=d.getAllowedOrigin(e),n=new h(e,r);await n.init();const i=Object.assign({},this._localState,{__command:t});n.callAndSaveLocalState(this._returnUrl,i,t,!0,...await Promise.all(s))}}class p extends d{constructor(e=p.DEFAULT_OPTIONS){super(u.POPUP),this._options=e}async request(e,t,s){const r=d.getAllowedOrigin(e),n=this.createPopup(e),i=new l(n,r);await i.init();try{return await i.call(t,...await Promise.all(s))}catch(e){throw e}finally{i.close(),n.close()}}createPopup(e){const t=window.open(e,"NimiqAccounts",this._options);if(!t)throw new Error("Failed to open popup");return t}}p.DEFAULT_OPTIONS="";class w extends d{constructor(){super(u.IFRAME),this._iframe=null,this._client=null}async request(e,t,s){if(this._iframe&&this._iframe.src!==`${e}${w.IFRAME_PATH_SUFFIX}`)throw new Error("Hub iframe is already opened with another endpoint");const r=d.getAllowedOrigin(e);if(this._iframe||(this._iframe=await this.createIFrame(e)),!this._iframe.contentWindow)throw new Error(`IFrame contentWindow is ${typeof this._iframe.contentWindow}`);return this._client||(this._client=new l(this._iframe.contentWindow,r),await this._client.init()),await this._client.call(t,...await Promise.all(s))}async createIFrame(e){return new Promise((t,s)=>{const r=document.createElement("iframe");r.name="NimiqAccountsIFrame",r.style.display="none",document.body.appendChild(r),r.src=`${e}${w.IFRAME_PATH_SUFFIX}`,r.onload=(()=>t(r)),r.onerror=s})}}w.IFRAME_PATH_SUFFIX="/iframe.html",function(e){e.LIST="list",e.MIGRATE="migrate",e.CHECKOUT="checkout",e.SIGN_MESSAGE="sign-message",e.SIGN_TRANSACTION="sign-transaction",e.ONBOARD="onboard",e.SIGNUP="signup",e.LOGIN="login",e.EXPORT="export",e.CHANGE_PASSWORD="change-password",e.LOGOUT="logout",e.ADD_ADDRESS="add-address",e.RENAME="rename",e.CHOOSE_ADDRESS="choose-address"}(_||(_={}));class R{constructor(e=R.DEFAULT_ENDPOINT,t){this._endpoint=e,this._defaultBehavior=t||new p(`left=${window.innerWidth/2-400},top=75,width=800,height=850,location=yes,dependent=yes`),this._iframeBehavior=new w,this._redirectClient=new h("",d.getAllowedOrigin(this._endpoint))}static get DEFAULT_ENDPOINT(){const e=location.origin.split(".");switch(e.shift(),e.join(".")){case"nimiq.com":return"https://hub.nimiq.com";case"nimiq-testnet.com":return"https://hub.nimiq-testnet.com";default:return"http://localhost:8080"}}checkRedirectResponse(){return this._redirectClient.init()}on(e,t,s){this._redirectClient.onResponse(e,(e,s,r)=>t(e,r),(e,t,r)=>{s&&s(e,r)})}checkout(e,t=this._defaultBehavior){return this._request(t,_.CHECKOUT,[e])}chooseAddress(e,t=this._defaultBehavior){return this._request(t,_.CHOOSE_ADDRESS,[e])}signTransaction(e,t=this._defaultBehavior){return this._request(t,_.SIGN_TRANSACTION,[e])}signMessage(e,t=this._defaultBehavior){return this._request(t,_.SIGN_MESSAGE,[e])}onboard(e,t=this._defaultBehavior){return this._request(t,_.ONBOARD,[e])}signup(e,t=this._defaultBehavior){return this._request(t,_.SIGNUP,[e])}login(e,t=this._defaultBehavior){return this._request(t,_.LOGIN,[e])}logout(e,t=this._defaultBehavior){return this._request(t,_.LOGOUT,[e])}export(e,t=this._defaultBehavior){return this._request(t,_.EXPORT,[e])}changePassword(e,t=this._defaultBehavior){return this._request(t,_.CHANGE_PASSWORD,[e])}addAddress(e,t=this._defaultBehavior){return this._request(t,_.ADD_ADDRESS,[e])}rename(e,t=this._defaultBehavior){return this._request(t,_.RENAME,[e])}migrate(e=this._defaultBehavior){return this._request(e,_.MIGRATE,[{appName:"Account list"}])}list(e=this._iframeBehavior){return this._request(e,_.LIST,[])}_request(e,t,s){return e.request(this._endpoint,t,s)}}return R.RequestType=_,R.RedirectRequestBehavior=g,R.MSG_PREFIX="Nimiq Signed Message:\n",R}); |
{ | ||
"name": "@nimiq/hub-api", | ||
"version": "1.1.1", | ||
"version": "1.2.0-beta.0", | ||
"main": "dist/HubApi.umd.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/HubApi.es.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69153
989
1