@babylonlabs-io/ts-sdk 0.17.1 → 0.18.0
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/{errors-DKLboMnq.cjs → errors-DAKpPfl7.cjs} +2 -2
- package/dist/{errors-DKLboMnq.cjs.map → errors-DAKpPfl7.cjs.map} +1 -1
- package/dist/{errors-KY63mgWT.js → errors-DARTQrP3.js} +2 -2
- package/dist/{errors-KY63mgWT.js.map → errors-DARTQrP3.js.map} +1 -1
- package/dist/index-DKfhZvdn.cjs +2 -0
- package/dist/index-DKfhZvdn.cjs.map +1 -0
- package/dist/{index-Dw7FAwne.js → index-qIcjkxLw.js} +135 -137
- package/dist/index-qIcjkxLw.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +2 -2
- package/dist/tbv/core/clients/eth/vault-registry-reader.d.ts.map +1 -1
- package/dist/tbv/core/clients/index.cjs +1 -1
- package/dist/tbv/core/clients/index.js +1 -1
- package/dist/tbv/core/contracts/abis/BTCVaultRegistry.abi.d.ts +54 -0
- package/dist/tbv/core/contracts/abis/BTCVaultRegistry.abi.d.ts.map +1 -1
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +2 -2
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +2 -2
- package/dist/tbv/integrations/aave/clients/abis/AaveSpoke.abi.json.d.ts +140 -0
- package/dist/tbv/integrations/aave/clients/index.d.ts +1 -1
- package/dist/tbv/integrations/aave/clients/index.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/clients/query.d.ts +12 -1
- package/dist/tbv/integrations/aave/clients/query.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/index.cjs +1 -1
- package/dist/tbv/integrations/aave/index.cjs.map +1 -1
- package/dist/tbv/integrations/aave/index.d.ts +2 -2
- package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/index.js +141 -129
- package/dist/tbv/integrations/aave/index.js.map +1 -1
- package/dist/tbv/integrations/aave/types.d.ts +10 -0
- package/dist/tbv/integrations/aave/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/index-D3z3SZAj.cjs +0 -2
- package/dist/index-D3z3SZAj.cjs.map +0 -1
- package/dist/index-Dw7FAwne.js.map +0 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var Yt=Object.defineProperty;var te=(e,t,n)=>t in e?Yt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var f=(e,t,n)=>te(e,typeof t!="symbol"?t+"":t,n);const it=require("bitcoinjs-lib"),lt=require("buffer"),S=require("viem"),ee=require("./signing-Deg5lCoC.cjs"),V=require("./index-D3z3SZAj.cjs"),T=require("./constants-EiyZkXce.cjs"),wt=require("@babylonlabs-io/babylon-tbv-rust-wasm"),X=require("./challengeAssert-D_k_ADgP.cjs"),z=require("./psbtInputFields-C0nKn_GD.cjs"),ne=require("./fundPeginTransaction-EbrZzlrh.cjs");function se(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 oe=se(it),$={"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."};function ct(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 o=5;for(;n&&typeof n=="object"&&s<o;){const c=n;if(typeof c.data=="string"&&c.data.startsWith("0x"))return c.data;n=c.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 ie(e){const t=ct(e);if(t){const n=t.substring(0,10);return $[t]??$[n]}}function re(e){const t=ct(e);if(t===void 0)return!1;const n=t.substring(0,10);return t in $||n in $}function D(e){console.error("[Contract Error] Raw error:",e);const t=ct(e);if(console.error("[Contract Error] Extracted error data:",t),t){const s=t.substring(0,10),o=$[t]??$[s];if(o)throw console.error("[Contract Error] Known error:",o),new Error(o)}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 ce=0;function ae(e,t,n,s){const o=n==null?void 0:n[`${e}:${t}`];return o?Promise.resolve({txid:e,vout:t,value:o.value,scriptPubKey:o.scriptPubKey}):V.getUtxoInfo(e,t,s)}const Pt=12e4;class he{constructor(t){f(this,"config");this.config=t}async preparePegin(t){const n=await this.config.btcWallet.getPublicKeyHex(),s=n.length===66?n.slice(2):n,o=T.stripHexPrefix(t.vaultProviderBtcPubkey),r=t.vaultKeeperBtcPubkeys.map(T.stripHexPrefix),i=t.universalChallengerBtcPubkeys.map(T.stripHexPrefix);if(t.hashlocks.length!==t.amounts.length)throw new Error(`hashlocks.length (${t.hashlocks.length}) must equal amounts.length (${t.amounts.length})`);if(t.hashlocks.length===0)throw new Error("hashlocks must contain at least one entry");const c=r.length,h={depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlocks:t.hashlocks,timelockRefund:t.timelockRefund,pegInAmounts:t.amounts,feeRate:t.protocolFeeRate,numLocalChallengers:c,councilQuorum:t.councilQuorum,councilSize:t.councilSize,network:this.config.btcNetwork},x=await X.buildPrePeginPsbt(h),d=z.selectUtxosForPegin([...t.availableUTXOs],x.totalOutputValue,t.mempoolFeeRate,T.peginOutputCount(x.htlcValues.length)),a=T.getNetwork(this.config.btcNetwork),u=ne.fundPeginTransaction({unfundedTxHex:x.psbtHex,selectedUTXOs:d.selectedUTXOs,changeAddress:t.changeAddress,changeAmount:d.changeAmount,network:a}),p=T.stripHexPrefix(z.calculateBtcTxHash(u)),g=[],y=[],m=[];for(let l=0;l<t.hashlocks.length;l++){const w=await X.buildPeginTxFromFundedPrePegin({prePeginParams:h,timelockPegin:t.timelockPegin,fundedPrePeginTxHex:u,htlcVout:l}),k=await X.buildPeginInputPsbt({peginTxHex:w.txHex,fundedPrePeginTxHex:u,depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlock:t.hashlocks[l],timelockRefund:t.timelockRefund,network:this.config.btcNetwork});g.push(w),y.push(k.psbtHex),m.push(ee.createTaprootScriptPathSignOptions(n,1))}const P=await this.signPsbtsWithFallback(y,m),b=[];for(let l=0;l<P.length;l++){const w=X.extractPeginInputSignature(P[l],s),k=X.finalizePeginInputPsbt(P[l]);b.push({htlcVout:l,htlcValue:x.htlcValues[l],peginTxHex:k,peginTxid:g[l].txid,peginInputSignature:w,vaultScriptPubKey:g[l].vaultScriptPubKey})}return{fundedPrePeginTxHex:u,prePeginTxid:p,unsignedPrePeginTxHex:x.psbtHex,perVault:b,selectedUTXOs:d.selectedUTXOs,fee:d.fee,changeAmount:d.changeAmount}}async signPsbtsWithFallback(t,n){if(typeof this.config.btcWallet.signPsbts=="function"){const o=await this.config.btcWallet.signPsbts(t,n);if(o.length!==t.length)throw new Error(`Expected ${t.length} signed PSBTs but received ${o.length}`);return o}const s=[];for(let o=0;o<t.length;o++){const r=await this.config.btcWallet.signPsbt(t[o],n[o]);s.push(r)}return s}async signAndBroadcast(t){const{fundedPrePeginTxHex:n,depositorBtcPubkey:s}=t,o=n.startsWith("0x")?n.slice(2):n,r=it.Transaction.fromHex(o);if(r.ins.length===0)throw new Error("Transaction has no inputs");const i=new it.Psbt;i.setVersion(r.version),i.setLocktime(r.locktime);const c=s.startsWith("0x")?s.slice(2):s;if(c.length!==64||!/^[0-9a-fA-F]+$/.test(c))throw new Error("Invalid depositorBtcPubkey: expected 64 hex characters (x-only pubkey)");const h=lt.Buffer.from(c,"hex");if(h.length!==32)throw new Error(`Invalid depositorBtcPubkey length: expected 32 bytes, got ${h.length}`);const x=this.config.mempoolApiUrl,d=r.ins.map(b=>{const l=lt.Buffer.from(b.hash).reverse().toString("hex"),w=b.index;return ae(l,w,t.localPrevouts,x).then(k=>({input:b,utxoData:k,txid:l,vout:w}))}),a=await Promise.all(d),u=a.reduce((b,l)=>b+BigInt(l.utxoData.value),0n),p=r.outs.reduce((b,l)=>b+BigInt(l.value),0n);if(u<p)throw new Error(`UTXO value mismatch: total input value (${u} sat) is less than total output value (${p} sat). This may indicate the mempool API returned manipulated UTXO data.`);for(const{input:b,utxoData:l,txid:w,vout:k}of a){const H=z.getPsbtInputFields({value:l.value,scriptPubKey:l.scriptPubKey},h);i.addInput({hash:b.hash,index:b.index,sequence:b.sequence,...H})}for(const b of r.outs)i.addOutput({script:b.script,value:b.value});const g=await this.config.btcWallet.signPsbt(i.toHex()),y=it.Psbt.fromHex(g);try{y.finalizeAllInputs()}catch(b){if(!y.data.inputs.every(w=>w.finalScriptWitness||w.finalScriptSig))throw new Error(`PSBT finalization failed and wallet did not auto-finalize: ${b}`)}const m=y.extractTransaction().toHex();return await V.pushTx(m,x)}async registerPeginOnChain(t){const{depositorBtcPubkey:n,unsignedPrePeginTx:s,depositorSignedPeginTx:o,vaultProvider:r,hashlock:i,htlcVout:c,onPopSigned:h,depositorPayoutBtcAddress:x,depositorWotsPkHash:d,preSignedBtcPopSignature:a}=t;if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const u=this.config.ethWallet.account.address,p=await this.resolvePopSignature(u,a);h&&await h();const g=T.ensureHexPrefix(n),y=T.ensureHexPrefix(s),m=T.ensureHexPrefix(o),P=await this.resolvePayoutScriptPubKey(x),b=z.calculateBtcTxHash(m),l=await wt.deriveVaultId(T.stripHexPrefix(b),T.stripHexPrefix(u)),w=T.ensureHexPrefix(l);if(await this.checkVaultExists(w))throw new Error(`Vault already exists (ID: ${w}, peginTxHash: ${b}). 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 H=S.createPublicClient({chain:this.config.ethChain,transport:S.http()});let v;try{v=await H.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:V.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=S.encodeFunctionData({abi:V.BTCVaultRegistryABI,functionName:"submitPeginRequest",args:[u,g,p,y,m,r,i,c,P,d]});let A;try{A=await H.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:v,account:this.config.ethWallet.account.address})}catch(U){D(U)}let _;try{_=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:v,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:A})}catch(U){D(U)}const M=await H.waitForTransactionReceipt({hash:_,timeout:Pt});return M.status==="reverted"&&D(new Error(`Transaction reverted. Hash: ${_}. Check the transaction on block explorer for details.`)),{ethTxHash:M.transactionHash,vaultId:w,peginTxHash:b,btcPopSignature:p}}async registerPeginBatchOnChain(t){const{vaultProvider:n,requests:s,preSignedBtcPopSignature:o,onPopSigned:r}=t;if(s.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,c=await this.resolvePopSignature(i,o);r&&await r();const h=[];for(const b of s)h.push(await this.resolvePayoutScriptPubKey(b.depositorPayoutBtcAddress));const x=[];for(const b of s){const l=T.ensureHexPrefix(b.depositorSignedPeginTx),w=z.calculateBtcTxHash(l),k=await wt.deriveVaultId(T.stripHexPrefix(w),T.stripHexPrefix(i)),H=T.ensureHexPrefix(k);if(await this.checkVaultExists(H))throw new Error(`Vault already exists (ID: ${H}, peginTxHash: ${w}). To create a new vault, use different UTXOs or a different amount.`);x.push({vaultId:H,peginTxHash:w})}const d=S.createPublicClient({chain:this.config.ethChain,transport:S.http()});let a;try{a=await d.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:V.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 u=a*BigInt(s.length),p=s.map((b,l)=>({depositorBtcPubKey:T.ensureHexPrefix(b.depositorBtcPubkey),btcPopSignature:c,unsignedPrePeginTx:T.ensureHexPrefix(b.unsignedPrePeginTx),depositorSignedPeginTx:T.ensureHexPrefix(b.depositorSignedPeginTx),hashlock:b.hashlock,htlcVout:b.htlcVout,referralCode:ce,depositorPayoutBtcAddress:h[l],depositorWotsPkHash:b.depositorWotsPkHash})),g=S.encodeFunctionData({abi:V.BTCVaultRegistryABI,functionName:"submitPeginRequestBatch",args:[i,n,p]});let y;try{y=await d.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:g,value:u,account:this.config.ethWallet.account.address})}catch(b){D(b)}let m;try{m=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:g,value:u,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:y})}catch(b){D(b)}const P=await d.waitForTransactionReceipt({hash:m,timeout:Pt});return P.status==="reverted"&&D(new Error(`Batch transaction reverted. Hash: ${m}. Check the transaction on block explorer for details.`)),{ethTxHash:P.transactionHash,vaults:x,btcPopSignature:c}}async checkVaultExists(t){try{return(await S.createPublicClient({chain:this.config.ethChain,transport:S.http()}).readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:V.BTCVaultRegistryABI,functionName:"getBtcVaultBasicInfo",args:[t]}))[0]!==S.zeroAddress}catch{return!1}}async resolvePayoutScriptPubKey(t){let n;if(t)n=t;else{n=await this.config.btcWallet.getAddress();const o=await this.config.btcWallet.getPublicKeyHex();if(!T.isAddressFromPublicKey(n,o,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=T.getNetwork(this.config.btcNetwork);try{return`0x${oe.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 resolvePopSignature(t,n){if(n)return n;const s=this.config.vaultContracts.btcVaultRegistry,o=`${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`,r=await this.config.btcWallet.signMessage(o,"bip322-simple");return r.startsWith("0x")?r:`0x${lt.Buffer.from(r,"base64").toString("hex")}`}getNetwork(){return this.config.btcNetwork}getVaultContractAddress(){return this.config.vaultContracts.btcVaultRegistry}}function le(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function K(e,t=""){if(!Number.isSafeInteger(e)||e<0){const n=t&&`"${t}" `;throw new Error(`${n}expected integer >= 0, got ${e}`)}}function N(e,t,n=""){const s=le(e),o=e==null?void 0:e.length,r=t!==void 0;if(!s||r&&o!==t){const i=n&&`"${n}" `,c=r?` of length ${t}`:"",h=s?`length=${o}`:`type=${typeof e}`;throw new Error(i+"expected Uint8Array"+c+", got "+h)}return e}function Lt(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash must wrapped by utils.createHasher");K(e.outputLen),K(e.blockLen)}function G(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function _t(e,t){N(e,void 0,"digestInto() output");const n=t.outputLen;if(e.length<n)throw new Error('"digestInto() output" expected to be of length >='+n)}function ue(e){return new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4))}function B(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function rt(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function C(e,t){return e<<32-t|e>>>t}function Q(e,t){return e<<t|e>>>32-t>>>0}const fe=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function de(e){return e<<24&4278190080|e<<8&16711680|e>>>8&65280|e>>>24&255}function xe(e){for(let t=0;t<e.length;t++)e[t]=de(e[t]);return e}const mt=fe?e=>e:xe;function be(e){if(typeof e!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(e))}function Ht(e,t=""){return typeof e=="string"?be(e):N(e,void 0,t)}function ge(e,t){if(t!==void 0&&{}.toString.call(t)!=="[object Object]")throw new Error("options must be object or undefined");return Object.assign(e,t)}function at(e,t={}){const n=(o,r)=>e(r).update(o).digest(),s=e(void 0);return n.outputLen=s.outputLen,n.blockLen=s.blockLen,n.create=o=>e(o),Object.assign(n,t),Object.freeze(n)}const Ut=e=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,e])});class Ot{constructor(t,n){f(this,"oHash");f(this,"iHash");f(this,"blockLen");f(this,"outputLen");f(this,"finished",!1);f(this,"destroyed",!1);if(Lt(t),N(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,o=new Uint8Array(s);o.set(n.length>s?t.create().update(n).digest():n);for(let r=0;r<o.length;r++)o[r]^=54;this.iHash.update(o),this.oHash=t.create();for(let r=0;r<o.length;r++)o[r]^=106;this.oHash.update(o),B(o)}update(t){return G(this),this.iHash.update(t),this}digestInto(t){G(this),N(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:o,destroyed:r,blockLen:i,outputLen:c}=this;return t=t,t.finished=o,t.destroyed=r,t.blockLen=i,t.outputLen=c,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 bt=(e,t,n)=>new Ot(e,t).update(n).digest();bt.create=(e,t)=>new Ot(e,t);function pe(e,t,n){return e&t^~e&n}function ye(e,t,n){return e&t^e&n^t&n}class gt{constructor(t,n,s,o){f(this,"blockLen");f(this,"outputLen");f(this,"padOffset");f(this,"isLE");f(this,"buffer");f(this,"view");f(this,"finished",!1);f(this,"length",0);f(this,"pos",0);f(this,"destroyed",!1);this.blockLen=t,this.outputLen=n,this.padOffset=s,this.isLE=o,this.buffer=new Uint8Array(t),this.view=rt(this.buffer)}update(t){G(this),N(t);const{view:n,buffer:s,blockLen:o}=this,r=t.length;for(let i=0;i<r;){const c=Math.min(o-this.pos,r-i);if(c===o){const h=rt(t);for(;o<=r-i;i+=o)this.process(h,i);continue}s.set(t.subarray(i,i+c),this.pos),this.pos+=c,i+=c,this.pos===o&&(this.process(n,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){G(this),_t(t,this),this.finished=!0;const{buffer:n,view:s,blockLen:o,isLE:r}=this;let{pos:i}=this;n[i++]=128,B(this.buffer.subarray(i)),this.padOffset>o-i&&(this.process(s,0),i=0);for(let a=i;a<o;a++)n[a]=0;s.setBigUint64(o-8,BigInt(this.length*8),r),this.process(s,0);const c=rt(t),h=this.outputLen;if(h%4)throw new Error("_sha2: outputLen must be aligned to 32bit");const x=h/4,d=this.get();if(x>d.length)throw new Error("_sha2: outputLen bigger than state");for(let a=0;a<x;a++)c.setUint32(4*a,d[a],r)}digest(){const{buffer:t,outputLen:n}=this;this.digestInto(t);const s=t.slice(0,n);return this.destroy(),s}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:n,buffer:s,length:o,finished:r,destroyed:i,pos:c}=this;return t.destroyed=i,t.finished=r,t.length=o,t.pos=c,o%n&&t.buffer.set(s),t}clone(){return this._cloneInto()}}const O=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),I=Uint32Array.from([1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209]),we=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),Rt=Uint8Array.from(new Array(16).fill(0).map((e,t)=>t)),Pe=Rt.map(e=>(9*e+5)%16),Ft=(()=>{const n=[[Rt],[Pe]];for(let s=0;s<4;s++)for(let o of n)o.push(o[s].map(r=>we[r]));return n})(),Wt=Ft[0],Vt=Ft[1],Dt=[[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)),me=Wt.map((e,t)=>e.map(n=>Dt[t][n])),He=Vt.map((e,t)=>e.map(n=>Dt[t][n])),ke=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),Te=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function kt(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 Y=new Uint32Array(16);class Ie extends gt{constructor(){super(64,20,8,!0);f(this,"h0",1732584193);f(this,"h1",-271733879);f(this,"h2",-1732584194);f(this,"h3",271733878);f(this,"h4",-1009589776)}get(){const{h0:n,h1:s,h2:o,h3:r,h4:i}=this;return[n,s,o,r,i]}set(n,s,o,r,i){this.h0=n|0,this.h1=s|0,this.h2=o|0,this.h3=r|0,this.h4=i|0}process(n,s){for(let g=0;g<16;g++,s+=4)Y[g]=n.getUint32(s,!0);let o=this.h0|0,r=o,i=this.h1|0,c=i,h=this.h2|0,x=h,d=this.h3|0,a=d,u=this.h4|0,p=u;for(let g=0;g<5;g++){const y=4-g,m=ke[g],P=Te[g],b=Wt[g],l=Vt[g],w=me[g],k=He[g];for(let H=0;H<16;H++){const v=Q(o+kt(g,i,h,d)+Y[b[H]]+m,w[H])+u|0;o=u,u=d,d=Q(h,10)|0,h=i,i=v}for(let H=0;H<16;H++){const v=Q(r+kt(y,c,x,a)+Y[l[H]]+P,k[H])+p|0;r=p,p=a,a=Q(x,10)|0,x=c,c=v}}this.set(this.h1+h+a|0,this.h2+d+p|0,this.h3+u+r|0,this.h4+o+c|0,this.h0+i+x|0)}roundClean(){B(Y)}destroy(){this.destroyed=!0,B(this.buffer),this.set(0,0,0,0,0)}}const ve=at(()=>new Ie),tt=BigInt(2**32-1),Tt=BigInt(32);function Ee(e,t=!1){return t?{h:Number(e&tt),l:Number(e>>Tt&tt)}:{h:Number(e>>Tt&tt)|0,l:Number(e&tt)|0}}function Kt(e,t=!1){const n=e.length;let s=new Uint32Array(n),o=new Uint32Array(n);for(let r=0;r<n;r++){const{h:i,l:c}=Ee(e[r],t);[s[r],o[r]]=[i,c]}return[s,o]}const It=(e,t,n)=>e>>>n,vt=(e,t,n)=>e<<32-n|t>>>n,q=(e,t,n)=>e>>>n|t<<32-n,j=(e,t,n)=>e<<32-n|t>>>n,et=(e,t,n)=>e<<64-n|t>>>n-32,nt=(e,t,n)=>e>>>n-32|t<<64-n,Ae=(e,t,n)=>e<<n|t>>>32-n,Be=(e,t,n)=>t<<n|e>>>32-n,Ce=(e,t,n)=>t<<n-32|e>>>64-n,Se=(e,t,n)=>e<<n-32|t>>>64-n;function L(e,t,n,s){const o=(t>>>0)+(s>>>0);return{h:e+n+(o/2**32|0)|0,l:o|0}}const Le=(e,t,n)=>(e>>>0)+(t>>>0)+(n>>>0),_e=(e,t,n,s)=>t+n+s+(e/2**32|0)|0,Ue=(e,t,n,s)=>(e>>>0)+(t>>>0)+(n>>>0)+(s>>>0),Oe=(e,t,n,s,o)=>t+n+s+o+(e/2**32|0)|0,Re=(e,t,n,s,o)=>(e>>>0)+(t>>>0)+(n>>>0)+(s>>>0)+(o>>>0),Fe=(e,t,n,s,o,r)=>t+n+s+o+r+(e/2**32|0)|0,We=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),R=new Uint32Array(64);class Ve extends gt{constructor(t){super(64,t,8,!1)}get(){const{A:t,B:n,C:s,D:o,E:r,F:i,G:c,H:h}=this;return[t,n,s,o,r,i,c,h]}set(t,n,s,o,r,i,c,h){this.A=t|0,this.B=n|0,this.C=s|0,this.D=o|0,this.E=r|0,this.F=i|0,this.G=c|0,this.H=h|0}process(t,n){for(let a=0;a<16;a++,n+=4)R[a]=t.getUint32(n,!1);for(let a=16;a<64;a++){const u=R[a-15],p=R[a-2],g=C(u,7)^C(u,18)^u>>>3,y=C(p,17)^C(p,19)^p>>>10;R[a]=y+R[a-7]+g+R[a-16]|0}let{A:s,B:o,C:r,D:i,E:c,F:h,G:x,H:d}=this;for(let a=0;a<64;a++){const u=C(c,6)^C(c,11)^C(c,25),p=d+u+pe(c,h,x)+We[a]+R[a]|0,y=(C(s,2)^C(s,13)^C(s,22))+ye(s,o,r)|0;d=x,x=h,h=c,c=i+p|0,i=r,r=o,o=s,s=p+y|0}s=s+this.A|0,o=o+this.B|0,r=r+this.C|0,i=i+this.D|0,c=c+this.E|0,h=h+this.F|0,x=x+this.G|0,d=d+this.H|0,this.set(s,o,r,i,c,h,x,d)}roundClean(){B(R)}destroy(){this.set(0,0,0,0,0,0,0,0),B(this.buffer)}}class De extends Ve{constructor(){super(32);f(this,"A",O[0]|0);f(this,"B",O[1]|0);f(this,"C",O[2]|0);f(this,"D",O[3]|0);f(this,"E",O[4]|0);f(this,"F",O[5]|0);f(this,"G",O[6]|0);f(this,"H",O[7]|0)}}const $t=Kt(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(e=>BigInt(e))),Ke=$t[0],$e=$t[1],F=new Uint32Array(80),W=new Uint32Array(80);class Ne extends gt{constructor(t){super(128,t,16,!1)}get(){const{Ah:t,Al:n,Bh:s,Bl:o,Ch:r,Cl:i,Dh:c,Dl:h,Eh:x,El:d,Fh:a,Fl:u,Gh:p,Gl:g,Hh:y,Hl:m}=this;return[t,n,s,o,r,i,c,h,x,d,a,u,p,g,y,m]}set(t,n,s,o,r,i,c,h,x,d,a,u,p,g,y,m){this.Ah=t|0,this.Al=n|0,this.Bh=s|0,this.Bl=o|0,this.Ch=r|0,this.Cl=i|0,this.Dh=c|0,this.Dl=h|0,this.Eh=x|0,this.El=d|0,this.Fh=a|0,this.Fl=u|0,this.Gh=p|0,this.Gl=g|0,this.Hh=y|0,this.Hl=m|0}process(t,n){for(let l=0;l<16;l++,n+=4)F[l]=t.getUint32(n),W[l]=t.getUint32(n+=4);for(let l=16;l<80;l++){const w=F[l-15]|0,k=W[l-15]|0,H=q(w,k,1)^q(w,k,8)^It(w,k,7),v=j(w,k,1)^j(w,k,8)^vt(w,k,7),E=F[l-2]|0,A=W[l-2]|0,_=q(E,A,19)^et(E,A,61)^It(E,A,6),M=j(E,A,19)^nt(E,A,61)^vt(E,A,6),U=Ue(v,M,W[l-7],W[l-16]),ht=Oe(U,H,_,F[l-7],F[l-16]);F[l]=ht|0,W[l]=U|0}let{Ah:s,Al:o,Bh:r,Bl:i,Ch:c,Cl:h,Dh:x,Dl:d,Eh:a,El:u,Fh:p,Fl:g,Gh:y,Gl:m,Hh:P,Hl:b}=this;for(let l=0;l<80;l++){const w=q(a,u,14)^q(a,u,18)^et(a,u,41),k=j(a,u,14)^j(a,u,18)^nt(a,u,41),H=a&p^~a&y,v=u&g^~u&m,E=Re(b,k,v,$e[l],W[l]),A=Fe(E,P,w,H,Ke[l],F[l]),_=E|0,M=q(s,o,28)^et(s,o,34)^et(s,o,39),U=j(s,o,28)^nt(s,o,34)^nt(s,o,39),ht=s&r^s&c^r&c,Qt=o&i^o&h^i&h;P=y|0,b=m|0,y=p|0,m=g|0,p=a|0,g=u|0,{h:a,l:u}=L(x|0,d|0,A|0,_|0),x=c|0,d=h|0,c=r|0,h=i|0,r=s|0,i=o|0;const yt=Le(_,U,Qt);s=_e(yt,A,M,ht),o=yt|0}({h:s,l:o}=L(this.Ah|0,this.Al|0,s|0,o|0)),{h:r,l:i}=L(this.Bh|0,this.Bl|0,r|0,i|0),{h:c,l:h}=L(this.Ch|0,this.Cl|0,c|0,h|0),{h:x,l:d}=L(this.Dh|0,this.Dl|0,x|0,d|0),{h:a,l:u}=L(this.Eh|0,this.El|0,a|0,u|0),{h:p,l:g}=L(this.Fh|0,this.Fl|0,p|0,g|0),{h:y,l:m}=L(this.Gh|0,this.Gl|0,y|0,m|0),{h:P,l:b}=L(this.Hh|0,this.Hl|0,P|0,b|0),this.set(s,o,r,i,c,h,x,d,a,u,p,g,y,m,P,b)}roundClean(){B(F,W)}destroy(){B(this.buffer),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}}class Me extends Ne{constructor(){super(64);f(this,"Ah",I[0]|0);f(this,"Al",I[1]|0);f(this,"Bh",I[2]|0);f(this,"Bl",I[3]|0);f(this,"Ch",I[4]|0);f(this,"Cl",I[5]|0);f(this,"Dh",I[6]|0);f(this,"Dl",I[7]|0);f(this,"Eh",I[8]|0);f(this,"El",I[9]|0);f(this,"Fh",I[10]|0);f(this,"Fl",I[11]|0);f(this,"Gh",I[12]|0);f(this,"Gl",I[13]|0);f(this,"Hh",I[14]|0);f(this,"Hl",I[15]|0)}}const qe=at(()=>new De,Ut(1)),Nt=at(()=>new Me,Ut(3)),je=BigInt(0),Z=BigInt(1),Ge=BigInt(2),Xe=BigInt(7),ze=BigInt(256),Ze=BigInt(113),Mt=[],qt=[],jt=[];for(let e=0,t=Z,n=1,s=0;e<24;e++){[n,s]=[s,(2*n+3*s)%5],Mt.push(2*(5*s+n)),qt.push((e+1)*(e+2)/2%64);let o=je;for(let r=0;r<7;r++)t=(t<<Z^(t>>Xe)*Ze)%ze,t&Ge&&(o^=Z<<(Z<<BigInt(r))-Z);jt.push(o)}const Gt=Kt(jt,!0),Je=Gt[0],Qe=Gt[1],Et=(e,t,n)=>n>32?Ce(e,t,n):Ae(e,t,n),At=(e,t,n)=>n>32?Se(e,t,n):Be(e,t,n);function Ye(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 c=(i+8)%10,h=(i+2)%10,x=n[h],d=n[h+1],a=Et(x,d,1)^n[c],u=At(x,d,1)^n[c+1];for(let p=0;p<50;p+=10)e[i+p]^=a,e[i+p+1]^=u}let o=e[2],r=e[3];for(let i=0;i<24;i++){const c=qt[i],h=Et(o,r,c),x=At(o,r,c),d=Mt[i];o=e[d],r=e[d+1],e[d]=h,e[d+1]=x}for(let i=0;i<50;i+=10){for(let c=0;c<10;c++)n[c]=e[i+c];for(let c=0;c<10;c++)e[i+c]^=~n[(c+2)%10]&n[(c+4)%10]}e[0]^=Je[s],e[1]^=Qe[s]}B(n)}class pt{constructor(t,n,s,o=!1,r=24){f(this,"state");f(this,"pos",0);f(this,"posOut",0);f(this,"finished",!1);f(this,"state32");f(this,"destroyed",!1);f(this,"blockLen");f(this,"suffix");f(this,"outputLen");f(this,"enableXOF",!1);f(this,"rounds");if(this.blockLen=t,this.suffix=n,this.outputLen=s,this.enableXOF=o,this.rounds=r,K(s,"outputLen"),!(0<t&&t<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=ue(this.state)}clone(){return this._cloneInto()}keccak(){mt(this.state32),Ye(this.state32,this.rounds),mt(this.state32),this.posOut=0,this.pos=0}update(t){G(this),N(t);const{blockLen:n,state:s}=this,o=t.length;for(let r=0;r<o;){const i=Math.min(n-this.pos,o-r);for(let c=0;c<i;c++)s[this.pos++]^=t[r++];this.pos===n&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:n,pos:s,blockLen:o}=this;t[s]^=n,(n&128)!==0&&s===o-1&&this.keccak(),t[o-1]^=128,this.keccak()}writeInto(t){G(this,!1),N(t),this.finish();const n=this.state,{blockLen:s}=this;for(let o=0,r=t.length;o<r;){this.posOut>=s&&this.keccak();const i=Math.min(s-this.posOut,r-o);t.set(n.subarray(this.posOut,this.posOut+i),o),this.posOut+=i,o+=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 K(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(_t(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,B(this.state)}_cloneInto(t){const{blockLen:n,suffix:s,outputLen:o,rounds:r,enableXOF:i}=this;return t||(t=new pt(n,s,o,i,r)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=r,t.suffix=s,t.outputLen=o,t.enableXOF=i,t.destroyed=this.destroyed,t}}const tn=(e,t,n,s={})=>at(()=>new pt(t,e,n),s),en=tn(1,136,32);function nn(e,t,n,s){Lt(e);const o=ge({dkLen:32,asyncTick:10},s),{c:r,dkLen:i,asyncTick:c}=o;if(K(r,"c"),K(i,"dkLen"),K(c,"asyncTick"),r<1)throw new Error("iterations (c) must be >= 1");const h=Ht(t,"password"),x=Ht(n,"salt"),d=new Uint8Array(i),a=bt.create(e,h),u=a._cloneInto().update(x);return{c:r,dkLen:i,asyncTick:c,DK:d,PRF:a,PRFSalt:u}}function sn(e,t,n,s,o){return e.destroy(),t.destroy(),s&&s.destroy(),B(o),n}function on(e,t,n,s){const{c:o,dkLen:r,DK:i,PRF:c,PRFSalt:h}=nn(e,t,n,s);let x;const d=new Uint8Array(4),a=rt(d),u=new Uint8Array(c.outputLen);for(let p=1,g=0;g<r;p++,g+=c.outputLen){const y=i.subarray(g,g+c.outputLen);a.setInt32(0,p,!1),(x=h._cloneInto(x)).update(d).digestInto(u),y.set(u.subarray(0,y.length));for(let m=1;m<o;m++){c._cloneInto(x).update(u).digestInto(u);for(let P=0;P<y.length;P++)y[P]^=u[P]}}return sn(c,h,i,x,u)}function Xt(e){if(typeof e!="string")throw new TypeError("invalid mnemonic type: "+typeof e);return e.normalize("NFKD")}function rn(e){const t=Xt(e),n=t.split(" ");if(![12,15,18,21,24].includes(n.length))throw new Error("Invalid mnemonic");return{nfkd:t,words:n}}const cn=e=>Xt("mnemonic"+e);function an(e,t=""){return on(Nt,rn(e).nfkd,cn(t),{c:2048,dkLen:64})}const hn=508,Bt=16,st=32,ot=64,Ct=5,ln=4;function J(...e){const t=e.reduce((o,r)=>o+r.length,0),n=new Uint8Array(t);let s=0;for(const o of e)n.set(o,s),s+=o.length;return n}function ut(e){return new TextEncoder().encode(e)}function ft(e){const t=new Uint8Array(ln);return new DataView(t.buffer).setUint32(0,e.length,!1),J(t,e)}function dt(e,t){return bt(Nt,e,t)}function St(e){return ve(qe(e))}const xt=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function zt(e){const t=an(e),n=new Uint8Array(t);return t.fill(0),n}async function Zt(e,t,n,s){if(e.length!==ot)throw new Error(`WOTS seed must be ${ot} bytes, got ${e.length}`);t=T.stripHexPrefix(t),n=T.stripHexPrefix(n);const o=e.slice(st,ot),r=e.slice(0,st),i=[o,r];try{const c=J(ft(ut(t)),ft(ut(n)),ft(ut(s))),h=J(r,c);i.push(h);const x=dt(o,h);i.push(x);const d=x.slice(0,st),a=x.slice(st,ot);i.push(d,a);const u=[],p=[],g=[],y=[];let m=!1;try{for(let P=0;P<hn;P++){const b=new Uint8Array(Ct);b[0]=0,new DataView(b.buffer).setUint32(1,P,!1);const l=new Uint8Array(Ct);l[0]=1,new DataView(l.buffer).setUint32(1,P,!1);const w=J(d,b),k=J(d,l),H=dt(a,w),v=dt(a,k);try{const E=H.slice(0,Bt),A=v.slice(0,Bt);u.push(E),p.push(A),g.push(St(E)),y.push(St(A))}finally{w.fill(0),k.fill(0),H.fill(0),v.fill(0)}}return m=!0,{falsePreimages:u,truePreimages:p,falseHashes:g,trueHashes:y}}finally{if(!m){for(const P of u)P.fill(0);for(const P of p)P.fill(0)}}}finally{for(const c of i)c.fill(0)}}function un(e){return{false_list:e.falseHashes.map(xt),true_list:e.trueHashes.map(xt)}}function Jt(e){if(e.falseHashes.length===0||e.trueHashes.length===0)throw new Error("computeWotsPkHash: keypair hash arrays must not be empty");const t=e.falseHashes[0].length,n=(e.falseHashes.length+e.trueHashes.length)*t,s=new Uint8Array(n);let o=0;for(const i of e.falseHashes)s.set(i,o),o+=t;for(const i of e.trueHashes)s.set(i,o),o+=t;const r=en(s);return`0x${xt(r)}`}async function fn(e,t,n,s){const o=zt(e);try{const r=await Zt(o,t,n,s);try{return Jt(r)}finally{for(const i of r.falsePreimages)i.fill(0);for(const i of r.truePreimages)i.fill(0)}}finally{o.fill(0)}}function dn(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")}exports.CONTRACT_ERRORS=$;exports.PeginManager=he;exports.computeWotsPkHash=Jt;exports.deriveWotsKeypair=Zt;exports.deriveWotsPkHash=fn;exports.extractErrorData=ct;exports.getContractErrorMessage=ie;exports.handleContractError=D;exports.isKnownContractError=re;exports.isWotsMismatchError=dn;exports.keypairToPublicKey=un;exports.mnemonicToWotsSeed=zt;
|
|
2
|
-
//# sourceMappingURL=errors-
|
|
1
|
+
"use strict";var Yt=Object.defineProperty;var te=(e,t,n)=>t in e?Yt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var f=(e,t,n)=>te(e,typeof t!="symbol"?t+"":t,n);const it=require("bitcoinjs-lib"),lt=require("buffer"),S=require("viem"),ee=require("./signing-Deg5lCoC.cjs"),V=require("./index-DKfhZvdn.cjs"),T=require("./constants-EiyZkXce.cjs"),wt=require("@babylonlabs-io/babylon-tbv-rust-wasm"),X=require("./challengeAssert-D_k_ADgP.cjs"),z=require("./psbtInputFields-C0nKn_GD.cjs"),ne=require("./fundPeginTransaction-EbrZzlrh.cjs");function se(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 oe=se(it),$={"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."};function ct(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 o=5;for(;n&&typeof n=="object"&&s<o;){const c=n;if(typeof c.data=="string"&&c.data.startsWith("0x"))return c.data;n=c.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 ie(e){const t=ct(e);if(t){const n=t.substring(0,10);return $[t]??$[n]}}function re(e){const t=ct(e);if(t===void 0)return!1;const n=t.substring(0,10);return t in $||n in $}function D(e){console.error("[Contract Error] Raw error:",e);const t=ct(e);if(console.error("[Contract Error] Extracted error data:",t),t){const s=t.substring(0,10),o=$[t]??$[s];if(o)throw console.error("[Contract Error] Known error:",o),new Error(o)}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 ce=0;function ae(e,t,n,s){const o=n==null?void 0:n[`${e}:${t}`];return o?Promise.resolve({txid:e,vout:t,value:o.value,scriptPubKey:o.scriptPubKey}):V.getUtxoInfo(e,t,s)}const Pt=12e4;class he{constructor(t){f(this,"config");this.config=t}async preparePegin(t){const n=await this.config.btcWallet.getPublicKeyHex(),s=n.length===66?n.slice(2):n,o=T.stripHexPrefix(t.vaultProviderBtcPubkey),r=t.vaultKeeperBtcPubkeys.map(T.stripHexPrefix),i=t.universalChallengerBtcPubkeys.map(T.stripHexPrefix);if(t.hashlocks.length!==t.amounts.length)throw new Error(`hashlocks.length (${t.hashlocks.length}) must equal amounts.length (${t.amounts.length})`);if(t.hashlocks.length===0)throw new Error("hashlocks must contain at least one entry");const c=r.length,h={depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlocks:t.hashlocks,timelockRefund:t.timelockRefund,pegInAmounts:t.amounts,feeRate:t.protocolFeeRate,numLocalChallengers:c,councilQuorum:t.councilQuorum,councilSize:t.councilSize,network:this.config.btcNetwork},x=await X.buildPrePeginPsbt(h),d=z.selectUtxosForPegin([...t.availableUTXOs],x.totalOutputValue,t.mempoolFeeRate,T.peginOutputCount(x.htlcValues.length)),a=T.getNetwork(this.config.btcNetwork),u=ne.fundPeginTransaction({unfundedTxHex:x.psbtHex,selectedUTXOs:d.selectedUTXOs,changeAddress:t.changeAddress,changeAmount:d.changeAmount,network:a}),p=T.stripHexPrefix(z.calculateBtcTxHash(u)),g=[],y=[],m=[];for(let l=0;l<t.hashlocks.length;l++){const w=await X.buildPeginTxFromFundedPrePegin({prePeginParams:h,timelockPegin:t.timelockPegin,fundedPrePeginTxHex:u,htlcVout:l}),k=await X.buildPeginInputPsbt({peginTxHex:w.txHex,fundedPrePeginTxHex:u,depositorPubkey:s,vaultProviderPubkey:o,vaultKeeperPubkeys:r,universalChallengerPubkeys:i,hashlock:t.hashlocks[l],timelockRefund:t.timelockRefund,network:this.config.btcNetwork});g.push(w),y.push(k.psbtHex),m.push(ee.createTaprootScriptPathSignOptions(n,1))}const P=await this.signPsbtsWithFallback(y,m),b=[];for(let l=0;l<P.length;l++){const w=X.extractPeginInputSignature(P[l],s),k=X.finalizePeginInputPsbt(P[l]);b.push({htlcVout:l,htlcValue:x.htlcValues[l],peginTxHex:k,peginTxid:g[l].txid,peginInputSignature:w,vaultScriptPubKey:g[l].vaultScriptPubKey})}return{fundedPrePeginTxHex:u,prePeginTxid:p,unsignedPrePeginTxHex:x.psbtHex,perVault:b,selectedUTXOs:d.selectedUTXOs,fee:d.fee,changeAmount:d.changeAmount}}async signPsbtsWithFallback(t,n){if(typeof this.config.btcWallet.signPsbts=="function"){const o=await this.config.btcWallet.signPsbts(t,n);if(o.length!==t.length)throw new Error(`Expected ${t.length} signed PSBTs but received ${o.length}`);return o}const s=[];for(let o=0;o<t.length;o++){const r=await this.config.btcWallet.signPsbt(t[o],n[o]);s.push(r)}return s}async signAndBroadcast(t){const{fundedPrePeginTxHex:n,depositorBtcPubkey:s}=t,o=n.startsWith("0x")?n.slice(2):n,r=it.Transaction.fromHex(o);if(r.ins.length===0)throw new Error("Transaction has no inputs");const i=new it.Psbt;i.setVersion(r.version),i.setLocktime(r.locktime);const c=s.startsWith("0x")?s.slice(2):s;if(c.length!==64||!/^[0-9a-fA-F]+$/.test(c))throw new Error("Invalid depositorBtcPubkey: expected 64 hex characters (x-only pubkey)");const h=lt.Buffer.from(c,"hex");if(h.length!==32)throw new Error(`Invalid depositorBtcPubkey length: expected 32 bytes, got ${h.length}`);const x=this.config.mempoolApiUrl,d=r.ins.map(b=>{const l=lt.Buffer.from(b.hash).reverse().toString("hex"),w=b.index;return ae(l,w,t.localPrevouts,x).then(k=>({input:b,utxoData:k,txid:l,vout:w}))}),a=await Promise.all(d),u=a.reduce((b,l)=>b+BigInt(l.utxoData.value),0n),p=r.outs.reduce((b,l)=>b+BigInt(l.value),0n);if(u<p)throw new Error(`UTXO value mismatch: total input value (${u} sat) is less than total output value (${p} sat). This may indicate the mempool API returned manipulated UTXO data.`);for(const{input:b,utxoData:l,txid:w,vout:k}of a){const H=z.getPsbtInputFields({value:l.value,scriptPubKey:l.scriptPubKey},h);i.addInput({hash:b.hash,index:b.index,sequence:b.sequence,...H})}for(const b of r.outs)i.addOutput({script:b.script,value:b.value});const g=await this.config.btcWallet.signPsbt(i.toHex()),y=it.Psbt.fromHex(g);try{y.finalizeAllInputs()}catch(b){if(!y.data.inputs.every(w=>w.finalScriptWitness||w.finalScriptSig))throw new Error(`PSBT finalization failed and wallet did not auto-finalize: ${b}`)}const m=y.extractTransaction().toHex();return await V.pushTx(m,x)}async registerPeginOnChain(t){const{depositorBtcPubkey:n,unsignedPrePeginTx:s,depositorSignedPeginTx:o,vaultProvider:r,hashlock:i,htlcVout:c,onPopSigned:h,depositorPayoutBtcAddress:x,depositorWotsPkHash:d,preSignedBtcPopSignature:a}=t;if(!this.config.ethWallet.account)throw new Error("Ethereum wallet account not found");const u=this.config.ethWallet.account.address,p=await this.resolvePopSignature(u,a);h&&await h();const g=T.ensureHexPrefix(n),y=T.ensureHexPrefix(s),m=T.ensureHexPrefix(o),P=await this.resolvePayoutScriptPubKey(x),b=z.calculateBtcTxHash(m),l=await wt.deriveVaultId(T.stripHexPrefix(b),T.stripHexPrefix(u)),w=T.ensureHexPrefix(l);if(await this.checkVaultExists(w))throw new Error(`Vault already exists (ID: ${w}, peginTxHash: ${b}). 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 H=S.createPublicClient({chain:this.config.ethChain,transport:S.http()});let v;try{v=await H.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:V.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=S.encodeFunctionData({abi:V.BTCVaultRegistryABI,functionName:"submitPeginRequest",args:[u,g,p,y,m,r,i,c,P,d]});let A;try{A=await H.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:v,account:this.config.ethWallet.account.address})}catch(U){D(U)}let _;try{_=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:E,value:v,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:A})}catch(U){D(U)}const M=await H.waitForTransactionReceipt({hash:_,timeout:Pt});return M.status==="reverted"&&D(new Error(`Transaction reverted. Hash: ${_}. Check the transaction on block explorer for details.`)),{ethTxHash:M.transactionHash,vaultId:w,peginTxHash:b,btcPopSignature:p}}async registerPeginBatchOnChain(t){const{vaultProvider:n,requests:s,preSignedBtcPopSignature:o,onPopSigned:r}=t;if(s.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,c=await this.resolvePopSignature(i,o);r&&await r();const h=[];for(const b of s)h.push(await this.resolvePayoutScriptPubKey(b.depositorPayoutBtcAddress));const x=[];for(const b of s){const l=T.ensureHexPrefix(b.depositorSignedPeginTx),w=z.calculateBtcTxHash(l),k=await wt.deriveVaultId(T.stripHexPrefix(w),T.stripHexPrefix(i)),H=T.ensureHexPrefix(k);if(await this.checkVaultExists(H))throw new Error(`Vault already exists (ID: ${H}, peginTxHash: ${w}). To create a new vault, use different UTXOs or a different amount.`);x.push({vaultId:H,peginTxHash:w})}const d=S.createPublicClient({chain:this.config.ethChain,transport:S.http()});let a;try{a=await d.readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:V.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 u=a*BigInt(s.length),p=s.map((b,l)=>({depositorBtcPubKey:T.ensureHexPrefix(b.depositorBtcPubkey),btcPopSignature:c,unsignedPrePeginTx:T.ensureHexPrefix(b.unsignedPrePeginTx),depositorSignedPeginTx:T.ensureHexPrefix(b.depositorSignedPeginTx),hashlock:b.hashlock,htlcVout:b.htlcVout,referralCode:ce,depositorPayoutBtcAddress:h[l],depositorWotsPkHash:b.depositorWotsPkHash})),g=S.encodeFunctionData({abi:V.BTCVaultRegistryABI,functionName:"submitPeginRequestBatch",args:[i,n,p]});let y;try{y=await d.estimateGas({to:this.config.vaultContracts.btcVaultRegistry,data:g,value:u,account:this.config.ethWallet.account.address})}catch(b){D(b)}let m;try{m=await this.config.ethWallet.sendTransaction({to:this.config.vaultContracts.btcVaultRegistry,data:g,value:u,account:this.config.ethWallet.account,chain:this.config.ethChain,gas:y})}catch(b){D(b)}const P=await d.waitForTransactionReceipt({hash:m,timeout:Pt});return P.status==="reverted"&&D(new Error(`Batch transaction reverted. Hash: ${m}. Check the transaction on block explorer for details.`)),{ethTxHash:P.transactionHash,vaults:x,btcPopSignature:c}}async checkVaultExists(t){try{return(await S.createPublicClient({chain:this.config.ethChain,transport:S.http()}).readContract({address:this.config.vaultContracts.btcVaultRegistry,abi:V.BTCVaultRegistryABI,functionName:"getBtcVaultBasicInfo",args:[t]}))[0]!==S.zeroAddress}catch{return!1}}async resolvePayoutScriptPubKey(t){let n;if(t)n=t;else{n=await this.config.btcWallet.getAddress();const o=await this.config.btcWallet.getPublicKeyHex();if(!T.isAddressFromPublicKey(n,o,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=T.getNetwork(this.config.btcNetwork);try{return`0x${oe.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 resolvePopSignature(t,n){if(n)return n;const s=this.config.vaultContracts.btcVaultRegistry,o=`${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`,r=await this.config.btcWallet.signMessage(o,"bip322-simple");return r.startsWith("0x")?r:`0x${lt.Buffer.from(r,"base64").toString("hex")}`}getNetwork(){return this.config.btcNetwork}getVaultContractAddress(){return this.config.vaultContracts.btcVaultRegistry}}function le(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function K(e,t=""){if(!Number.isSafeInteger(e)||e<0){const n=t&&`"${t}" `;throw new Error(`${n}expected integer >= 0, got ${e}`)}}function N(e,t,n=""){const s=le(e),o=e==null?void 0:e.length,r=t!==void 0;if(!s||r&&o!==t){const i=n&&`"${n}" `,c=r?` of length ${t}`:"",h=s?`length=${o}`:`type=${typeof e}`;throw new Error(i+"expected Uint8Array"+c+", got "+h)}return e}function Lt(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash must wrapped by utils.createHasher");K(e.outputLen),K(e.blockLen)}function G(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function _t(e,t){N(e,void 0,"digestInto() output");const n=t.outputLen;if(e.length<n)throw new Error('"digestInto() output" expected to be of length >='+n)}function ue(e){return new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4))}function B(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function rt(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function C(e,t){return e<<32-t|e>>>t}function Q(e,t){return e<<t|e>>>32-t>>>0}const fe=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function de(e){return e<<24&4278190080|e<<8&16711680|e>>>8&65280|e>>>24&255}function xe(e){for(let t=0;t<e.length;t++)e[t]=de(e[t]);return e}const mt=fe?e=>e:xe;function be(e){if(typeof e!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(e))}function Ht(e,t=""){return typeof e=="string"?be(e):N(e,void 0,t)}function ge(e,t){if(t!==void 0&&{}.toString.call(t)!=="[object Object]")throw new Error("options must be object or undefined");return Object.assign(e,t)}function at(e,t={}){const n=(o,r)=>e(r).update(o).digest(),s=e(void 0);return n.outputLen=s.outputLen,n.blockLen=s.blockLen,n.create=o=>e(o),Object.assign(n,t),Object.freeze(n)}const Ut=e=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,e])});class Ot{constructor(t,n){f(this,"oHash");f(this,"iHash");f(this,"blockLen");f(this,"outputLen");f(this,"finished",!1);f(this,"destroyed",!1);if(Lt(t),N(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,o=new Uint8Array(s);o.set(n.length>s?t.create().update(n).digest():n);for(let r=0;r<o.length;r++)o[r]^=54;this.iHash.update(o),this.oHash=t.create();for(let r=0;r<o.length;r++)o[r]^=106;this.oHash.update(o),B(o)}update(t){return G(this),this.iHash.update(t),this}digestInto(t){G(this),N(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:o,destroyed:r,blockLen:i,outputLen:c}=this;return t=t,t.finished=o,t.destroyed=r,t.blockLen=i,t.outputLen=c,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 bt=(e,t,n)=>new Ot(e,t).update(n).digest();bt.create=(e,t)=>new Ot(e,t);function pe(e,t,n){return e&t^~e&n}function ye(e,t,n){return e&t^e&n^t&n}class gt{constructor(t,n,s,o){f(this,"blockLen");f(this,"outputLen");f(this,"padOffset");f(this,"isLE");f(this,"buffer");f(this,"view");f(this,"finished",!1);f(this,"length",0);f(this,"pos",0);f(this,"destroyed",!1);this.blockLen=t,this.outputLen=n,this.padOffset=s,this.isLE=o,this.buffer=new Uint8Array(t),this.view=rt(this.buffer)}update(t){G(this),N(t);const{view:n,buffer:s,blockLen:o}=this,r=t.length;for(let i=0;i<r;){const c=Math.min(o-this.pos,r-i);if(c===o){const h=rt(t);for(;o<=r-i;i+=o)this.process(h,i);continue}s.set(t.subarray(i,i+c),this.pos),this.pos+=c,i+=c,this.pos===o&&(this.process(n,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){G(this),_t(t,this),this.finished=!0;const{buffer:n,view:s,blockLen:o,isLE:r}=this;let{pos:i}=this;n[i++]=128,B(this.buffer.subarray(i)),this.padOffset>o-i&&(this.process(s,0),i=0);for(let a=i;a<o;a++)n[a]=0;s.setBigUint64(o-8,BigInt(this.length*8),r),this.process(s,0);const c=rt(t),h=this.outputLen;if(h%4)throw new Error("_sha2: outputLen must be aligned to 32bit");const x=h/4,d=this.get();if(x>d.length)throw new Error("_sha2: outputLen bigger than state");for(let a=0;a<x;a++)c.setUint32(4*a,d[a],r)}digest(){const{buffer:t,outputLen:n}=this;this.digestInto(t);const s=t.slice(0,n);return this.destroy(),s}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:n,buffer:s,length:o,finished:r,destroyed:i,pos:c}=this;return t.destroyed=i,t.finished=r,t.length=o,t.pos=c,o%n&&t.buffer.set(s),t}clone(){return this._cloneInto()}}const O=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),I=Uint32Array.from([1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209]),we=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),Rt=Uint8Array.from(new Array(16).fill(0).map((e,t)=>t)),Pe=Rt.map(e=>(9*e+5)%16),Ft=(()=>{const n=[[Rt],[Pe]];for(let s=0;s<4;s++)for(let o of n)o.push(o[s].map(r=>we[r]));return n})(),Wt=Ft[0],Vt=Ft[1],Dt=[[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)),me=Wt.map((e,t)=>e.map(n=>Dt[t][n])),He=Vt.map((e,t)=>e.map(n=>Dt[t][n])),ke=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),Te=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function kt(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 Y=new Uint32Array(16);class Ie extends gt{constructor(){super(64,20,8,!0);f(this,"h0",1732584193);f(this,"h1",-271733879);f(this,"h2",-1732584194);f(this,"h3",271733878);f(this,"h4",-1009589776)}get(){const{h0:n,h1:s,h2:o,h3:r,h4:i}=this;return[n,s,o,r,i]}set(n,s,o,r,i){this.h0=n|0,this.h1=s|0,this.h2=o|0,this.h3=r|0,this.h4=i|0}process(n,s){for(let g=0;g<16;g++,s+=4)Y[g]=n.getUint32(s,!0);let o=this.h0|0,r=o,i=this.h1|0,c=i,h=this.h2|0,x=h,d=this.h3|0,a=d,u=this.h4|0,p=u;for(let g=0;g<5;g++){const y=4-g,m=ke[g],P=Te[g],b=Wt[g],l=Vt[g],w=me[g],k=He[g];for(let H=0;H<16;H++){const v=Q(o+kt(g,i,h,d)+Y[b[H]]+m,w[H])+u|0;o=u,u=d,d=Q(h,10)|0,h=i,i=v}for(let H=0;H<16;H++){const v=Q(r+kt(y,c,x,a)+Y[l[H]]+P,k[H])+p|0;r=p,p=a,a=Q(x,10)|0,x=c,c=v}}this.set(this.h1+h+a|0,this.h2+d+p|0,this.h3+u+r|0,this.h4+o+c|0,this.h0+i+x|0)}roundClean(){B(Y)}destroy(){this.destroyed=!0,B(this.buffer),this.set(0,0,0,0,0)}}const ve=at(()=>new Ie),tt=BigInt(2**32-1),Tt=BigInt(32);function Ee(e,t=!1){return t?{h:Number(e&tt),l:Number(e>>Tt&tt)}:{h:Number(e>>Tt&tt)|0,l:Number(e&tt)|0}}function Kt(e,t=!1){const n=e.length;let s=new Uint32Array(n),o=new Uint32Array(n);for(let r=0;r<n;r++){const{h:i,l:c}=Ee(e[r],t);[s[r],o[r]]=[i,c]}return[s,o]}const It=(e,t,n)=>e>>>n,vt=(e,t,n)=>e<<32-n|t>>>n,q=(e,t,n)=>e>>>n|t<<32-n,j=(e,t,n)=>e<<32-n|t>>>n,et=(e,t,n)=>e<<64-n|t>>>n-32,nt=(e,t,n)=>e>>>n-32|t<<64-n,Ae=(e,t,n)=>e<<n|t>>>32-n,Be=(e,t,n)=>t<<n|e>>>32-n,Ce=(e,t,n)=>t<<n-32|e>>>64-n,Se=(e,t,n)=>e<<n-32|t>>>64-n;function L(e,t,n,s){const o=(t>>>0)+(s>>>0);return{h:e+n+(o/2**32|0)|0,l:o|0}}const Le=(e,t,n)=>(e>>>0)+(t>>>0)+(n>>>0),_e=(e,t,n,s)=>t+n+s+(e/2**32|0)|0,Ue=(e,t,n,s)=>(e>>>0)+(t>>>0)+(n>>>0)+(s>>>0),Oe=(e,t,n,s,o)=>t+n+s+o+(e/2**32|0)|0,Re=(e,t,n,s,o)=>(e>>>0)+(t>>>0)+(n>>>0)+(s>>>0)+(o>>>0),Fe=(e,t,n,s,o,r)=>t+n+s+o+r+(e/2**32|0)|0,We=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),R=new Uint32Array(64);class Ve extends gt{constructor(t){super(64,t,8,!1)}get(){const{A:t,B:n,C:s,D:o,E:r,F:i,G:c,H:h}=this;return[t,n,s,o,r,i,c,h]}set(t,n,s,o,r,i,c,h){this.A=t|0,this.B=n|0,this.C=s|0,this.D=o|0,this.E=r|0,this.F=i|0,this.G=c|0,this.H=h|0}process(t,n){for(let a=0;a<16;a++,n+=4)R[a]=t.getUint32(n,!1);for(let a=16;a<64;a++){const u=R[a-15],p=R[a-2],g=C(u,7)^C(u,18)^u>>>3,y=C(p,17)^C(p,19)^p>>>10;R[a]=y+R[a-7]+g+R[a-16]|0}let{A:s,B:o,C:r,D:i,E:c,F:h,G:x,H:d}=this;for(let a=0;a<64;a++){const u=C(c,6)^C(c,11)^C(c,25),p=d+u+pe(c,h,x)+We[a]+R[a]|0,y=(C(s,2)^C(s,13)^C(s,22))+ye(s,o,r)|0;d=x,x=h,h=c,c=i+p|0,i=r,r=o,o=s,s=p+y|0}s=s+this.A|0,o=o+this.B|0,r=r+this.C|0,i=i+this.D|0,c=c+this.E|0,h=h+this.F|0,x=x+this.G|0,d=d+this.H|0,this.set(s,o,r,i,c,h,x,d)}roundClean(){B(R)}destroy(){this.set(0,0,0,0,0,0,0,0),B(this.buffer)}}class De extends Ve{constructor(){super(32);f(this,"A",O[0]|0);f(this,"B",O[1]|0);f(this,"C",O[2]|0);f(this,"D",O[3]|0);f(this,"E",O[4]|0);f(this,"F",O[5]|0);f(this,"G",O[6]|0);f(this,"H",O[7]|0)}}const $t=Kt(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(e=>BigInt(e))),Ke=$t[0],$e=$t[1],F=new Uint32Array(80),W=new Uint32Array(80);class Ne extends gt{constructor(t){super(128,t,16,!1)}get(){const{Ah:t,Al:n,Bh:s,Bl:o,Ch:r,Cl:i,Dh:c,Dl:h,Eh:x,El:d,Fh:a,Fl:u,Gh:p,Gl:g,Hh:y,Hl:m}=this;return[t,n,s,o,r,i,c,h,x,d,a,u,p,g,y,m]}set(t,n,s,o,r,i,c,h,x,d,a,u,p,g,y,m){this.Ah=t|0,this.Al=n|0,this.Bh=s|0,this.Bl=o|0,this.Ch=r|0,this.Cl=i|0,this.Dh=c|0,this.Dl=h|0,this.Eh=x|0,this.El=d|0,this.Fh=a|0,this.Fl=u|0,this.Gh=p|0,this.Gl=g|0,this.Hh=y|0,this.Hl=m|0}process(t,n){for(let l=0;l<16;l++,n+=4)F[l]=t.getUint32(n),W[l]=t.getUint32(n+=4);for(let l=16;l<80;l++){const w=F[l-15]|0,k=W[l-15]|0,H=q(w,k,1)^q(w,k,8)^It(w,k,7),v=j(w,k,1)^j(w,k,8)^vt(w,k,7),E=F[l-2]|0,A=W[l-2]|0,_=q(E,A,19)^et(E,A,61)^It(E,A,6),M=j(E,A,19)^nt(E,A,61)^vt(E,A,6),U=Ue(v,M,W[l-7],W[l-16]),ht=Oe(U,H,_,F[l-7],F[l-16]);F[l]=ht|0,W[l]=U|0}let{Ah:s,Al:o,Bh:r,Bl:i,Ch:c,Cl:h,Dh:x,Dl:d,Eh:a,El:u,Fh:p,Fl:g,Gh:y,Gl:m,Hh:P,Hl:b}=this;for(let l=0;l<80;l++){const w=q(a,u,14)^q(a,u,18)^et(a,u,41),k=j(a,u,14)^j(a,u,18)^nt(a,u,41),H=a&p^~a&y,v=u&g^~u&m,E=Re(b,k,v,$e[l],W[l]),A=Fe(E,P,w,H,Ke[l],F[l]),_=E|0,M=q(s,o,28)^et(s,o,34)^et(s,o,39),U=j(s,o,28)^nt(s,o,34)^nt(s,o,39),ht=s&r^s&c^r&c,Qt=o&i^o&h^i&h;P=y|0,b=m|0,y=p|0,m=g|0,p=a|0,g=u|0,{h:a,l:u}=L(x|0,d|0,A|0,_|0),x=c|0,d=h|0,c=r|0,h=i|0,r=s|0,i=o|0;const yt=Le(_,U,Qt);s=_e(yt,A,M,ht),o=yt|0}({h:s,l:o}=L(this.Ah|0,this.Al|0,s|0,o|0)),{h:r,l:i}=L(this.Bh|0,this.Bl|0,r|0,i|0),{h:c,l:h}=L(this.Ch|0,this.Cl|0,c|0,h|0),{h:x,l:d}=L(this.Dh|0,this.Dl|0,x|0,d|0),{h:a,l:u}=L(this.Eh|0,this.El|0,a|0,u|0),{h:p,l:g}=L(this.Fh|0,this.Fl|0,p|0,g|0),{h:y,l:m}=L(this.Gh|0,this.Gl|0,y|0,m|0),{h:P,l:b}=L(this.Hh|0,this.Hl|0,P|0,b|0),this.set(s,o,r,i,c,h,x,d,a,u,p,g,y,m,P,b)}roundClean(){B(F,W)}destroy(){B(this.buffer),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}}class Me extends Ne{constructor(){super(64);f(this,"Ah",I[0]|0);f(this,"Al",I[1]|0);f(this,"Bh",I[2]|0);f(this,"Bl",I[3]|0);f(this,"Ch",I[4]|0);f(this,"Cl",I[5]|0);f(this,"Dh",I[6]|0);f(this,"Dl",I[7]|0);f(this,"Eh",I[8]|0);f(this,"El",I[9]|0);f(this,"Fh",I[10]|0);f(this,"Fl",I[11]|0);f(this,"Gh",I[12]|0);f(this,"Gl",I[13]|0);f(this,"Hh",I[14]|0);f(this,"Hl",I[15]|0)}}const qe=at(()=>new De,Ut(1)),Nt=at(()=>new Me,Ut(3)),je=BigInt(0),Z=BigInt(1),Ge=BigInt(2),Xe=BigInt(7),ze=BigInt(256),Ze=BigInt(113),Mt=[],qt=[],jt=[];for(let e=0,t=Z,n=1,s=0;e<24;e++){[n,s]=[s,(2*n+3*s)%5],Mt.push(2*(5*s+n)),qt.push((e+1)*(e+2)/2%64);let o=je;for(let r=0;r<7;r++)t=(t<<Z^(t>>Xe)*Ze)%ze,t&Ge&&(o^=Z<<(Z<<BigInt(r))-Z);jt.push(o)}const Gt=Kt(jt,!0),Je=Gt[0],Qe=Gt[1],Et=(e,t,n)=>n>32?Ce(e,t,n):Ae(e,t,n),At=(e,t,n)=>n>32?Se(e,t,n):Be(e,t,n);function Ye(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 c=(i+8)%10,h=(i+2)%10,x=n[h],d=n[h+1],a=Et(x,d,1)^n[c],u=At(x,d,1)^n[c+1];for(let p=0;p<50;p+=10)e[i+p]^=a,e[i+p+1]^=u}let o=e[2],r=e[3];for(let i=0;i<24;i++){const c=qt[i],h=Et(o,r,c),x=At(o,r,c),d=Mt[i];o=e[d],r=e[d+1],e[d]=h,e[d+1]=x}for(let i=0;i<50;i+=10){for(let c=0;c<10;c++)n[c]=e[i+c];for(let c=0;c<10;c++)e[i+c]^=~n[(c+2)%10]&n[(c+4)%10]}e[0]^=Je[s],e[1]^=Qe[s]}B(n)}class pt{constructor(t,n,s,o=!1,r=24){f(this,"state");f(this,"pos",0);f(this,"posOut",0);f(this,"finished",!1);f(this,"state32");f(this,"destroyed",!1);f(this,"blockLen");f(this,"suffix");f(this,"outputLen");f(this,"enableXOF",!1);f(this,"rounds");if(this.blockLen=t,this.suffix=n,this.outputLen=s,this.enableXOF=o,this.rounds=r,K(s,"outputLen"),!(0<t&&t<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=ue(this.state)}clone(){return this._cloneInto()}keccak(){mt(this.state32),Ye(this.state32,this.rounds),mt(this.state32),this.posOut=0,this.pos=0}update(t){G(this),N(t);const{blockLen:n,state:s}=this,o=t.length;for(let r=0;r<o;){const i=Math.min(n-this.pos,o-r);for(let c=0;c<i;c++)s[this.pos++]^=t[r++];this.pos===n&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:n,pos:s,blockLen:o}=this;t[s]^=n,(n&128)!==0&&s===o-1&&this.keccak(),t[o-1]^=128,this.keccak()}writeInto(t){G(this,!1),N(t),this.finish();const n=this.state,{blockLen:s}=this;for(let o=0,r=t.length;o<r;){this.posOut>=s&&this.keccak();const i=Math.min(s-this.posOut,r-o);t.set(n.subarray(this.posOut,this.posOut+i),o),this.posOut+=i,o+=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 K(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(_t(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,B(this.state)}_cloneInto(t){const{blockLen:n,suffix:s,outputLen:o,rounds:r,enableXOF:i}=this;return t||(t=new pt(n,s,o,i,r)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=r,t.suffix=s,t.outputLen=o,t.enableXOF=i,t.destroyed=this.destroyed,t}}const tn=(e,t,n,s={})=>at(()=>new pt(t,e,n),s),en=tn(1,136,32);function nn(e,t,n,s){Lt(e);const o=ge({dkLen:32,asyncTick:10},s),{c:r,dkLen:i,asyncTick:c}=o;if(K(r,"c"),K(i,"dkLen"),K(c,"asyncTick"),r<1)throw new Error("iterations (c) must be >= 1");const h=Ht(t,"password"),x=Ht(n,"salt"),d=new Uint8Array(i),a=bt.create(e,h),u=a._cloneInto().update(x);return{c:r,dkLen:i,asyncTick:c,DK:d,PRF:a,PRFSalt:u}}function sn(e,t,n,s,o){return e.destroy(),t.destroy(),s&&s.destroy(),B(o),n}function on(e,t,n,s){const{c:o,dkLen:r,DK:i,PRF:c,PRFSalt:h}=nn(e,t,n,s);let x;const d=new Uint8Array(4),a=rt(d),u=new Uint8Array(c.outputLen);for(let p=1,g=0;g<r;p++,g+=c.outputLen){const y=i.subarray(g,g+c.outputLen);a.setInt32(0,p,!1),(x=h._cloneInto(x)).update(d).digestInto(u),y.set(u.subarray(0,y.length));for(let m=1;m<o;m++){c._cloneInto(x).update(u).digestInto(u);for(let P=0;P<y.length;P++)y[P]^=u[P]}}return sn(c,h,i,x,u)}function Xt(e){if(typeof e!="string")throw new TypeError("invalid mnemonic type: "+typeof e);return e.normalize("NFKD")}function rn(e){const t=Xt(e),n=t.split(" ");if(![12,15,18,21,24].includes(n.length))throw new Error("Invalid mnemonic");return{nfkd:t,words:n}}const cn=e=>Xt("mnemonic"+e);function an(e,t=""){return on(Nt,rn(e).nfkd,cn(t),{c:2048,dkLen:64})}const hn=508,Bt=16,st=32,ot=64,Ct=5,ln=4;function J(...e){const t=e.reduce((o,r)=>o+r.length,0),n=new Uint8Array(t);let s=0;for(const o of e)n.set(o,s),s+=o.length;return n}function ut(e){return new TextEncoder().encode(e)}function ft(e){const t=new Uint8Array(ln);return new DataView(t.buffer).setUint32(0,e.length,!1),J(t,e)}function dt(e,t){return bt(Nt,e,t)}function St(e){return ve(qe(e))}const xt=e=>Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("");function zt(e){const t=an(e),n=new Uint8Array(t);return t.fill(0),n}async function Zt(e,t,n,s){if(e.length!==ot)throw new Error(`WOTS seed must be ${ot} bytes, got ${e.length}`);t=T.stripHexPrefix(t),n=T.stripHexPrefix(n);const o=e.slice(st,ot),r=e.slice(0,st),i=[o,r];try{const c=J(ft(ut(t)),ft(ut(n)),ft(ut(s))),h=J(r,c);i.push(h);const x=dt(o,h);i.push(x);const d=x.slice(0,st),a=x.slice(st,ot);i.push(d,a);const u=[],p=[],g=[],y=[];let m=!1;try{for(let P=0;P<hn;P++){const b=new Uint8Array(Ct);b[0]=0,new DataView(b.buffer).setUint32(1,P,!1);const l=new Uint8Array(Ct);l[0]=1,new DataView(l.buffer).setUint32(1,P,!1);const w=J(d,b),k=J(d,l),H=dt(a,w),v=dt(a,k);try{const E=H.slice(0,Bt),A=v.slice(0,Bt);u.push(E),p.push(A),g.push(St(E)),y.push(St(A))}finally{w.fill(0),k.fill(0),H.fill(0),v.fill(0)}}return m=!0,{falsePreimages:u,truePreimages:p,falseHashes:g,trueHashes:y}}finally{if(!m){for(const P of u)P.fill(0);for(const P of p)P.fill(0)}}}finally{for(const c of i)c.fill(0)}}function un(e){return{false_list:e.falseHashes.map(xt),true_list:e.trueHashes.map(xt)}}function Jt(e){if(e.falseHashes.length===0||e.trueHashes.length===0)throw new Error("computeWotsPkHash: keypair hash arrays must not be empty");const t=e.falseHashes[0].length,n=(e.falseHashes.length+e.trueHashes.length)*t,s=new Uint8Array(n);let o=0;for(const i of e.falseHashes)s.set(i,o),o+=t;for(const i of e.trueHashes)s.set(i,o),o+=t;const r=en(s);return`0x${xt(r)}`}async function fn(e,t,n,s){const o=zt(e);try{const r=await Zt(o,t,n,s);try{return Jt(r)}finally{for(const i of r.falsePreimages)i.fill(0);for(const i of r.truePreimages)i.fill(0)}}finally{o.fill(0)}}function dn(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")}exports.CONTRACT_ERRORS=$;exports.PeginManager=he;exports.computeWotsPkHash=Jt;exports.deriveWotsKeypair=Zt;exports.deriveWotsPkHash=fn;exports.extractErrorData=ct;exports.getContractErrorMessage=ie;exports.handleContractError=D;exports.isKnownContractError=re;exports.isWotsMismatchError=dn;exports.keypairToPublicKey=un;exports.mnemonicToWotsSeed=zt;
|
|
2
|
+
//# sourceMappingURL=errors-DAKpPfl7.cjs.map
|