@omnity/ree-client-ts-sdk 0.4.4 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ree-sdk.cjs.js +1 -1
- package/dist/ree-sdk.es.js +1 -1
- package/package.json +1 -1
package/dist/ree-sdk.cjs.js
CHANGED
|
@@ -73,7 +73,7 @@ ic-request`);const cx=new TextEncoder().encode("\vic-response");new TextEncoder(
|
|
|
73
73
|
`,bech32:"tb",bip32:{public:70617039,private:70615956},pubKeyHash:111,scriptHash:196,wif:239}),fr}var mf={},oo={},wf={},lr={},D0;function Gw(){if(D0)return lr;D0=1,Object.defineProperty(lr,"__esModule",{value:!0}),lr.encode=lr.decode=lr.check=void 0;function e(r){if(r.length<8||r.length>72||r[0]!==48||r[1]!==r.length-2||r[2]!==2)return!1;const i=r[3];if(i===0||5+i>=r.length||r[4+i]!==2)return!1;const s=r[5+i];return!(s===0||6+i+s!==r.length||r[4]&128||i>1&&r[4]===0&&!(r[5]&128)||r[i+6]&128||s>1&&r[i+6]===0&&!(r[i+7]&128))}lr.check=e;function t(r){if(r.length<8)throw new Error("DER sequence length is too short");if(r.length>72)throw new Error("DER sequence length is too long");if(r[0]!==48)throw new Error("Expected DER sequence");if(r[1]!==r.length-2)throw new Error("DER sequence length is invalid");if(r[2]!==2)throw new Error("Expected DER integer");const i=r[3];if(i===0)throw new Error("R length is zero");if(5+i>=r.length)throw new Error("R length is too long");if(r[4+i]!==2)throw new Error("Expected DER integer (2)");const s=r[5+i];if(s===0)throw new Error("S length is zero");if(6+i+s!==r.length)throw new Error("S length is invalid");if(r[4]&128)throw new Error("R value is negative");if(i>1&&r[4]===0&&!(r[5]&128))throw new Error("R value excessively padded");if(r[i+6]&128)throw new Error("S value is negative");if(s>1&&r[i+6]===0&&!(r[i+7]&128))throw new Error("S value excessively padded");return{r:r.slice(4,4+i),s:r.slice(6+i)}}lr.decode=t;function n(r,i){const s=r.length,o=i.length;if(s===0)throw new Error("R length is zero");if(o===0)throw new Error("S length is zero");if(s>33)throw new Error("R length is too long");if(o>33)throw new Error("S length is too long");if(r[0]&128)throw new Error("R value is negative");if(i[0]&128)throw new Error("S value is negative");if(s>1&&r[0]===0&&!(r[1]&128))throw new Error("R value excessively padded");if(o>1&&i[0]===0&&!(i[1]&128))throw new Error("S value excessively padded");const c=Buffer.allocUnsafe(6+s+o);return c[0]=48,c[1]=c.length-2,c[2]=2,c[3]=r.length,r.copy(c,4),c[4+s]=2,c[5+s]=i.length,i.copy(c,6+s),c}return lr.encode=n,lr}var _i={},j0;function Eh(){if(j0)return _i;j0=1,Object.defineProperty(_i,"__esModule",{value:!0}),_i.REVERSE_OPS=_i.OPS=void 0;const e={OP_FALSE:0,OP_0:0,OP_PUSHDATA1:76,OP_PUSHDATA2:77,OP_PUSHDATA4:78,OP_1NEGATE:79,OP_RESERVED:80,OP_TRUE:81,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_VER:98,OP_IF:99,OP_NOTIF:100,OP_VERIF:101,OP_VERNOTIF:102,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_CAT:126,OP_SUBSTR:127,OP_LEFT:128,OP_RIGHT:129,OP_SIZE:130,OP_INVERT:131,OP_AND:132,OP_OR:133,OP_XOR:134,OP_EQUAL:135,OP_EQUALVERIFY:136,OP_RESERVED1:137,OP_RESERVED2:138,OP_1ADD:139,OP_1SUB:140,OP_2MUL:141,OP_2DIV:142,OP_NEGATE:143,OP_ABS:144,OP_NOT:145,OP_0NOTEQUAL:146,OP_ADD:147,OP_SUB:148,OP_MUL:149,OP_DIV:150,OP_MOD:151,OP_LSHIFT:152,OP_RSHIFT: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_NOP2:177,OP_CHECKLOCKTIMEVERIFY:177,OP_NOP3:178,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_PUBKEYHASH:253,OP_PUBKEY:254,OP_INVALIDOPCODE:255};_i.OPS=e;const t={};_i.REVERSE_OPS=t;for(const n of Object.keys(e)){const r=e[n];t[r]=n}return _i}var hr={},K0;function V8(){if(K0)return hr;K0=1,Object.defineProperty(hr,"__esModule",{value:!0}),hr.decode=hr.encode=hr.encodingLength=void 0;const e=Eh();function t(i){return i<e.OPS.OP_PUSHDATA1?1:i<=255?2:i<=65535?3:5}hr.encodingLength=t;function n(i,s,o){const c=t(s);return c===1?i.writeUInt8(s,o):c===2?(i.writeUInt8(e.OPS.OP_PUSHDATA1,o),i.writeUInt8(s,o+1)):c===3?(i.writeUInt8(e.OPS.OP_PUSHDATA2,o),i.writeUInt16LE(s,o+1)):(i.writeUInt8(e.OPS.OP_PUSHDATA4,o),i.writeUInt32LE(s,o+1)),c}hr.encode=n;function r(i,s){const o=i.readUInt8(s);let c,a;if(o<e.OPS.OP_PUSHDATA1)c=o,a=1;else if(o===e.OPS.OP_PUSHDATA1){if(s+2>i.length)return null;c=i.readUInt8(s+1),a=2}else if(o===e.OPS.OP_PUSHDATA2){if(s+3>i.length)return null;c=i.readUInt16LE(s+1),a=3}else{if(s+5>i.length)return null;if(o!==e.OPS.OP_PUSHDATA4)throw new Error("Unexpected opcode");c=i.readUInt32LE(s+1),a=5}return{opcode:o,number:c,size:a}}return hr.decode=r,hr}var Si={},G0;function $8(){if(G0)return Si;G0=1,Object.defineProperty(Si,"__esModule",{value:!0}),Si.encode=Si.decode=void 0;function e(r,i,s){i=i||4,s=s===void 0?!0:s;const o=r.length;if(o===0)return 0;if(o>i)throw new TypeError("Script number overflow");if(s&&(r[o-1]&127)===0&&(o<=1||(r[o-2]&128)===0))throw new Error("Non-minimally encoded script number");if(o===5){const a=r.readUInt32LE(0),u=r.readUInt8(4);return u&128?-((u&-129)*4294967296+a):u*4294967296+a}let c=0;for(let a=0;a<o;++a)c|=r[a]<<8*a;return r[o-1]&128?-(c&~(128<<8*(o-1))):c}Si.decode=e;function t(r){return r>2147483647?5:r>8388607?4:r>32767?3:r>127?2:r>0?1:0}function n(r){let i=Math.abs(r);const s=t(i),o=Buffer.allocUnsafe(s),c=r<0;for(let a=0;a<s;++a)o.writeUInt8(i&255,a),i>>=8;return o[s-1]&128?o.writeUInt8(c?128:0,s-1):c&&(o[s-1]|=128),o}return Si.encode=n,Si}var Ti={},bf={},Ef,W0;function _h(){if(W0)return Ef;W0=1;var e={Array:function(n){return n!=null&&n.constructor===Array},Boolean:function(n){return typeof n=="boolean"},Function:function(n){return typeof n=="function"},Nil:function(n){return n==null},Number:function(n){return typeof n=="number"},Object:function(n){return typeof n=="object"},String:function(n){return typeof n=="string"},"":function(){return!0}};e.Null=e.Nil;for(var t in e)e[t].toJSON=(function(n){return n}).bind(null,t);return Ef=e,Ef}var _f,X0;function Ww(){if(X0)return _f;X0=1;var e=_h();function t(d){return d.name||d.toString().match(/function (.*?)\s*\(/)[1]}function n(d){return e.Nil(d)?"":t(d.constructor)}function r(d){return e.Function(d)?"":e.String(d)?JSON.stringify(d):d&&e.Object(d)?"":d}function i(d,p){Error.captureStackTrace&&Error.captureStackTrace(d,p)}function s(d){return e.Function(d)?d.toJSON?d.toJSON():t(d):e.Array(d)?"Array":d&&e.Object(d)?"Object":d!==void 0?d:""}function o(d,p,g){var m=r(p);return"Expected "+s(d)+", got"+(g!==""?" "+g:"")+(m!==""?" "+m:"")}function c(d,p,g){g=g||n(p),this.message=o(d,p,g),i(this,c),this.__type=d,this.__value=p,this.__valueTypeName=g}c.prototype=Object.create(Error.prototype),c.prototype.constructor=c;function a(d,p,g,m,E){var _='" of type ';return p==="key"&&(_='" with key type '),o('property "'+s(g)+_+s(d),m,E)}function u(d,p,g,m,E){d?(E=E||n(m),this.message=a(d,g,p,m,E)):this.message='Unexpected property "'+p+'"',i(this,c),this.__label=g,this.__property=p,this.__type=d,this.__value=m,this.__valueTypeName=E}u.prototype=Object.create(Error.prototype),u.prototype.constructor=c;function f(d,p){return new c(d,{},p)}function l(d,p,g){return d instanceof u?(p=p+"."+d.__property,d=new u(d.__type,p,d.__label,d.__value,d.__valueTypeName)):d instanceof c&&(d=new u(d.__type,p,g,d.__value,d.__valueTypeName)),i(d),d}return _f={TfTypeError:c,TfPropertyTypeError:u,tfCustomError:f,tfSubError:l,tfJSON:s,getValueTypeName:n},_f}var Sf,z0;function D8(){if(z0)return Sf;z0=1;var e=_h(),t=Ww();function n(A){return Buffer.isBuffer(A)}function r(A){return typeof A=="string"&&/^([0-9a-f]{2})+$/i.test(A)}function i(A,R){var M=A.toJSON();function v(U){if(!A(U))return!1;if(U.length===R)return!0;throw t.tfCustomError(M+"(Length: "+R+")",M+"(Length: "+U.length+")")}return v.toJSON=function(){return M},v}var s=i.bind(null,e.Array),o=i.bind(null,n),c=i.bind(null,r),a=i.bind(null,e.String);function u(A,R,M){M=M||e.Number;function v(U,L){return M(U,L)&&U>A&&U<R}return v.toJSON=function(){return`${M.toJSON()} between [${A}, ${R}]`},v}var f=Math.pow(2,53)-1;function l(A){return typeof A=="number"&&isFinite(A)}function d(A){return A<<24>>24===A}function p(A){return A<<16>>16===A}function g(A){return(A|0)===A}function m(A){return typeof A=="number"&&A>=-f&&A<=f&&Math.floor(A)===A}function E(A){return(A&255)===A}function _(A){return(A&65535)===A}function S(A){return A>>>0===A}function w(A){return typeof A=="number"&&A>=0&&A<=f&&Math.floor(A)===A}var T={ArrayN:s,Buffer:n,BufferN:o,Finite:l,Hex:r,HexN:c,Int8:d,Int16:p,Int32:g,Int53:m,Range:u,StringN:a,UInt8:E,UInt16:_,UInt32:S,UInt53:w};for(var O in T)T[O].toJSON=(function(A){return A}).bind(null,O);return Sf=T,Sf}var Tf,Y0;function j8(){if(Y0)return Tf;Y0=1;var e=Ww(),t=_h(),n=e.tfJSON,r=e.TfTypeError,i=e.TfPropertyTypeError,s=e.tfSubError,o=e.getValueTypeName,c={arrayOf:function(p,g){p=a(p),g=g||{};function m(E,_){return!t.Array(E)||t.Nil(E)||g.minLength!==void 0&&E.length<g.minLength||g.maxLength!==void 0&&E.length>g.maxLength||g.length!==void 0&&E.length!==g.length?!1:E.every(function(S,w){try{return u(p,S,_)}catch(T){throw s(T,w)}})}return m.toJSON=function(){var E="["+n(p)+"]";return g.length!==void 0?E+="{"+g.length+"}":(g.minLength!==void 0||g.maxLength!==void 0)&&(E+="{"+(g.minLength===void 0?0:g.minLength)+","+(g.maxLength===void 0?1/0:g.maxLength)+"}"),E},m},maybe:function d(p){p=a(p);function g(m,E){return t.Nil(m)||p(m,E,d)}return g.toJSON=function(){return"?"+n(p)},g},map:function(p,g){p=a(p),g&&(g=a(g));function m(E,_){if(!t.Object(E)||t.Nil(E))return!1;for(var S in E){try{g&&u(g,S,_)}catch(T){throw s(T,S,"key")}try{var w=E[S];u(p,w,_)}catch(T){throw s(T,S)}}return!0}return g?m.toJSON=function(){return"{"+n(g)+": "+n(p)+"}"}:m.toJSON=function(){return"{"+n(p)+"}"},m},object:function(p){var g={};for(var m in p)g[m]=a(p[m]);function E(_,S){if(!t.Object(_)||t.Nil(_))return!1;var w;try{for(w in g){var T=g[w],O=_[w];u(T,O,S)}}catch(A){throw s(A,w)}if(S){for(w in _)if(!g[w])throw new i(void 0,w)}return!0}return E.toJSON=function(){return n(g)},E},anyOf:function(){var p=[].slice.call(arguments).map(a);function g(m,E){return p.some(function(_){try{return u(_,m,E)}catch{return!1}})}return g.toJSON=function(){return p.map(n).join("|")},g},allOf:function(){var p=[].slice.call(arguments).map(a);function g(m,E){return p.every(function(_){try{return u(_,m,E)}catch{return!1}})}return g.toJSON=function(){return p.map(n).join(" & ")},g},quacksLike:function(p){function g(m){return p===o(m)}return g.toJSON=function(){return p},g},tuple:function(){var p=[].slice.call(arguments).map(a);function g(m,E){return t.Nil(m)||t.Nil(m.length)||E&&m.length!==p.length?!1:p.every(function(_,S){try{return u(_,m[S],E)}catch(w){throw s(w,S)}})}return g.toJSON=function(){return"("+p.map(n).join(", ")+")"},g},value:function(p){function g(m){return m===p}return g.toJSON=function(){return p},g}};c.oneOf=c.anyOf;function a(d){if(t.String(d))return d[0]==="?"?c.maybe(d.slice(1)):t[d]||c.quacksLike(d);if(d&&t.Object(d)){if(t.Array(d)){if(d.length!==1)throw new TypeError("Expected compile() parameter of type Array of length 1");return c.arrayOf(d[0])}return c.object(d)}else if(t.Function(d))return d;return c.value(d)}function u(d,p,g,m){if(t.Function(d)){if(d(p,g))return!0;throw new r(m||d,p)}return u(a(d),p,g)}for(var f in t)u[f]=t[f];for(f in c)u[f]=c[f];var l=D8();for(f in l)u[f]=l[f];return u.compile=a,u.TfTypeError=r,u.TfPropertyTypeError=i,Tf=u,Tf}var Z0;function zt(){return Z0||(Z0=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.oneOf=e.Null=e.BufferN=e.Function=e.UInt32=e.UInt8=e.tuple=e.maybe=e.Hex=e.Buffer=e.String=e.Boolean=e.Array=e.Number=e.Hash256bit=e.Hash160bit=e.Buffer256bit=e.isTaptree=e.isTapleaf=e.TAPLEAF_VERSION_MASK=e.Satoshi=e.isPoint=e.stacksEqual=e.typeforce=void 0;const t=tc();e.typeforce=j8();const n=t.Buffer.alloc(32,0),r=t.Buffer.from("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f","hex");function i(f,l){return f.length!==l.length?!1:f.every((d,p)=>d.equals(l[p]))}e.stacksEqual=i;function s(f){if(!t.Buffer.isBuffer(f)||f.length<33)return!1;const l=f[0],d=f.slice(1,33);if(d.compare(n)===0||d.compare(r)>=0)return!1;if((l===2||l===3)&&f.length===33)return!0;const p=f.slice(33);return p.compare(n)===0||p.compare(r)>=0?!1:l===4&&f.length===65}e.isPoint=s;const o=21*1e14;function c(f){return e.typeforce.UInt53(f)&&f<=o}e.Satoshi=c,e.TAPLEAF_VERSION_MASK=254;function a(f){return!f||!("output"in f)||!t.Buffer.isBuffer(f.output)?!1:f.version!==void 0?(f.version&e.TAPLEAF_VERSION_MASK)===f.version:!0}e.isTapleaf=a;function u(f){return(0,e.Array)(f)?f.length!==2?!1:f.every(l=>u(l)):a(f)}e.isTaptree=u,e.Buffer256bit=e.typeforce.BufferN(32),e.Hash160bit=e.typeforce.BufferN(20),e.Hash256bit=e.typeforce.BufferN(32),e.Number=e.typeforce.Number,e.Array=e.typeforce.Array,e.Boolean=e.typeforce.Boolean,e.String=e.typeforce.String,e.Buffer=e.typeforce.Buffer,e.Hex=e.typeforce.Hex,e.maybe=e.typeforce.maybe,e.tuple=e.typeforce.tuple,e.UInt8=e.typeforce.UInt8,e.UInt32=e.typeforce.UInt32,e.Function=e.typeforce.Function,e.BufferN=e.typeforce.BufferN,e.Null=e.typeforce.Null,e.oneOf=e.typeforce.oneOf})(bf)),bf}var J0;function K8(){if(J0)return Ti;J0=1,Object.defineProperty(Ti,"__esModule",{value:!0}),Ti.encode=Ti.decode=void 0;const e=Gw(),t=rn(),n=zt(),{typeforce:r}=n,i=Buffer.alloc(1,0);function s(u){let f=0;for(;u[f]===0;)++f;return f===u.length?i:(u=u.slice(f),u[0]&128?Buffer.concat([i,u],1+u.length):u)}function o(u){u[0]===0&&(u=u.slice(1));const f=Buffer.alloc(32,0),l=Math.max(0,32-u.length);return u.copy(f,l),f}function c(u){const f=u.readUInt8(u.length-1);if(!(0,t.isDefinedHashType)(f))throw new Error("Invalid hashType "+f);const l=e.decode(u.slice(0,-1)),d=o(l.r),p=o(l.s);return{signature:Buffer.concat([d,p],64),hashType:f}}Ti.decode=c;function a(u,f){if(r({signature:n.BufferN(64),hashType:n.UInt8},{signature:u,hashType:f}),!(0,t.isDefinedHashType)(f))throw new Error("Invalid hashType "+f);const l=Buffer.allocUnsafe(1);l.writeUInt8(f,0);const d=s(u.slice(0,32)),p=s(u.slice(32,64));return Buffer.concat([e.encode(d,p),l])}return Ti.encode=a,Ti}var Q0;function rn(){return Q0||(Q0=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.signature=e.number=e.isCanonicalScriptSignature=e.isDefinedHashType=e.isCanonicalPubKey=e.toStack=e.fromASM=e.toASM=e.decompile=e.compile=e.countNonPushOnlyOPs=e.isPushOnly=e.OPS=void 0;const t=Gw(),n=Eh();Object.defineProperty(e,"OPS",{enumerable:!0,get:function(){return n.OPS}});const r=V8(),i=$8(),s=K8(),o=zt(),{typeforce:c}=o,a=n.OPS.OP_RESERVED;function u(v){return o.Number(v)&&(v===n.OPS.OP_0||v>=n.OPS.OP_1&&v<=n.OPS.OP_16||v===n.OPS.OP_1NEGATE)}function f(v){return o.Buffer(v)||u(v)}function l(v){return o.Array(v)&&v.every(f)}e.isPushOnly=l;function d(v){return v.length-v.filter(f).length}e.countNonPushOnlyOPs=d;function p(v){if(v.length===0)return n.OPS.OP_0;if(v.length===1){if(v[0]>=1&&v[0]<=16)return a+v[0];if(v[0]===129)return n.OPS.OP_1NEGATE}}function g(v){return Buffer.isBuffer(v)}function m(v){return o.Array(v)}function E(v){return Buffer.isBuffer(v)}function _(v){if(g(v))return v;c(o.Array,v);const U=v.reduce((H,$)=>E($)?$.length===1&&p($)!==void 0?H+1:H+r.encodingLength($.length)+$.length:H+1,0),L=Buffer.allocUnsafe(U);let K=0;if(v.forEach(H=>{if(E(H)){const $=p(H);if($!==void 0){L.writeUInt8($,K),K+=1;return}K+=r.encode(L,H.length,K),H.copy(L,K),K+=H.length}else L.writeUInt8(H,K),K+=1}),K!==L.length)throw new Error("Could not decode chunks");return L}e.compile=_;function S(v){if(m(v))return v;c(o.Buffer,v);const U=[];let L=0;for(;L<v.length;){const K=v[L];if(K>n.OPS.OP_0&&K<=n.OPS.OP_PUSHDATA4){const H=r.decode(v,L);if(H===null||(L+=H.size,L+H.number>v.length))return null;const $=v.slice(L,L+H.number);L+=H.number;const P=p($);P!==void 0?U.push(P):U.push($)}else U.push(K),L+=1}return U}e.decompile=S;function w(v){if(g(v)&&(v=S(v)),!v)throw new Error("Could not convert invalid chunks to ASM");return v.map(U=>{if(E(U)){const L=p(U);if(L===void 0)return U.toString("hex");U=L}return n.REVERSE_OPS[U]}).join(" ")}e.toASM=w;function T(v){return c(o.String,v),_(v.split(" ").map(U=>n.OPS[U]!==void 0?n.OPS[U]:(c(o.Hex,U),Buffer.from(U,"hex"))))}e.fromASM=T;function O(v){return v=S(v),c(l,v),v.map(U=>E(U)?U:U===n.OPS.OP_0?Buffer.allocUnsafe(0):i.encode(U-a))}e.toStack=O;function A(v){return o.isPoint(v)}e.isCanonicalPubKey=A;function R(v){const U=v&-129;return U>0&&U<4}e.isDefinedHashType=R;function M(v){return!Buffer.isBuffer(v)||!R(v[v.length-1])?!1:t.check(v.slice(0,-1))}e.isCanonicalScriptSignature=M,e.number=i,e.signature=s})(wf)),wf}var xi={},ep;function gi(){if(ep)return xi;ep=1,Object.defineProperty(xi,"__esModule",{value:!0}),xi.value=xi.prop=void 0;function e(n,r,i){Object.defineProperty(n,r,{configurable:!0,enumerable:!0,get(){const s=i.call(this);return this[r]=s,s},set(s){Object.defineProperty(this,r,{configurable:!0,enumerable:!0,value:s,writable:!0})}})}xi.prop=e;function t(n){let r;return()=>(r!==void 0||(r=n()),r)}return xi.value=t,xi}var tp;function G8(){if(tp)return oo;tp=1,Object.defineProperty(oo,"__esModule",{value:!0}),oo.p2data=void 0;const e=Xn(),t=rn(),n=zt(),r=gi(),i=t.OPS;function s(o,c){if(!o.data&&!o.output)throw new TypeError("Not enough data");c=Object.assign({validate:!0},c||{}),(0,n.typeforce)({network:n.typeforce.maybe(n.typeforce.Object),output:n.typeforce.maybe(n.typeforce.Buffer),data:n.typeforce.maybe(n.typeforce.arrayOf(n.typeforce.Buffer))},o);const u={name:"embed",network:o.network||e.bitcoin};if(r.prop(u,"output",()=>{if(o.data)return t.compile([i.OP_RETURN].concat(o.data))}),r.prop(u,"data",()=>{if(o.output)return t.decompile(o.output).slice(1)}),c.validate&&o.output){const f=t.decompile(o.output);if(f[0]!==i.OP_RETURN)throw new TypeError("Output is invalid");if(!f.slice(1).every(n.typeforce.Buffer))throw new TypeError("Output is invalid");if(o.data&&!(0,n.stacksEqual)(o.data,u.data))throw new TypeError("Data mismatch")}return Object.assign(u,o)}return oo.p2data=s,oo}var co={},np;function W8(){if(np)return co;np=1,Object.defineProperty(co,"__esModule",{value:!0}),co.p2ms=void 0;const e=Xn(),t=rn(),n=zt(),r=gi(),i=t.OPS,s=i.OP_RESERVED;function o(c,a){if(!c.input&&!c.output&&!(c.pubkeys&&c.m!==void 0)&&!c.signatures)throw new TypeError("Not enough data");a=Object.assign({validate:!0},a||{});function u(m){return t.isCanonicalScriptSignature(m)||(a.allowIncomplete&&m===i.OP_0)!==void 0}(0,n.typeforce)({network:n.typeforce.maybe(n.typeforce.Object),m:n.typeforce.maybe(n.typeforce.Number),n:n.typeforce.maybe(n.typeforce.Number),output:n.typeforce.maybe(n.typeforce.Buffer),pubkeys:n.typeforce.maybe(n.typeforce.arrayOf(n.isPoint)),signatures:n.typeforce.maybe(n.typeforce.arrayOf(u)),input:n.typeforce.maybe(n.typeforce.Buffer)},c);const l={network:c.network||e.bitcoin};let d=[],p=!1;function g(m){p||(p=!0,d=t.decompile(m),l.m=d[0]-s,l.n=d[d.length-2]-s,l.pubkeys=d.slice(1,-2))}if(r.prop(l,"output",()=>{if(c.m&&l.n&&c.pubkeys)return t.compile([].concat(s+c.m,c.pubkeys,s+l.n,i.OP_CHECKMULTISIG))}),r.prop(l,"m",()=>{if(l.output)return g(l.output),l.m}),r.prop(l,"n",()=>{if(l.pubkeys)return l.pubkeys.length}),r.prop(l,"pubkeys",()=>{if(c.output)return g(c.output),l.pubkeys}),r.prop(l,"signatures",()=>{if(c.input)return t.decompile(c.input).slice(1)}),r.prop(l,"input",()=>{if(c.signatures)return t.compile([i.OP_0].concat(c.signatures))}),r.prop(l,"witness",()=>{if(l.input)return[]}),r.prop(l,"name",()=>{if(!(!l.m||!l.n))return`p2ms(${l.m} of ${l.n})`}),a.validate){if(c.output){if(g(c.output),!n.typeforce.Number(d[0]))throw new TypeError("Output is invalid");if(!n.typeforce.Number(d[d.length-2]))throw new TypeError("Output is invalid");if(d[d.length-1]!==i.OP_CHECKMULTISIG)throw new TypeError("Output is invalid");if(l.m<=0||l.n>16||l.m>l.n||l.n!==d.length-3)throw new TypeError("Output is invalid");if(!l.pubkeys.every(m=>(0,n.isPoint)(m)))throw new TypeError("Output is invalid");if(c.m!==void 0&&c.m!==l.m)throw new TypeError("m mismatch");if(c.n!==void 0&&c.n!==l.n)throw new TypeError("n mismatch");if(c.pubkeys&&!(0,n.stacksEqual)(c.pubkeys,l.pubkeys))throw new TypeError("Pubkeys mismatch")}if(c.pubkeys){if(c.n!==void 0&&c.n!==c.pubkeys.length)throw new TypeError("Pubkey count mismatch");if(l.n=c.pubkeys.length,l.n<l.m)throw new TypeError("Pubkey count cannot be less than m")}if(c.signatures){if(c.signatures.length<l.m)throw new TypeError("Not enough signatures provided");if(c.signatures.length>l.m)throw new TypeError("Too many signatures provided")}if(c.input){if(c.input[0]!==i.OP_0)throw new TypeError("Input is invalid");if(l.signatures.length===0||!l.signatures.every(u))throw new TypeError("Input has invalid signature(s)");if(c.signatures&&!(0,n.stacksEqual)(c.signatures,l.signatures))throw new TypeError("Signature mismatch");if(c.m!==void 0&&c.m!==c.signatures.length)throw new TypeError("Signature count mismatch")}}return Object.assign(l,c)}return co.p2ms=o,co}var ao={},rp;function X8(){if(rp)return ao;rp=1,Object.defineProperty(ao,"__esModule",{value:!0}),ao.p2pk=void 0;const e=Xn(),t=rn(),n=zt(),r=gi(),i=t.OPS;function s(o,c){if(!o.input&&!o.output&&!o.pubkey&&!o.input&&!o.signature)throw new TypeError("Not enough data");c=Object.assign({validate:!0},c||{}),(0,n.typeforce)({network:n.typeforce.maybe(n.typeforce.Object),output:n.typeforce.maybe(n.typeforce.Buffer),pubkey:n.typeforce.maybe(n.isPoint),signature:n.typeforce.maybe(t.isCanonicalScriptSignature),input:n.typeforce.maybe(n.typeforce.Buffer)},o);const a=r.value(()=>t.decompile(o.input)),f={name:"p2pk",network:o.network||e.bitcoin};if(r.prop(f,"output",()=>{if(o.pubkey)return t.compile([o.pubkey,i.OP_CHECKSIG])}),r.prop(f,"pubkey",()=>{if(o.output)return o.output.slice(1,-1)}),r.prop(f,"signature",()=>{if(o.input)return a()[0]}),r.prop(f,"input",()=>{if(o.signature)return t.compile([o.signature])}),r.prop(f,"witness",()=>{if(f.input)return[]}),c.validate){if(o.output){if(o.output[o.output.length-1]!==i.OP_CHECKSIG)throw new TypeError("Output is invalid");if(!(0,n.isPoint)(f.pubkey))throw new TypeError("Output pubkey is invalid");if(o.pubkey&&!o.pubkey.equals(f.pubkey))throw new TypeError("Pubkey mismatch")}if(o.signature&&o.input&&!o.input.equals(f.input))throw new TypeError("Signature mismatch");if(o.input){if(a().length!==1)throw new TypeError("Input is invalid");if(!t.isCanonicalScriptSignature(f.signature))throw new TypeError("Input has invalid signature")}}return Object.assign(f,o)}return ao.p2pk=s,ao}var uo={},xf={},Ai={},Jt={},ip;function Xw(){if(ip)return Jt;ip=1,Object.defineProperty(Jt,"__esModule",{value:!0}),Jt.ripemd160=Jt.RIPEMD160=Jt.md5=Jt.MD5=Jt.sha1=Jt.SHA1=void 0;const e=Uy(),t=ui(),n=Uint32Array.from([1732584193,4023233417,2562383102,271733878,3285377520]),r=new Uint32Array(80);class i extends e.HashMD{constructor(){super(64,20,8,!1),this.A=n[0]|0,this.B=n[1]|0,this.C=n[2]|0,this.D=n[3]|0,this.E=n[4]|0}get(){const{A:v,B:U,C:L,D:K,E:H}=this;return[v,U,L,K,H]}set(v,U,L,K,H){this.A=v|0,this.B=U|0,this.C=L|0,this.D=K|0,this.E=H|0}process(v,U){for(let D=0;D<16;D++,U+=4)r[D]=v.getUint32(U,!1);for(let D=16;D<80;D++)r[D]=(0,t.rotl)(r[D-3]^r[D-8]^r[D-14]^r[D-16],1);let{A:L,B:K,C:H,D:$,E:P}=this;for(let D=0;D<80;D++){let F,x;D<20?(F=(0,e.Chi)(K,H,$),x=1518500249):D<40?(F=K^H^$,x=1859775393):D<60?(F=(0,e.Maj)(K,H,$),x=2400959708):(F=K^H^$,x=3395469782);const B=(0,t.rotl)(L,5)+F+P+x+r[D]|0;P=$,$=H,H=(0,t.rotl)(K,30),K=L,L=B}L=L+this.A|0,K=K+this.B|0,H=H+this.C|0,$=$+this.D|0,P=P+this.E|0,this.set(L,K,H,$,P)}roundClean(){(0,t.clean)(r)}destroy(){this.set(0,0,0,0,0),(0,t.clean)(this.buffer)}}Jt.SHA1=i,Jt.sha1=(0,t.createHasher)(()=>new i);const s=Math.pow(2,32),o=Array.from({length:64},(M,v)=>Math.floor(s*Math.abs(Math.sin(v+1)))),c=n.slice(0,4),a=new Uint32Array(16);class u extends e.HashMD{constructor(){super(64,16,8,!0),this.A=c[0]|0,this.B=c[1]|0,this.C=c[2]|0,this.D=c[3]|0}get(){const{A:v,B:U,C:L,D:K}=this;return[v,U,L,K]}set(v,U,L,K){this.A=v|0,this.B=U|0,this.C=L|0,this.D=K|0}process(v,U){for(let P=0;P<16;P++,U+=4)a[P]=v.getUint32(U,!0);let{A:L,B:K,C:H,D:$}=this;for(let P=0;P<64;P++){let D,F,x;P<16?(D=(0,e.Chi)(K,H,$),F=P,x=[7,12,17,22]):P<32?(D=(0,e.Chi)($,K,H),F=(5*P+1)%16,x=[5,9,14,20]):P<48?(D=K^H^$,F=(3*P+5)%16,x=[4,11,16,23]):(D=H^(K|~$),F=7*P%16,x=[6,10,15,21]),D=D+L+o[P]+a[F],L=$,$=H,H=K,K=K+(0,t.rotl)(D,x[P%4])}L=L+this.A|0,K=K+this.B|0,H=H+this.C|0,$=$+this.D|0,this.set(L,K,H,$)}roundClean(){(0,t.clean)(a)}destroy(){this.set(0,0,0,0),(0,t.clean)(this.buffer)}}Jt.MD5=u,Jt.md5=(0,t.createHasher)(()=>new u);const f=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),l=Uint8Array.from(new Array(16).fill(0).map((M,v)=>v)),d=l.map(M=>(9*M+5)%16),p=(()=>{const U=[[l],[d]];for(let L=0;L<4;L++)for(let K of U)K.push(K[L].map(H=>f[H]));return U})(),g=p[0],m=p[1],E=[[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(M=>Uint8Array.from(M)),_=g.map((M,v)=>M.map(U=>E[v][U])),S=m.map((M,v)=>M.map(U=>E[v][U])),w=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),T=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function O(M,v,U,L){return M===0?v^U^L:M===1?v&U|~v&L:M===2?(v|~U)^L:M===3?v&L|U&~L:v^(U|~L)}const A=new Uint32Array(16);class R extends e.HashMD{constructor(){super(64,20,8,!0),this.h0=1732584193,this.h1=-271733879,this.h2=-1732584194,this.h3=271733878,this.h4=-1009589776}get(){const{h0:v,h1:U,h2:L,h3:K,h4:H}=this;return[v,U,L,K,H]}set(v,U,L,K,H){this.h0=v|0,this.h1=U|0,this.h2=L|0,this.h3=K|0,this.h4=H|0}process(v,U){for(let N=0;N<16;N++,U+=4)A[N]=v.getUint32(U,!0);let L=this.h0|0,K=L,H=this.h1|0,$=H,P=this.h2|0,D=P,F=this.h3|0,x=F,B=this.h4|0,q=B;for(let N=0;N<5;N++){const V=4-N,G=w[N],X=T[N],Y=g[N],ce=m[N],ue=_[N],te=S[N];for(let Z=0;Z<16;Z++){const ae=(0,t.rotl)(L+O(N,H,P,F)+A[Y[Z]]+G,ue[Z])+B|0;L=B,B=F,F=(0,t.rotl)(P,10)|0,P=H,H=ae}for(let Z=0;Z<16;Z++){const ae=(0,t.rotl)(K+O(V,$,D,x)+A[ce[Z]]+X,te[Z])+q|0;K=q,q=x,x=(0,t.rotl)(D,10)|0,D=$,$=ae}}this.set(this.h1+P+x|0,this.h2+F+q|0,this.h3+B+K|0,this.h4+L+$|0,this.h0+H+D|0)}roundClean(){(0,t.clean)(A)}destroy(){this.destroyed=!0,(0,t.clean)(this.buffer),this.set(0,0,0,0,0)}}return Jt.RIPEMD160=R,Jt.ripemd160=(0,t.createHasher)(()=>new R),Jt}var sp;function z8(){if(sp)return Ai;sp=1,Object.defineProperty(Ai,"__esModule",{value:!0}),Ai.ripemd160=Ai.RIPEMD160=void 0;const e=Xw();return Ai.RIPEMD160=e.RIPEMD160,Ai.ripemd160=e.ripemd160,Ai}var vi={},op;function Y8(){if(op)return vi;op=1,Object.defineProperty(vi,"__esModule",{value:!0}),vi.sha1=vi.SHA1=void 0;const e=Xw();return vi.SHA1=e.SHA1,vi.sha1=e.sha1,vi}var Cn={},cp;function zw(){if(cp)return Cn;cp=1,Object.defineProperty(Cn,"__esModule",{value:!0}),Cn.sha224=Cn.SHA224=Cn.sha256=Cn.SHA256=void 0;const e=Cy();return Cn.SHA256=e.SHA256,Cn.sha256=e.sha256,Cn.SHA224=e.SHA224,Cn.sha224=e.sha224,Cn}var ap;function Ur(){return ap||(ap=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.taggedHash=e.TAGGED_HASH_PREFIXES=e.TAGS=e.hash256=e.hash160=e.sha256=e.sha1=e.ripemd160=void 0;const t=z8(),n=Y8(),r=zw();function i(f){return Buffer.from((0,t.ripemd160)(Uint8Array.from(f)))}e.ripemd160=i;function s(f){return Buffer.from((0,n.sha1)(Uint8Array.from(f)))}e.sha1=s;function o(f){return Buffer.from((0,r.sha256)(Uint8Array.from(f)))}e.sha256=o;function c(f){return Buffer.from((0,t.ripemd160)((0,r.sha256)(Uint8Array.from(f))))}e.hash160=c;function a(f){return Buffer.from((0,r.sha256)((0,r.sha256)(Uint8Array.from(f))))}e.hash256=a,e.TAGS=["BIP0340/challenge","BIP0340/aux","BIP0340/nonce","TapLeaf","TapBranch","TapSighash","TapTweak","KeyAgg list","KeyAgg coefficient"],e.TAGGED_HASH_PREFIXES={"BIP0340/challenge":Buffer.from([123,181,45,122,159,239,88,50,62,177,191,122,64,125,179,130,210,243,242,216,27,177,34,79,73,254,81,143,109,72,211,124,123,181,45,122,159,239,88,50,62,177,191,122,64,125,179,130,210,243,242,216,27,177,34,79,73,254,81,143,109,72,211,124]),"BIP0340/aux":Buffer.from([241,239,78,94,192,99,202,218,109,148,202,250,157,152,126,160,105,38,88,57,236,193,31,151,45,119,165,46,216,193,204,144,241,239,78,94,192,99,202,218,109,148,202,250,157,152,126,160,105,38,88,57,236,193,31,151,45,119,165,46,216,193,204,144]),"BIP0340/nonce":Buffer.from([7,73,119,52,167,155,203,53,91,155,140,125,3,79,18,28,244,52,215,62,247,45,218,25,135,0,97,251,82,191,235,47,7,73,119,52,167,155,203,53,91,155,140,125,3,79,18,28,244,52,215,62,247,45,218,25,135,0,97,251,82,191,235,47]),TapLeaf:Buffer.from([174,234,143,220,66,8,152,49,5,115,75,88,8,29,30,38,56,211,95,28,181,64,8,212,211,87,202,3,190,120,233,238,174,234,143,220,66,8,152,49,5,115,75,88,8,29,30,38,56,211,95,28,181,64,8,212,211,87,202,3,190,120,233,238]),TapBranch:Buffer.from([25,65,161,242,229,110,185,95,162,169,241,148,190,92,1,247,33,111,51,237,130,176,145,70,52,144,208,91,245,22,160,21,25,65,161,242,229,110,185,95,162,169,241,148,190,92,1,247,33,111,51,237,130,176,145,70,52,144,208,91,245,22,160,21]),TapSighash:Buffer.from([244,10,72,223,75,42,112,200,180,146,75,242,101,70,97,237,61,149,253,102,163,19,235,135,35,117,151,198,40,228,160,49,244,10,72,223,75,42,112,200,180,146,75,242,101,70,97,237,61,149,253,102,163,19,235,135,35,117,151,198,40,228,160,49]),TapTweak:Buffer.from([232,15,225,99,156,156,160,80,227,175,27,57,193,67,198,62,66,156,188,235,21,217,64,251,181,197,161,244,175,87,197,233,232,15,225,99,156,156,160,80,227,175,27,57,193,67,198,62,66,156,188,235,21,217,64,251,181,197,161,244,175,87,197,233]),"KeyAgg list":Buffer.from([72,28,151,28,60,11,70,215,240,178,117,174,89,141,78,44,126,215,49,156,89,74,92,110,199,158,160,212,153,2,148,240,72,28,151,28,60,11,70,215,240,178,117,174,89,141,78,44,126,215,49,156,89,74,92,110,199,158,160,212,153,2,148,240]),"KeyAgg coefficient":Buffer.from([191,201,4,3,77,28,136,232,200,14,34,229,61,36,86,109,100,130,78,214,66,114,129,192,145,0,249,77,205,82,201,129,191,201,4,3,77,28,136,232,200,14,34,229,61,36,86,109,100,130,78,214,66,114,129,192,145,0,249,77,205,82,201,129])};function u(f,l){return o(Buffer.concat([e.TAGGED_HASH_PREFIXES[f],l]))}e.taggedHash=u})(xf)),xf}var Af,up;function Z8(){if(up)return Af;up=1;function e(t){if(t.length>=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r<n.length;r++)n[r]=255;for(var i=0;i<t.length;i++){var s=t.charAt(i),o=s.charCodeAt(0);if(n[o]!==255)throw new TypeError(s+" is ambiguous");n[o]=i}var c=t.length,a=t.charAt(0),u=Math.log(c)/Math.log(256),f=Math.log(256)/Math.log(c);function l(g){if(g instanceof Uint8Array||(ArrayBuffer.isView(g)?g=new Uint8Array(g.buffer,g.byteOffset,g.byteLength):Array.isArray(g)&&(g=Uint8Array.from(g))),!(g instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(g.length===0)return"";for(var m=0,E=0,_=0,S=g.length;_!==S&&g[_]===0;)_++,m++;for(var w=(S-_)*f+1>>>0,T=new Uint8Array(w);_!==S;){for(var O=g[_],A=0,R=w-1;(O!==0||A<E)&&R!==-1;R--,A++)O+=256*T[R]>>>0,T[R]=O%c>>>0,O=O/c>>>0;if(O!==0)throw new Error("Non-zero carry");E=A,_++}for(var M=w-E;M!==w&&T[M]===0;)M++;for(var v=a.repeat(m);M<w;++M)v+=t.charAt(T[M]);return v}function d(g){if(typeof g!="string")throw new TypeError("Expected String");if(g.length===0)return new Uint8Array;for(var m=0,E=0,_=0;g[m]===a;)E++,m++;for(var S=(g.length-m)*u+1>>>0,w=new Uint8Array(S);g[m];){var T=g.charCodeAt(m);if(T>255)return;var O=n[T];if(O===255)return;for(var A=0,R=S-1;(O!==0||A<_)&&R!==-1;R--,A++)O+=c*w[R]>>>0,w[R]=O%256>>>0,O=O/256>>>0;if(O!==0)throw new Error("Non-zero carry");_=A,m++}for(var M=S-_;M!==S&&w[M]===0;)M++;for(var v=new Uint8Array(E+(S-M)),U=E;M!==S;)v[U++]=w[M++];return v}function p(g){var m=d(g);if(m)return m;throw new Error("Non-base"+c+" character")}return{encode:l,decodeUnsafe:d,decode:p}}return Af=e,Af}var vf,fp;function J8(){return fp||(fp=1,vf=Z8()("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")),vf}var If,lp;function Q8(){if(lp)return If;lp=1;var e=J8();return If=function(t){function n(o){var c=Uint8Array.from(o),a=t(c),u=c.length+4,f=new Uint8Array(u);return f.set(c,0),f.set(a.subarray(0,4),c.length),e.encode(f,u)}function r(o){var c=o.slice(0,-4),a=o.slice(-4),u=t(c);if(!(a[0]^u[0]|a[1]^u[1]|a[2]^u[2]|a[3]^u[3]))return c}function i(o){var c=e.decodeUnsafe(o);if(c)return r(c)}function s(o){var c=e.decode(o),a=r(c);if(!a)throw new Error("Invalid checksum");return a}return{encode:n,decode:s,decodeUnsafe:i}},If}var Of,hp;function Sh(){if(hp)return Of;hp=1;var{sha256:e}=zw(),t=Q8();function n(r){return e(e(r))}return Of=t(n),Of}var dp;function eA(){if(dp)return uo;dp=1,Object.defineProperty(uo,"__esModule",{value:!0}),uo.p2pkh=void 0;const e=Ur(),t=Xn(),n=rn(),r=zt(),i=gi(),s=Sh(),o=n.OPS;function c(a,u){if(!a.address&&!a.hash&&!a.output&&!a.pubkey&&!a.input)throw new TypeError("Not enough data");u=Object.assign({validate:!0},u||{}),(0,r.typeforce)({network:r.typeforce.maybe(r.typeforce.Object),address:r.typeforce.maybe(r.typeforce.String),hash:r.typeforce.maybe(r.typeforce.BufferN(20)),output:r.typeforce.maybe(r.typeforce.BufferN(25)),pubkey:r.typeforce.maybe(r.isPoint),signature:r.typeforce.maybe(n.isCanonicalScriptSignature),input:r.typeforce.maybe(r.typeforce.Buffer)},a);const f=i.value(()=>{const g=Buffer.from(s.decode(a.address)),m=g.readUInt8(0),E=g.slice(1);return{version:m,hash:E}}),l=i.value(()=>n.decompile(a.input)),d=a.network||t.bitcoin,p={name:"p2pkh",network:d};if(i.prop(p,"address",()=>{if(!p.hash)return;const g=Buffer.allocUnsafe(21);return g.writeUInt8(d.pubKeyHash,0),p.hash.copy(g,1),s.encode(g)}),i.prop(p,"hash",()=>{if(a.output)return a.output.slice(3,23);if(a.address)return f().hash;if(a.pubkey||p.pubkey)return e.hash160(a.pubkey||p.pubkey)}),i.prop(p,"output",()=>{if(p.hash)return n.compile([o.OP_DUP,o.OP_HASH160,p.hash,o.OP_EQUALVERIFY,o.OP_CHECKSIG])}),i.prop(p,"pubkey",()=>{if(a.input)return l()[1]}),i.prop(p,"signature",()=>{if(a.input)return l()[0]}),i.prop(p,"input",()=>{if(a.pubkey&&a.signature)return n.compile([a.signature,a.pubkey])}),i.prop(p,"witness",()=>{if(p.input)return[]}),u.validate){let g=Buffer.from([]);if(a.address){if(f().version!==d.pubKeyHash)throw new TypeError("Invalid version or Network mismatch");if(f().hash.length!==20)throw new TypeError("Invalid address");g=f().hash}if(a.hash){if(g.length>0&&!g.equals(a.hash))throw new TypeError("Hash mismatch");g=a.hash}if(a.output){if(a.output.length!==25||a.output[0]!==o.OP_DUP||a.output[1]!==o.OP_HASH160||a.output[2]!==20||a.output[23]!==o.OP_EQUALVERIFY||a.output[24]!==o.OP_CHECKSIG)throw new TypeError("Output is invalid");const m=a.output.slice(3,23);if(g.length>0&&!g.equals(m))throw new TypeError("Hash mismatch");g=m}if(a.pubkey){const m=e.hash160(a.pubkey);if(g.length>0&&!g.equals(m))throw new TypeError("Hash mismatch");g=m}if(a.input){const m=l();if(m.length!==2)throw new TypeError("Input is invalid");if(!n.isCanonicalScriptSignature(m[0]))throw new TypeError("Input has invalid signature");if(!(0,r.isPoint)(m[1]))throw new TypeError("Input has invalid pubkey");if(a.signature&&!a.signature.equals(m[0]))throw new TypeError("Signature mismatch");if(a.pubkey&&!a.pubkey.equals(m[1]))throw new TypeError("Pubkey mismatch");const E=e.hash160(m[1]);if(g.length>0&&!g.equals(E))throw new TypeError("Hash mismatch")}}return Object.assign(p,a)}return uo.p2pkh=c,uo}var fo={},pp;function tA(){if(pp)return fo;pp=1,Object.defineProperty(fo,"__esModule",{value:!0}),fo.p2sh=void 0;const e=Ur(),t=Xn(),n=rn(),r=zt(),i=gi(),s=Sh(),o=n.OPS;function c(a,u){if(!a.address&&!a.hash&&!a.output&&!a.redeem&&!a.input)throw new TypeError("Not enough data");u=Object.assign({validate:!0},u||{}),(0,r.typeforce)({network:r.typeforce.maybe(r.typeforce.Object),address:r.typeforce.maybe(r.typeforce.String),hash:r.typeforce.maybe(r.typeforce.BufferN(20)),output:r.typeforce.maybe(r.typeforce.BufferN(23)),redeem:r.typeforce.maybe({network:r.typeforce.maybe(r.typeforce.Object),output:r.typeforce.maybe(r.typeforce.Buffer),input:r.typeforce.maybe(r.typeforce.Buffer),witness:r.typeforce.maybe(r.typeforce.arrayOf(r.typeforce.Buffer))}),input:r.typeforce.maybe(r.typeforce.Buffer),witness:r.typeforce.maybe(r.typeforce.arrayOf(r.typeforce.Buffer))},a);let f=a.network;f||(f=a.redeem&&a.redeem.network||t.bitcoin);const l={network:f},d=i.value(()=>{const m=Buffer.from(s.decode(a.address)),E=m.readUInt8(0),_=m.slice(1);return{version:E,hash:_}}),p=i.value(()=>n.decompile(a.input)),g=i.value(()=>{const m=p(),E=m[m.length-1];return{network:f,output:E===o.OP_FALSE?Buffer.from([]):E,input:n.compile(m.slice(0,-1)),witness:a.witness||[]}});if(i.prop(l,"address",()=>{if(!l.hash)return;const m=Buffer.allocUnsafe(21);return m.writeUInt8(l.network.scriptHash,0),l.hash.copy(m,1),s.encode(m)}),i.prop(l,"hash",()=>{if(a.output)return a.output.slice(2,22);if(a.address)return d().hash;if(l.redeem&&l.redeem.output)return e.hash160(l.redeem.output)}),i.prop(l,"output",()=>{if(l.hash)return n.compile([o.OP_HASH160,l.hash,o.OP_EQUAL])}),i.prop(l,"redeem",()=>{if(a.input)return g()}),i.prop(l,"input",()=>{if(!(!a.redeem||!a.redeem.input||!a.redeem.output))return n.compile([].concat(n.decompile(a.redeem.input),a.redeem.output))}),i.prop(l,"witness",()=>{if(l.redeem&&l.redeem.witness)return l.redeem.witness;if(l.input)return[]}),i.prop(l,"name",()=>{const m=["p2sh"];return l.redeem!==void 0&&l.redeem.name!==void 0&&m.push(l.redeem.name),m.join("-")}),u.validate){let m=Buffer.from([]);if(a.address){if(d().version!==f.scriptHash)throw new TypeError("Invalid version or Network mismatch");if(d().hash.length!==20)throw new TypeError("Invalid address");m=d().hash}if(a.hash){if(m.length>0&&!m.equals(a.hash))throw new TypeError("Hash mismatch");m=a.hash}if(a.output){if(a.output.length!==23||a.output[0]!==o.OP_HASH160||a.output[1]!==20||a.output[22]!==o.OP_EQUAL)throw new TypeError("Output is invalid");const _=a.output.slice(2,22);if(m.length>0&&!m.equals(_))throw new TypeError("Hash mismatch");m=_}const E=_=>{if(_.output){const S=n.decompile(_.output);if(!S||S.length<1)throw new TypeError("Redeem.output too short");if(_.output.byteLength>520)throw new TypeError("Redeem.output unspendable if larger than 520 bytes");if(n.countNonPushOnlyOPs(S)>201)throw new TypeError("Redeem.output unspendable with more than 201 non-push ops");const w=e.hash160(_.output);if(m.length>0&&!m.equals(w))throw new TypeError("Hash mismatch");m=w}if(_.input){const S=_.input.length>0,w=_.witness&&_.witness.length>0;if(!S&&!w)throw new TypeError("Empty input");if(S&&w)throw new TypeError("Input and witness provided");if(S){const T=n.decompile(_.input);if(!n.isPushOnly(T))throw new TypeError("Non push-only scriptSig")}}};if(a.input){const _=p();if(!_||_.length<1)throw new TypeError("Input too short");if(!Buffer.isBuffer(g().output))throw new TypeError("Input is invalid");E(g())}if(a.redeem){if(a.redeem.network&&a.redeem.network!==f)throw new TypeError("Network mismatch");if(a.input){const _=g();if(a.redeem.output&&!a.redeem.output.equals(_.output))throw new TypeError("Redeem.output mismatch");if(a.redeem.input&&!a.redeem.input.equals(_.input))throw new TypeError("Redeem.input mismatch")}E(a.redeem)}if(a.witness&&a.redeem&&a.redeem.witness&&!(0,r.stacksEqual)(a.redeem.witness,a.witness))throw new TypeError("Witness and redeem.witness mismatch")}return Object.assign(l,a)}return fo.p2sh=c,fo}var lo={},yp;function nA(){if(yp)return lo;yp=1,Object.defineProperty(lo,"__esModule",{value:!0}),lo.p2wpkh=void 0;const e=Ur(),t=Xn(),n=rn(),r=zt(),i=gi(),s=ac(),o=n.OPS,c=Buffer.alloc(0);function a(u,f){if(!u.address&&!u.hash&&!u.output&&!u.pubkey&&!u.witness)throw new TypeError("Not enough data");f=Object.assign({validate:!0},f||{}),(0,r.typeforce)({address:r.typeforce.maybe(r.typeforce.String),hash:r.typeforce.maybe(r.typeforce.BufferN(20)),input:r.typeforce.maybe(r.typeforce.BufferN(0)),network:r.typeforce.maybe(r.typeforce.Object),output:r.typeforce.maybe(r.typeforce.BufferN(22)),pubkey:r.typeforce.maybe(r.isPoint),signature:r.typeforce.maybe(n.isCanonicalScriptSignature),witness:r.typeforce.maybe(r.typeforce.arrayOf(r.typeforce.Buffer))},u);const l=i.value(()=>{const g=s.bech32.decode(u.address),m=g.words.shift(),E=s.bech32.fromWords(g.words);return{version:m,prefix:g.prefix,data:Buffer.from(E)}}),d=u.network||t.bitcoin,p={name:"p2wpkh",network:d};if(i.prop(p,"address",()=>{if(!p.hash)return;const g=s.bech32.toWords(p.hash);return g.unshift(0),s.bech32.encode(d.bech32,g)}),i.prop(p,"hash",()=>{if(u.output)return u.output.slice(2,22);if(u.address)return l().data;if(u.pubkey||p.pubkey)return e.hash160(u.pubkey||p.pubkey)}),i.prop(p,"output",()=>{if(p.hash)return n.compile([o.OP_0,p.hash])}),i.prop(p,"pubkey",()=>{if(u.pubkey)return u.pubkey;if(u.witness)return u.witness[1]}),i.prop(p,"signature",()=>{if(u.witness)return u.witness[0]}),i.prop(p,"input",()=>{if(p.witness)return c}),i.prop(p,"witness",()=>{if(u.pubkey&&u.signature)return[u.signature,u.pubkey]}),f.validate){let g=Buffer.from([]);if(u.address){if(d&&d.bech32!==l().prefix)throw new TypeError("Invalid prefix or Network mismatch");if(l().version!==0)throw new TypeError("Invalid address version");if(l().data.length!==20)throw new TypeError("Invalid address data");g=l().data}if(u.hash){if(g.length>0&&!g.equals(u.hash))throw new TypeError("Hash mismatch");g=u.hash}if(u.output){if(u.output.length!==22||u.output[0]!==o.OP_0||u.output[1]!==20)throw new TypeError("Output is invalid");if(g.length>0&&!g.equals(u.output.slice(2)))throw new TypeError("Hash mismatch");g=u.output.slice(2)}if(u.pubkey){const m=e.hash160(u.pubkey);if(g.length>0&&!g.equals(m))throw new TypeError("Hash mismatch");if(g=m,!(0,r.isPoint)(u.pubkey)||u.pubkey.length!==33)throw new TypeError("Invalid pubkey for p2wpkh")}if(u.witness){if(u.witness.length!==2)throw new TypeError("Witness is invalid");if(!n.isCanonicalScriptSignature(u.witness[0]))throw new TypeError("Witness has invalid signature");if(!(0,r.isPoint)(u.witness[1])||u.witness[1].length!==33)throw new TypeError("Witness has invalid pubkey");if(u.signature&&!u.signature.equals(u.witness[0]))throw new TypeError("Signature mismatch");if(u.pubkey&&!u.pubkey.equals(u.witness[1]))throw new TypeError("Pubkey mismatch");const m=e.hash160(u.witness[1]);if(g.length>0&&!g.equals(m))throw new TypeError("Hash mismatch")}}return Object.assign(p,u)}return lo.p2wpkh=a,lo}var ho={},gp;function rA(){if(gp)return ho;gp=1,Object.defineProperty(ho,"__esModule",{value:!0}),ho.p2wsh=void 0;const e=Ur(),t=Xn(),n=rn(),r=zt(),i=gi(),s=ac(),o=n.OPS,c=Buffer.alloc(0);function a(f){return!!(Buffer.isBuffer(f)&&f.length===65&&f[0]===4&&(0,r.isPoint)(f))}function u(f,l){if(!f.address&&!f.hash&&!f.output&&!f.redeem&&!f.witness)throw new TypeError("Not enough data");l=Object.assign({validate:!0},l||{}),(0,r.typeforce)({network:r.typeforce.maybe(r.typeforce.Object),address:r.typeforce.maybe(r.typeforce.String),hash:r.typeforce.maybe(r.typeforce.BufferN(32)),output:r.typeforce.maybe(r.typeforce.BufferN(34)),redeem:r.typeforce.maybe({input:r.typeforce.maybe(r.typeforce.Buffer),network:r.typeforce.maybe(r.typeforce.Object),output:r.typeforce.maybe(r.typeforce.Buffer),witness:r.typeforce.maybe(r.typeforce.arrayOf(r.typeforce.Buffer))}),input:r.typeforce.maybe(r.typeforce.BufferN(0)),witness:r.typeforce.maybe(r.typeforce.arrayOf(r.typeforce.Buffer))},f);const d=i.value(()=>{const E=s.bech32.decode(f.address),_=E.words.shift(),S=s.bech32.fromWords(E.words);return{version:_,prefix:E.prefix,data:Buffer.from(S)}}),p=i.value(()=>n.decompile(f.redeem.input));let g=f.network;g||(g=f.redeem&&f.redeem.network||t.bitcoin);const m={network:g};if(i.prop(m,"address",()=>{if(!m.hash)return;const E=s.bech32.toWords(m.hash);return E.unshift(0),s.bech32.encode(g.bech32,E)}),i.prop(m,"hash",()=>{if(f.output)return f.output.slice(2);if(f.address)return d().data;if(m.redeem&&m.redeem.output)return e.sha256(m.redeem.output)}),i.prop(m,"output",()=>{if(m.hash)return n.compile([o.OP_0,m.hash])}),i.prop(m,"redeem",()=>{if(f.witness)return{output:f.witness[f.witness.length-1],input:c,witness:f.witness.slice(0,-1)}}),i.prop(m,"input",()=>{if(m.witness)return c}),i.prop(m,"witness",()=>{if(f.redeem&&f.redeem.input&&f.redeem.input.length>0&&f.redeem.output&&f.redeem.output.length>0){const E=n.toStack(p());return m.redeem=Object.assign({witness:E},f.redeem),m.redeem.input=c,[].concat(E,f.redeem.output)}if(f.redeem&&f.redeem.output&&f.redeem.witness)return[].concat(f.redeem.witness,f.redeem.output)}),i.prop(m,"name",()=>{const E=["p2wsh"];return m.redeem!==void 0&&m.redeem.name!==void 0&&E.push(m.redeem.name),E.join("-")}),l.validate){let E=Buffer.from([]);if(f.address){if(d().prefix!==g.bech32)throw new TypeError("Invalid prefix or Network mismatch");if(d().version!==0)throw new TypeError("Invalid address version");if(d().data.length!==32)throw new TypeError("Invalid address data");E=d().data}if(f.hash){if(E.length>0&&!E.equals(f.hash))throw new TypeError("Hash mismatch");E=f.hash}if(f.output){if(f.output.length!==34||f.output[0]!==o.OP_0||f.output[1]!==32)throw new TypeError("Output is invalid");const _=f.output.slice(2);if(E.length>0&&!E.equals(_))throw new TypeError("Hash mismatch");E=_}if(f.redeem){if(f.redeem.network&&f.redeem.network!==g)throw new TypeError("Network mismatch");if(f.redeem.input&&f.redeem.input.length>0&&f.redeem.witness&&f.redeem.witness.length>0)throw new TypeError("Ambiguous witness source");if(f.redeem.output){const _=n.decompile(f.redeem.output);if(!_||_.length<1)throw new TypeError("Redeem.output is invalid");if(f.redeem.output.byteLength>3600)throw new TypeError("Redeem.output unspendable if larger than 3600 bytes");if(n.countNonPushOnlyOPs(_)>201)throw new TypeError("Redeem.output unspendable with more than 201 non-push ops");const S=e.sha256(f.redeem.output);if(E.length>0&&!E.equals(S))throw new TypeError("Hash mismatch");E=S}if(f.redeem.input&&!n.isPushOnly(p()))throw new TypeError("Non push-only scriptSig");if(f.witness&&f.redeem.witness&&!(0,r.stacksEqual)(f.witness,f.redeem.witness))throw new TypeError("Witness and redeem.witness mismatch");if(f.redeem.input&&p().some(a)||f.redeem.output&&(n.decompile(f.redeem.output)||[]).some(a))throw new TypeError("redeem.input or redeem.output contains uncompressed pubkey")}if(f.witness&&f.witness.length>0){const _=f.witness[f.witness.length-1];if(f.redeem&&f.redeem.output&&!f.redeem.output.equals(_))throw new TypeError("Witness and redeem.output mismatch");if(f.witness.some(a)||(n.decompile(_)||[]).some(a))throw new TypeError("Witness contains uncompressed pubkey")}}return Object.assign(m,f)}return ho.p2wsh=u,ho}var po={},Ii={},mp;function Th(){if(mp)return Ii;mp=1,Object.defineProperty(Ii,"__esModule",{value:!0}),Ii.getEccLib=Ii.initEccLib=void 0;const e={};function t(c,a){c?c!==e.eccLib&&(a?.DANGER_DO_NOT_VERIFY_ECCLIB||i(c),e.eccLib=c):e.eccLib=c}Ii.initEccLib=t;function n(){if(!e.eccLib)throw new Error("No ECC Library provided. You must call initEccLib() with a valid TinySecp256k1Interface instance");return e.eccLib}Ii.getEccLib=n;const r=c=>Buffer.from(c,"hex");function i(c){s(typeof c.isXOnlyPoint=="function"),s(c.isXOnlyPoint(r("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))),s(c.isXOnlyPoint(r("fffffffffffffffffffffffffffffffffffffffffffffffffffffffeeffffc2e"))),s(c.isXOnlyPoint(r("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"))),s(c.isXOnlyPoint(r("0000000000000000000000000000000000000000000000000000000000000001"))),s(!c.isXOnlyPoint(r("0000000000000000000000000000000000000000000000000000000000000000"))),s(!c.isXOnlyPoint(r("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"))),s(typeof c.xOnlyPointAddTweak=="function"),o.forEach(a=>{const u=c.xOnlyPointAddTweak(r(a.pubkey),r(a.tweak));a.result===null?s(u===null):(s(u!==null),s(u.parity===a.parity),s(Buffer.from(u.xOnlyPubkey).equals(r(a.result))))})}function s(c){if(!c)throw new Error("ecc library invalid")}const o=[{pubkey:"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",tweak:"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",parity:-1,result:null},{pubkey:"1617d38ed8d8657da4d4761e8057bc396ea9e4b9d29776d4be096016dbd2509b",tweak:"a8397a935f0dfceba6ba9618f6451ef4d80637abf4e6af2669fbc9de6a8fd2ac",parity:1,result:"e478f99dab91052ab39a33ea35fd5e6e4933f4d28023cd597c9a1f6760346adf"},{pubkey:"2c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",tweak:"823c3cd2142744b075a87eade7e1b8678ba308d566226a0056ca2b7a76f86b47",parity:0,result:"9534f8dc8c6deda2dc007655981c78b49c5d96c778fbf363462a11ec9dfd948c"}];return Ii}var Bf={},Pt={},Fc={exports:{}};/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */var wp;function iA(){return wp||(wp=1,(function(e,t){var n=tc(),r=n.Buffer;function i(o,c){for(var a in o)c[a]=o[a]}r.from&&r.alloc&&r.allocUnsafe&&r.allocUnsafeSlow?e.exports=n:(i(n,t),t.Buffer=s);function s(o,c,a){return r(o,c,a)}s.prototype=Object.create(r.prototype),i(r,s),s.from=function(o,c,a){if(typeof o=="number")throw new TypeError("Argument must not be a number");return r(o,c,a)},s.alloc=function(o,c,a){if(typeof o!="number")throw new TypeError("Argument must be a number");var u=r(o);return c!==void 0?typeof a=="string"?u.fill(c,a):u.fill(c):u.fill(0),u},s.allocUnsafe=function(o){if(typeof o!="number")throw new TypeError("Argument must be a number");return r(o)},s.allocUnsafeSlow=function(o){if(typeof o!="number")throw new TypeError("Argument must be a number");return n.SlowBuffer(o)}})(Fc,Fc.exports)),Fc.exports}var Nf,bp;function sA(){if(bp)return Nf;bp=1;var e=iA().Buffer,t=9007199254740991;function n(o){if(o<0||o>t||o%1!==0)throw new RangeError("value out of range")}function r(o,c,a){if(n(o),c||(c=e.allocUnsafe(s(o))),!e.isBuffer(c))throw new TypeError("buffer must be a Buffer instance");return a||(a=0),o<253?(c.writeUInt8(o,a),r.bytes=1):o<=65535?(c.writeUInt8(253,a),c.writeUInt16LE(o,a+1),r.bytes=3):o<=4294967295?(c.writeUInt8(254,a),c.writeUInt32LE(o,a+1),r.bytes=5):(c.writeUInt8(255,a),c.writeUInt32LE(o>>>0,a+1),c.writeUInt32LE(o/4294967296|0,a+5),r.bytes=9),c}function i(o,c){if(!e.isBuffer(o))throw new TypeError("buffer must be a Buffer instance");c||(c=0);var a=o.readUInt8(c);if(a<253)return i.bytes=1,a;if(a===253)return i.bytes=3,o.readUInt16LE(c+1);if(a===254)return i.bytes=5,o.readUInt32LE(c+1);i.bytes=9;var u=o.readUInt32LE(c+1),f=o.readUInt32LE(c+5),l=f*4294967296+u;return n(l),l}function s(o){return n(o),o<253?1:o<=65535?3:o<=4294967295?5:9}return Nf={encode:r,decode:i,encodingLength:s},Nf}var Ep;function _u(){if(Ep)return Pt;Ep=1,Object.defineProperty(Pt,"__esModule",{value:!0}),Pt.BufferReader=Pt.BufferWriter=Pt.cloneBuffer=Pt.reverseBuffer=Pt.writeUInt64LE=Pt.readUInt64LE=Pt.varuint=void 0;const e=zt(),{typeforce:t}=e,n=sA();Pt.varuint=n;function r(f,l){if(typeof f!="number")throw new Error("cannot write a non-number as a number");if(f<0)throw new Error("specified a negative value for writing an unsigned value");if(f>l)throw new Error("RangeError: value out of range");if(Math.floor(f)!==f)throw new Error("value has a fractional component")}function i(f,l){const d=f.readUInt32LE(l);let p=f.readUInt32LE(l+4);return p*=4294967296,r(p+d,9007199254740991),p+d}Pt.readUInt64LE=i;function s(f,l,d){return r(l,9007199254740991),f.writeInt32LE(l&-1,d),f.writeUInt32LE(Math.floor(l/4294967296),d+4),d+8}Pt.writeUInt64LE=s;function o(f){if(f.length<1)return f;let l=f.length-1,d=0;for(let p=0;p<f.length/2;p++)d=f[p],f[p]=f[l],f[l]=d,l--;return f}Pt.reverseBuffer=o;function c(f){const l=Buffer.allocUnsafe(f.length);return f.copy(l),l}Pt.cloneBuffer=c;class a{static withCapacity(l){return new a(Buffer.alloc(l))}constructor(l,d=0){this.buffer=l,this.offset=d,t(e.tuple(e.Buffer,e.UInt32),[l,d])}writeUInt8(l){this.offset=this.buffer.writeUInt8(l,this.offset)}writeInt32(l){this.offset=this.buffer.writeInt32LE(l,this.offset)}writeUInt32(l){this.offset=this.buffer.writeUInt32LE(l,this.offset)}writeUInt64(l){this.offset=s(this.buffer,l,this.offset)}writeVarInt(l){n.encode(l,this.buffer,this.offset),this.offset+=n.encode.bytes}writeSlice(l){if(this.buffer.length<this.offset+l.length)throw new Error("Cannot write slice out of bounds");this.offset+=l.copy(this.buffer,this.offset)}writeVarSlice(l){this.writeVarInt(l.length),this.writeSlice(l)}writeVector(l){this.writeVarInt(l.length),l.forEach(d=>this.writeVarSlice(d))}end(){if(this.buffer.length===this.offset)return this.buffer;throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`)}}Pt.BufferWriter=a;class u{constructor(l,d=0){this.buffer=l,this.offset=d,t(e.tuple(e.Buffer,e.UInt32),[l,d])}readUInt8(){const l=this.buffer.readUInt8(this.offset);return this.offset++,l}readInt32(){const l=this.buffer.readInt32LE(this.offset);return this.offset+=4,l}readUInt32(){const l=this.buffer.readUInt32LE(this.offset);return this.offset+=4,l}readUInt64(){const l=i(this.buffer,this.offset);return this.offset+=8,l}readVarInt(){const l=n.decode(this.buffer,this.offset);return this.offset+=n.decode.bytes,l}readSlice(l){if(this.buffer.length<this.offset+l)throw new Error("Cannot read slice out of bounds");const d=this.buffer.slice(this.offset,this.offset+l);return this.offset+=l,d}readVarSlice(){return this.readSlice(this.readVarInt())}readVector(){const l=this.readVarInt(),d=[];for(let p=0;p<l;p++)d.push(this.readVarSlice());return d}}return Pt.BufferReader=u,Pt}var _p;function xh(){return _p||(_p=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.tweakKey=e.tapTweakHash=e.tapleafHash=e.findScriptPath=e.toHashTree=e.rootHashFromPath=e.MAX_TAPTREE_DEPTH=e.LEAF_VERSION_TAPSCRIPT=void 0;const t=tc(),n=Th(),r=Ur(),i=_u(),s=zt();e.LEAF_VERSION_TAPSCRIPT=192,e.MAX_TAPTREE_DEPTH=128;const o=m=>"left"in m&&"right"in m;function c(m,E){if(m.length<33)throw new TypeError(`The control-block length is too small. Got ${m.length}, expected min 33.`);const _=(m.length-33)/32;let S=E;for(let w=0;w<_;w++){const T=m.slice(33+32*w,65+32*w);S.compare(T)<0?S=p(S,T):S=p(T,S)}return S}e.rootHashFromPath=c;function a(m){if((0,s.isTapleaf)(m))return{hash:f(m)};const E=[a(m[0]),a(m[1])];E.sort((w,T)=>w.hash.compare(T.hash));const[_,S]=E;return{hash:p(_.hash,S.hash),left:_,right:S}}e.toHashTree=a;function u(m,E){if(o(m)){const _=u(m.left,E);if(_!==void 0)return[..._,m.right.hash];const S=u(m.right,E);if(S!==void 0)return[...S,m.left.hash]}else if(m.hash.equals(E))return[]}e.findScriptPath=u;function f(m){const E=m.version||e.LEAF_VERSION_TAPSCRIPT;return r.taggedHash("TapLeaf",t.Buffer.concat([t.Buffer.from([E]),g(m.output)]))}e.tapleafHash=f;function l(m,E){return r.taggedHash("TapTweak",t.Buffer.concat(E?[m,E]:[m]))}e.tapTweakHash=l;function d(m,E){if(!t.Buffer.isBuffer(m)||m.length!==32||E&&E.length!==32)return null;const _=l(m,E),S=(0,n.getEccLib)().xOnlyPointAddTweak(m,_);return!S||S.xOnlyPubkey===null?null:{parity:S.parity,x:t.Buffer.from(S.xOnlyPubkey)}}e.tweakKey=d;function p(m,E){return r.taggedHash("TapBranch",t.Buffer.concat([m,E]))}function g(m){const E=i.varuint.encodingLength(m.length),_=t.Buffer.allocUnsafe(E);return i.varuint.encode(m.length,_),t.Buffer.concat([_,m])}})(Bf)),Bf}var Sp;function oA(){if(Sp)return po;Sp=1,Object.defineProperty(po,"__esModule",{value:!0}),po.p2tr=void 0;const e=tc(),t=Xn(),n=rn(),r=zt(),i=Th(),s=xh(),o=gi(),c=ac(),a=Ah(),u=n.OPS,f=1,l=80;function d(p,g){if(!p.address&&!p.output&&!p.pubkey&&!p.internalPubkey&&!(p.witness&&p.witness.length>1))throw new TypeError("Not enough data");g=Object.assign({validate:!0},g||{}),(0,r.typeforce)({address:r.typeforce.maybe(r.typeforce.String),input:r.typeforce.maybe(r.typeforce.BufferN(0)),network:r.typeforce.maybe(r.typeforce.Object),output:r.typeforce.maybe(r.typeforce.BufferN(34)),internalPubkey:r.typeforce.maybe(r.typeforce.BufferN(32)),hash:r.typeforce.maybe(r.typeforce.BufferN(32)),pubkey:r.typeforce.maybe(r.typeforce.BufferN(32)),signature:r.typeforce.maybe(r.typeforce.anyOf(r.typeforce.BufferN(64),r.typeforce.BufferN(65))),witness:r.typeforce.maybe(r.typeforce.arrayOf(r.typeforce.Buffer)),scriptTree:r.typeforce.maybe(r.isTaptree),redeem:r.typeforce.maybe({output:r.typeforce.maybe(r.typeforce.Buffer),redeemVersion:r.typeforce.maybe(r.typeforce.Number),witness:r.typeforce.maybe(r.typeforce.arrayOf(r.typeforce.Buffer))}),redeemVersion:r.typeforce.maybe(r.typeforce.Number)},p);const m=o.value(()=>(0,a.fromBech32)(p.address)),E=o.value(()=>{if(!(!p.witness||!p.witness.length))return p.witness.length>=2&&p.witness[p.witness.length-1][0]===l?p.witness.slice(0,-1):p.witness.slice()}),_=o.value(()=>{if(p.scriptTree)return(0,s.toHashTree)(p.scriptTree);if(p.hash)return{hash:p.hash}}),S=p.network||t.bitcoin,w={name:"p2tr",network:S};if(o.prop(w,"address",()=>{if(!w.pubkey)return;const T=c.bech32m.toWords(w.pubkey);return T.unshift(f),c.bech32m.encode(S.bech32,T)}),o.prop(w,"hash",()=>{const T=_();if(T)return T.hash;const O=E();if(O&&O.length>1){const A=O[O.length-1],R=A[0]&r.TAPLEAF_VERSION_MASK,M=O[O.length-2],v=(0,s.tapleafHash)({output:M,version:R});return(0,s.rootHashFromPath)(A,v)}return null}),o.prop(w,"output",()=>{if(w.pubkey)return n.compile([u.OP_1,w.pubkey])}),o.prop(w,"redeemVersion",()=>p.redeemVersion?p.redeemVersion:p.redeem&&p.redeem.redeemVersion!==void 0&&p.redeem.redeemVersion!==null?p.redeem.redeemVersion:s.LEAF_VERSION_TAPSCRIPT),o.prop(w,"redeem",()=>{const T=E();if(!(!T||T.length<2))return{output:T[T.length-2],witness:T.slice(0,-2),redeemVersion:T[T.length-1][0]&r.TAPLEAF_VERSION_MASK}}),o.prop(w,"pubkey",()=>{if(p.pubkey)return p.pubkey;if(p.output)return p.output.slice(2);if(p.address)return m().data;if(w.internalPubkey){const T=(0,s.tweakKey)(w.internalPubkey,w.hash);if(T)return T.x}}),o.prop(w,"internalPubkey",()=>{if(p.internalPubkey)return p.internalPubkey;const T=E();if(T&&T.length>1)return T[T.length-1].slice(1,33)}),o.prop(w,"signature",()=>{if(p.signature)return p.signature;const T=E();if(!(!T||T.length!==1))return T[0]}),o.prop(w,"witness",()=>{if(p.witness)return p.witness;const T=_();if(T&&p.redeem&&p.redeem.output&&p.internalPubkey){const O=(0,s.tapleafHash)({output:p.redeem.output,version:w.redeemVersion}),A=(0,s.findScriptPath)(T,O);if(!A)return;const R=(0,s.tweakKey)(p.internalPubkey,T.hash);if(!R)return;const M=e.Buffer.concat([e.Buffer.from([w.redeemVersion|R.parity]),p.internalPubkey].concat(A));return[p.redeem.output,M]}if(p.signature)return[p.signature]}),g.validate){let T=e.Buffer.from([]);if(p.address){if(S&&S.bech32!==m().prefix)throw new TypeError("Invalid prefix or Network mismatch");if(m().version!==f)throw new TypeError("Invalid address version");if(m().data.length!==32)throw new TypeError("Invalid address data");T=m().data}if(p.pubkey){if(T.length>0&&!T.equals(p.pubkey))throw new TypeError("Pubkey mismatch");T=p.pubkey}if(p.output){if(p.output.length!==34||p.output[0]!==u.OP_1||p.output[1]!==32)throw new TypeError("Output is invalid");if(T.length>0&&!T.equals(p.output.slice(2)))throw new TypeError("Pubkey mismatch");T=p.output.slice(2)}if(p.internalPubkey){const R=(0,s.tweakKey)(p.internalPubkey,w.hash);if(T.length>0&&!T.equals(R.x))throw new TypeError("Pubkey mismatch");T=R.x}if(T&&T.length&&!(0,i.getEccLib)().isXOnlyPoint(T))throw new TypeError("Invalid pubkey for p2tr");const O=_();if(p.hash&&O&&!p.hash.equals(O.hash))throw new TypeError("Hash mismatch");if(p.redeem&&p.redeem.output&&O){const R=(0,s.tapleafHash)({output:p.redeem.output,version:w.redeemVersion});if(!(0,s.findScriptPath)(O,R))throw new TypeError("Redeem script not in tree")}const A=E();if(p.redeem&&w.redeem){if(p.redeem.redeemVersion&&p.redeem.redeemVersion!==w.redeem.redeemVersion)throw new TypeError("Redeem.redeemVersion and witness mismatch");if(p.redeem.output){if(n.decompile(p.redeem.output).length===0)throw new TypeError("Redeem.output is invalid");if(w.redeem.output&&!p.redeem.output.equals(w.redeem.output))throw new TypeError("Redeem.output and witness mismatch")}if(p.redeem.witness&&w.redeem.witness&&!(0,r.stacksEqual)(p.redeem.witness,w.redeem.witness))throw new TypeError("Redeem.witness and witness mismatch")}if(A&&A.length)if(A.length===1){if(p.signature&&!p.signature.equals(A[0]))throw new TypeError("Signature mismatch")}else{const R=A[A.length-1];if(R.length<33)throw new TypeError(`The control-block length is too small. Got ${R.length}, expected min 33.`);if((R.length-33)%32!==0)throw new TypeError(`The control-block length of ${R.length} is incorrect!`);const M=(R.length-33)/32;if(M>128)throw new TypeError(`The script path is too long. Got ${M}, expected max 128.`);const v=R.slice(1,33);if(p.internalPubkey&&!p.internalPubkey.equals(v))throw new TypeError("Internal pubkey mismatch");if(!(0,i.getEccLib)().isXOnlyPoint(v))throw new TypeError("Invalid internalPubkey for p2tr witness");const U=R[0]&r.TAPLEAF_VERSION_MASK,L=A[A.length-2],K=(0,s.tapleafHash)({output:L,version:U}),H=(0,s.rootHashFromPath)(R,K),$=(0,s.tweakKey)(v,H);if(!$)throw new TypeError("Invalid outputKey for p2tr witness");if(T.length&&!T.equals($.x))throw new TypeError("Pubkey mismatch for p2tr witness");if($.parity!==(R[0]&1))throw new Error("Incorrect parity")}}return Object.assign(w,p)}return po.p2tr=d,po}var Tp;function Ec(){return Tp||(Tp=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.p2tr=e.p2wsh=e.p2wpkh=e.p2sh=e.p2pkh=e.p2pk=e.p2ms=e.embed=void 0;const t=G8();Object.defineProperty(e,"embed",{enumerable:!0,get:function(){return t.p2data}});const n=W8();Object.defineProperty(e,"p2ms",{enumerable:!0,get:function(){return n.p2ms}});const r=X8();Object.defineProperty(e,"p2pk",{enumerable:!0,get:function(){return r.p2pk}});const i=eA();Object.defineProperty(e,"p2pkh",{enumerable:!0,get:function(){return i.p2pkh}});const s=tA();Object.defineProperty(e,"p2sh",{enumerable:!0,get:function(){return s.p2sh}});const o=nA();Object.defineProperty(e,"p2wpkh",{enumerable:!0,get:function(){return o.p2wpkh}});const c=rA();Object.defineProperty(e,"p2wsh",{enumerable:!0,get:function(){return c.p2wsh}});const a=oA();Object.defineProperty(e,"p2tr",{enumerable:!0,get:function(){return a.p2tr}})})(mf)),mf}var xp;function Ah(){if(xp)return Zt;xp=1,Object.defineProperty(Zt,"__esModule",{value:!0}),Zt.toOutputScript=Zt.fromOutputScript=Zt.toBech32=Zt.toBase58Check=Zt.fromBech32=Zt.fromBase58Check=void 0;const e=Xn(),t=Ec(),n=rn(),r=zt(),i=ac(),s=Sh(),o=40,c=2,a=16,u=2,f=80,l="WARNING: Sending to a future segwit version address can lead to loss of funds. End users MUST be warned carefully in the GUI and asked if they wish to proceed with caution. Wallets should verify the segwit version from the output of fromBech32, then decide when it is safe to use which version of segwit.";function d(w,T){const O=w.slice(2);if(O.length<c||O.length>o)throw new TypeError("Invalid program length for segwit address");const A=w[0]-f;if(A<u||A>a)throw new TypeError("Invalid version for segwit address");if(w[1]!==O.length)throw new TypeError("Invalid script for segwit address");return console.warn(l),E(O,A,T.bech32)}function p(w){const T=Buffer.from(s.decode(w));if(T.length<21)throw new TypeError(w+" is too short");if(T.length>21)throw new TypeError(w+" is too long");const O=T.readUInt8(0),A=T.slice(1);return{version:O,hash:A}}Zt.fromBase58Check=p;function g(w){let T,O;try{T=i.bech32.decode(w)}catch{}if(T){if(O=T.words[0],O!==0)throw new TypeError(w+" uses wrong encoding")}else if(T=i.bech32m.decode(w),O=T.words[0],O===0)throw new TypeError(w+" uses wrong encoding");const A=i.bech32.fromWords(T.words.slice(1));return{version:O,prefix:T.prefix,data:Buffer.from(A)}}Zt.fromBech32=g;function m(w,T){(0,r.typeforce)((0,r.tuple)(r.Hash160bit,r.UInt8),arguments);const O=Buffer.allocUnsafe(21);return O.writeUInt8(T,0),w.copy(O,1),s.encode(O)}Zt.toBase58Check=m;function E(w,T,O){const A=i.bech32.toWords(w);return A.unshift(T),T===0?i.bech32.encode(O,A):i.bech32m.encode(O,A)}Zt.toBech32=E;function _(w,T){T=T||e.bitcoin;try{return t.p2pkh({output:w,network:T}).address}catch{}try{return t.p2sh({output:w,network:T}).address}catch{}try{return t.p2wpkh({output:w,network:T}).address}catch{}try{return t.p2wsh({output:w,network:T}).address}catch{}try{return t.p2tr({output:w,network:T}).address}catch{}try{return d(w,T)}catch{}throw new Error(n.toASM(w)+" has no matching Address")}Zt.fromOutputScript=_;function S(w,T){T=T||e.bitcoin;let O,A;try{O=p(w)}catch{}if(O){if(O.version===T.pubKeyHash)return t.p2pkh({hash:O.hash}).output;if(O.version===T.scriptHash)return t.p2sh({hash:O.hash}).output}else{try{A=g(w)}catch{}if(A){if(A.prefix!==T.bech32)throw new Error(w+" has an invalid prefix");if(A.version===0){if(A.data.length===20)return t.p2wpkh({hash:A.data}).output;if(A.data.length===32)return t.p2wsh({hash:A.data}).output}else if(A.version===1){if(A.data.length===32)return t.p2tr({pubkey:A.data}).output}else if(A.version>=u&&A.version<=a&&A.data.length>=c&&A.data.length<=o)return console.warn(l),n.compile([A.version+f,A.data])}}throw new Error(w+" has no matching Script")}return Zt.toOutputScript=S,Zt}var yo={},go={},Ap;function cA(){if(Ap)return go;Ap=1,Object.defineProperty(go,"__esModule",{value:!0}),go.fastMerkleRoot=void 0;function e(t,n){if(!Array.isArray(t))throw TypeError("Expected values Array");if(typeof n!="function")throw TypeError("Expected digest Function");let r=t.length;const i=t.concat();for(;r>1;){let s=0;for(let o=0;o<r;o+=2,++s){const c=i[o],a=o+1===r?c:i[o+1],u=Buffer.concat([c,a]);i[s]=n(u)}r=s}return i[0]}return go.fastMerkleRoot=e,go}var mo={},vp;function _c(){if(vp)return mo;vp=1,Object.defineProperty(mo,"__esModule",{value:!0}),mo.Transaction=void 0;const e=_u(),t=Ur(),n=rn(),r=rn(),i=zt(),{typeforce:s}=i;function o(E){const _=E.length;return e.varuint.encodingLength(_)+_}function c(E){const _=E.length;return e.varuint.encodingLength(_)+E.reduce((S,w)=>S+o(w),0)}const a=Buffer.allocUnsafe(0),u=[],f=Buffer.from("0000000000000000000000000000000000000000000000000000000000000000","hex"),l=Buffer.from("0000000000000000000000000000000000000000000000000000000000000001","hex"),d=Buffer.from("ffffffffffffffff","hex"),p={script:a,valueBuffer:d};function g(E){return E.value!==void 0}class m{constructor(){this.version=1,this.locktime=0,this.ins=[],this.outs=[]}static fromBuffer(_,S){const w=new e.BufferReader(_),T=new m;T.version=w.readInt32();const O=w.readUInt8(),A=w.readUInt8();let R=!1;O===m.ADVANCED_TRANSACTION_MARKER&&A===m.ADVANCED_TRANSACTION_FLAG?R=!0:w.offset-=2;const M=w.readVarInt();for(let U=0;U<M;++U)T.ins.push({hash:w.readSlice(32),index:w.readUInt32(),script:w.readVarSlice(),sequence:w.readUInt32(),witness:u});const v=w.readVarInt();for(let U=0;U<v;++U)T.outs.push({value:w.readUInt64(),script:w.readVarSlice()});if(R){for(let U=0;U<M;++U)T.ins[U].witness=w.readVector();if(!T.hasWitnesses())throw new Error("Transaction has superfluous witness data")}if(T.locktime=w.readUInt32(),S)return T;if(w.offset!==_.length)throw new Error("Transaction has unexpected data");return T}static fromHex(_){return m.fromBuffer(Buffer.from(_,"hex"),!1)}static isCoinbaseHash(_){s(i.Hash256bit,_);for(let S=0;S<32;++S)if(_[S]!==0)return!1;return!0}isCoinbase(){return this.ins.length===1&&m.isCoinbaseHash(this.ins[0].hash)}addInput(_,S,w,T){return s(i.tuple(i.Hash256bit,i.UInt32,i.maybe(i.UInt32),i.maybe(i.Buffer)),arguments),i.Null(w)&&(w=m.DEFAULT_SEQUENCE),this.ins.push({hash:_,index:S,script:T||a,sequence:w,witness:u})-1}addOutput(_,S){return s(i.tuple(i.Buffer,i.Satoshi),arguments),this.outs.push({script:_,value:S})-1}hasWitnesses(){return this.ins.some(_=>_.witness.length!==0)}stripWitnesses(){this.ins.forEach(_=>{_.witness=u})}weight(){const _=this.byteLength(!1),S=this.byteLength(!0);return _*3+S}virtualSize(){return Math.ceil(this.weight()/4)}byteLength(_=!0){const S=_&&this.hasWitnesses();return(S?10:8)+e.varuint.encodingLength(this.ins.length)+e.varuint.encodingLength(this.outs.length)+this.ins.reduce((w,T)=>w+40+o(T.script),0)+this.outs.reduce((w,T)=>w+8+o(T.script),0)+(S?this.ins.reduce((w,T)=>w+c(T.witness),0):0)}clone(){const _=new m;return _.version=this.version,_.locktime=this.locktime,_.ins=this.ins.map(S=>({hash:S.hash,index:S.index,script:S.script,sequence:S.sequence,witness:S.witness})),_.outs=this.outs.map(S=>({script:S.script,value:S.value})),_}hashForSignature(_,S,w){if(s(i.tuple(i.UInt32,i.Buffer,i.Number),arguments),_>=this.ins.length)return l;const T=n.compile(n.decompile(S).filter(R=>R!==r.OPS.OP_CODESEPARATOR)),O=this.clone();if((w&31)===m.SIGHASH_NONE)O.outs=[],O.ins.forEach((R,M)=>{M!==_&&(R.sequence=0)});else if((w&31)===m.SIGHASH_SINGLE){if(_>=this.outs.length)return l;O.outs.length=_+1;for(let R=0;R<_;R++)O.outs[R]=p;O.ins.forEach((R,M)=>{M!==_&&(R.sequence=0)})}w&m.SIGHASH_ANYONECANPAY?(O.ins=[O.ins[_]],O.ins[0].script=T):(O.ins.forEach(R=>{R.script=a}),O.ins[_].script=T);const A=Buffer.allocUnsafe(O.byteLength(!1)+4);return A.writeInt32LE(w,A.length-4),O.__toBuffer(A,0,!1),t.hash256(A)}hashForWitnessV1(_,S,w,T,O,A){if(s(i.tuple(i.UInt32,s.arrayOf(i.Buffer),s.arrayOf(i.Satoshi),i.UInt32),arguments),w.length!==this.ins.length||S.length!==this.ins.length)throw new Error("Must supply prevout script and value for all inputs");const R=T===m.SIGHASH_DEFAULT?m.SIGHASH_ALL:T&m.SIGHASH_OUTPUT_MASK,v=(T&m.SIGHASH_INPUT_MASK)===m.SIGHASH_ANYONECANPAY,U=R===m.SIGHASH_NONE,L=R===m.SIGHASH_SINGLE;let K=a,H=a,$=a,P=a,D=a;if(!v){let q=e.BufferWriter.withCapacity(36*this.ins.length);this.ins.forEach(N=>{q.writeSlice(N.hash),q.writeUInt32(N.index)}),K=t.sha256(q.end()),q=e.BufferWriter.withCapacity(8*this.ins.length),w.forEach(N=>q.writeUInt64(N)),H=t.sha256(q.end()),q=e.BufferWriter.withCapacity(S.map(o).reduce((N,V)=>N+V)),S.forEach(N=>q.writeVarSlice(N)),$=t.sha256(q.end()),q=e.BufferWriter.withCapacity(4*this.ins.length),this.ins.forEach(N=>q.writeUInt32(N.sequence)),P=t.sha256(q.end())}if(U||L){if(L&&_<this.outs.length){const q=this.outs[_],N=e.BufferWriter.withCapacity(8+o(q.script));N.writeUInt64(q.value),N.writeVarSlice(q.script),D=t.sha256(N.end())}}else{const q=this.outs.map(V=>8+o(V.script)).reduce((V,G)=>V+G),N=e.BufferWriter.withCapacity(q);this.outs.forEach(V=>{N.writeUInt64(V.value),N.writeVarSlice(V.script)}),D=t.sha256(N.end())}const F=(O?2:0)+(A?1:0),x=174-(v?49:0)-(U?32:0)+(A?32:0)+(O?37:0),B=e.BufferWriter.withCapacity(x);if(B.writeUInt8(T),B.writeInt32(this.version),B.writeUInt32(this.locktime),B.writeSlice(K),B.writeSlice(H),B.writeSlice($),B.writeSlice(P),U||L||B.writeSlice(D),B.writeUInt8(F),v){const q=this.ins[_];B.writeSlice(q.hash),B.writeUInt32(q.index),B.writeUInt64(w[_]),B.writeVarSlice(S[_]),B.writeUInt32(q.sequence)}else B.writeUInt32(_);if(A){const q=e.BufferWriter.withCapacity(o(A));q.writeVarSlice(A),B.writeSlice(t.sha256(q.end()))}return L&&B.writeSlice(D),O&&(B.writeSlice(O),B.writeUInt8(0),B.writeUInt32(4294967295)),t.taggedHash("TapSighash",Buffer.concat([Buffer.from([0]),B.end()]))}hashForWitnessV0(_,S,w,T){s(i.tuple(i.UInt32,i.Buffer,i.Satoshi,i.UInt32),arguments);let O=Buffer.from([]),A,R=f,M=f,v=f;if(T&m.SIGHASH_ANYONECANPAY||(O=Buffer.allocUnsafe(36*this.ins.length),A=new e.BufferWriter(O,0),this.ins.forEach(L=>{A.writeSlice(L.hash),A.writeUInt32(L.index)}),M=t.hash256(O)),!(T&m.SIGHASH_ANYONECANPAY)&&(T&31)!==m.SIGHASH_SINGLE&&(T&31)!==m.SIGHASH_NONE&&(O=Buffer.allocUnsafe(4*this.ins.length),A=new e.BufferWriter(O,0),this.ins.forEach(L=>{A.writeUInt32(L.sequence)}),v=t.hash256(O)),(T&31)!==m.SIGHASH_SINGLE&&(T&31)!==m.SIGHASH_NONE){const L=this.outs.reduce((K,H)=>K+8+o(H.script),0);O=Buffer.allocUnsafe(L),A=new e.BufferWriter(O,0),this.outs.forEach(K=>{A.writeUInt64(K.value),A.writeVarSlice(K.script)}),R=t.hash256(O)}else if((T&31)===m.SIGHASH_SINGLE&&_<this.outs.length){const L=this.outs[_];O=Buffer.allocUnsafe(8+o(L.script)),A=new e.BufferWriter(O,0),A.writeUInt64(L.value),A.writeVarSlice(L.script),R=t.hash256(O)}O=Buffer.allocUnsafe(156+o(S)),A=new e.BufferWriter(O,0);const U=this.ins[_];return A.writeInt32(this.version),A.writeSlice(M),A.writeSlice(v),A.writeSlice(U.hash),A.writeUInt32(U.index),A.writeVarSlice(S),A.writeUInt64(w),A.writeUInt32(U.sequence),A.writeSlice(R),A.writeUInt32(this.locktime),A.writeUInt32(T),t.hash256(O)}getHash(_){return _&&this.isCoinbase()?Buffer.alloc(32,0):t.hash256(this.__toBuffer(void 0,void 0,_))}getId(){return(0,e.reverseBuffer)(this.getHash(!1)).toString("hex")}toBuffer(_,S){return this.__toBuffer(_,S,!0)}toHex(){return this.toBuffer(void 0,void 0).toString("hex")}setInputScript(_,S){s(i.tuple(i.Number,i.Buffer),arguments),this.ins[_].script=S}setWitness(_,S){s(i.tuple(i.Number,[i.Buffer]),arguments),this.ins[_].witness=S}__toBuffer(_,S,w=!1){_||(_=Buffer.allocUnsafe(this.byteLength(w)));const T=new e.BufferWriter(_,S||0);T.writeInt32(this.version);const O=w&&this.hasWitnesses();return O&&(T.writeUInt8(m.ADVANCED_TRANSACTION_MARKER),T.writeUInt8(m.ADVANCED_TRANSACTION_FLAG)),T.writeVarInt(this.ins.length),this.ins.forEach(A=>{T.writeSlice(A.hash),T.writeUInt32(A.index),T.writeVarSlice(A.script),T.writeUInt32(A.sequence)}),T.writeVarInt(this.outs.length),this.outs.forEach(A=>{g(A)?T.writeUInt64(A.value):T.writeSlice(A.valueBuffer),T.writeVarSlice(A.script)}),O&&this.ins.forEach(A=>{T.writeVector(A.witness)}),T.writeUInt32(this.locktime),S!==void 0?_.slice(S,T.offset):_}}return mo.Transaction=m,m.DEFAULT_SEQUENCE=4294967295,m.SIGHASH_DEFAULT=0,m.SIGHASH_ALL=1,m.SIGHASH_NONE=2,m.SIGHASH_SINGLE=3,m.SIGHASH_ANYONECANPAY=128,m.SIGHASH_OUTPUT_MASK=3,m.SIGHASH_INPUT_MASK=128,m.ADVANCED_TRANSACTION_MARKER=0,m.ADVANCED_TRANSACTION_FLAG=1,mo}var Ip;function aA(){if(Ip)return yo;Ip=1,Object.defineProperty(yo,"__esModule",{value:!0}),yo.Block=void 0;const e=_u(),t=Ur(),n=cA(),r=_c(),i=zt(),{typeforce:s}=i,o=new TypeError("Cannot compute merkle root for zero transactions"),c=new TypeError("Cannot compute witness commit for non-segwit block");class a{constructor(){this.version=1,this.prevHash=void 0,this.merkleRoot=void 0,this.timestamp=0,this.witnessCommit=void 0,this.bits=0,this.nonce=0,this.transactions=void 0}static fromBuffer(d){if(d.length<80)throw new Error("Buffer too small (< 80 bytes)");const p=new e.BufferReader(d),g=new a;if(g.version=p.readInt32(),g.prevHash=p.readSlice(32),g.merkleRoot=p.readSlice(32),g.timestamp=p.readUInt32(),g.bits=p.readUInt32(),g.nonce=p.readUInt32(),d.length===80)return g;const m=()=>{const S=r.Transaction.fromBuffer(p.buffer.slice(p.offset),!0);return p.offset+=S.byteLength(),S},E=p.readVarInt();g.transactions=[];for(let S=0;S<E;++S){const w=m();g.transactions.push(w)}const _=g.getWitnessCommit();return _&&(g.witnessCommit=_),g}static fromHex(d){return a.fromBuffer(Buffer.from(d,"hex"))}static calculateTarget(d){const p=((d&4278190080)>>24)-3,g=d&8388607,m=Buffer.alloc(32,0);return m.writeUIntBE(g,29-p,3),m}static calculateMerkleRoot(d,p){if(s([{getHash:i.Function}],d),d.length===0)throw o;if(p&&!u(d))throw c;const g=d.map(E=>E.getHash(p)),m=(0,n.fastMerkleRoot)(g,t.hash256);return p?t.hash256(Buffer.concat([m,d[0].ins[0].witness[0]])):m}getWitnessCommit(){if(!u(this.transactions))return null;const d=this.transactions[0].outs.filter(g=>g.script.slice(0,6).equals(Buffer.from("6a24aa21a9ed","hex"))).map(g=>g.script.slice(6,38));if(d.length===0)return null;const p=d[d.length-1];return p instanceof Buffer&&p.length===32?p:null}hasWitnessCommit(){return this.witnessCommit instanceof Buffer&&this.witnessCommit.length===32||this.getWitnessCommit()!==null}hasWitness(){return f(this.transactions)}weight(){const d=this.byteLength(!1,!1),p=this.byteLength(!1,!0);return d*3+p}byteLength(d,p=!0){return d||!this.transactions?80:80+e.varuint.encodingLength(this.transactions.length)+this.transactions.reduce((g,m)=>g+m.byteLength(p),0)}getHash(){return t.hash256(this.toBuffer(!0))}getId(){return(0,e.reverseBuffer)(this.getHash()).toString("hex")}getUTCDate(){const d=new Date(0);return d.setUTCSeconds(this.timestamp),d}toBuffer(d){const p=Buffer.allocUnsafe(this.byteLength(d)),g=new e.BufferWriter(p);return g.writeInt32(this.version),g.writeSlice(this.prevHash),g.writeSlice(this.merkleRoot),g.writeUInt32(this.timestamp),g.writeUInt32(this.bits),g.writeUInt32(this.nonce),d||!this.transactions||(e.varuint.encode(this.transactions.length,p,g.offset),g.offset+=e.varuint.encode.bytes,this.transactions.forEach(m=>{const E=m.byteLength();m.toBuffer(p,g.offset),g.offset+=E})),p}toHex(d){return this.toBuffer(d).toString("hex")}checkTxRoots(){const d=this.hasWitnessCommit();return!d&&this.hasWitness()?!1:this.__checkMerkleRoot()&&(d?this.__checkWitnessCommit():!0)}checkProofOfWork(){const d=(0,e.reverseBuffer)(this.getHash()),p=a.calculateTarget(this.bits);return d.compare(p)<=0}__checkMerkleRoot(){if(!this.transactions)throw o;const d=a.calculateMerkleRoot(this.transactions);return this.merkleRoot.compare(d)===0}__checkWitnessCommit(){if(!this.transactions)throw o;if(!this.hasWitnessCommit())throw c;const d=a.calculateMerkleRoot(this.transactions,!0);return this.witnessCommit.compare(d)===0}}yo.Block=a;function u(l){return l instanceof Array&&l[0]&&l[0].ins&&l[0].ins instanceof Array&&l[0].ins[0]&&l[0].ins[0].witness&&l[0].ins[0].witness instanceof Array&&l[0].ins[0].witness.length>0}function f(l){return l instanceof Array&&l.some(d=>typeof d=="object"&&d.ins instanceof Array&&d.ins.some(p=>typeof p=="object"&&p.witness instanceof Array&&p.witness.length>0))}return yo}var wo={},Mc={},qc={},kf={},as={},us={},Pf={},Op;function qt(){return Op||(Op=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),(function(t){t[t.UNSIGNED_TX=0]="UNSIGNED_TX",t[t.GLOBAL_XPUB=1]="GLOBAL_XPUB"})(e.GlobalTypes||(e.GlobalTypes={})),e.GLOBAL_TYPE_NAMES=["unsignedTx","globalXpub"],(function(t){t[t.NON_WITNESS_UTXO=0]="NON_WITNESS_UTXO",t[t.WITNESS_UTXO=1]="WITNESS_UTXO",t[t.PARTIAL_SIG=2]="PARTIAL_SIG",t[t.SIGHASH_TYPE=3]="SIGHASH_TYPE",t[t.REDEEM_SCRIPT=4]="REDEEM_SCRIPT",t[t.WITNESS_SCRIPT=5]="WITNESS_SCRIPT",t[t.BIP32_DERIVATION=6]="BIP32_DERIVATION",t[t.FINAL_SCRIPTSIG=7]="FINAL_SCRIPTSIG",t[t.FINAL_SCRIPTWITNESS=8]="FINAL_SCRIPTWITNESS",t[t.POR_COMMITMENT=9]="POR_COMMITMENT",t[t.TAP_KEY_SIG=19]="TAP_KEY_SIG",t[t.TAP_SCRIPT_SIG=20]="TAP_SCRIPT_SIG",t[t.TAP_LEAF_SCRIPT=21]="TAP_LEAF_SCRIPT",t[t.TAP_BIP32_DERIVATION=22]="TAP_BIP32_DERIVATION",t[t.TAP_INTERNAL_KEY=23]="TAP_INTERNAL_KEY",t[t.TAP_MERKLE_ROOT=24]="TAP_MERKLE_ROOT"})(e.InputTypes||(e.InputTypes={})),e.INPUT_TYPE_NAMES=["nonWitnessUtxo","witnessUtxo","partialSig","sighashType","redeemScript","witnessScript","bip32Derivation","finalScriptSig","finalScriptWitness","porCommitment","tapKeySig","tapScriptSig","tapLeafScript","tapBip32Derivation","tapInternalKey","tapMerkleRoot"],(function(t){t[t.REDEEM_SCRIPT=0]="REDEEM_SCRIPT",t[t.WITNESS_SCRIPT=1]="WITNESS_SCRIPT",t[t.BIP32_DERIVATION=2]="BIP32_DERIVATION",t[t.TAP_INTERNAL_KEY=5]="TAP_INTERNAL_KEY",t[t.TAP_TREE=6]="TAP_TREE",t[t.TAP_BIP32_DERIVATION=7]="TAP_BIP32_DERIVATION"})(e.OutputTypes||(e.OutputTypes={})),e.OUTPUT_TYPE_NAMES=["redeemScript","witnessScript","bip32Derivation","tapInternalKey","tapTree","tapBip32Derivation"]})(Pf)),Pf}var $r={},Bp;function uA(){if(Bp)return $r;Bp=1,Object.defineProperty($r,"__esModule",{value:!0});const e=qt(),t=o=>[...Array(o).keys()];function n(o){if(o.key[0]!==e.GlobalTypes.GLOBAL_XPUB)throw new Error("Decode Error: could not decode globalXpub with key 0x"+o.key.toString("hex"));if(o.key.length!==79||![2,3].includes(o.key[46]))throw new Error("Decode Error: globalXpub has invalid extended pubkey in key 0x"+o.key.toString("hex"));if(o.value.length/4%1!==0)throw new Error("Decode Error: Global GLOBAL_XPUB value length should be multiple of 4");const c=o.key.slice(1),a={masterFingerprint:o.value.slice(0,4),extendedPubkey:c,path:"m"};for(const u of t(o.value.length/4-1)){const f=o.value.readUInt32LE(u*4+4),l=!!(f&2147483648),d=f&2147483647;a.path+="/"+d.toString(10)+(l?"'":"")}return a}$r.decode=n;function r(o){const c=Buffer.from([e.GlobalTypes.GLOBAL_XPUB]),a=Buffer.concat([c,o.extendedPubkey]),u=o.path.split("/"),f=Buffer.allocUnsafe(u.length*4);o.masterFingerprint.copy(f,0);let l=4;return u.slice(1).forEach(d=>{const p=d.slice(-1)==="'";let g=2147483647&parseInt(p?d.slice(0,-1):d,10);p&&(g+=2147483648),f.writeUInt32LE(g,l),l+=4}),{key:a,value:f}}$r.encode=r,$r.expected="{ masterFingerprint: Buffer; extendedPubkey: Buffer; path: string; }";function i(o){const c=o.extendedPubkey,a=o.masterFingerprint,u=o.path;return Buffer.isBuffer(c)&&c.length===78&&[2,3].indexOf(c[45])>-1&&Buffer.isBuffer(a)&&a.length===4&&typeof u=="string"&&!!u.match(/^m(\/\d+'?)*$/)}$r.check=i;function s(o,c,a){const u=c.extendedPubkey.toString("hex");return a.has(u)?!1:(a.add(u),o.filter(f=>f.extendedPubkey.equals(c.extendedPubkey)).length===0)}return $r.canAddToArray=s,$r}var Lc={},Np;function fA(){if(Np)return Lc;Np=1,Object.defineProperty(Lc,"__esModule",{value:!0});const e=qt();function t(n){return{key:Buffer.from([e.GlobalTypes.UNSIGNED_TX]),value:n.toBuffer()}}return Lc.encode=t,Lc}var Dr={},kp;function lA(){if(kp)return Dr;kp=1,Object.defineProperty(Dr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.FINAL_SCRIPTSIG)throw new Error("Decode Error: could not decode finalScriptSig with key 0x"+s.key.toString("hex"));return s.value}Dr.decode=t;function n(s){return{key:Buffer.from([e.InputTypes.FINAL_SCRIPTSIG]),value:s}}Dr.encode=n,Dr.expected="Buffer";function r(s){return Buffer.isBuffer(s)}Dr.check=r;function i(s,o){return!!s&&!!o&&s.finalScriptSig===void 0}return Dr.canAdd=i,Dr}var jr={},Pp;function hA(){if(Pp)return jr;Pp=1,Object.defineProperty(jr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.FINAL_SCRIPTWITNESS)throw new Error("Decode Error: could not decode finalScriptWitness with key 0x"+s.key.toString("hex"));return s.value}jr.decode=t;function n(s){return{key:Buffer.from([e.InputTypes.FINAL_SCRIPTWITNESS]),value:s}}jr.encode=n,jr.expected="Buffer";function r(s){return Buffer.isBuffer(s)}jr.check=r;function i(s,o){return!!s&&!!o&&s.finalScriptWitness===void 0}return jr.canAdd=i,jr}var Kr={},Rp;function dA(){if(Rp)return Kr;Rp=1,Object.defineProperty(Kr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.NON_WITNESS_UTXO)throw new Error("Decode Error: could not decode nonWitnessUtxo with key 0x"+s.key.toString("hex"));return s.value}Kr.decode=t;function n(s){return{key:Buffer.from([e.InputTypes.NON_WITNESS_UTXO]),value:s}}Kr.encode=n,Kr.expected="Buffer";function r(s){return Buffer.isBuffer(s)}Kr.check=r;function i(s,o){return!!s&&!!o&&s.nonWitnessUtxo===void 0}return Kr.canAdd=i,Kr}var Gr={},Up;function pA(){if(Up)return Gr;Up=1,Object.defineProperty(Gr,"__esModule",{value:!0});const e=qt();function t(o){if(o.key[0]!==e.InputTypes.PARTIAL_SIG)throw new Error("Decode Error: could not decode partialSig with key 0x"+o.key.toString("hex"));if(!(o.key.length===34||o.key.length===66)||![2,3,4].includes(o.key[1]))throw new Error("Decode Error: partialSig has invalid pubkey in key 0x"+o.key.toString("hex"));return{pubkey:o.key.slice(1),signature:o.value}}Gr.decode=t;function n(o){const c=Buffer.from([e.InputTypes.PARTIAL_SIG]);return{key:Buffer.concat([c,o.pubkey]),value:o.signature}}Gr.encode=n,Gr.expected="{ pubkey: Buffer; signature: Buffer; }";function r(o){return Buffer.isBuffer(o.pubkey)&&Buffer.isBuffer(o.signature)&&[33,65].includes(o.pubkey.length)&&[2,3,4].includes(o.pubkey[0])&&i(o.signature)}Gr.check=r;function i(o){if(!Buffer.isBuffer(o)||o.length<9||o[0]!==48||o.length!==o[1]+3||o[2]!==2)return!1;const c=o[3];if(c>33||c<1||o[3+c+1]!==2)return!1;const a=o[3+c+2];return!(a>33||a<1||o.length!==3+c+2+a+2)}function s(o,c,a){const u=c.pubkey.toString("hex");return a.has(u)?!1:(a.add(u),o.filter(f=>f.pubkey.equals(c.pubkey)).length===0)}return Gr.canAddToArray=s,Gr}var Wr={},Cp;function yA(){if(Cp)return Wr;Cp=1,Object.defineProperty(Wr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.POR_COMMITMENT)throw new Error("Decode Error: could not decode porCommitment with key 0x"+s.key.toString("hex"));return s.value.toString("utf8")}Wr.decode=t;function n(s){return{key:Buffer.from([e.InputTypes.POR_COMMITMENT]),value:Buffer.from(s,"utf8")}}Wr.encode=n,Wr.expected="string";function r(s){return typeof s=="string"}Wr.check=r;function i(s,o){return!!s&&!!o&&s.porCommitment===void 0}return Wr.canAdd=i,Wr}var Xr={},Hp;function gA(){if(Hp)return Xr;Hp=1,Object.defineProperty(Xr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.SIGHASH_TYPE)throw new Error("Decode Error: could not decode sighashType with key 0x"+s.key.toString("hex"));return s.value.readUInt32LE(0)}Xr.decode=t;function n(s){const o=Buffer.from([e.InputTypes.SIGHASH_TYPE]),c=Buffer.allocUnsafe(4);return c.writeUInt32LE(s,0),{key:o,value:c}}Xr.encode=n,Xr.expected="number";function r(s){return typeof s=="number"}Xr.check=r;function i(s,o){return!!s&&!!o&&s.sighashType===void 0}return Xr.canAdd=i,Xr}var zr={},Fp;function mA(){if(Fp)return zr;Fp=1,Object.defineProperty(zr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.TAP_KEY_SIG||s.key.length!==1)throw new Error("Decode Error: could not decode tapKeySig with key 0x"+s.key.toString("hex"));if(!r(s.value))throw new Error("Decode Error: tapKeySig not a valid 64-65-byte BIP340 signature");return s.value}zr.decode=t;function n(s){return{key:Buffer.from([e.InputTypes.TAP_KEY_SIG]),value:s}}zr.encode=n,zr.expected="Buffer";function r(s){return Buffer.isBuffer(s)&&(s.length===64||s.length===65)}zr.check=r;function i(s,o){return!!s&&!!o&&s.tapKeySig===void 0}return zr.canAdd=i,zr}var Yr={},Mp;function wA(){if(Mp)return Yr;Mp=1,Object.defineProperty(Yr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.TAP_LEAF_SCRIPT)throw new Error("Decode Error: could not decode tapLeafScript with key 0x"+s.key.toString("hex"));if((s.key.length-2)%32!==0)throw new Error("Decode Error: tapLeafScript has invalid control block in key 0x"+s.key.toString("hex"));const o=s.value[s.value.length-1];if((s.key[1]&254)!==o)throw new Error("Decode Error: tapLeafScript bad leaf version in key 0x"+s.key.toString("hex"));const c=s.value.slice(0,-1);return{controlBlock:s.key.slice(1),script:c,leafVersion:o}}Yr.decode=t;function n(s){const o=Buffer.from([e.InputTypes.TAP_LEAF_SCRIPT]),c=Buffer.from([s.leafVersion]);return{key:Buffer.concat([o,s.controlBlock]),value:Buffer.concat([s.script,c])}}Yr.encode=n,Yr.expected="{ controlBlock: Buffer; leafVersion: number, script: Buffer; }";function r(s){return Buffer.isBuffer(s.controlBlock)&&(s.controlBlock.length-1)%32===0&&(s.controlBlock[0]&254)===s.leafVersion&&Buffer.isBuffer(s.script)}Yr.check=r;function i(s,o,c){const a=o.controlBlock.toString("hex");return c.has(a)?!1:(c.add(a),s.filter(u=>u.controlBlock.equals(o.controlBlock)).length===0)}return Yr.canAddToArray=i,Yr}var Zr={},qp;function bA(){if(qp)return Zr;qp=1,Object.defineProperty(Zr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.TAP_MERKLE_ROOT||s.key.length!==1)throw new Error("Decode Error: could not decode tapMerkleRoot with key 0x"+s.key.toString("hex"));if(!r(s.value))throw new Error("Decode Error: tapMerkleRoot not a 32-byte hash");return s.value}Zr.decode=t;function n(s){return{key:Buffer.from([e.InputTypes.TAP_MERKLE_ROOT]),value:s}}Zr.encode=n,Zr.expected="Buffer";function r(s){return Buffer.isBuffer(s)&&s.length===32}Zr.check=r;function i(s,o){return!!s&&!!o&&s.tapMerkleRoot===void 0}return Zr.canAdd=i,Zr}var Jr={},Lp;function EA(){if(Lp)return Jr;Lp=1,Object.defineProperty(Jr,"__esModule",{value:!0});const e=qt();function t(s){if(s.key[0]!==e.InputTypes.TAP_SCRIPT_SIG)throw new Error("Decode Error: could not decode tapScriptSig with key 0x"+s.key.toString("hex"));if(s.key.length!==65)throw new Error("Decode Error: tapScriptSig has invalid key 0x"+s.key.toString("hex"));if(s.value.length!==64&&s.value.length!==65)throw new Error("Decode Error: tapScriptSig has invalid signature in key 0x"+s.key.toString("hex"));const o=s.key.slice(1,33),c=s.key.slice(33);return{pubkey:o,leafHash:c,signature:s.value}}Jr.decode=t;function n(s){const o=Buffer.from([e.InputTypes.TAP_SCRIPT_SIG]);return{key:Buffer.concat([o,s.pubkey,s.leafHash]),value:s.signature}}Jr.encode=n,Jr.expected="{ pubkey: Buffer; leafHash: Buffer; signature: Buffer; }";function r(s){return Buffer.isBuffer(s.pubkey)&&Buffer.isBuffer(s.leafHash)&&Buffer.isBuffer(s.signature)&&s.pubkey.length===32&&s.leafHash.length===32&&(s.signature.length===64||s.signature.length===65)}Jr.check=r;function i(s,o,c){const a=o.pubkey.toString("hex")+o.leafHash.toString("hex");return c.has(a)?!1:(c.add(a),s.filter(u=>u.pubkey.equals(o.pubkey)&&u.leafHash.equals(o.leafHash)).length===0)}return Jr.canAddToArray=i,Jr}var Qr={},dr={},fs={},Vp;function ns(){if(Vp)return fs;Vp=1,Object.defineProperty(fs,"__esModule",{value:!0});const e=9007199254740991;function t(s){if(s<0||s>e||s%1!==0)throw new RangeError("value out of range")}function n(s,o,c){if(t(s),o||(o=Buffer.allocUnsafe(i(s))),!Buffer.isBuffer(o))throw new TypeError("buffer must be a Buffer instance");return c||(c=0),s<253?(o.writeUInt8(s,c),Object.assign(n,{bytes:1})):s<=65535?(o.writeUInt8(253,c),o.writeUInt16LE(s,c+1),Object.assign(n,{bytes:3})):s<=4294967295?(o.writeUInt8(254,c),o.writeUInt32LE(s,c+1),Object.assign(n,{bytes:5})):(o.writeUInt8(255,c),o.writeUInt32LE(s>>>0,c+1),o.writeUInt32LE(s/4294967296|0,c+5),Object.assign(n,{bytes:9})),o}fs.encode=n;function r(s,o){if(!Buffer.isBuffer(s))throw new TypeError("buffer must be a Buffer instance");o||(o=0);const c=s.readUInt8(o);if(c<253)return Object.assign(r,{bytes:1}),c;if(c===253)return Object.assign(r,{bytes:3}),s.readUInt16LE(o+1);if(c===254)return Object.assign(r,{bytes:5}),s.readUInt32LE(o+1);{Object.assign(r,{bytes:9});const a=s.readUInt32LE(o+1),f=s.readUInt32LE(o+5)*4294967296+a;return t(f),f}}fs.decode=r;function i(s){return t(s),s<253?1:s<=65535?3:s<=4294967295?5:9}return fs.encodingLength=i,fs}var $p;function vh(){if($p)return dr;$p=1,Object.defineProperty(dr,"__esModule",{value:!0});const e=ns();dr.range=c=>[...Array(c).keys()];function t(c){if(c.length<1)return c;let a=c.length-1,u=0;for(let f=0;f<c.length/2;f++)u=c[f],c[f]=c[a],c[a]=u,a--;return c}dr.reverseBuffer=t;function n(c){const a=c.map(r);return a.push(Buffer.from([0])),Buffer.concat(a)}dr.keyValsToBuffer=n;function r(c){const a=c.key.length,u=c.value.length,f=e.encodingLength(a),l=e.encodingLength(u),d=Buffer.allocUnsafe(f+a+l+u);return e.encode(a,d,0),c.key.copy(d,f),e.encode(u,d,f+a),c.value.copy(d,f+a+l),d}dr.keyValToBuffer=r;function i(c,a){if(typeof c!="number")throw new Error("cannot write a non-number as a number");if(c<0)throw new Error("specified a negative value for writing an unsigned value");if(c>a)throw new Error("RangeError: value out of range");if(Math.floor(c)!==c)throw new Error("value has a fractional component")}function s(c,a){const u=c.readUInt32LE(a);let f=c.readUInt32LE(a+4);return f*=4294967296,i(f+u,9007199254740991),f+u}dr.readUInt64LE=s;function o(c,a,u){return i(a,9007199254740991),c.writeInt32LE(a&-1,u),c.writeUInt32LE(Math.floor(a/4294967296),u+4),u+8}return dr.writeUInt64LE=o,dr}var Dp;function _A(){if(Dp)return Qr;Dp=1,Object.defineProperty(Qr,"__esModule",{value:!0});const e=qt(),t=vh(),n=ns();function r(c){if(c.key[0]!==e.InputTypes.WITNESS_UTXO)throw new Error("Decode Error: could not decode witnessUtxo with key 0x"+c.key.toString("hex"));const a=t.readUInt64LE(c.value,0);let u=8;const f=n.decode(c.value,u);u+=n.encodingLength(f);const l=c.value.slice(u);if(l.length!==f)throw new Error("Decode Error: WITNESS_UTXO script is not proper length");return{script:l,value:a}}Qr.decode=r;function i(c){const{script:a,value:u}=c,f=n.encodingLength(a.length),l=Buffer.allocUnsafe(8+f+a.length);return t.writeUInt64LE(l,u,0),n.encode(a.length,l,8),a.copy(l,8+f),{key:Buffer.from([e.InputTypes.WITNESS_UTXO]),value:l}}Qr.encode=i,Qr.expected="{ script: Buffer; value: number; }";function s(c){return Buffer.isBuffer(c.script)&&typeof c.value=="number"}Qr.check=s;function o(c,a){return!!c&&!!a&&c.witnessUtxo===void 0}return Qr.canAdd=o,Qr}var ei={},jp;function SA(){if(jp)return ei;jp=1,Object.defineProperty(ei,"__esModule",{value:!0});const e=qt(),t=ns();function n(o){if(o.key[0]!==e.OutputTypes.TAP_TREE||o.key.length!==1)throw new Error("Decode Error: could not decode tapTree with key 0x"+o.key.toString("hex"));let c=0;const a=[];for(;c<o.value.length;){const u=o.value[c++],f=o.value[c++],l=t.decode(o.value,c);c+=t.encodingLength(l),a.push({depth:u,leafVersion:f,script:o.value.slice(c,c+l)}),c+=l}return{leaves:a}}ei.decode=n;function r(o){const c=Buffer.from([e.OutputTypes.TAP_TREE]),a=[].concat(...o.leaves.map(u=>[Buffer.of(u.depth,u.leafVersion),t.encode(u.script.length),u.script]));return{key:c,value:Buffer.concat(a)}}ei.encode=r,ei.expected="{ leaves: [{ depth: number; leafVersion: number, script: Buffer; }] }";function i(o){return Array.isArray(o.leaves)&&o.leaves.every(c=>c.depth>=0&&c.depth<=128&&(c.leafVersion&254)===c.leafVersion&&Buffer.isBuffer(c.script))}ei.check=i;function s(o,c){return!!o&&!!c&&o.tapTree===void 0}return ei.canAdd=s,ei}var Vc={},Kp;function Yw(){if(Kp)return Vc;Kp=1,Object.defineProperty(Vc,"__esModule",{value:!0});const e=r=>[...Array(r).keys()],t=r=>r.length===33&&[2,3].includes(r[0])||r.length===65&&r[0]===4;function n(r,i=t){function s(f){if(f.key[0]!==r)throw new Error("Decode Error: could not decode bip32Derivation with key 0x"+f.key.toString("hex"));const l=f.key.slice(1);if(!i(l))throw new Error("Decode Error: bip32Derivation has invalid pubkey in key 0x"+f.key.toString("hex"));if(f.value.length/4%1!==0)throw new Error("Decode Error: Input BIP32_DERIVATION value length should be multiple of 4");const d={masterFingerprint:f.value.slice(0,4),pubkey:l,path:"m"};for(const p of e(f.value.length/4-1)){const g=f.value.readUInt32LE(p*4+4),m=!!(g&2147483648),E=g&2147483647;d.path+="/"+E.toString(10)+(m?"'":"")}return d}function o(f){const l=Buffer.from([r]),d=Buffer.concat([l,f.pubkey]),p=f.path.split("/"),g=Buffer.allocUnsafe(p.length*4);f.masterFingerprint.copy(g,0);let m=4;return p.slice(1).forEach(E=>{const _=E.slice(-1)==="'";let S=2147483647&parseInt(_?E.slice(0,-1):E,10);_&&(S+=2147483648),g.writeUInt32LE(S,m),m+=4}),{key:d,value:g}}const c="{ masterFingerprint: Buffer; pubkey: Buffer; path: string; }";function a(f){return Buffer.isBuffer(f.pubkey)&&Buffer.isBuffer(f.masterFingerprint)&&typeof f.path=="string"&&i(f.pubkey)&&f.masterFingerprint.length===4}function u(f,l,d){const p=l.pubkey.toString("hex");return d.has(p)?!1:(d.add(p),f.filter(g=>g.pubkey.equals(l.pubkey)).length===0)}return{decode:s,encode:o,check:a,expected:c,canAddToArray:u}}return Vc.makeConverter=n,Vc}var $c={},Gp;function TA(){if(Gp)return $c;Gp=1,Object.defineProperty($c,"__esModule",{value:!0});function e(t){return n;function n(r){let i;if(t.includes(r.key[0])&&(i=r.key.slice(1),!(i.length===33||i.length===65)||![2,3,4].includes(i[0])))throw new Error("Format Error: invalid pubkey in key 0x"+r.key.toString("hex"));return i}}return $c.makeChecker=e,$c}var Dc={},Wp;function xA(){if(Wp)return Dc;Wp=1,Object.defineProperty(Dc,"__esModule",{value:!0});function e(t){function n(c){if(c.key[0]!==t)throw new Error("Decode Error: could not decode redeemScript with key 0x"+c.key.toString("hex"));return c.value}function r(c){return{key:Buffer.from([t]),value:c}}const i="Buffer";function s(c){return Buffer.isBuffer(c)}function o(c,a){return!!c&&!!a&&c.redeemScript===void 0}return{decode:n,encode:r,check:s,expected:i,canAdd:o}}return Dc.makeConverter=e,Dc}var jc={},Xp;function AA(){if(Xp)return jc;Xp=1,Object.defineProperty(jc,"__esModule",{value:!0});const e=ns(),t=Yw(),n=i=>i.length===32;function r(i){const s=t.makeConverter(i,n);function o(f){const l=e.decode(f.value),d=e.encodingLength(l),p=s.decode({key:f.key,value:f.value.slice(d+l*32)}),g=new Array(l);for(let m=0,E=d;m<l;m++,E+=32)g[m]=f.value.slice(E,E+32);return Object.assign({},p,{leafHashes:g})}function c(f){const l=s.encode(f),d=e.encodingLength(f.leafHashes.length),p=Buffer.allocUnsafe(d);e.encode(f.leafHashes.length,p);const g=Buffer.concat([p,...f.leafHashes,l.value]);return Object.assign({},l,{value:g})}const a="{ masterFingerprint: Buffer; pubkey: Buffer; path: string; leafHashes: Buffer[]; }";function u(f){return Array.isArray(f.leafHashes)&&f.leafHashes.every(l=>Buffer.isBuffer(l)&&l.length===32)&&s.check(f)}return{decode:o,encode:c,check:u,expected:a,canAddToArray:s.canAddToArray}}return jc.makeConverter=r,jc}var Kc={},zp;function vA(){if(zp)return Kc;zp=1,Object.defineProperty(Kc,"__esModule",{value:!0});function e(t){function n(c){if(c.key[0]!==t||c.key.length!==1)throw new Error("Decode Error: could not decode tapInternalKey with key 0x"+c.key.toString("hex"));if(c.value.length!==32)throw new Error("Decode Error: tapInternalKey not a 32-byte x-only pubkey");return c.value}function r(c){return{key:Buffer.from([t]),value:c}}const i="Buffer";function s(c){return Buffer.isBuffer(c)&&c.length===32}function o(c,a){return!!c&&!!a&&c.tapInternalKey===void 0}return{decode:n,encode:r,check:s,expected:i,canAdd:o}}return Kc.makeConverter=e,Kc}var Gc={},Yp;function IA(){if(Yp)return Gc;Yp=1,Object.defineProperty(Gc,"__esModule",{value:!0});function e(t){function n(c){if(c.key[0]!==t)throw new Error("Decode Error: could not decode witnessScript with key 0x"+c.key.toString("hex"));return c.value}function r(c){return{key:Buffer.from([t]),value:c}}const i="Buffer";function s(c){return Buffer.isBuffer(c)}function o(c,a){return!!c&&!!a&&c.witnessScript===void 0}return{decode:n,encode:r,check:s,expected:i,canAdd:o}}return Gc.makeConverter=e,Gc}var Zp;function Ih(){if(Zp)return us;Zp=1,Object.defineProperty(us,"__esModule",{value:!0});const e=qt(),t=uA(),n=fA(),r=lA(),i=hA(),s=dA(),o=pA(),c=yA(),a=gA(),u=mA(),f=wA(),l=bA(),d=EA(),p=_A(),g=SA(),m=Yw(),E=TA(),_=xA(),S=AA(),w=vA(),T=IA(),O={unsignedTx:n,globalXpub:t,checkPubkey:E.makeChecker([])};us.globals=O;const A={nonWitnessUtxo:s,partialSig:o,sighashType:a,finalScriptSig:r,finalScriptWitness:i,porCommitment:c,witnessUtxo:p,bip32Derivation:m.makeConverter(e.InputTypes.BIP32_DERIVATION),redeemScript:_.makeConverter(e.InputTypes.REDEEM_SCRIPT),witnessScript:T.makeConverter(e.InputTypes.WITNESS_SCRIPT),checkPubkey:E.makeChecker([e.InputTypes.PARTIAL_SIG,e.InputTypes.BIP32_DERIVATION]),tapKeySig:u,tapScriptSig:d,tapLeafScript:f,tapBip32Derivation:S.makeConverter(e.InputTypes.TAP_BIP32_DERIVATION),tapInternalKey:w.makeConverter(e.InputTypes.TAP_INTERNAL_KEY),tapMerkleRoot:l};us.inputs=A;const R={bip32Derivation:m.makeConverter(e.OutputTypes.BIP32_DERIVATION),redeemScript:_.makeConverter(e.OutputTypes.REDEEM_SCRIPT),witnessScript:T.makeConverter(e.OutputTypes.WITNESS_SCRIPT),checkPubkey:E.makeChecker([e.OutputTypes.BIP32_DERIVATION]),tapBip32Derivation:S.makeConverter(e.OutputTypes.TAP_BIP32_DERIVATION),tapTree:g,tapInternalKey:w.makeConverter(e.OutputTypes.TAP_INTERNAL_KEY)};return us.outputs=R,us}var Jp;function OA(){if(Jp)return as;Jp=1,Object.defineProperty(as,"__esModule",{value:!0});const e=Ih(),t=vh(),n=ns(),r=qt();function i(c,a){let u=0;function f(){const R=n.decode(c,u);u+=n.encodingLength(R);const M=c.slice(u,u+R);return u+=R,M}function l(){const R=c.readUInt32BE(u);return u+=4,R}function d(){const R=c.readUInt8(u);return u+=1,R}function p(){const R=f(),M=f();return{key:R,value:M}}function g(){if(u>=c.length)throw new Error("Format Error: Unexpected End of PSBT");const R=c.readUInt8(u)===0;return R&&u++,R}if(l()!==1886610036)throw new Error("Format Error: Invalid Magic Number");if(d()!==255)throw new Error("Format Error: Magic Number must be followed by 0xff separator");const m=[],E={};for(;!g();){const R=p(),M=R.key.toString("hex");if(E[M])throw new Error("Format Error: Keys must be unique for global keymap: key "+M);E[M]=1,m.push(R)}const _=m.filter(R=>R.key[0]===r.GlobalTypes.UNSIGNED_TX);if(_.length!==1)throw new Error("Format Error: Only one UNSIGNED_TX allowed");const S=a(_[0].value),{inputCount:w,outputCount:T}=S.getInputOutputCounts(),O=[],A=[];for(const R of t.range(w)){const M={},v=[];for(;!g();){const U=p(),L=U.key.toString("hex");if(M[L])throw new Error("Format Error: Keys must be unique for each input: input index "+R+" key "+L);M[L]=1,v.push(U)}O.push(v)}for(const R of t.range(T)){const M={},v=[];for(;!g();){const U=p(),L=U.key.toString("hex");if(M[L])throw new Error("Format Error: Keys must be unique for each output: output index "+R+" key "+L);M[L]=1,v.push(U)}A.push(v)}return o(S,{globalMapKeyVals:m,inputKeyVals:O,outputKeyVals:A})}as.psbtFromBuffer=i;function s(c,a,u){if(!a.equals(Buffer.from([u])))throw new Error(`Format Error: Invalid ${c} key: ${a.toString("hex")}`)}as.checkKeyBuffer=s;function o(c,{globalMapKeyVals:a,inputKeyVals:u,outputKeyVals:f}){const l={unsignedTx:c};let d=0;for(const _ of a)switch(_.key[0]){case r.GlobalTypes.UNSIGNED_TX:if(s("global",_.key,r.GlobalTypes.UNSIGNED_TX),d>0)throw new Error("Format Error: GlobalMap has multiple UNSIGNED_TX");d++;break;case r.GlobalTypes.GLOBAL_XPUB:l.globalXpub===void 0&&(l.globalXpub=[]),l.globalXpub.push(e.globals.globalXpub.decode(_));break;default:l.unknownKeyVals||(l.unknownKeyVals=[]),l.unknownKeyVals.push(_)}const p=u.length,g=f.length,m=[],E=[];for(const _ of t.range(p)){const S={};for(const w of u[_])switch(e.inputs.checkPubkey(w),w.key[0]){case r.InputTypes.NON_WITNESS_UTXO:if(s("input",w.key,r.InputTypes.NON_WITNESS_UTXO),S.nonWitnessUtxo!==void 0)throw new Error("Format Error: Input has multiple NON_WITNESS_UTXO");S.nonWitnessUtxo=e.inputs.nonWitnessUtxo.decode(w);break;case r.InputTypes.WITNESS_UTXO:if(s("input",w.key,r.InputTypes.WITNESS_UTXO),S.witnessUtxo!==void 0)throw new Error("Format Error: Input has multiple WITNESS_UTXO");S.witnessUtxo=e.inputs.witnessUtxo.decode(w);break;case r.InputTypes.PARTIAL_SIG:S.partialSig===void 0&&(S.partialSig=[]),S.partialSig.push(e.inputs.partialSig.decode(w));break;case r.InputTypes.SIGHASH_TYPE:if(s("input",w.key,r.InputTypes.SIGHASH_TYPE),S.sighashType!==void 0)throw new Error("Format Error: Input has multiple SIGHASH_TYPE");S.sighashType=e.inputs.sighashType.decode(w);break;case r.InputTypes.REDEEM_SCRIPT:if(s("input",w.key,r.InputTypes.REDEEM_SCRIPT),S.redeemScript!==void 0)throw new Error("Format Error: Input has multiple REDEEM_SCRIPT");S.redeemScript=e.inputs.redeemScript.decode(w);break;case r.InputTypes.WITNESS_SCRIPT:if(s("input",w.key,r.InputTypes.WITNESS_SCRIPT),S.witnessScript!==void 0)throw new Error("Format Error: Input has multiple WITNESS_SCRIPT");S.witnessScript=e.inputs.witnessScript.decode(w);break;case r.InputTypes.BIP32_DERIVATION:S.bip32Derivation===void 0&&(S.bip32Derivation=[]),S.bip32Derivation.push(e.inputs.bip32Derivation.decode(w));break;case r.InputTypes.FINAL_SCRIPTSIG:s("input",w.key,r.InputTypes.FINAL_SCRIPTSIG),S.finalScriptSig=e.inputs.finalScriptSig.decode(w);break;case r.InputTypes.FINAL_SCRIPTWITNESS:s("input",w.key,r.InputTypes.FINAL_SCRIPTWITNESS),S.finalScriptWitness=e.inputs.finalScriptWitness.decode(w);break;case r.InputTypes.POR_COMMITMENT:s("input",w.key,r.InputTypes.POR_COMMITMENT),S.porCommitment=e.inputs.porCommitment.decode(w);break;case r.InputTypes.TAP_KEY_SIG:s("input",w.key,r.InputTypes.TAP_KEY_SIG),S.tapKeySig=e.inputs.tapKeySig.decode(w);break;case r.InputTypes.TAP_SCRIPT_SIG:S.tapScriptSig===void 0&&(S.tapScriptSig=[]),S.tapScriptSig.push(e.inputs.tapScriptSig.decode(w));break;case r.InputTypes.TAP_LEAF_SCRIPT:S.tapLeafScript===void 0&&(S.tapLeafScript=[]),S.tapLeafScript.push(e.inputs.tapLeafScript.decode(w));break;case r.InputTypes.TAP_BIP32_DERIVATION:S.tapBip32Derivation===void 0&&(S.tapBip32Derivation=[]),S.tapBip32Derivation.push(e.inputs.tapBip32Derivation.decode(w));break;case r.InputTypes.TAP_INTERNAL_KEY:s("input",w.key,r.InputTypes.TAP_INTERNAL_KEY),S.tapInternalKey=e.inputs.tapInternalKey.decode(w);break;case r.InputTypes.TAP_MERKLE_ROOT:s("input",w.key,r.InputTypes.TAP_MERKLE_ROOT),S.tapMerkleRoot=e.inputs.tapMerkleRoot.decode(w);break;default:S.unknownKeyVals||(S.unknownKeyVals=[]),S.unknownKeyVals.push(w)}m.push(S)}for(const _ of t.range(g)){const S={};for(const w of f[_])switch(e.outputs.checkPubkey(w),w.key[0]){case r.OutputTypes.REDEEM_SCRIPT:if(s("output",w.key,r.OutputTypes.REDEEM_SCRIPT),S.redeemScript!==void 0)throw new Error("Format Error: Output has multiple REDEEM_SCRIPT");S.redeemScript=e.outputs.redeemScript.decode(w);break;case r.OutputTypes.WITNESS_SCRIPT:if(s("output",w.key,r.OutputTypes.WITNESS_SCRIPT),S.witnessScript!==void 0)throw new Error("Format Error: Output has multiple WITNESS_SCRIPT");S.witnessScript=e.outputs.witnessScript.decode(w);break;case r.OutputTypes.BIP32_DERIVATION:S.bip32Derivation===void 0&&(S.bip32Derivation=[]),S.bip32Derivation.push(e.outputs.bip32Derivation.decode(w));break;case r.OutputTypes.TAP_INTERNAL_KEY:s("output",w.key,r.OutputTypes.TAP_INTERNAL_KEY),S.tapInternalKey=e.outputs.tapInternalKey.decode(w);break;case r.OutputTypes.TAP_TREE:s("output",w.key,r.OutputTypes.TAP_TREE),S.tapTree=e.outputs.tapTree.decode(w);break;case r.OutputTypes.TAP_BIP32_DERIVATION:S.tapBip32Derivation===void 0&&(S.tapBip32Derivation=[]),S.tapBip32Derivation.push(e.outputs.tapBip32Derivation.decode(w));break;default:S.unknownKeyVals||(S.unknownKeyVals=[]),S.unknownKeyVals.push(w)}E.push(S)}return{globalMap:l,inputs:m,outputs:E}}return as.psbtFromKeyVals=o,as}var bo={},Qp;function BA(){if(Qp)return bo;Qp=1,Object.defineProperty(bo,"__esModule",{value:!0});const e=Ih(),t=vh();function n({globalMap:o,inputs:c,outputs:a}){const{globalKeyVals:u,inputKeyVals:f,outputKeyVals:l}=s({globalMap:o,inputs:c,outputs:a}),d=t.keyValsToBuffer(u),p=_=>_.length===0?[Buffer.from([0])]:_.map(t.keyValsToBuffer),g=p(f),m=p(l),E=Buffer.allocUnsafe(5);return E.writeUIntBE(482972169471,0,5),Buffer.concat([E,d].concat(g,m))}bo.psbtToBuffer=n;const r=(o,c)=>o.key.compare(c.key);function i(o,c){const a=new Set,u=Object.entries(o).reduce((l,[d,p])=>{if(d==="unknownKeyVals")return l;const g=c[d];if(g===void 0)return l;const m=(Array.isArray(p)?p:[p]).map(g.encode);return m.map(_=>_.key.toString("hex")).forEach(_=>{if(a.has(_))throw new Error("Serialize Error: Duplicate key: "+_);a.add(_)}),l.concat(m)},[]),f=o.unknownKeyVals?o.unknownKeyVals.filter(l=>!a.has(l.key.toString("hex"))):[];return u.concat(f).sort(r)}function s({globalMap:o,inputs:c,outputs:a}){return{globalKeyVals:i(o,e.globals),inputKeyVals:c.map(u=>i(u,e.inputs)),outputKeyVals:a.map(u=>i(u,e.outputs))}}return bo.psbtToKeyVals=s,bo}var ey;function Zw(){return ey||(ey=1,(function(e){function t(n){for(var r in n)e.hasOwnProperty(r)||(e[r]=n[r])}Object.defineProperty(e,"__esModule",{value:!0}),t(OA()),t(BA())})(kf)),kf}var ty;function NA(){if(ty)return qc;ty=1,Object.defineProperty(qc,"__esModule",{value:!0});const e=Zw();function t(s){const o=s[0],c=e.psbtToKeyVals(o),a=s.slice(1);if(a.length===0)throw new Error("Combine: Nothing to combine");const u=r(o);if(u===void 0)throw new Error("Combine: Self missing transaction");const f=i(c.globalKeyVals),l=c.inputKeyVals.map(i),d=c.outputKeyVals.map(i);for(const p of a){const g=r(p);if(g===void 0||!g.toBuffer().equals(u.toBuffer()))throw new Error("Combine: One of the Psbts does not have the same transaction.");const m=e.psbtToKeyVals(p);i(m.globalKeyVals).forEach(n(f,c.globalKeyVals,m.globalKeyVals)),m.inputKeyVals.map(i).forEach((w,T)=>w.forEach(n(l[T],c.inputKeyVals[T],m.inputKeyVals[T]))),m.outputKeyVals.map(i).forEach((w,T)=>w.forEach(n(d[T],c.outputKeyVals[T],m.outputKeyVals[T])))}return e.psbtFromKeyVals(u,{globalMapKeyVals:c.globalKeyVals,inputKeyVals:c.inputKeyVals,outputKeyVals:c.outputKeyVals})}qc.combine=t;function n(s,o,c){return a=>{if(s.has(a))return;const u=c.filter(f=>f.key.toString("hex")===a)[0];o.push(u),s.add(a)}}function r(s){return s.globalMap.unsignedTx}function i(s){const o=new Set;return s.forEach(c=>{const a=c.key.toString("hex");if(o.has(a))throw new Error("Combine: KeyValue Map keys should be unique");o.add(a)}),o}return qc}var Rf={},ny;function Jw(){return ny||(ny=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0});const t=Ih();function n(p,g){const m=p[g];if(m===void 0)throw new Error(`No input #${g}`);return m}e.checkForInput=n;function r(p,g){const m=p[g];if(m===void 0)throw new Error(`No output #${g}`);return m}e.checkForOutput=r;function i(p,g,m){if(p.key[0]<m)throw new Error("Use the method for your specific key instead of addUnknownKeyVal*");if(g&&g.filter(E=>E.key.equals(p.key)).length!==0)throw new Error(`Duplicate Key: ${p.key.toString("hex")}`)}e.checkHasKey=i;function s(p){let g=0;return Object.keys(p).forEach(m=>{Number(isNaN(Number(m)))&&g++}),g}e.getEnumLength=s;function o(p,g){let m=!1;if(g.nonWitnessUtxo||g.witnessUtxo){const E=!!g.redeemScript,_=!!g.witnessScript,S=!E||!!g.finalScriptSig,w=!_||!!g.finalScriptWitness,T=!!g.finalScriptSig||!!g.finalScriptWitness;m=S&&w&&T}if(m===!1)throw new Error(`Input #${p} has too much or too little data to clean`)}e.inputCheckUncleanFinalized=o;function c(p,g,m,E){throw new Error(`Data for ${p} key ${g} is incorrect: Expected ${m} and got ${JSON.stringify(E)}`)}function a(p){return(g,m)=>{for(const E of Object.keys(g)){const _=g[E],{canAdd:S,canAddToArray:w,check:T,expected:O}=t[p+"s"][E]||{},A=!!w;if(T)if(A){if(!Array.isArray(_)||m[E]&&!Array.isArray(m[E]))throw new Error(`Key type ${E} must be an array`);_.every(T)||c(p,E,O,_);const R=m[E]||[],M=new Set;if(!_.every(v=>w(R,v,M)))throw new Error("Can not add duplicate data to array");m[E]=R.concat(_)}else{if(T(_)||c(p,E,O,_),!S(m,_))throw new Error(`Can not add duplicate data to ${p}`);m[E]=_}}}}e.updateGlobal=a("global"),e.updateInput=a("input"),e.updateOutput=a("output");function u(p,g){const m=p.length-1,E=n(p,m);e.updateInput(g,E)}e.addInputAttributes=u;function f(p,g){const m=p.length-1,E=r(p,m);e.updateOutput(g,E)}e.addOutputAttributes=f;function l(p,g){if(!Buffer.isBuffer(g)||g.length<4)throw new Error("Set Version: Invalid Transaction");return g.writeUInt32LE(p,0),g}e.defaultVersionSetter=l;function d(p,g){if(!Buffer.isBuffer(g)||g.length<4)throw new Error("Set Locktime: Invalid Transaction");return g.writeUInt32LE(p,g.length-4),g}e.defaultLocktimeSetter=d})(Rf)),Rf}var ry;function kA(){if(ry)return Mc;ry=1,Object.defineProperty(Mc,"__esModule",{value:!0});const e=NA(),t=Zw(),n=qt(),r=Jw();class i{constructor(o){this.inputs=[],this.outputs=[],this.globalMap={unsignedTx:o}}static fromBase64(o,c){const a=Buffer.from(o,"base64");return this.fromBuffer(a,c)}static fromHex(o,c){const a=Buffer.from(o,"hex");return this.fromBuffer(a,c)}static fromBuffer(o,c){const a=t.psbtFromBuffer(o,c),u=new this(a.globalMap.unsignedTx);return Object.assign(u,a),u}toBase64(){return this.toBuffer().toString("base64")}toHex(){return this.toBuffer().toString("hex")}toBuffer(){return t.psbtToBuffer(this)}updateGlobal(o){return r.updateGlobal(o,this.globalMap),this}updateInput(o,c){const a=r.checkForInput(this.inputs,o);return r.updateInput(c,a),this}updateOutput(o,c){const a=r.checkForOutput(this.outputs,o);return r.updateOutput(c,a),this}addUnknownKeyValToGlobal(o){return r.checkHasKey(o,this.globalMap.unknownKeyVals,r.getEnumLength(n.GlobalTypes)),this.globalMap.unknownKeyVals||(this.globalMap.unknownKeyVals=[]),this.globalMap.unknownKeyVals.push(o),this}addUnknownKeyValToInput(o,c){const a=r.checkForInput(this.inputs,o);return r.checkHasKey(c,a.unknownKeyVals,r.getEnumLength(n.InputTypes)),a.unknownKeyVals||(a.unknownKeyVals=[]),a.unknownKeyVals.push(c),this}addUnknownKeyValToOutput(o,c){const a=r.checkForOutput(this.outputs,o);return r.checkHasKey(c,a.unknownKeyVals,r.getEnumLength(n.OutputTypes)),a.unknownKeyVals||(a.unknownKeyVals=[]),a.unknownKeyVals.push(c),this}addInput(o){this.globalMap.unsignedTx.addInput(o),this.inputs.push({unknownKeyVals:[]});const c=o.unknownKeyVals||[],a=this.inputs.length-1;if(!Array.isArray(c))throw new Error("unknownKeyVals must be an Array");return c.forEach(u=>this.addUnknownKeyValToInput(a,u)),r.addInputAttributes(this.inputs,o),this}addOutput(o){this.globalMap.unsignedTx.addOutput(o),this.outputs.push({unknownKeyVals:[]});const c=o.unknownKeyVals||[],a=this.outputs.length-1;if(!Array.isArray(c))throw new Error("unknownKeyVals must be an Array");return c.forEach(u=>this.addUnknownKeyValToOutput(a,u)),r.addOutputAttributes(this.outputs,o),this}clearFinalizedInput(o){const c=r.checkForInput(this.inputs,o);r.inputCheckUncleanFinalized(o,c);for(const a of Object.keys(c))["witnessUtxo","nonWitnessUtxo","finalScriptSig","finalScriptWitness","unknownKeyVals"].includes(a)||delete c[a];return this}combine(...o){const c=e.combine([this].concat(o));return Object.assign(this,c),this}getTransaction(){return this.globalMap.unsignedTx.toBuffer()}}return Mc.Psbt=i,Mc}var et={},Ye={},iy;function pl(){if(iy)return Ye;iy=1,Object.defineProperty(Ye,"__esModule",{value:!0}),Ye.signatureBlocksAction=Ye.checkInputForSig=Ye.pubkeyInScript=Ye.pubkeyPositionInScript=Ye.witnessStackToScriptWitness=Ye.isP2TR=Ye.isP2SHScript=Ye.isP2WSHScript=Ye.isP2WPKH=Ye.isP2PKH=Ye.isP2PK=Ye.isP2MS=void 0;const e=ns(),t=rn(),n=_c(),r=Ur(),i=Ec();function s(p){return g=>{try{return p({output:g}),!0}catch{return!1}}}Ye.isP2MS=s(i.p2ms),Ye.isP2PK=s(i.p2pk),Ye.isP2PKH=s(i.p2pkh),Ye.isP2WPKH=s(i.p2wpkh),Ye.isP2WSHScript=s(i.p2wsh),Ye.isP2SHScript=s(i.p2sh),Ye.isP2TR=s(i.p2tr);function o(p){let g=Buffer.allocUnsafe(0);function m(w){g=Buffer.concat([g,Buffer.from(w)])}function E(w){const T=g.length,O=e.encodingLength(w);g=Buffer.concat([g,Buffer.allocUnsafe(O)]),e.encode(w,g,T)}function _(w){E(w.length),m(w)}function S(w){E(w.length),w.forEach(_)}return S(p),g}Ye.witnessStackToScriptWitness=o;function c(p,g){const m=(0,r.hash160)(p),E=p.slice(1,33),_=t.decompile(g);if(_===null)throw new Error("Unknown script error");return _.findIndex(S=>typeof S=="number"?!1:S.equals(p)||S.equals(m)||S.equals(E))}Ye.pubkeyPositionInScript=c;function a(p,g){return c(p,g)!==-1}Ye.pubkeyInScript=a;function u(p,g){return l(p).some(E=>f(E,t.signature.decode,g))}Ye.checkInputForSig=u;function f(p,g,m){const{hashType:E}=g(p),_=[];switch(E&n.Transaction.SIGHASH_ANYONECANPAY&&_.push("addInput"),E&31){case n.Transaction.SIGHASH_ALL:break;case n.Transaction.SIGHASH_SINGLE:case n.Transaction.SIGHASH_NONE:_.push("addOutput"),_.push("setInputSequence");break}return _.indexOf(m)===-1}Ye.signatureBlocksAction=f;function l(p){let g=[];if((p.partialSig||[]).length===0){if(!p.finalScriptSig&&!p.finalScriptWitness)return[];g=d(p)}else g=p.partialSig;return g.map(m=>m.signature)}function d(p){const g=p.finalScriptSig?t.decompile(p.finalScriptSig)||[]:[],m=p.finalScriptWitness?t.decompile(p.finalScriptWitness)||[]:[];return g.concat(m).filter(E=>Buffer.isBuffer(E)&&t.isCanonicalScriptSignature(E)).map(E=>({signature:E}))}return Ye}var sy;function PA(){if(sy)return et;sy=1,Object.defineProperty(et,"__esModule",{value:!0}),et.checkTaprootInputForSigs=et.tapTreeFromList=et.tapTreeToList=et.tweakInternalPubKey=et.checkTaprootOutputFields=et.checkTaprootInputFields=et.isTaprootOutput=et.isTaprootInput=et.serializeTaprootSignature=et.tapScriptFinalizer=et.toXOnly=void 0;const e=zt(),t=_c(),n=pl(),r=xh(),i=Ec(),s=pl(),o=x=>x.length===32?x:x.slice(1,33);et.toXOnly=o;function c(x,B,q){const N=P(B,x,q);try{const G=H(B,N).concat(N.script).concat(N.controlBlock);return{finalScriptWitness:(0,n.witnessStackToScriptWitness)(G)}}catch(V){throw new Error(`Can not finalize taproot input #${x}: ${V}`)}}et.tapScriptFinalizer=c;function a(x,B){const q=B?Buffer.from([B]):Buffer.from([]);return Buffer.concat([x,q])}et.serializeTaprootSignature=a;function u(x){return x&&!!(x.tapInternalKey||x.tapMerkleRoot||x.tapLeafScript&&x.tapLeafScript.length||x.tapBip32Derivation&&x.tapBip32Derivation.length||x.witnessUtxo&&(0,n.isP2TR)(x.witnessUtxo.script))}et.isTaprootInput=u;function f(x,B){return x&&!!(x.tapInternalKey||x.tapTree||x.tapBip32Derivation&&x.tapBip32Derivation.length||B&&(0,n.isP2TR)(B))}et.isTaprootOutput=f;function l(x,B,q){v(x,B,q),L(x,B,q)}et.checkTaprootInputFields=l;function d(x,B,q){U(x,B,q),p(x,B)}et.checkTaprootOutputFields=d;function p(x,B){if(!B.tapTree&&!B.tapInternalKey)return;const q=B.tapInternalKey||x.tapInternalKey,N=B.tapTree||x.tapTree;if(q){const{script:V}=x,G=g(q,N);if(V&&!V.equals(G))throw new Error("Error adding output. Script or address missmatch.")}}function g(x,B){const q=B&&_(B.leaves),{output:N}=(0,i.p2tr)({internalPubkey:x,scriptTree:q});return N}function m(x,B){const q=B.tapInternalKey,N=q&&(0,r.tweakKey)(q,B.tapMerkleRoot);if(!N)throw new Error(`Cannot tweak tap internal key for input #${x}. Public key: ${q&&q.toString("hex")}`);return N.x}et.tweakInternalPubKey=m;function E(x){if(!(0,e.isTaptree)(x))throw new Error("Cannot convert taptree to tapleaf list. Expecting a tapree structure.");return A(x)}et.tapTreeToList=E;function _(x=[]){return x.length===1&&x[0].depth===0?{output:x[0].script,version:x[0].leafVersion}:R(x)}et.tapTreeFromList=_;function S(x,B){return T(x).some(N=>(0,s.signatureBlocksAction)(N,w,B))}et.checkTaprootInputForSigs=S;function w(x){return{signature:x.slice(0,64),hashType:x.slice(64)[0]||t.Transaction.SIGHASH_DEFAULT}}function T(x){const B=[];if(x.tapKeySig&&B.push(x.tapKeySig),x.tapScriptSig&&B.push(...x.tapScriptSig.map(q=>q.signature)),!B.length){const q=O(x.finalScriptWitness);q&&B.push(q)}return B}function O(x){if(!x)return;const B=x.slice(2);if(B.length===64||B.length===65)return B}function A(x,B=[],q=0){if(q>r.MAX_TAPTREE_DEPTH)throw new Error("Max taptree depth exceeded.");return x?(0,e.isTapleaf)(x)?(B.push({depth:q,leafVersion:x.version||r.LEAF_VERSION_TAPSCRIPT,script:x.output}),B):(x[0]&&A(x[0],B,q+1),x[1]&&A(x[1],B,q+1),B):[]}function R(x){let B;for(const q of x)if(B=M(q,B),!B)throw new Error("No room left to insert tapleaf in tree");return B}function M(x,B,q=0){if(q>r.MAX_TAPTREE_DEPTH)throw new Error("Max taptree depth exceeded.");if(x.depth===q)return B?void 0:{output:x.script,version:x.leafVersion};if((0,e.isTapleaf)(B))return;const N=M(x,B&&B[0],q+1);if(N)return[N,B&&B[1]];const V=M(x,B&&B[1],q+1);if(V)return[B&&B[0],V]}function v(x,B,q){const N=u(x)&&F(B),V=F(x)&&u(B),G=x===B&&u(B)&&F(B);if(N||V||G)throw new Error(`Invalid arguments for Psbt.${q}. Cannot use both taproot and non-taproot fields.`)}function U(x,B,q){const N=f(x)&&F(B),V=F(x)&&f(B),G=x===B&&f(B)&&F(B);if(N||V||G)throw new Error(`Invalid arguments for Psbt.${q}. Cannot use both taproot and non-taproot fields.`)}function L(x,B,q){if(B.tapMerkleRoot){const N=(B.tapLeafScript||[]).every(G=>K(G,B.tapMerkleRoot)),V=(x.tapLeafScript||[]).every(G=>K(G,B.tapMerkleRoot));if(!N||!V)throw new Error(`Invalid arguments for Psbt.${q}. Tapleaf not part of taptree.`)}else if(x.tapMerkleRoot&&!(B.tapLeafScript||[]).every(V=>K(V,x.tapMerkleRoot)))throw new Error(`Invalid arguments for Psbt.${q}. Tapleaf not part of taptree.`)}function K(x,B){if(!B)return!0;const q=(0,r.tapleafHash)({output:x.script,version:x.leafVersion});return(0,r.rootHashFromPath)(x.controlBlock,q).equals(B)}function H(x,B){const q=(0,r.tapleafHash)({output:B.script,version:B.leafVersion});return(x.tapScriptSig||[]).filter(N=>N.leafHash.equals(q)).map(N=>$(B.script,N)).sort((N,V)=>V.positionInScript-N.positionInScript).map(N=>N.signature)}function $(x,B){return Object.assign({positionInScript:(0,n.pubkeyPositionInScript)(B.pubkey,x)},B)}function P(x,B,q){if(!x.tapScriptSig||!x.tapScriptSig.length)throw new Error(`Can not finalize taproot input #${B}. No tapleaf script signature provided.`);const N=(x.tapLeafScript||[]).sort((V,G)=>V.controlBlock.length-G.controlBlock.length).find(V=>D(V,x.tapScriptSig,q));if(!N)throw new Error(`Can not finalize taproot input #${B}. Signature for tapleaf script not found.`);return N}function D(x,B,q){const N=(0,r.tapleafHash)({output:x.script,version:x.leafVersion});return(!q||q.equals(N))&&B.find(G=>G.leafHash.equals(N))!==void 0}function F(x){return x&&!!(x.redeemScript||x.witnessScript||x.bip32Derivation&&x.bip32Derivation.length)}return et}var oy;function RA(){if(oy)return wo;oy=1,Object.defineProperty(wo,"__esModule",{value:!0}),wo.Psbt=void 0;const e=kA(),t=ns(),n=Jw(),r=Ah(),i=_u(),s=Xn(),o=Ec(),c=xh(),a=rn(),u=_c(),f=PA(),l=pl(),d={network:s.bitcoin,maximumFeeRate:5e3};class p{static fromBase64(h,b={}){const I=Buffer.from(h,"base64");return this.fromBuffer(I,b)}static fromHex(h,b={}){const I=Buffer.from(h,"hex");return this.fromBuffer(I,b)}static fromBuffer(h,b={}){const I=e.Psbt.fromBuffer(h,g),C=new p(b,I);return L(C.__CACHE.__TX,C.__CACHE),C}constructor(h={},b=new e.Psbt(new m)){this.data=b,this.opts=Object.assign({},d,h),this.__CACHE={__NON_WITNESS_UTXO_TX_CACHE:[],__NON_WITNESS_UTXO_BUF_CACHE:[],__TX_IN_CACHE:{},__TX:this.data.globalMap.unsignedTx.tx,__UNSAFE_SIGN_NONSEGWIT:!1},this.data.inputs.length===0&&this.setVersion(2);const I=(C,j,W,ie)=>Object.defineProperty(C,j,{enumerable:W,writable:ie});I(this,"__CACHE",!1,!0),I(this,"opts",!1,!0)}get inputCount(){return this.data.inputs.length}get version(){return this.__CACHE.__TX.version}set version(h){this.setVersion(h)}get locktime(){return this.__CACHE.__TX.locktime}set locktime(h){this.setLocktime(h)}get txInputs(){return this.__CACHE.__TX.ins.map(h=>({hash:(0,i.cloneBuffer)(h.hash),index:h.index,sequence:h.sequence}))}get txOutputs(){return this.__CACHE.__TX.outs.map(h=>{let b;try{b=(0,r.fromOutputScript)(h.script,this.opts.network)}catch{}return{script:(0,i.cloneBuffer)(h.script),value:h.value,address:b}})}combine(...h){return this.data.combine(...h.map(b=>b.data)),this}clone(){const h=p.fromBuffer(this.data.toBuffer());return h.opts=JSON.parse(JSON.stringify(this.opts)),h}setMaximumFeeRate(h){O(h),this.opts.maximumFeeRate=h}setVersion(h){O(h),R(this.data.inputs,"setVersion");const b=this.__CACHE;return b.__TX.version=h,b.__EXTRACTED_TX=void 0,this}setLocktime(h){O(h),R(this.data.inputs,"setLocktime");const b=this.__CACHE;return b.__TX.locktime=h,b.__EXTRACTED_TX=void 0,this}setInputSequence(h,b){O(b),R(this.data.inputs,"setInputSequence");const I=this.__CACHE;if(I.__TX.ins.length<=h)throw new Error("Input index too high");return I.__TX.ins[h].sequence=b,I.__EXTRACTED_TX=void 0,this}addInputs(h){return h.forEach(b=>this.addInput(b)),this}addInput(h){if(arguments.length>1||!h||h.hash===void 0||h.index===void 0)throw new Error("Invalid arguments for Psbt.addInput. Requires single object with at least [hash] and [index]");(0,f.checkTaprootInputFields)(h,h,"addInput"),R(this.data.inputs,"addInput"),h.witnessScript&&ne(h.witnessScript);const b=this.__CACHE;this.data.addInput(h);const I=b.__TX.ins[b.__TX.ins.length-1];K(b,I);const C=this.data.inputs.length-1,j=this.data.inputs[C];return j.nonWitnessUtxo&&ye(this.__CACHE,j,C),b.__FEE=void 0,b.__FEE_RATE=void 0,b.__EXTRACTED_TX=void 0,this}addOutputs(h){return h.forEach(b=>this.addOutput(b)),this}addOutput(h){if(arguments.length>1||!h||h.value===void 0||h.address===void 0&&h.script===void 0)throw new Error("Invalid arguments for Psbt.addOutput. Requires single object with at least [script or address] and [value]");R(this.data.inputs,"addOutput");const{address:b}=h;if(typeof b=="string"){const{network:C}=this.opts,j=(0,r.toOutputScript)(b,C);h=Object.assign({},h,{script:j})}(0,f.checkTaprootOutputFields)(h,h,"addOutput");const I=this.__CACHE;return this.data.addOutput(h),I.__FEE=void 0,I.__FEE_RATE=void 0,I.__EXTRACTED_TX=void 0,this}extractTransaction(h){if(!this.data.inputs.every(w))throw new Error("Not finalized");const b=this.__CACHE;if(h||A(this,b,this.opts),b.__EXTRACTED_TX)return b.__EXTRACTED_TX;const I=b.__TX.clone();return J(this.data.inputs,I,b,!0),I}getFeeRate(){return D("__FEE_RATE","fee rate",this.data.inputs,this.__CACHE)}getFee(){return D("__FEE","fee",this.data.inputs,this.__CACHE)}finalizeAllInputs(){return(0,n.checkForInput)(this.data.inputs,0),k(this.data.inputs.length).forEach(h=>this.finalizeInput(h)),this}finalizeInput(h,b){const I=(0,n.checkForInput)(this.data.inputs,h);return(0,f.isTaprootInput)(I)?this._finalizeTaprootInput(h,I,void 0,b):this._finalizeInput(h,I,b)}finalizeTaprootInput(h,b,I=f.tapScriptFinalizer){const C=(0,n.checkForInput)(this.data.inputs,h);if((0,f.isTaprootInput)(C))return this._finalizeTaprootInput(h,C,b,I);throw new Error(`Cannot finalize input #${h}. Not Taproot.`)}_finalizeInput(h,b,I=F){const{script:C,isP2SH:j,isP2WSH:W,isSegwit:ie}=ue(h,b,this.__CACHE);if(!C)throw new Error(`No script found for input #${h}`);M(b);const{finalScriptSig:Q,finalScriptWitness:fe}=I(h,b,C,ie,j,W);if(Q&&this.data.updateInput(h,{finalScriptSig:Q}),fe&&this.data.updateInput(h,{finalScriptWitness:fe}),!Q&&!fe)throw new Error(`Unknown error finalizing input #${h}`);return this.data.clearFinalizedInput(h),this}_finalizeTaprootInput(h,b,I,C=f.tapScriptFinalizer){if(!b.witnessUtxo)throw new Error(`Cannot finalize input #${h}. Missing withness utxo.`);if(b.tapKeySig){const j=o.p2tr({output:b.witnessUtxo.script,signature:b.tapKeySig}),W=(0,l.witnessStackToScriptWitness)(j.witness);this.data.updateInput(h,{finalScriptWitness:W})}else{const{finalScriptWitness:j}=C(h,b,I);this.data.updateInput(h,{finalScriptWitness:j})}return this.data.clearFinalizedInput(h),this}getInputType(h){const b=(0,n.checkForInput)(this.data.inputs,h),I=ve(h,b,this.__CACHE),C=oe(I,h,"input",b.redeemScript||Ot(b.finalScriptSig),b.witnessScript||Be(b.finalScriptWitness)),j=C.type==="raw"?"":C.type+"-",W=re(C.meaningfulScript);return j+W}inputHasPubkey(h,b){const I=(0,n.checkForInput)(this.data.inputs,h);return nt(b,I,h,this.__CACHE)}inputHasHDKey(h,b){const I=(0,n.checkForInput)(this.data.inputs,h),C=T(b);return!!I.bip32Derivation&&I.bip32Derivation.some(C)}outputHasPubkey(h,b){const I=(0,n.checkForOutput)(this.data.outputs,h);return ft(b,I,h,this.__CACHE)}outputHasHDKey(h,b){const I=(0,n.checkForOutput)(this.data.outputs,h),C=T(b);return!!I.bip32Derivation&&I.bip32Derivation.some(C)}validateSignaturesOfAllInputs(h){return(0,n.checkForInput)(this.data.inputs,0),k(this.data.inputs.length).map(I=>this.validateSignaturesOfInput(I,h)).reduce((I,C)=>C===!0&&I,!0)}validateSignaturesOfInput(h,b,I){const C=this.data.inputs[h];return(0,f.isTaprootInput)(C)?this.validateSignaturesOfTaprootInput(h,b,I):this._validateSignaturesOfInput(h,b,I)}_validateSignaturesOfInput(h,b,I){const C=this.data.inputs[h],j=(C||{}).partialSig;if(!C||!j||j.length<1)throw new Error("No signatures to validate");if(typeof b!="function")throw new Error("Need validator function to validate signatures");const W=I?j.filter(ke=>ke.pubkey.equals(I)):j;if(W.length<1)throw new Error("No signatures for this pubkey");const ie=[];let Q,fe,he;for(const ke of W){const Re=a.signature.decode(ke.signature),{hash:rt,script:Ke}=he!==Re.hashType?q(h,Object.assign({},C,{sighashType:Re.hashType}),this.__CACHE,!0):{hash:Q,script:fe};he=Re.hashType,Q=rt,fe=Ke,v(ke.pubkey,Ke,"verify"),ie.push(b(ke.pubkey,rt,Re.signature))}return ie.every(ke=>ke===!0)}validateSignaturesOfTaprootInput(h,b,I){const C=this.data.inputs[h],j=(C||{}).tapKeySig,W=(C||{}).tapScriptSig;if(!C&&!j&&!(W&&!W.length))throw new Error("No signatures to validate");if(typeof b!="function")throw new Error("Need validator function to validate signatures");I=I&&(0,f.toXOnly)(I);const ie=I?X(h,C,this.data.inputs,I,this.__CACHE):N(h,C,this.data.inputs,this.__CACHE);if(!ie.length)throw new Error("No signatures for this pubkey");const Q=ie.find(he=>!he.leafHash);let fe=0;if(j&&Q){if(!b(Q.pubkey,Q.hash,G(j)))return!1;fe++}if(W)for(const he of W){const ke=ie.find(Re=>he.pubkey.equals(Re.pubkey));if(ke){if(!b(he.pubkey,ke.hash,G(he.signature)))return!1;fe++}}return fe>0}signAllInputsHD(h,b=[u.Transaction.SIGHASH_ALL]){if(!h||!h.publicKey||!h.fingerprint)throw new Error("Need HDSigner to sign input");const I=[];for(const C of k(this.data.inputs.length))try{this.signInputHD(C,h,b),I.push(!0)}catch{I.push(!1)}if(I.every(C=>C===!1))throw new Error("No inputs were signed");return this}signAllInputsHDAsync(h,b=[u.Transaction.SIGHASH_ALL]){return new Promise((I,C)=>{if(!h||!h.publicKey||!h.fingerprint)return C(new Error("Need HDSigner to sign input"));const j=[],W=[];for(const ie of k(this.data.inputs.length))W.push(this.signInputHDAsync(ie,h,b).then(()=>{j.push(!0)},()=>{j.push(!1)}));return Promise.all(W).then(()=>{if(j.every(ie=>ie===!1))return C(new Error("No inputs were signed"));I()})})}signInputHD(h,b,I=[u.Transaction.SIGHASH_ALL]){if(!b||!b.publicKey||!b.fingerprint)throw new Error("Need HDSigner to sign input");return te(h,this.data.inputs,b).forEach(j=>this.signInput(h,j,I)),this}signInputHDAsync(h,b,I=[u.Transaction.SIGHASH_ALL]){return new Promise((C,j)=>{if(!b||!b.publicKey||!b.fingerprint)return j(new Error("Need HDSigner to sign input"));const ie=te(h,this.data.inputs,b).map(Q=>this.signInputAsync(h,Q,I));return Promise.all(ie).then(()=>{C()}).catch(j)})}signAllInputs(h,b){if(!h||!h.publicKey)throw new Error("Need Signer to sign input");const I=[];for(const C of k(this.data.inputs.length))try{this.signInput(C,h,b),I.push(!0)}catch{I.push(!1)}if(I.every(C=>C===!1))throw new Error("No inputs were signed");return this}signAllInputsAsync(h,b){return new Promise((I,C)=>{if(!h||!h.publicKey)return C(new Error("Need Signer to sign input"));const j=[],W=[];for(const[ie]of this.data.inputs.entries())W.push(this.signInputAsync(ie,h,b).then(()=>{j.push(!0)},()=>{j.push(!1)}));return Promise.all(W).then(()=>{if(j.every(ie=>ie===!1))return C(new Error("No inputs were signed"));I()})})}signInput(h,b,I){if(!b||!b.publicKey)throw new Error("Need Signer to sign input");const C=(0,n.checkForInput)(this.data.inputs,h);return(0,f.isTaprootInput)(C)?this._signTaprootInput(h,C,b,void 0,I):this._signInput(h,b,I)}signTaprootInput(h,b,I,C){if(!b||!b.publicKey)throw new Error("Need Signer to sign input");const j=(0,n.checkForInput)(this.data.inputs,h);if((0,f.isTaprootInput)(j))return this._signTaprootInput(h,j,b,I,C);throw new Error(`Input #${h} is not of type Taproot.`)}_signInput(h,b,I=[u.Transaction.SIGHASH_ALL]){const{hash:C,sighashType:j}=B(this.data.inputs,h,b.publicKey,this.__CACHE,I),W=[{pubkey:b.publicKey,signature:a.signature.encode(b.sign(C),j)}];return this.data.updateInput(h,{partialSig:W}),this}_signTaprootInput(h,b,I,C,j=[u.Transaction.SIGHASH_DEFAULT]){const W=this.checkTaprootHashesForSig(h,b,I,C,j),ie=W.filter(fe=>!fe.leafHash).map(fe=>(0,f.serializeTaprootSignature)(I.signSchnorr(fe.hash),b.sighashType))[0],Q=W.filter(fe=>!!fe.leafHash).map(fe=>({pubkey:(0,f.toXOnly)(I.publicKey),signature:(0,f.serializeTaprootSignature)(I.signSchnorr(fe.hash),b.sighashType),leafHash:fe.leafHash}));return ie&&this.data.updateInput(h,{tapKeySig:ie}),Q.length&&this.data.updateInput(h,{tapScriptSig:Q}),this}signInputAsync(h,b,I){return Promise.resolve().then(()=>{if(!b||!b.publicKey)throw new Error("Need Signer to sign input");const C=(0,n.checkForInput)(this.data.inputs,h);return(0,f.isTaprootInput)(C)?this._signTaprootInputAsync(h,C,b,void 0,I):this._signInputAsync(h,b,I)})}signTaprootInputAsync(h,b,I,C){return Promise.resolve().then(()=>{if(!b||!b.publicKey)throw new Error("Need Signer to sign input");const j=(0,n.checkForInput)(this.data.inputs,h);if((0,f.isTaprootInput)(j))return this._signTaprootInputAsync(h,j,b,I,C);throw new Error(`Input #${h} is not of type Taproot.`)})}_signInputAsync(h,b,I=[u.Transaction.SIGHASH_ALL]){const{hash:C,sighashType:j}=B(this.data.inputs,h,b.publicKey,this.__CACHE,I);return Promise.resolve(b.sign(C)).then(W=>{const ie=[{pubkey:b.publicKey,signature:a.signature.encode(W,j)}];this.data.updateInput(h,{partialSig:ie})})}async _signTaprootInputAsync(h,b,I,C,j=[u.Transaction.SIGHASH_DEFAULT]){const W=this.checkTaprootHashesForSig(h,b,I,C,j),ie=[],Q=W.filter(he=>!he.leafHash)[0];if(Q){const he=Promise.resolve(I.signSchnorr(Q.hash)).then(ke=>({tapKeySig:(0,f.serializeTaprootSignature)(ke,b.sighashType)}));ie.push(he)}const fe=W.filter(he=>!!he.leafHash);if(fe.length){const he=fe.map(ke=>Promise.resolve(I.signSchnorr(ke.hash)).then(Re=>({tapScriptSig:[{pubkey:(0,f.toXOnly)(I.publicKey),signature:(0,f.serializeTaprootSignature)(Re,b.sighashType),leafHash:ke.leafHash}]})));ie.push(...he)}return Promise.all(ie).then(he=>{he.forEach(ke=>this.data.updateInput(h,ke))})}checkTaprootHashesForSig(h,b,I,C,j){if(typeof I.signSchnorr!="function")throw new Error(`Need Schnorr Signer to sign taproot input #${h}.`);const W=X(h,b,this.data.inputs,I.publicKey,this.__CACHE,C,j);if(!W||!W.length)throw new Error(`Can not sign for input #${h} with the key ${I.publicKey.toString("hex")}`);return W}toBuffer(){return _(this.__CACHE),this.data.toBuffer()}toHex(){return _(this.__CACHE),this.data.toHex()}toBase64(){return _(this.__CACHE),this.data.toBase64()}updateGlobal(h){return this.data.updateGlobal(h),this}updateInput(h,b){return b.witnessScript&&ne(b.witnessScript),(0,f.checkTaprootInputFields)(this.data.inputs[h],b,"updateInput"),this.data.updateInput(h,b),b.nonWitnessUtxo&&ye(this.__CACHE,this.data.inputs[h],h),this}updateOutput(h,b){const I=this.data.outputs[h];return(0,f.checkTaprootOutputFields)(I,b,"updateOutput"),this.data.updateOutput(h,b),this}addUnknownKeyValToGlobal(h){return this.data.addUnknownKeyValToGlobal(h),this}addUnknownKeyValToInput(h,b){return this.data.addUnknownKeyValToInput(h,b),this}addUnknownKeyValToOutput(h,b){return this.data.addUnknownKeyValToOutput(h,b),this}clearFinalizedInput(h){return this.data.clearFinalizedInput(h),this}}wo.Psbt=p;const g=y=>new m(y);class m{constructor(h=Buffer.from([2,0,0,0,0,0,0,0,0,0])){this.tx=u.Transaction.fromBuffer(h),U(this.tx),Object.defineProperty(this,"tx",{enumerable:!1,writable:!0})}getInputOutputCounts(){return{inputCount:this.tx.ins.length,outputCount:this.tx.outs.length}}addInput(h){if(h.hash===void 0||h.index===void 0||!Buffer.isBuffer(h.hash)&&typeof h.hash!="string"||typeof h.index!="number")throw new Error("Error adding input.");const b=typeof h.hash=="string"?(0,i.reverseBuffer)(Buffer.from(h.hash,"hex")):h.hash;this.tx.addInput(b,h.index,h.sequence)}addOutput(h){if(h.script===void 0||h.value===void 0||!Buffer.isBuffer(h.script)||typeof h.value!="number")throw new Error("Error adding output.");this.tx.addOutput(h.script,h.value)}toBuffer(){return this.tx.toBuffer()}}function E(y,h,b){switch(b){case"pubkey":case"pubkeyhash":case"witnesspubkeyhash":return S(1,y.partialSig);case"multisig":const I=o.p2ms({output:h});return S(I.m,y.partialSig,I.pubkeys);default:return!1}}function _(y){if(y.__UNSAFE_SIGN_NONSEGWIT!==!1)throw new Error("Not BIP174 compliant, can not export")}function S(y,h,b){if(!h)return!1;let I;if(b?I=b.map(C=>{const j=gn(C);return h.find(W=>W.pubkey.equals(j))}).filter(C=>!!C):I=h,I.length>y)throw new Error("Too many signatures");return I.length===y}function w(y){return!!y.finalScriptSig||!!y.finalScriptWitness}function T(y){return h=>!(!h.masterFingerprint.equals(y.fingerprint)||!y.derivePath(h.path).publicKey.equals(h.pubkey))}function O(y){if(typeof y!="number"||y!==Math.floor(y)||y>4294967295||y<0)throw new Error("Invalid 32 bit integer")}function A(y,h,b){const I=h.__FEE_RATE||y.getFeeRate(),C=h.__EXTRACTED_TX.virtualSize(),j=I*C;if(I>=b.maximumFeeRate)throw new Error(`Warning: You are paying around ${(j/1e8).toFixed(8)} in fees, which is ${I} satoshi per byte for a transaction with a VSize of ${C} bytes (segwit counted as 0.25 byte per byte). Use setMaximumFeeRate method to raise your threshold, or pass true to the first arg of extractTransaction.`)}function R(y,h){y.forEach(b=>{if((0,f.isTaprootInput)(b)?(0,f.checkTaprootInputForSigs)(b,h):(0,l.checkInputForSig)(b,h))throw new Error("Can not modify transaction, signatures exist.")})}function M(y){if(!y.sighashType||!y.partialSig)return;const{partialSig:h,sighashType:b}=y;h.forEach(I=>{const{hashType:C}=a.signature.decode(I.signature);if(b!==C)throw new Error("Signature sighash does not match input sighash type")})}function v(y,h,b){if(!(0,l.pubkeyInScript)(y,h))throw new Error(`Can not ${b} for this input with the key ${y.toString("hex")}`)}function U(y){if(!y.ins.every(b=>b.script&&b.script.length===0&&b.witness&&b.witness.length===0))throw new Error("Format Error: Transaction ScriptSigs are not empty")}function L(y,h){y.ins.forEach(b=>{K(h,b)})}function K(y,h){const b=(0,i.reverseBuffer)(Buffer.from(h.hash)).toString("hex")+":"+h.index;if(y.__TX_IN_CACHE[b])throw new Error("Duplicate input detected.");y.__TX_IN_CACHE[b]=1}function H(y,h){return(b,I,C,j)=>{const W=y({redeem:{output:C}}).output;if(!I.equals(W))throw new Error(`${h} for ${j} #${b} doesn't match the scriptPubKey in the prevout`)}}const $=H(o.p2sh,"Redeem script"),P=H(o.p2wsh,"Witness script");function D(y,h,b,I){if(!b.every(w))throw new Error(`PSBT must be finalized to calculate ${h}`);if(y==="__FEE_RATE"&&I.__FEE_RATE)return I.__FEE_RATE;if(y==="__FEE"&&I.__FEE)return I.__FEE;let C,j=!0;if(I.__EXTRACTED_TX?(C=I.__EXTRACTED_TX,j=!1):C=I.__TX.clone(),J(b,C,I,j),y==="__FEE_RATE")return I.__FEE_RATE;if(y==="__FEE")return I.__FEE}function F(y,h,b,I,C,j){const W=re(b);if(!E(h,b,W))throw new Error(`Can not finalize input #${y}`);return x(b,W,h.partialSig,I,C,j)}function x(y,h,b,I,C,j){let W,ie;const Q=ce(y,h,b),fe=j?o.p2wsh({redeem:Q}):null,he=C?o.p2sh({redeem:fe||Q}):null;return I?(fe?ie=(0,l.witnessStackToScriptWitness)(fe.witness):ie=(0,l.witnessStackToScriptWitness)(Q.witness),he&&(W=he.input)):he?W=he.input:W=Q.input,{finalScriptSig:W,finalScriptWitness:ie}}function B(y,h,b,I,C){const j=(0,n.checkForInput)(y,h),{hash:W,sighashType:ie,script:Q}=q(h,j,I,!1,C);return v(b,Q,"sign"),{hash:W,sighashType:ie}}function q(y,h,b,I,C){const j=b.__TX,W=h.sighashType||u.Transaction.SIGHASH_ALL;Y(W,C);let ie,Q;if(h.nonWitnessUtxo){const ke=le(b,h,y),Re=j.ins[y].hash,rt=ke.getHash();if(!Re.equals(rt))throw new Error(`Non-witness UTXO hash for input #${y} doesn't match the hash specified in the prevout`);const Ke=j.ins[y].index;Q=ke.outs[Ke]}else if(h.witnessUtxo)Q=h.witnessUtxo;else throw new Error("Need a Utxo input item for signing");const{meaningfulScript:fe,type:he}=oe(Q.script,y,"input",h.redeemScript,h.witnessScript);if(["p2sh-p2wsh","p2wsh"].indexOf(he)>=0)ie=j.hashForWitnessV0(y,fe,Q.value,W);else if((0,l.isP2WPKH)(fe)){const ke=o.p2pkh({hash:fe.slice(2)}).output;ie=j.hashForWitnessV0(y,ke,Q.value,W)}else{if(h.nonWitnessUtxo===void 0&&b.__UNSAFE_SIGN_NONSEGWIT===!1)throw new Error(`Input #${y} has witnessUtxo but non-segwit script: ${fe.toString("hex")}`);!I&&b.__UNSAFE_SIGN_NONSEGWIT!==!1&&console.warn(`Warning: Signing non-segwit inputs without the full parent transaction means there is a chance that a miner could feed you incorrect information to trick you into paying large fees. This behavior is the same as Psbt's predecessor (TransactionBuilder - now removed) when signing non-segwit scripts. You are not able to export this Psbt with toBuffer|toBase64|toHex since it is not BIP174 compliant.
|
|
74
74
|
*********************
|
|
75
75
|
PROCEED WITH CAUTION!
|
|
76
|
-
*********************`),ie=j.hashForSignature(y,fe,W)}return{script:fe,sighashType:W,hash:ie}}function N(y,h,b,I){const C=[];if(h.tapInternalKey){const W=V(y,h,I);W&&C.push(W)}if(h.tapScriptSig){const W=h.tapScriptSig.map(ie=>ie.pubkey);C.push(...W)}return C.map(W=>X(y,h,b,W,I)).flat()}function V(y,h,b){const{script:I}=Le(y,h,b);return(0,l.isP2TR)(I)?I.subarray(2,34):null}function G(y){return y.length===64?y:y.subarray(0,64)}function X(y,h,b,I,C,j,W){const ie=C.__TX,Q=h.sighashType||u.Transaction.SIGHASH_DEFAULT;Y(Q,W);const fe=b.map((Ke,zn)=>Le(zn,Ke,C)),he=fe.map(Ke=>Ke.script),ke=fe.map(Ke=>Ke.value),Re=[];if(h.tapInternalKey&&!j){const Ke=V(y,h,C)||Buffer.from([]);if((0,f.toXOnly)(I).equals(Ke)){const zn=ie.hashForWitnessV1(y,he,ke,Q);Re.push({pubkey:I,hash:zn})}}const rt=(h.tapLeafScript||[]).filter(Ke=>(0,l.pubkeyInScript)(I,Ke.script)).map(Ke=>{const zn=(0,c.tapleafHash)({output:Ke.script,version:Ke.leafVersion});return Object.assign({hash:zn},Ke)}).filter(Ke=>!j||j.equals(Ke.hash)).map(Ke=>{const zn=ie.hashForWitnessV1(y,he,ke,Q,Ke.hash);return{pubkey:I,hash:zn,leafHash:Ke.hash}});return Re.concat(rt)}function Y(y,h){if(h&&h.indexOf(y)<0){const b=we(y);throw new Error(`Sighash type is not allowed. Retry the sign method passing the sighashTypes array of whitelisted types. Sighash type: ${b}`)}}function ce(y,h,b){let I;switch(h){case"multisig":const C=Z(y,b);I=o.p2ms({output:y,signatures:C});break;case"pubkey":I=o.p2pk({output:y,signature:b[0].signature});break;case"pubkeyhash":I=o.p2pkh({output:y,pubkey:b[0].pubkey,signature:b[0].signature});break;case"witnesspubkeyhash":I=o.p2wpkh({output:y,pubkey:b[0].pubkey,signature:b[0].signature});break}return I}function ue(y,h,b){const I=b.__TX,C={script:null,isSegwit:!1,isP2SH:!1,isP2WSH:!1};if(C.isP2SH=!!h.redeemScript,C.isP2WSH=!!h.witnessScript,h.witnessScript)C.script=h.witnessScript;else if(h.redeemScript)C.script=h.redeemScript;else if(h.nonWitnessUtxo){const j=le(b,h,y),W=I.ins[y].index;C.script=j.outs[W].script}else h.witnessUtxo&&(C.script=h.witnessUtxo.script);return(h.witnessScript||(0,l.isP2WPKH)(C.script))&&(C.isSegwit=!0),C}function te(y,h,b){const I=(0,n.checkForInput)(h,y);if(!I.bip32Derivation||I.bip32Derivation.length===0)throw new Error("Need bip32Derivation to sign with HD");const C=I.bip32Derivation.map(W=>{if(W.masterFingerprint.equals(b.fingerprint))return W}).filter(W=>!!W);if(C.length===0)throw new Error("Need one bip32Derivation masterFingerprint to match the HDSigner fingerprint");return C.map(W=>{const ie=b.derivePath(W.path);if(!W.pubkey.equals(ie.publicKey))throw new Error("pubkey did not match bip32Derivation");return ie})}function Z(y,h){return o.p2ms({output:y}).pubkeys.map(I=>(h.filter(C=>C.pubkey.equals(I))[0]||{}).signature).filter(I=>!!I)}function ae(y){let h=0;function b(W){return h+=W,y.slice(h-W,h)}function I(){const W=t.decode(y,h);return h+=t.decode.bytes,W}function C(){return b(I())}function j(){const W=I(),ie=[];for(let Q=0;Q<W;Q++)ie.push(C());return ie}return j()}function we(y){let h=y&u.Transaction.SIGHASH_ANYONECANPAY?"SIGHASH_ANYONECANPAY | ":"";switch(y&31){case u.Transaction.SIGHASH_ALL:h+="SIGHASH_ALL";break;case u.Transaction.SIGHASH_SINGLE:h+="SIGHASH_SINGLE";break;case u.Transaction.SIGHASH_NONE:h+="SIGHASH_NONE";break}return h}function ye(y,h,b){y.__NON_WITNESS_UTXO_BUF_CACHE[b]=h.nonWitnessUtxo;const I=u.Transaction.fromBuffer(h.nonWitnessUtxo);y.__NON_WITNESS_UTXO_TX_CACHE[b]=I;const C=y,j=b;delete h.nonWitnessUtxo,Object.defineProperty(h,"nonWitnessUtxo",{enumerable:!0,get(){const W=C.__NON_WITNESS_UTXO_BUF_CACHE[j],ie=C.__NON_WITNESS_UTXO_TX_CACHE[j];if(W!==void 0)return W;{const Q=ie.toBuffer();return C.__NON_WITNESS_UTXO_BUF_CACHE[j]=Q,Q}},set(W){C.__NON_WITNESS_UTXO_BUF_CACHE[j]=W}})}function J(y,h,b,I){let C=0;y.forEach((Q,fe)=>{if(I&&Q.finalScriptSig&&(h.ins[fe].script=Q.finalScriptSig),I&&Q.finalScriptWitness&&(h.ins[fe].witness=ae(Q.finalScriptWitness)),Q.witnessUtxo)C+=Q.witnessUtxo.value;else if(Q.nonWitnessUtxo){const he=le(b,Q,fe),ke=h.ins[fe].index,Re=he.outs[ke];C+=Re.value}});const j=h.outs.reduce((Q,fe)=>Q+fe.value,0),W=C-j;if(W<0)throw new Error("Outputs are spending more than Inputs");const ie=h.virtualSize();b.__FEE=W,b.__EXTRACTED_TX=h,b.__FEE_RATE=Math.floor(W/ie)}function le(y,h,b){const I=y.__NON_WITNESS_UTXO_TX_CACHE;return I[b]||ye(y,h,b),I[b]}function ve(y,h,b){const{script:I}=Le(y,h,b);return I}function Le(y,h,b){if(h.witnessUtxo!==void 0)return{script:h.witnessUtxo.script,value:h.witnessUtxo.value};if(h.nonWitnessUtxo!==void 0){const C=le(b,h,y).outs[b.__TX.ins[y].index];return{script:C.script,value:C.value}}else throw new Error("Can't find pubkey in input without Utxo data")}function nt(y,h,b,I){const C=ve(b,h,I),{meaningfulScript:j}=oe(C,b,"input",h.redeemScript,h.witnessScript);return(0,l.pubkeyInScript)(y,j)}function ft(y,h,b,I){const C=I.__TX.outs[b].script,{meaningfulScript:j}=oe(C,b,"output",h.redeemScript,h.witnessScript);return(0,l.pubkeyInScript)(y,j)}function Ot(y){if(!y)return;const h=a.decompile(y);if(!h)return;const b=h[h.length-1];if(!(!Buffer.isBuffer(b)||st(b)||He(b)||!a.decompile(b)))return b}function Be(y){if(!y)return;const h=ae(y),b=h[h.length-1];if(!(st(b)||!a.decompile(b)))return b}function gn(y){if(y.length===65){const h=y[64]&1,b=y.slice(0,33);return b[0]=2|h,b}return y.slice()}function st(y){return y.length===33&&a.isCanonicalPubKey(y)}function He(y){return a.isCanonicalScriptSignature(y)}function oe(y,h,b,I,C){const j=(0,l.isP2SHScript)(y),W=j&&I&&(0,l.isP2WSHScript)(I),ie=(0,l.isP2WSHScript)(y);if(j&&I===void 0)throw new Error("scriptPubkey is P2SH but redeemScript missing");if((ie||W)&&C===void 0)throw new Error("scriptPubkey or redeemScript is P2WSH but witnessScript missing");let Q;return W?(Q=C,$(h,y,I,b),P(h,I,C,b),ne(Q)):ie?(Q=C,P(h,y,C,b),ne(Q)):j?(Q=I,$(h,y,I,b)):Q=y,{meaningfulScript:Q,type:W?"p2sh-p2wsh":j?"p2sh":ie?"p2wsh":"raw"}}function ne(y){if((0,l.isP2WPKH)(y)||(0,l.isP2SHScript)(y))throw new Error("P2WPKH or P2SH can not be contained within P2WSH")}function re(y){return(0,l.isP2WPKH)(y)?"witnesspubkeyhash":(0,l.isP2PKH)(y)?"pubkeyhash":(0,l.isP2MS)(y)?"multisig":(0,l.isP2PK)(y)?"pubkey":"nonstandard"}function k(y){return[...Array(y).keys()]}return wo}var cy;function UA(){return cy||(cy=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.initEccLib=e.Transaction=e.opcodes=e.Psbt=e.Block=e.script=e.payments=e.networks=e.crypto=e.address=void 0;const t=Ah();e.address=t;const n=Ur();e.crypto=n;const r=Xn();e.networks=r;const i=Ec();e.payments=i;const s=rn();e.script=s;var o=aA();Object.defineProperty(e,"Block",{enumerable:!0,get:function(){return o.Block}});var c=RA();Object.defineProperty(e,"Psbt",{enumerable:!0,get:function(){return c.Psbt}});var a=Eh();Object.defineProperty(e,"opcodes",{enumerable:!0,get:function(){return a.OPS}});var u=_c();Object.defineProperty(e,"Transaction",{enumerable:!0,get:function(){return u.Transaction}});var f=Th();Object.defineProperty(e,"initEccLib",{enumerable:!0,get:function(){return f.initEccLib}})})(gf)),gf}var Oi={},ay;function CA(){if(ay)return Oi;ay=1,Object.defineProperty(Oi,"__esModule",{value:!0}),Oi.base26Decode=Oi.base26Encode=void 0;function e(n){let r=0n;for(let i=0;i<n.length;i++){const s=BigInt(n.charCodeAt(i)-65),o=BigInt(n.length)-1n-BigInt(i);if(o==0n)r+=s;else{const c=26n**o;r+=c*(s+1n)}}return r}Oi.base26Encode=e;function t(n){if(n===340282366920938463463374607431768211455n)return"BCGDENLQRQWDSLRUGSNLBTMFIJAV";n+=1n;let r=[];for(;n>0;){const i=(n-1n)%26n;r.push("ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Number(i))),n=(n-1n)/26n}return r.reverse().join("")}return Oi.base26Decode=t,Oi}var Bi={},uy;function Qw(){if(uy)return Bi;uy=1,Object.defineProperty(Bi,"__esModule",{value:!0}),Bi.none=Bi.some=void 0;class e{constructor(){}isSome(){return!1}map(s){return new e}value(){return null}}class t{constructor(s){this._value=s}isSome(){return!0}map(s){return new t(s(this.value()))}value(){return this._value}}function n(i){return new t(i)}Bi.some=n;function r(){return new e}return Bi.none=r,Bi}var Ni={},fy;function HA(){if(fy)return Ni;fy=1,Object.defineProperty(Ni,"__esModule",{value:!0}),Ni.decodeLEB128=Ni.encodeLEB128=void 0;function e(n){const r=[];let i=!0;for(;i;){let s=Number(n&BigInt(127));n>>=BigInt(7),n===BigInt(0)?i=!1:s|=128,r.push(s)}return r}Ni.encodeLEB128=e;function t(n){let r=BigInt(0);for(let i=0;i<n.length;i++){const s=BigInt(n[i]);if(i>18)throw new Error("Overlong");let o=s&BigInt(127);if(i==18&&(o&BigInt(124))!=BigInt(0))throw new Error("Overflow");if(r|=o<<BigInt(7)*BigInt(i),(s&BigInt(128))==BigInt(0))return{n:r,len:i+1}}throw new Error("Unterminated")}return Ni.decodeLEB128=t,Ni}var Uf={},ly;function FA(){return ly||(ly=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.toPushData=e.chunks=e.toHex=e.zero2=void 0;const t=s=>s.length%2===1?"0"+s:s;e.zero2=t;const n=s=>{let o="";for(let c=0;c<s.length;c++)o+=(0,e.zero2)(s[c].toString(16));return o};e.toHex=n;function r(s,o){const c=[];let a=0;for(;a<s.length;){const u=s.slice(a,a+o);c.push(u),a+=o}return c}e.chunks=r;function i(s){const o=[],c=s.length;if(c<76){const a=Buffer.alloc(1);a.writeUInt8(c),o.push(a)}else if(c<=255){o.push(Buffer.from("4c","hex"));const a=Buffer.alloc(1);a.writeUInt8(c),o.push(a)}else if(c<=65535){o.push(Buffer.from("4d","hex"));const a=Buffer.alloc(2);a.writeUint16LE(c),o.push(a)}else{o.push(Buffer.from("4e","hex"));const a=Buffer.alloc(4);a.writeUint32LE(c),o.push(a)}return o.push(s),Buffer.concat(o)}e.toPushData=i})(Uf)),Uf}var pr={},hy;function e1(){if(hy)return pr;hy=1,Object.defineProperty(pr,"__esModule",{value:!0}),pr.removeSpacers=pr.getSpacersVal=pr.applySpacers=void 0;function e(r,i){let s="";for(let o=0;o<r.length;o++)s+=r.charAt(o),i>0&&((i&1)===1&&(s+="•"),i>>=1);return s}pr.applySpacers=e;function t(r){let i=0,s=0;for(let o=0;o<r.length;o++)r.charAt(o)==="•"&&(i+=1<<o-1-s,s++);return i}pr.getSpacersVal=t;function n(r){return r.replace(/[•]+/g,"")}return pr.removeSpacers=n,pr}var dy;function MA(){if(dy)return ze;dy=1,Object.defineProperty(ze,"__esModule",{value:!0}),ze.EtchInscription=ze.Message=ze.Runestone=ze.Etching=ze.Rune=ze.Terms=ze.Range=ze.Flaw=ze.Tag=ze.Flag=ze.Edict=ze.RuneId=void 0;const e=UA(),t=CA(),n=Qw(),r=HA(),i=FA(),s=e1();class o{constructor(w,T){this.block=w,this.idx=T}next(w,T){if(w>BigInt(Number.MAX_SAFE_INTEGER)||T>BigInt(Number.MAX_SAFE_INTEGER))return(0,n.none)();let O=BigInt(this.block)+w;if(O>BigInt(Number.MAX_SAFE_INTEGER))return(0,n.none)();let A=w===0n?BigInt(this.idx)+T:T;return A>BigInt(Number.MAX_SAFE_INTEGER)?(0,n.none)():(0,n.some)(new o(Number(O),Number(A)))}}ze.RuneId=o;class c{constructor(w,T,O){this.id=w,this.amount=T,this.output=O}static from_integers(w,T,O,A){return A>4294967295n||A<0n||Number(A)>w.outs.length?(0,n.none)():(0,n.some)(new c(T,O,Number(A)))}}ze.Edict=c;var a;(function(S){S[S.Etching=0]="Etching",S[S.Terms=1]="Terms",S[S.Turbo=2]="Turbo",S[S.Cenotaph=127]="Cenotaph"})(a||(ze.Flag=a={}));var u;(function(S){S[S.Body=0]="Body",S[S.Flags=2]="Flags",S[S.Rune=4]="Rune",S[S.Premine=6]="Premine",S[S.Cap=8]="Cap",S[S.Amount=10]="Amount",S[S.HeightStart=12]="HeightStart",S[S.HeightEnd=14]="HeightEnd",S[S.OffsetStart=16]="OffsetStart",S[S.OffsetEnd=18]="OffsetEnd",S[S.Mint=20]="Mint",S[S.Pointer=22]="Pointer",S[S.Cenotaph=126]="Cenotaph",S[S.Divisibility=1]="Divisibility",S[S.Spacers=3]="Spacers",S[S.Symbol=5]="Symbol",S[S.Nop=127]="Nop"})(u||(ze.Tag=u={}));var f;(function(S){S[S.EdictOutput=0]="EdictOutput",S[S.EdictRuneId=1]="EdictRuneId",S[S.InvalidScript=2]="InvalidScript",S[S.Opcode=3]="Opcode",S[S.SupplyOverflow=4]="SupplyOverflow",S[S.TrailingIntegers=5]="TrailingIntegers",S[S.TruncatedField=6]="TruncatedField",S[S.UnrecognizedEvenTag=7]="UnrecognizedEvenTag",S[S.UnrecognizedFlag=8]="UnrecognizedFlag",S[S.Varint=9]="Varint"})(f||(ze.Flaw=f={}));class l{constructor(w,T){this.start=w,this.end=T}}ze.Range=l;class d{constructor(w,T,O,A){this.amount=w,this.cap=T,this.height=O,this.offset=A}}ze.Terms=d;class p{constructor(w){this.value=w}get name(){return p.toName(this.value)}static toName(w){return(0,t.base26Decode)(w)}static fromName(w){return new p((0,t.base26Encode)((0,s.removeSpacers)(w)))}toString(){return this.name}}ze.Rune=p;class g{constructor(w,T,O,A,R,M,v){this.divisibility=w,this.premine=T,this.rune=O,this.spacers=A,this.symbol=R,this.terms=M,this.turbo=v}}ze.Etching=g,g.MAX_DIVISIBILITY=38,g.MAX_SPACERS=134217727;class m{constructor(w=[],T,O,A){this.edicts=w,this.etching=T,this.mint=O,this.pointer=A}static create(w,T="etch"){if(T==="etch"){w=w;const O=p.fromName(w.name),A=new d(w.amount,w.cap,new l(w.startHeight?(0,n.some)(w.startHeight):(0,n.none)(),w.endHeight?(0,n.some)(w.endHeight):(0,n.none)()),new l(w.startOffset?(0,n.some)(w.startOffset):(0,n.none)(),w.endOffset?(0,n.some)(w.endOffset):(0,n.none)())),R=w.divisibility?(0,n.some)(w.divisibility):(0,n.none)(),M=w.premine?(0,n.some)(w.premine):(0,n.none)(),v=w.name.indexOf("•")>-1?(0,n.some)((0,s.getSpacersVal)(w.name)):(0,n.none)(),U=w.symbol?(0,n.some)(w.symbol):(0,n.none)(),L=typeof w.pointer=="number"?(0,n.some)(w.pointer):(0,n.none)(),K=new g(R,M,(0,n.some)(O),v,U,(0,n.some)(A),!0);return new m([],(0,n.some)(K),(0,n.none)(),L)}else if(T==="mint"){w=w;const O=typeof w.pointer=="number"?(0,n.some)(w.pointer):(0,n.none)();return new m([],(0,n.none)(),(0,n.some)(new o(w.block,w.txIdx)),O)}else throw new Error(`not ${T} support now`)}static decipher(w){const T=e.Transaction.fromHex(w),O=m.payload(T);if(O.isSome()){const A=m.integers(O.value()),R=E.from_integers(T,A.value()),M=R.getEtching(),v=R.getMint(),U=R.getPointer();return(0,n.some)(new m(R.edicts,M,v,U))}return(0,n.none)()}encipher(){const T=this.toMessage().toBuffer(),O=Buffer.from("6a5d","hex");let A;if(T.length<76)A=Buffer.alloc(1),A.writeUint8(T.length);else if(T.length<256)A=Buffer.alloc(2),A.writeUint8(76),A.writeUint8(T.length);else if(T.length<65536)A=Buffer.alloc(3),A.writeUint8(77),A.writeUint16LE(T.length);else if(T.length<4294967296)A=Buffer.alloc(5),A.writeUint8(78),A.writeUint32LE(T.length);else throw new Error("runestone too big!");return Buffer.concat([O,A,T])}static payload(w){for(const T of w.outs){const O=e.script.decompile(T.script);if(O[0]===e.script.OPS.OP_RETURN&&O[1]===m.MAGIC_NUMBER){for(let A=2;A<O.length;A++){const R=O[A];return R instanceof Uint8Array?(0,n.some)(Array.from(R)):(0,n.none)()}return(0,n.none)()}}return(0,n.none)()}static integers(w){let T=[],O=0;for(;O<w.length;){let{n:A,len:R}=(0,r.decodeLEB128)(w.slice(O));T.push(A),O+=R}return(0,n.some)(T)}toMessage(){let w=new Map;const T=this.etching.value();if(T){let R=1;if(T.terms.isSome()){let $=1<<a.Terms;R|=$}if(T.turbo){let $=1<<a.Turbo;R|=$}w.set(u.Flags,[BigInt(R)]);const M=T.rune.value();M!==null&&w.set(u.Rune,[BigInt(M.value)]);const v=T.divisibility.value();v!==null&&w.set(u.Divisibility,[BigInt(v)]);const U=T.spacers.value();U!==null&&w.set(u.Spacers,[BigInt(U)]);const L=T.symbol.value();L!==null&&w.set(u.Symbol,[BigInt(L.charCodeAt(0))]);const K=T.premine.value();K!==null&&w.set(u.Premine,[BigInt(K)]);const H=T.terms.value();if(H!==null){w.set(u.Amount,[BigInt(H.amount)]),w.set(u.Cap,[BigInt(H.cap)]);const $=H.height.start.value();$&&w.set(u.HeightStart,[BigInt($)]);const P=H.height.end.value();P&&w.set(u.HeightEnd,[BigInt(P)]);const D=H.offset.start.value();D&&w.set(u.OffsetStart,[BigInt(D)]);const F=H.offset.end.value();F&&w.set(u.OffsetEnd,[BigInt(F)])}}const O=this.mint.value();O!==null&&w.set(u.Mint,[BigInt(O.block),BigInt(O.idx)]);const A=this.pointer.value();return A!==null&&w.set(u.Pointer,[BigInt(A)]),new E(w,this.edicts,0)}}ze.Runestone=m,m.MAGIC_NUMBER=93;class E{constructor(w=new Map,T=[],O=0){this.fields=w,this.edicts=T,this.flaws=O}static from_integers(w,T){let O=new Map,A=[],R=0,M=!1;for(let v=0;v<T.length;){let U=T[v];if(Number(U)===u.Body){M=!0,v+=1;continue}if(M){let L=new o(0,0);for(const K of(0,i.chunks)(T.slice(v),4)){if(K.length!=4){R|=f.TrailingIntegers;break}let H=L.next(K[0],K[1]);if(!H.isSome()){R|=f.EdictRuneId;break}const $=c.from_integers(w,H.value(),K[2],K[3]);if(!$.isSome()){R|=f.EdictOutput;break}L=H.value(),A.push($.value())}v+=4}else{let L=T[v+1];const K=O.get(Number(U))||[];K.push(L),O.set(Number(U),K),v+=2}}return new E(O,A,R)}addFieldVal(w,T){const O=this.fields.get(Number(w))||[];O.push(T),this.fields.set(Number(w),O)}addEdict(w){this.edicts.push(w)}toBuffer(){const w=[];for(const[T,O]of this.fields)for(const A of O){const R=Buffer.alloc(1);R.writeUInt8(T),w.push(R),w.push(Buffer.from((0,r.encodeLEB128)(A)))}if(this.edicts.length>0){w.push(Buffer.from("00","hex")),this.edicts.sort((A,R)=>A.id.block==R.id.block?A.id.idx-R.id.idx:A.id.block-R.id.block);let T=0n,O=0n;for(let A=0;A<this.edicts.length;A++){const R=this.edicts[A];if(A==0)T=BigInt(R.id.block),O=BigInt(R.id.idx),w.push(Buffer.from((0,r.encodeLEB128)(T))),w.push(Buffer.from((0,r.encodeLEB128)(O)));else{const M=BigInt(R.id.block),v=BigInt(R.id.idx);if(M==T){const U=v-O;O=v,w.push(Buffer.from((0,r.encodeLEB128)(0n))),w.push(Buffer.from((0,r.encodeLEB128)(U)))}else{const U=M-T;T=M,O=v,w.push(Buffer.from((0,r.encodeLEB128)(U))),w.push(Buffer.from((0,r.encodeLEB128)(v)))}}w.push(Buffer.from((0,r.encodeLEB128)(BigInt(R.amount)))),w.push(Buffer.from((0,r.encodeLEB128)(BigInt(R.output))))}}return Buffer.concat(w)}getFlags(){return Number(this.fields.get(u.Flags))}hasFlags(w){const T=this.getFlags(),O=1<<w;return(T&O)!=0}getMint(){if(!this.fields.has(u.Mint))return(0,n.none)();const[w,T]=this.fields.get(u.Mint);return(0,n.some)(new o(Number(w),Number(T)))}getEtching(){if(!this.hasFlags(a.Etching))return(0,n.none)();const w=this.getDivisibility(),T=this.getPremine(),O=this.getRune(),A=this.getSpacers(),R=this.getSymbol(),M=this.getTerms(),v=this.hasFlags(a.Turbo);return(0,n.some)(new g(w,T,O,A,R,M,v))}getDivisibility(){if(!this.fields.has(u.Divisibility))return(0,n.none)();const[w]=this.fields.get(u.Divisibility);if(w>g.MAX_DIVISIBILITY)throw new Error("invalid divisibility");return(0,n.some)(Number(w))}getPremine(){if(!this.fields.has(u.Premine))return(0,n.none)();const[w]=this.fields.get(u.Premine);return(0,n.some)(Number(w))}getRune(){if(!this.fields.has(u.Rune))return(0,n.none)();const[w]=this.fields.get(u.Rune);return(0,n.some)(new p(w))}getSpacers(){if(!this.fields.has(u.Spacers))return(0,n.none)();const[w]=this.fields.get(u.Spacers);if(w>g.MAX_SPACERS)throw new Error("invalid spacers");return(0,n.some)(Number(w))}getHeightStart(){if(!this.fields.has(u.HeightStart))return(0,n.none)();const[w]=this.fields.get(u.HeightStart);return(0,n.some)(Number(w))}getHeightEnd(){if(!this.fields.has(u.HeightEnd))return(0,n.none)();const[w]=this.fields.get(u.HeightEnd);return(0,n.some)(Number(w))}getOffsetStart(){if(!this.fields.has(u.OffsetStart))return(0,n.none)();const[w]=this.fields.get(u.OffsetStart);return(0,n.some)(Number(w))}getOffsetEnd(){if(!this.fields.has(u.OffsetEnd))return(0,n.none)();const[w]=this.fields.get(u.OffsetEnd);return(0,n.some)(Number(w))}getCap(){if(!this.fields.has(u.Cap))return(0,n.none)();const[w]=this.fields.get(u.Cap);return(0,n.some)(Number(w))}getAmount(){if(!this.fields.has(u.Amount))return(0,n.none)();const[w]=this.fields.get(u.Amount);return(0,n.some)(Number(w))}getSymbol(){if(!this.fields.has(u.Symbol))return(0,n.none)();const[w]=this.fields.get(u.Symbol);return(0,n.some)(String.fromCharCode(Number(w)))}getTerms(){if(!this.hasFlags(a.Terms))return(0,n.none)();const w=this.getCap();if(!w.isSome())throw new Error("no cap field");const T=this.getAmount();if(!T.isSome())throw new Error("no amount field");const O=this.getHeightStart(),A=this.getHeightEnd(),R=this.getOffsetStart(),M=this.getOffsetEnd(),v=new l(O,A),U=new l(R,M);return(0,n.some)(new d(T.value(),w.value(),v,U))}getPointer(){if(!this.fields.has(u.Pointer))return(0,n.none)();const[w]=this.fields.get(u.Pointer);return(0,n.some)(Number(w))}}ze.Message=E;class _{constructor(w=new Map,T=Buffer.alloc(0)){this.fields=w,this.data=T}setContent(w,T){this.fields.set(1,Buffer.from(w,"utf8")),this.data=T}setRune(w){let O=(0,t.base26Encode)((0,s.removeSpacers)(w)).toString(16);O.length%2===1&&(O="0"+O),this.setField(_.Tag.RUNE,Buffer.from(O,"hex").reverse())}setField(w,T){this.fields.set(w,T)}static decipher(w,T){const R=e.Transaction.fromHex(w).ins[T].witness[1],M=e.script.decompile(R),v=new Map,U=[];let L=!1;for(let K=5;K<M.length-1;){const H=M[K];if(H===0){L=!0,K++;continue}else if(L)U.push(H),K++;else{const $=H-80,P=M[K+1];if(typeof P=="number"){const D=Buffer.alloc(1);D.writeUint8(P),v.set($,D)}else v.set($,P);K+=2}}return new _(v,Buffer.concat(U))}encipher(){const w=[];if(this.data&&this.data.length>0){w.push(Buffer.from("0063036f7264","hex")),Array.from(this.fields.entries()).sort((O,A)=>O[0]-A[0]).forEach(([O,A])=>{const R=Buffer.alloc(1);R.writeUInt8(O),w.push(Buffer.from("01","hex")),w.push(R),A.length!=1||A[0]!=0?w.push((0,i.toPushData)(A)):w.push(A)}),w.push(Buffer.from("00","hex"));const T=(0,i.chunks)(Array.from(this.data),520);for(const O of T)w.push((0,i.toPushData)(Buffer.from(O)))}else{w.push(Buffer.from("0063","hex"));const T=this.fields.get(_.Tag.RUNE);if(!T)throw new Error("No rune found!");w.push((0,i.toPushData)(T))}return w.push(Buffer.from("68","hex")),Buffer.concat(w)}}return ze.EtchInscription=_,_.Tag={CONTENT_TYPE:1,POINTER:2,PARENT:3,METADATA:5,METAPROTOCOL:7,CONTENT_ENCODING:9,DELEGATE:11,RUNE:13},ze}var py;function qA(){return py||(py=1,(function(e){var t=Ei&&Ei.__createBinding||(Object.create?(function(s,o,c,a){a===void 0&&(a=c);var u=Object.getOwnPropertyDescriptor(o,c);(!u||("get"in u?!o.__esModule:u.writable||u.configurable))&&(u={enumerable:!0,get:function(){return o[c]}}),Object.defineProperty(s,a,u)}):(function(s,o,c,a){a===void 0&&(a=c),s[a]=o[c]})),n=Ei&&Ei.__exportStar||function(s,o){for(var c in s)c!=="default"&&!Object.prototype.hasOwnProperty.call(o,c)&&t(o,s,c)};Object.defineProperty(e,"__esModule",{value:!0}),e.removeSpacers=e.getSpacersVal=e.applySpacers=e.some=e.none=void 0,n(MA(),e);var r=Qw();Object.defineProperty(e,"none",{enumerable:!0,get:function(){return r.none}}),Object.defineProperty(e,"some",{enumerable:!0,get:function(){return r.some}});var i=e1();Object.defineProperty(e,"applySpacers",{enumerable:!0,get:function(){return i.applySpacers}}),Object.defineProperty(e,"getSpacersVal",{enumerable:!0,get:function(){return i.getSpacersVal}}),Object.defineProperty(e,"removeSpacers",{enumerable:!0,get:function(){return i.removeSpacers}})})(Ei)),Ei}var ls=qA();function t1(e){if(e.length%2!==0)throw new Error("Hex string must have even length");const t=new Uint8Array(e.length/2);for(let n=0;n<e.length;n+=2)t[n/2]=parseInt(e.substr(n,2),16);return t}function Oh(e){return Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("")}function Bh(e){return e===ks.Mainnet?Rn:My}function Nh(e,t=ks.Testnet){const n=Bh(t);return vg(e,n)}function Mi(e){const t=Rn,n=My,r=X1;let i,s,o=mn.UNKNOWN;if(e.startsWith("bc1")||e.startsWith("tb1")||e.startsWith("bcrt1"))try{return s=Pl(e),s.version===0?s.data.length===20?o=mn.P2WPKH:o=mn.P2WSH:o=mn.P2TR,o}catch{return mn.UNKNOWN}else try{return i=Ag(e),i.version===t.pubKeyHash||i.version===n.pubKeyHash||i.version===r.pubKeyHash?o=mn.P2PKH:(i.version===t.scriptHash||(i.version,n.scriptHash),o=mn.P2SH_P2WPKH),o}catch{return mn.UNKNOWN}}function LA(e,t,n){const r=Nh(e,n),i=t.coins?.[0];return{txid:t.txid,vout:t.vout,satoshis:t.sats.toString(),height:void 0,runes:i?[{id:i.id,amount:i.value.toString()}]:[],address:e,scriptPk:Oh(r)}}async function n1(e,t){try{const n=t===ks.Mainnet?"https://mpc.omnity.network/utxo-status":"https://mpc.omnity.network/testnet4/utxo-status",r=await dt.post(n,e).then(c=>c.data);if(!r)return null;const i=JSON.stringify(r),o=new TextEncoder().encode(i);return Array.from(o)}catch{return null}}const VA=Object.freeze(Object.defineProperty({__proto__:null,bytesToHex:Oh,formatPoolUtxo:LA,getAddressType:Mi,getScriptByAddress:Nh,getUtxoProof:n1,hexToBytes:t1,toBitcoinNetwork:Bh},Symbol.toStringTag,{value:"Module"})),$A="kqs64-paaaa-aaaar-qamza-cai",DA="hvyp5-5yaaa-aaaao-qjxha-cai",Wc=BigInt(546),xn="0:0",jA="https://runescan-hasura-mainnet.omnity.network/v1/graphql",KA="https://runescan-hasura-testnet.omnity.network/v1/graphql",yy={P2PKH:148,P2SH_P2WPKH:91,P2WPKH:68,P2WSH:140,P2SH:108,P2TR:58,UNKNOWN:110},gy={P2PKH:34,P2SH_P2WPKH:32,P2WPKH:31,P2WSH:43,P2SH:32,P2TR:43,UNKNOWN:34},GA=new Set(["P2WPKH","P2WSH","P2SH_P2WPKH","P2TR"]),WA=2;class oi{psbt;client;inputAddressTypes=[];outputAddressTypes=[];config;userInputUtxoDusts=BigInt(0);intentions=[];txFee=BigInt(0);additionalDustNeeded=BigInt(0);inputUtxos=[];constructor(t,n){this.config=t,this.psbt=new wa({network:Bh(t.network)}),this.client=n}addInput(t){if(this.inputUtxos.findIndex(r=>r.txid===t.txid&&r.vout===t.vout)>=0)return;const{address:n}=t;this.psbt.data.addInput({hash:t.txid,index:t.vout,witnessUtxo:{value:BigInt(t.satoshis),script:t1(t.scriptPk)}}),this.inputAddressTypes.push(Mi(n)),this.inputUtxos.push(t),(n===this.config.address||n===this.config.paymentAddress)&&t.runes.length!==0&&(this.userInputUtxoDusts+=BigInt(t.satoshis))}addOutput(t,n){this.psbt.addOutput({address:t,value:n}),this.outputAddressTypes.push(Mi(t))}addScriptOutput(t){this.psbt.addOutput({script:t,value:BigInt(0)}),this.outputAddressTypes.push({OpReturn:BigInt(t.length)})}selectRuneUtxos(t,n,r){const i=[];if(r==BigInt(0))return i;for(const s of t)if(s.runes.length){const o=s.runes.find(c=>c.id==n);if(o&&BigInt(o.amount)==r){i.push(s);break}}if(i.length==0){let s=BigInt(0);for(const o of t)if(o.runes.forEach(c=>{c.id==n&&(s=s+BigInt(c.amount))}),i.push(o),s>=r)break;if(s<r)throw new Error("INSUFFICIENT_RUNE_UTXOs")}return i}selectBtcUtxos(t,n,r=!1){const i=[];if(n<=BigInt(0))return i;let s=BigInt(0);for(const o of t)if(!(o.runes.length&&!r)&&(s+=BigInt(o.satoshis),i.push(o),s>=n))break;if(s<n)throw new Error(`Insufficient BTC UTXOs: need ${n}, have ${s}`);return i}async addBtcAndFees(t,n){const r=this.config.paymentAddress,i=this.additionalDustNeeded,s=Mi(r),o=this.userInputUtxoDusts,c=this.config.feeRate;this.outputAddressTypes.push(s);let a=!0,u=BigInt(0),f=BigInt(0),l=[],d=BigInt(0),p=BigInt(0);const g=[...this.inputAddressTypes];do{if(u=f,c!==void 0){const _=oi.estimateTxVirtualSize(this.inputAddressTypes,this.outputAddressTypes);f=BigInt(Math.round(c*_))}else f=(await this.client.orchestrator.estimate_min_tx_fee({input_types:this.inputAddressTypes,pool_address:this.intentions.map(S=>S.poolAddress),output_types:this.outputAddressTypes})).Ok;if(d=n+f+i-o,f>u&&d>0){const _=this.selectBtcUtxos(t,d);if(_.length===0)throw new Error("INSUFFICIENT_BTC_UTXOs");this.inputAddressTypes=g.concat(_.map(()=>s));const S=_.reduce((w,T)=>w+BigInt(T.satoshis),BigInt(0));S-d>0&&S-d>Wc||(this.outputAddressTypes.pop(),a=!1),l=_}}while(f>u&&d>0);this.inputAddressTypes=[...g],a&&this.outputAddressTypes.pop();let m=BigInt(0);l.forEach(_=>{this.addInput(_),m+=BigInt(_.satoshis)});const E=m-d;if(E<0)throw new Error("Insufficient UTXO(s)");E>Wc?this.addOutput(r,E):E>BigInt(0)&&(p=E),this.txFee=p+f}async getInvolvedAddressUtxos(){const t={},n={},r=new Map,i=this.intentions.map(o=>o.poolAddress);r.set(this.config.paymentAddress,{needBtc:!0,runeIds:new Set});const s=o=>{const c=o.trim();return r.has(c)||r.set(c,{needBtc:!1,runeIds:new Set}),r.get(c)};for(const o of this.intentions){for(const u of o.inputCoins){const f=s(u.from);u.coin.id===xn?f.needBtc=!0:f.runeIds.add(u.coin.id)}for(const u of o.outputCoins){const f=s(u.to);u.coin.id===xn?f.needBtc=!0:f.runeIds.add(u.coin.id)}const c=s(o.poolAddress),a=[...o.inputCoins.map(u=>u.coin.id),...o.outputCoins.map(u=>u.coin.id)];for(const u of a)u===xn?c.needBtc=!0:c.runeIds.add(u)}return await Promise.all(Array.from(r.entries()).map(async([o,c])=>{if(c.needBtc)try{t[o]=await this.client.getBtcUtxos(o,!i.includes(o))}catch{t[o]=[]}c.runeIds.size>0&&(n[o]={},await Promise.all(Array.from(c.runeIds).map(async a=>{try{n[o][a]=await this.client.getRuneUtxos(o,a)}catch{n[o][a]=[]}})))})),{btc:t,rune:n}}addInputsAndCalculateOutputs(t){if(!this.intentions.length)throw new Error("No intentions added");const n=this.intentions.map(c=>c.poolAddress),r={},i={},s=new Map,o=new Set;this.intentions.forEach(({poolAddress:c,inputCoins:a,outputCoins:u,poolUtxos:f})=>{const l=[...a.filter(({from:p})=>!n.includes(p))],d=[...u];if(l.forEach(({coin:p,from:g})=>{!u.find(m=>m.coin.id===p.id)&&!(n.includes(g)&&g!==c)&&d.push({coin:p,to:c})}),d.forEach(({coin:p})=>{a.find(g=>g.coin.id===p.id)||l.push({coin:p,from:c})}),l.length===0&&d.length===0&&f?.length){const p=s.get(c);s.set(c,p?p.concat(f):[...f]),o.add(c);return}l.forEach(({coin:p,from:g})=>{r[g]??={},r[g][p.id]=(r[g][p.id]??BigInt(0))+BigInt(p.value)}),d.forEach(({coin:p,to:g})=>{i[g]??={},i[g][p.id]=(i[g][p.id]??BigInt(0))+BigInt(p.value)})});for(const[c,a]of s.entries()){if(!a.length)continue;i[c]??={};const u=i[c];a.forEach(f=>{this.addInput(f),u[xn]=(u[xn]??BigInt(0))+BigInt(f.satoshis),f.runes.forEach(l=>{u[l.id]=(u[l.id]??BigInt(0))+BigInt(l.amount)})})}for(const[c,a]of Object.entries(r))for(const[u,f]of Object.entries(a))if(u===xn){if(c===this.config.paymentAddress){i[c]??={},i[c][u]=(i[c][u]??BigInt(0))-f;continue}const l=t.btc[c]||[],d=this.selectBtcUtxos(l,f,n.includes(c)),g=d.reduce((m,E)=>m+BigInt(E.satoshis),BigInt(0))-f;g>BigInt(0)&&(i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+g),d.forEach(m=>{this.addInput(m),n.includes(c)&&m.runes.forEach(E=>{i[c]??={},i[c][E.id]=(i[c][E.id]??BigInt(0))+BigInt(E.amount)})})}else{const l=t.rune[c]?.[u]||[],d=this.selectRuneUtxos(l,u,f),g=d.reduce((m,E)=>{const _=E.runes.find(S=>S.id===u);return m+BigInt(_?.amount??0)},BigInt(0))-f;n.includes(c)?(i[c]??={},i[c][u]=(i[c][u]??BigInt(0))-f):g>BigInt(0)&&(i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+g),d.forEach(m=>this.addInput(m))}for(const[c,a]of Object.entries(i))if(!(r[c]||!n.includes(c)||o.has(c)))for(const[u]of Object.entries(a))if(u===xn){const f=t.btc[c]||[],l=f.reduce((d,p)=>d+BigInt(p.satoshis),BigInt(0));i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+l,f.forEach(d=>{this.addInput(d),d.runes.forEach(p=>{i[c]??={},i[c][p.id]=(i[c][p.id]??BigInt(0))+BigInt(p.amount)})})}else{const f=t.rune[c]?.[u]||[],l=f.reduce((d,p)=>{const g=p.runes.find(m=>m.id===u);return d+BigInt(g?.amount??0)},BigInt(0));i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+l,f.forEach(d=>this.addInput(d))}return i}addOutputs(t){const n=this.config.mergeSelfRuneBtcOutputs===!0,r=new Set;for(const[,s]of Object.entries(t))for(const o of Object.keys(s))o!==xn&&r.add(o);const i=Array.from(r);if(i.length>0){const s=[],o=[];let c=1;i.forEach(u=>{const f=new ls.RuneId(Number(u.split(":")[0]),Number(u.split(":")[1]));for(const[l,d]of Object.entries(t)){const p=d[u]??BigInt(0);p>BigInt(0)&&(s.push(new ls.Edict(f,p,c)),o.push(l),c++)}});const a=new ls.Runestone(s,ls.none(),ls.none(),ls.none());this.addScriptOutput(new Uint8Array(a.encipher())),o.forEach(u=>{const f=t[u]?.[xn]??BigInt(0),l=u===this.config.address,d=f>BigInt(0)&&(!l||n),p=d?f:Wc;this.addOutput(u,p),d?delete t[u][xn]:this.additionalDustNeeded+=Wc})}for(const[s,o]of Object.entries(t)){const c=o[xn]??BigInt(0);c>BigInt(0)&&this.addOutput(s,c)}}addIntention(t){this.intentions.push(t)}async build(){const t=await this.getInvolvedAddressUtxos();for(const u of this.intentions){const f=u.poolUtxos;if(!f)continue;t.btc[u.poolAddress]=f,t.rune[u.poolAddress]={};const l=t.rune[u.poolAddress];for(const d of f)for(const p of d.runes)(l[p.id]??=[]).push(d)}const n=this.addInputsAndCalculateOutputs(t);this.addOutputs(n);const r=this.config.paymentAddress,i=t.btc[r]??[],s=n[r]?.[xn]??BigInt(0);await this.addBtcAndFees(i,s<0?-s:BigInt(0));const c=this.psbt.__CACHE.__TX.clone();for(let u=0;u<this.inputUtxos.length;u++){const l=this.inputUtxos[u].address;if(l!==this.config.paymentAddress||l!==this.config.address)continue;const d=this.psbt.data.inputs[u].redeemScript,p=Mi(l);if(d&&p===mn.P2SH_P2WPKH){const g=nn([d]);c.setInputScript(u,g)}}const a=c.getId();return{psbt:this.psbt,txid:a,fee:this.txFee}}static estimateTxVirtualSize(t,n){const r=t.length,i=n.length;let s=8+oi.varIntSize(r)+oi.varIntSize(i),o=!1;for(const c of t){const a=oi.parseAddressType(c);if(a.key==="OpReturn")continue;const u=yy[a.key]??yy.UNKNOWN;s+=u,GA.has(a.key)&&(o=!0)}for(const c of n){const a=oi.parseAddressType(c);if(a.key==="OpReturn"){const f=Math.max(0,a.opReturnLength??0);s+=11+f;continue}const u=gy[a.key]??gy.UNKNOWN;s+=u}return o&&(s+=1),s+WA}static parseAddressType(t){if("OpReturn"in t){const r=t.OpReturn!==void 0?Number(t.OpReturn):0;return{key:"OpReturn",opReturnLength:Number.isFinite(r)?r:0}}const n=["P2PKH","P2SH_P2WPKH","P2WPKH","P2WSH","P2SH","P2TR"];for(const r of n)if(r in t)return{key:r};return{key:"UNKNOWN"}}static varIntSize(t){return t<253?1:t<=65535?3:t<=4294967295?5:9}async send(t){if(!this.intentions.length)throw new Error("No itentions added");const n=this.inputUtxos.filter(i=>i.address===this.config.paymentAddress||i.address===this.config.address),r=await n1(n,this.config.network);if(!r)throw new Error("Failed to get utxo proof");return this.client.orchestrator.invoke({intention_set:{tx_fee_in_sats:this.txFee,initiator_address:this.config.paymentAddress,intentions:this.intentions.map(({action:i,actionParams:s,poolAddress:o,inputCoins:c,outputCoins:a,exchangeId:u,nonce:f})=>({exchange_id:u??this.config.exchangeId,input_coins:c.filter(l=>l.from!==o),output_coins:a,pool_address:o,action:i,action_params:s??"",pool_utxo_spent:[],pool_utxo_received:[],nonce:f}))},initiator_utxo_proof:r,psbt_hex:t,client_info:this.config.clientInfo??""}).then(i=>{if(i?.Ok)return i.Ok;{const s=i?.Err??{},o=Object.keys(s)[0],c=s[o];throw new Error(c?o==="ErrorOccurredDuringExecution"?`${o}: ${c.execution_steps?.[0]?.result?.Err??"Unknown Error"}`:`Invoke Error: ${JSON.stringify(i)}`:`Invoke Error: ${JSON.stringify(i)}`)}})}}class Os extends Error{response;request;constructor(t,n){const r=`${Os.extractMessage(t)}: ${JSON.stringify({response:t,request:n})}`;super(r),Object.setPrototypeOf(this,Os.prototype),this.response=t,this.request=n,typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(this,Os)}static extractMessage(t){return t.errors?.[0]?.message??`GraphQL Error (Code: ${String(t.status)})`}}const my=e=>e.toUpperCase(),Cf=e=>typeof e=="function"?e():e,r1=(e,t)=>e.map((n,r)=>[n,t[r]]),hs=e=>{let t={};return e instanceof Headers?t=XA(e):Array.isArray(e)?e.forEach(([n,r])=>{n&&r!==void 0&&(t[n]=r)}):e&&(t=e),t},XA=e=>{const t={};return e.forEach((n,r)=>{t[r]=n}),t},zA=e=>{try{const t=e();return YA(t)?t.catch(n=>wy(n)):t}catch(t){return wy(t)}},wy=e=>e instanceof Error?e:new Error(String(e)),YA=e=>typeof e=="object"&&e!==null&&"then"in e&&typeof e.then=="function"&&"catch"in e&&typeof e.catch=="function"&&"finally"in e&&typeof e.finally=="function",kh=e=>{throw new Error(`Unhandled case: ${String(e)}`)},ia=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),ZA=(e,t)=>e.documents?e:{documents:e,requestHeaders:t,signal:void 0},JA=(e,t,n)=>e.query?e:{query:e,variables:t,requestHeaders:n,signal:void 0};function sa(e,t){if(!!!e)throw new Error(t)}function QA(e){return typeof e=="object"&&e!==null}function ev(e,t){if(!!!e)throw new Error("Unexpected invariant triggered.")}const tv=/\r\n|[\n\r]/g;function yl(e,t){let n=0,r=1;for(const i of e.body.matchAll(tv)){if(typeof i.index=="number"||ev(!1),i.index>=t)break;n=i.index+i[0].length,r+=1}return{line:r,column:t+1-n}}function nv(e){return i1(e.source,yl(e.source,e.start))}function i1(e,t){const n=e.locationOffset.column-1,r="".padStart(n)+e.body,i=t.line-1,s=e.locationOffset.line-1,o=t.line+s,c=t.line===1?n:0,a=t.column+c,u=`${e.name}:${o}:${a}
|
|
76
|
+
*********************`),ie=j.hashForSignature(y,fe,W)}return{script:fe,sighashType:W,hash:ie}}function N(y,h,b,I){const C=[];if(h.tapInternalKey){const W=V(y,h,I);W&&C.push(W)}if(h.tapScriptSig){const W=h.tapScriptSig.map(ie=>ie.pubkey);C.push(...W)}return C.map(W=>X(y,h,b,W,I)).flat()}function V(y,h,b){const{script:I}=Le(y,h,b);return(0,l.isP2TR)(I)?I.subarray(2,34):null}function G(y){return y.length===64?y:y.subarray(0,64)}function X(y,h,b,I,C,j,W){const ie=C.__TX,Q=h.sighashType||u.Transaction.SIGHASH_DEFAULT;Y(Q,W);const fe=b.map((Ke,zn)=>Le(zn,Ke,C)),he=fe.map(Ke=>Ke.script),ke=fe.map(Ke=>Ke.value),Re=[];if(h.tapInternalKey&&!j){const Ke=V(y,h,C)||Buffer.from([]);if((0,f.toXOnly)(I).equals(Ke)){const zn=ie.hashForWitnessV1(y,he,ke,Q);Re.push({pubkey:I,hash:zn})}}const rt=(h.tapLeafScript||[]).filter(Ke=>(0,l.pubkeyInScript)(I,Ke.script)).map(Ke=>{const zn=(0,c.tapleafHash)({output:Ke.script,version:Ke.leafVersion});return Object.assign({hash:zn},Ke)}).filter(Ke=>!j||j.equals(Ke.hash)).map(Ke=>{const zn=ie.hashForWitnessV1(y,he,ke,Q,Ke.hash);return{pubkey:I,hash:zn,leafHash:Ke.hash}});return Re.concat(rt)}function Y(y,h){if(h&&h.indexOf(y)<0){const b=we(y);throw new Error(`Sighash type is not allowed. Retry the sign method passing the sighashTypes array of whitelisted types. Sighash type: ${b}`)}}function ce(y,h,b){let I;switch(h){case"multisig":const C=Z(y,b);I=o.p2ms({output:y,signatures:C});break;case"pubkey":I=o.p2pk({output:y,signature:b[0].signature});break;case"pubkeyhash":I=o.p2pkh({output:y,pubkey:b[0].pubkey,signature:b[0].signature});break;case"witnesspubkeyhash":I=o.p2wpkh({output:y,pubkey:b[0].pubkey,signature:b[0].signature});break}return I}function ue(y,h,b){const I=b.__TX,C={script:null,isSegwit:!1,isP2SH:!1,isP2WSH:!1};if(C.isP2SH=!!h.redeemScript,C.isP2WSH=!!h.witnessScript,h.witnessScript)C.script=h.witnessScript;else if(h.redeemScript)C.script=h.redeemScript;else if(h.nonWitnessUtxo){const j=le(b,h,y),W=I.ins[y].index;C.script=j.outs[W].script}else h.witnessUtxo&&(C.script=h.witnessUtxo.script);return(h.witnessScript||(0,l.isP2WPKH)(C.script))&&(C.isSegwit=!0),C}function te(y,h,b){const I=(0,n.checkForInput)(h,y);if(!I.bip32Derivation||I.bip32Derivation.length===0)throw new Error("Need bip32Derivation to sign with HD");const C=I.bip32Derivation.map(W=>{if(W.masterFingerprint.equals(b.fingerprint))return W}).filter(W=>!!W);if(C.length===0)throw new Error("Need one bip32Derivation masterFingerprint to match the HDSigner fingerprint");return C.map(W=>{const ie=b.derivePath(W.path);if(!W.pubkey.equals(ie.publicKey))throw new Error("pubkey did not match bip32Derivation");return ie})}function Z(y,h){return o.p2ms({output:y}).pubkeys.map(I=>(h.filter(C=>C.pubkey.equals(I))[0]||{}).signature).filter(I=>!!I)}function ae(y){let h=0;function b(W){return h+=W,y.slice(h-W,h)}function I(){const W=t.decode(y,h);return h+=t.decode.bytes,W}function C(){return b(I())}function j(){const W=I(),ie=[];for(let Q=0;Q<W;Q++)ie.push(C());return ie}return j()}function we(y){let h=y&u.Transaction.SIGHASH_ANYONECANPAY?"SIGHASH_ANYONECANPAY | ":"";switch(y&31){case u.Transaction.SIGHASH_ALL:h+="SIGHASH_ALL";break;case u.Transaction.SIGHASH_SINGLE:h+="SIGHASH_SINGLE";break;case u.Transaction.SIGHASH_NONE:h+="SIGHASH_NONE";break}return h}function ye(y,h,b){y.__NON_WITNESS_UTXO_BUF_CACHE[b]=h.nonWitnessUtxo;const I=u.Transaction.fromBuffer(h.nonWitnessUtxo);y.__NON_WITNESS_UTXO_TX_CACHE[b]=I;const C=y,j=b;delete h.nonWitnessUtxo,Object.defineProperty(h,"nonWitnessUtxo",{enumerable:!0,get(){const W=C.__NON_WITNESS_UTXO_BUF_CACHE[j],ie=C.__NON_WITNESS_UTXO_TX_CACHE[j];if(W!==void 0)return W;{const Q=ie.toBuffer();return C.__NON_WITNESS_UTXO_BUF_CACHE[j]=Q,Q}},set(W){C.__NON_WITNESS_UTXO_BUF_CACHE[j]=W}})}function J(y,h,b,I){let C=0;y.forEach((Q,fe)=>{if(I&&Q.finalScriptSig&&(h.ins[fe].script=Q.finalScriptSig),I&&Q.finalScriptWitness&&(h.ins[fe].witness=ae(Q.finalScriptWitness)),Q.witnessUtxo)C+=Q.witnessUtxo.value;else if(Q.nonWitnessUtxo){const he=le(b,Q,fe),ke=h.ins[fe].index,Re=he.outs[ke];C+=Re.value}});const j=h.outs.reduce((Q,fe)=>Q+fe.value,0),W=C-j;if(W<0)throw new Error("Outputs are spending more than Inputs");const ie=h.virtualSize();b.__FEE=W,b.__EXTRACTED_TX=h,b.__FEE_RATE=Math.floor(W/ie)}function le(y,h,b){const I=y.__NON_WITNESS_UTXO_TX_CACHE;return I[b]||ye(y,h,b),I[b]}function ve(y,h,b){const{script:I}=Le(y,h,b);return I}function Le(y,h,b){if(h.witnessUtxo!==void 0)return{script:h.witnessUtxo.script,value:h.witnessUtxo.value};if(h.nonWitnessUtxo!==void 0){const C=le(b,h,y).outs[b.__TX.ins[y].index];return{script:C.script,value:C.value}}else throw new Error("Can't find pubkey in input without Utxo data")}function nt(y,h,b,I){const C=ve(b,h,I),{meaningfulScript:j}=oe(C,b,"input",h.redeemScript,h.witnessScript);return(0,l.pubkeyInScript)(y,j)}function ft(y,h,b,I){const C=I.__TX.outs[b].script,{meaningfulScript:j}=oe(C,b,"output",h.redeemScript,h.witnessScript);return(0,l.pubkeyInScript)(y,j)}function Ot(y){if(!y)return;const h=a.decompile(y);if(!h)return;const b=h[h.length-1];if(!(!Buffer.isBuffer(b)||st(b)||He(b)||!a.decompile(b)))return b}function Be(y){if(!y)return;const h=ae(y),b=h[h.length-1];if(!(st(b)||!a.decompile(b)))return b}function gn(y){if(y.length===65){const h=y[64]&1,b=y.slice(0,33);return b[0]=2|h,b}return y.slice()}function st(y){return y.length===33&&a.isCanonicalPubKey(y)}function He(y){return a.isCanonicalScriptSignature(y)}function oe(y,h,b,I,C){const j=(0,l.isP2SHScript)(y),W=j&&I&&(0,l.isP2WSHScript)(I),ie=(0,l.isP2WSHScript)(y);if(j&&I===void 0)throw new Error("scriptPubkey is P2SH but redeemScript missing");if((ie||W)&&C===void 0)throw new Error("scriptPubkey or redeemScript is P2WSH but witnessScript missing");let Q;return W?(Q=C,$(h,y,I,b),P(h,I,C,b),ne(Q)):ie?(Q=C,P(h,y,C,b),ne(Q)):j?(Q=I,$(h,y,I,b)):Q=y,{meaningfulScript:Q,type:W?"p2sh-p2wsh":j?"p2sh":ie?"p2wsh":"raw"}}function ne(y){if((0,l.isP2WPKH)(y)||(0,l.isP2SHScript)(y))throw new Error("P2WPKH or P2SH can not be contained within P2WSH")}function re(y){return(0,l.isP2WPKH)(y)?"witnesspubkeyhash":(0,l.isP2PKH)(y)?"pubkeyhash":(0,l.isP2MS)(y)?"multisig":(0,l.isP2PK)(y)?"pubkey":"nonstandard"}function k(y){return[...Array(y).keys()]}return wo}var cy;function UA(){return cy||(cy=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.initEccLib=e.Transaction=e.opcodes=e.Psbt=e.Block=e.script=e.payments=e.networks=e.crypto=e.address=void 0;const t=Ah();e.address=t;const n=Ur();e.crypto=n;const r=Xn();e.networks=r;const i=Ec();e.payments=i;const s=rn();e.script=s;var o=aA();Object.defineProperty(e,"Block",{enumerable:!0,get:function(){return o.Block}});var c=RA();Object.defineProperty(e,"Psbt",{enumerable:!0,get:function(){return c.Psbt}});var a=Eh();Object.defineProperty(e,"opcodes",{enumerable:!0,get:function(){return a.OPS}});var u=_c();Object.defineProperty(e,"Transaction",{enumerable:!0,get:function(){return u.Transaction}});var f=Th();Object.defineProperty(e,"initEccLib",{enumerable:!0,get:function(){return f.initEccLib}})})(gf)),gf}var Oi={},ay;function CA(){if(ay)return Oi;ay=1,Object.defineProperty(Oi,"__esModule",{value:!0}),Oi.base26Decode=Oi.base26Encode=void 0;function e(n){let r=0n;for(let i=0;i<n.length;i++){const s=BigInt(n.charCodeAt(i)-65),o=BigInt(n.length)-1n-BigInt(i);if(o==0n)r+=s;else{const c=26n**o;r+=c*(s+1n)}}return r}Oi.base26Encode=e;function t(n){if(n===340282366920938463463374607431768211455n)return"BCGDENLQRQWDSLRUGSNLBTMFIJAV";n+=1n;let r=[];for(;n>0;){const i=(n-1n)%26n;r.push("ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Number(i))),n=(n-1n)/26n}return r.reverse().join("")}return Oi.base26Decode=t,Oi}var Bi={},uy;function Qw(){if(uy)return Bi;uy=1,Object.defineProperty(Bi,"__esModule",{value:!0}),Bi.none=Bi.some=void 0;class e{constructor(){}isSome(){return!1}map(s){return new e}value(){return null}}class t{constructor(s){this._value=s}isSome(){return!0}map(s){return new t(s(this.value()))}value(){return this._value}}function n(i){return new t(i)}Bi.some=n;function r(){return new e}return Bi.none=r,Bi}var Ni={},fy;function HA(){if(fy)return Ni;fy=1,Object.defineProperty(Ni,"__esModule",{value:!0}),Ni.decodeLEB128=Ni.encodeLEB128=void 0;function e(n){const r=[];let i=!0;for(;i;){let s=Number(n&BigInt(127));n>>=BigInt(7),n===BigInt(0)?i=!1:s|=128,r.push(s)}return r}Ni.encodeLEB128=e;function t(n){let r=BigInt(0);for(let i=0;i<n.length;i++){const s=BigInt(n[i]);if(i>18)throw new Error("Overlong");let o=s&BigInt(127);if(i==18&&(o&BigInt(124))!=BigInt(0))throw new Error("Overflow");if(r|=o<<BigInt(7)*BigInt(i),(s&BigInt(128))==BigInt(0))return{n:r,len:i+1}}throw new Error("Unterminated")}return Ni.decodeLEB128=t,Ni}var Uf={},ly;function FA(){return ly||(ly=1,(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.toPushData=e.chunks=e.toHex=e.zero2=void 0;const t=s=>s.length%2===1?"0"+s:s;e.zero2=t;const n=s=>{let o="";for(let c=0;c<s.length;c++)o+=(0,e.zero2)(s[c].toString(16));return o};e.toHex=n;function r(s,o){const c=[];let a=0;for(;a<s.length;){const u=s.slice(a,a+o);c.push(u),a+=o}return c}e.chunks=r;function i(s){const o=[],c=s.length;if(c<76){const a=Buffer.alloc(1);a.writeUInt8(c),o.push(a)}else if(c<=255){o.push(Buffer.from("4c","hex"));const a=Buffer.alloc(1);a.writeUInt8(c),o.push(a)}else if(c<=65535){o.push(Buffer.from("4d","hex"));const a=Buffer.alloc(2);a.writeUint16LE(c),o.push(a)}else{o.push(Buffer.from("4e","hex"));const a=Buffer.alloc(4);a.writeUint32LE(c),o.push(a)}return o.push(s),Buffer.concat(o)}e.toPushData=i})(Uf)),Uf}var pr={},hy;function e1(){if(hy)return pr;hy=1,Object.defineProperty(pr,"__esModule",{value:!0}),pr.removeSpacers=pr.getSpacersVal=pr.applySpacers=void 0;function e(r,i){let s="";for(let o=0;o<r.length;o++)s+=r.charAt(o),i>0&&((i&1)===1&&(s+="•"),i>>=1);return s}pr.applySpacers=e;function t(r){let i=0,s=0;for(let o=0;o<r.length;o++)r.charAt(o)==="•"&&(i+=1<<o-1-s,s++);return i}pr.getSpacersVal=t;function n(r){return r.replace(/[•]+/g,"")}return pr.removeSpacers=n,pr}var dy;function MA(){if(dy)return ze;dy=1,Object.defineProperty(ze,"__esModule",{value:!0}),ze.EtchInscription=ze.Message=ze.Runestone=ze.Etching=ze.Rune=ze.Terms=ze.Range=ze.Flaw=ze.Tag=ze.Flag=ze.Edict=ze.RuneId=void 0;const e=UA(),t=CA(),n=Qw(),r=HA(),i=FA(),s=e1();class o{constructor(w,T){this.block=w,this.idx=T}next(w,T){if(w>BigInt(Number.MAX_SAFE_INTEGER)||T>BigInt(Number.MAX_SAFE_INTEGER))return(0,n.none)();let O=BigInt(this.block)+w;if(O>BigInt(Number.MAX_SAFE_INTEGER))return(0,n.none)();let A=w===0n?BigInt(this.idx)+T:T;return A>BigInt(Number.MAX_SAFE_INTEGER)?(0,n.none)():(0,n.some)(new o(Number(O),Number(A)))}}ze.RuneId=o;class c{constructor(w,T,O){this.id=w,this.amount=T,this.output=O}static from_integers(w,T,O,A){return A>4294967295n||A<0n||Number(A)>w.outs.length?(0,n.none)():(0,n.some)(new c(T,O,Number(A)))}}ze.Edict=c;var a;(function(S){S[S.Etching=0]="Etching",S[S.Terms=1]="Terms",S[S.Turbo=2]="Turbo",S[S.Cenotaph=127]="Cenotaph"})(a||(ze.Flag=a={}));var u;(function(S){S[S.Body=0]="Body",S[S.Flags=2]="Flags",S[S.Rune=4]="Rune",S[S.Premine=6]="Premine",S[S.Cap=8]="Cap",S[S.Amount=10]="Amount",S[S.HeightStart=12]="HeightStart",S[S.HeightEnd=14]="HeightEnd",S[S.OffsetStart=16]="OffsetStart",S[S.OffsetEnd=18]="OffsetEnd",S[S.Mint=20]="Mint",S[S.Pointer=22]="Pointer",S[S.Cenotaph=126]="Cenotaph",S[S.Divisibility=1]="Divisibility",S[S.Spacers=3]="Spacers",S[S.Symbol=5]="Symbol",S[S.Nop=127]="Nop"})(u||(ze.Tag=u={}));var f;(function(S){S[S.EdictOutput=0]="EdictOutput",S[S.EdictRuneId=1]="EdictRuneId",S[S.InvalidScript=2]="InvalidScript",S[S.Opcode=3]="Opcode",S[S.SupplyOverflow=4]="SupplyOverflow",S[S.TrailingIntegers=5]="TrailingIntegers",S[S.TruncatedField=6]="TruncatedField",S[S.UnrecognizedEvenTag=7]="UnrecognizedEvenTag",S[S.UnrecognizedFlag=8]="UnrecognizedFlag",S[S.Varint=9]="Varint"})(f||(ze.Flaw=f={}));class l{constructor(w,T){this.start=w,this.end=T}}ze.Range=l;class d{constructor(w,T,O,A){this.amount=w,this.cap=T,this.height=O,this.offset=A}}ze.Terms=d;class p{constructor(w){this.value=w}get name(){return p.toName(this.value)}static toName(w){return(0,t.base26Decode)(w)}static fromName(w){return new p((0,t.base26Encode)((0,s.removeSpacers)(w)))}toString(){return this.name}}ze.Rune=p;class g{constructor(w,T,O,A,R,M,v){this.divisibility=w,this.premine=T,this.rune=O,this.spacers=A,this.symbol=R,this.terms=M,this.turbo=v}}ze.Etching=g,g.MAX_DIVISIBILITY=38,g.MAX_SPACERS=134217727;class m{constructor(w=[],T,O,A){this.edicts=w,this.etching=T,this.mint=O,this.pointer=A}static create(w,T="etch"){if(T==="etch"){w=w;const O=p.fromName(w.name),A=new d(w.amount,w.cap,new l(w.startHeight?(0,n.some)(w.startHeight):(0,n.none)(),w.endHeight?(0,n.some)(w.endHeight):(0,n.none)()),new l(w.startOffset?(0,n.some)(w.startOffset):(0,n.none)(),w.endOffset?(0,n.some)(w.endOffset):(0,n.none)())),R=w.divisibility?(0,n.some)(w.divisibility):(0,n.none)(),M=w.premine?(0,n.some)(w.premine):(0,n.none)(),v=w.name.indexOf("•")>-1?(0,n.some)((0,s.getSpacersVal)(w.name)):(0,n.none)(),U=w.symbol?(0,n.some)(w.symbol):(0,n.none)(),L=typeof w.pointer=="number"?(0,n.some)(w.pointer):(0,n.none)(),K=new g(R,M,(0,n.some)(O),v,U,(0,n.some)(A),!0);return new m([],(0,n.some)(K),(0,n.none)(),L)}else if(T==="mint"){w=w;const O=typeof w.pointer=="number"?(0,n.some)(w.pointer):(0,n.none)();return new m([],(0,n.none)(),(0,n.some)(new o(w.block,w.txIdx)),O)}else throw new Error(`not ${T} support now`)}static decipher(w){const T=e.Transaction.fromHex(w),O=m.payload(T);if(O.isSome()){const A=m.integers(O.value()),R=E.from_integers(T,A.value()),M=R.getEtching(),v=R.getMint(),U=R.getPointer();return(0,n.some)(new m(R.edicts,M,v,U))}return(0,n.none)()}encipher(){const T=this.toMessage().toBuffer(),O=Buffer.from("6a5d","hex");let A;if(T.length<76)A=Buffer.alloc(1),A.writeUint8(T.length);else if(T.length<256)A=Buffer.alloc(2),A.writeUint8(76),A.writeUint8(T.length);else if(T.length<65536)A=Buffer.alloc(3),A.writeUint8(77),A.writeUint16LE(T.length);else if(T.length<4294967296)A=Buffer.alloc(5),A.writeUint8(78),A.writeUint32LE(T.length);else throw new Error("runestone too big!");return Buffer.concat([O,A,T])}static payload(w){for(const T of w.outs){const O=e.script.decompile(T.script);if(O[0]===e.script.OPS.OP_RETURN&&O[1]===m.MAGIC_NUMBER){for(let A=2;A<O.length;A++){const R=O[A];return R instanceof Uint8Array?(0,n.some)(Array.from(R)):(0,n.none)()}return(0,n.none)()}}return(0,n.none)()}static integers(w){let T=[],O=0;for(;O<w.length;){let{n:A,len:R}=(0,r.decodeLEB128)(w.slice(O));T.push(A),O+=R}return(0,n.some)(T)}toMessage(){let w=new Map;const T=this.etching.value();if(T){let R=1;if(T.terms.isSome()){let $=1<<a.Terms;R|=$}if(T.turbo){let $=1<<a.Turbo;R|=$}w.set(u.Flags,[BigInt(R)]);const M=T.rune.value();M!==null&&w.set(u.Rune,[BigInt(M.value)]);const v=T.divisibility.value();v!==null&&w.set(u.Divisibility,[BigInt(v)]);const U=T.spacers.value();U!==null&&w.set(u.Spacers,[BigInt(U)]);const L=T.symbol.value();L!==null&&w.set(u.Symbol,[BigInt(L.charCodeAt(0))]);const K=T.premine.value();K!==null&&w.set(u.Premine,[BigInt(K)]);const H=T.terms.value();if(H!==null){w.set(u.Amount,[BigInt(H.amount)]),w.set(u.Cap,[BigInt(H.cap)]);const $=H.height.start.value();$&&w.set(u.HeightStart,[BigInt($)]);const P=H.height.end.value();P&&w.set(u.HeightEnd,[BigInt(P)]);const D=H.offset.start.value();D&&w.set(u.OffsetStart,[BigInt(D)]);const F=H.offset.end.value();F&&w.set(u.OffsetEnd,[BigInt(F)])}}const O=this.mint.value();O!==null&&w.set(u.Mint,[BigInt(O.block),BigInt(O.idx)]);const A=this.pointer.value();return A!==null&&w.set(u.Pointer,[BigInt(A)]),new E(w,this.edicts,0)}}ze.Runestone=m,m.MAGIC_NUMBER=93;class E{constructor(w=new Map,T=[],O=0){this.fields=w,this.edicts=T,this.flaws=O}static from_integers(w,T){let O=new Map,A=[],R=0,M=!1;for(let v=0;v<T.length;){let U=T[v];if(Number(U)===u.Body){M=!0,v+=1;continue}if(M){let L=new o(0,0);for(const K of(0,i.chunks)(T.slice(v),4)){if(K.length!=4){R|=f.TrailingIntegers;break}let H=L.next(K[0],K[1]);if(!H.isSome()){R|=f.EdictRuneId;break}const $=c.from_integers(w,H.value(),K[2],K[3]);if(!$.isSome()){R|=f.EdictOutput;break}L=H.value(),A.push($.value())}v+=4}else{let L=T[v+1];const K=O.get(Number(U))||[];K.push(L),O.set(Number(U),K),v+=2}}return new E(O,A,R)}addFieldVal(w,T){const O=this.fields.get(Number(w))||[];O.push(T),this.fields.set(Number(w),O)}addEdict(w){this.edicts.push(w)}toBuffer(){const w=[];for(const[T,O]of this.fields)for(const A of O){const R=Buffer.alloc(1);R.writeUInt8(T),w.push(R),w.push(Buffer.from((0,r.encodeLEB128)(A)))}if(this.edicts.length>0){w.push(Buffer.from("00","hex")),this.edicts.sort((A,R)=>A.id.block==R.id.block?A.id.idx-R.id.idx:A.id.block-R.id.block);let T=0n,O=0n;for(let A=0;A<this.edicts.length;A++){const R=this.edicts[A];if(A==0)T=BigInt(R.id.block),O=BigInt(R.id.idx),w.push(Buffer.from((0,r.encodeLEB128)(T))),w.push(Buffer.from((0,r.encodeLEB128)(O)));else{const M=BigInt(R.id.block),v=BigInt(R.id.idx);if(M==T){const U=v-O;O=v,w.push(Buffer.from((0,r.encodeLEB128)(0n))),w.push(Buffer.from((0,r.encodeLEB128)(U)))}else{const U=M-T;T=M,O=v,w.push(Buffer.from((0,r.encodeLEB128)(U))),w.push(Buffer.from((0,r.encodeLEB128)(v)))}}w.push(Buffer.from((0,r.encodeLEB128)(BigInt(R.amount)))),w.push(Buffer.from((0,r.encodeLEB128)(BigInt(R.output))))}}return Buffer.concat(w)}getFlags(){return Number(this.fields.get(u.Flags))}hasFlags(w){const T=this.getFlags(),O=1<<w;return(T&O)!=0}getMint(){if(!this.fields.has(u.Mint))return(0,n.none)();const[w,T]=this.fields.get(u.Mint);return(0,n.some)(new o(Number(w),Number(T)))}getEtching(){if(!this.hasFlags(a.Etching))return(0,n.none)();const w=this.getDivisibility(),T=this.getPremine(),O=this.getRune(),A=this.getSpacers(),R=this.getSymbol(),M=this.getTerms(),v=this.hasFlags(a.Turbo);return(0,n.some)(new g(w,T,O,A,R,M,v))}getDivisibility(){if(!this.fields.has(u.Divisibility))return(0,n.none)();const[w]=this.fields.get(u.Divisibility);if(w>g.MAX_DIVISIBILITY)throw new Error("invalid divisibility");return(0,n.some)(Number(w))}getPremine(){if(!this.fields.has(u.Premine))return(0,n.none)();const[w]=this.fields.get(u.Premine);return(0,n.some)(Number(w))}getRune(){if(!this.fields.has(u.Rune))return(0,n.none)();const[w]=this.fields.get(u.Rune);return(0,n.some)(new p(w))}getSpacers(){if(!this.fields.has(u.Spacers))return(0,n.none)();const[w]=this.fields.get(u.Spacers);if(w>g.MAX_SPACERS)throw new Error("invalid spacers");return(0,n.some)(Number(w))}getHeightStart(){if(!this.fields.has(u.HeightStart))return(0,n.none)();const[w]=this.fields.get(u.HeightStart);return(0,n.some)(Number(w))}getHeightEnd(){if(!this.fields.has(u.HeightEnd))return(0,n.none)();const[w]=this.fields.get(u.HeightEnd);return(0,n.some)(Number(w))}getOffsetStart(){if(!this.fields.has(u.OffsetStart))return(0,n.none)();const[w]=this.fields.get(u.OffsetStart);return(0,n.some)(Number(w))}getOffsetEnd(){if(!this.fields.has(u.OffsetEnd))return(0,n.none)();const[w]=this.fields.get(u.OffsetEnd);return(0,n.some)(Number(w))}getCap(){if(!this.fields.has(u.Cap))return(0,n.none)();const[w]=this.fields.get(u.Cap);return(0,n.some)(Number(w))}getAmount(){if(!this.fields.has(u.Amount))return(0,n.none)();const[w]=this.fields.get(u.Amount);return(0,n.some)(Number(w))}getSymbol(){if(!this.fields.has(u.Symbol))return(0,n.none)();const[w]=this.fields.get(u.Symbol);return(0,n.some)(String.fromCharCode(Number(w)))}getTerms(){if(!this.hasFlags(a.Terms))return(0,n.none)();const w=this.getCap();if(!w.isSome())throw new Error("no cap field");const T=this.getAmount();if(!T.isSome())throw new Error("no amount field");const O=this.getHeightStart(),A=this.getHeightEnd(),R=this.getOffsetStart(),M=this.getOffsetEnd(),v=new l(O,A),U=new l(R,M);return(0,n.some)(new d(T.value(),w.value(),v,U))}getPointer(){if(!this.fields.has(u.Pointer))return(0,n.none)();const[w]=this.fields.get(u.Pointer);return(0,n.some)(Number(w))}}ze.Message=E;class _{constructor(w=new Map,T=Buffer.alloc(0)){this.fields=w,this.data=T}setContent(w,T){this.fields.set(1,Buffer.from(w,"utf8")),this.data=T}setRune(w){let O=(0,t.base26Encode)((0,s.removeSpacers)(w)).toString(16);O.length%2===1&&(O="0"+O),this.setField(_.Tag.RUNE,Buffer.from(O,"hex").reverse())}setField(w,T){this.fields.set(w,T)}static decipher(w,T){const R=e.Transaction.fromHex(w).ins[T].witness[1],M=e.script.decompile(R),v=new Map,U=[];let L=!1;for(let K=5;K<M.length-1;){const H=M[K];if(H===0){L=!0,K++;continue}else if(L)U.push(H),K++;else{const $=H-80,P=M[K+1];if(typeof P=="number"){const D=Buffer.alloc(1);D.writeUint8(P),v.set($,D)}else v.set($,P);K+=2}}return new _(v,Buffer.concat(U))}encipher(){const w=[];if(this.data&&this.data.length>0){w.push(Buffer.from("0063036f7264","hex")),Array.from(this.fields.entries()).sort((O,A)=>O[0]-A[0]).forEach(([O,A])=>{const R=Buffer.alloc(1);R.writeUInt8(O),w.push(Buffer.from("01","hex")),w.push(R),A.length!=1||A[0]!=0?w.push((0,i.toPushData)(A)):w.push(A)}),w.push(Buffer.from("00","hex"));const T=(0,i.chunks)(Array.from(this.data),520);for(const O of T)w.push((0,i.toPushData)(Buffer.from(O)))}else{w.push(Buffer.from("0063","hex"));const T=this.fields.get(_.Tag.RUNE);if(!T)throw new Error("No rune found!");w.push((0,i.toPushData)(T))}return w.push(Buffer.from("68","hex")),Buffer.concat(w)}}return ze.EtchInscription=_,_.Tag={CONTENT_TYPE:1,POINTER:2,PARENT:3,METADATA:5,METAPROTOCOL:7,CONTENT_ENCODING:9,DELEGATE:11,RUNE:13},ze}var py;function qA(){return py||(py=1,(function(e){var t=Ei&&Ei.__createBinding||(Object.create?(function(s,o,c,a){a===void 0&&(a=c);var u=Object.getOwnPropertyDescriptor(o,c);(!u||("get"in u?!o.__esModule:u.writable||u.configurable))&&(u={enumerable:!0,get:function(){return o[c]}}),Object.defineProperty(s,a,u)}):(function(s,o,c,a){a===void 0&&(a=c),s[a]=o[c]})),n=Ei&&Ei.__exportStar||function(s,o){for(var c in s)c!=="default"&&!Object.prototype.hasOwnProperty.call(o,c)&&t(o,s,c)};Object.defineProperty(e,"__esModule",{value:!0}),e.removeSpacers=e.getSpacersVal=e.applySpacers=e.some=e.none=void 0,n(MA(),e);var r=Qw();Object.defineProperty(e,"none",{enumerable:!0,get:function(){return r.none}}),Object.defineProperty(e,"some",{enumerable:!0,get:function(){return r.some}});var i=e1();Object.defineProperty(e,"applySpacers",{enumerable:!0,get:function(){return i.applySpacers}}),Object.defineProperty(e,"getSpacersVal",{enumerable:!0,get:function(){return i.getSpacersVal}}),Object.defineProperty(e,"removeSpacers",{enumerable:!0,get:function(){return i.removeSpacers}})})(Ei)),Ei}var ls=qA();function t1(e){if(e.length%2!==0)throw new Error("Hex string must have even length");const t=new Uint8Array(e.length/2);for(let n=0;n<e.length;n+=2)t[n/2]=parseInt(e.substr(n,2),16);return t}function Oh(e){return Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("")}function Bh(e){return e===ks.Mainnet?Rn:My}function Nh(e,t=ks.Testnet){const n=Bh(t);return vg(e,n)}function Mi(e){const t=Rn,n=My,r=X1;let i,s,o=mn.UNKNOWN;if(e.startsWith("bc1")||e.startsWith("tb1")||e.startsWith("bcrt1"))try{return s=Pl(e),s.version===0?s.data.length===20?o=mn.P2WPKH:o=mn.P2WSH:o=mn.P2TR,o}catch{return mn.UNKNOWN}else try{return i=Ag(e),i.version===t.pubKeyHash||i.version===n.pubKeyHash||i.version===r.pubKeyHash?o=mn.P2PKH:(i.version===t.scriptHash||(i.version,n.scriptHash),o=mn.P2SH_P2WPKH),o}catch{return mn.UNKNOWN}}function LA(e,t,n){const r=Nh(e,n),i=t.coins?.[0];return{txid:t.txid,vout:t.vout,satoshis:t.sats.toString(),height:void 0,runes:i?[{id:i.id,amount:i.value.toString()}]:[],address:e,scriptPk:Oh(r)}}async function n1(e,t){try{const n=t===ks.Mainnet?"https://mpc.omnity.network/utxo-status":"https://mpc.omnity.network/testnet4/utxo-status",r=await dt.post(n,e).then(c=>c.data);if(!r)return null;const i=JSON.stringify(r),o=new TextEncoder().encode(i);return Array.from(o)}catch{return null}}const VA=Object.freeze(Object.defineProperty({__proto__:null,bytesToHex:Oh,formatPoolUtxo:LA,getAddressType:Mi,getScriptByAddress:Nh,getUtxoProof:n1,hexToBytes:t1,toBitcoinNetwork:Bh},Symbol.toStringTag,{value:"Module"})),$A="kqs64-paaaa-aaaar-qamza-cai",DA="hvyp5-5yaaa-aaaao-qjxha-cai",Wc=BigInt(546),xn="0:0",jA="https://runescan-hasura-mainnet.omnity.network/v1/graphql",KA="https://runescan-hasura-testnet.omnity.network/v1/graphql",yy={P2PKH:148,P2SH_P2WPKH:91,P2WPKH:68,P2WSH:140,P2SH:108,P2TR:58,UNKNOWN:110},gy={P2PKH:34,P2SH_P2WPKH:32,P2WPKH:31,P2WSH:43,P2SH:32,P2TR:43,UNKNOWN:34},GA=new Set(["P2WPKH","P2WSH","P2SH_P2WPKH","P2TR"]),WA=2;class oi{psbt;client;inputAddressTypes=[];outputAddressTypes=[];config;userInputUtxoDusts=BigInt(0);intentions=[];txFee=BigInt(0);additionalDustNeeded=BigInt(0);inputUtxos=[];constructor(t,n){this.config=t,this.psbt=new wa({network:Bh(t.network)}),this.client=n}addInput(t){if(this.inputUtxos.findIndex(r=>r.txid===t.txid&&r.vout===t.vout)>=0)return;const{address:n}=t;this.psbt.data.addInput({hash:t.txid,index:t.vout,witnessUtxo:{value:BigInt(t.satoshis),script:t1(t.scriptPk)}}),this.inputAddressTypes.push(Mi(n)),this.inputUtxos.push(t),(n===this.config.address||n===this.config.paymentAddress)&&t.runes.length!==0&&(this.userInputUtxoDusts+=BigInt(t.satoshis))}addOutput(t,n){this.psbt.addOutput({address:t,value:n}),this.outputAddressTypes.push(Mi(t))}addScriptOutput(t){this.psbt.addOutput({script:t,value:BigInt(0)}),this.outputAddressTypes.push({OpReturn:BigInt(t.length)})}selectRuneUtxos(t,n,r){const i=[];if(r==BigInt(0))return i;for(const s of t)if(s.runes.length){const o=s.runes.find(c=>c.id==n);if(o&&BigInt(o.amount)==r){i.push(s);break}}if(i.length==0){let s=BigInt(0);for(const o of t)if(o.runes.forEach(c=>{c.id==n&&(s=s+BigInt(c.amount))}),i.push(o),s>=r)break;if(s<r)throw new Error("INSUFFICIENT_RUNE_UTXOs")}return i}selectBtcUtxos(t,n,r=!1){const i=[];if(n<=BigInt(0))return i;let s=BigInt(0);for(const o of t)if(!(o.runes.length&&!r)&&(s+=BigInt(o.satoshis),i.push(o),s>=n))break;if(s<n)throw new Error(`Insufficient BTC UTXOs: need ${n}, have ${s}`);return i}async addBtcAndFees(t,n){const r=this.config.paymentAddress,i=this.additionalDustNeeded,s=Mi(r),o=this.userInputUtxoDusts,c=this.config.feeRate;this.outputAddressTypes.push(s);let a=!0,u=BigInt(0),f=BigInt(0),l=[],d=BigInt(0),p=BigInt(0);const g=[...this.inputAddressTypes];do{if(u=f,c!==void 0){const _=oi.estimateTxVirtualSize(this.inputAddressTypes,this.outputAddressTypes);f=BigInt(Math.round(c*_))}else f=(await this.client.orchestrator.estimate_min_tx_fee({input_types:this.inputAddressTypes,pool_address:this.intentions.map(S=>S.poolAddress),output_types:this.outputAddressTypes})).Ok;if(d=n+f+i-o,f>u&&d>0){const _=this.selectBtcUtxos(t,d);if(_.length===0)throw new Error("INSUFFICIENT_BTC_UTXOs");this.inputAddressTypes=g.concat(_.map(()=>s));const S=_.reduce((w,T)=>w+BigInt(T.satoshis),BigInt(0));S-d>0&&S-d>Wc||(this.outputAddressTypes.pop(),a=!1),l=_}}while(f>u&&d>0);this.inputAddressTypes=[...g],a&&this.outputAddressTypes.pop();let m=BigInt(0);l.forEach(_=>{this.addInput(_),m+=BigInt(_.satoshis)});const E=m-d;if(E<0)throw new Error("Insufficient UTXO(s)");E>Wc?this.addOutput(r,E):E>BigInt(0)&&(p=E),this.txFee=p+f}async getInvolvedAddressUtxos(){const t={},n={},r=new Map,i=this.intentions.map(o=>o.poolAddress);r.set(this.config.paymentAddress,{needBtc:!0,runeIds:new Set});const s=o=>{const c=o.trim();return r.has(c)||r.set(c,{needBtc:!1,runeIds:new Set}),r.get(c)};for(const o of this.intentions){for(const u of o.inputCoins){const f=s(u.from);u.coin.id===xn?f.needBtc=!0:f.runeIds.add(u.coin.id)}for(const u of o.outputCoins){const f=s(u.to);u.coin.id===xn?f.needBtc=!0:f.runeIds.add(u.coin.id)}const c=s(o.poolAddress),a=[...o.inputCoins.map(u=>u.coin.id),...o.outputCoins.map(u=>u.coin.id)];for(const u of a)u===xn?c.needBtc=!0:c.runeIds.add(u)}return await Promise.all(Array.from(r.entries()).map(async([o,c])=>{if(c.needBtc)try{t[o]=await this.client.getBtcUtxos(o,!i.includes(o))}catch{t[o]=[]}c.runeIds.size>0&&(n[o]={},await Promise.all(Array.from(c.runeIds).map(async a=>{try{n[o][a]=await this.client.getRuneUtxos(o,a)}catch{n[o][a]=[]}})))})),{btc:t,rune:n}}addInputsAndCalculateOutputs(t){if(!this.intentions.length)throw new Error("No intentions added");const n=this.intentions.map(c=>c.poolAddress),r={},i={},s=new Map,o=new Set;this.intentions.forEach(({poolAddress:c,inputCoins:a,outputCoins:u,poolUtxos:f})=>{const l=[...a.filter(({from:p})=>!n.includes(p))],d=[...u];if(l.forEach(({coin:p,from:g})=>{!u.find(m=>m.coin.id===p.id)&&!(n.includes(g)&&g!==c)&&d.push({coin:p,to:c})}),d.forEach(({coin:p})=>{a.find(g=>g.coin.id===p.id)||l.push({coin:p,from:c})}),l.length===0&&d.length===0&&f?.length){const p=s.get(c);s.set(c,p?p.concat(f):[...f]),o.add(c);return}l.forEach(({coin:p,from:g})=>{r[g]??={},r[g][p.id]=(r[g][p.id]??BigInt(0))+BigInt(p.value)}),d.forEach(({coin:p,to:g})=>{i[g]??={},i[g][p.id]=(i[g][p.id]??BigInt(0))+BigInt(p.value)})});for(const[c,a]of s.entries()){if(!a.length)continue;i[c]??={};const u=i[c];a.forEach(f=>{this.addInput(f),u[xn]=(u[xn]??BigInt(0))+BigInt(f.satoshis),f.runes.forEach(l=>{u[l.id]=(u[l.id]??BigInt(0))+BigInt(l.amount)})})}for(const[c,a]of Object.entries(r))for(const[u,f]of Object.entries(a))if(u===xn){if(c===this.config.paymentAddress){i[c]??={},i[c][u]=(i[c][u]??BigInt(0))-f;continue}const l=t.btc[c]||[],d=this.selectBtcUtxos(l,f,n.includes(c)),g=d.reduce((m,E)=>m+BigInt(E.satoshis),BigInt(0))-f;g>BigInt(0)&&(i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+g),d.forEach(m=>{this.addInput(m),n.includes(c)&&m.runes.forEach(E=>{i[c]??={},i[c][E.id]=(i[c][E.id]??BigInt(0))+BigInt(E.amount)})})}else{const l=t.rune[c]?.[u]||[],d=this.selectRuneUtxos(l,u,f),g=d.reduce((m,E)=>{const _=E.runes.find(S=>S.id===u);return m+BigInt(_?.amount??0)},BigInt(0))-f;n.includes(c)?(i[c]??={},i[c][u]=(i[c][u]??BigInt(0))-f):g>BigInt(0)&&(i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+g),d.forEach(m=>this.addInput(m))}for(const[c,a]of Object.entries(i))if(!(r[c]||!n.includes(c)||o.has(c)))for(const[u]of Object.entries(a))if(u===xn){const f=t.btc[c]||[],l=f.reduce((d,p)=>d+BigInt(p.satoshis),BigInt(0));i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+l,f.forEach(d=>{this.addInput(d),d.runes.forEach(p=>{i[c]??={},i[c][p.id]=(i[c][p.id]??BigInt(0))+BigInt(p.amount)})})}else{const f=t.rune[c]?.[u]||[],l=f.reduce((d,p)=>{const g=p.runes.find(m=>m.id===u);return d+BigInt(g?.amount??0)},BigInt(0));i[c]??={},i[c][u]=(i[c][u]??BigInt(0))+l,f.forEach(d=>this.addInput(d))}return i}addOutputs(t){const n=this.config.mergeSelfRuneBtcOutputs===!0,r=new Set;for(const[,s]of Object.entries(t))for(const o of Object.keys(s))o!==xn&&r.add(o);const i=Array.from(r);if(i.length>0){const s=[],o=[];let c=1;i.forEach(u=>{const f=new ls.RuneId(Number(u.split(":")[0]),Number(u.split(":")[1]));for(const[l,d]of Object.entries(t)){const p=d[u]??BigInt(0);p>BigInt(0)&&(s.push(new ls.Edict(f,p,c)),o.push(l),c++)}});const a=new ls.Runestone(s,ls.none(),ls.none(),ls.none());this.addScriptOutput(new Uint8Array(a.encipher())),o.forEach(u=>{const f=t[u]?.[xn]??BigInt(0),l=u===this.config.address,d=f>BigInt(0)&&(!l||n),p=d?f:Wc;this.addOutput(u,p),d?delete t[u][xn]:this.additionalDustNeeded+=Wc})}for(const[s,o]of Object.entries(t)){const c=o[xn]??BigInt(0);c>BigInt(0)&&this.addOutput(s,c)}}addIntention(t){this.intentions.push(t)}async build(){const t=await this.getInvolvedAddressUtxos();for(const u of this.intentions){const f=u.poolUtxos;if(!f)continue;t.btc[u.poolAddress]=f,t.rune[u.poolAddress]={};const l=t.rune[u.poolAddress];for(const d of f)for(const p of d.runes)(l[p.id]??=[]).push(d)}const n=this.addInputsAndCalculateOutputs(t);this.addOutputs(n);const r=this.config.paymentAddress,i=t.btc[r]??[],s=n[r]?.[xn]??BigInt(0);await this.addBtcAndFees(i,s<0?-s:BigInt(0));const c=this.psbt.__CACHE.__TX.clone();for(let u=0;u<this.inputUtxos.length;u++){const l=this.inputUtxos[u].address;if(l!==this.config.paymentAddress||l!==this.config.address)continue;const d=this.psbt.data.inputs[u].redeemScript,p=Mi(l);if(d&&p===mn.P2SH_P2WPKH){const g=nn([d]);c.setInputScript(u,g)}}const a=c.getId();return{psbt:this.psbt,txid:a,fee:this.txFee}}static estimateTxVirtualSize(t,n){const r=t.length,i=n.length;let s=8+oi.varIntSize(r)+oi.varIntSize(i),o=!1;for(const c of t){const a=oi.parseAddressType(c);if(a.key==="OpReturn")continue;const u=yy[a.key]??yy.UNKNOWN;s+=u,GA.has(a.key)&&(o=!0)}for(const c of n){const a=oi.parseAddressType(c);if(a.key==="OpReturn"){const f=Math.max(0,a.opReturnLength??0);s+=11+f;continue}const u=gy[a.key]??gy.UNKNOWN;s+=u}return o&&(s+=1),s+WA}static parseAddressType(t){if("OpReturn"in t){const r=t.OpReturn!==void 0?Number(t.OpReturn):0;return{key:"OpReturn",opReturnLength:Number.isFinite(r)?r:0}}const n=["P2PKH","P2SH_P2WPKH","P2WPKH","P2WSH","P2SH","P2TR"];for(const r of n)if(r in t)return{key:r};return{key:"UNKNOWN"}}static varIntSize(t){return t<253?1:t<=65535?3:t<=4294967295?5:9}async send(t){if(!this.intentions.length)throw new Error("No itentions added");const n=this.inputUtxos.filter(i=>i.address===this.config.paymentAddress||i.address===this.config.address),r=await n1(n,this.config.network);if(!r)throw new Error("Failed to get utxo proof");return this.client.orchestrator.invoke({intention_set:{tx_fee_in_sats:this.txFee,initiator_address:this.config.paymentAddress,intentions:this.intentions.map(({action:i,actionParams:s,poolAddress:o,inputCoins:c,outputCoins:a,exchangeId:u,nonce:f})=>({exchange_id:u??this.config.exchangeId,input_coins:c.filter(l=>l.from!==o),output_coins:a,pool_address:o,action:i,action_params:s??"",pool_utxo_spent:[],pool_utxo_received:[],nonce:f}))},initiator_utxo_proof:r,psbt_hex:t,client_info:[this.config.clientInfo??""]}).then(i=>{if(i?.Ok)return i.Ok;{const s=i?.Err??{},o=Object.keys(s)[0],c=s[o];throw new Error(c?o==="ErrorOccurredDuringExecution"?`${o}: ${c.execution_steps?.[0]?.result?.Err??"Unknown Error"}`:`Invoke Error: ${JSON.stringify(i)}`:`Invoke Error: ${JSON.stringify(i)}`)}})}}class Os extends Error{response;request;constructor(t,n){const r=`${Os.extractMessage(t)}: ${JSON.stringify({response:t,request:n})}`;super(r),Object.setPrototypeOf(this,Os.prototype),this.response=t,this.request=n,typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(this,Os)}static extractMessage(t){return t.errors?.[0]?.message??`GraphQL Error (Code: ${String(t.status)})`}}const my=e=>e.toUpperCase(),Cf=e=>typeof e=="function"?e():e,r1=(e,t)=>e.map((n,r)=>[n,t[r]]),hs=e=>{let t={};return e instanceof Headers?t=XA(e):Array.isArray(e)?e.forEach(([n,r])=>{n&&r!==void 0&&(t[n]=r)}):e&&(t=e),t},XA=e=>{const t={};return e.forEach((n,r)=>{t[r]=n}),t},zA=e=>{try{const t=e();return YA(t)?t.catch(n=>wy(n)):t}catch(t){return wy(t)}},wy=e=>e instanceof Error?e:new Error(String(e)),YA=e=>typeof e=="object"&&e!==null&&"then"in e&&typeof e.then=="function"&&"catch"in e&&typeof e.catch=="function"&&"finally"in e&&typeof e.finally=="function",kh=e=>{throw new Error(`Unhandled case: ${String(e)}`)},ia=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),ZA=(e,t)=>e.documents?e:{documents:e,requestHeaders:t,signal:void 0},JA=(e,t,n)=>e.query?e:{query:e,variables:t,requestHeaders:n,signal:void 0};function sa(e,t){if(!!!e)throw new Error(t)}function QA(e){return typeof e=="object"&&e!==null}function ev(e,t){if(!!!e)throw new Error("Unexpected invariant triggered.")}const tv=/\r\n|[\n\r]/g;function yl(e,t){let n=0,r=1;for(const i of e.body.matchAll(tv)){if(typeof i.index=="number"||ev(!1),i.index>=t)break;n=i.index+i[0].length,r+=1}return{line:r,column:t+1-n}}function nv(e){return i1(e.source,yl(e.source,e.start))}function i1(e,t){const n=e.locationOffset.column-1,r="".padStart(n)+e.body,i=t.line-1,s=e.locationOffset.line-1,o=t.line+s,c=t.line===1?n:0,a=t.column+c,u=`${e.name}:${o}:${a}
|
|
77
77
|
`,f=r.split(/\r\n|[\n\r]/g),l=f[i];if(l.length>120){const d=Math.floor(a/80),p=a%80,g=[];for(let m=0;m<l.length;m+=80)g.push(l.slice(m,m+80));return u+by([[`${o} |`,g[0]],...g.slice(1,d+1).map(m=>["|",m]),["|","^".padStart(p)],["|",g[d+1]]])}return u+by([[`${o-1} |`,f[i-1]],[`${o} |`,l],["|","^".padStart(a)],[`${o+1} |`,f[i+1]]])}function by(e){const t=e.filter(([r,i])=>i!==void 0),n=Math.max(...t.map(([r])=>r.length));return t.map(([r,i])=>r.padStart(n)+(i?" "+i:"")).join(`
|
|
78
78
|
`)}function rv(e){const t=e[0];return t==null||"kind"in t||"length"in t?{nodes:t,source:e[1],positions:e[2],path:e[3],originalError:e[4],extensions:e[5]}:t}class Ph extends Error{constructor(t,...n){var r,i,s;const{nodes:o,source:c,positions:a,path:u,originalError:f,extensions:l}=rv(n);super(t),this.name="GraphQLError",this.path=u??void 0,this.originalError=f??void 0,this.nodes=Ey(Array.isArray(o)?o:o?[o]:void 0);const d=Ey((r=this.nodes)===null||r===void 0?void 0:r.map(g=>g.loc).filter(g=>g!=null));this.source=c??(d==null||(i=d[0])===null||i===void 0?void 0:i.source),this.positions=a??d?.map(g=>g.start),this.locations=a&&c?a.map(g=>yl(c,g)):d?.map(g=>yl(g.source,g.start));const p=QA(f?.extensions)?f?.extensions:void 0;this.extensions=(s=l??p)!==null&&s!==void 0?s:Object.create(null),Object.defineProperties(this,{message:{writable:!0,enumerable:!0},name:{enumerable:!1},nodes:{enumerable:!1},source:{enumerable:!1},positions:{enumerable:!1},originalError:{enumerable:!1}}),f!=null&&f.stack?Object.defineProperty(this,"stack",{value:f.stack,writable:!0,configurable:!0}):Error.captureStackTrace?Error.captureStackTrace(this,Ph):Object.defineProperty(this,"stack",{value:Error().stack,writable:!0,configurable:!0})}get[Symbol.toStringTag](){return"GraphQLError"}toString(){let t=this.message;if(this.nodes)for(const n of this.nodes)n.loc&&(t+=`
|
|
79
79
|
|
package/dist/ree-sdk.es.js
CHANGED
|
@@ -28380,7 +28380,7 @@ class Ri {
|
|
|
28380
28380
|
},
|
|
28381
28381
|
initiator_utxo_proof: r,
|
|
28382
28382
|
psbt_hex: t,
|
|
28383
|
-
client_info: this.config.clientInfo ?? ""
|
|
28383
|
+
client_info: [this.config.clientInfo ?? ""]
|
|
28384
28384
|
}).then((i) => {
|
|
28385
28385
|
if (i?.Ok)
|
|
28386
28386
|
return i.Ok;
|