!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).walletProtocolUtils={})}(this,(function(t){"use strict";const e=async t=>{{const e=await Promise.resolve().then((function(){return nt}));return await e.pinHtmlFormDialog(t?.htmlFormDialog)}},r=e;const n={"SHA-1":{outputLength:20,blockSize:64},"SHA-256":{outputLength:32,blockSize:64},"SHA-384":{outputLength:48,blockSize:128},"SHA-512":{outputLength:64,blockSize:128}};function o(...t){const e=t.reduce(((t,e)=>t+e.length),0);if(0===t.length)throw new RangeError("Cannot concat no arrays");const r=new Uint8Array(e);let n=0;for(const e of t)r.set(e,n),n+=e.length;return r}function i(t,e){for(let r=0;rArray.isArray(t)||s(t)?a(t):t)):Object.keys(t).sort().map((e=>[e,a(t[e])])):t}function c(t){return JSON.stringify(a(t))}function l(t,e="SHA-256"){const r=["SHA-1","SHA-256","SHA-384","SHA-512"];if(!r.includes(e))throw RangeError(`Valid hash algorithm values are any of ${JSON.stringify(r)}`);return async function(t,e){const r=(new TextEncoder).encode(c(t)).buffer;let n="";{const t=await crypto.subtle.digest(e,r),o="0123456789abcdef";new Uint8Array(t).forEach((t=>{n+=o[t>>4]+o[15&t]}))}return n}(t,e)}Object.freeze({__proto__:null,COMMITMENT_LENGTH:256,DEFAULT_RANDOM_LENGTH:36,DEFAULT_TIMEOUT:3e4,INITIAL_PORT:29170,NONCE_LENGTH:128,PORT_LENGTH:12,PORT_SPACE:4096}),Object.freeze({__proto__:null,RPC_URL_PATH:".well-known/wallet-protocol"});class u{async randomFill(t,e,r){throw new Error("not implemented")}async randomFillBits(t,e,r){const n=Math.ceil(r/8),o=new Uint8Array(n);await this.randomFill(o,0,n),m.insertBits(o,t,0,e,r)}}class p{constructor(t,e){this.algorithm=t,this.key=e}async encrypt(t){throw new Error("not implemented")}async decrypt(t){throw new Error("not implemented")}}const h=new class extends u{async randomFill(t,e,r){const n=new Uint8Array(r);crypto.getRandomValues(n);for(let o=0;o{const e=t.match(/.{1,2}/g);if(null===e)throw new Error(`not a hex: ${t}`);return new Uint8Array(e.map((t=>parseInt(t,16))))},_=t=>t.reduce(((t,e)=>t+e.toString(16).padStart(2,"0")),""),b=t=>function(t,e=!1,r=!0){let n="";return n=(t=>{const e=[];for(let r=0;rfunction(t,e=!1){{let r=!1;if(/^[0-9a-zA-Z_-]+={0,2}$/.test(t))r=!0;else if(!/^[0-9a-zA-Z+/]*={0,2}$/.test(t))throw new Error("Not a valid base64 input");r&&(t=t.replace(/-/g,"+").replace(/_/g,"/").replace(/=/g,""));const n=new Uint8Array(atob(t).split("").map((t=>t.charCodeAt(0))));return e?(new TextDecoder).decode(n):n}}(t,!1),m={join:(...t)=>{const e=t.reduce(((t,e)=>t+e.length),0),r=new Uint8Array(e);let n=0;for(const e of t)r.set(e,n),n+=e.length;return r},split:(t,...e)=>{const r=[];let n=0;for(const o of e)r.push(t.slice(n,n+o)),n+=o;return r},insertBytes:(t,e,r,n,o)=>{for(let i=0;i{let i=Math.floor(r/8),s=r%8,a=Math.floor(n/8),c=n%8,l=t[i]??0;const u=c-s;for(let r=0;r=0?(l&128>>s)<>s;const n=e[a]&~(128>>c)|r;e[a]=n,s++,c++,s>=8&&(i++,s=0,l=t[i]??0),c>=8&&(a++,c=0)}},extractBits:(t,e,r)=>{const n=Math.ceil(r/8),o=new Uint8Array(n);return m.insertBits(t,o,e,0,r),o}},v=async(t,e,r)=>{const s=new Uint8Array(16),a=new Uint8Array(96),c=y(t),l=y(e);m.insertBytes(r,a,0,0,32),m.insertBytes(c,a,0,32,32),m.insertBytes(l,a,0,64,32);const u=await function(t,e,r,s,a="SHA-256"){return new Promise(((c,l)=>{a in n||l(new RangeError(`Valid hash algorithm values are any of ${Object.keys(n).toString()}`)),"string"==typeof t?t=(new TextEncoder).encode(t):t instanceof ArrayBuffer?t=new Uint8Array(t):ArrayBuffer.isView(t)||l(RangeError("P should be string, ArrayBuffer, TypedArray, DataView")),"string"==typeof e?e=(new TextEncoder).encode(e):e instanceof ArrayBuffer?e=new Uint8Array(e):ArrayBuffer.isView(e)?e=new Uint8Array(e.buffer,e.byteOffset,e.byteLength):l(RangeError("S should be string, ArrayBuffer, TypedArray, DataView")),crypto.subtle.importKey("raw",t,"PBKDF2",!1,["deriveBits"]).then((u=>{const p={name:"PBKDF2",hash:a,salt:e,iterations:r};crypto.subtle.deriveBits(p,u,8*s).then((t=>c(t)),(u=>{(async function(t,e,r,s,a){if(!(a in n))throw new RangeError(`Valid hash algorithm values are any of ${Object.keys(n).toString()}`);if(!Number.isInteger(r)||r<=0)throw new RangeError("c must be a positive integer");const c=n[a].outputLength;if(!Number.isInteger(s)||s<=0||s>=(2**32-1)*c)throw new RangeError("dkLen must be a positive integer < (2 ** 32 - 1) * hLen");const l=Math.ceil(s/c),u=s-(l-1)*c,p=new Array(l);0===t.byteLength&&(t=new Uint8Array(n[a].blockSize));const h=await crypto.subtle.importKey("raw",t,{name:"HMAC",hash:{name:a}},!0,["sign"]),f=async function(t,e){const r=await crypto.subtle.sign("HMAC",t,e);return new Uint8Array(r)};for(let t=0;tc(t)),(t=>l(t)))}))}),(t=>l(t)))}))}(a,s,1,32);return new Uint8Array(u)};class g{constructor(t,e,r,n,o,i,s,a){this.port=t,this.from=e,this.to=r,this.na=n,this.nb=o,this.secret=i,this.cipher=new d("aes-256-gcm",s),this.decipher=new d("aes-256-gcm",a)}async encrypt(t){return await this.cipher.encrypt(t)}async decrypt(t){return await this.decipher.decrypt(t)}toJSON(){return{from:this.from,to:this.to,port:this.port,na:b(this.na),nb:b(this.nb),secret:b(this.secret)}}async fromHash(){return await l(this.from)}async toHash(){return await l(this.to)}static async fromSecret(t,e,r,n,o,i){const s=await l(e),a=await l(r),c=await v(s,a,i),u=await v(a,s,i);return new g(t,e,r,n,o,i,c,u)}static async fromJSON(t){const e=w(t.na),r=w(t.nb),n=w(t.secret);return await this.fromSecret(t.port,t.from,t.to,e,r,n)}}class A{constructor(t,e,r){this.transport=t,this.masterKey=e,this.code=r}async send(t){return await this.transport.send(this.masterKey,this.code,t)}toJSON(){return{masterKey:this.masterKey.toJSON(),code:_(this.code)}}static async fromJSON(t,e){const r=await g.fromJSON(e.masterKey),n=y(e.code);let o;if("object"==typeof t)o=t;else{if(!(t instanceof Function))throw new Error("First param must be transport or constructor of transport");o=new t}return new A(o,r,n)}}var E=function(t,e){return E=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},E(t,e)};function S(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}E(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}function O(t){var e="function"==typeof Symbol&&Symbol.iterator,r=e&&t[e],n=0;if(r)return r.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function L(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)s.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return s}function T(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o0},enumerable:!1,configurable:!0}),e.prototype._trySubscribe=function(e){return this._throwIfClosed(),t.prototype._trySubscribe.call(this,e)},e.prototype._subscribe=function(t){return this._throwIfClosed(),this._checkFinalizedStatuses(t),this._innerSubscribe(t)},e.prototype._innerSubscribe=function(t){var e=this,r=this,n=r.hasError,o=r.isStopped,i=r.observers;return n||o?k:(this.currentObservers=null,i.push(t),new U((function(){e.currentObservers=null,N(i,t)})))},e.prototype._checkFinalizedStatuses=function(t){var e=this,r=e.hasError,n=e.thrownError,o=e.isStopped;r?t.error(n):o&&t.complete()},e.prototype.asObservable=function(){var t=new G;return t.source=this,t},e.create=function(t,e){return new Q(t,e)},e}(G),Q=function(t){function e(e,r){var n=t.call(this)||this;return n.destination=e,n.source=r,n}return S(e,t),e.prototype.next=function(t){var e,r;null===(r=null===(e=this.destination)||void 0===e?void 0:e.next)||void 0===r||r.call(e,t)},e.prototype.error=function(t){var e,r;null===(r=null===(e=this.destination)||void 0===e?void 0:e.error)||void 0===r||r.call(e,t)},e.prototype.complete=function(){var t,e;null===(e=null===(t=this.destination)||void 0===t?void 0:t.complete)||void 0===e||e.call(t)},e.prototype._subscribe=function(t){var e,r;return null!==(r=null===(e=this.source)||void 0===e?void 0:e.subscribe(t))&&void 0!==r?r:k},e}(q),tt=function(t){function e(e){var r=t.call(this)||this;return r._value=e,r}return S(e,t),Object.defineProperty(e.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),e.prototype._subscribe=function(e){var r=t.prototype._subscribe.call(this,e);return!r.closed&&e.next(this._value),r},e.prototype.getValue=function(){var t=this,e=t.hasError,r=t.thrownError,n=t._value;if(e)throw r;return this._throwIfClosed(),n},e.prototype.next=function(e){t.prototype.next.call(this,this._value=e)},e}(q);class et{constructor(t){this.fetch=async(...t)=>{if(await this.initialized,null==this.session)throw new Error("no session");return await this.session.send(...t)},this.protocol=t.protocol,this.$session=new tt(void 0),this.initialized=this.init()}async init(t,e){if(void 0===t){const t=(await Promise.resolve().then((function(){return ot}))).SessionLocalStorage;this.storage=new t(e?.localStorage)}else this.storage=t}get hasSession(){return void 0!==this.session}async createIfNotExists(){if(await this.initialized,void 0!==this.session)return this.session;const t=await this.protocol.run();return await this.setSession(t),t}async removeSession(){await this.initialized,await this.setSession()}async setSession(t){if(await this.initialized,this.session=t,null==t)await this.storage.clear();else{const e=t.toJSON();await this.storage.setSessionData(e)}this.$session.next(t)}async loadSession(){let t;await this.initialized;try{const e=await this.storage.getSessionData();null!==e&&(t=await A.fromJSON(this.protocol.transport,e))}catch(t){}await this.setSession(t)}}const rt={overlayClass:"wallet-protocol-overlay",modalClass:"wallet-modal",titleClass:"wallet-title",messageClass:"wallet-message",inputBoxClass:"wallet-input-box",inputClass:"wallet-input",buttonClass:"wallet-button"};var nt=Object.freeze({__proto__:null,pinHtmlFormDialog:async(t=rt)=>{const e=Object.assign({},t,rt),r=document.createElement("div");document.body.appendChild(r),r.className=e.overlayClass;const n=document.createElement("style");r.appendChild(n),n.innerText=".__WALLET_PROTOCOL_OVERLAY__ {\n position: absolute;\n display: flex;\n height: 100%;\n width: 100%;\n top: 0;\n left: 0;\n align-items: center;\n justify-content: center;\n background-color: #000000AA;\n font-family: 'sans-serif';\n color: #202531;\n}\n\n.__WALLET_PROTOCOL_OVERLAY__ .__WALLET_MODAL__ {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n border: 2px solid #1A1E27;\n border-radius: 5px;\n padding: 10px 20px;\n background-image: linear-gradient(to bottom left, white, #D2D6E1);\n}\n\n.__WALLET_PROTOCOL_OVERLAY__ .__WALLET_TITLE__ {\n font-weight: bold;\n padding: 5px;\n}\n\n.__WALLET_PROTOCOL_OVERLAY__ .__WALLET_MESSAGE__ {\n opacity: 0.5;\n padding: 5px;\n font-size: 15px\n}\n\n.__WALLET_PROTOCOL_OVERLAY__ .__WALLET_INPUT_BOX__ {\n display: flex;\n margin: 20px;\n height: 32px;\n}\n\n.__WALLET_PROTOCOL_OVERLAY__ .__WALLET_INPUT__ {\n border-radius: 3px;\n border-top-right-radius: 0;\n border-bottom-right-radius: 0;\n outline: none;\n padding: 5px;\n font-family: monospace;\n border: 2px solid #1A1E27;\n border-right: none;\n}\n\n.__WALLET_PROTOCOL_OVERLAY__ .__WALLET_BUTTON__ {\n height: 100%;\n padding: 5px;\n border-radius: 3px;\n border: 2px solid #1A1E27;\n border-top-left-radius: 0;\n border-bottom-left-radius: 0;\n cursor: pointer;\n}\n".replace(/__WALLET_PROTOCOL_OVERLAY__/g,e.overlayClass).replace(/__WALLET_MODAL__/g,e.modalClass).replace(/__WALLET_TITLE__/g,e.titleClass).replace(/__WALLET_MESSAGE__/g,e.messageClass).replace(/__WALLET_INPUT_BOX__/g,e.inputBoxClass).replace(/__WALLET_INPUT__/g,e.inputClass).replace(/__WALLET_BUTTON__/g,e.buttonClass);const o=document.createElement("div");r.appendChild(o),o.className=e.modalClass;const i=document.createElement("span");o.appendChild(i),i.className=e.titleClass,i.innerText="Connecting to your wallet...";const s=document.createElement("span");o.appendChild(s),s.className=e.messageClass,s.innerText="Set up your wallet on pairing mode and put the PIN here";const a=document.createElement("div");o.appendChild(a),a.className=e.inputBoxClass;const c=document.createElement("input");a.appendChild(c),c.className=e.inputClass,c.autofocus=!0,c.setAttribute("placeholder","pin...");const l=document.createElement("button");return a.appendChild(l),l.className=e.buttonClass,l.innerText="Synchronize",await new Promise(((t,e)=>{const n=e=>{document.body.removeChild(r),t(e??"")};c.addEventListener("keypress",(t=>{"Enter"===t.key&&n(c.value)})),l.addEventListener("click",(()=>n(c.value))),r.addEventListener("click",(t=>{t.target===r&&n()}))}))}});var ot=Object.freeze({__proto__:null,SessionLocalStorage:class{constructor(t){this.key="string"==typeof t?.key&&""!==t.key?t.key:"wallet-session"}async getSessionData(){const t=localStorage.getItem(this.key);if(null==t)throw new Error("no session data stored");return JSON.parse(t)}async setSessionData(t){localStorage.setItem(this.key,JSON.stringify(t))}async clear(){localStorage.removeItem(this.key)}}});t.LocalSessionManager=class extends et{constructor(t,e={}){super({protocol:t,storageOptions:{localStorage:{key:e.localStorageKey}}}),this.protocol=t}},t.SessionManager=et,t.openModal=r,t.pinDialog=e}));