@gardenfi/core 0.2.0-beta.31 → 0.2.0-beta.32

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/index.cjs CHANGED
@@ -17,7 +17,7 @@
17
17
  OP_CHECKSIGADD
18
18
  OP_2
19
19
  OP_NUMEQUAL
20
- `.trim().replace(/\s+/g," "))}leaves(){return[{version:Ot,output:this.redeemLeaf()},[{version:Ot,output:this.refundLeaf()},{version:Ot,output:this.instantRefundLeaf()}]]}generateMerkleProofFor(t){const r=this.leafHash(1),a=this.leafHash(2),o=this.leafHash(0);switch(t){case 1:{const s=vi(o,a);return[de.crypto.taggedHash("TapBranch",Buffer.concat(s))]}case 0:return[a,r];case 2:return[o,r];default:throw new Error(ye.invalidLeaf)}}}function rr(e,t){const r=[];for(let a=0;a<t;a++)r.push(e);return r}class Oi{constructor(){this.cache={}}set(t,r,a,o){const s={txHash:a,timeStamp:Date.now(),btcRedeemUTXO:o};this.cache[`${r}_${t.create_order.create_id}`]=s}get(t,r){return this.cache[`${r}_${t.create_order.create_id}`]||null}remove(t,r){delete this.cache[`${r}_${t.create_order.create_id}`]}}class Li{constructor(t){if(this.eventListeners=new Map,this.getOrderThreshold=20,this.pendingOrdersCount=0,this.useRelay=!0,this.orderBook=new J.Orderbook({url:t.orderbookURl,walletClient:t.wallets.evmWallet,auth:t.auth}),this.quote=t.quote,this.secretManager=t.secretManager,this.wallets=t.wallets,this.orderbookUrl=t.orderbookURl,this.auth=t.auth,this.orderExecutorCache=new Oi,!t.wallets.evmWallet.account)throw new Error("Account not found in evmWallet");this.evmAddress=t.wallets.evmWallet.account.address}getPendingOrderCount(){return this.pendingOrdersCount}setUseRelay(t){this.useRelay=t}async swap(t){const r=await this.validateAndFillParams(t);if(r.error)return O.Err(r.error);const{sendAddress:a,receiveAddress:o,timelock:s}=r.val,c=await this.orderBook.getOrdersCount(this.evmAddress);if(c.error)return O.Err(c.error);const u=c.val+1,p=this.secretManager.generateSecret(u);if(p.error)return O.Err(p.error);const{strategyId:w,btcAddress:m}=t.additionalData,v={strategy_id:w,...m&&{bitcoin_optional_recipient:m}},T={source_chain:t.fromAsset.chain,destination_chain:t.toAsset.chain,source_asset:t.fromAsset.atomicSwapAddress,destination_asset:t.toAsset.atomicSwapAddress,initiator_source_address:a,initiator_destination_address:o,source_amount:t.sendAmount,destination_amount:t.receiveAmount,fee:"1",nonce:u.toString(),timelock:s,secret_hash:O.trim0x(p.val.secretHash),min_destination_confirmations:t.minDestinationConfirmations??0,additional_data:v},C=await this.quote.getAttestedQuote(T);if(C.error)return O.Err(C.error);const R=await this.orderBook.createOrder(C.val);if(R.error)return O.Err(R.error);const k=await this.pollOrder(R.val);return k.error?O.Err(k.error):O.Ok(k.val)}async validateAndFillParams(t){if(t.fromAsset.chain===t.toAsset.chain&&t.fromAsset.atomicSwapAddress===t.toAsset.atomicSwapAddress)return O.Err("Source and destination assets cannot be the same");if(J.isMainnet(t.fromAsset.chain)&&!J.isMainnet(t.toAsset.chain)||!J.isMainnet(t.fromAsset.chain)&&J.isMainnet(t.toAsset.chain))return O.Err("Both assets should be on the same network (either mainnet or testnet)");if(J.isBitcoin(t.fromAsset.chain)||J.isBitcoin(t.toAsset.chain)){if(!this.wallets.btcWallet)return O.Err("btcWallet is required for bitcoin chain. Please provide btcWallet in the constructor");if(!t.additionalData.btcAddress)return O.Err("btcAddress in additionalData is required for bitcoin chain")}const r=await this.getAddresses(t.fromAsset.chain);if(r.error)return O.Err(r.error);const a=await this.getAddresses(t.toAsset.chain);if(a.error)return O.Err(a.error);const o=this.validateAmount(t.sendAmount);if(o.error)return O.Err(o.error);const s=this.validateAmount(t.receiveAmount);if(s.error)return O.Err(s.error);if(o<s)return O.Err("Send amount should be greater than receive amount");const c=this.getTimelock(t.fromAsset.chain);return c?O.Ok({sendAddress:r.val,receiveAddress:a.val,timelock:t.timelock??c}):O.Err("Unsupported chain for timelock")}async getAddresses(t){var a;switch(J.getBlockchainType(t)){case J.BlockchainType.EVM:return this.wallets.evmWallet.account?O.Ok(this.wallets.evmWallet.account.address):O.Err("EVM Wallet not found");case J.BlockchainType.Bitcoin:{const o=await((a=this.wallets.btcWallet)==null?void 0:a.getPublicKey());return!o||!xi(o)?O.Err("Invalid btc public key"):O.Ok(yt(o))}default:return O.Err("Unsupported chain")}}validateAmount(t){const r=BigInt(t);return t==null||r<=0n||t.includes(".")?O.Err("Invalid amount ",t):O.Ok(r)}getTimelock(t){switch(J.getBlockchainType(t)){case J.BlockchainType.EVM:return gr.evm;case J.BlockchainType.Bitcoin:return gr.btc;default:return}}async pollOrder(t){let r=await this.orderBook.getOrder(t,!0),a=0;for(;a<this.getOrderThreshold;){if(await ge.sleep(1e3),a++,r.error){if(!r.error.includes("result is undefined"))return O.Err(r.error)}else if(r.val&&r.val.create_order.create_id.toLowerCase()===t.toLowerCase())return O.Ok(r.val);r=await this.orderBook.getOrder(t,!0)}return O.Err(`Order not found, createOrder id: ${t}`)}emit(t,...r){(this.eventListeners.get(t)??[]).forEach(o=>{o(...r)})}async execute(t=5e3){return await this.orderBook.subscribeToOrders(!0,t,async r=>{this.pendingOrdersCount=r.data.length,this.emit("onPendingOrdersChanged",r.data);for(let a=0;a<r.data.length;a++){const o=r.data[a],s=o.source_swap.chain,c=o.destination_swap.chain,u=this.getWallet(s),p=this.getWallet(c);if(u.error||p.error||!u.val||!p.val){this.emit("error",o,"Source or Destination Wallet not found while executing order");return}const w=await this.fetchCurrentBlockNumbers(o,{source:u.val,destination:p.val});if(w.error){this.emit("error",o,"Error while fetching CurrentBlockNumbers: "+w.error);return}switch(mn(o,w.val.source,w.val.destination)){case pe.Redeem:{const v=this.secretManager.generateSecret(Number(o.create_order.nonce));if(v.error){this.emit("error",o,v.error);return}switch(J.getBlockchainType(o.destination_swap.chain)){case J.BlockchainType.EVM:{await this.evmRedeem(o,v.val.secret);break}case J.BlockchainType.Bitcoin:{await this.btcRedeem(p.val,o,v.val.secret);break}default:this.emit("error",o,"Unsupported chain: "+o.destination_swap.chain)}break}case pe.Refund:{switch(J.getBlockchainType(o.source_swap.chain)){case J.BlockchainType.EVM:{this.emit("error",o,"EVM refund is automatically done by relay service");break}case J.BlockchainType.Bitcoin:{await this.btcRefund(u.val,o);break}default:this.emit("error",o,"Unsupported chain: "+o.source_swap.chain)}break}}}},{per_page:500},!0)}async evmRedeem(t,r){this.emit("log",t.create_order.create_id,"executing evm redeem");const a=this.orderExecutorCache.get(t,pe.Redeem);if(console.log("cache inside evm redeem :",a),a){this.emit("log",t.create_order.create_id,"already redeemed");return}const s=await new vn(t,this.orderbookUrl,this.auth).redeem(t.create_order.create_id,r);if(s.error){this.emit("error",t,s.error);return}this.emit("success",t,pe.Redeem,s.val),this.orderExecutorCache.set(t,pe.Redeem,s.val)}async btcRedeem(t,r,a){var c;const o=this.orderExecutorCache.get(r,pe.Redeem);console.log("cache inside btc redeem :",o);let s=!1;if(o)if(o.btcRedeemUTXO&&o.btcRedeemUTXO!==r.destination_swap.initiate_tx_hash)s=!0,this.emit("log",r.create_order.create_id,"rbf btc redeem");else{this.emit("log",r.create_order.create_id,"btcRedeem: already redeemed");return}this.emit("log",r.create_order.create_id,"executing btc redeem");try{const p=await(await Nt.from(t,Number(r.destination_swap.amount),r.create_order.secret_hash,yt(r.destination_swap.initiator),yt(r.destination_swap.redeemer),r.destination_swap.timelock,s?[r.destination_swap.initiate_tx_hash]:[])).redeem(O.trim0x(a),(c=r.create_order.additional_data)==null?void 0:c.bitcoin_optional_recipient);this.emit("success",r,pe.Redeem,p),this.orderExecutorCache.set(r,pe.Redeem,p,r.destination_swap.initiate_tx_hash)}catch(u){this.emit("error",r,"Failed btc redeem: "+u)}}async btcRefund(t,r){var a;if(!this.orderExecutorCache.get(r,pe.Refund)){this.emit("log",r.create_order.create_id,"executing btc refund");try{const s=await(await Nt.from(t,Number(r.source_swap.amount),r.create_order.secret_hash,yt(r.source_swap.initiator),yt(r.source_swap.redeemer),r.source_swap.timelock)).refund((a=r.create_order.additional_data)==null?void 0:a.bitcoin_optional_recipient);this.emit("success",r,pe.Refund,s),this.orderExecutorCache.set(r,pe.Refund,s)}catch(o){this.emit("error",r,"Failed btc refund: "+o)}}}getWallet(t){switch(J.getBlockchainType(t)){case J.BlockchainType.EVM:return O.Ok(this.wallets.evmWallet);case J.BlockchainType.Bitcoin:return O.Ok(this.wallets.btcWallet);default:return O.Err("Unsupported chain for wallet")}}on(t,r){const a=this.eventListeners.get(t)??[];a.push(r),this.eventListeners.set(t,a)}off(t,r){const a=this.eventListeners.get(t)??[],o=a.indexOf(r);o!==-1&&a.splice(o,1)}async fetchCurrentBlockNumbers(t,r){if(!r||!r.source||!r.destination)return O.Err("Provide wallets to fetch the current block number");const a=J.isBitcoin(t.source_swap.chain)?await ge.fetchBitcoinBlockNumber(await r.source.getProvider()):await ge.fetchEVMBlockNumber(r.source);if(a.error)return O.Err(a.error);const o=J.isBitcoin(t.destination_swap.chain)?await ge.fetchBitcoinBlockNumber(await r.destination.getProvider()):await ge.fetchEVMBlockNumber(r.destination);return o.error?O.Err(o.error):O.Ok({source:a.val,destination:o.val})}}var Ge=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function Mi(e){if(e.__esModule)return e;var t=e.default;if(typeof t=="function"){var r=function a(){return this instanceof a?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};r.prototype=t.prototype}else r={};return Object.defineProperty(r,"__esModule",{value:!0}),Object.keys(e).forEach(function(a){var o=Object.getOwnPropertyDescriptor(e,a);Object.defineProperty(r,a,o.get?o:{enumerable:!0,get:function(){return e[a]}})}),r}var xn={},ut={},ct={};Object.defineProperty(ct,"__esModule",{value:!0});ct.testnet=ct.bitcoin=void 0;ct.bitcoin={messagePrefix:`Bitcoin Signed Message:
20
+ `.trim().replace(/\s+/g," "))}leaves(){return[{version:Ot,output:this.redeemLeaf()},[{version:Ot,output:this.refundLeaf()},{version:Ot,output:this.instantRefundLeaf()}]]}generateMerkleProofFor(t){const r=this.leafHash(1),a=this.leafHash(2),o=this.leafHash(0);switch(t){case 1:{const s=vi(o,a);return[de.crypto.taggedHash("TapBranch",Buffer.concat(s))]}case 0:return[a,r];case 2:return[o,r];default:throw new Error(ye.invalidLeaf)}}}function rr(e,t){const r=[];for(let a=0;a<t;a++)r.push(e);return r}class Oi{constructor(){this.cache={}}set(t,r,a,o){const s={txHash:a,timeStamp:Date.now(),btcRedeemUTXO:o};this.cache[`${r}_${t.create_order.create_id}`]=s}get(t,r){return this.cache[`${r}_${t.create_order.create_id}`]||null}remove(t,r){delete this.cache[`${r}_${t.create_order.create_id}`]}}class Li{constructor(t){if(this.eventListeners=new Map,this.getOrderThreshold=20,this.pendingOrdersCount=0,this.useRelay=!0,this.orderBook=new J.Orderbook({url:t.orderbookURl,walletClient:t.wallets.evmWallet,auth:t.auth}),this.quote=t.quote,this.secretManager=t.secretManager,this.wallets=t.wallets,this.orderbookUrl=t.orderbookURl,this.auth=t.auth,this.orderExecutorCache=new Oi,!t.wallets.evmWallet.account)throw new Error("Account not found in evmWallet");this.evmAddress=t.wallets.evmWallet.account.address}getPendingOrderCount(){return this.pendingOrdersCount}setUseRelay(t){this.useRelay=t}async swap(t){const r=await this.validateAndFillParams(t);if(r.error)return O.Err(r.error);const{sendAddress:a,receiveAddress:o,timelock:s}=r.val,c=await this.orderBook.getOrdersCount(this.evmAddress);if(c.error)return O.Err(c.error);const u=c.val+1,p=this.secretManager.generateSecret(u);if(p.error)return O.Err(p.error);const{strategyId:w,btcAddress:m}=t.additionalData,v={strategy_id:w,...m&&{bitcoin_optional_recipient:m}},T={source_chain:t.fromAsset.chain,destination_chain:t.toAsset.chain,source_asset:t.fromAsset.atomicSwapAddress,destination_asset:t.toAsset.atomicSwapAddress,initiator_source_address:a,initiator_destination_address:o,source_amount:t.sendAmount,destination_amount:t.receiveAmount,fee:"1",nonce:u.toString(),timelock:s,secret_hash:O.trim0x(p.val.secretHash),min_destination_confirmations:t.minDestinationConfirmations??0,additional_data:v},C=await this.quote.getAttestedQuote(T);if(C.error)return O.Err(C.error);const R=await this.orderBook.createOrder(C.val);if(R.error)return O.Err(R.error);const k=await this.pollOrder(R.val);return k.error?O.Err(k.error):O.Ok(k.val)}async validateAndFillParams(t){if(t.fromAsset.chain===t.toAsset.chain&&t.fromAsset.atomicSwapAddress===t.toAsset.atomicSwapAddress)return O.Err("Source and destination assets cannot be the same");if(J.isMainnet(t.fromAsset.chain)&&!J.isMainnet(t.toAsset.chain)||!J.isMainnet(t.fromAsset.chain)&&J.isMainnet(t.toAsset.chain))return O.Err("Both assets should be on the same network (either mainnet or testnet)");if(J.isBitcoin(t.fromAsset.chain)||J.isBitcoin(t.toAsset.chain)){if(!this.wallets.btcWallet)return O.Err("btcWallet is required for bitcoin chain. Please provide btcWallet in the constructor");if(!t.additionalData.btcAddress)return O.Err("btcAddress in additionalData is required for bitcoin chain")}const r=await this.getAddresses(t.fromAsset.chain);if(r.error)return O.Err(r.error);const a=await this.getAddresses(t.toAsset.chain);if(a.error)return O.Err(a.error);const o=this.validateAmount(t.sendAmount);if(o.error)return O.Err(o.error);const s=this.validateAmount(t.receiveAmount);if(s.error)return O.Err(s.error);if(o<s)return O.Err("Send amount should be greater than receive amount");const c=this.getTimelock(t.fromAsset.chain);return c?O.Ok({sendAddress:r.val,receiveAddress:a.val,timelock:t.timelock??c}):O.Err("Unsupported chain for timelock")}async getAddresses(t){var a;switch(J.getBlockchainType(t)){case J.BlockchainType.EVM:return this.wallets.evmWallet.account?O.Ok(this.wallets.evmWallet.account.address):O.Err("EVM Wallet not found");case J.BlockchainType.Bitcoin:{const o=await((a=this.wallets.btcWallet)==null?void 0:a.getPublicKey());return!o||!xi(o)?O.Err("Invalid btc public key"):O.Ok(yt(o))}default:return O.Err("Unsupported chain")}}validateAmount(t){const r=BigInt(t);return t==null||r<=0n||t.includes(".")?O.Err("Invalid amount ",t):O.Ok(r)}getTimelock(t){switch(J.getBlockchainType(t)){case J.BlockchainType.EVM:return gr.evm;case J.BlockchainType.Bitcoin:return gr.btc;default:return}}async pollOrder(t){let r=await this.orderBook.getOrder(t,!0),a=0;for(;a<this.getOrderThreshold;){if(await ge.sleep(1e3),a++,r.error){if(!r.error.includes("result is undefined"))return O.Err(r.error)}else if(r.val&&r.val.create_order.create_id.toLowerCase()===t.toLowerCase())return O.Ok(r.val);r=await this.orderBook.getOrder(t,!0)}return O.Err(`Order not found, createOrder id: ${t}`)}emit(t,...r){(this.eventListeners.get(t)??[]).forEach(o=>{o(...r)})}async execute(t=5e3){return await this.orderBook.subscribeToOrders(!0,t,async r=>{this.pendingOrdersCount=r.data.length,this.emit("onPendingOrdersChanged",r.data);for(let a=0;a<r.data.length;a++){const o=r.data[a],s=o.source_swap.chain,c=o.destination_swap.chain;this.emit("log",o.create_order.create_id,"executing order");const u=this.getWallet(s),p=this.getWallet(c);if(u.error||p.error||!u.val||!p.val){this.emit("error",o,"Source or Destination Wallet not found while executing order");return}const w=await this.fetchCurrentBlockNumbers(o,{source:u.val,destination:p.val});if(w.error){this.emit("error",o,"Error while fetching CurrentBlockNumbers: "+w.error);return}switch(mn(o,w.val.source,w.val.destination)){case pe.Redeem:{const v=this.secretManager.generateSecret(Number(o.create_order.nonce));if(v.error){this.emit("error",o,v.error);return}switch(J.getBlockchainType(o.destination_swap.chain)){case J.BlockchainType.EVM:{await this.evmRedeem(o,v.val.secret);break}case J.BlockchainType.Bitcoin:{await this.btcRedeem(p.val,o,v.val.secret);break}default:this.emit("error",o,"Unsupported chain: "+o.destination_swap.chain)}break}case pe.Refund:{switch(J.getBlockchainType(o.source_swap.chain)){case J.BlockchainType.EVM:{this.emit("error",o,"EVM refund is automatically done by relay service");break}case J.BlockchainType.Bitcoin:{await this.btcRefund(u.val,o);break}default:this.emit("error",o,"Unsupported chain: "+o.source_swap.chain)}break}}}},{per_page:500},!0)}async evmRedeem(t,r){this.emit("log",t.create_order.create_id,"executing evm redeem");const a=this.orderExecutorCache.get(t,pe.Redeem);if(console.log("cache inside evm redeem :",a),a){this.emit("log",t.create_order.create_id,"already redeemed");return}const s=await new vn(t,this.orderbookUrl,this.auth).redeem(t.create_order.create_id,r);if(s.error){this.emit("error",t,s.error);return}this.emit("success",t,pe.Redeem,s.val),this.orderExecutorCache.set(t,pe.Redeem,s.val)}async btcRedeem(t,r,a){var c;const o=this.orderExecutorCache.get(r,pe.Redeem);console.log("cache inside btc redeem :",o);let s=!1;if(o)if(o.btcRedeemUTXO&&o.btcRedeemUTXO!==r.destination_swap.initiate_tx_hash)s=!0,this.emit("log",r.create_order.create_id,"rbf btc redeem");else{this.emit("log",r.create_order.create_id,"btcRedeem: already redeemed");return}this.emit("log",r.create_order.create_id,"executing btc redeem");try{const p=await(await Nt.from(t,Number(r.destination_swap.amount),r.create_order.secret_hash,yt(r.destination_swap.initiator),yt(r.destination_swap.redeemer),r.destination_swap.timelock,s?[r.destination_swap.initiate_tx_hash]:[])).redeem(O.trim0x(a),(c=r.create_order.additional_data)==null?void 0:c.bitcoin_optional_recipient);this.emit("success",r,pe.Redeem,p),this.orderExecutorCache.set(r,pe.Redeem,p,r.destination_swap.initiate_tx_hash)}catch(u){this.emit("error",r,"Failed btc redeem: "+u)}}async btcRefund(t,r){var a;if(!this.orderExecutorCache.get(r,pe.Refund)){this.emit("log",r.create_order.create_id,"executing btc refund");try{const s=await(await Nt.from(t,Number(r.source_swap.amount),r.create_order.secret_hash,yt(r.source_swap.initiator),yt(r.source_swap.redeemer),r.source_swap.timelock)).refund((a=r.create_order.additional_data)==null?void 0:a.bitcoin_optional_recipient);this.emit("success",r,pe.Refund,s),this.orderExecutorCache.set(r,pe.Refund,s)}catch(o){this.emit("error",r,"Failed btc refund: "+o)}}}getWallet(t){switch(J.getBlockchainType(t)){case J.BlockchainType.EVM:return O.Ok(this.wallets.evmWallet);case J.BlockchainType.Bitcoin:return O.Ok(this.wallets.btcWallet);default:return O.Err("Unsupported chain for wallet")}}on(t,r){const a=this.eventListeners.get(t)??[];a.push(r),this.eventListeners.set(t,a)}off(t,r){const a=this.eventListeners.get(t)??[],o=a.indexOf(r);o!==-1&&a.splice(o,1)}async fetchCurrentBlockNumbers(t,r){if(!r||!r.source||!r.destination)return O.Err("Provide wallets to fetch the current block number");const a=J.isBitcoin(t.source_swap.chain)?await ge.fetchBitcoinBlockNumber(await r.source.getProvider()):await ge.fetchEVMBlockNumber(r.source);if(a.error)return O.Err(a.error);const o=J.isBitcoin(t.destination_swap.chain)?await ge.fetchBitcoinBlockNumber(await r.destination.getProvider()):await ge.fetchEVMBlockNumber(r.destination);return o.error?O.Err(o.error):O.Ok({source:a.val,destination:o.val})}}var Ge=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function Mi(e){if(e.__esModule)return e;var t=e.default;if(typeof t=="function"){var r=function a(){return this instanceof a?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};r.prototype=t.prototype}else r={};return Object.defineProperty(r,"__esModule",{value:!0}),Object.keys(e).forEach(function(a){var o=Object.getOwnPropertyDescriptor(e,a);Object.defineProperty(r,a,o.get?o:{enumerable:!0,get:function(){return e[a]}})}),r}var xn={},ut={},ct={};Object.defineProperty(ct,"__esModule",{value:!0});ct.testnet=ct.bitcoin=void 0;ct.bitcoin={messagePrefix:`Bitcoin Signed Message:
21
21
  `,bech32:"bc",bip32:{public:76067358,private:76066276},pubKeyHash:0,scriptHash:5,wif:128};ct.testnet={messagePrefix:`Bitcoin Signed Message:
