UNPKG

@cmdcode/tapscript

Version:

A basic library for working with Tapscript, signatures and Bitcoin transactions.

9 lines 170 kB
var tapscript=function(t){"use strict";function e(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`Wrong positive integer: ${t}`)}function n(t,...e){if(!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(e.length>0&&!e.includes(t.length))throw new TypeError(`Expected Uint8Array of length ${e}, not of length=${t.length}`)}const r={number:e,bool:function(t){if("boolean"!=typeof t)throw new Error(`Expected boolean, not ${t}`)},bytes:n,hash:function(t){if("function"!=typeof t||"function"!=typeof t.create)throw new Error("Hash should be wrapped by utils.wrapConstructor");e(t.outputLen),e(t.blockLen)},exists:function(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")},output:function(t,e){n(t);const r=e.outputLen;if(t.length<r)throw new Error(`digestInto() expects output buffer of length at least ${r}`)}};var s=r; /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */const i=t=>new DataView(t.buffer,t.byteOffset,t.byteLength),o=(t,e)=>t<<32-e|t>>>e;if(!(68===new Uint8Array(new Uint32Array([287454020]).buffer)[0]))throw new Error("Non little-endian hardware is not supported");function a(t){if("string"==typeof t&&(t=function(t){if("string"!=typeof t)throw new TypeError("utf8ToBytes expected string, got "+typeof t);return(new TextEncoder).encode(t)}(t)),!(t instanceof Uint8Array))throw new TypeError(`Expected input type is Uint8Array (got ${typeof t})`);return t}Array.from({length:256},((t,e)=>e.toString(16).padStart(2,"0")));let u=class{clone(){return this._cloneInto()}};function c(t){const e=e=>t().update(a(e)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}let f=class extends u{constructor(t,e,n,r){super(),this.blockLen=t,this.outputLen=e,this.padOffset=n,this.isLE=r,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=i(this.buffer)}update(t){s.exists(this);const{view:e,buffer:n,blockLen:r}=this,o=(t=a(t)).length;for(let s=0;s<o;){const a=Math.min(r-this.pos,o-s);if(a!==r)n.set(t.subarray(s,s+a),this.pos),this.pos+=a,s+=a,this.pos===r&&(this.process(e,0),this.pos=0);else{const e=i(t);for(;r<=o-s;s+=r)this.process(e,s)}}return this.length+=t.length,this.roundClean(),this}digestInto(t){s.exists(this),s.output(t,this),this.finished=!0;const{buffer:e,view:n,blockLen:r,isLE:o}=this;let{pos:a}=this;e[a++]=128,this.buffer.subarray(a).fill(0),this.padOffset>r-a&&(this.process(n,0),a=0);for(let t=a;t<r;t++)e[t]=0;!function(t,e,n,r){if("function"==typeof t.setBigUint64)return t.setBigUint64(e,n,r);const s=BigInt(32),i=BigInt(4294967295),o=Number(n>>s&i),a=Number(n&i),u=r?4:0,c=r?0:4;t.setUint32(e+u,o,r),t.setUint32(e+c,a,r)}(n,r-8,BigInt(8*this.length),o),this.process(n,0);const u=i(t),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const f=c/4,h=this.get();if(f>h.length)throw new Error("_sha2: outputLen bigger than state");for(let t=0;t<f;t++)u.setUint32(4*t,h[t],o)}digest(){const{buffer:t,outputLen:e}=this;this.digestInto(t);const n=t.slice(0,e);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:e,buffer:n,length:r,finished:s,destroyed:i,pos:o}=this;return t.length=r,t.pos=o,t.finished=s,t.destroyed=i,r%e&&t.buffer.set(n),t}};const h=(t,e,n)=>t&e^t&n^e&n,d=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]),l=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),p=new Uint32Array(64);let g=class extends f{constructor(){super(64,32,8,!1),this.A=0|l[0],this.B=0|l[1],this.C=0|l[2],this.D=0|l[3],this.E=0|l[4],this.F=0|l[5],this.G=0|l[6],this.H=0|l[7]}get(){const{A:t,B:e,C:n,D:r,E:s,F:i,G:o,H:a}=this;return[t,e,n,r,s,i,o,a]}set(t,e,n,r,s,i,o,a){this.A=0|t,this.B=0|e,this.C=0|n,this.D=0|r,this.E=0|s,this.F=0|i,this.G=0|o,this.H=0|a}process(t,e){for(let n=0;n<16;n++,e+=4)p[n]=t.getUint32(e,!1);for(let t=16;t<64;t++){const e=p[t-15],n=p[t-2],r=o(e,7)^o(e,18)^e>>>3,s=o(n,17)^o(n,19)^n>>>10;p[t]=s+p[t-7]+r+p[t-16]|0}let{A:n,B:r,C:s,D:i,E:a,F:u,G:c,H:f}=this;for(let t=0;t<64;t++){const e=f+(o(a,6)^o(a,11)^o(a,25))+((l=a)&u^~l&c)+d[t]+p[t]|0,g=(o(n,2)^o(n,13)^o(n,22))+h(n,r,s)|0;f=c,c=u,u=a,a=i+e|0,i=s,s=r,r=n,n=e+g|0}var l;n=n+this.A|0,r=r+this.B|0,s=s+this.C|0,i=i+this.D|0,a=a+this.E|0,u=u+this.F|0,c=c+this.G|0,f=f+this.H|0,this.set(n,r,s,i,a,u,c,f)}roundClean(){p.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}},y=class extends g{constructor(){super(),this.A=-1056596264,this.B=914150663,this.C=812702999,this.D=-150054599,this.E=-4191439,this.F=1750603025,this.G=1694076839,this.H=-1090891868,this.outputLen=28}};const m=c((()=>new g));function w(t){if(t>Number.MAX_SAFE_INTEGER)throw new TypeError("Number exceeds safe bounds!")}c((()=>new y));const{getRandomValues:b}=crypto??globalThis.crypto??window.crypto;function v(t,e,n="be"){void 0===e&&(e=t.length),function(t,e){if(t.length>e)throw new TypeError(`Data is larger than array size: ${t.length} > ${e}`)}(t,e);const r=new Uint8Array(e).fill(0),s="be"===n?0:e-t.length;return r.set(t,s),r}function _(t){let e,n=0;const r=t.reduce(((t,e)=>t+e.length),0),s=new Uint8Array(r);for(e=0;e<t.length;e++){const r=t[e];s.set(r,n),n+=r.length}return s}const x=new TextEncoder,E=[{name:"base58",charset:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}];function S(t){for(const e of E)if(e.name===t)return e.charset;throw TypeError("Charset does not exist: "+t)}function O(t){return m(m(t))}const k={encode:function(t,e,n=!1){"string"==typeof t&&(t=x.encode(t));const r=S(e),s=r.length,i=[];let o,a,u,c="",f=0;for(o=0;o<t.length;o++)for(f=0,a=t[o],c+=a>0||(c.length^o)>0?"":"1";f in i||a>0;)u=i[f],u=u>0?256*u+a:a,a=u/s|0,i[f]=u%s,f++;for(;f-- >0;)c+=r[i[f]];return n&&c.length%4>0?c+"=".repeat(4-c.length%4):c},decode:function(t,e){const n=S(e),r=n.length,s=[],i=[];t=t.replace("=","");let o,a,u,c=0;for(o=0;o<t.length;o++){if(c=0,a=n.indexOf(t[o]),a<0)throw new Error(`Character range out of bounds: ${a}`);for(a>0||(i.length^o)>0||i.push(0);c in s||a>0;)u=s[c],u=u>0?u*r+a:a,a=u>>8,s[c]=u%256,c++}for(;c-- >0;)i.push(s[c]);return new Uint8Array(i)}},C={encode:t=>{const e=function(t){return _([t,O(t).slice(0,4)])}(t);return k.encode(e,"base58")},decode:t=>function(t){const e=t.slice(0,-4),n=t.slice(-4);if(O(e).slice(0,4).toString()!==n.toString())throw new Error("Invalid checksum!");return e}(k.decode(t,"base58"))},P="qpzry9x8gf2tvdw0s3jn54khce6mua7l",A=[996825010,642813549,513874426,1027748829,705979059],U=[{version:0,name:"bech32",const:1},{version:1,name:"bech32m",const:734539939}];function I(t){let e=1;for(let n=0;n<t.length;++n){const r=e>>25;e=(33554431&e)<<5^t[n];for(let t=0;t<5;++t)0!=(r>>t&1)&&(e^=A[t])}return e}function B(t){const e=[];let n;for(n=0;n<t.length;++n)e.push(t.charCodeAt(n)>>5);for(e.push(0),n=0;n<t.length;++n)e.push(31&t.charCodeAt(n));return e}function T(t,e,n,r=!0){const s=[];let i=0,o=0;const a=(1<<n)-1,u=(1<<e+n-1)-1;for(const r of t){if(r<0||r>>e>0)throw new Error("Failed to perform base conversion. Invalid value: "+String(r));for(i=(i<<e|r)&u,o+=e;o>=n;)o-=n,s.push(i>>o&a)}if(r)o>0&&s.push(i<<n-o&a);else if(o>=e||(i<<n-o&a)>0)throw new Error("Failed to perform base conversion. Invalid Size!");return s}function N(t,e,n){const r=e.concat(function(t,e,n){const r=I(B(t).concat(e).concat([0,0,0,0,0,0]))^n.const,s=[];for(let t=0;t<6;++t)s.push(r>>5*(5-t)&31);return s}(t,e,n));let s=t+"1";for(let t=0;t<r.length;++t)s+=P.charAt(r[t]);return s}function L(t){if(!function(t){let e,n,r=!1,s=!1;for(e=0;e<t.length;++e){if(n=t.charCodeAt(e),n<33||n>126)return!1;n>=97&&n<=122&&(r=!0),n>=65&&n<=90&&(s=!0)}return!r||!s}(t))throw new Error("Encoded string goes out of bounds!");if(!function(t){const e=t.lastIndexOf("1");return!(e<1||e+7>t.length||t.length>90)}(t=t.toLowerCase()))throw new Error("Encoded string has invalid separator!");const e=[],n=t.lastIndexOf("1"),r=t.substring(0,n);for(let r=n+1;r<t.length;++r){const n=P.indexOf(t.charAt(r));if(-1===n)throw new Error("Character idx out of bounds: "+String(r));e.push(n)}const s=U.find((t=>t.version===e[0]))??U[0];if(!function(t,e,n){return I(B(t).concat(e))===n.const}(r,e,s))throw new Error("Checksum verification failed!");return[r,e.slice(0,e.length-6)]}function Z(t){const e=(t=t.toLowerCase()).split("1",1)[0],[n,r]=L(t),s=T(r.slice(1),5,8,!1),i=s.length;switch(!0){case e!==n:throw new Error("Returned hrp string is invalid.");case null===s||i<2||i>40:throw new Error("Decoded string is invalid or out of spec.");case r[0]>16:throw new Error("Returned version bit is out of range.");default:return Uint8Array.from(s)}}const R={encode:function(t,e="bc",n=0){const r=N(e,[n,...T([...t],8,5)],U.find((t=>t.version===n))??U[0]);return Z(r),r},decode:Z,version:function(t){t=t.toLowerCase();const[e,n]=L(t);return n[0]}},H="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",j="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",D=new TextEncoder;function $(t,e=!1,n=!0){"string"==typeof t&&(t=D.encode(t));const r=e?j:H;let s="",i=0,o=0;for(let e=0;e<t.length;e++)for(o=o<<8|t[e],i+=8;i>=6;)i-=6,s+=r[o>>i&63];if(i>0)for(o<<=6-i,s+=r[63&o];i<6;)s+=n?"=":"",i+=2;return s}function z(t,e=!1){const n=e||t.includes("-")||t.includes("_")?j.split(""):H.split(""),r=(t=t.replace(/=+$/,"")).split("");let s=0,i=0;const o=[];for(let t=0;t<r.length;t++){const e=r[t],a=n.indexOf(e);if(-1===a)throw new Error("Invalid character: "+e);s+=6,i<<=6,i|=a,s>=8&&(s-=8,o.push(i>>>s&255))}return new Uint8Array(o)}const K={encode:$,decode:z},F={encode:t=>$(t,!0,!1),decode:t=>z(t,!0)},q=BigInt(0),V=BigInt(255),M=BigInt(256);function G(t,e,n="be"){void 0===e&&(e=function(t){if(t<=0xffn)return 1;if(t<=0xffffn)return 2;if(t<=0xffffffffn)return 4;if(t<=0xffffffffffffffffn)return 8;if(t<=0xffffffffffffffffffffffffffffffffn)return 16;if(t<=0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn)return 32;throw new TypeError("Must specify a fixed buffer size for bigints greater than 32 bytes.")}(t));const r="le"===n,s=new ArrayBuffer(e),i=new DataView(s);let o=r?0:e-1;for(;t>q;){const e=t&V,n=Number(e);r?i.setUint8(o++,n):i.setUint8(o--,n),t=(t-e)/M}return new Uint8Array(s)}function W(t,e,n="be"){void 0===e&&(e=function(t){if(t<=255)return 1;if(t<=65535)return 2;if(t<=4294967295)return 4;throw new TypeError("Numbers larger than 4 bytes must specify a fixed size!")}(t));const r="le"===n,s=new ArrayBuffer(e),i=new DataView(s);let o=r?0:e-1;for(;t>0;){const e=255&t;r?i.setUint8(o++,t):i.setUint8(o--,t),t=(t-e)/256}return new Uint8Array(s)}const Y=new TextEncoder,J=new TextDecoder;function Q(t){return Y.encode(t)}function X(t){return J.decode(t)}function tt(t,e){!function(t){if(null!==t.match(/[^a-fA-f0-9]/))throw new TypeError("Invalid characters in hex string: "+t);if(t.length%2!=0)throw new Error(`Length of hex string is invalid: ${t.length}`)}(t);const n=t.length/2;if(void 0===e&&(e=n),n>e)throw new TypeError(`Hex string is larger than array size: ${n} > ${e}`);return e}function et(t,e,n){if(t instanceof ArrayBuffer)return new Uint8Array(t);if(t instanceof Uint8Array)return v(t,e,n);if("string"==typeof t)return function(t,e,n="le"){e=tt(t,e);const r="le"===n,s=new ArrayBuffer(e),i=new DataView(s);let o=r?0:e-1;for(let e=0;e<t.length;e+=2){const n=t.substring(e,e+2),s=parseInt(n,16);r?i.setUint8(o++,s):i.setUint8(o--,s)}return new Uint8Array(s)}(t,e,n);if("bigint"==typeof t)return G(t,e,n);if("number"==typeof t)return W(t,e,n);if("boolean"==typeof t)return Uint8Array.of(t?1:0);throw TypeError("Unsupported format:"+String(typeof t))}let nt=class t extends Uint8Array{static{this.num=rt}static{this.big=it}static{this.bin=st}static{this.raw=ot}static{this.str=at}static{this.hex=ut}static{this.bytes=ct}static{this.json=ft}static{this.base64=ht}static{this.b64url=dt}static{this.bech32=lt}static{this.b58chk=pt}static{this.encode=Q}static{this.decode=X}static random(e=32){const n=function(t=32){if("function"==typeof b)return crypto.getRandomValues(new Uint8Array(t));throw new Error("Crypto module missing getRandomValues!")}(e);return new t(n,e)}constructor(t,e,n){super(et(t,e,n))}get arr(){return[...this]}get num(){return this.toNum()}get big(){return this.toBig()}get str(){return this.toStr()}get hex(){return this.toHex()}get raw(){return new Uint8Array(this)}get bin(){return this.toBin()}get b58chk(){return this.tob58chk()}get base64(){return this.toBase64()}get b64url(){return this.toB64url()}get digest(){return this.toHash()}get id(){return this.toHash().hex}get stream(){return new gt(this)}toNum(t="be"){return function(t){let e=0;for(let n=t.length-1;n>=0;n--)e=256*e+t[n],w(e);return e}("be"===t?this.reverse():this)}toBin(){return function(t){const e=new Array(8*t.length);let n=0;for(const r of t){if(r>255)throw new Error(`Invalid byte value: ${r}. Byte values must be between 0 and 255.`);for(let t=7;t>=0;t--,n++)e[n]=r>>t&1}return e.join("")}(this)}toBig(t="be"){return function(t){let e=BigInt(0);for(let n=t.length-1;n>=0;n--)e=e*M+BigInt(t[n]);return BigInt(e)}("be"===t?this.reverse():this)}toHash(){const e=m(this);return new t(e)}toJson(){const t=X(this);return JSON.parse(t)}toBech32(t,e=0){return R.encode(this,t,e)}toStr(){return X(this)}toHex(){return function(t){let e="";for(let n=0;n<t.length;n++)e+=t[n].toString(16).padStart(2,"0");return e}(this)}toBytes(){return new Uint8Array(this)}tob58chk(){return C.encode(this)}toBase64(){return K.encode(this)}toB64url(){return F.encode(this)}prepend(e){return t.join([t.bytes(e),this])}append(e){return t.join([this,t.bytes(e)])}slice(e,n){const r=new Uint8Array(this).slice(e,n);return new t(r)}subarray(e,n){const r=new Uint8Array(this).subarray(e,n);return new t(r)}reverse(){const e=new Uint8Array(this).reverse();return new t(e)}write(e,n){const r=t.bytes(e);this.set(r,n)}prefixSize(e){const n=t.varInt(this.length,e);return t.join([n,this])}static from(e){return new t(Uint8Array.from(e))}static of(...e){return new t(Uint8Array.of(...e))}static join(e){const n=_(e.map((e=>t.bytes(e))));return new t(n)}static varInt(e,n){if(e<253)return t.num(e,1);if(e<65536)return t.of(253,...t.num(e,2,n));if(e<4294967296)return t.of(254,...t.num(e,4,n));if(BigInt(e)<0x10000000000000000n)return t.of(255,...t.num(e,8,n));throw new Error(`Value is too large: ${e}`)}};function rt(t,e,n){return new nt(t,e,n)}function st(t,e,n){return new nt(function(t){const e=t.split("").map(Number);if(e.length%8!=0)throw new Error(`Binary array is invalid length: ${t.length}`);const n=new Uint8Array(e.length/8);for(let t=0,r=0;t<e.length;t+=8,r++){let s=0;for(let n=0;n<8;n++)s|=e[t+n]<<7-n;n[r]=s}return n}(t),e,n)}function it(t,e,n){return new nt(t,e,n)}function ot(t,e,n){return new nt(t,e,n)}function at(t,e,n){return new nt(Q(t),e,n)}function ut(t,e,n){return new nt(t,e,n)}function ct(t,e,n){return new nt(t,e,n)}function ft(t){return new nt((e=t,Q(JSON.stringify(e,((t,e)=>"bigint"==typeof e?`${e}n`:e)))));var e}function ht(t){return new nt(K.decode(t))}function dt(t){return new nt(F.decode(t))}function lt(t){return new nt(R.decode(t))}function pt(t){return new nt(C.decode(t))}let gt=class{constructor(t){this.data=nt.bytes(t),this.size=this.data.length}peek(t){if(t>this.size)throw new Error(`Size greater than stream: ${t} > ${this.size}`);return new nt(this.data.slice(0,t))}read(t){t=t??this.readSize();const e=this.peek(t);return this.data=this.data.slice(t),this.size=this.data.length,e}readSize(t){const e=this.read(1).num;switch(!0){case e>=0&&e<253:return e;case 253===e:return this.read(2).toNum(t);case 254===e:return this.read(4).toNum(t);case 255===e:return this.read(8).toNum(t);default:throw new Error(`Varint is out of range: ${e}`)}}};function yt(t,e){const n=nt.bytes(t);if(n.length!==e)throw new Error(`Invalid input size: ${n.hex} !== ${e}`)}function mt(t,e){if(e)throw new Error(t);return!1}function wt(t,...e){const n=nt.str(t).digest.raw,r=e.map((t=>nt.bytes(t)));return nt.join([n,n,nt.join(r)]).digest}const bt={OP_0:0,OP_PUSHDATA1:76,OP_PUSHDATA2:77,OP_PUSHDATA4:78,OP_1NEGATE:79,OP_SUCCESS80:80,OP_1:81,OP_2:82,OP_3:83,OP_4:84,OP_5:85,OP_6:86,OP_7:87,OP_8:88,OP_9:89,OP_10:90,OP_11:91,OP_12:92,OP_13:93,OP_14:94,OP_15:95,OP_16:96,OP_NOP:97,OP_SUCCESS98:98,OP_IF:99,OP_NOTIF:100,OP_ELSE:103,OP_ENDIF:104,OP_VERIFY:105,OP_RETURN:106,OP_TOALTSTACK:107,OP_FROMALTSTACK:108,OP_2DROP:109,OP_2DUP:110,OP_3DUP:111,OP_2OVER:112,OP_2ROT:113,OP_2SWAP:114,OP_IFDUP:115,OP_DEPTH:116,OP_DROP:117,OP_DUP:118,OP_NIP:119,OP_OVER:120,OP_PICK:121,OP_ROLL:122,OP_ROT:123,OP_SWAP:124,OP_TUCK:125,OP_SUCCESS126:126,OP_SUCCESS127:127,OP_SUCCESS128:128,OP_SUCCESS129:129,OP_SIZE:130,OP_SUCCESS131:131,OP_SUCCESS132:132,OP_SUCCESS133:133,OP_SUCCESS134:134,OP_EQUAL:135,OP_EQUALVERIFY:136,OP_SUCCESS137:137,OP_SUCCESS138:138,OP_1ADD:139,OP_1SUB:140,OP_SUCCESS141:141,OP_SUCCESS142:142,OP_NEGATE:143,OP_ABS:144,OP_NOT:145,OP_0NOTEQUAL:146,OP_ADD:147,OP_SUB:148,OP_SUCCESS149:149,OP_SUCCESS150:150,OP_SUCCESS151:151,OP_SUCCESS152:152,OP_SUCCESS153:153,OP_BOOLAND:154,OP_BOOLOR:155,OP_NUMEQUAL:156,OP_NUMEQUALVERIFY:157,OP_NUMNOTEQUAL:158,OP_LESSTHAN:159,OP_GREATERTHAN:160,OP_LESSTHANOREQUAL:161,OP_GREATERTHANOREQUAL:162,OP_MIN:163,OP_MAX:164,OP_WITHIN:165,OP_RIPEMD160:166,OP_SHA1:167,OP_SHA256:168,OP_HASH160:169,OP_HASH256:170,OP_CODESEPARATOR:171,OP_CHECKSIG:172,OP_CHECKSIGVERIFY:173,OP_CHECKMULTISIG:174,OP_CHECKMULTISIGVERIFY:175,OP_NOP1:176,OP_CHECKLOCKTIMEVERIFY:177,OP_CHECKSEQUENCEVERIFY:178,OP_NOP4:179,OP_NOP5:180,OP_NOP6:181,OP_NOP7:182,OP_NOP8:183,OP_NOP9:184,OP_NOP10:185,OP_CHECKSIGADD:186,OP_SUCCESS187:187,OP_SUCCESS188:188,OP_SUCCESS189:189,OP_SUCCESS190:190,OP_SUCCESS191:191,OP_SUCCESS192:192,OP_SUCCESS193:193,OP_SUCCESS194:194,OP_SUCCESS195:195,OP_SUCCESS196:196,OP_SUCCESS197:197,OP_SUCCESS198:198,OP_SUCCESS199:199,OP_SUCCESS200:200,OP_SUCCESS201:201,OP_SUCCESS202:202,OP_SUCCESS203:203,OP_SUCCESS204:204,OP_SUCCESS205:205,OP_SUCCESS206:206,OP_SUCCESS207:207,OP_SUCCESS208:208,OP_SUCCESS209:209,OP_SUCCESS210:210,OP_SUCCESS211:211,OP_SUCCESS212:212,OP_SUCCESS213:213,OP_SUCCESS214:214,OP_SUCCESS215:215,OP_SUCCESS216:216,OP_SUCCESS217:217,OP_SUCCESS218:218,OP_SUCCESS219:219,OP_SUCCESS220:220,OP_SUCCESS221:221,OP_SUCCESS222:222,OP_SUCCESS223:223,OP_SUCCESS224:224,OP_SUCCESS225:225,OP_SUCCESS226:226,OP_SUCCESS227:227,OP_SUCCESS228:228,OP_SUCCESS229:229,OP_SUCCESS230:230,OP_SUCCESS231:231,OP_SUCCESS232:232,OP_SUCCESS233:233,OP_SUCCESS234:234,OP_SUCCESS235:235,OP_SUCCESS236:236,OP_SUCCESS237:237,OP_SUCCESS238:238,OP_SUCCESS239:239,OP_SUCCESS240:240,OP_SUCCESS241:241,OP_SUCCESS242:242,OP_SUCCESS243:243,OP_SUCCESS244:244,OP_SUCCESS245:245,OP_SUCCESS246:246,OP_SUCCESS247:247,OP_SUCCESS248:248,OP_SUCCESS249:249,OP_SUCCESS250:250,OP_SUCCESS251:251,OP_SUCCESS252:252,OP_SUCCESS253:253,OP_SUCCESS254:254};function vt(t){if(t>186&&t<255)return"OP_SUCCESS"+String(t);for(const[e,n]of Object.entries(bt))if(n===t)return e;throw new Error("OPCODE not found:"+String(t))}function _t(t){switch(!0){case 0===t:return"opcode";case t>=1&&t<=75:return"varint";case 76===t:return"pushdata1";case 77===t:return"pushdata2";case 78===t:return"pushdata4";case t<=254:return"opcode";default:throw new Error(`Invalid word range: ${t}`)}}function xt(t){switch(!0){case"number"!=typeof t:return!1;case 0===t:return!0;case[].includes(t):return!1;case 75<t&&t<254:return!0;default:return!1}}function Et(t){return"string"==typeof t&&t.length%2==0&&/[0-9a-fA-F]/.test(t)}function St(t){return Et(t)||t instanceof Uint8Array}const Ot=520;function kt(t=[],e=!0){let n=nt.num(0);return Array.isArray(t)&&(n=nt.raw(Ct(t))),Et(t)&&(n=nt.hex(t)),t instanceof Uint8Array&&(n=nt.raw(t)),e&&(n=n.prefixSize("le")),n}function Ct(t){const e=[];for(const n of t)e.push(Pt(n));return e.length>0?nt.join(e):new Uint8Array}function Pt(t){let e=new Uint8Array;if("string"==typeof t){if(t.startsWith("OP_"))return nt.num(function(t){for(const[e,n]of Object.entries(bt))if(e===t)return Number(n);throw new Error("OPCODE not found:"+t)}(t),1);e=Et(t)?nt.hex(t):nt.str(t)}else e=nt.bytes(t);if(1===e.length)return 0!==e[0]&&e[0]<=16?(e[0]+=80,e):(e[0]>128&&e[0]<=255&&(e=new Uint8Array([e[0],0])),nt.join([At(e.length),e]));if(e.length>Ot){const t=function(t){const e=[],n=new gt(t);for(;n.size>Ot;)e.push(n.read(Ot));return e.push(n.read(n.size)),e}(e);return Ct(t)}return nt.join([At(e.length),e])}function At(t){const e=nt.num(76,1),n=nt.num(77,1);switch(!0){case t<=75:return nt.num(t);case t>75&&t<256:return nt.join([e,nt.num(t,1,"le")]);case t>=256&&t<=Ot:return nt.join([n,nt.num(t,2,"le")]);default:throw new Error("Invalid word size:"+t.toString())}}function Ut(t,e=!1){let n=nt.bytes(t);if(e){const t=n.stream.readSize("le");if(n=n.slice(1),n.length!==t)throw new Error(`Varint does not match stream size: ${t} !== ${n.length}`)}return function(t){const e=new gt(t),n=[],r=e.size;let s,i,o,a=0;for(;a<r;)switch(s=e.read(1).num,i=_t(s),a++,i){case"varint":n.push(e.read(s).hex),a+=s;break;case"pushdata1":o=e.read(1).reverse().num,n.push(e.read(o).hex),a+=o+1;break;case"pushdata2":o=e.read(2).reverse().num,n.push(e.read(o).hex),a+=o+2;break;case"pushdata4":o=e.read(4).reverse().num,n.push(e.read(o).hex),a+=o+4;break;case"opcode":if(!xt(s))throw new Error(`Invalid OPCODE: ${s}`);n.push(vt(s));break;default:throw new Error(`Word type undefined: ${s}`)}return n}(n)}const It={toAsm:function(t,e){if(Array.isArray(t)&&(t=kt(t,e)),t instanceof Uint8Array||Et(t))return Ut(t,e);throw new Error("Invalid format: "+String(typeof t))},toBytes:function(t,e){if((t instanceof Uint8Array||Et(t))&&(t=Ut(t,e)),Array.isArray(t))return kt(t,e);throw new Error("Invalid format: "+String(typeof t))},toParam:function(t){if(!Array.isArray(t))return nt.bytes(t);throw new Error("Invalid format: "+String(typeof t))}},Bt={encode:kt,decode:Ut,fmt:It};function Tt(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`Wrong positive integer: ${t}`)}function Nt(t,...e){if(!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(e.length>0&&!e.includes(t.length))throw new TypeError(`Expected Uint8Array of length ${e}, not of length=${t.length}`)}const Lt={number:Tt,bool:function(t){if("boolean"!=typeof t)throw new Error(`Expected boolean, not ${t}`)},bytes:Nt,hash:function(t){if("function"!=typeof t||"function"!=typeof t.create)throw new Error("Hash should be wrapped by utils.wrapConstructor");Tt(t.outputLen),Tt(t.blockLen)},exists:function(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")},output:function(t,e){Nt(t);const n=e.outputLen;if(t.length<n)throw new Error(`digestInto() expects output buffer of length at least ${n}`)}};var Zt=Lt;const Rt="object"==typeof globalThis&&"crypto"in globalThis?globalThis.crypto:void 0,Ht=t=>new DataView(t.buffer,t.byteOffset,t.byteLength),jt=(t,e)=>t<<32-e|t>>>e; /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */if(!(68===new Uint8Array(new Uint32Array([287454020]).buffer)[0]))throw new Error("Non little-endian hardware is not supported");function Dt(t){if("string"==typeof t&&(t=function(t){if("string"!=typeof t)throw new TypeError("utf8ToBytes expected string, got "+typeof t);return(new TextEncoder).encode(t)}(t)),!(t instanceof Uint8Array))throw new TypeError(`Expected input type is Uint8Array (got ${typeof t})`);return t}Array.from({length:256},((t,e)=>e.toString(16).padStart(2,"0")));let $t=class{clone(){return this._cloneInto()}};function zt(t){const e=e=>t().update(Dt(e)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}function Kt(t=32){if(Rt&&"function"==typeof Rt.getRandomValues)return Rt.getRandomValues(new Uint8Array(t));throw new Error("crypto.getRandomValues must be defined")}let Ft=class extends $t{constructor(t,e,n,r){super(),this.blockLen=t,this.outputLen=e,this.padOffset=n,this.isLE=r,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=Ht(this.buffer)}update(t){Zt.exists(this);const{view:e,buffer:n,blockLen:r}=this,s=(t=Dt(t)).length;for(let i=0;i<s;){const o=Math.min(r-this.pos,s-i);if(o!==r)n.set(t.subarray(i,i+o),this.pos),this.pos+=o,i+=o,this.pos===r&&(this.process(e,0),this.pos=0);else{const e=Ht(t);for(;r<=s-i;i+=r)this.process(e,i)}}return this.length+=t.length,this.roundClean(),this}digestInto(t){Zt.exists(this),Zt.output(t,this),this.finished=!0;const{buffer:e,view:n,blockLen:r,isLE:s}=this;let{pos:i}=this;e[i++]=128,this.buffer.subarray(i).fill(0),this.padOffset>r-i&&(this.process(n,0),i=0);for(let t=i;t<r;t++)e[t]=0;!function(t,e,n,r){if("function"==typeof t.setBigUint64)return t.setBigUint64(e,n,r);const s=BigInt(32),i=BigInt(4294967295),o=Number(n>>s&i),a=Number(n&i),u=r?4:0,c=r?0:4;t.setUint32(e+u,o,r),t.setUint32(e+c,a,r)}(n,r-8,BigInt(8*this.length),s),this.process(n,0);const o=Ht(t),a=this.outputLen;if(a%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const u=a/4,c=this.get();if(u>c.length)throw new Error("_sha2: outputLen bigger than state");for(let t=0;t<u;t++)o.setUint32(4*t,c[t],s)}digest(){const{buffer:t,outputLen:e}=this;this.digestInto(t);const n=t.slice(0,e);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:e,buffer:n,length:r,finished:s,destroyed:i,pos:o}=this;return t.length=r,t.pos=o,t.finished=s,t.destroyed=i,r%e&&t.buffer.set(n),t}};const qt=(t,e,n)=>t&e^t&n^e&n,Vt=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]),Mt=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),Gt=new Uint32Array(64);let Wt=class extends Ft{constructor(){super(64,32,8,!1),this.A=0|Mt[0],this.B=0|Mt[1],this.C=0|Mt[2],this.D=0|Mt[3],this.E=0|Mt[4],this.F=0|Mt[5],this.G=0|Mt[6],this.H=0|Mt[7]}get(){const{A:t,B:e,C:n,D:r,E:s,F:i,G:o,H:a}=this;return[t,e,n,r,s,i,o,a]}set(t,e,n,r,s,i,o,a){this.A=0|t,this.B=0|e,this.C=0|n,this.D=0|r,this.E=0|s,this.F=0|i,this.G=0|o,this.H=0|a}process(t,e){for(let n=0;n<16;n++,e+=4)Gt[n]=t.getUint32(e,!1);for(let t=16;t<64;t++){const e=Gt[t-15],n=Gt[t-2],r=jt(e,7)^jt(e,18)^e>>>3,s=jt(n,17)^jt(n,19)^n>>>10;Gt[t]=s+Gt[t-7]+r+Gt[t-16]|0}let{A:n,B:r,C:s,D:i,E:o,F:a,G:u,H:c}=this;for(let t=0;t<64;t++){const e=c+(jt(o,6)^jt(o,11)^jt(o,25))+((f=o)&a^~f&u)+Vt[t]+Gt[t]|0,h=(jt(n,2)^jt(n,13)^jt(n,22))+qt(n,r,s)|0;c=u,u=a,a=o,o=i+e|0,i=s,s=r,r=n,n=e+h|0}var f;n=n+this.A|0,r=r+this.B|0,s=s+this.C|0,i=i+this.D|0,o=o+this.E|0,a=a+this.F|0,u=u+this.G|0,c=c+this.H|0,this.set(n,r,s,i,o,a,u,c)}roundClean(){Gt.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}},Yt=class extends Wt{constructor(){super(),this.A=-1056596264,this.B=914150663,this.C=812702999,this.D=-150054599,this.E=-4191439,this.F=1750603025,this.G=1694076839,this.H=-1090891868,this.outputLen=28}};const Jt=zt((()=>new Wt));zt((()=>new Yt)); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ const Qt=BigInt(0),Xt=BigInt(1),te=BigInt(2),ee=t=>t instanceof Uint8Array,ne=Array.from({length:256},((t,e)=>e.toString(16).padStart(2,"0")));function re(t){if(!ee(t))throw new Error("Uint8Array expected");let e="";for(let n=0;n<t.length;n++)e+=ne[t[n]];return e}function se(t){const e=t.toString(16);return 1&e.length?`0${e}`:e}function ie(t){if("string"!=typeof t)throw new Error("hex string expected, got "+typeof t);return BigInt(""===t?"0":`0x${t}`)}function oe(t){if("string"!=typeof t)throw new Error("hex string expected, got "+typeof t);if(t.length%2)throw new Error("hex string is invalid: unpadded "+t.length);const e=new Uint8Array(t.length/2);for(let n=0;n<e.length;n++){const r=2*n,s=t.slice(r,r+2),i=Number.parseInt(s,16);if(Number.isNaN(i)||i<0)throw new Error("invalid byte sequence");e[n]=i}return e}function ae(t){return ie(re(t))}function ue(t){if(!ee(t))throw new Error("Uint8Array expected");return ie(re(Uint8Array.from(t).reverse()))}const ce=(t,e)=>oe(t.toString(16).padStart(2*e,"0")),fe=(t,e)=>ce(t,e).reverse();function he(t,e,n){let r;if("string"==typeof e)try{r=oe(e)}catch(n){throw new Error(`${t} must be valid hex string, got "${e}". Cause: ${n}`)}else{if(!ee(e))throw new Error(`${t} must be hex string or Uint8Array`);r=Uint8Array.from(e)}const s=r.length;if("number"==typeof n&&s!==n)throw new Error(`${t} expected ${n} bytes, got ${s}`);return r}function de(...t){const e=new Uint8Array(t.reduce(((t,e)=>t+e.length),0));let n=0;return t.forEach((t=>{if(!ee(t))throw new Error("Uint8Array expected");e.set(t,n),n+=t.length})),e}function le(t){if("string"!=typeof t)throw new Error("utf8ToBytes expected string, got "+typeof t);return(new TextEncoder).encode(t)}const pe=t=>(te<<BigInt(t-1))-Xt,ge=t=>new Uint8Array(t),ye=t=>Uint8Array.from(t);function me(t,e,n){if("number"!=typeof t||t<2)throw new Error("hashLen must be a number");if("number"!=typeof e||e<2)throw new Error("qByteLen must be a number");if("function"!=typeof n)throw new Error("hmacFn must be a function");let r=ge(t),s=ge(t),i=0;const o=()=>{r.fill(1),s.fill(0),i=0},a=(...t)=>n(s,r,...t),u=(t=ge())=>{s=a(ye([0]),t),r=a(),0!==t.length&&(s=a(ye([1]),t),r=a())},c=()=>{if(i++>=1e3)throw new Error("drbg: tried 1000 values");let t=0;const n=[];for(;t<e;){r=a();const e=r.slice();n.push(e),t+=r.length}return de(...n)};return(t,e)=>{let n;for(o(),u(t);!(n=e(c()));)u();return o(),n}}const we={bigint:t=>"bigint"==typeof t,function:t=>"function"==typeof t,boolean:t=>"boolean"==typeof t,string:t=>"string"==typeof t,isSafeInteger:t=>Number.isSafeInteger(t),array:t=>Array.isArray(t),field:(t,e)=>e.Fp.isValid(t),hash:t=>"function"==typeof t&&Number.isSafeInteger(t.outputLen)};function be(t,e,n={}){const r=(e,n,r)=>{const s=we[n];if("function"!=typeof s)throw new Error(`Invalid validator "${n}", expected function`);const i=t[e];if(!(r&&void 0===i||s(i,t)))throw new Error(`Invalid param ${String(e)}=${i} (${typeof i}), expected ${n}`)};for(const[t,n]of Object.entries(e))r(t,n,!1);for(const[t,e]of Object.entries(n))r(t,e,!0);return t}var ve=Object.freeze({__proto__:null,bitGet:(t,e)=>t>>BigInt(e)&1n,bitLen:function(t){let e;for(e=0;t>0n;t>>=Xt,e+=1);return e},bitMask:pe,bitSet:(t,e,n)=>t|(n?Xt:Qt)<<BigInt(e),bytesToHex:re,bytesToNumberBE:ae,bytesToNumberLE:ue,concatBytes:de,createHmacDrbg:me,ensureBytes:he,equalBytes:function(t,e){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0},hexToBytes:oe,hexToNumber:ie,numberToBytesBE:ce,numberToBytesLE:fe,numberToHexUnpadded:se,numberToVarBytesBE:t=>oe(se(t)),utf8ToBytes:le,validateObject:be}); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const _e=BigInt(0),xe=BigInt(1),Ee=BigInt(2),Se=BigInt(3),Oe=BigInt(4),ke=BigInt(5),Ce=BigInt(8);function Pe(t,e){const n=t%e;return n>=_e?n:e+n}function Ae(t,e,n){if(n<=_e||e<_e)throw new Error("Expected power/modulo > 0");if(n===xe)return _e;let r=xe;for(;e>_e;)e&xe&&(r=r*t%n),t=t*t%n,e>>=xe;return r}function Ue(t,e,n){let r=t;for(;e-- >_e;)r*=r,r%=n;return r}function Ie(t,e){if(t===_e||e<=_e)throw new Error(`invert: expected positive integers, got n=${t} mod=${e}`);let n=Pe(t,e),r=e,s=_e,i=xe;for(;n!==_e;){const t=r%n,e=s-i*(r/n);r=n,n=t,s=i,i=e}if(r!==xe)throw new Error("invert: does not exist");return Pe(s,e)}function Be(t){if(t%Oe===Se){const e=(t+xe)/Oe;return function(t,n){const r=t.pow(n,e);if(!t.eql(t.sqr(r),n))throw new Error("Cannot find square root");return r}}if(t%Ce===ke){const e=(t-ke)/Ce;return function(t,n){const r=t.mul(n,Ee),s=t.pow(r,e),i=t.mul(n,s),o=t.mul(t.mul(i,Ee),s),a=t.mul(i,t.sub(o,t.ONE));if(!t.eql(t.sqr(a),n))throw new Error("Cannot find square root");return a}}return function(t){const e=(t-xe)/Ee;let n,r,s;for(n=t-xe,r=0;n%Ee===_e;n/=Ee,r++);for(s=Ee;s<t&&Ae(s,e,t)!==t-xe;s++);if(1===r){const e=(t+xe)/Oe;return function(t,n){const r=t.pow(n,e);if(!t.eql(t.sqr(r),n))throw new Error("Cannot find square root");return r}}const i=(n+xe)/Ee;return function(t,o){if(t.pow(o,e)===t.neg(t.ONE))throw new Error("Cannot find square root");let a=r,u=t.pow(t.mul(t.ONE,s),n),c=t.pow(o,i),f=t.pow(o,n);for(;!t.eql(f,t.ONE);){if(t.eql(f,t.ZERO))return t.ZERO;let e=1;for(let n=t.sqr(f);e<a&&!t.eql(n,t.ONE);e++)n=t.sqr(n);const n=t.pow(u,xe<<BigInt(a-e-1));u=t.sqr(n),c=t.mul(c,n),f=t.mul(f,u),a=e}return c}}(t)}BigInt(9),BigInt(16);const Te=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Ne(t){return be(t,Te.reduce(((t,e)=>(t[e]="function",t)),{ORDER:"bigint",MASK:"bigint",BYTES:"isSafeInteger",BITS:"isSafeInteger"}))}function Le(t,e){const n=void 0!==e?e:t.toString(2).length;return{nBitLength:n,nByteLength:Math.ceil(n/8)}}function Ze(t,e,n=!1,r={}){if(t<=_e)throw new Error(`Expected Fp ORDER > 0, got ${t}`);const{nBitLength:s,nByteLength:i}=Le(t,e);if(i>2048)throw new Error("Field lengths over 2048 bytes are not supported");const o=Be(t),a=Object.freeze({ORDER:t,BITS:s,BYTES:i,MASK:pe(s),ZERO:_e,ONE:xe,create:e=>Pe(e,t),isValid:e=>{if("bigint"!=typeof e)throw new Error("Invalid field element: expected bigint, got "+typeof e);return _e<=e&&e<t},is0:t=>t===_e,isOdd:t=>(t&xe)===xe,neg:e=>Pe(-e,t),eql:(t,e)=>t===e,sqr:e=>Pe(e*e,t),add:(e,n)=>Pe(e+n,t),sub:(e,n)=>Pe(e-n,t),mul:(e,n)=>Pe(e*n,t),pow:(t,e)=>function(t,e,n){if(n<_e)throw new Error("Expected power > 0");if(n===_e)return t.ONE;if(n===xe)return e;let r=t.ONE,s=e;for(;n>_e;)n&xe&&(r=t.mul(r,s)),s=t.sqr(s),n>>=xe;return r}(a,t,e),div:(e,n)=>Pe(e*Ie(n,t),t),sqrN:t=>t*t,addN:(t,e)=>t+e,subN:(t,e)=>t-e,mulN:(t,e)=>t*e,inv:e=>Ie(e,t),sqrt:r.sqrt||(t=>o(a,t)),invertBatch:t=>function(t,e){const n=new Array(e.length),r=e.reduce(((e,r,s)=>t.is0(r)?e:(n[s]=e,t.mul(e,r))),t.ONE),s=t.inv(r);return e.reduceRight(((e,r,s)=>t.is0(r)?e:(n[s]=t.mul(e,n[s]),t.mul(e,r))),s),n}(a,t),cmov:(t,e,n)=>n?e:t,toBytes:t=>n?fe(t,i):ce(t,i),fromBytes:t=>{if(t.length!==i)throw new Error(`Fp.fromBytes: expected ${i}, got ${t.length}`);return n?ue(t):ae(t)}});return Object.freeze(a)} /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ const Re=BigInt(0),He=BigInt(1);function je(t){return Ne(t.Fp),be(t,{n:"bigint",h:"bigint",Gx:"field",Gy:"field"},{nBitLength:"isSafeInteger",nByteLength:"isSafeInteger"}),Object.freeze({...Le(t.n,t.nBitLength),...t,p:t.Fp.ORDER})} /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const{bytesToNumberBE:De,hexToBytes:$e}=ve,ze={Err:class extends Error{constructor(t=""){super(t)}},_parseInt(t){const{Err:e}=ze;if(t.length<2||2!==t[0])throw new e("Invalid signature integer tag");const n=t[1],r=t.subarray(2,n+2);if(!n||r.length!==n)throw new e("Invalid signature integer: wrong length");if(0===r[0]&&r[1]<=127)throw new e("Invalid signature integer: trailing length");return{d:De(r),l:t.subarray(n+2)}},toSig(t){const{Err:e}=ze,n="string"==typeof t?$e(t):t;if(!(n instanceof Uint8Array))throw new Error("ui8a expected");let r=n.length;if(r<2||48!=n[0])throw new e("Invalid signature tag");if(n[1]!==r-2)throw new e("Invalid signature: incorrect length");const{d:s,l:i}=ze._parseInt(n.subarray(2)),{d:o,l:a}=ze._parseInt(i);if(a.length)throw new e("Invalid signature: left bytes after parsing");return{r:s,s:o}},hexFromSig(t){const e=t=>Number.parseInt(t[0],16)>=8?"00"+t:t,n=t=>{const e=t.toString(16);return 1&e.length?`0${e}`:e},r=e(n(t.s)),s=e(n(t.r)),i=r.length/2,o=s.length/2,a=n(i),u=n(o);return`30${n(o+i+4)}02${u}${s}02${a}${r}`}},Ke=BigInt(0),Fe=BigInt(1),qe=BigInt(2),Ve=BigInt(3),Me=BigInt(4);function Ge(t){const e=function(t){const e=je(t);be(e,{a:"field",b:"field"},{allowedPrivateKeyLengths:"array",wrapPrivateKey:"boolean",isTorsionFree:"function",clearCofactor:"function",allowInfinityPoint:"boolean",fromBytes:"function",toBytes:"function"});const{endo:n,Fp:r,a:s}=e;if(n){if(!r.eql(s,r.ZERO))throw new Error("Endomorphism can only be defined for Koblitz curves that have a=0");if("object"!=typeof n||"bigint"!=typeof n.beta||"function"!=typeof n.splitScalar)throw new Error("Expected endomorphism with beta: bigint and splitScalar: function")}return Object.freeze({...e})}(t),{Fp:n}=e,r=e.toBytes||((t,e,r)=>{const s=e.toAffine();return de(Uint8Array.from([4]),n.toBytes(s.x),n.toBytes(s.y))}),s=e.fromBytes||(t=>{const e=t.subarray(1);return{x:n.fromBytes(e.subarray(0,n.BYTES)),y:n.fromBytes(e.subarray(n.BYTES,2*n.BYTES))}});function i(t){const{a:r,b:s}=e,i=n.sqr(t),o=n.mul(i,t);return n.add(n.add(o,n.mul(t,r)),s)}function o(t){return"bigint"==typeof t&&Ke<t&&t<e.n}function a(t){if(!o(t))throw new Error("Expected valid bigint: 0 < bigint < curve.n")}function u(t){const{allowedPrivateKeyLengths:n,nByteLength:r,wrapPrivateKey:s,n:i}=e;if(n&&"bigint"!=typeof t){if(t instanceof Uint8Array&&(t=re(t)),"string"!=typeof t||!n.includes(t.length))throw new Error("Invalid key");t=t.padStart(2*r,"0")}let o;try{o="bigint"==typeof t?t:ae(he("private key",t,r))}catch(e){throw new Error(`private key must be ${r} bytes, hex or bigint, not ${typeof t}`)}return s&&(o=Pe(o,i)),a(o),o}const c=new Map;function f(t){if(!(t instanceof h))throw new Error("ProjectivePoint expected")}class h{constructor(t,e,r){if(this.px=t,this.py=e,this.pz=r,null==t||!n.isValid(t))throw new Error("x required");if(null==e||!n.isValid(e))throw new Error("y required");if(null==r||!n.isValid(r))throw new Error("z required")}static fromAffine(t){const{x:e,y:r}=t||{};if(!t||!n.isValid(e)||!n.isValid(r))throw new Error("invalid affine point");if(t instanceof h)throw new Error("projective point not allowed");const s=t=>n.eql(t,n.ZERO);return s(e)&&s(r)?h.ZERO:new h(e,r,n.ONE)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}static normalizeZ(t){const e=n.invertBatch(t.map((t=>t.pz)));return t.map(((t,n)=>t.toAffine(e[n]))).map(h.fromAffine)}static fromHex(t){const e=h.fromAffine(s(he("pointHex",t)));return e.assertValidity(),e}static fromPrivateKey(t){return h.BASE.multiply(u(t))}_setWindowSize(t){this._WINDOW_SIZE=t,c.delete(this)}assertValidity(){if(this.is0()){if(e.allowInfinityPoint)return;throw new Error("bad point: ZERO")}const{x:t,y:r}=this.toAffine();if(!n.isValid(t)||!n.isValid(r))throw new Error("bad point: x or y not FE");const s=n.sqr(r),o=i(t);if(!n.eql(s,o))throw new Error("bad point: equation left != right");if(!this.isTorsionFree())throw new Error("bad point: not in prime-order subgroup")}hasEvenY(){const{y:t}=this.toAffine();if(n.isOdd)return!n.isOdd(t);throw new Error("Field doesn't support isOdd")}equals(t){f(t);const{px:e,py:r,pz:s}=this,{px:i,py:o,pz:a}=t,u=n.eql(n.mul(e,a),n.mul(i,s)),c=n.eql(n.mul(r,a),n.mul(o,s));return u&&c}negate(){return new h(this.px,n.neg(this.py),this.pz)}double(){const{a:t,b:r}=e,s=n.mul(r,Ve),{px:i,py:o,pz:a}=this;let u=n.ZERO,c=n.ZERO,f=n.ZERO,d=n.mul(i,i),l=n.mul(o,o),p=n.mul(a,a),g=n.mul(i,o);return g=n.add(g,g),f=n.mul(i,a),f=n.add(f,f),u=n.mul(t,f),c=n.mul(s,p),c=n.add(u,c),u=n.sub(l,c),c=n.add(l,c),c=n.mul(u,c),u=n.mul(g,u),f=n.mul(s,f),p=n.mul(t,p),g=n.sub(d,p),g=n.mul(t,g),g=n.add(g,f),f=n.add(d,d),d=n.add(f,d),d=n.add(d,p),d=n.mul(d,g),c=n.add(c,d),p=n.mul(o,a),p=n.add(p,p),d=n.mul(p,g),u=n.sub(u,d),f=n.mul(p,l),f=n.add(f,f),f=n.add(f,f),new h(u,c,f)}add(t){f(t);const{px:r,py:s,pz:i}=this,{px:o,py:a,pz:u}=t;let c=n.ZERO,d=n.ZERO,l=n.ZERO;const p=e.a,g=n.mul(e.b,Ve);let y=n.mul(r,o),m=n.mul(s,a),w=n.mul(i,u),b=n.add(r,s),v=n.add(o,a);b=n.mul(b,v),v=n.add(y,m),b=n.sub(b,v),v=n.add(r,i);let _=n.add(o,u);return v=n.mul(v,_),_=n.add(y,w),v=n.sub(v,_),_=n.add(s,i),c=n.add(a,u),_=n.mul(_,c),c=n.add(m,w),_=n.sub(_,c),l=n.mul(p,v),c=n.mul(g,w),l=n.add(c,l),c=n.sub(m,l),l=n.add(m,l),d=n.mul(c,l),m=n.add(y,y),m=n.add(m,y),w=n.mul(p,w),v=n.mul(g,v),m=n.add(m,w),w=n.sub(y,w),w=n.mul(p,w),v=n.add(v,w),y=n.mul(m,v),d=n.add(d,y),y=n.mul(_,v),c=n.mul(b,c),c=n.sub(c,y),y=n.mul(b,m),l=n.mul(_,l),l=n.add(l,y),new h(c,d,l)}subtract(t){return this.add(t.negate())}is0(){return this.equals(h.ZERO)}wNAF(t){return l.wNAFCached(this,c,t,(t=>{const e=n.invertBatch(t.map((t=>t.pz)));return t.map(((t,n)=>t.toAffine(e[n]))).map(h.fromAffine)}))}multiplyUnsafe(t){const r=h.ZERO;if(t===Ke)return r;if(a(t),t===Fe)return this;const{endo:s}=e;if(!s)return l.unsafeLadder(this,t);let{k1neg:i,k1:o,k2neg:u,k2:c}=s.splitScalar(t),f=r,d=r,p=this;for(;o>Ke||c>Ke;)o&Fe&&(f=f.add(p)),c&Fe&&(d=d.add(p)),p=p.double(),o>>=Fe,c>>=Fe;return i&&(f=f.negate()),u&&(d=d.negate()),d=new h(n.mul(d.px,s.beta),d.py,d.pz),f.add(d)}multiply(t){a(t);let r,s,i=t;const{endo:o}=e;if(o){const{k1neg:t,k1:e,k2neg:a,k2:u}=o.splitScalar(i);let{p:c,f:f}=this.wNAF(e),{p:d,f:p}=this.wNAF(u);c=l.constTimeNegate(t,c),d=l.constTimeNegate(a,d),d=new h(n.mul(d.px,o.beta),d.py,d.pz),r=c.add(d),s=f.add(p)}else{const{p:t,f:e}=this.wNAF(i);r=t,s=e}return h.normalizeZ([r,s])[0]}multiplyAndAddUnsafe(t,e,n){const r=h.BASE,s=(t,e)=>e!==Ke&&e!==Fe&&t.equals(r)?t.multiply(e):t.multiplyUnsafe(e),i=s(this,e).add(s(t,n));return i.is0()?void 0:i}toAffine(t){const{px:e,py:r,pz:s}=this,i=this.is0();null==t&&(t=i?n.ONE:n.inv(s));const o=n.mul(e,t),a=n.mul(r,t),u=n.mul(s,t);if(i)return{x:n.ZERO,y:n.ZERO};if(!n.eql(u,n.ONE))throw new Error("invZ was invalid");return{x:o,y:a}}isTorsionFree(){const{h:t,isTorsionFree:n}=e;if(t===Fe)return!0;if(n)return n(h,this);throw new Error("isTorsionFree() has not been declared for the elliptic curve")}clearCofactor(){const{h:t,clearCofactor:n}=e;return t===Fe?this:n?n(h,this):this.multiplyUnsafe(e.h)}toRawBytes(t=!0){return this.assertValidity(),r(h,this,t)}toHex(t=!0){return re(this.toRawBytes(t))}}h.BASE=new h(e.Gx,e.Gy,n.ONE),h.ZERO=new h(n.ZERO,n.ONE,n.ZERO);const d=e.nBitLength,l=function(t,e){const n=(t,e)=>{const n=e.negate();return t?n:e},r=t=>({windows:Math.ceil(e/t)+1,windowSize:2**(t-1)});return{constTimeNegate:n,unsafeLadder(e,n){let r=t.ZERO,s=e;for(;n>Re;)n&He&&(r=r.add(s)),s=s.double(),n>>=He;return r},precomputeWindow(t,e){const{windows:n,windowSize:s}=r(e),i=[];let o=t,a=o;for(let t=0;t<n;t++){a=o,i.push(a);for(let t=1;t<s;t++)a=a.add(o),i.push(a);o=a.double()}return i},wNAF(e,s,i){const{windows:o,windowSize:a}=r(e);let u=t.ZERO,c=t.BASE;const f=BigInt(2**e-1),h=2**e,d=BigInt(e);for(let t=0;t<o;t++){const e=t*a;let r=Number(i&f);i>>=d,r>a&&(r-=h,i+=He);const o=e,l=e+Math.abs(r)-1,p=t%2!=0,g=r<0;0===r?c=c.add(n(p,s[o])):u=u.add(n(g,s[l]))}return{p:u,f:c}},wNAFCached(t,e,n,r){const s=t._WINDOW_SIZE||1;let i=e.get(t);return i||(i=this.precomputeWindow(t,s),1!==s&&e.set(t,r(i))),this.wNAF(s,i,n)}}}(h,e.endo?Math.ceil(d/2):d);return{CURVE:e,ProjectivePoint:h,normPrivateKeyToScalar:u,weierstrassEquation:i,isWithinCurveOrder:o}}function We(t){const e=function(t){const e=je(t);return be(e,{hash:"hash",hmac:"function",randomBytes:"function"},{bits2int:"function",bits2int_modN:"function",lowS:"boolean"}),Object.freeze({lowS:!0,...e})}(t),{Fp:n,n:r}=e,s=n.BYTES+1,i=2*n.BYTES+1;function o(t){return Pe(t,r)}function a(t){return Ie(t,r)}const{ProjectivePoint:u,normPrivateKeyToScalar:c,weierstrassEquation:f,isWithinCurveOrder:h}=Ge({...e,toBytes(t,e,r){const s=e.toAffine(),i=n.toBytes(s.x),o=de;return r?o(Uint8Array.from([e.hasEvenY()?2:3]),i):o(Uint8Array.from([4]),i,n.toBytes(s.y))},fromBytes(t){const e=t.length,r=t[0],o=t.subarray(1);if(e!==s||2!==r&&3!==r){if(e===i&&4===r){return{x:n.fromBytes(o.subarray(0,n.BYTES)),y:n.fromBytes(o.subarray(n.BYTES,2*n.BYTES))}}throw new Error(`Point of length ${e} was invalid. Expected ${s} compressed bytes or ${i} uncompressed bytes`)}{const t=ae(o);if(!(Ke<(a=t)&&a<n.ORDER))throw new Error("Point is not on curve");const e=f(t);let s=n.sqrt(e);return 1==(1&r)!==((s&Fe)===Fe)&&(s=n.neg(s)),{x:t,y:s}}var a}}),d=t=>re(ce(t,e.nByteLength));function l(t){return t>r>>Fe}const p=(t,e,n)=>ae(t.slice(e,n));class g{constructor(t,e,n){this.r=t,this.s=e,this.recovery=n,this.assertValidity()}static fromCompact(t){const n=e.nByteLength;return t=he("compactSignature",t,2*n),new g(p(t,0,n),p(t,n,2*n))}static fromDER(t){const{r:e,s:n}=ze.toSig(he("DER",t));return new g(e,n)}assertValidity(){if(!h(this.r))throw new Error("r must be 0 < r < CURVE.n");if(!h(this.s))throw new Error("s must be 0 < s < CURVE.n")}addRecoveryBit(t){return new g(this.r,this.s,t)}recoverPublicKey(t){const{r:r,s:s,recovery:i}=this,c=b(he("msgHash",t));if(null==i||![0,1,2,3].includes(i))throw new Error("recovery id invalid");const f=2===i||3===i?r+e.n:r;if(f>=n.ORDER)throw new Error("recovery id 2 or 3 invalid");const h=0==(1&i)?"02":"03",l=u.fromHex(h+d(f)),p=a(f),g=o(-c*p),y=o(s*p),m=u.BASE.multiplyAndAddUnsafe(l,g,y);if(!m)throw new Error("point at infinify");return m.assertValidity(),m}hasHighS(){return l(this.s)}normalizeS(){return this.hasHighS()?new g(this.r,o(-this.s),this.recovery):this}toDERRawBytes(){return oe(this.toDERHex())}toDERHex(){return ze.hexFromSig({r:this.r,s:this.s})}toCompactRawBytes(){return oe(this.toCompactHex())}toCompactHex(){return d(this.r)+d(this.s)}}const y={isValidPrivateKey(t){try{return c(t),!0}catch(t){return!1}},normPrivateKeyToScalar:c,randomPrivateKey:()=>{const t=function(t,e,n=!1){const r=(t=he("privateHash",t)).length,s=Le(e).nByteLength+8;if(s<24||r<s||r>1024)throw new Error(`hashToPrivateScalar: expected ${s}-1024 bytes of input, got ${r}`);return Pe(n?ue(t):ae(t),e-xe)+xe}(e.randomBytes(n.BYTES+8),r);return ce(t,e.nByteLength)},precompute:(t=8,e=u.BASE)=>(e._setWindowSize(t),e.multiply(BigInt(3)),e)};function m(t){const e=t instanceof Uint8Array,n="string"==typeof t,r=(e||n)&&t.length;return e?r===s||r===i:n?r===2*s||r===2*i:t instanceof u}const w=e.bits2int||function(t){const n=ae(t),r=8*t.length-e.nBitLength;return r>0?n>>BigInt(r):n},b=e.bits2int_modN||function(t){return o(w(t))},v=pe(e.nBitLength);function _(t){if("bigint"!=typeof t)throw new Error("bigint expected");if(!(Ke<=t&&t<v))throw new Error(`bigint expected < 2^${e.nBitLength}`);return ce(t,e.nByteLength)}function x(t,r,s=E){if(["recovered","canonical"].some((t=>t in s)))throw new Error("sign() legacy options not supported");const{hash:i,randomBytes:f}=e;let{lowS:d,prehash:p,extraEntropy:y}=s;null==d&&(d=!0),t=he("msgHash",t),p&&(t=he("prehashed msgHash",i(t)));const m=b(t),v=c(r),x=[_(v),_(m)];if(null!=y){const t=!0===y?f(n.BYTES):y;x.push(he("extraEntropy",t,n.BYTES))}const S=de(...x),O=m;return{seed:S,k2sig:function(t){const e=w(t);if(!h(e))return;const n=a(e),r=u.BASE.multiply(e).toAffine(),s=o(r.x);if(s===Ke)return;const i=o(n*o(O+s*v));if(i===Ke)return;let c=(r.x===s?0:2)|Number(r.y&Fe),f=i;return d&&l(i)&&(f=function(t){return l(t)?o(-t):t}(i),c^=1),new g(s,f,c)}}}const E={lowS:e.lowS,prehash:!1},S={lowS:e.lowS,prehash:!1};return u.BASE._setWindowSize(8),{CURVE:e,getPublicKey:function(t,e=!0){return u.fromPrivateKey(t).toRawBytes(e)},getSharedSecret:function(t,e,n=!0){if(m(t))throw new Error("first arg must be private key");if(!m(e))throw new Error("second arg must be public key");return u.fromHex(e).multiply(c(t)).toRawBytes(n)},sign:function(t,n,r=E){const{seed:s,k2sig:i}=x(t,n,r);return me(e.hash.outputLen,e.nByteLength,e.hmac)(s,i)},verify:function(t,n,r,s=S){const i=t;if(n=he("msgHash",n),r=he("publicKey",r),"strict"in s)throw new Error("options.strict was renamed to lowS");const{lowS:c,prehash:f}=s;let h,d;try{if("string"==typeof i||i instanceof Uint8Array)try{h=g.fromDER(i)}catch(t){if(!(t instanceof ze.Err))throw t;h=g.fromCompact(i)}else{if("object"!=typeof i||"bigint"!=typeof i.r||"bigint"!=typeof i.s)throw new Error("PARSE");{const{r:t,s:e}=i;h=new g(t,e)}}d=u.fromHex(r)}catch(t){if("PARSE"===t.message)throw new Error("signature must be Signature instance, Uint8Array or hex string");return!1}if(c&&h.hasHighS())return!1;f&&(n=e.hash(n));const{r:l,s:p}=h,y=b(n),m=a(p),w=o(y*m),v=o(l*m),_=u.BASE.multiplyAndAddUnsafe(d,w,v)?.toAffine();return!!_&&o(_.x)===l},ProjectivePoint:u,Signature:g,utils:y}}let Ye=class extends $t{constructor(t,e){super(),this.finished=!1,this.destroyed=!1,Zt.hash(t);const n=Dt(e);if(this.iHash=t.create(),"function"!=typeof this.iHash.update)throw new TypeError("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const r=this.blockLen,s=new Uint8Array(r);s.set(n.length>r?t.create().update(n).digest():n);for(let t=0;t<s.length;t++)s[t]^=54;this.iHash.update(s),this.oHash=t.create();for(let t=0;t<s.length;t++)s[t]^=106;this.oHash.update(s),s.fill(0)}update(t){return Zt.exists(this),this.iHash.update(t),this}digestInto(t){Zt.exists(this),Zt.bytes(t,this.outputLen),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));const{oHash:e,iHash:n,finished: