mochimo-wallet
Version:
Mochimo HD Wallet Implementation with React Integration
503 lines • 285 kB
JavaScript
"use strict";var Dl=Object.defineProperty;var Fl=(e,r,t)=>r in e?Dl(e,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[r]=t;var fe=(e,r,t)=>Fl(e,typeof r!="symbol"?r+"":r,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=require("react");function Tl(e){const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const t in e)if(t!=="default"){const n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:()=>e[t]})}}return r.default=e,Object.freeze(r)}const Ie=Tl($);function Rl(e){return crypto.getRandomValues(new Uint8Array(e))}function Ol(e=32){return Rl(e)}function Fn(e){e.fill(0)}var kn=(e=>(e[e.BIG_ENDIAN=0]="BIG_ENDIAN",e[e.LITTLE_ENDIAN=1]="LITTLE_ENDIAN",e))(kn||{});class rr{constructor(r){this.buf=new Uint8Array(r),this.pos=0,this.byteOrder=0}static allocate(r){return new rr(r)}static wrap(r){const t=new rr(r.length);return t.buf.set(r),t}order(r){return this.byteOrder=r,this}position(r){if(r===void 0)return this.pos;if(r<0||r>this.buf.length)throw new Error("Invalid position, position: "+r+", length: "+this.buf.length);return this.pos=r,this}capacity(){return this.buf.length}put(r,t,n){if(typeof r=="number"){if(this.pos>=this.buf.length)throw new Error("Buffer overflow");return this.buf[this.pos++]=r&255,this}const o=t||0,a=n||r.length;if(o<0||o>r.length)throw new Error("Invalid offset");if(a<0||o+a>r.length)throw new Error("Invalid length");if(this.pos+a>this.buf.length)throw new Error("Buffer overflow");return this.buf.set(r.subarray(o,o+a),this.pos),this.pos+=a,this}putInt(r){if(this.pos+4>this.buf.length)throw new Error("Buffer overflow");return this.byteOrder===0?(this.buf[this.pos++]=r>>>24&255,this.buf[this.pos++]=r>>>16&255,this.buf[this.pos++]=r>>>8&255,this.buf[this.pos++]=r&255):(this.buf[this.pos++]=r&255,this.buf[this.pos++]=r>>>8&255,this.buf[this.pos++]=r>>>16&255,this.buf[this.pos++]=r>>>24&255),this}get(r){if(this.pos+r.length>this.buf.length)throw new Error("Buffer underflow");for(let t=0;t<r.length;t++)r[t]=this.buf[this.pos++];return this}get_(){if(this.pos>=this.buf.length)throw new Error("Buffer underflow");return this.buf[this.pos++]}array(){return new Uint8Array(this.buf)}rewind(){return this.pos=0,this}}const Ba="0123456789abcdef";class Ye{static copyOf(r,t){const n=new Uint8Array(t);return n.set(r.slice(0,t)),n}static hexToBytes(r){let t=r.toLowerCase();t.startsWith("0x")&&(t=t.slice(2)),t.length%2!==0&&(t="0"+t);const n=new Uint8Array(t.length/2);for(let o=0;o<t.length;o+=2)n[o/2]=parseInt(t.slice(o,o+2),16);return n}static compareBytes(r,t){if(r.length!==t.length)return!1;for(let n=0;n<r.length;n++)if(r[n]!==t[n])return!1;return!0}static readLittleEndianUnsigned(r,t=8){const n=new Uint8Array(t);r.get(n);let o=0n;for(let a=t-1;a>=0;a--)o=o<<8n|BigInt(n[a]);return o}static trimAddress(r){return`${r.substring(0,32)}...${r.substring(r.length-24)}`}static numberToLittleEndian(r,t){const n=new Uint8Array(t);let o=r;for(let a=0;a<t;a++)n[a]=o&255,o=o>>>8;return n}static bytesToLittleEndian(r){const t=new Uint8Array(r.length);for(let n=0;n<r.length;n++)t[n]=r[r.length-1-n];return t}static fit(r,t){if(typeof r=="string"){const a=BigInt(r),c=new Uint8Array(t);let s=a;for(let u=0;u<t;u++)c[u]=Number(s&0xffn),s>>=8n;return c}const n=new Uint8Array(t),o=Math.min(r.length,t);return n.set(r.subarray(0,o)),n}static bytesToHex(r,t=0,n=r.length){const o=new Array(n*2);for(let a=0;a<n;a++){const c=r[a+t]&255;o[a*2]=Ba[c>>>4],o[a*2+1]=Ba[c&15]}return o.join("")}static toBytes(r,t){const n=r.toString(16).padStart(t*2,"0");return Ye.hexToBytes(n)}static toLittleEndian(r,t=0,n=r.length){const o=new Uint8Array(n);o.set(r.slice(t,t+n));for(let a=0;a<o.length>>1;a++){const c=o[a];o[a]=o[o.length-a-1],o[o.length-a-1]=c}return o}static clear(r){r.fill(0)}static areEqual(r,t){if(r.length!==t.length)return!1;for(let n=0;n<r.length;n++)if(r[n]!==t[n])return!1;return!0}}function Ca(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function Il(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function Jo(e,...r){if(!Il(e))throw new Error("Uint8Array expected");if(r.length>0&&!r.includes(e.length))throw new Error("Uint8Array expected of length "+r+", got length="+e.length)}function Tn(e,r=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(r&&e.finished)throw new Error("Hash#digest() has already been called")}function Ds(e,r){Jo(e);const t=r.outputLen;if(e.length<t)throw new Error("digestInto() expects output buffer of length at least "+t)}/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Nl=e=>new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4)),v0=e=>new DataView(e.buffer,e.byteOffset,e.byteLength),Ve=(e,r)=>e<<32-r|e>>>r,dn=(e,r)=>e<<r|e>>>32-r>>>0,ka=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68,Hl=e=>e<<24&4278190080|e<<8&16711680|e>>>8&65280|e>>>24&255;function Sa(e){for(let r=0;r<e.length;r++)e[r]=Hl(e[r])}function zl(e){if(typeof e!="string")throw new Error("utf8ToBytes expected string, got "+typeof e);return new Uint8Array(new TextEncoder().encode(e))}function Qo(e){return typeof e=="string"&&(e=zl(e)),Jo(e),e}class Fs{clone(){return this._cloneInto()}}function ea(e){const r=n=>e().update(Qo(n)).digest(),t=e();return r.outputLen=t.outputLen,r.blockLen=t.blockLen,r.create=()=>e(),r}function Ll(e,r,t,n){if(typeof e.setBigUint64=="function")return e.setBigUint64(r,t,n);const o=BigInt(32),a=BigInt(4294967295),c=Number(t>>o&a),s=Number(t&a),u=n?4:0,i=n?0:4;e.setUint32(r+u,c,n),e.setUint32(r+i,s,n)}const Ul=(e,r,t)=>e&r^~e&t,Pl=(e,r,t)=>e&r^e&t^r&t;class Ts extends Fs{constructor(r,t,n,o){super(),this.blockLen=r,this.outputLen=t,this.padOffset=n,this.isLE=o,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(r),this.view=v0(this.buffer)}update(r){Tn(this);const{view:t,buffer:n,blockLen:o}=this;r=Qo(r);const a=r.length;for(let c=0;c<a;){const s=Math.min(o-this.pos,a-c);if(s===o){const u=v0(r);for(;o<=a-c;c+=o)this.process(u,c);continue}n.set(r.subarray(c,c+s),this.pos),this.pos+=s,c+=s,this.pos===o&&(this.process(t,0),this.pos=0)}return this.length+=r.length,this.roundClean(),this}digestInto(r){Tn(this),Ds(r,this),this.finished=!0;const{buffer:t,view:n,blockLen:o,isLE:a}=this;let{pos:c}=this;t[c++]=128,this.buffer.subarray(c).fill(0),this.padOffset>o-c&&(this.process(n,0),c=0);for(let h=c;h<o;h++)t[h]=0;Ll(n,o-8,BigInt(this.length*8),a),this.process(n,0);const s=v0(r),u=this.outputLen;if(u%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const i=u/4,l=this.get();if(i>l.length)throw new Error("_sha2: outputLen bigger than state");for(let h=0;h<i;h++)s.setUint32(4*h,l[h],a)}digest(){const{buffer:r,outputLen:t}=this;this.digestInto(r);const n=r.slice(0,t);return this.destroy(),n}_cloneInto(r){r||(r=new this.constructor),r.set(...this.get());const{blockLen:t,buffer:n,length:o,finished:a,destroyed:c,pos:s}=this;return r.length=o,r.pos=s,r.finished=a,r.destroyed=c,o%t&&r.buffer.set(n),r}}const Ml=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),xt=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),pt=new Uint32Array(64);class Wl extends Ts{constructor(){super(64,32,8,!1),this.A=xt[0]|0,this.B=xt[1]|0,this.C=xt[2]|0,this.D=xt[3]|0,this.E=xt[4]|0,this.F=xt[5]|0,this.G=xt[6]|0,this.H=xt[7]|0}get(){const{A:r,B:t,C:n,D:o,E:a,F:c,G:s,H:u}=this;return[r,t,n,o,a,c,s,u]}set(r,t,n,o,a,c,s,u){this.A=r|0,this.B=t|0,this.C=n|0,this.D=o|0,this.E=a|0,this.F=c|0,this.G=s|0,this.H=u|0}process(r,t){for(let h=0;h<16;h++,t+=4)pt[h]=r.getUint32(t,!1);for(let h=16;h<64;h++){const d=pt[h-15],f=pt[h-2],x=Ve(d,7)^Ve(d,18)^d>>>3,w=Ve(f,17)^Ve(f,19)^f>>>10;pt[h]=w+pt[h-7]+x+pt[h-16]|0}let{A:n,B:o,C:a,D:c,E:s,F:u,G:i,H:l}=this;for(let h=0;h<64;h++){const d=Ve(s,6)^Ve(s,11)^Ve(s,25),f=l+d+Ul(s,u,i)+Ml[h]+pt[h]|0,x=(Ve(n,2)^Ve(n,13)^Ve(n,22))+Pl(n,o,a)|0;l=i,i=u,u=s,s=c+f|0,c=a,a=o,o=n,n=f+x|0}n=n+this.A|0,o=o+this.B|0,a=a+this.C|0,c=c+this.D|0,s=s+this.E|0,u=u+this.F|0,i=i+this.G|0,l=l+this.H|0,this.set(n,o,a,c,s,u,i,l)}roundClean(){pt.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}}const jl=ea(()=>new Wl),hn=BigInt(2**32-1),Da=BigInt(32);function $l(e,r=!1){return r?{h:Number(e&hn),l:Number(e>>Da&hn)}:{h:Number(e>>Da&hn)|0,l:Number(e&hn)|0}}function Kl(e,r=!1){let t=new Uint32Array(e.length),n=new Uint32Array(e.length);for(let o=0;o<e.length;o++){const{h:a,l:c}=$l(e[o],r);[t[o],n[o]]=[a,c]}return[t,n]}const Vl=(e,r,t)=>e<<t|r>>>32-t,ql=(e,r,t)=>r<<t|e>>>32-t,Gl=(e,r,t)=>r<<t-32|e>>>64-t,Zl=(e,r,t)=>e<<t-32|r>>>64-t,Rs=[],Os=[],Is=[],Yl=BigInt(0),br=BigInt(1),Xl=BigInt(2),Jl=BigInt(7),Ql=BigInt(256),eu=BigInt(113);for(let e=0,r=br,t=1,n=0;e<24;e++){[t,n]=[n,(2*t+3*n)%5],Rs.push(2*(5*n+t)),Os.push((e+1)*(e+2)/2%64);let o=Yl;for(let a=0;a<7;a++)r=(r<<br^(r>>Jl)*eu)%Ql,r&Xl&&(o^=br<<(br<<BigInt(a))-br);Is.push(o)}const[tu,ru]=Kl(Is,!0),Fa=(e,r,t)=>t>32?Gl(e,r,t):Vl(e,r,t),Ta=(e,r,t)=>t>32?Zl(e,r,t):ql(e,r,t);function nu(e,r=24){const t=new Uint32Array(10);for(let n=24-r;n<24;n++){for(let c=0;c<10;c++)t[c]=e[c]^e[c+10]^e[c+20]^e[c+30]^e[c+40];for(let c=0;c<10;c+=2){const s=(c+8)%10,u=(c+2)%10,i=t[u],l=t[u+1],h=Fa(i,l,1)^t[s],d=Ta(i,l,1)^t[s+1];for(let f=0;f<50;f+=10)e[c+f]^=h,e[c+f+1]^=d}let o=e[2],a=e[3];for(let c=0;c<24;c++){const s=Os[c],u=Fa(o,a,s),i=Ta(o,a,s),l=Rs[c];o=e[l],a=e[l+1],e[l]=u,e[l+1]=i}for(let c=0;c<50;c+=10){for(let s=0;s<10;s++)t[s]=e[c+s];for(let s=0;s<10;s++)e[c+s]^=~t[(s+2)%10]&t[(s+4)%10]}e[0]^=tu[n],e[1]^=ru[n]}t.fill(0)}class ta extends Fs{constructor(r,t,n,o=!1,a=24){if(super(),this.blockLen=r,this.suffix=t,this.outputLen=n,this.enableXOF=o,this.rounds=a,this.pos=0,this.posOut=0,this.finished=!1,this.destroyed=!1,Ca(n),0>=this.blockLen||this.blockLen>=200)throw new Error("Sha3 supports only keccak-f1600 function");this.state=new Uint8Array(200),this.state32=Nl(this.state)}keccak(){ka||Sa(this.state32),nu(this.state32,this.rounds),ka||Sa(this.state32),this.posOut=0,this.pos=0}update(r){Tn(this);const{blockLen:t,state:n}=this;r=Qo(r);const o=r.length;for(let a=0;a<o;){const c=Math.min(t-this.pos,o-a);for(let s=0;s<c;s++)n[this.pos++]^=r[a++];this.pos===t&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:r,suffix:t,pos:n,blockLen:o}=this;r[n]^=t,t&128&&n===o-1&&this.keccak(),r[o-1]^=128,this.keccak()}writeInto(r){Tn(this,!1),Jo(r),this.finish();const t=this.state,{blockLen:n}=this;for(let o=0,a=r.length;o<a;){this.posOut>=n&&this.keccak();const c=Math.min(n-this.posOut,a-o);r.set(t.subarray(this.posOut,this.posOut+c),o),this.posOut+=c,o+=c}return r}xofInto(r){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(r)}xof(r){return Ca(r),this.xofInto(new Uint8Array(r))}digestInto(r){if(Ds(r,this),this.finished)throw new Error("digest() was already called");return this.writeInto(r),this.destroy(),r}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,this.state.fill(0)}_cloneInto(r){const{blockLen:t,suffix:n,outputLen:o,rounds:a,enableXOF:c}=this;return r||(r=new ta(t,n,o,c,a)),r.state32.set(this.state32),r.pos=this.pos,r.posOut=this.posOut,r.finished=this.finished,r.rounds=a,r.suffix=n,r.outputLen=o,r.enableXOF=c,r.destroyed=this.destroyed,r}}const ou=(e,r,t)=>ea(()=>new ta(r,e,t)),au=ou(6,72,512/8),iu=new Uint8Array([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),Ns=new Uint8Array(new Array(16).fill(0).map((e,r)=>r)),su=Ns.map(e=>(9*e+5)%16);let ra=[Ns],na=[su];for(let e=0;e<4;e++)for(let r of[ra,na])r.push(r[e].map(t=>iu[t]));const Hs=[[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8],[12,13,11,15,6,9,9,7,12,15,11,13,7,8,7,7],[13,15,14,11,7,7,6,8,13,14,13,12,5,5,6,9],[14,11,12,14,8,6,5,5,15,12,15,14,9,9,8,6],[15,12,13,13,9,5,8,6,14,11,12,11,8,6,5,5]].map(e=>new Uint8Array(e)),cu=ra.map((e,r)=>e.map(t=>Hs[r][t])),lu=na.map((e,r)=>e.map(t=>Hs[r][t])),uu=new Uint32Array([0,1518500249,1859775393,2400959708,2840853838]),fu=new Uint32Array([1352829926,1548603684,1836072691,2053994217,0]);function Ra(e,r,t,n){return e===0?r^t^n:e===1?r&t|~r&n:e===2?(r|~t)^n:e===3?r&n|t&~n:r^(t|~n)}const xn=new Uint32Array(16);class du extends Ts{constructor(){super(64,20,8,!0),this.h0=1732584193,this.h1=-271733879,this.h2=-1732584194,this.h3=271733878,this.h4=-1009589776}get(){const{h0:r,h1:t,h2:n,h3:o,h4:a}=this;return[r,t,n,o,a]}set(r,t,n,o,a){this.h0=r|0,this.h1=t|0,this.h2=n|0,this.h3=o|0,this.h4=a|0}process(r,t){for(let f=0;f<16;f++,t+=4)xn[f]=r.getUint32(t,!0);let n=this.h0|0,o=n,a=this.h1|0,c=a,s=this.h2|0,u=s,i=this.h3|0,l=i,h=this.h4|0,d=h;for(let f=0;f<5;f++){const x=4-f,w=uu[f],v=fu[f],b=ra[f],p=na[f],g=cu[f],y=lu[f];for(let _=0;_<16;_++){const A=dn(n+Ra(f,a,s,i)+xn[b[_]]+w,g[_])+h|0;n=h,h=i,i=dn(s,10)|0,s=a,a=A}for(let _=0;_<16;_++){const A=dn(o+Ra(x,c,u,l)+xn[p[_]]+v,y[_])+d|0;o=d,d=l,l=dn(u,10)|0,u=c,c=A}}this.set(this.h1+s+l|0,this.h2+i+d|0,this.h3+h+o|0,this.h4+n+c|0,this.h0+a+u|0)}roundClean(){xn.fill(0)}destroy(){this.destroyed=!0,this.buffer.fill(0),this.set(0,0,0,0,0)}}const hu=ea(()=>new du);class je{constructor(r="sha256"){this.algorithm=r,this.hasher=this.createHasher(r)}createHasher(r){switch(r.toLowerCase()){case"sha256":return jl.create();case"sha3-512":return au.create();case"ripemd160":return hu.create();default:throw new Error(`Unsupported hash algorithm: ${r}`)}}update(r,t=0,n=r.length){if(t<0||t>r.length)throw new Error("Invalid offset");if(n<0||t+n>r.length)throw new Error("Invalid length");const o=r.subarray(t,t+n);this.hasher.update(o)}digest(){const r=this.hasher.digest();return this.hasher=this.createHasher(this.algorithm),r}static hash(r,t,n){const o=new je;return t!==void 0&&n!==void 0?o.update(new Uint8Array(r.subarray(t,t+n))):o.update(new Uint8Array(r)),o.digest()}static hashWith(r,t){const n=new je(r);return n.update(t),n.digest()}}const ko=class{static setChainAddr(r,t){r.position(20),r.putInt(t)}static setHashAddr(r,t){r.position(24),r.putInt(t)}static setKeyAndMask(r,t){r.position(28),r.putInt(t)}static addrToBytes(r){r.position(0);const t=new Uint8Array(r.capacity());for(let n=0;n<t.length;n+=4){const o=r.get_(),a=r.get_(),c=r.get_(),s=r.get_();t[n]=s,t[n+1]=c,t[n+2]=a,t[n+3]=o}return t}static prf(r,t,n,o){const a=new Uint8Array(96),c=new Uint8Array(32);c[31]=this.XMSS_HASH_PADDING_PRF,a.set(c,0),a.set(o,32),a.set(n,64);const s=new je;s.update(a);const u=s.digest();return r.set(u,t),r}static thashF(r,t,n,o,a,c){const s=new Uint8Array(96),u=new Uint8Array(32);u[31]=this.XMSS_HASH_PADDING_F,s.set(u,0),this.setKeyAndMask(c,0);let i=this.addrToBytes(c);this.prf(s,32,i,a),this.setKeyAndMask(c,1),i=this.addrToBytes(c);const l=new Uint8Array(32);this.prf(l,0,i,a);for(let f=0;f<32;f++)s[64+f]=n[f+o]^l[f];const h=new je;h.update(s);const d=h.digest();r.set(d,t)}};ko.XMSS_HASH_PADDING_F=0,ko.XMSS_HASH_PADDING_PRF=3;let Jt=ko;const zs=class Ar{static getTag(r){if(r.length!==2208)throw new Error("Invalid address length");const t=new Uint8Array(Ar.TAG_LENGTH);return t.set(r.subarray(r.length-Ar.TAG_LENGTH)),t}static isZero(r){return!r||r.length!==Ar.TAG_LENGTH?!1:r.every(t=>t===0)}static isValid(r){return!(!r||r.length!==Ar.TAG_LENGTH)}static tag(r,t){if(!this.isValid(t))throw new Error("Invalid tag");if(r.length!==2208)throw new Error("Invalid address length");if(t.length!==12)throw new Error("Invalid tag length");const n=new Uint8Array(r);return n.set(t,n.length-t.length),n}};zs.TAG_LENGTH=12;let Oa=zs;const at=class Ze{static gen_chain(r,t,n,o,a,c,s,u){r.set(n.subarray(o,o+Ze.PARAMSN),t);for(let i=a;i<a+c&&i<16;i++)Jt.setHashAddr(u,i),Jt.thashF(r,t,r,t,s,u)}static expand_seed(r,t){for(let n=0;n<Ze.WOTSLEN;n++){const o=Ye.toBytes(n,32);Jt.prf(r,n*32,o,t)}}static base_w(r,t){return this.base_w_(r,t,0,t.length)}static base_w_(r,t,n=0,o=t.length){let a=0,c=0,s=0,u=0;for(let i=0;i<o;i++)u===0&&(s=r[a++],u+=8),u-=4,t[c+++n]=s>>u&15;return t}static wotsChecksum(r,t){let n=0;for(let a=0;a<64;a++)n+=15-r[a];n<<=4;const o=new Uint8Array(2);return o[0]=n>>8&255,o[1]=n&255,this.base_w_(o,r,t,r.length-t)}static chain_lengths(r,t){const n=this.base_w_(r,t,0,64);return this.wotsChecksum(n,64)}static wots_pkgen(r,t,n,o,a){this.expand_seed(r,t);const c=rr.wrap(a);c.order(kn.LITTLE_ENDIAN);for(let s=0;s<Ze.WOTSLEN;s++)Jt.setChainAddr(c,s),this.gen_chain(r,s*32,r,s*32,0,15,n.subarray(o),c)}static wots_sign(r,t,n,o,a,c){const s=new Array(Ze.WOTSLEN);this.chain_lengths(t,s),this.expand_seed(r,n);const u=rr.wrap(c);u.order(kn.LITTLE_ENDIAN);for(let i=0;i<Ze.WOTSLEN;i++)Jt.setChainAddr(u,i),this.gen_chain(r,i*32,r,i*32,0,s[i],o.subarray(a),u)}static wots_pk_from_sig(r,t,n,o){const a=new Uint8Array(Ze.WOTSSIGBYTES),c=new Array(Ze.WOTSLEN),s=new Uint8Array(o),u=rr.wrap(s);u.order(kn.LITTLE_ENDIAN),this.chain_lengths(t,c);for(let i=0;i<Ze.WOTSLEN;i++)Jt.setChainAddr(u,i),this.gen_chain(a,i*32,r,i*32,c[i],15-c[i],n,u);return a}static generateAddress(r,t,n){if(!n)throw new Error("Invalid componentsGenerator");if(t.length!==32)throw new Error("Invalid secret length");if(r!==null&&r.length!==12)throw new Error("Invalid tag");const o=new Uint8Array(2144),a=n(t);Ze.wots_pkgen(o,a.private_seed,a.public_seed,0,a.addr_seed);const c=new Uint8Array(2208);c.set(o,0),c.set(a.public_seed,2144),c.set(a.addr_seed,2176);const s=r?Oa.tag(c,r):c;for(let u=0;u<10;u++)if(!this.isValid(a.private_seed,s,pn))throw new Error("Invalid WOTS");return s}static isValidWithComponents(r,t,n,o,a){if(r.length!==32)throw new Error("Invalid secret length");if(t.length!==2144)throw new Error("Invalid pk length");if(n.length!==32)throw new Error("Invalid pubSeed length");if(o.length!==32)throw new Error("Invalid rnd2 length");const c=new Uint8Array(32);a(c);const s=new Uint8Array(2144);this.wots_sign(s,c,r,n,0,o);const u=this.wots_pk_from_sig(s,c,n,o);return Ye.compareBytes(u,t)}static splitAddress(r,t,n,o,a){if(r.length!==2208)throw new Error("Invalid address length");if(t.length!==2144)throw new Error("Invalid pk length");if(n.length!==32)throw new Error("Invalid pubSeed length");if(o.length!==32)throw new Error("Invalid rnd2 length");if(a!==null&&a.length!==12)throw new Error("Invalid tag length");t.set(r.subarray(0,2144)),n.set(r.subarray(2144,2176)),o.set(r.subarray(2176,2208)),a!==null&&a.set(o.subarray(20,32))}static isValid(r,t,n=pn){const o=new Uint8Array(2144),a=new Uint8Array(32),c=new Uint8Array(32);return this.splitAddress(t,o,a,c,null),this.isValidWithComponents(r,o,a,c,pn)}static generateRandomAddress(r,t,n=pn){if(t.length!==32)throw new Error("Invalid secret length");if(r!==null&&r.length!==12)throw new Error("Invalid tag");const o=new Uint8Array(2208),a=new Uint8Array(32);n(o),a.set(o.subarray(2176,2208)),this.wots_pkgen(o,t,o,2144,a),o.set(a,2176);const c=r?Oa.tag(o,r):o;for(let s=0;s<10;s++)if(!this.isValid(t,c,n))throw new Error("Invalid WOTS");return c}};at.WOTSW=16,at.WOTSLOGW=4,at.PARAMSN=32,at.WOTSLEN1=64,at.WOTSLEN2=3,at.WOTSLEN=67,at.WOTSSIGBYTES=2144,at.TXSIGLEN=2144;let ie=at;function pn(e){for(let r=0;r<e.length;r++)e[r]=Math.floor(Math.random()*256)}const Ce=40,ke=20,w0=2144,b0=8;class Lt{constructor(){this.address=new Uint8Array(Ce),this.amount=BigInt(0)}bytes(){const r=new Uint8Array(Ce+b0);return r.set(this.address),r.set(this.getAmountBytes(),Ce),r}getTag(){return this.address.slice(0,ke)}setTag(r){this.address.set(r.slice(0,ke),0)}getAddrHash(){return this.address.slice(ke,Ce)}getAddress(){return this.address.slice(0,Ce)}setAddrHash(r){this.address.set(r.slice(0,ke),ke)}setAmountBytes(r){this.amount=BigInt(new DataView(r.buffer).getBigUint64(0,!0))}getAmount(){return this.amount}getAmountBytes(){const r=new ArrayBuffer(b0);return new DataView(r).setBigUint64(0,this.amount,!0),new Uint8Array(r)}static wotsAddressFromBytes(r){const t=new Lt;if(r.length===w0){const n=this.addrFromWots(r);n&&(t.setTag(n.slice(0,ke)),t.setAddrHash(n.slice(ke,Ce)))}else r.length===Ce?(t.setTag(r.slice(0,ke)),t.setAddrHash(r.slice(ke,Ce))):r.length===Ce+b0&&(t.setTag(r.slice(0,ke)),t.setAddrHash(r.slice(ke,Ce)),t.setAmountBytes(r.slice(Ce)));return t}static wotsAddressFromHex(r){const t=Buffer.from(r,"hex");return t.length!==Ce?new Lt:this.wotsAddressFromBytes(t)}static addrFromImplicit(r){const t=new Uint8Array(Ce);return t.set(r.slice(0,ke),0),t.set(r.slice(0,Ce-ke),ke),t}static addrHashGenerate(r){const t=je.hashWith("sha3-512",r);return je.hashWith("ripemd160",t)}static addrFromWots(r){if(r.length!==w0)return null;const t=this.addrHashGenerate(r.slice(0,w0));return this.addrFromImplicit(t)}}const xu=[0,4129,8258,12387,16516,20645,24774,28903,33032,37161,41290,45419,49548,53677,57806,61935,4657,528,12915,8786,21173,17044,29431,25302,37689,33560,45947,41818,54205,50076,62463,58334,9314,13379,1056,5121,25830,29895,17572,21637,42346,46411,34088,38153,58862,62927,50604,54669,13907,9842,5649,1584,30423,26358,22165,18100,46939,42874,38681,34616,63455,59390,55197,51132,18628,22757,26758,30887,2112,6241,10242,14371,51660,55789,59790,63919,35144,39273,43274,47403,23285,19156,31415,27286,6769,2640,14899,10770,56317,52188,64447,60318,39801,35672,47931,43802,27814,31879,19684,23749,11298,15363,3168,7233,60846,64911,52716,56781,44330,48395,36200,40265,32407,28342,24277,20212,15891,11826,7761,3696,65439,61374,57309,53244,48923,44858,40793,36728,37256,33193,45514,41451,53516,49453,61774,57711,4224,161,12482,8419,20484,16421,28742,24679,33721,37784,41979,46042,49981,54044,58239,62302,689,4752,8947,13010,16949,21012,25207,29270,46570,42443,38312,34185,62830,58703,54572,50445,13538,9411,5280,1153,29798,25671,21540,17413,42971,47098,34713,38840,59231,63358,50973,55100,9939,14066,1681,5808,26199,30326,17941,22068,55628,51565,63758,59695,39368,35305,47498,43435,22596,18533,30726,26663,6336,2273,14466,10403,52093,56156,60223,64286,35833,39896,43963,48026,19061,23124,27191,31254,2801,6864,10931,14994,64814,60687,56684,52557,48554,44427,40424,36297,31782,27655,23652,19525,15522,11395,7392,3265,61215,65342,53085,57212,44955,49082,36825,40952,28183,32310,20053,24180,11923,16050,3793,7920];function pu(e,r,t){if(r+t>e.length)throw new Error("Offset + length exceeds array bounds");let n=0;for(let o=r;o<r+t;o++){const a=e[o]&255,c=(n>>>8^a)&255;n=(n<<8^xu[c])&65535}return n}function gu(e){if(e.length>=255)throw new TypeError("Alphabet too long");const r=new Uint8Array(256);for(let i=0;i<r.length;i++)r[i]=255;for(let i=0;i<e.length;i++){const l=e.charAt(i),h=l.charCodeAt(0);if(r[h]!==255)throw new TypeError(l+" is ambiguous");r[h]=i}const t=e.length,n=e.charAt(0),o=Math.log(t)/Math.log(256),a=Math.log(256)/Math.log(t);function c(i){if(i instanceof Uint8Array||(ArrayBuffer.isView(i)?i=new Uint8Array(i.buffer,i.byteOffset,i.byteLength):Array.isArray(i)&&(i=Uint8Array.from(i))),!(i instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(i.length===0)return"";let l=0,h=0,d=0;const f=i.length;for(;d!==f&&i[d]===0;)d++,l++;const x=(f-d)*a+1>>>0,w=new Uint8Array(x);for(;d!==f;){let p=i[d],g=0;for(let y=x-1;(p!==0||g<h)&&y!==-1;y--,g++)p+=256*w[y]>>>0,w[y]=p%t>>>0,p=p/t>>>0;if(p!==0)throw new Error("Non-zero carry");h=g,d++}let v=x-h;for(;v!==x&&w[v]===0;)v++;let b=n.repeat(l);for(;v<x;++v)b+=e.charAt(w[v]);return b}function s(i){if(typeof i!="string")throw new TypeError("Expected String");if(i.length===0)return new Uint8Array;let l=0,h=0,d=0;for(;i[l]===n;)h++,l++;const f=(i.length-l)*o+1>>>0,x=new Uint8Array(f);for(;i[l];){let p=r[i.charCodeAt(l)];if(p===255)return;let g=0;for(let y=f-1;(p!==0||g<d)&&y!==-1;y--,g++)p+=t*x[y]>>>0,x[y]=p%256>>>0,p=p/256>>>0;if(p!==0)throw new Error("Non-zero carry");d=g,l++}let w=f-d;for(;w!==f&&x[w]===0;)w++;const v=new Uint8Array(h+(f-w));let b=h;for(;w!==f;)v[b++]=x[w++];return v}function u(i){const l=s(i);if(l)return l;throw new Error("Non-base"+t+" character")}return{encode:c,decodeUnsafe:s,decode:u}}var yu="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";const vu=gu(yu);function wu(e){if(!e)return null;if(e.length!==20)throw new Error("Invalid address tag length");const r=pu(e,0,20),t=new Uint8Array(22);t.set(e);const n=[r&255,r>>8&255];return t.set(n,e.length),vu.encode(t)}let Ut=class Ls{constructor({name:r=null,wots:t=null,addrTag:n=null,secret:o=null}){var a;if(o&&o.length!==32)throw new Error("Invalid secret length");if(n&&n.length!==20)throw new Error("Invalid address tag");this.name=r,this.wots=t?new Uint8Array(t):null,this.addrTag=n?new Uint8Array(n):null,this.secret=o?new Uint8Array(o):null,this.wotsAddrHex=this.wots?Ye.bytesToHex(this.wots):null,this.addrTagHex=this.addrTag?Ye.bytesToHex(this.addrTag):null,this.mochimoAddr=this.wots?Lt.wotsAddressFromBytes(this.wots.slice(0,2144)):null,(a=this.mochimoAddr)==null||a.setTag(this.addrTag)}getName(){return this.name}getWots(){return this.wots?new Uint8Array(this.wots):null}getWotsHex(){return this.wotsAddrHex}getWotsPk(){return this.wots?new Uint8Array(this.wots.slice(0,ie.WOTSSIGBYTES)):null}getWotsPubSeed(){return this.wots?this.wots.subarray(ie.WOTSSIGBYTES,ie.WOTSSIGBYTES+32):null}getWotsAdrs(){return this.wots?this.wots.subarray(ie.WOTSSIGBYTES+32,ie.WOTSSIGBYTES+64):null}getWotsTag(){return this.wots?this.wots.subarray(ie.WOTSSIGBYTES+64-12,ie.WOTSSIGBYTES+64):null}getAddress(){return this.mochimoAddr?this.mochimoAddr.bytes().slice(0,40):null}getAddrTag(){return this.addrTag?new Uint8Array(this.addrTag):null}getAddrTagHex(){return this.addrTagHex}getAddrTagBase58(){return this.addrTag?wu(this.getAddrTag()):null}getAddrHash(){return this.mochimoAddr?this.mochimoAddr.getAddrHash():null}getSecret(){return this.secret?new Uint8Array(this.secret):null}hasSecret(){return this.secret!==null}sign(r){const t=this.secret,n=this.wots;if(!t||!n)throw new Error("Cannot sign without secret key or address");if(t.length!==32)throw new Error("Invalid sourceSeed length, expected 32, got "+t.length);if(n.length!==2208)throw new Error("Invalid sourceWots length, expected 2208, got "+n.length);n.subarray(0,ie.WOTSSIGBYTES);const o=n.subarray(ie.WOTSSIGBYTES,ie.WOTSSIGBYTES+32),a=n.subarray(ie.WOTSSIGBYTES+32,ie.WOTSSIGBYTES+64),c=new Uint8Array(ie.WOTSSIGBYTES);return ie.wots_sign(c,r,t,o,0,a),c}verify(r,t){if(!this.wots)throw new Error("Cannot verify without public key (address)");const n=this.wots,o=n.subarray(0,ie.WOTSSIGBYTES),a=n.subarray(ie.WOTSSIGBYTES,ie.WOTSSIGBYTES+32),c=n.subarray(ie.WOTSSIGBYTES+32,ie.WOTSSIGBYTES+64),s=ie.wots_pk_from_sig(t,r,a,c);return Ye.areEqual(s,o)}static componentsGenerator(r){const t=Buffer.from(r).toString("ascii"),n=je.hash(Buffer.from(t+"seed","ascii")),o=je.hash(Buffer.from(t+"publ","ascii")),a=je.hash(Buffer.from(t+"addr","ascii"));return{private_seed:n,public_seed:o,addr_seed:a}}clear(){this.secret&&Ye.clear(this.secret),this.wots&&Ye.clear(this.wots),this.addrTag&&Ye.clear(this.addrTag),this.addrTagHex&&(this.addrTagHex=null),this.wotsAddrHex&&(this.wotsAddrHex=null),this.mochimoAddr&&(this.mochimoAddr=null)}toString(){let r="Empty address";return this.wotsAddrHex?r=`${this.wotsAddrHex.substring(0,32)}...${this.wotsAddrHex.substring(this.wotsAddrHex.length-24)}`:this.addrTagHex&&(r=`tag-${this.addrTagHex}`),r}static create(r,t,n,o){if(t.length!==32)throw new Error("Invalid secret length");let a=t,c=null;const s=Buffer.from("420000000e00000001000000","hex");if(o?c=ie.generateRandomAddress(s,t,o):({private_seed:a}=this.componentsGenerator(t),c=ie.generateAddress(s,t,this.componentsGenerator)),c.length!==2208)throw new Error("Invalid sourcePK length");let u=n;if(u||(u=Lt.wotsAddressFromBytes(c.slice(0,2144)).getTag()),u.length!==20)throw new Error("Invalid tag");return new Ls({name:r,wots:c,addrTag:u,secret:a})}toJSON(){return{name:this.name,wots:this.wots,addrTag:this.addrTag,secret:this.secret,addrTagHex:this.addrTagHex,wotsAddrHex:this.wotsAddrHex}}};function Dr(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function bu(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function Gn(e,...r){if(!bu(e))throw new Error("Uint8Array expected");if(r.length>0&&!r.includes(e.length))throw new Error("Uint8Array expected of length "+r+", got length="+e.length)}function Us(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");Dr(e.outputLen),Dr(e.blockLen)}function Rn(e,r=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(r&&e.finished)throw new Error("Hash#digest() has already been called")}function _u(e,r){Gn(e);const t=r.outputLen;if(e.length<t)throw new Error("digestInto() expects output buffer of length at least "+t)}/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Sn=e=>new DataView(e.buffer,e.byteOffset,e.byteLength),qe=(e,r)=>e<<32-r|e>>>r,mu=async()=>{};async function Eu(e,r,t){let n=Date.now();for(let o=0;o<e;o++){t(o);const a=Date.now()-n;a>=0&&a<r||(await mu(),n+=a)}}function Au(e){if(typeof e!="string")throw new Error("utf8ToBytes expected string, got "+typeof e);return new Uint8Array(new TextEncoder().encode(e))}function Ir(e){return typeof e=="string"&&(e=Au(e)),Gn(e),e}class Ps{clone(){return this._cloneInto()}}function Bu(e,r){if(r!==void 0&&{}.toString.call(r)!=="[object Object]")throw new Error("Options should be object or undefined");return Object.assign(e,r)}function Ms(e){const r=n=>e().update(Ir(n)).digest(),t=e();return r.outputLen=t.outputLen,r.blockLen=t.blockLen,r.create=()=>e(),r}class Ws extends Ps{constructor(r,t){super(),this.finished=!1,this.destroyed=!1,Us(r);const n=Ir(t);if(this.iHash=r.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const o=this.blockLen,a=new Uint8Array(o);a.set(n.length>o?r.create().update(n).digest():n);for(let c=0;c<a.length;c++)a[c]^=54;this.iHash.update(a),this.oHash=r.create();for(let c=0;c<a.length;c++)a[c]^=106;this.oHash.update(a),a.fill(0)}update(r){return Rn(this),this.iHash.update(r),this}digestInto(r){Rn(this),Gn(r,this.outputLen),this.finished=!0,this.iHash.digestInto(r),this.oHash.update(r),this.oHash.digestInto(r),this.destroy()}digest(){const r=new Uint8Array(this.oHash.outputLen);return this.digestInto(r),r}_cloneInto(r){r||(r=Object.create(Object.getPrototypeOf(this),{}));const{oHash:t,iHash:n,finished:o,destroyed:a,blockLen:c,outputLen:s}=this;return r=r,r.finished=o,r.destroyed=a,r.blockLen=c,r.outputLen=s,r.oHash=t._cloneInto(r.oHash),r.iHash=n._cloneInto(r.iHash),r}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const js=(e,r,t)=>new Ws(e,r).update(t).digest();js.create=(e,r)=>new Ws(e,r);function Cu(e,r,t,n){Us(e);const o=Bu({dkLen:32,asyncTick:10},n),{c:a,dkLen:c,asyncTick:s}=o;if(Dr(a),Dr(c),Dr(s),a<1)throw new Error("PBKDF2: iterations (c) should be >= 1");const u=Ir(r),i=Ir(t),l=new Uint8Array(c),h=js.create(e,u),d=h._cloneInto().update(i);return{c:a,dkLen:c,asyncTick:s,DK:l,PRF:h,PRFSalt:d}}function ku(e,r,t,n,o){return e.destroy(),r.destroy(),n&&n.destroy(),o.fill(0),t}async function Su(e,r,t,n){const{c:o,dkLen:a,asyncTick:c,DK:s,PRF:u,PRFSalt:i}=Cu(e,r,t,n);let l;const h=new Uint8Array(4),d=Sn(h),f=new Uint8Array(u.outputLen);for(let x=1,w=0;w<a;x++,w+=u.outputLen){const v=s.subarray(w,w+u.outputLen);d.setInt32(0,x,!1),(l=i._cloneInto(l)).update(h).digestInto(f),v.set(f.subarray(0,v.length)),await Eu(o-1,c,()=>{u._cloneInto(l).update(f).digestInto(f);for(let b=0;b<v.length;b++)v[b]^=f[b]})}return ku(u,i,s,l,f)}function Du(e,r,t,n){if(typeof e.setBigUint64=="function")return e.setBigUint64(r,t,n);const o=BigInt(32),a=BigInt(4294967295),c=Number(t>>o&a),s=Number(t&a),u=n?4:0,i=n?0:4;e.setUint32(r+u,c,n),e.setUint32(r+i,s,n)}const Fu=(e,r,t)=>e&r^~e&t,Tu=(e,r,t)=>e&r^e&t^r&t;class $s extends Ps{constructor(r,t,n,o){super(),this.blockLen=r,this.outputLen=t,this.padOffset=n,this.isLE=o,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(r),this.view=Sn(this.buffer)}update(r){Rn(this);const{view:t,buffer:n,blockLen:o}=this;r=Ir(r);const a=r.length;for(let c=0;c<a;){const s=Math.min(o-this.pos,a-c);if(s===o){const u=Sn(r);for(;o<=a-c;c+=o)this.process(u,c);continue}n.set(r.subarray(c,c+s),this.pos),this.pos+=s,c+=s,this.pos===o&&(this.process(t,0),this.pos=0)}return this.length+=r.length,this.roundClean(),this}digestInto(r){Rn(this),_u(r,this),this.finished=!0;const{buffer:t,view:n,blockLen:o,isLE:a}=this;let{pos:c}=this;t[c++]=128,this.buffer.subarray(c).fill(0),this.padOffset>o-c&&(this.process(n,0),c=0);for(let h=c;h<o;h++)t[h]=0;Du(n,o-8,BigInt(this.length*8),a),this.process(n,0);const s=Sn(r),u=this.outputLen;if(u%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const i=u/4,l=this.get();if(i>l.length)throw new Error("_sha2: outputLen bigger than state");for(let h=0;h<i;h++)s.setUint32(4*h,l[h],a)}digest(){const{buffer:r,outputLen:t}=this;this.digestInto(r);const n=r.slice(0,t);return this.destroy(),n}_cloneInto(r){r||(r=new this.constructor),r.set(...this.get());const{blockLen:t,buffer:n,length:o,finished:a,destroyed:c,pos:s}=this;return r.length=o,r.pos=s,r.finished=a,r.destroyed=c,o%t&&r.buffer.set(n),r}}const Ru=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),gt=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),yt=new Uint32Array(64);class Ou extends $s{constructor(){super(64,32,8,!1),this.A=gt[0]|0,this.B=gt[1]|0,this.C=gt[2]|0,this.D=gt[3]|0,this.E=gt[4]|0,this.F=gt[5]|0,this.G=gt[6]|0,this.H=gt[7]|0}get(){const{A:r,B:t,C:n,D:o,E:a,F:c,G:s,H:u}=this;return[r,t,n,o,a,c,s,u]}set(r,t,n,o,a,c,s,u){this.A=r|0,this.B=t|0,this.C=n|0,this.D=o|0,this.E=a|0,this.F=c|0,this.G=s|0,this.H=u|0}process(r,t){for(let h=0;h<16;h++,t+=4)yt[h]=r.getUint32(t,!1);for(let h=16;h<64;h++){const d=yt[h-15],f=yt[h-2],x=qe(d,7)^qe(d,18)^d>>>3,w=qe(f,17)^qe(f,19)^f>>>10;yt[h]=w+yt[h-7]+x+yt[h-16]|0}let{A:n,B:o,C:a,D:c,E:s,F:u,G:i,H:l}=this;for(let h=0;h<64;h++){const d=qe(s,6)^qe(s,11)^qe(s,25),f=l+d+Fu(s,u,i)+Ru[h]+yt[h]|0,w=(qe(n,2)^qe(n,13)^qe(n,22))+Tu(n,o,a)|0;l=i,i=u,u=s,s=c+f|0,c=a,a=o,o=n,n=f+w|0}n=n+this.A|0,o=o+this.B|0,a=a+this.C|0,c=c+this.D|0,s=s+this.E|0,u=u+this.F|0,i=i+this.G|0,l=l+this.H|0,this.set(n,o,a,c,s,u,i,l)}roundClean(){yt.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}}const Iu=Ms(()=>new Ou),gn=BigInt(2**32-1),So=BigInt(32);function Ks(e,r=!1){return r?{h:Number(e&gn),l:Number(e>>So&gn)}:{h:Number(e>>So&gn)|0,l:Number(e&gn)|0}}function Nu(e,r=!1){let t=new Uint32Array(e.length),n=new Uint32Array(e.length);for(let o=0;o<e.length;o++){const{h:a,l:c}=Ks(e[o],r);[t[o],n[o]]=[a,c]}return[t,n]}const Hu=(e,r)=>BigInt(e>>>0)<<So|BigInt(r>>>0),zu=(e,r,t)=>e>>>t,Lu=(e,r,t)=>e<<32-t|r>>>t,Uu=(e,r,t)=>e>>>t|r<<32-t,Pu=(e,r,t)=>e<<32-t|r>>>t,Mu=(e,r,t)=>e<<64-t|r>>>t-32,Wu=(e,r,t)=>e>>>t-32|r<<64-t,ju=(e,r)=>r,$u=(e,r)=>e,Ku=(e,r,t)=>e<<t|r>>>32-t,Vu=(e,r,t)=>r<<t|e>>>32-t,qu=(e,r,t)=>r<<t-32|e>>>64-t,Gu=(e,r,t)=>e<<t-32|r>>>64-t;function Zu(e,r,t,n){const o=(r>>>0)+(n>>>0);return{h:e+t+(o/2**32|0)|0,l:o|0}}const Yu=(e,r,t)=>(e>>>0)+(r>>>0)+(t>>>0),Xu=(e,r,t,n)=>r+t+n+(e/2**32|0)|0,Ju=(e,r,t,n)=>(e>>>0)+(r>>>0)+(t>>>0)+(n>>>0),Qu=(e,r,t,n,o)=>r+t+n+o+(e/2**32|0)|0,ef=(e,r,t,n,o)=>(e>>>0)+(r>>>0)+(t>>>0)+(n>>>0)+(o>>>0),tf=(e,r,t,n,o,a)=>r+t+n+o+a+(e/2**32|0)|0,W={fromBig:Ks,split:Nu,toBig:Hu,shrSH:zu,shrSL:Lu,rotrSH:Uu,rotrSL:Pu,rotrBH:Mu,rotrBL:Wu,rotr32H:ju,rotr32L:$u,rotlSH:Ku,rotlSL:Vu,rotlBH:qu,rotlBL:Gu,add:Zu,add3L:Yu,add3H:Xu,add4L:Ju,add4H:Qu,add5H:tf,add5L:ef},[rf,nf]=W.split(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(e=>BigInt(e))),vt=new Uint32Array(80),wt=new Uint32Array(80);class of extends $s{constructor(){super(128,64,16,!1),this.Ah=1779033703,this.Al=-205731576,this.Bh=-1150833019,this.Bl=-2067093701,this.Ch=1013904242,this.Cl=-23791573,this.Dh=-1521486534,this.Dl=1595750129,this.Eh=1359893119,this.El=-1377402159,this.Fh=-1694144372,this.Fl=725511199,this.Gh=528734635,this.Gl=-79577749,this.Hh=1541459225,this.Hl=327033209}get(){const{Ah:r,Al:t,Bh:n,Bl:o,Ch:a,Cl:c,Dh:s,Dl:u,Eh:i,El:l,Fh:h,Fl:d,Gh:f,Gl:x,Hh:w,Hl:v}=this;return[r,t,n,o,a,c,s,u,i,l,h,d,f,x,w,v]}set(r,t,n,o,a,c,s,u,i,l,h,d,f,x,w,v){this.Ah=r|0,this.Al=t|0,this.Bh=n|0,this.Bl=o|0,this.Ch=a|0,this.Cl=c|0,this.Dh=s|0,this.Dl=u|0,this.Eh=i|0,this.El=l|0,this.Fh=h|0,this.Fl=d|0,this.Gh=f|0,this.Gl=x|0,this.Hh=w|0,this.Hl=v|0}process(r,t){for(let g=0;g<16;g++,t+=4)vt[g]=r.getUint32(t),wt[g]=r.getUint32(t+=4);for(let g=16;g<80;g++){const y=vt[g-15]|0,_=wt[g-15]|0,A=W.rotrSH(y,_,1)^W.rotrSH(y,_,8)^W.shrSH(y,_,7),m=W.rotrSL(y,_,1)^W.rotrSL(y,_,8)^W.shrSL(y,_,7),C=vt[g-2]|0,S=wt[g-2]|0,E=W.rotrSH(C,S,19)^W.rotrBH(C,S,61)^W.shrSH(C,S,6),k=W.rotrSL(C,S,19)^W.rotrBL(C,S,61)^W.shrSL(C,S,6),D=W.add4L(m,k,wt[g-7],wt[g-16]),F=W.add4H(D,A,E,vt[g-7],vt[g-16]);vt[g]=F|0,wt[g]=D|0}let{Ah:n,Al:o,Bh:a,Bl:c,Ch:s,Cl:u,Dh:i,Dl:l,Eh:h,El:d,Fh:f,Fl:x,Gh:w,Gl:v,Hh:b,Hl:p}=this;for(let g=0;g<80;g++){const y=W.rotrSH(h,d,14)^W.rotrSH(h,d,18)^W.rotrBH(h,d,41),_=W.rotrSL(h,d,14)^W.rotrSL(h,d,18)^W.rotrBL(h,d,41),A=h&f^~h&w,m=d&x^~d&v,C=W.add5L(p,_,m,nf[g],wt[g]),S=W.add5H(C,b,y,A,rf[g],vt[g]),E=C|0,k=W.rotrSH(n,o,28)^W.rotrBH(n,o,34)^W.rotrBH(n,o,39),D=W.rotrSL(n,o,28)^W.rotrBL(n,o,34)^W.rotrBL(n,o,39),F=n&a^n&s^a&s,H=o&c^o&u^c&u;b=w|0,p=v|0,w=f|0,v=x|0,f=h|0,x=d|0,{h,l:d}=W.add(i|0,l|0,S|0,E|0),i=s|0,l=u|0,s=a|0,u=c|0,a=n|0,c=o|0;const L=W.add3L(E,D,H);n=W.add3H(L,S,k,F),o=L|0}({h:n,l:o}=W.add(this.Ah|0,this.Al|0,n|0,o|0)),{h:a,l:c}=W.add(this.Bh|0,this.Bl|0,a|0,c|0),{h:s,l:u}=W.add(this.Ch|0,this.Cl|0,s|0,u|0),{h:i,l}=W.add(this.Dh|0,this.Dl|0,i|0,l|0),{h,l:d}=W.add(this.Eh|0,this.El|0,h|0,d|0),{h:f,l:x}=W.add(this.Fh|0,this.Fl|0,f|0,x|0),{h:w,l:v}=W.add(this.Gh|0,this.Gl|0,w|0,v|0),{h:b,l:p}=W.add(this.Hh|0,this.Hl|0,b|0,p|0),this.set(n,o,a,c,s,u,i,l,h,d,f,x,w,v,b,p)}roundClean(){vt.fill(0),wt.fill(0)}destroy(){this.buffer.fill(0),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}}const af=Ms(()=>new of);/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */function On(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function Vs(e,r){return Array.isArray(r)?r.length===0?!0:e?r.every(t=>typeof t=="string"):r.every(t=>Number.isSafeInteger(t)):!1}function sf(e){if(typeof e!="function")throw new Error("function expected");return!0}function In(e,r){if(typeof r!="string")throw new Error(`${e}: string expected`);return!0}function ur(e){if(!Number.isSafeInteger(e))throw new Error(`invalid integer: ${e}`)}function Nn(e){if(!Array.isArray(e))throw new Error("array expected")}function Hn(e,r){if(!Vs(!0,r))throw new Error(`${e}: array of strings expected`)}function qs(e,r){if(!Vs(!1,r))throw new Error(`${e}: array of numbers expected`)}function cf(...e){const r=a=>a,t=(a,c)=>s=>a(c(s)),n=e.map(a=>a.encode).reduceRight(t,r),o=e.map(a=>a.decode).reduce(t,r);return{encode:n,decode:o}}function lf(e){const r=typeof e=="string"?e.split(""):e,t=r.length;Hn("alphabet",r);const n=new Map(r.map((o,a)=>[o,a]));return{encode:o=>(Nn(o),o.map(a=>{if(!Number.isSafeInteger(a)||a<0||a>=t)throw new Error(`alphabet.encode: digit index outside alphabet "${a}". Allowed: ${e}`);return r[a]})),decode:o=>(Nn(o),o.map(a=>{In("alphabet.decode",a);const c=n.get(a);if(c===void 0)throw new Error(`Unknown letter: "${a}". Allowed: ${e}`);return c}))}}function uf(e=""){return In("join",e),{encode:r=>(Hn("join.decode",r),r.join(e)),decode:r=>(In("join.decode",r),r.split(e))}}function ff(e,r="="){return ur(e),In("padding",r),{encode(t){for(Hn("padding.encode",t);t.length*e%8;)t.push(r);return t},decode(t){Hn("padding.decode",t);let n=t.length;if(n*e%8)throw new Error("padding: invalid, string should have whole number of bytes");for(;n>0&&t[n-1]===r;n--)if((n-1)*e%8===0)throw new Error("padding: invalid, string has too much padding");return t.slice(0,n)}}}function Do(e,r,t){if(r<2)throw new Error(`convertRadix: invalid from=${r}, base cannot be less than 2`);if(t<2)throw new Error(`convertRadix: invalid to=${t}, base cannot be less than 2`);if(Nn(e),!e.length)return[];let n=0;const o=[],a=Array.from(e,s=>{if(ur(s),s<0||s>=r)throw new Error(`invalid integer: ${s}`);return s}),c=a.length;for(;;){let s=0,u=!0;for(let i=n;i<c;i++){const l=a[i],h=r*s,d=h+l;if(!Number.isSafeInteger(d)||h/r!==s||d-l!==h)throw new Error("convertRadix: carry overflow");const f=d/t;s=d%t;const x=Math.floor(f);if(a[i]=x,!Number.isSafeInteger(x)||x*t+s!==d)throw new Error("convertRadix: carry overflow");if(u)x?u=!1:n=i;else continue}if(o.push(s),u)break}for(let s=0;s<e.length-1&&e[s]===0;s++)o.push(0);return o.reverse()}const Gs=(e,r)=>r===0?e:Gs(r,e%r),zn=(e,r)=>e+(r-Gs(e,r)),_0=(()=>{let e=[];for(let r=0;r<40;r++)e.push(2**r);return e})();function Fo(e,r,t,n){if(Nn(e),r<=0||r>32)throw new Error(`convertRadix2: wrong from=${r}`);if(t<=0||t>32)throw new Error(`convertRadix2: wrong to=${t}`);if(zn(r,t)>32)throw new Error(`convertRadix2: carry overflow from=${r} to=${t} carryBits=${zn(r,t)}`);let o=0,a=0;const c=_0[r],s=_0[t]-1,u=[];for(const i of e){if(ur(i),i>=c)throw new Error(`convertRadix2: invalid data word=${i} from=${r}`);if(o=o<<r|i,a+r>32)throw new Error(`convertRadix2: carry overflow pos=${a} from=${r}`);for(a+=r;a>=t;a-=t)u.push((o>>a-t&s)>>>0);const l=_0[a];if(l===void 0)throw new Error("invalid carry");o&=l-1}if(o=o<<t-a&s,!n&&a>=r)throw new Error("Excess padding");if(!n&&o>0)throw new Error(`Non-zero padding: ${o}`);return n&&a>0&&u.push(o>>>0),u}function df(e){ur(e);const r=2**8;return{encode:t=>{if(!On(t))throw new Error("radix.encode input should be Uint8Array");return Do(Array.from(t),r,e)},decode:t=>(qs("radix.decode",t),Uint8Array.from(Do(t,e,r)))}}function hf(e,r=!1){if(ur(e),e<=0||e>32)throw new Error("radix2: bits should be in (0..32]");if(zn(8,e)>32||zn(e,8)>32)throw new Error("radix2: carry overflow");return{encode:t=>{if(!On(t))throw new Error("radix2.encode input should be Uint8Array");return Fo(Array.from(t),8,e,!r)},decode:t=>(qs("radix2.decode",t),Uint8Array.from(Fo(t,e,8,r)))}}function xf(e,r){return ur(e),sf(r),{encode(t){if(!On(t))throw new Error("checksum.encode: input should be Uint8Array");const n=r(t).slice(0,e),o=new Uint8Array(t.length+e);return o.set(t),o.set(n,t.length),o},decode(t){if(!On(t))throw new Error("checksum.decode: input should be Uint8Array");const n=t.slice(0,-e),o=t.slice(-e),a=r(n).slice(0,e);for(let c=0;c<e;c++)if(a[c]!==o[c])throw new Error("Invalid checksum");return n}}}const yn={alphabet:lf,chain:cf,checksum:xf,convertRadix:Do,convertRadix2:Fo,radix:df,radix2:hf,join:uf,padding:ff};/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */const pf=e=>e[0]==="あいこくしん";function Zs(e){if(typeof e!="string")throw new TypeError("invalid mnemonic type: "+typeof e);return e.normalize("NFKD")}function Ys(e){const r=Zs(e),t=r.split(" ");if(![12,15,18,21,24].includes(t.length))throw new Error("Invalid mnemonic");return{nfkd:r,words:t}}function Xs(e){Gn(e,16,20,24,28,32)}const gf=e=>{const r=8-e.length/4;return new Uint8Array([Iu(e)[0]>>r<<r])};function Js(e){if(!Array.isArray(e)||e.length!==2048||typeof e[0]!="string")throw new Error("Wordlist: expected array of 2048 strings");return e.forEach(r=>{if(typeof r!="string")throw new Error("wordlist: non-string element: "+r)}),yn.chain(yn.checksum(1,gf),yn.radix2(11,!0),yn.alphabet(e))}function Qs(e,r){const{words:t}=Ys(e),n=Js(r).decode(t);return Xs(n),n}function Ia(e,r){return Xs(e),Js(r).encode(e).join(pf(r)?" ":" ")}function yf(e,r){try{Qs(e,r)}catch{return!1}return!0}const vf=e=>Zs("mnemonic"+e);function wf(e,r=""){return Su(af,Ys(e).nfkd,vf(r),{c:2048,dkLen:64})}const vn=`abandon
ability
able
about
above
absent
absorb
abstract
absurd
abuse
access
accident
account
accuse
achieve
acid
acoustic
acquire
across
act
action
actor
actress
actual
adapt
add
addict
address
adjust
admit
adult
advance
advice
aerobic
affair
afford
afraid
again
age
agent
agree
ahead
aim
air
airport
aisle
alarm
album
alcohol
alert
alien
all
alley
allow
almost
alone
alpha
already
also
alter
always
amateur
amazing
among
amount
amused
analyst
anchor
ancient
anger
angle
angry
animal
ankle
announce
annual
another
answer
antenna
antique
anxiety
any
apart
apology
appear
apple
approve
april
arch
arctic
area
arena
argue
arm
armed
armor
army
around
arrange
arrest
arrive
arrow
art
artefact
artist
artwork
ask
aspect
assault
asset
assist
assume
asthma
athlete
atom
attack
attend
attitude
attract
auction
audit
august
aunt
author
auto
autumn
average
avocado
avoid
awake
aware
away
awesome
awful
awkward
axis
baby
bachelor
bacon
badge
bag
balance
balcony
ball
bamboo
banana
banner
bar
barely
bargain
barrel
base
basic
basket
battle
beach
bean
beauty
because
become
beef
before
begin
behave
behind
believe
below
belt
bench
benefit
best
betray
better
between
beyond
bicycle
bid
bike
bind
biology
bird
birth
bitter
black
blade
blame
blanket
blast
bleak
bless
blind
blood
blossom
blouse
blue
blur
blush
board
boat
body
boil
bomb
bone
bonus
book
boost
border
boring
borrow
boss
bottom
bounce
box
boy
bracket
brain
brand
brass
brave
bread
breeze
brick
bridge
brief
bright
bring
brisk
broccoli
broken
bronze
broom
brother
brown
brush
bubble
buddy
budget
buffalo
build
bulb
bulk
bullet
bundle
bunker
burden
burger
burst
bus
business
busy
butter
buyer
buzz
cabbage
cabin
cable
cactus
cage
cake
call
calm
camera
camp
can
canal
cancel
candy
cannon
canoe
canvas
canyon
capable
capital
captain
car
carbon
card
cargo
carpet
carry
cart
case
cash
casino
castle
casual
cat
catalog
catch
category
cattle
caught
cause
caution
cave
ceiling
celery
cement
census
century
cereal
certain
chair
chalk
champion
change
chaos
chapter
charge
chase
chat
cheap
check
cheese
chef
cherry
chest
chicken
chief
child
chimney
choice
choose
chronic
chuckle
chunk
churn
cigar
cinnamon
circle
citizen
city
civil
claim
clap
clarify
claw
clay
clean
clerk
clever
click
client
cliff
climb
clinic
clip
clock
clog
close
cloth
cloud
clown
club
clump
cluster
clutch
coach
coast
coconut
code
coffee
coil
coin
collect
color
column
combine
come
comfort
comic
common
company
concert
conduct
confirm
congress
connect
consider
control
convince
cook
cool
copper
copy
coral
core
corn
correct
cost
cotton
couch
country
couple
course
cousin
cover
coyote
crack
cradle
craft
cram
crane
crash
crater
crawl
crazy
cream
credit
creek
crew
cricket
crime
crisp
critic
crop
cross
crouch
crowd
crucial
cruel
cruise
crumble
crunch
crush
cry
crystal
cube
culture
cup
cupboard
curious
current
curtain
curve
cushion
custom
cute
cycle
dad
damage
damp
dance
danger
daring
dash
daughter
dawn
day
deal
debate
debris
decade
december
decide
decline
decorate
decrease
deer
defense
define
defy
degree
delay
deliver
demand
demise
denial
dentist
deny
depart
depend
deposit
depth
deputy
derive
describe
desert
design
desk
despair
destroy
detail
detect
develop
device
devote
diagram
dial
diamond
diary
dice
diesel
diet
differ
digital
dignity
dilemma
dinner
dinosaur
direct
dirt
disagree