@babylonlabs-io/ts-sdk 0.33.0 → 0.33.1
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/{PeginManager-DUMF84XK.cjs → PeginManager-D2pBM0E-.cjs} +2 -2
- package/dist/{PeginManager-DUMF84XK.cjs.map → PeginManager-D2pBM0E-.cjs.map} +1 -1
- package/dist/{PeginManager-BMrWtTji.js → PeginManager-LeGYbRN2.js} +3 -3
- package/dist/{PeginManager-BMrWtTji.js.map → PeginManager-LeGYbRN2.js.map} +1 -1
- package/dist/{buildAndBroadcastRefund-6pR681fI.cjs → buildAndBroadcastRefund-BXjYAqdb.cjs} +2 -2
- package/dist/{buildAndBroadcastRefund-6pR681fI.cjs.map → buildAndBroadcastRefund-BXjYAqdb.cjs.map} +1 -1
- package/dist/{buildAndBroadcastRefund-Ba56IKL7.js → buildAndBroadcastRefund-CK8MbGQh.js} +2 -2
- package/dist/{buildAndBroadcastRefund-Ba56IKL7.js.map → buildAndBroadcastRefund-CK8MbGQh.js.map} +1 -1
- package/dist/challengeAssert-06GLZtV8.cjs +2 -0
- package/dist/challengeAssert-06GLZtV8.cjs.map +1 -0
- package/dist/{challengeAssert-DSlCIBoT.js → challengeAssert-KGVKQh0J.js} +64 -70
- package/dist/challengeAssert-KGVKQh0J.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +4 -4
- package/dist/{noPayout-DohtepqZ.js → noPayout-BmMd4NNH.js} +44 -50
- package/dist/{noPayout-DohtepqZ.js.map → noPayout-BmMd4NNH.js.map} +1 -1
- package/dist/noPayout-Bp2TYA_X.cjs +2 -0
- package/dist/{noPayout-CDhbGn_B.cjs.map → noPayout-Bp2TYA_X.cjs.map} +1 -1
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +4 -4
- package/dist/tbv/core/primitives/index.cjs +1 -1
- package/dist/tbv/core/primitives/index.js +2 -2
- package/dist/tbv/core/primitives/psbt/__tests__/peginInput.test.d.ts +3 -3
- package/dist/tbv/core/primitives/psbt/payout.d.ts +3 -2
- package/dist/tbv/core/primitives/psbt/payout.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts +11 -5
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts.map +1 -1
- package/dist/tbv/core/services/index.cjs +1 -1
- package/dist/tbv/core/services/index.js +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +4 -4
- package/package.json +1 -1
- package/dist/challengeAssert-DSlCIBoT.js.map +0 -1
- package/dist/challengeAssert-DuhkzZG-.cjs +0 -2
- package/dist/challengeAssert-DuhkzZG-.cjs.map +0 -1
- package/dist/noPayout-CDhbGn_B.cjs +0 -2
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var Qt=Object.defineProperty;var Jt=(e,t,n)=>t in e?Qt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var b=(e,t,n)=>Jt(e,typeof t!="symbol"?t+"":t,n);const V=require("bitcoinjs-lib"),K=require("buffer"),h=require("./sha2-CsTynrfJ.cjs"),I=require("viem"),ct=require("@babylonlabs-io/babylon-tbv-rust-wasm"),O=require("./challengeAssert-DuhkzZG-.cjs"),d=require("./bitcoin-EYBKDtEW.cjs"),te=require("./signing-DHSXjhLM.cjs"),lt=require("./validation-u8W7Lp2x.cjs"),ee=require("./buildAndBroadcastRefund-6pR681fI.cjs"),R=require("./psbtInputFields-BLi7Ta-T.cjs"),ut=require("./fundPeginTransaction-DaWoYCgO.cjs"),_t=require("./vault-registry-reader-DdUgC5IF.cjs"),$=require("./types-C2IUB0Jd.cjs");function ne(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const s=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,s.get?s:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const Ht=ne(V),W=BigInt(2**32-1),ht=BigInt(32);function se(e,t=!1){return t?{h:Number(e&W),l:Number(e>>ht&W)}:{h:Number(e>>ht&W)|0,l:Number(e&W)|0}}function re(e,t=!1){const n=e.length;let s=new Uint32Array(n),r=new Uint32Array(n);for(let o=0;o<n;o++){const{h:i,l:a}=se(e[o],t);[s[o],r[o]]=[i,a]}return[s,r]}const oe=(e,t,n)=>e<<n|t>>>32-n,ie=(e,t,n)=>t<<n|e>>>32-n,ae=(e,t,n)=>t<<n-32|e>>>64-n,ce=(e,t,n)=>e<<n-32|t>>>64-n,le=106,ue=32,dt=34;function he(e,t,n){const s=d.stripHexPrefix(e),r=Ht.Transaction.fromHex(s);if(r.outs.length<=t)throw new Error(`Pre-PegIn auth-anchor OP_RETURN missing: tx has ${r.outs.length} outputs, expected at least ${t+1} (vault outputs + OP_RETURN)`);const o=r.outs[t],i=o.script;if(i.length!==dt||i[0]!==le||i[1]!==ue)throw new Error(`Pre-PegIn auth-anchor OP_RETURN at vout ${t} has unexpected script encoding (got ${i.length}-byte script with prefix 0x${i.slice(0,Math.min(2,i.length)).toString("hex")}; expected ${dt}-byte OP_RETURN + PUSH32 layout)`);const a=i.slice(2).toString("hex").toLowerCase();if(a!==n.toLowerCase())throw new Error(`Pre-PegIn auth-anchor OP_RETURN payload mismatch at vout ${t}: tx pushes ${a}, expected ${n}`);if(o.value!==0)throw new Error(`Pre-PegIn auth-anchor OP_RETURN at vout ${t} has non-zero value ${o.value}; OP_RETURN outputs must be 0-value`)}class vt{constructor(t,n){b(this,"oHash");b(this,"iHash");b(this,"blockLen");b(this,"outputLen");b(this,"finished",!1);b(this,"destroyed",!1);if(h.ahash(t),h.abytes(n,void 0,"key"),this.iHash=t.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const s=this.blockLen,r=new Uint8Array(s);r.set(n.length>s?t.create().update(n).digest():n);for(let o=0;o<r.length;o++)r[o]^=54;this.iHash.update(r),this.oHash=t.create();for(let o=0;o<r.length;o++)r[o]^=106;this.oHash.update(r),h.clean(r)}update(t){return h.aexists(this),this.iHash.update(t),this}digestInto(t){h.aexists(this),h.abytes(t,this.outputLen,"output"),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));const{oHash:n,iHash:s,finished:r,destroyed:o,blockLen:i,outputLen:a}=this;return t=t,t.finished=r,t.destroyed=o,t.blockLen=i,t.outputLen=a,t.oHash=n._cloneInto(t.oHash),t.iHash=s._cloneInto(t.iHash),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const It=(e,t,n)=>new vt(e,t).update(n).digest();It.create=(e,t)=>new vt(e,t);const z=Uint8Array.of(0),ft=Uint8Array.of();function st(e,t,n,s=32){h.ahash(e),h.anumber(s,"length");const r=e.outputLen;if(s>255*r)throw new Error("Length must be <= 255*HashLen");const o=Math.ceil(s/r);n===void 0?n=ft:h.abytes(n,void 0,"info");const i=new Uint8Array(o*r),a=It.create(e,t),l=a._cloneInto(),u=new Uint8Array(a.outputLen);for(let c=0;c<o;c++)z[0]=c+1,l.update(c===0?ft:u).update(n).update(z).digestInto(u),i.set(u,r*c),a._cloneInto(l);return a.destroy(),l.destroy(),h.clean(u,z),i.slice(0,s)}const G=new TextEncoder().encode("babylonvault"),gt=255,pt=65535,mt=2,de="hashlock",fe="auth-anchor",ge="wots-seed";function At(e){if(!Number.isInteger(e)||e<0||e>4294967295)throw new Error(`i2osp4: value must be a u32, got ${e}`);const t=new Uint8Array(4);return t[0]=e>>>24&255,t[1]=e>>>16&255,t[2]=e>>>8&255,t[3]=e&255,t}function rt(e,t=new Uint8Array(0)){const n=new TextEncoder().encode(e);if(n.length===0||n.length>gt)throw new Error(`info: label length must be in [1, ${gt}], got ${n.length}`);if(t.length>pt)throw new Error(`info: ctx length must be in [0, ${pt}], got ${t.length}`);const s=G.length+1+n.length+mt+t.length,r=new Uint8Array(s);let o=0;return r.set(G,o),o+=G.length,r[o]=n.length,o+=1,r.set(n,o),o+=n.length,r[o]=t.length>>>8&255,r[o+1]=t.length&255,o+=mt,r.set(t,o),r}const xt=32,pe=32,me=32,xe=64;function ot(e){if(e.length!==xt)throw new Error(`vault-secrets: root must be exactly ${xt} bytes, got ${e.length}`)}function Bt(e){return ot(e),st(h.sha256,e,rt(fe),pe)}function St(e,t){return ot(e),st(h.sha256,e,rt(de,At(t)),me)}function Ct(e,t){return ot(e),st(h.sha256,e,rt(ge,At(t)),xe)}const N=32,Z=32,tt=36,Ot=32,D=4,be=D+N+D+Ot;function et(e,t,n){e[t]=n>>>24&255,e[t+1]=n>>>16&255,e[t+2]=n>>>8&255,e[t+3]=n&255}function we(e){if(e.txid.length!==Z)throw new Error(`outpoint.txid must be exactly ${Z} bytes, got ${e.txid.length}`);if(!Number.isInteger(e.vout)||e.vout<0||e.vout>4294967295)throw new Error(`outpoint.vout must be a u32, got ${e.vout}`);const t=new Uint8Array(tt);return t.set(e.txid,0),et(t,Z,e.vout),t}function bt(e,t){const n=Math.min(e.length,t.length);for(let s=0;s<n;s++)if(e[s]!==t[s])return e[s]-t[s];return e.length-t.length}function Rt(e){if(e.length===0)throw new Error("buildFundingOutpointsCommitment: outpoints must be non-empty");const t=e.map(we);t.sort(bt);for(let s=1;s<t.length;s++)if(bt(t[s-1],t[s])===0)throw new Error("buildFundingOutpointsCommitment: duplicate outpoint detected");const n=new Uint8Array(t.length*tt);for(let s=0;s<t.length;s++)n.set(t[s],s*tt);return h.sha256(n)}function $t(e){if(e.depositorBtcPubkey.length!==N)throw new Error(`vaultContext: depositorBtcPubkey must be exactly ${N} bytes, got ${e.depositorBtcPubkey.length}`);const t=Rt(e.fundingOutpoints),n=new Uint8Array(be);let s=0;return et(n,s,N),s+=D,n.set(e.depositorBtcPubkey,s),s+=N,et(n,s,Ot),s+=D,n.set(t,s),n}const Ut="babylon-vault",Lt=32,wt=Lt*2,ye=/^[0-9a-f]+$/;async function Nt(e,t){const n=$t(t),s=d.uint8ArrayToHex(n),r=await e.deriveContextHash(Ut,s);if(typeof r!="string")throw new Error(`deriveVaultRoot: wallet must return a string, got ${typeof r}`);if(r.length!==wt)throw new Error(`deriveVaultRoot: wallet must return a ${wt}-character hex string (${Lt} bytes), got length ${r.length}`);if(!ye.test(r))throw new Error("deriveVaultRoot: wallet must return lowercase hex per derive-context-hash.md §2.1; got value with non-lowercase or non-hex characters");return d.hexToUint8Array(r)}function Pe(e){if(!e)throw new Error("Pre-pegin transaction hex is empty");const t=e.startsWith("0x")?e.slice(2):e,n=V.Transaction.fromHex(t);if(n.ins.length===0)throw new Error("Pre-pegin transaction has no inputs");return n.ins.map(s=>({txid:Uint8Array.from(K.Buffer.from(s.hash).reverse()),vout:s.index}))}const Ee=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),Vt=Uint8Array.from(new Array(16).fill(0).map((e,t)=>t)),Te=Vt.map(e=>(9*e+5)%16),Wt=(()=>{const n=[[Vt],[Te]];for(let s=0;s<4;s++)for(let r of n)r.push(r[s].map(o=>Ee[o]));return n})(),Mt=Wt[0],Ft=Wt[1],Xt=[[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8],[12,13,11,15,6,9,9,7,12,15,11,13,7,8,7,7],[13,15,14,11,7,7,6,8,13,14,13,12,5,5,6,9],[14,11,12,14,8,6,5,5,15,12,15,14,9,9,8,6],[15,12,13,13,9,5,8,6,14,11,12,11,8,6,5,5]].map(e=>Uint8Array.from(e)),ke=Mt.map((e,t)=>e.map(n=>Xt[t][n])),_e=Ft.map((e,t)=>e.map(n=>Xt[t][n])),He=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),ve=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function yt(e,t,n,s){return e===0?t^n^s:e===1?t&n|~t&s:e===2?(t|~n)^s:e===3?t&s|n&~s:t^(n|~s)}const M=new Uint32Array(16);class Ie extends h.HashMD{constructor(){super(64,20,8,!0);b(this,"h0",1732584193);b(this,"h1",-271733879);b(this,"h2",-1732584194);b(this,"h3",271733878);b(this,"h4",-1009589776)}get(){const{h0:n,h1:s,h2:r,h3:o,h4:i}=this;return[n,s,r,o,i]}set(n,s,r,o,i){this.h0=n|0,this.h1=s|0,this.h2=r|0,this.h3=o|0,this.h4=i|0}process(n,s){for(let f=0;f<16;f++,s+=4)M[f]=n.getUint32(s,!0);let r=this.h0|0,o=r,i=this.h1|0,a=i,l=this.h2|0,u=l,c=this.h3|0,w=c,y=this.h4|0,P=y;for(let f=0;f<5;f++){const p=4-f,_=He[f],A=ve[f],g=Mt[f],T=Ft[f],m=ke[f],k=_e[f];for(let E=0;E<16;E++){const x=h.rotl(r+yt(f,i,l,c)+M[g[E]]+_,m[E])+y|0;r=y,y=c,c=h.rotl(l,10)|0,l=i,i=x}for(let E=0;E<16;E++){const x=h.rotl(o+yt(p,a,u,w)+M[T[E]]+A,k[E])+P|0;o=P,P=w,w=h.rotl(u,10)|0,u=a,a=x}}this.set(this.h1+l+w|0,this.h2+c+P|0,this.h3+y+o|0,this.h4+r+a|0,this.h0+i+u|0)}roundClean(){h.clean(M)}destroy(){this.destroyed=!0,h.clean(this.buffer),this.set(0,0,0,0,0)}}const Ae=h.createHasher(()=>new Ie),Be=BigInt(0),U=BigInt(1),Se=BigInt(2),Ce=BigInt(7),Oe=BigInt(256),Re=BigInt(113),Kt=[],Dt=[],qt=[];for(let e=0,t=U,n=1,s=0;e<24;e++){[n,s]=[s,(2*n+3*s)%5],Kt.push(2*(5*s+n)),Dt.push((e+1)*(e+2)/2%64);let r=Be;for(let o=0;o<7;o++)t=(t<<U^(t>>Ce)*Re)%Oe,t&Se&&(r^=U<<(U<<BigInt(o))-U);qt.push(r)}const jt=re(qt,!0),$e=jt[0],Ue=jt[1],Pt=(e,t,n)=>n>32?ae(e,t,n):oe(e,t,n),Et=(e,t,n)=>n>32?ce(e,t,n):ie(e,t,n);function Le(e,t=24){const n=new Uint32Array(10);for(let s=24-t;s<24;s++){for(let i=0;i<10;i++)n[i]=e[i]^e[i+10]^e[i+20]^e[i+30]^e[i+40];for(let i=0;i<10;i+=2){const a=(i+8)%10,l=(i+2)%10,u=n[l],c=n[l+1],w=Pt(u,c,1)^n[a],y=Et(u,c,1)^n[a+1];for(let P=0;P<50;P+=10)e[i+P]^=w,e[i+P+1]^=y}let r=e[2],o=e[3];for(let i=0;i<24;i++){const a=Dt[i],l=Pt(r,o,a),u=Et(r,o,a),c=Kt[i];r=e[c],o=e[c+1],e[c]=l,e[c+1]=u}for(let i=0;i<50;i+=10){for(let a=0;a<10;a++)n[a]=e[i+a];for(let a=0;a<10;a++)e[i+a]^=~n[(a+2)%10]&n[(a+4)%10]}e[0]^=$e[s],e[1]^=Ue[s]}h.clean(n)}class it{constructor(t,n,s,r=!1,o=24){b(this,"state");b(this,"pos",0);b(this,"posOut",0);b(this,"finished",!1);b(this,"state32");b(this,"destroyed",!1);b(this,"blockLen");b(this,"suffix");b(this,"outputLen");b(this,"enableXOF",!1);b(this,"rounds");if(this.blockLen=t,this.suffix=n,this.outputLen=s,this.enableXOF=r,this.rounds=o,h.anumber(s,"outputLen"),!(0<t&&t<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=h.u32(this.state)}clone(){return this._cloneInto()}keccak(){h.swap32IfBE(this.state32),Le(this.state32,this.rounds),h.swap32IfBE(this.state32),this.posOut=0,this.pos=0}update(t){h.aexists(this),h.abytes(t);const{blockLen:n,state:s}=this,r=t.length;for(let o=0;o<r;){const i=Math.min(n-this.pos,r-o);for(let a=0;a<i;a++)s[this.pos++]^=t[o++];this.pos===n&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:n,pos:s,blockLen:r}=this;t[s]^=n,(n&128)!==0&&s===r-1&&this.keccak(),t[r-1]^=128,this.keccak()}writeInto(t){h.aexists(this,!1),h.abytes(t),this.finish();const n=this.state,{blockLen:s}=this;for(let r=0,o=t.length;r<o;){this.posOut>=s&&this.keccak();const i=Math.min(s-this.posOut,o-r);t.set(n.subarray(this.posOut,this.posOut+i),r),this.posOut+=i,r+=i}return t}xofInto(t){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(t)}xof(t){return h.anumber(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(h.aoutput(t,this),this.finished)throw new Error("digest() was already called");return this.writeInto(t),this.destroy(),t}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,h.clean(this.state)}_cloneInto(t){const{blockLen:n,suffix:s,outputLen:r,rounds:o,enableXOF:i}=this;return t||(t=new it(n,s,r,i,o)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=o,t.suffix=s,t.outputLen=r,t.enableXOF=i,t.destroyed=this.destroyed,t}}const Ne=(e,t,n,s={})=>h.createHasher(()=>new it(t,e,n),s),Ve=Ne(1,136,32),Tt=64,v=20,nt=4,zt=2,We=0,Me=1,F=[64,64],Fe=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function at(e){return Ae(h.sha256(e))}function Gt(e){return(1<<e)-1}function Xe(e){let t=1;for(;t*t<e+1;)t++;return Math.max(t,2)}function Ke(e){const t=nt,n=e*Gt(t);return{d:t,n:e,checksum_radix:Xe(n)}}function Y(e,t){const n=[];let s=t;for(;s>0;)n.push(s&255),s>>>=8;const r=new Uint8Array(e.length+n.length);r.set(e);for(let o=0;o<n.length;o++)r[e.length+o]=n[o];return at(r)}function Q(e,t){let n=e;for(let s=0;s<t;s++)n=at(n);return n}function De(e,t){const n=Gt(t.d),s=t.checksum_radix-1,r=Math.floor(t.n*n/t.checksum_radix),o=[];for(let c=0;c<t.n;c++){const w=Y(e,c+zt),y=Q(w,n);o.push(Array.from(y))}const i=Y(e,We),a=Q(i,s),l=Y(e,Me),u=Q(l,r);return{config:t,message_terminals:o,checksum_major_terminal:Array.from(u),checksum_minor_terminal:Array.from(a)}}async function Zt(e){try{if(e.length!==Tt)throw new Error(`WOTS seed must be exactly ${Tt} bytes, got ${e.length}`);const t=[];for(let n=0;n<F.length;n++){const s=F[n],r=Ke(s),o=new Uint8Array(e.length+1);o.set(e),o[e.length]=n;const i=at(o);try{const a=De(i,r);if(a.config.d!==nt)throw new Error(`Block ${n}: expected d=${nt}, got d=${a.config.d}`);if(a.config.n!==s)throw new Error(`Block ${n}: expected n=${s}, got n=${a.config.n}`);if(a.message_terminals.length!==s)throw new Error(`Block ${n}: expected ${s} message terminals, got ${a.message_terminals.length}`);for(let l=0;l<a.message_terminals.length;l++)if(a.message_terminals[l].length!==v)throw new Error(`Block ${n} terminal ${l}: expected ${v} bytes, got ${a.message_terminals[l].length}`);if(a.checksum_minor_terminal.length!==v)throw new Error(`Block ${n} checksum_minor: expected ${v} bytes`);if(a.checksum_major_terminal.length!==v)throw new Error(`Block ${n} checksum_major: expected ${v} bytes`);t.push(a)}finally{o.fill(0),i.fill(0)}}if(t.length!==F.length)throw new Error(`Expected ${F.length} blocks, got ${t.length}`);return t}finally{e.fill(0)}}function J(e,t,n){if(e.length!==v)throw new Error(`Block ${t} ${n}: expected ${v} bytes, got ${e.length}`);for(let s=0;s<e.length;s++){const r=e[s];if(!Number.isInteger(r)||r<0||r>255)throw new Error(`Block ${t} ${n}[${s}]: invalid byte value ${r}`)}}function Yt(e){if(e.length===0)throw new Error("Public keys array must not be empty");for(let o=0;o<e.length;o++){const i=e[o];J(i.checksum_minor_terminal,o,"checksum_minor_terminal"),J(i.checksum_major_terminal,o,"checksum_major_terminal");for(let a=0;a<i.message_terminals.length;a++)J(i.message_terminals[a],o,`message_terminal[${a}]`)}let t=0;for(const o of e)t+=zt+o.message_terminals.length;const n=new Uint8Array(t*v);let s=0;for(const o of e){n.set(o.checksum_minor_terminal,s),s+=v,n.set(o.checksum_major_terminal,s),s+=v;for(const i of o.message_terminals)n.set(i,s),s+=v}const r=Ve(n);return`0x${Fe(r)}`}function qe(e){const t=(e instanceof Error?e.message:typeof e=="string"?e:"").toLowerCase();return t.includes("wots")&&t.includes("hash")&&t.includes("does not match")}async function je(e,t){const n=[],s=[],r=[],o=[];try{for(let i=0;i<t;i++){const a=Ct(e,i);try{const u=await Zt(a);n.push(u),s.push(Yt(u))}finally{a.fill(0)}const l=St(e,i);try{const u=d.uint8ArrayToHex(l);r.push(u),o.push(ee.computeHashlock(d.ensureHexPrefix(u)).slice(2))}finally{l.fill(0)}}}finally{e.fill(0)}return{perVaultWotsKeys:n,wotsPkHashes:s,htlcSecretHexes:r,hashlocks:o}}const ze=/^0x[0-9a-f]+$/i,Ge=/^[0-9a-f]+$/i,Ze=/^[A-Za-z0-9+/]+={0,2}$/;function L(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty public key");return d.processPublicKeyToXOnly(e).toLowerCase()}function Ye(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty BIP-322 signature");if(e.startsWith("0x")||e.startsWith("0X")){if(!ze.test(e)||e.length<4||e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return e.toLowerCase()}if(Ge.test(e)){if(e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return`0x${e.toLowerCase()}`}if(!Ze.test(e)||e.length%4!==0)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");const t=K.Buffer.from(e,"base64");if(t.length===0||t.toString("base64")!==e)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");return`0x${t.toString("hex")}`}async function Qe(e,t,n){if(typeof e.signPsbts=="function"){const r=await e.signPsbts(t,n);if(r.length!==t.length)throw new Error(`Expected ${t.length} signed PSBTs but received ${r.length}`);return r}const s=[];for(let r=0;r<t.length;r++){const o=await e.signPsbt(t[r],n[r]);s.push(o)}return s}const C={"0x04aabf33":"Vault already exists: This Bitcoin transaction has already been registered. Please select different UTXOs or use a different amount to create a unique transaction.","0x4fec082d":"Script mismatch: The Bitcoin transaction's taproot output does not match the expected vault script. This may be caused by incorrect vault participants or key configuration.","0x6cc363a5":"Invalid BTC proof of possession: The signature could not be verified. Please ensure you're signing with the correct Bitcoin wallet.","0x6c3f2bf6":"Invalid BTC public key: The Bitcoin public key format is invalid.","0x2c5211c6":"Invalid amount: The deposit amount is invalid or below the minimum required.","0x0405f772":"Application not registered: The application controller is not registered in the system.","0x24e165cc":"Invalid provider status: The vault provider is not in a valid state to accept deposits.","0xd92e233d":"Zero address: One of the required addresses is the zero address.","0x65aa7007":"BTC key mismatch: The Bitcoin public key does not match the expected key.","0x82b42900":"Unauthorized: You must be the depositor or vault provider to submit this transaction.","0x8baa579f":"Invalid signature: The BTC proof of possession signature could not be verified.","0x2f9d01e9":"Invalid BTC transaction: The Bitcoin transaction format is invalid.","0x5a3c6b3e":"Vault provider not registered: The selected vault provider is not registered.","0x979f4518":"Invalid pegin fee: The ETH fee sent does not match the required amount. This may indicate a fee rate change during the transaction.","0x5fad9694":"This pre-pegin output has already been used to activate another vault.","0x7ed061c9":"This pegin transaction has already been used to activate another vault."};function q(e){if(!e||typeof e!="object")return;const t=e;if(typeof t.data=="string"&&t.data.startsWith("0x"))return t.data;if(typeof t.details=="string"&&t.details.startsWith("0x"))return t.details;let n=t.cause,s=0;const r=5;for(;n&&typeof n=="object"&&s<r;){const a=n;if(typeof a.data=="string"&&a.data.startsWith("0x"))return a.data;n=a.cause,s++}const i=(typeof t.message=="string"?t.message:"").match(/\b(0x[a-fA-F0-9]{8})\b/);if(i)return i[1]}function Je(e){const t=q(e);if(t){const n=t.substring(0,10);return C[t]??C[n]}}function tn(e){const t=q(e);if(t===void 0)return!1;const n=t.substring(0,10);return t in C||n in C}function S(e){console.error("[Contract Error] Raw error:",e);const t=q(e);if(console.error("[Contract Error] Extracted error data:",t),t){const s=t.substring(0,10),r=C[t]??C[s];if(r)throw console.error("[Contract Error] Known error:",r),new Error(r)}const n=(e==null?void 0:e.message)||"";if(n.includes("gas limit too high")||n.includes("21000000")||n.includes("Internal JSON-RPC error")){const s=t?` (error code: ${t})`:"";throw console.error("[Contract Error] Transaction rejected. Error code:",t,"Message:",n),new Error(`Transaction failed: The contract rejected this transaction${s}. Possible causes: (1) Vault already exists for this transaction, (2) Invalid signature, (3) Unauthorized caller. Please check your transaction parameters and try again.`)}throw e instanceof Error?(console.error("[Contract Error] Unhandled error:",e.message),e):new Error(`Contract call failed: ${String(e)}`)}const en=0,X="00".repeat(32);function nn(e,t,n,s){const r=n==null?void 0:n[`${e}:${t}`];return r?Promise.resolve({txid:e,vout:t,value:r.value,scriptPubKey:r.scriptPubKey}):_t.getUtxoInfo(e,t,s)}const kt=12e4;class sn{constructor(t){b(this,"config");this.config=t}async preparePegin(t){if(t.amounts.length===0)throw new Error("amounts must contain at least one entry");const n=await this.config.btcWallet.getPublicKeyHex(),s=L(n),r=await this.prepareSizing(s,t),o=r.selectedUTXOs.map(p=>({txid:d.hexToUint8Array(p.txid),vout:p.vout})),i=await Nt(this.config.btcWallet,{depositorBtcPubkey:d.hexToUint8Array(s),fundingOutpoints:o});let a,l;try{const p=Bt(i);try{a=d.uint8ArrayToHex(p),l=d.uint8ArrayToHex(h.sha256(p))}finally{p.fill(0)}}catch(p){throw i.fill(0),p}const u=await je(i,t.amounts.length),{perVaultWotsKeys:c,wotsPkHashes:w,htlcSecretHexes:y,hashlocks:P}=u,f=await this.preparePeginCommit({depositorBtcPubkeyRaw:n,depositorBtcPubkey:s,hashlocks:P,authAnchorHash:l,sizing:r,params:t});for(let p=0;p<f.perVault.length;p++)if(f.perVault[p].htlcVout!==p)throw new Error(`Internal invariant violation: htlcVout/index mismatch at vault ${p} (expected ${p}, got ${f.perVault[p].htlcVout})`);return he(f.fundedPrePeginTxHex,t.amounts.length,l),{transaction:{...f,selectedUTXOs:r.selectedUTXOs,fee:r.fee,changeAmount:r.changeAmount},depositorBtcPubkey:s,derivedSecrets:{perVaultWotsKeys:c,wotsPkHashes:w,htlcSecretHexes:y,authAnchorHex:a}}}async prepareSizing(t,n){const s=n.amounts.map(()=>X),r=n.vaultKeeperBtcPubkeys.length,o=await O.buildPrePeginPsbt({depositorPubkey:t,vaultProviderPubkey:d.stripHexPrefix(n.vaultProviderBtcPubkey),vaultKeeperPubkeys:n.vaultKeeperBtcPubkeys.map(d.stripHexPrefix),universalChallengerPubkeys:n.universalChallengerBtcPubkeys.map(d.stripHexPrefix),hashlocks:s,timelockRefund:n.timelockRefund,pegInAmounts:n.amounts,feeRate:n.protocolFeeRate,numLocalChallengers:r,councilQuorum:n.councilQuorum,councilSize:n.councilSize,network:this.config.btcNetwork,authAnchorHash:X}),i=R.selectUtxosForPegin([...n.availableUTXOs],o.totalOutputValue,n.mempoolFeeRate,ut.peginOutputCount(o.htlcValues.length,X));return{selectedUTXOs:i.selectedUTXOs,fee:i.fee,changeAmount:i.changeAmount}}async preparePeginCommit(t){const{depositorBtcPubkeyRaw:n,depositorBtcPubkey:s,hashlocks:r,authAnchorHash:o,sizing:i,params:a}=t,l=X.toLowerCase();for(let x=0;x<r.length;x++)if(r[x].toLowerCase()===l)throw new Error(`preparePeginCommit refusing to build with sizing-pass placeholder hashlock at vault ${x} — internal substitution bug`);if(o.toLowerCase()===l)throw new Error("preparePeginCommit refusing to build with sizing-pass placeholder auth-anchor hash — internal substitution bug");const u=d.stripHexPrefix(a.vaultProviderBtcPubkey),c=a.vaultKeeperBtcPubkeys.map(d.stripHexPrefix),w=a.universalChallengerBtcPubkeys.map(d.stripHexPrefix),y=c.length,P={depositorPubkey:s,vaultProviderPubkey:u,vaultKeeperPubkeys:c,universalChallengerPubkeys:w,hashlocks:r,timelockRefund:a.timelockRefund,pegInAmounts:a.amounts,feeRate:a.protocolFeeRate,numLocalChallengers:y,councilQuorum:a.councilQuorum,councilSize:a.councilSize,network:this.config.btcNetwork,authAnchorHash:o},f=await O.buildPrePeginPsbt(P),p=d.getNetwork(this.config.btcNetwork),_=ut.fundPeginTransaction({unfundedTxHex:f.psbtHex,selectedUTXOs:i.selectedUTXOs,changeAddress:a.changeAddress,changeAmount:i.changeAmount,network:p}),A=d.stripHexPrefix(R.calculateBtcTxHash(_)),g=[],T=[],m=[];for(let x=0;x<r.length;x++){const H=await O.buildPeginTxFromFundedPrePegin({prePeginParams:P,timelockPegin:a.timelockPegin,fundedPrePeginTxHex:_,htlcVout:x}),B=await O.buildPeginInputPsbt({peginTxHex:H.txHex,fundedPrePeginTxHex:_,depositorPubkey:s,vaultProviderPubkey:u,vaultKeeperPubkeys:c,universalChallengerPubkeys:w,hashlock:r[x],timelockRefund:a.timelockRefund,network:this.config.btcNetwork});g.push(H),T.push(B.psbtHex),m.push(te.createTaprootScriptPathSignOptions(n,1))}const k=await Qe(this.config.btcWallet,T,m),E=[];for(let x=0;x<k.length;x++){const H=O.extractPeginInputSignature(k[x],s),B=O.finalizePeginInputPsbt(k[x]);E.push({htlcVout:x,htlcValue:f.htlcValues[x],peginTxHex:B,peginTxid:g[x].txid,peginInputSignature:H,vaultScriptPubKey:g[x].vaultScriptPubKey})}return{fundedPrePeginTxHex:_,prePeginTxid:A,perVault:E}}async signAndBroadcast(t){const{fundedPrePeginTxHex:n,depositorBtcPubkey:s}=t,r=n.startsWith("0x")?n.slice(2):n,o=V.Transaction.fromHex(r);if(o.ins.length===0)throw new Error("Transaction has no inputs");const i=new V.Psbt;i.setVersion(o.version),i.setLocktime(o.locktime);const a=K.Buffer.from(L(s),"hex"),l=this.config.mempoolApiUrl,u=o.ins.map(g=>{const T=K.Buffer.from(g.hash).reverse().toString("hex"),m=g.index;return nn(T,m,t.localPrevouts,l).then(k=>({input:g,utxoData:k,txid:T,vout:m}))}),c=await Promise.all(u),w=c.reduce((g,T)=>g+BigInt(T.utxoData.value),0n),y=o.outs.reduce((g,T)=>g+BigInt(T.value),0n);if(w<y)throw new Error(`UTXO value mismatch: total input value (${w} sat) is less than total output value (${y} sat). This may indicate the mempool API returned manipulated UTXO data.`);const P=w-y;if(P>lt.MAX_REASONABLE_FEE_SATS)throw new Error(`Implied transaction fee (${P} sat) exceeds maximum reasonable fee (${lt.MAX_REASONABLE_FEE_SATS} sat). This may indicate manipulated UTXO data.`);for(const{input:g,utxoData:T,txid:m,vout:k}of c){const E=R.getPsbtInputFields({value:T.value,scriptPubKey:T.scriptPubKey},a);i.addInput({hash:g.hash,index:g.index,sequence:g.sequence,...E})}for(const g of o.outs)i.addOutput({script:g.script,value:g.value});const f=await this.config.btcWallet.signPsbt(i.toHex()),p=V.Psbt.fromHex(f);try{p.finalizeAllInputs()}catch(g){if(!p.data.inputs.every(m=>m.finalScriptWitness||m.finalScriptSig))throw new Error(`PSBT finalization failed and wallet did not auto-finalize: ${g}`)}const _=p.extractTransaction().toHex();return await _t.pushTx(_,l)}async registerPeginOnChain(t){const{unsignedPrePeginTx:n,depositorSignedPeginTx:s,vaultProvider:r,hashlock:o,htlcVout:i,depositorPayoutBtcAddress:a,depositorWotsPkHash:l,popSignature:u}=t;if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const c=this.config.ethWallet.account.address;if(!I.isAddressEqual(u.depositorEthAddress,c))throw new Error(`Proof of possession was signed for ${u.depositorEthAddress} but the Ethereum wallet is currently connected to ${c}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(u);const w=u.btcPopSignature,y=d.ensureHexPrefix(u.depositorBtcPubkey),P=d.ensureHexPrefix(n),f=d.ensureHexPrefix(s),p=await this.resolvePayoutScriptPubKey(a),_=R.calculateBtcTxHash(f),A=await ct.deriveVaultId(d.stripHexPrefix(_),d.stripHexPrefix(c)),g=d.ensureHexPrefix(A);if(await this.checkVaultExists(g))throw new Error(`Vault already exists (ID: ${g}, peginTxHash: ${_}). Vault IDs are derived from the pegin transaction hash and depositor address. To create a new vault, use different UTXOs or a different amount to generate a unique transaction.`);const m=I.createPublicClient({chain:this.config.ethChain,transport:I.http()});let k;try{k=await m.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:$.BTCVaultRegistryABI,functionName:"getPegInFee",args:[r]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const E=I.encodeFunctionData({abi:$.BTCVaultRegistryABI,functionName:"submitPeginRequest",args:[c,y,w,P,f,r,o,i,p,l]});let x;try{x=await m.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:k,account:this.config.ethWallet.account.address})}catch(j){S(j)}let H;try{H=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:k,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:x})}catch(j){S(j)}const B=await m.waitForTransactionReceipt({hash:H,timeout:kt});return B.status==="reverted"&&S(new Error(`Transaction reverted. Hash: ${H}. Check the transaction on block explorer for details.`)),{ethTxHash:B.transactionHash,vaultId:g,peginTxHash:_}}async registerPeginBatchOnChain(t){const{vaultProvider:n,unsignedPrePeginTx:s,requests:r,popSignature:o}=t;if(r.length===0)throw new Error("Batch pegin requires at least one request");if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const i=this.config.ethWallet.account.address;if(!I.isAddressEqual(o.depositorEthAddress,i))throw new Error(`Proof of possession was signed for ${o.depositorEthAddress} but the Ethereum wallet is currently connected to ${i}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(o);const a=o.btcPopSignature,l=[];for(const m of r)l.push(await this.resolvePayoutScriptPubKey(m.depositorPayoutBtcAddress));const u=[];for(const m of r){const k=d.ensureHexPrefix(m.depositorSignedPeginTx),E=R.calculateBtcTxHash(k),x=await ct.deriveVaultId(d.stripHexPrefix(E),d.stripHexPrefix(i)),H=d.ensureHexPrefix(x);if(await this.checkVaultExists(H))throw new Error(`Vault already exists (ID: ${H}, peginTxHash: ${E}). To create a new vault, use different UTXOs or a different amount.`);u.push({vaultId:H,peginTxHash:E})}const c=I.createPublicClient({chain:this.config.ethChain,transport:I.http()});let w;try{w=await c.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:$.BTCVaultRegistryABI,functionName:"getPegInFee",args:[n]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const y=w*BigInt(r.length),P=d.ensureHexPrefix(o.depositorBtcPubkey),f=d.ensureHexPrefix(s),p=r.map((m,k)=>({depositorBtcPubKey:P,btcPopSignature:a,unsignedPrePeginTx:f,depositorSignedPeginTx:d.ensureHexPrefix(m.depositorSignedPeginTx),hashlock:m.hashlock,htlcVout:m.htlcVout,referralCode:en,depositorPayoutBtcAddress:l[k],depositorWotsPkHash:m.depositorWotsPkHash})),_=I.encodeFunctionData({abi:$.BTCVaultRegistryABI,functionName:"submitPeginRequestBatch",args:[i,n,p]});let A;try{A=await c.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:_,value:y,account:this.config.ethWallet.account.address})}catch(m){S(m)}let g;try{g=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:_,value:y,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:A})}catch(m){S(m)}const T=await c.waitForTransactionReceipt({hash:g,timeout:kt});return T.status==="reverted"&&S(new Error(`Batch transaction reverted. Hash: ${g}. Check the transaction on block explorer for details.`)),{ethTxHash:T.transactionHash,vaults:u}}async checkVaultExists(t){try{return(await I.createPublicClient({chain:this.config.ethChain,transport:I.http()}).readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:$.BTCVaultRegistryABI,functionName:"getBtcVaultBasicInfo",args:[t]})).depositor!==I.zeroAddress}catch{return!1}}async resolvePayoutScriptPubKey(t){let n;if(t)n=t;else{n=await this.config.btcWallet.getAddress();const r=await this.config.btcWallet.getPublicKeyHex();if(!d.isAddressFromPublicKey(n,r,this.config.btcNetwork))throw new Error("The BTC address from your wallet does not match the wallet's public key. Please ensure your wallet is using a supported address type (Taproot or Native SegWit).")}const s=d.getNetwork(this.config.btcNetwork);try{return`0x${Ht.address.toOutputScript(n,s).toString("hex")}`}catch{throw new Error(`Invalid BTC payout address: "${n}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`)}}async signProofOfPossession(){if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const t=this.config.ethWallet.account.address,n=L(await this.config.btcWallet.getPublicKeyHex()),s=this.config.vaultContracts.btcVaultRegistry,r=`${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`,o=await this.config.btcWallet.signMessage(r,"bip322-simple");return{btcPopSignature:Ye(o),depositorEthAddress:t,depositorBtcPubkey:n}}async assertPopMatchesBtcWallet(t){const n=L(await this.config.btcWallet.getPublicKeyHex()),s=L(t.depositorBtcPubkey);if(n!==s)throw new Error(`Proof of possession was signed with BTC pubkey ${s} but the BTC wallet is currently connected to ${n}. Reconnect the original wallet or call signProofOfPossession() again.`)}getNetwork(){return this.config.btcNetwork}getVaultContractAddress(){return this.config.vaultContracts.btcVaultRegistry}}exports.CONTRACT_ERRORS=C;exports.PeginManager=sn;exports.VAULT_APP_NAME=Ut;exports.buildFundingOutpointsCommitment=Rt;exports.buildVaultContext=$t;exports.computeWotsBlockPublicKeysHash=Yt;exports.deriveVaultRoot=Nt;exports.deriveWotsBlocksFromSeed=Zt;exports.expandAuthAnchor=Bt;exports.expandHashlockSecret=St;exports.expandWotsSeed=Ct;exports.extractErrorData=q;exports.getContractErrorMessage=Je;exports.handleContractError=S;exports.isKnownContractError=tn;exports.isWotsMismatchError=qe;exports.parseFundingOutpointsFromTx=Pe;
|
|
2
|
-
//# sourceMappingURL=PeginManager-
|
|
1
|
+
"use strict";var Qt=Object.defineProperty;var Jt=(e,t,n)=>t in e?Qt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var b=(e,t,n)=>Jt(e,typeof t!="symbol"?t+"":t,n);const V=require("bitcoinjs-lib"),K=require("buffer"),h=require("./sha2-CsTynrfJ.cjs"),I=require("viem"),ct=require("@babylonlabs-io/babylon-tbv-rust-wasm"),O=require("./challengeAssert-06GLZtV8.cjs"),d=require("./bitcoin-EYBKDtEW.cjs"),te=require("./signing-DHSXjhLM.cjs"),lt=require("./validation-u8W7Lp2x.cjs"),ee=require("./buildAndBroadcastRefund-BXjYAqdb.cjs"),R=require("./psbtInputFields-BLi7Ta-T.cjs"),ut=require("./fundPeginTransaction-DaWoYCgO.cjs"),_t=require("./vault-registry-reader-DdUgC5IF.cjs"),$=require("./types-C2IUB0Jd.cjs");function ne(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const s=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,s.get?s:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const Ht=ne(V),W=BigInt(2**32-1),ht=BigInt(32);function se(e,t=!1){return t?{h:Number(e&W),l:Number(e>>ht&W)}:{h:Number(e>>ht&W)|0,l:Number(e&W)|0}}function re(e,t=!1){const n=e.length;let s=new Uint32Array(n),r=new Uint32Array(n);for(let o=0;o<n;o++){const{h:i,l:a}=se(e[o],t);[s[o],r[o]]=[i,a]}return[s,r]}const oe=(e,t,n)=>e<<n|t>>>32-n,ie=(e,t,n)=>t<<n|e>>>32-n,ae=(e,t,n)=>t<<n-32|e>>>64-n,ce=(e,t,n)=>e<<n-32|t>>>64-n,le=106,ue=32,dt=34;function he(e,t,n){const s=d.stripHexPrefix(e),r=Ht.Transaction.fromHex(s);if(r.outs.length<=t)throw new Error(`Pre-PegIn auth-anchor OP_RETURN missing: tx has ${r.outs.length} outputs, expected at least ${t+1} (vault outputs + OP_RETURN)`);const o=r.outs[t],i=o.script;if(i.length!==dt||i[0]!==le||i[1]!==ue)throw new Error(`Pre-PegIn auth-anchor OP_RETURN at vout ${t} has unexpected script encoding (got ${i.length}-byte script with prefix 0x${i.slice(0,Math.min(2,i.length)).toString("hex")}; expected ${dt}-byte OP_RETURN + PUSH32 layout)`);const a=i.slice(2).toString("hex").toLowerCase();if(a!==n.toLowerCase())throw new Error(`Pre-PegIn auth-anchor OP_RETURN payload mismatch at vout ${t}: tx pushes ${a}, expected ${n}`);if(o.value!==0)throw new Error(`Pre-PegIn auth-anchor OP_RETURN at vout ${t} has non-zero value ${o.value}; OP_RETURN outputs must be 0-value`)}class vt{constructor(t,n){b(this,"oHash");b(this,"iHash");b(this,"blockLen");b(this,"outputLen");b(this,"finished",!1);b(this,"destroyed",!1);if(h.ahash(t),h.abytes(n,void 0,"key"),this.iHash=t.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const s=this.blockLen,r=new Uint8Array(s);r.set(n.length>s?t.create().update(n).digest():n);for(let o=0;o<r.length;o++)r[o]^=54;this.iHash.update(r),this.oHash=t.create();for(let o=0;o<r.length;o++)r[o]^=106;this.oHash.update(r),h.clean(r)}update(t){return h.aexists(this),this.iHash.update(t),this}digestInto(t){h.aexists(this),h.abytes(t,this.outputLen,"output"),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));const{oHash:n,iHash:s,finished:r,destroyed:o,blockLen:i,outputLen:a}=this;return t=t,t.finished=r,t.destroyed=o,t.blockLen=i,t.outputLen=a,t.oHash=n._cloneInto(t.oHash),t.iHash=s._cloneInto(t.iHash),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const It=(e,t,n)=>new vt(e,t).update(n).digest();It.create=(e,t)=>new vt(e,t);const z=Uint8Array.of(0),ft=Uint8Array.of();function st(e,t,n,s=32){h.ahash(e),h.anumber(s,"length");const r=e.outputLen;if(s>255*r)throw new Error("Length must be <= 255*HashLen");const o=Math.ceil(s/r);n===void 0?n=ft:h.abytes(n,void 0,"info");const i=new Uint8Array(o*r),a=It.create(e,t),l=a._cloneInto(),u=new Uint8Array(a.outputLen);for(let c=0;c<o;c++)z[0]=c+1,l.update(c===0?ft:u).update(n).update(z).digestInto(u),i.set(u,r*c),a._cloneInto(l);return a.destroy(),l.destroy(),h.clean(u,z),i.slice(0,s)}const G=new TextEncoder().encode("babylonvault"),gt=255,pt=65535,mt=2,de="hashlock",fe="auth-anchor",ge="wots-seed";function At(e){if(!Number.isInteger(e)||e<0||e>4294967295)throw new Error(`i2osp4: value must be a u32, got ${e}`);const t=new Uint8Array(4);return t[0]=e>>>24&255,t[1]=e>>>16&255,t[2]=e>>>8&255,t[3]=e&255,t}function rt(e,t=new Uint8Array(0)){const n=new TextEncoder().encode(e);if(n.length===0||n.length>gt)throw new Error(`info: label length must be in [1, ${gt}], got ${n.length}`);if(t.length>pt)throw new Error(`info: ctx length must be in [0, ${pt}], got ${t.length}`);const s=G.length+1+n.length+mt+t.length,r=new Uint8Array(s);let o=0;return r.set(G,o),o+=G.length,r[o]=n.length,o+=1,r.set(n,o),o+=n.length,r[o]=t.length>>>8&255,r[o+1]=t.length&255,o+=mt,r.set(t,o),r}const xt=32,pe=32,me=32,xe=64;function ot(e){if(e.length!==xt)throw new Error(`vault-secrets: root must be exactly ${xt} bytes, got ${e.length}`)}function Bt(e){return ot(e),st(h.sha256,e,rt(fe),pe)}function St(e,t){return ot(e),st(h.sha256,e,rt(de,At(t)),me)}function Ct(e,t){return ot(e),st(h.sha256,e,rt(ge,At(t)),xe)}const N=32,Z=32,tt=36,Ot=32,D=4,be=D+N+D+Ot;function et(e,t,n){e[t]=n>>>24&255,e[t+1]=n>>>16&255,e[t+2]=n>>>8&255,e[t+3]=n&255}function we(e){if(e.txid.length!==Z)throw new Error(`outpoint.txid must be exactly ${Z} bytes, got ${e.txid.length}`);if(!Number.isInteger(e.vout)||e.vout<0||e.vout>4294967295)throw new Error(`outpoint.vout must be a u32, got ${e.vout}`);const t=new Uint8Array(tt);return t.set(e.txid,0),et(t,Z,e.vout),t}function bt(e,t){const n=Math.min(e.length,t.length);for(let s=0;s<n;s++)if(e[s]!==t[s])return e[s]-t[s];return e.length-t.length}function Rt(e){if(e.length===0)throw new Error("buildFundingOutpointsCommitment: outpoints must be non-empty");const t=e.map(we);t.sort(bt);for(let s=1;s<t.length;s++)if(bt(t[s-1],t[s])===0)throw new Error("buildFundingOutpointsCommitment: duplicate outpoint detected");const n=new Uint8Array(t.length*tt);for(let s=0;s<t.length;s++)n.set(t[s],s*tt);return h.sha256(n)}function $t(e){if(e.depositorBtcPubkey.length!==N)throw new Error(`vaultContext: depositorBtcPubkey must be exactly ${N} bytes, got ${e.depositorBtcPubkey.length}`);const t=Rt(e.fundingOutpoints),n=new Uint8Array(be);let s=0;return et(n,s,N),s+=D,n.set(e.depositorBtcPubkey,s),s+=N,et(n,s,Ot),s+=D,n.set(t,s),n}const Ut="babylon-vault",Lt=32,wt=Lt*2,ye=/^[0-9a-f]+$/;async function Nt(e,t){const n=$t(t),s=d.uint8ArrayToHex(n),r=await e.deriveContextHash(Ut,s);if(typeof r!="string")throw new Error(`deriveVaultRoot: wallet must return a string, got ${typeof r}`);if(r.length!==wt)throw new Error(`deriveVaultRoot: wallet must return a ${wt}-character hex string (${Lt} bytes), got length ${r.length}`);if(!ye.test(r))throw new Error("deriveVaultRoot: wallet must return lowercase hex per derive-context-hash.md §2.1; got value with non-lowercase or non-hex characters");return d.hexToUint8Array(r)}function Pe(e){if(!e)throw new Error("Pre-pegin transaction hex is empty");const t=e.startsWith("0x")?e.slice(2):e,n=V.Transaction.fromHex(t);if(n.ins.length===0)throw new Error("Pre-pegin transaction has no inputs");return n.ins.map(s=>({txid:Uint8Array.from(K.Buffer.from(s.hash).reverse()),vout:s.index}))}const Ee=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),Vt=Uint8Array.from(new Array(16).fill(0).map((e,t)=>t)),Te=Vt.map(e=>(9*e+5)%16),Wt=(()=>{const n=[[Vt],[Te]];for(let s=0;s<4;s++)for(let r of n)r.push(r[s].map(o=>Ee[o]));return n})(),Mt=Wt[0],Ft=Wt[1],Xt=[[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8],[12,13,11,15,6,9,9,7,12,15,11,13,7,8,7,7],[13,15,14,11,7,7,6,8,13,14,13,12,5,5,6,9],[14,11,12,14,8,6,5,5,15,12,15,14,9,9,8,6],[15,12,13,13,9,5,8,6,14,11,12,11,8,6,5,5]].map(e=>Uint8Array.from(e)),ke=Mt.map((e,t)=>e.map(n=>Xt[t][n])),_e=Ft.map((e,t)=>e.map(n=>Xt[t][n])),He=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),ve=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function yt(e,t,n,s){return e===0?t^n^s:e===1?t&n|~t&s:e===2?(t|~n)^s:e===3?t&s|n&~s:t^(n|~s)}const M=new Uint32Array(16);class Ie extends h.HashMD{constructor(){super(64,20,8,!0);b(this,"h0",1732584193);b(this,"h1",-271733879);b(this,"h2",-1732584194);b(this,"h3",271733878);b(this,"h4",-1009589776)}get(){const{h0:n,h1:s,h2:r,h3:o,h4:i}=this;return[n,s,r,o,i]}set(n,s,r,o,i){this.h0=n|0,this.h1=s|0,this.h2=r|0,this.h3=o|0,this.h4=i|0}process(n,s){for(let f=0;f<16;f++,s+=4)M[f]=n.getUint32(s,!0);let r=this.h0|0,o=r,i=this.h1|0,a=i,l=this.h2|0,u=l,c=this.h3|0,w=c,y=this.h4|0,P=y;for(let f=0;f<5;f++){const p=4-f,_=He[f],A=ve[f],g=Mt[f],T=Ft[f],m=ke[f],k=_e[f];for(let E=0;E<16;E++){const x=h.rotl(r+yt(f,i,l,c)+M[g[E]]+_,m[E])+y|0;r=y,y=c,c=h.rotl(l,10)|0,l=i,i=x}for(let E=0;E<16;E++){const x=h.rotl(o+yt(p,a,u,w)+M[T[E]]+A,k[E])+P|0;o=P,P=w,w=h.rotl(u,10)|0,u=a,a=x}}this.set(this.h1+l+w|0,this.h2+c+P|0,this.h3+y+o|0,this.h4+r+a|0,this.h0+i+u|0)}roundClean(){h.clean(M)}destroy(){this.destroyed=!0,h.clean(this.buffer),this.set(0,0,0,0,0)}}const Ae=h.createHasher(()=>new Ie),Be=BigInt(0),U=BigInt(1),Se=BigInt(2),Ce=BigInt(7),Oe=BigInt(256),Re=BigInt(113),Kt=[],Dt=[],qt=[];for(let e=0,t=U,n=1,s=0;e<24;e++){[n,s]=[s,(2*n+3*s)%5],Kt.push(2*(5*s+n)),Dt.push((e+1)*(e+2)/2%64);let r=Be;for(let o=0;o<7;o++)t=(t<<U^(t>>Ce)*Re)%Oe,t&Se&&(r^=U<<(U<<BigInt(o))-U);qt.push(r)}const jt=re(qt,!0),$e=jt[0],Ue=jt[1],Pt=(e,t,n)=>n>32?ae(e,t,n):oe(e,t,n),Et=(e,t,n)=>n>32?ce(e,t,n):ie(e,t,n);function Le(e,t=24){const n=new Uint32Array(10);for(let s=24-t;s<24;s++){for(let i=0;i<10;i++)n[i]=e[i]^e[i+10]^e[i+20]^e[i+30]^e[i+40];for(let i=0;i<10;i+=2){const a=(i+8)%10,l=(i+2)%10,u=n[l],c=n[l+1],w=Pt(u,c,1)^n[a],y=Et(u,c,1)^n[a+1];for(let P=0;P<50;P+=10)e[i+P]^=w,e[i+P+1]^=y}let r=e[2],o=e[3];for(let i=0;i<24;i++){const a=Dt[i],l=Pt(r,o,a),u=Et(r,o,a),c=Kt[i];r=e[c],o=e[c+1],e[c]=l,e[c+1]=u}for(let i=0;i<50;i+=10){for(let a=0;a<10;a++)n[a]=e[i+a];for(let a=0;a<10;a++)e[i+a]^=~n[(a+2)%10]&n[(a+4)%10]}e[0]^=$e[s],e[1]^=Ue[s]}h.clean(n)}class it{constructor(t,n,s,r=!1,o=24){b(this,"state");b(this,"pos",0);b(this,"posOut",0);b(this,"finished",!1);b(this,"state32");b(this,"destroyed",!1);b(this,"blockLen");b(this,"suffix");b(this,"outputLen");b(this,"enableXOF",!1);b(this,"rounds");if(this.blockLen=t,this.suffix=n,this.outputLen=s,this.enableXOF=r,this.rounds=o,h.anumber(s,"outputLen"),!(0<t&&t<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=h.u32(this.state)}clone(){return this._cloneInto()}keccak(){h.swap32IfBE(this.state32),Le(this.state32,this.rounds),h.swap32IfBE(this.state32),this.posOut=0,this.pos=0}update(t){h.aexists(this),h.abytes(t);const{blockLen:n,state:s}=this,r=t.length;for(let o=0;o<r;){const i=Math.min(n-this.pos,r-o);for(let a=0;a<i;a++)s[this.pos++]^=t[o++];this.pos===n&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:n,pos:s,blockLen:r}=this;t[s]^=n,(n&128)!==0&&s===r-1&&this.keccak(),t[r-1]^=128,this.keccak()}writeInto(t){h.aexists(this,!1),h.abytes(t),this.finish();const n=this.state,{blockLen:s}=this;for(let r=0,o=t.length;r<o;){this.posOut>=s&&this.keccak();const i=Math.min(s-this.posOut,o-r);t.set(n.subarray(this.posOut,this.posOut+i),r),this.posOut+=i,r+=i}return t}xofInto(t){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(t)}xof(t){return h.anumber(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(h.aoutput(t,this),this.finished)throw new Error("digest() was already called");return this.writeInto(t),this.destroy(),t}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,h.clean(this.state)}_cloneInto(t){const{blockLen:n,suffix:s,outputLen:r,rounds:o,enableXOF:i}=this;return t||(t=new it(n,s,r,i,o)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=o,t.suffix=s,t.outputLen=r,t.enableXOF=i,t.destroyed=this.destroyed,t}}const Ne=(e,t,n,s={})=>h.createHasher(()=>new it(t,e,n),s),Ve=Ne(1,136,32),Tt=64,v=20,nt=4,zt=2,We=0,Me=1,F=[64,64],Fe=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function at(e){return Ae(h.sha256(e))}function Gt(e){return(1<<e)-1}function Xe(e){let t=1;for(;t*t<e+1;)t++;return Math.max(t,2)}function Ke(e){const t=nt,n=e*Gt(t);return{d:t,n:e,checksum_radix:Xe(n)}}function Y(e,t){const n=[];let s=t;for(;s>0;)n.push(s&255),s>>>=8;const r=new Uint8Array(e.length+n.length);r.set(e);for(let o=0;o<n.length;o++)r[e.length+o]=n[o];return at(r)}function Q(e,t){let n=e;for(let s=0;s<t;s++)n=at(n);return n}function De(e,t){const n=Gt(t.d),s=t.checksum_radix-1,r=Math.floor(t.n*n/t.checksum_radix),o=[];for(let c=0;c<t.n;c++){const w=Y(e,c+zt),y=Q(w,n);o.push(Array.from(y))}const i=Y(e,We),a=Q(i,s),l=Y(e,Me),u=Q(l,r);return{config:t,message_terminals:o,checksum_major_terminal:Array.from(u),checksum_minor_terminal:Array.from(a)}}async function Zt(e){try{if(e.length!==Tt)throw new Error(`WOTS seed must be exactly ${Tt} bytes, got ${e.length}`);const t=[];for(let n=0;n<F.length;n++){const s=F[n],r=Ke(s),o=new Uint8Array(e.length+1);o.set(e),o[e.length]=n;const i=at(o);try{const a=De(i,r);if(a.config.d!==nt)throw new Error(`Block ${n}: expected d=${nt}, got d=${a.config.d}`);if(a.config.n!==s)throw new Error(`Block ${n}: expected n=${s}, got n=${a.config.n}`);if(a.message_terminals.length!==s)throw new Error(`Block ${n}: expected ${s} message terminals, got ${a.message_terminals.length}`);for(let l=0;l<a.message_terminals.length;l++)if(a.message_terminals[l].length!==v)throw new Error(`Block ${n} terminal ${l}: expected ${v} bytes, got ${a.message_terminals[l].length}`);if(a.checksum_minor_terminal.length!==v)throw new Error(`Block ${n} checksum_minor: expected ${v} bytes`);if(a.checksum_major_terminal.length!==v)throw new Error(`Block ${n} checksum_major: expected ${v} bytes`);t.push(a)}finally{o.fill(0),i.fill(0)}}if(t.length!==F.length)throw new Error(`Expected ${F.length} blocks, got ${t.length}`);return t}finally{e.fill(0)}}function J(e,t,n){if(e.length!==v)throw new Error(`Block ${t} ${n}: expected ${v} bytes, got ${e.length}`);for(let s=0;s<e.length;s++){const r=e[s];if(!Number.isInteger(r)||r<0||r>255)throw new Error(`Block ${t} ${n}[${s}]: invalid byte value ${r}`)}}function Yt(e){if(e.length===0)throw new Error("Public keys array must not be empty");for(let o=0;o<e.length;o++){const i=e[o];J(i.checksum_minor_terminal,o,"checksum_minor_terminal"),J(i.checksum_major_terminal,o,"checksum_major_terminal");for(let a=0;a<i.message_terminals.length;a++)J(i.message_terminals[a],o,`message_terminal[${a}]`)}let t=0;for(const o of e)t+=zt+o.message_terminals.length;const n=new Uint8Array(t*v);let s=0;for(const o of e){n.set(o.checksum_minor_terminal,s),s+=v,n.set(o.checksum_major_terminal,s),s+=v;for(const i of o.message_terminals)n.set(i,s),s+=v}const r=Ve(n);return`0x${Fe(r)}`}function qe(e){const t=(e instanceof Error?e.message:typeof e=="string"?e:"").toLowerCase();return t.includes("wots")&&t.includes("hash")&&t.includes("does not match")}async function je(e,t){const n=[],s=[],r=[],o=[];try{for(let i=0;i<t;i++){const a=Ct(e,i);try{const u=await Zt(a);n.push(u),s.push(Yt(u))}finally{a.fill(0)}const l=St(e,i);try{const u=d.uint8ArrayToHex(l);r.push(u),o.push(ee.computeHashlock(d.ensureHexPrefix(u)).slice(2))}finally{l.fill(0)}}}finally{e.fill(0)}return{perVaultWotsKeys:n,wotsPkHashes:s,htlcSecretHexes:r,hashlocks:o}}const ze=/^0x[0-9a-f]+$/i,Ge=/^[0-9a-f]+$/i,Ze=/^[A-Za-z0-9+/]+={0,2}$/;function L(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty public key");return d.processPublicKeyToXOnly(e).toLowerCase()}function Ye(e){if(typeof e!="string"||e.length===0)throw new Error("BTC wallet returned empty BIP-322 signature");if(e.startsWith("0x")||e.startsWith("0X")){if(!ze.test(e)||e.length<4||e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return e.toLowerCase()}if(Ge.test(e)){if(e.length%2!==0)throw new Error("BTC wallet returned malformed hex BIP-322 signature");return`0x${e.toLowerCase()}`}if(!Ze.test(e)||e.length%4!==0)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");const t=K.Buffer.from(e,"base64");if(t.length===0||t.toString("base64")!==e)throw new Error("BTC wallet returned malformed base64 BIP-322 signature");return`0x${t.toString("hex")}`}async function Qe(e,t,n){if(typeof e.signPsbts=="function"){const r=await e.signPsbts(t,n);if(r.length!==t.length)throw new Error(`Expected ${t.length} signed PSBTs but received ${r.length}`);return r}const s=[];for(let r=0;r<t.length;r++){const o=await e.signPsbt(t[r],n[r]);s.push(o)}return s}const C={"0x04aabf33":"Vault already exists: This Bitcoin transaction has already been registered. Please select different UTXOs or use a different amount to create a unique transaction.","0x4fec082d":"Script mismatch: The Bitcoin transaction's taproot output does not match the expected vault script. This may be caused by incorrect vault participants or key configuration.","0x6cc363a5":"Invalid BTC proof of possession: The signature could not be verified. Please ensure you're signing with the correct Bitcoin wallet.","0x6c3f2bf6":"Invalid BTC public key: The Bitcoin public key format is invalid.","0x2c5211c6":"Invalid amount: The deposit amount is invalid or below the minimum required.","0x0405f772":"Application not registered: The application controller is not registered in the system.","0x24e165cc":"Invalid provider status: The vault provider is not in a valid state to accept deposits.","0xd92e233d":"Zero address: One of the required addresses is the zero address.","0x65aa7007":"BTC key mismatch: The Bitcoin public key does not match the expected key.","0x82b42900":"Unauthorized: You must be the depositor or vault provider to submit this transaction.","0x8baa579f":"Invalid signature: The BTC proof of possession signature could not be verified.","0x2f9d01e9":"Invalid BTC transaction: The Bitcoin transaction format is invalid.","0x5a3c6b3e":"Vault provider not registered: The selected vault provider is not registered.","0x979f4518":"Invalid pegin fee: The ETH fee sent does not match the required amount. This may indicate a fee rate change during the transaction.","0x5fad9694":"This pre-pegin output has already been used to activate another vault.","0x7ed061c9":"This pegin transaction has already been used to activate another vault."};function q(e){if(!e||typeof e!="object")return;const t=e;if(typeof t.data=="string"&&t.data.startsWith("0x"))return t.data;if(typeof t.details=="string"&&t.details.startsWith("0x"))return t.details;let n=t.cause,s=0;const r=5;for(;n&&typeof n=="object"&&s<r;){const a=n;if(typeof a.data=="string"&&a.data.startsWith("0x"))return a.data;n=a.cause,s++}const i=(typeof t.message=="string"?t.message:"").match(/\b(0x[a-fA-F0-9]{8})\b/);if(i)return i[1]}function Je(e){const t=q(e);if(t){const n=t.substring(0,10);return C[t]??C[n]}}function tn(e){const t=q(e);if(t===void 0)return!1;const n=t.substring(0,10);return t in C||n in C}function S(e){console.error("[Contract Error] Raw error:",e);const t=q(e);if(console.error("[Contract Error] Extracted error data:",t),t){const s=t.substring(0,10),r=C[t]??C[s];if(r)throw console.error("[Contract Error] Known error:",r),new Error(r)}const n=(e==null?void 0:e.message)||"";if(n.includes("gas limit too high")||n.includes("21000000")||n.includes("Internal JSON-RPC error")){const s=t?` (error code: ${t})`:"";throw console.error("[Contract Error] Transaction rejected. Error code:",t,"Message:",n),new Error(`Transaction failed: The contract rejected this transaction${s}. Possible causes: (1) Vault already exists for this transaction, (2) Invalid signature, (3) Unauthorized caller. Please check your transaction parameters and try again.`)}throw e instanceof Error?(console.error("[Contract Error] Unhandled error:",e.message),e):new Error(`Contract call failed: ${String(e)}`)}const en=0,X="00".repeat(32);function nn(e,t,n,s){const r=n==null?void 0:n[`${e}:${t}`];return r?Promise.resolve({txid:e,vout:t,value:r.value,scriptPubKey:r.scriptPubKey}):_t.getUtxoInfo(e,t,s)}const kt=12e4;class sn{constructor(t){b(this,"config");this.config=t}async preparePegin(t){if(t.amounts.length===0)throw new Error("amounts must contain at least one entry");const n=await this.config.btcWallet.getPublicKeyHex(),s=L(n),r=await this.prepareSizing(s,t),o=r.selectedUTXOs.map(p=>({txid:d.hexToUint8Array(p.txid),vout:p.vout})),i=await Nt(this.config.btcWallet,{depositorBtcPubkey:d.hexToUint8Array(s),fundingOutpoints:o});let a,l;try{const p=Bt(i);try{a=d.uint8ArrayToHex(p),l=d.uint8ArrayToHex(h.sha256(p))}finally{p.fill(0)}}catch(p){throw i.fill(0),p}const u=await je(i,t.amounts.length),{perVaultWotsKeys:c,wotsPkHashes:w,htlcSecretHexes:y,hashlocks:P}=u,f=await this.preparePeginCommit({depositorBtcPubkeyRaw:n,depositorBtcPubkey:s,hashlocks:P,authAnchorHash:l,sizing:r,params:t});for(let p=0;p<f.perVault.length;p++)if(f.perVault[p].htlcVout!==p)throw new Error(`Internal invariant violation: htlcVout/index mismatch at vault ${p} (expected ${p}, got ${f.perVault[p].htlcVout})`);return he(f.fundedPrePeginTxHex,t.amounts.length,l),{transaction:{...f,selectedUTXOs:r.selectedUTXOs,fee:r.fee,changeAmount:r.changeAmount},depositorBtcPubkey:s,derivedSecrets:{perVaultWotsKeys:c,wotsPkHashes:w,htlcSecretHexes:y,authAnchorHex:a}}}async prepareSizing(t,n){const s=n.amounts.map(()=>X),r=n.vaultKeeperBtcPubkeys.length,o=await O.buildPrePeginPsbt({depositorPubkey:t,vaultProviderPubkey:d.stripHexPrefix(n.vaultProviderBtcPubkey),vaultKeeperPubkeys:n.vaultKeeperBtcPubkeys.map(d.stripHexPrefix),universalChallengerPubkeys:n.universalChallengerBtcPubkeys.map(d.stripHexPrefix),hashlocks:s,timelockRefund:n.timelockRefund,pegInAmounts:n.amounts,feeRate:n.protocolFeeRate,numLocalChallengers:r,councilQuorum:n.councilQuorum,councilSize:n.councilSize,network:this.config.btcNetwork,authAnchorHash:X}),i=R.selectUtxosForPegin([...n.availableUTXOs],o.totalOutputValue,n.mempoolFeeRate,ut.peginOutputCount(o.htlcValues.length,X));return{selectedUTXOs:i.selectedUTXOs,fee:i.fee,changeAmount:i.changeAmount}}async preparePeginCommit(t){const{depositorBtcPubkeyRaw:n,depositorBtcPubkey:s,hashlocks:r,authAnchorHash:o,sizing:i,params:a}=t,l=X.toLowerCase();for(let x=0;x<r.length;x++)if(r[x].toLowerCase()===l)throw new Error(`preparePeginCommit refusing to build with sizing-pass placeholder hashlock at vault ${x} — internal substitution bug`);if(o.toLowerCase()===l)throw new Error("preparePeginCommit refusing to build with sizing-pass placeholder auth-anchor hash — internal substitution bug");const u=d.stripHexPrefix(a.vaultProviderBtcPubkey),c=a.vaultKeeperBtcPubkeys.map(d.stripHexPrefix),w=a.universalChallengerBtcPubkeys.map(d.stripHexPrefix),y=c.length,P={depositorPubkey:s,vaultProviderPubkey:u,vaultKeeperPubkeys:c,universalChallengerPubkeys:w,hashlocks:r,timelockRefund:a.timelockRefund,pegInAmounts:a.amounts,feeRate:a.protocolFeeRate,numLocalChallengers:y,councilQuorum:a.councilQuorum,councilSize:a.councilSize,network:this.config.btcNetwork,authAnchorHash:o},f=await O.buildPrePeginPsbt(P),p=d.getNetwork(this.config.btcNetwork),_=ut.fundPeginTransaction({unfundedTxHex:f.psbtHex,selectedUTXOs:i.selectedUTXOs,changeAddress:a.changeAddress,changeAmount:i.changeAmount,network:p}),A=d.stripHexPrefix(R.calculateBtcTxHash(_)),g=[],T=[],m=[];for(let x=0;x<r.length;x++){const H=await O.buildPeginTxFromFundedPrePegin({prePeginParams:P,timelockPegin:a.timelockPegin,fundedPrePeginTxHex:_,htlcVout:x}),B=await O.buildPeginInputPsbt({peginTxHex:H.txHex,fundedPrePeginTxHex:_,depositorPubkey:s,vaultProviderPubkey:u,vaultKeeperPubkeys:c,universalChallengerPubkeys:w,hashlock:r[x],timelockRefund:a.timelockRefund,network:this.config.btcNetwork});g.push(H),T.push(B.psbtHex),m.push(te.createTaprootScriptPathSignOptions(n,1))}const k=await Qe(this.config.btcWallet,T,m),E=[];for(let x=0;x<k.length;x++){const H=O.extractPeginInputSignature(k[x],s),B=O.finalizePeginInputPsbt(k[x]);E.push({htlcVout:x,htlcValue:f.htlcValues[x],peginTxHex:B,peginTxid:g[x].txid,peginInputSignature:H,vaultScriptPubKey:g[x].vaultScriptPubKey})}return{fundedPrePeginTxHex:_,prePeginTxid:A,perVault:E}}async signAndBroadcast(t){const{fundedPrePeginTxHex:n,depositorBtcPubkey:s}=t,r=n.startsWith("0x")?n.slice(2):n,o=V.Transaction.fromHex(r);if(o.ins.length===0)throw new Error("Transaction has no inputs");const i=new V.Psbt;i.setVersion(o.version),i.setLocktime(o.locktime);const a=K.Buffer.from(L(s),"hex"),l=this.config.mempoolApiUrl,u=o.ins.map(g=>{const T=K.Buffer.from(g.hash).reverse().toString("hex"),m=g.index;return nn(T,m,t.localPrevouts,l).then(k=>({input:g,utxoData:k,txid:T,vout:m}))}),c=await Promise.all(u),w=c.reduce((g,T)=>g+BigInt(T.utxoData.value),0n),y=o.outs.reduce((g,T)=>g+BigInt(T.value),0n);if(w<y)throw new Error(`UTXO value mismatch: total input value (${w} sat) is less than total output value (${y} sat). This may indicate the mempool API returned manipulated UTXO data.`);const P=w-y;if(P>lt.MAX_REASONABLE_FEE_SATS)throw new Error(`Implied transaction fee (${P} sat) exceeds maximum reasonable fee (${lt.MAX_REASONABLE_FEE_SATS} sat). This may indicate manipulated UTXO data.`);for(const{input:g,utxoData:T,txid:m,vout:k}of c){const E=R.getPsbtInputFields({value:T.value,scriptPubKey:T.scriptPubKey},a);i.addInput({hash:g.hash,index:g.index,sequence:g.sequence,...E})}for(const g of o.outs)i.addOutput({script:g.script,value:g.value});const f=await this.config.btcWallet.signPsbt(i.toHex()),p=V.Psbt.fromHex(f);try{p.finalizeAllInputs()}catch(g){if(!p.data.inputs.every(m=>m.finalScriptWitness||m.finalScriptSig))throw new Error(`PSBT finalization failed and wallet did not auto-finalize: ${g}`)}const _=p.extractTransaction().toHex();return await _t.pushTx(_,l)}async registerPeginOnChain(t){const{unsignedPrePeginTx:n,depositorSignedPeginTx:s,vaultProvider:r,hashlock:o,htlcVout:i,depositorPayoutBtcAddress:a,depositorWotsPkHash:l,popSignature:u}=t;if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const c=this.config.ethWallet.account.address;if(!I.isAddressEqual(u.depositorEthAddress,c))throw new Error(`Proof of possession was signed for ${u.depositorEthAddress} but the Ethereum wallet is currently connected to ${c}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(u);const w=u.btcPopSignature,y=d.ensureHexPrefix(u.depositorBtcPubkey),P=d.ensureHexPrefix(n),f=d.ensureHexPrefix(s),p=await this.resolvePayoutScriptPubKey(a),_=R.calculateBtcTxHash(f),A=await ct.deriveVaultId(d.stripHexPrefix(_),d.stripHexPrefix(c)),g=d.ensureHexPrefix(A);if(await this.checkVaultExists(g))throw new Error(`Vault already exists (ID: ${g}, peginTxHash: ${_}). Vault IDs are derived from the pegin transaction hash and depositor address. To create a new vault, use different UTXOs or a different amount to generate a unique transaction.`);const m=I.createPublicClient({chain:this.config.ethChain,transport:I.http()});let k;try{k=await m.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:$.BTCVaultRegistryABI,functionName:"getPegInFee",args:[r]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const E=I.encodeFunctionData({abi:$.BTCVaultRegistryABI,functionName:"submitPeginRequest",args:[c,y,w,P,f,r,o,i,p,l]});let x;try{x=await m.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:k,account:this.config.ethWallet.account.address})}catch(j){S(j)}let H;try{H=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:k,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:x})}catch(j){S(j)}const B=await m.waitForTransactionReceipt({hash:H,timeout:kt});return B.status==="reverted"&&S(new Error(`Transaction reverted. Hash: ${H}. Check the transaction on block explorer for details.`)),{ethTxHash:B.transactionHash,vaultId:g,peginTxHash:_}}async registerPeginBatchOnChain(t){const{vaultProvider:n,unsignedPrePeginTx:s,requests:r,popSignature:o}=t;if(r.length===0)throw new Error("Batch pegin requires at least one request");if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const i=this.config.ethWallet.account.address;if(!I.isAddressEqual(o.depositorEthAddress,i))throw new Error(`Proof of possession was signed for ${o.depositorEthAddress} but the Ethereum wallet is currently connected to ${i}. Reconnect the original account or call signProofOfPossession() again.`);await this.assertPopMatchesBtcWallet(o);const a=o.btcPopSignature,l=[];for(const m of r)l.push(await this.resolvePayoutScriptPubKey(m.depositorPayoutBtcAddress));const u=[];for(const m of r){const k=d.ensureHexPrefix(m.depositorSignedPeginTx),E=R.calculateBtcTxHash(k),x=await ct.deriveVaultId(d.stripHexPrefix(E),d.stripHexPrefix(i)),H=d.ensureHexPrefix(x);if(await this.checkVaultExists(H))throw new Error(`Vault already exists (ID: ${H}, peginTxHash: ${E}). To create a new vault, use different UTXOs or a different amount.`);u.push({vaultId:H,peginTxHash:E})}const c=I.createPublicClient({chain:this.config.ethChain,transport:I.http()});let w;try{w=await c.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:$.BTCVaultRegistryABI,functionName:"getPegInFee",args:[n]})}catch{throw new Error("Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct.")}const y=w*BigInt(r.length),P=d.ensureHexPrefix(o.depositorBtcPubkey),f=d.ensureHexPrefix(s),p=r.map((m,k)=>({depositorBtcPubKey:P,btcPopSignature:a,unsignedPrePeginTx:f,depositorSignedPeginTx:d.ensureHexPrefix(m.depositorSignedPeginTx),hashlock:m.hashlock,htlcVout:m.htlcVout,referralCode:en,depositorPayoutBtcAddress:l[k],depositorWotsPkHash:m.depositorWotsPkHash})),_=I.encodeFunctionData({abi:$.BTCVaultRegistryABI,functionName:"submitPeginRequestBatch",args:[i,n,p]});let A;try{A=await c.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:_,value:y,account:this.config.ethWallet.account.address})}catch(m){S(m)}let g;try{g=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:_,value:y,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:A})}catch(m){S(m)}const T=await c.waitForTransactionReceipt({hash:g,timeout:kt});return T.status==="reverted"&&S(new Error(`Batch transaction reverted. Hash: ${g}. Check the transaction on block explorer for details.`)),{ethTxHash:T.transactionHash,vaults:u}}async checkVaultExists(t){try{return(await I.createPublicClient({chain:this.config.ethChain,transport:I.http()}).readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:$.BTCVaultRegistryABI,functionName:"getBtcVaultBasicInfo",args:[t]})).depositor!==I.zeroAddress}catch{return!1}}async resolvePayoutScriptPubKey(t){let n;if(t)n=t;else{n=await this.config.btcWallet.getAddress();const r=await this.config.btcWallet.getPublicKeyHex();if(!d.isAddressFromPublicKey(n,r,this.config.btcNetwork))throw new Error("The BTC address from your wallet does not match the wallet's public key. Please ensure your wallet is using a supported address type (Taproot or Native SegWit).")}const s=d.getNetwork(this.config.btcNetwork);try{return`0x${Ht.address.toOutputScript(n,s).toString("hex")}`}catch{throw new Error(`Invalid BTC payout address: "${n}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`)}}async signProofOfPossession(){if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const t=this.config.ethWallet.account.address,n=L(await this.config.btcWallet.getPublicKeyHex()),s=this.config.vaultContracts.btcVaultRegistry,r=`${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`,o=await this.config.btcWallet.signMessage(r,"bip322-simple");return{btcPopSignature:Ye(o),depositorEthAddress:t,depositorBtcPubkey:n}}async assertPopMatchesBtcWallet(t){const n=L(await this.config.btcWallet.getPublicKeyHex()),s=L(t.depositorBtcPubkey);if(n!==s)throw new Error(`Proof of possession was signed with BTC pubkey ${s} but the BTC wallet is currently connected to ${n}. Reconnect the original wallet or call signProofOfPossession() again.`)}getNetwork(){return this.config.btcNetwork}getVaultContractAddress(){return this.config.vaultContracts.btcVaultRegistry}}exports.CONTRACT_ERRORS=C;exports.PeginManager=sn;exports.VAULT_APP_NAME=Ut;exports.buildFundingOutpointsCommitment=Rt;exports.buildVaultContext=$t;exports.computeWotsBlockPublicKeysHash=Yt;exports.deriveVaultRoot=Nt;exports.deriveWotsBlocksFromSeed=Zt;exports.expandAuthAnchor=Bt;exports.expandHashlockSecret=St;exports.expandWotsSeed=Ct;exports.extractErrorData=q;exports.getContractErrorMessage=Je;exports.handleContractError=S;exports.isKnownContractError=tn;exports.isWotsMismatchError=qe;exports.parseFundingOutpointsFromTx=Pe;
|
|
2
|
+
//# sourceMappingURL=PeginManager-D2pBM0E-.cjs.map
|