22
22
  `,bech32:"tb",bip32:{public:70617039,private:70615956},pubKeyHash:111,scriptHash:196,wif:239};var En={},Bt={Array:function(e){return e!=null&&e.constructor===Array},Boolean:function(e){return typeof e=="boolean"},Function:function(e){return typeof e=="function"},Nil:function(e){return e==null},Number:function(e){return typeof e=="number"},Object:function(e){return typeof e=="object"},String:function(e){return typeof e=="string"},"":function(){return!0}};Bt.Null=Bt.Nil;for(var Hr in Bt)Bt[Hr].toJSON=(function(e){return e}).bind(null,Hr);var Ir=Bt,Xe=Ir;function Bn(e){return e.name||e.toString().match(/function (.*?)\s*\(/)[1]}function kr(e){return Xe.Nil(e)?"":Bn(e.constructor)}function Pi(e){return Xe.Function(e)?"":Xe.String(e)?JSON.stringify(e):e&&Xe.Object(e)?"":e}function Cr(e,t){Error.captureStackTrace&&Error.captureStackTrace(e,t)}function Ut(e){return Xe.Function(e)?e.toJSON?e.toJSON():Bn(e):Xe.Array(e)?"Array":e&&Xe.Object(e)?"Object":e!==void 0?e:""}function Rn(e,t,r){var a=Pi(t);return"Expected "+Ut(e)+", got"+(r!==""?" "+r:"")+(a!==""?" "+a:"")}function Ue(e,t,r){r=r||kr(t),this.message=Rn(e,t,r),Cr(this,Ue),this.__type=e,this.__value=t,this.__valueTypeName=r}Ue.prototype=Object.create(Error.prototype);Ue.prototype.constructor=Ue;function Fi(e,t,r,a,o){var s='" of type ';return t==="key"&&(s='" with key type '),Rn('property "'+Ut(r)+s+Ut(e),a,o)}function ot(e,t,r,a,o){e?(o=o||kr(a),this.message=Fi(e,r,t,a,o)):this.message='Unexpected property "'+t+'"',Cr(this,Ue),this.__label=r,this.__property=t,this.__type=e,this.__value=a,this.__valueTypeName=o}ot.prototype=Object.create(Error.prototype);ot.prototype.constructor=Ue;function Ni(e,t){return new Ue(e,{},t)}function Ui(e,t,r){return e instanceof ot?(t=t+"."+e.__property,e=new ot(e.__type,t,e.__label,e.__value,e.__valueTypeName)):e instanceof Ue&&(e=new ot(e.__type,t,r,e.__value,e.__valueTypeName)),Cr(e),e}var Sn={TfTypeError:Ue,TfPropertyTypeError:ot,tfCustomError:Ni,tfSubError:Ui,tfJSON:Ut,getValueTypeName:kr},nr,jr;function Di(){if(jr)return nr;jr=1;var e=Ir,t=Sn;function r(E){return Buffer.isBuffer(E)}function a(E){return typeof E=="string"&&/^([0-9a-f]{2})+$/i.test(E)}function o(E,j){var U=E.toJSON();function $(ne){if(!E(ne))return!1;if(ne.length===j)return!0;throw t.tfCustomError(U+"(Length: "+j+")",U+"(Length: "+ne.length+")")}return $.toJSON=function(){return U},$}var s=o.bind(null,e.Array),c=o.bind(null,r),u=o.bind(null,a),p=o.bind(null,e.String);function w(E,j,U){U=U||e.Number;function $(ne,Te){return U(ne,Te)&&ne>E&&ne<j}return $.toJSON=function(){return`${U.toJSON()} between [${E}, ${j}]`},$}var m=Math.pow(2,53)-1;function v(E){return typeof E=="number"&&isFinite(E)}function T(E){return E<<24>>24===E}function C(E){return E<<16>>16===E}function R(E){return(E|0)===E}function k(E){return typeof E=="number"&&E>=-m&&E<=m&&Math.floor(E)===E}function A(E){return(E&255)===E}function I(E){return(E&65535)===E}function M(E){return E>>>0===E}function P(E){return typeof E=="number"&&E>=0&&E<=m&&Math.floor(E)===E}var L={ArrayN:s,Buffer:r,BufferN:c,Finite:v,Hex:a,HexN:u,Int8:T,Int16:C,Int32:R,Int53:k,Range:w,StringN:p,UInt8:A,UInt16:I,UInt32:M,UInt53:P};for(var N in L)L[N].toJSON=(function(E){return E}).bind(null,N);return nr=L,nr}var St=Sn,fe=Ir,Pe=St.tfJSON,An=St.TfTypeError,Tn=St.TfPropertyTypeError,_t=St.tfSubError,$i=St.getValueTypeName,Fe={arrayOf:function(t,r){t=Oe(t),r=r||{};function a(o,s){return!fe.Array(o)||fe.Nil(o)||r.minLength!==void 0&&o.length<r.minLength||r.maxLength!==void 0&&o.length>r.maxLength||r.length!==void 0&&o.length!==r.length?!1:o.every(function(c,u){try{return we(t,c,s)}catch(p){throw _t(p,u)}})}return a.toJSON=function(){var o="["+Pe(t)+"]";return r.length!==void 0?o+="{"+r.length+"}":(r.minLength!==void 0||r.maxLength!==void 0)&&(o+="{"+(r.minLength===void 0?0:r.minLength)+","+(r.maxLength===void 0?1/0:r.maxLength)+"}"),o},a},maybe:function e(t){t=Oe(t);function r(a,o){return fe.Nil(a)||t(a,o,e)}return r.toJSON=function(){return"?"+Pe(t)},r},map:function(t,r){t=Oe(t),r&&(r=Oe(r));function a(o,s){if(!fe.Object(o)||fe.Nil(o))return!1;for(var c in o){try{r&&we(r,c,s)}catch(p){throw _t(p,c,"key")}try{var u=o[c];we(t,u,s)}catch(p){throw _t(p,c)}}return!0}return r?a.toJSON=function(){return"{"+Pe(r)+": "+Pe(t)+"}"}:a.toJSON=function(){return"{"+Pe(t)+"}"},a},object:function(t){var r={};for(var a in t)r[a]=Oe(t[a]);function o(s,c){if(!fe.Object(s)||fe.Nil(s))return!1;var u;try{for(u in r){var p=r[u],w=s[u];we(p,w,c)}}catch(m){throw _t(m,u)}if(c){for(u in s)if(!r[u])throw new Tn(void 0,u)}return!0}return o.toJSON=function(){return Pe(r)},o},anyOf:function(){var t=[].slice.call(arguments).map(Oe);function r(a,o){return t.some(function(s){try{return we(s,a,o)}catch{return!1}})}return r.toJSON=function(){return t.map(Pe).join("|")},r},allOf:function(){var t=[].slice.call(arguments).map(Oe);function r(a,o){return t.every(function(s){try{return we(s,a,o)}catch{return!1}})}return r.toJSON=function(){return t.map(Pe).join(" & ")},r},quacksLike:function(t){function r(a){return t===$i(a)}return r.toJSON=function(){return t},r},tuple:function(){var t=[].slice.call(arguments).map(Oe);function r(a,o){return fe.Nil(a)||fe.Nil(a.length)||o&&a.length!==t.length?!1:t.every(function(s,c){try{return we(s,a[c],o)}catch(u){throw _t(u,c)}})}return r.toJSON=function(){return"("+t.map(Pe).join(", ")+")"},r},value:function(t){function r(a){return a===t}return r.toJSON=function(){return t},r}};Fe.oneOf=Fe.anyOf;function Oe(e){if(fe.String(e))return e[0]==="?"?Fe.maybe(e.slice(1)):fe[e]||Fe.quacksLike(e);if(e&&fe.Object(e)){if(fe.Array(e)){if(e.length!==1)throw new TypeError("Expected compile() parameter of type Array of length 1");return Fe.arrayOf(e[0])}return Fe.object(e)}else if(fe.Function(e))return e;return Fe.value(e)}function we(e,t,r,a){if(fe.Function(e)){if(e(t,r))return!0;throw new An(a||e,t)}return we(Oe(e),t,r)}for(var Ke in fe)we[Ke]=fe[Ke];for(Ke in Fe)we[Ke]=Fe[Ke];var qr=Di();for(Ke in qr)we[Ke]=qr[Ke];we.compile=Oe;we.TfTypeError=An;we.TfPropertyTypeError=Tn;var Hi=we;(function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.maybe=e.Boolean=e.Array=e.Buffer256bit=e.Network=e.typeforce=void 0,e.typeforce=Hi,e.Network=e.typeforce.compile({messagePrefix:e.typeforce.oneOf(e.typeforce.Buffer,e.typeforce.String),bip32:{public:e.typeforce.UInt32,private:e.typeforce.UInt32},pubKeyHash:e.typeforce.UInt8,scriptHash:e.typeforce.UInt8,wif:e.typeforce.UInt8}),e.Buffer256bit=e.typeforce.BufferN(32),e.Array=e.typeforce.Array,e.Boolean=e.typeforce.Boolean,e.maybe=e.typeforce.maybe})(En);var mr={exports:{}},vr={exports:{}},ht={},Wt={};Wt.byteLength=Wi;Wt.toByteArray=Gi;Wt.fromByteArray=Ji;var Le=[],Se=[],ji=typeof Uint8Array<"u"?Uint8Array:Array,ir="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(var at=0,qi=ir.length;at<qi;++at)Le[at]=ir[at],Se[ir.charCodeAt(at)]=at;Se[45]=62;Se[95]=63;function In(e){var t=e.length;if(t%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var r=e.indexOf("=");r===-1&&(r=t);var a=r===t?0:4-r%4;return[r,a]}function Wi(e){var t=In(e),r=t[0],a=t[1];return(r+a)*3/4-a}function Ki(e,t,r){return(t+r)*3/4-r}function Gi(e){var t,r=In(e),a=r[0],o=r[1],s=new ji(Ki(e,a,o)),c=0,u=o>0?a-4:a,p;for(p=0;p<u;p+=4)t=Se[e.charCodeAt(p)]<<18|Se[e.charCodeAt(p+1)]<<12|Se[e.charCodeAt(p+2)]<<6|Se[e.charCodeAt(p+3)],s[c++]=t>>16&255,s[c++]=t>>8&255,s[c++]=t&255;return o===2&&(t=Se[e.charCodeAt(p)]<<2|Se[e.charCodeAt(p+1)]>>4,s[c++]=t&255),o===1&&(t=Se[e.charCodeAt(p)]<<10|Se[e.charCodeAt(p+1)]<<4|Se[e.charCodeAt(p+2)]>>2,s[c++]=t>>8&255,s[c++]=t&255),s}function Vi(e){return Le[e>>18&63]+Le[e>>12&63]+Le[e>>6&63]+Le[e&63]}function zi(e,t,r){for(var a,o=[],s=t;s<r;s+=3)a=(e[s]<<16&16711680)+(e[s+1]<<8&65280)+(e[s+2]&255),o.push(Vi(a));return o.join("")}function Ji(e){for(var t,r=e.length,a=r%3,o=[],s=16383,c=0,u=r-a;c<u;c+=s)o.push(zi(e,c,c+s>u?u:c+s));return a===1?(t=e[r-1],o.push(Le[t>>2]+Le[t<<4&63]+"==")):a===2&&(t=(e[r-2]<<8)+e[r-1],o.push(Le[t>>10]+Le[t>>4&63]+Le[t<<2&63]+"=")),o.join("")}var Or={};/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */Or.read=function(e,t,r,a,o){var s,c,u=o*8-a-1,p=(1<<u)-1,w=p>>1,m=-7,v=r?o-1:0,T=r?-1:1,C=e[t+v];for(v+=T,s=C&(1<<-m)-1,C>>=-m,m+=u;m>0;s=s*256+e[t+v],v+=T,m-=8);for(c=s&(1<<-m)-1,s>>=-m,m+=a;m>0;c=c*256+e[t+v],v+=T,m-=8);if(s===0)s=1-w;else{if(s===p)return c?NaN:(C?-1:1)*(1/0);c=c+Math.pow(2,a),s=s-w}return(C?-1:1)*c*Math.pow(2,s-a)};Or.write=function(e,t,r,a,o,s){var c,u,p,w=s*8-o-1,m=(1<<w)-1,v=m>>1,T=o===23?Math.pow(2,-24)-Math.pow(2,-77):0,C=a?0:s-1,R=a?1:-1,k=t<0||t===0&&1/t<0?1:0;for(t=Math.abs(t),isNaN(t)||t===1/0?(u=isNaN(t)?1:0,c=m):(c=Math.floor(Math.log(t)/Math.LN2),t*(p=Math.pow(2,-c))<1&&(c--,p*=2),c+v>=1?t+=T/p:t+=T*Math.pow(2,1-v),t*p>=2&&(c++,p/=2),c+v>=m?(u=0,c=m):c+v>=1?(u=(t*p-1)*Math.pow(2,o),c=c+v):(u=t*Math.pow(2,v-1)*Math.pow(2,o),c=0));o>=8;e[r+C]=u&255,C+=R,u/=256,o-=8);for(c=c<<o|u,w+=o;w>0;e[r+C]=c&255,C+=R,c/=256,w-=8);e[r+C-R]|=k*128};/*!
23
23
  * The buffer module from node.js, for the browser.