1(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[1787],{2882:t=>{var e=function(t){if(!Array.isArray(t))throw Error("Input must be an Array");if(0===t.length)throw Error("Path must contain at least one level");for(var e=0;e<t.length;e++)if("number"!=typeof t[e])throw Error("Path element is not a number");this.path=t};e.validatePathArray=function(t){try{return e.fromPathArray(t),!0}catch(t){return!1}},e.validateString=function(t,r){try{return e.fromString(t,r),!0}catch(t){return!1}},e.fromPathArray=function(t){return new e(t)},e.fromString=function(t,r){if(/^m\//i.test(t))t=t.slice(2);else if(r)throw Error("Root element is required");for(var n=t.split("/"),i=Array(n.length),s=0;s<n.length;s++){var a=/(\d+)([hH\']?)/.exec(n[s]);if(null===a)throw Error("Invalid input");if(i[s]=parseInt(a[1],10),i[s]>=0x80000000)throw Error("Invalid child index");if("h"===a[2]||"H"===a[2]||"'"===a[2])i[s]+=0x80000000;else if(0!=a[2].length)throw Error("Invalid modifier")}return new e(i)},e.prototype.toPathArray=function(){return this.path},e.prototype.toString=function(t,e){for(var r=Array(this.path.length),n=0;n<this.path.length;n++){var i=this.path[n];0x80000000&i?r[n]=(0x7fffffff&i)+(e?"h":"'"):r[n]=i}return(t?"":"m/")+r.join("/")},e.prototype.inspect=function(){return"BIPPath <"+this.toString()+">"},t.exports=e},17208:t=>{"use strict";t.exports=function(t,e,r,n,i,s,a,o){if(!t){var l;if(void 0===e)l=Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var u=[r,n,i,s,a,o],c=0;(l=Error(e.replace(/%s/g,function(){return u[c++]}))).name="Invariant Violation"}throw l.framesToPop=1,l}}},21787:(t,e,r)=>{"use strict";r.r(e),r.d(e,{default:()=>eD});var n,i,s,a,o,l,u=r(41717),c=r.n(u),p=r(93328),h=r(56539),f=r(2882),g=r.n(f),d=r(74609),I=r.n(d),w=r(52235).Buffer;function m(t){let e=w.alloc(1+4*t.length);return e[0]=t.length,t.forEach((t,r)=>{e.writeUInt32BE(t,1+4*r)}),e}function E(t){return m(t?y(t):[])}function S(t){return g().fromPathArray(t).toString()}function y(t){return g().fromString(t).toPathArray()}function b(t){return I().decode(t).subarray(-33)}function T(t){let e=I().decode(t);return{chaincode:e.subarray(13,45),pubkey:e.subarray(-33),version:e.readUInt32BE(0)}}var v=r(90966),x=r.n(v),P=r(52235).Buffer;function _(t){if(t>Number.MAX_SAFE_INTEGER)throw Error("Can't convert numbers > MAX_SAFE_INT");let e=P.alloc(8,0);for(let r=0;r<e.length;r++){let n=255&t;e[r]=n,t=(t-n)/256}return e}function O(t){let e=0;if(8!=t.length)throw Error("Expected Buffer of length 8");if(0!=t[7]||t[6]>31)throw Error("Can't encode numbers > MAX_SAFE_INT");for(let r=t.length-1;r>=0;r--)e=256*e+t[r];return e}class B{bufs=[];write(t,e){let r=P.alloc(t);e(r),this.bufs.push(r)}writeUInt8(t){this.write(1,e=>e.writeUInt8(t,0))}writeInt32(t){this.write(4,e=>e.writeInt32LE(t,0))}writeUInt32(t){this.write(4,e=>e.writeUInt32LE(t,0))}writeUInt64(t){let e=_(t);this.writeSlice(e)}writeVarInt(t){this.bufs.push(x().encode(t))}writeSlice(t){this.bufs.push(P.from(t))}writeVarSlice(t){this.writeVarInt(t.length),this.writeSlice(t)}buffer(){return P.concat(this.bufs)}}class U{buffer;offset;constructor(t,e=0){this.buffer=t,this.offset=e}available(){return this.buffer.length-this.offset}readUInt8(){let t=this.buffer.readUInt8(this.offset);return this.offset++,t}readInt32(){let t=this.buffer.readInt32LE(this.offset);return this.offset+=4,t}readUInt32(){let t=this.buffer.readUInt32LE(this.offset);return this.offset+=4,t}readUInt64(){return O(this.readSlice(8))}readVarInt(){let t=x().decode(this.buffer,this.offset);return this.offset+=x().decode.bytes,t}readSlice(t){if(this.buffer.length<this.offset+t)throw Error("Cannot read slice out of bounds");let e=this.buffer.slice(this.offset,this.offset+t);return this.offset+=t,e}readVarSlice(){return this.readSlice(this.readVarInt())}readVector(){let t=this.readVarInt(),e=[];for(let r=0;r<t;r++)e.push(this.readVarSlice());return e}}var D=r(52235).Buffer;!function(t){t[t.TX_VERSION=2]="TX_VERSION",t[t.FALLBACK_LOCKTIME=3]="FALLBACK_LOCKTIME",t[t.INPUT_COUNT=4]="INPUT_COUNT",t[t.OUTPUT_COUNT=5]="OUTPUT_COUNT",t[t.TX_MODIFIABLE=6]="TX_MODIFIABLE",t[t.VERSION=251]="VERSION"}(n||(n={})),function(t){t[t.NON_WITNESS_UTXO=0]="NON_WITNESS_UTXO",t[t.WITNESS_UTXO=1]="WITNESS_UTXO",t[t.PARTIAL_SIG=2]="PARTIAL_SIG",t[t.SIGHASH_TYPE=3]="SIGHASH_TYPE",t[t.REDEEM_SCRIPT=4]="REDEEM_SCRIPT",t[t.BIP32_DERIVATION=6]="BIP32_DERIVATION",t[t.FINAL_SCRIPTSIG=7]="FINAL_SCRIPTSIG",t[t.FINAL_SCRIPTWITNESS=8]="FINAL_SCRIPTWITNESS",t[t.PREVIOUS_TXID=14]="PREVIOUS_TXID",t[t.OUTPUT_INDEX=15]="OUTPUT_INDEX",t[t.SEQUENCE=16]="SEQUENCE",t[t.TAP_KEY_SIG=19]="TAP_KEY_SIG",t[t.TAP_BIP32_DERIVATION=22]="TAP_BIP32_DERIVATION"}(i||(i={})),function(t){t[t.REDEEM_SCRIPT=0]="REDEEM_SCRIPT",t[t.BIP_32_DERIVATION=2]="BIP_32_DERIVATION",t[t.AMOUNT=3]="AMOUNT",t[t.SCRIPT=4]="SCRIPT",t[t.TAP_BIP32_DERIVATION=7]="TAP_BIP32_DERIVATION"}(s||(s={}));let N=D.from([112,115,98,116,255]);
1class M extends Error{}class R{globalMap=new Map;inputMaps=[];outputMaps=[];setGlobalTxVersion(t){this.setGlobal(n.TX_VERSION,V(t))}getGlobalTxVersion(){return this.getGlobal(n.TX_VERSION).readUInt32LE(0)}setGlobalFallbackLocktime(t){this.setGlobal(n.FALLBACK_LOCKTIME,V(t))}getGlobalFallbackLocktime(){return this.getGlobalOptional(n.FALLBACK_LOCKTIME)?.readUInt32LE(0)}setGlobalInputCount(t){this.setGlobal(n.INPUT_COUNT,F(t))}getGlobalInputCount(){return q(this.getGlobal(n.INPUT_COUNT))}setGlobalOutputCount(t){this.setGlobal(n.OUTPUT_COUNT,F(t))}getGlobalOutputCount(){return q(this.getGlobal(n.OUTPUT_COUNT))}setGlobalTxModifiable(t){this.setGlobal(n.TX_MODIFIABLE,t)}getGlobalTxModifiable(){return this.getGlobalOptional(n.TX_MODIFIABLE)}setGlobalPsbtVersion(t){this.setGlobal(n.VERSION,V(t))}getGlobalPsbtVersion(){return this.getGlobal(n.VERSION).readUInt32LE(0)}setInputNonWitnessUtxo(t,e){this.setInput(t,i.NON_WITNESS_UTXO,C(),e)}getInputNonWitnessUtxo(t){return this.getInputOptional(t,i.NON_WITNESS_UTXO,C())}setInputWitnessUtxo(t,e,r){let n=new B;n.writeSlice(e),n.writeVarSlice(r),this.setInput(t,i.WITNESS_UTXO,C(),n.buffer())}getInputWitnessUtxo(t){let e=this.getInputOptional(t,i.WITNESS_UTXO,C());if(!e)return;let r=new U(e);return{amount:r.readSlice(8),scriptPubKey:r.readVarSlice()}}setInputPartialSig(t,e,r){this.setInput(t,i.PARTIAL_SIG,e,r)}getInputPartialSig(t,e){return this.getInputOptional(t,i.PARTIAL_SIG,e)}setInputSighashType(t,e){this.setInput(t,i.SIGHASH_TYPE,C(),V(e))}getInputSighashType(t){let e=this.getInputOptional(t,i.SIGHASH_TYPE,C());if(e)return e.readUInt32LE(0)}setInputRedeemScript(t,e){this.setInput(t,i.REDEEM_SCRIPT,C(),e)}getInputRedeemScript(t){return this.getInputOptional(t,i.REDEEM_SCRIPT,C())}setInputBip32Derivation(t,e,r,n){if(33!=e.length)throw Error("Invalid pubkey length: "+e.length);this.setInput(t,i.BIP32_DERIVATION,e,this.encodeBip32Derivation(r,n))}getInputBip32Derivation(t,e){let r=this.getInputOptional(t,i.BIP32_DERIVATION,e);if(r)return this.decodeBip32Derivation(r)}setInputFinalScriptsig(t,e){this.setInput(t,i.FINAL_SCRIPTSIG,C(),e)}getInputFinalScriptsig(t){return this.getInputOptional(t,i.FINAL_SCRIPTSIG,C())}setInputFinalScriptwitness(t,e){this.setInput(t,i.FINAL_SCRIPTWITNESS,C(),e)}getInputFinalScriptwitness(t){return this.getInput(t,i.FINAL_SCRIPTWITNESS,C())}setInputPreviousTxId(t,e){this.setInput(t,i.PREVIOUS_TXID,C(),e)}getInputPreviousTxid(t){return this.getInput(t,i.PREVIOUS_TXID,C())}setInputOutputIndex(t,e){this.setInput(t,i.OUTPUT_INDEX,C(),V(e))}getInputOutputIndex(t){return this.getInput(t,i.OUTPUT_INDEX,C()).readUInt32LE(0)}setInputSequence(t,e){this.setInput(t,i.SEQUENCE,C(),V(e))}getInputSequence(t){return this.getInputOptional(t,i.SEQUENCE,C())?.readUInt32LE(0)??0xffffffff}setInputTapKeySig(t,e){this.setInput(t,i.TAP_KEY_SIG,C(),e)}getInputTapKeySig(t){return this.getInputOptional(t,i.TAP_KEY_SIG,C())}setInputTapBip32Derivation(t,e,r,n,s){if(32!=e.length)throw Error("Invalid pubkey length: "+e.length);let a=this.encodeTapBip32Derivation(r,n,s);this.setInput(t,i.TAP_BIP32_DERIVATION,e,a)}getInputTapBip32Derivation(t,e){let r=this.getInput(t,i.TAP_BIP32_DERIVATION,e);return this.decodeTapBip32Derivation(r)}getInputKeyDatas(t,e){return this.getKeyDatas(this.inputMaps[t],e)}setOutputRedeemScript(t,e){this.setOutput(t,s.REDEEM_SCRIPT,C(),e)}getOutputRedeemScript(t){return this.getOutput(t,s.REDEEM_SCRIPT,C())}setOutputBip32Derivation(t,e,r,n){this.setOutput(t,s.BIP_32_DERIVATION,e,this.encodeBip32Derivation(r,n))}getOutputBip32Derivation(t,e){let r=this.getOutput(t,s.BIP_32_DERIVATION,e);return this.decodeBip32Derivation(r)}setOutputAmount(t,e){this.setOutput(t,s.AMOUNT,C(),_(e))}getOutputAmount(t){return O(this.getOutput(t,s.AMOUNT,C()))}setOutputScript(t,e){this.setOutput(t,s.SCRIPT,C(),e)}getOutputScript(t){return this.getOutput(t,s.SCRIPT,C())}setOutputTapBip32Derivation(t,e,r,n,i){let a=this.encodeTapBip32Derivation(r,n,i);this.setOutput(t,s.TAP_BIP32_DERIVATION,e,a)}getOutputTapBip32Derivation(t,e){let r=this.getOutput(t,s.TAP_BIP32_DERIVATION,e);return this.decodeTapBip32Derivation(r)}getOutputKeyDatas(t,e){return this.getKeyDatas(this.outputMaps[t],e)}deleteInputEntries(t,e){this.inputMaps[t].forEach((t,r,n)=>{this.isKeyType(r,e)&&n.delete(r)})}copy(t){this.copyMap(this.globalMap,t.globalMap),this.copyMaps(this.inputMaps,t.inputMaps),this.copyMaps(this.outputMaps,t.outputMaps)}copyMaps(t,e){t.forEach((t,r)=>{let n=new Map;this.copyMap(t,n),e[r]=n})}copyMap(t,e){t.forEach((t,r)=>e.set(r,D.from(t)))}serialize(){let t=new B;return t.writeSlice(N),K(t,this.globalMap),this.inputMaps.forEach(e
1=>{K(t,e)}),this.outputMaps.forEach(e=>{K(t,e)}),t.buffer()}deserialize(t){let e=new U(t);if(!e.readSlice(5).equals(N))throw Error("Invalid magic bytes");for(;this.readKeyPair(this.globalMap,e););for(let t=0;t<this.getGlobalInputCount();t++)for(this.inputMaps[t]=new Map;this.readKeyPair(this.inputMaps[t],e););for(let t=0;t<this.getGlobalOutputCount();t++)for(this.outputMaps[t]=new Map;this.readKeyPair(this.outputMaps[t],e););}static getPsbtVersionNumber(t){let e=new Map,r=new U(t.subarray(N.length));for(;r.available()>0;){let t=r.readVarInt();if(0===t)break;let n=r.readUInt8(),i=t>1?r.readSlice(t-1):D.alloc(0),s=D.concat([D.from([n]),i]).toString("hex"),a=r.readVarInt(),o=a>0?r.readSlice(a):D.alloc(0);e.set(s,o)}let i=D.from([n.VERSION]).toString("hex"),s=e.get(i);return s?s.readUInt32LE(0):0}static fromV0(t,e=!1){let r=p.iL.fromBuffer(t),n=new R;R.initializeFromV0(n,r,e);let i=r.data.getTransaction(),s=p.ZX.fromBuffer(i);return R.addInputsFromV0(n,r,s),R.addOutputsFromV0(n,r,s),R.transferPartialSignatures(n,r),R.transferFinalizedScripts(n,r),n}static initializeFromV0(t,e,r){let n=e.data.getTransaction().readInt32LE(0);if(1===n&&!r)throw Error("Transaction version 1 detected. PSBTv2 recommends version 2 for BIP68 sequence support. Pass allowTxnVersion1=true to override.");t.setGlobalTxVersion(n),t.setGlobalFallbackLocktime(e.locktime),t.setGlobalInputCount(e.data.inputs.length),t.setGlobalOutputCount(e.data.outputs.length),t.setGlobalPsbtVersion(2)}static addInputsFromV0(t,e,r){for(let[n,i]of e.data.inputs.entries()){if(t.setInputPreviousTxId(n,r.ins[n].hash),t.setInputOutputIndex(n,r.ins[n].index),t.setInputSequence(n,r.ins[n].sequence),i.nonWitnessUtxo&&t.setInputNonWitnessUtxo(n,i.nonWitnessUtxo),i.witnessUtxo){let e=_(i.witnessUtxo.value);t.setInputWitnessUtxo(n,e,i.witnessUtxo.script)}if(i.redeemScript&&t.setInputRedeemScript(n,i.redeemScript),void 0!==i.sighashType&&t.setInputSighashType(n,i.sighashType),i.bip32Derivation)for(let e of i.bip32Derivation)t.setInputBip32Derivation(n,e.pubkey,e.masterFingerprint,W(e.path))}}static addOutputsFromV0(t,e,r){for(let[n,i]of e.data.outputs.entries())if(t.setOutputAmount(n,r.outs[n].value),t.setOutputScript(n,r.outs[n].script),i.redeemScript&&t.setOutputRedeemScript(n,i.redeemScript),i.bip32Derivation)for(let e of i.bip32Derivation)t.setOutputBip32Derivation(n,e.pubkey,e.masterFingerprint,W(e.path))}static transferPartialSignatures(t,e){for(let[r,n]of e.data.inputs.entries())if(n.partialSig)for(let e of n.partialSig)t.setInputPartialSig(r,e.pubkey,e.signature)}static transferFinalizedScripts(t,e){for(let[r,n]of e.data.inputs.entries())n.finalScriptSig&&t.setInputFinalScriptsig(r,n.finalScriptSig),n.finalScriptWitness&&t.setInputFinalScriptwitness(r,n.finalScriptWitness)}readKeyPair(t,e){let r=e.readVarInt();if(0==r)return!1;let n=e.readUInt8();return L(t,n,e.readSlice(r-1),e.readVarSlice()),!0}getKeyDatas(t,e){let r=[];return t.forEach((t,n)=>{this.isKeyType(n,[e])&&r.push(D.from(n.substring(2),"hex"))}),r}isKeyType(t,e){let r=D.from(t.substring(0,2),"hex").readUInt8(0);return e.some(t=>t==r)}setGlobal(t,e){let r=new k(t,D.from([]));this.globalMap.set(r.toString(),e)}getGlobal(t){return A(this.globalMap,t,C(),!1)}getGlobalOptional(t){return A(this.globalMap,t,C(),!0)}setInput(t,e,r,n){L(this.getMap(t,this.inputMaps),e,r,n)}getInput(t,e,r){return A(this.inputMaps[t],e,r,!1)}getInputOptional(t,e,r){return A(this.inputMaps[t],e,r,!0)}setOutput(t,e,r,n){L(this.getMap(t,this.outputMaps),e,r,n)}getOutput(t,e,r){return A(this.outputMaps[t],e,r,!1)}getMap(t,e){return e[t]||(e[t]=new Map),e[t]}encodeBip32Derivation(t,e){let r=new B;return this.writeBip32Derivation(r,t,e),r.buffer()}decodeBip32Derivation(t){let e=new U(t);return this.readBip32Derivation(e)}writeBip32Derivation(t,e,r){t.writeSlice(e),r.forEach(e=>{t.writeUInt32(e)})}readBip32Derivation(t){let e=t.readSlice(4),r=[];for(;t.offset<t.buffer.length;)r.push(t.readUInt32());return{masterFingerprint:e,path:r}}encodeTapBip32Derivation(t,e,r){let n=new B;return n.writeVarInt(t.length),t.forEach(t=>{n.writeSlice(t)}),this.writeBip32Derivation(n,e,r),n.buffer()}decodeTapBip32Derivation(t){let e=new U(t),r=e.readVarInt(),n=[];for(let t=0;t<r;t++)n.push(e.readSlice(32));return{hashes:n,...this.readBip32Derivation(e)}}}function A(t,e,r,n){if(!t)throw Error("No such map");let i=new k(e,r),s=t.get(i.toString());if(!s){if(n)return;throw new M(i.toString())}return D.from(s)}class k{keyType;keyData;constructor(t,e){this.keyType=t,this.keyData=e}toString(){let t=new B;return this.toBuffer(t),t.buffer().toString("hex")}serialize(t){t.writeVarInt(1+this.keyData.length),this.toBuffer(t)}toBuffer(t){t.writeUInt8(this.keyType),t.writeSlice(this.keyData)}}class G{key;value;constructor(t,e){this.key=t,this.value=e}serialize(t){this.key.serialize(t),t.writeVarSlice(this.value)}}function K(t,e){for(let n of e.keys()){var r;let i=e.get(n);new G(new k((r=D.from(n,"hex")).readUInt8(0),r.subarray(1)),i).serialize(t)}t.writeUInt8(0)}function C(){return D.from([])}function L(t,e,r,n){let i=new k(e,r);t.set(i.toString(),n)}function V(t){let e=D.alloc(4);return e.writeUInt32LE(t,0),e}function F(t){let e=new B;return e.writeVarInt(t),e.buffer()}function q(t){return new U(t).readVarInt()}function W(t){return t.split("/").slice(1).map(e=>{let r=e.endsWith("'")||e.endsWith("h"),n=Number(r?e.slice(0,-1):e);if(Number.isNaN(n))throw TypeError(`Invalid BIP32 path segment: ${t}`);return r?n+0x80000000:n})}r(52235).Buffer;let H={P2WPKH:{LENGTH:22,PREFIX:[0,20],SUFFIX:[]},P2TR:{LENGTH:34,PREFIX:[81,32],SUFFIX:[]},P2SH:{LENGTH:23,PREFIX:[169,20],SUFFIX:[135]},P2PKH:{LENGTH:25,PREFIX:[118,169,20],SUFFIX:[136,172]}}
1,X=[{type:"p2wpkh",spec:H.P2WPKH},{type:"p2tr",spec:H.P2TR},{type:"p2sh",spec:H.P2SH},{type:"p2pkh",spec:H.P2PKH}];function $(t){for(let{type:e,spec:r}of X){let{LENGTH:n,PREFIX:i,SUFFIX:s}=r;if(!(t.length!==n||i.some((e,r)=>t[r]!==e))&&!s.some((e,r)=>t[t.length-s.length+r]!==e))return e}}let z={NU6_1:3146400,NU6:2726400,NU5:1687104,CANOPY:1046400,HEARTWOOD:903e3,BLOSSOM:653600,SAPLING:419200};var Y=r(65072),j=r(29928),Q=r(52235).Buffer;function Z(t){return Q.from((0,j.H)((0,Y.sc)(t)))}var J=r(52235).Buffer;class tt{psbt;masterFp;constructor(t,e){this.psbt=t,this.masterFp=e}}class te extends tt{spendingCondition(t){if(1!=t.length)throw Error("Expected single key, got "+t.length);return this.singleKeyCondition(t[0])}setInput(t,e,r,n,i){if(1!=n.length)throw Error("Expected single key, got "+n.length);if(1!=i.length)throw Error("Expected single path, got "+i.length);this.setSingleKeyInput(t,e,r,n[0],i[0])}setOwnOutput(t,e,r,n){if(1!=r.length)throw Error("Expected single key, got "+r.length);if(1!=n.length)throw Error("Expected single path, got "+n.length);this.setSingleKeyOutput(t,e,r[0],n[0])}}class tr extends te{singleKeyCondition(t){let e=new B,r=Z(t);return e.writeSlice(J.from([118,169,20])),e.writeSlice(r),e.writeSlice(J.from([136,172])),{scriptPubKey:e.buffer()}}setSingleKeyInput(t,e,r,n,i){if(!e)throw Error("Full input base transaction required");this.psbt.setInputNonWitnessUtxo(t,e),this.psbt.setInputBip32Derivation(t,n,this.masterFp,i)}setSingleKeyOutput(t,e,r,n){this.psbt.setOutputBip32Derivation(t,r,this.masterFp,n)}getDescriptorTemplate(){return"pkh(@0)"}}class tn extends te{singleKeyCondition(t){let e=t.subarray(1),r=new B,n=this.getTaprootOutputKey(e);return r.writeSlice(J.from([81,32])),r.writeSlice(n),{scriptPubKey:r.buffer()}}setSingleKeyInput(t,e,r,n,i){let s=n.subarray(1);this.psbt.setInputTapBip32Derivation(t,s,[],this.masterFp,i),this.psbt.setInputWitnessUtxo(t,r.amount,r.cond.scriptPubKey)}setSingleKeyOutput(t,e,r,n){let i=r.subarray(1);this.psbt.setOutputTapBip32Derivation(t,i,[],this.masterFp,n)}getDescriptorTemplate(){return"tr(@0)"}getTaprootOutputKey(t){if(32!==t.length)throw Error("Expected 32 byte pubkey. Got "+t.length);let e=p.Et.sha256(J.from("TapTweak","utf-8")),r=p.Et.sha256(J.concat([e,e,t])),n=function(t,e){try{let r=h.bI.ProjectivePoint.fromHex(t),n=BigInt("0x"+Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join(""));return r.add(h.bI.ProjectivePoint.BASE.multiply(n)).toRawBytes(33===t.length)}catch{return null}}(J.concat([J.from([2]),t]),r);if(!n)throw Error("Point addition failed");return J.from(n).subarray(1)}}class ti extends te{singleKeyCondition(t){let e=new B,r=this.createRedeemScript(t),n=Z(r);return e.writeSlice(J.from([169,20])),e.writeSlice(n),e.writeUInt8(135),{scriptPubKey:e.buffer(),redeemScript:r}}setSingleKeyInput(t,e,r,n,i){if(!e)throw Error("Full input base transaction required");this.psbt.setInputNonWitnessUtxo(t,e),this.psbt.setInputBip32Derivation(t,n,this.masterFp,i);let s=r.cond.redeemScript,a=this.createRedeemScript(n);if(s&&!a.equals(s))throw Error(`User-supplied redeemScript ${s.toString("hex")} doesn't 2 match expected ${a.toString("hex")} for input ${t}`);this.psbt.setInputRedeemScript(t,a),this.psbt.setInputWitnessUtxo(t,r.amount,r.cond.scriptPubKey)}setSingleKeyOutput(t,e,r,n){e.redeemScript&&this.psbt.setOutputRedeemScript(t,e.redeemScript),this.psbt.setOutputBip32Derivation(t,r,this.masterFp,n)}getDescriptorTemplate(){return"sh(wpkh(@0))"}createRedeemScript(t){let e=Z(t);return J.concat([J.from("0014","hex"),e])}}class ts extends te{singleKeyCondition(t){let e=new B,r=Z(t);return e.writeSlice(J.from([0,20])),e.writeSlice(r),{scriptPubKey:e.buffer()}}setSingleKeyInput(t,e,r,n,i){if(!e)throw Error("Full input base transaction required");this.psbt.setInputNonWitnessUtxo(t,e),this.psbt.setInputBip32Derivation(t,n,this.masterFp,i),this.psbt.setInputWitnessUtxo(t,r.amount,r.cond.scriptPubKey)}setSingleKeyOutput(t,e,r,n){this.psbt.setOutputBip32Derivation(t,r,this.masterFp,n)}getDescriptorTemplate(){return"wpkh(@0)"}}var ta=r(52235).Buffer;class to{leaves;rootNode;leafNodes;h;constructor(t,e=p.Et.sha256){this.leaves=t,this.h=e;let r=this.calculateRoot(t);this.rootNode=r.root,this.leafNodes=r.leaves}getRoot(){return this.rootNode.hash}size(){return this.leaves.length}getLeaves(){return this.leaves}getLeafHash(t){return this.leafNodes[t].hash}getProof(t){if(t>=this.leaves.length)throw Error("Index out of bounds");return function t(e){if(!e.parent)return[];if(e.parent.leftChild==e){if(!e.parent.rightChild)throw Error("Expected right child to exist");return[e.parent.rightChild.hash,...t(e.parent)]}if(!e.parent.leftChild)throw Error("Expected left child to exist");return[e.parent.leftChild.hash,...t(e.parent)]}(this.leafNodes[t])}calculateRoot(t){let e=t.length;if(0==e)return{root:new tu(void 0,void 0,ta.alloc(32,0)),leaves:[]};if(1==e){let e=new tu(void 0,void 0,t[0]);return{root:e,leaves:[e]}}let r=function(t){var e;if(t<2)throw Error("Expected n >= 2");return((e=t)&e-1)==0?t/2:1<<Math.floor(Math.log2(t))}(e),n=this.calculateRoot(t.slice(0,r)),i=this.calculateRoot(t.slice(r)),s=n.root,a=i.root,o=this.hashNode(s.hash,a.hash),l=new tu(s,a,o);return s.parent=l,a.parent=l,{root:l,leaves:n.leaves.concat(i.leaves)}}hashNode(t,e){return this.h(ta.concat([ta.from([1]),t,e]))}}function tl(t,e=p.Et.sha256){var r,n;return r=ta.from([0]),n=t,e(ta.concat([r,n]))}class tu{leftChild;rightChild;parent;hash;constructor(t,e,r){this.leftChild=t,this.rightChild=e,this.hash=r}isLeaf(){return void 0==this.leftChild}}var tc=r(52235).Buffer;class tp{descriptorTemplate;keys;constructor(t,e){this.descriptorTemplate=t,this.keys=[e]}
2getWalletId(){return p.Et.sha256(this.serialize())}serialize(){let t=new to(this.keys.map(t=>tc.from(t,"ascii")).map(t=>tl(t))),e=new B;return e.writeUInt8(1),e.writeUInt8(0),e.writeVarSlice(tc.from(this.descriptorTemplate,"ascii")),e.writeVarInt(this.keys.length),e.writeSlice(t.getRoot()),e.buffer()}}function th(t,e,r){let n=S(e);return`[${t.toString("hex")}${n.substring(1)}]${r}/**`}var tf=r(52235).Buffer;function tg(t){let e=new B;e.writeUInt32(t.getGlobalTxVersion());let r=!!t.getInputWitnessUtxo(0);r&&e.writeSlice(tf.from([0,1]));let n=t.getGlobalInputCount();e.writeVarInt(n);let i=new B;for(let s=0;s<n;s++)e.writeSlice(t.getInputPreviousTxid(s)),e.writeUInt32(t.getInputOutputIndex(s)),e.writeVarSlice(t.getInputFinalScriptsig(s)??tf.from([])),e.writeUInt32(t.getInputSequence(s)),r&&i.writeSlice(t.getInputFinalScriptwitness(s));let s=t.getGlobalOutputCount();e.writeVarInt(s);for(let r=0;r<s;r++)e.writeUInt64(t.getOutputAmount(r)),e.writeVarSlice(t.getOutputScript(r));return e.writeSlice(i.buffer()),e.writeUInt32(t.getGlobalFallbackLocktime()??0),e.buffer()}var td=r(52235).Buffer;function tI(t){let e=t.getGlobalInputCount();for(let s=0;s<e;s++){let e=t.getInputKeyDatas(s,i.PARTIAL_SIG),a=t.getInputTapKeySig(s);if(0==e.length&&!a)throw Error(`No signature for input ${s} present`);if(e.length>0){if(e.length>1)throw Error(`Expected exactly one signature, got ${e.length}`);if(a)throw Error("Both taproot and non-taproot signatures present.");let r=!!t.getInputWitnessUtxo(s),n=t.getInputRedeemScript(s),i=!!n,o=t.getInputPartialSig(s,e[0]);if(!o)throw Error("Expected partial signature for input "+s);if(r){let r=new B;if(r.writeVarInt(2),r.writeVarInt(o.length),r.writeSlice(o),r.writeVarInt(e[0].length),r.writeSlice(e[0]),t.setInputFinalScriptwitness(s,r.buffer()),i){if(!n||0==n.length)throw Error("Expected non-empty redeemscript. Can't finalize intput "+s);let e=new B;e.writeUInt8(n.length),e.writeSlice(n),t.setInputFinalScriptsig(s,e.buffer())}}else{let r=new B;tw(r,o),tw(r,e[0]),t.setInputFinalScriptsig(s,r.buffer())}}else{let e=t.getInputTapKeySig(s);if(!e)throw Error("No taproot signature found");if(64!=e.length&&65!=e.length)throw Error("Unexpected length of schnorr signature.");let r=new B;r.writeVarInt(1),r.writeVarSlice(e),t.setInputFinalScriptwitness(s,r.buffer())}var r=t,n=s;let o=[i.BIP32_DERIVATION,i.PARTIAL_SIG,i.TAP_BIP32_DERIVATION,i.TAP_KEY_SIG],l=!!r.getInputWitnessUtxo(n),u=!!r.getInputNonWitnessUtxo(n);l&&u&&o.push(i.NON_WITNESS_UTXO),r.deleteInputEntries(n,o)}}function tw(t,e){if(e.length<=75)t.writeUInt8(e.length);else if(e.length<=256)t.writeUInt8(76),t.writeUInt8(e.length);else if(e.length<=65536){t.writeUInt8(77);let r=td.alloc(2);r.writeUInt16LE(e.length,0),t.writeSlice(r)}t.writeSlice(e)}var tm=r(52235).Buffer;function tE(t,e){if(t[e]<253)return[t[e],1];if(253===t[e])return[(t[e+2]<<8)+t[e+1],3];if(254===t[e])return[(t[e+4]<<24)+(t[e+3]<<16)+(t[e+2]<<8)+t[e+1],5];throw Error("getVarint called with unexpected parameters")}function tS(t){if(t<253){let e=tm.alloc(1);return e[0]=t,e}if(t<=65535){let e=tm.alloc(3);return e[0]=253,e[1]=255&t,e[2]=t>>8&255,e}let e=tm.alloc(5);return e[0]=254,e[1]=255&t,e[2]=t>>8&255,e[3]=t>>16&255,e[4]=t>>24&255,e}var ty=r(52235).Buffer;function tb({outputs:t}){let e=ty.alloc(0);return void 0!==t&&(e=ty.concat([e,tS(t.length)]),t.forEach(t=>{e=ty.concat([e,t.amount,tS(t.script.length),t.script])})),e}function tT(t,e,r,n=[]){let i=n.includes("decred"),s=n.includes("zcash"),a=n.includes("bech32"),o=ty.alloc(0),l=void 0!==t.witness&&!e;
2t.inputs.forEach(t=>{o=i||a?ty.concat([o,t.prevout,ty.from([0]),t.sequence]):ty.concat([o,t.prevout,tS(t.script.length),t.script,t.sequence])});let u=tb(t);return(void 0!==t.outputs&&void 0!==t.locktime&&(u=ty.concat([u,l&&t.witness||ty.alloc(0),t.locktime,t.nExpiryHeight||ty.alloc(0),t.extraData||ty.alloc(0)])),s)?ty.concat([t.version,t.nVersionGroupId||ty.alloc(0),t.consensusBranchId||ty.from([0,0,0,0]),t.locktime||ty.from([0,0,0,0]),t.nExpiryHeight||ty.from([0,0,0,0]),l?ty.from("0001","hex"):ty.alloc(0),tS(t.inputs.length),o,u]):ty.concat([t.version,r||ty.alloc(0),t.nVersionGroupId||ty.alloc(0),l?ty.from("0001","hex"):ty.alloc(0),tS(t.inputs.length),o,u])}function tv(t,e){return"input"===e?{getKeyDatas:(e,r)=>t.getInputKeyDatas(e,r),getBip32Derivation:(e,r)=>t.getInputBip32Derivation(e,r),getTapBip32Derivation:(e,r)=>t.getInputTapBip32Derivation(e,r),setBip32Derivation:(e,r,n,i)=>t.setInputBip32Derivation(e,r,n,i),setTapBip32Derivation:(e,r,n,i,s)=>t.setInputTapBip32Derivation(e,r,n,i,s),bip32KeyType:i.BIP32_DERIVATION,tapBip32KeyType:i.TAP_BIP32_DERIVATION}:{getKeyDatas:(e,r)=>t.getOutputKeyDatas(e,r),getBip32Derivation:(e,r)=>t.getOutputBip32Derivation(e,r),getTapBip32Derivation:(e,r)=>t.getOutputTapBip32Derivation(e,r),setBip32Derivation:(e,r,n,i)=>t.setOutputBip32Derivation(e,r,n,i),setTapBip32Derivation:(e,r,n,i,s)=>t.setOutputTapBip32Derivation(e,r,n,i,s),bip32KeyType:s.BIP_32_DERIVATION,tapBip32KeyType:s.TAP_BIP32_DERIVATION}}function tx(t,e,r){for(let n of t.getKeyDatas(e,t.bip32KeyType)){let i=t.getBip32Derivation(e,n);if(i?.masterFingerprint.equals(r))return tP(i.path)}for(let n of t.getKeyDatas(e,t.tapBip32KeyType)){let i=t.getTapBip32Derivation(e,n);if(i?.masterFingerprint.equals(r))return tP(i.path)}return{belongsToSigner:!1,accountPath:[]}}function tP(t){return{belongsToSigner:!0,accountPath:t.length>=2?t.slice(0,-2):[]}}function t_(t,e,r){return tx(tv(t,"input"),e,r)}function tO(t,e,r){switch(t){case"p2wpkh":return new ts(e,r);case"p2tr":return new tn(e,r);case"p2sh":case"p2sh-p2wpkh":return new ti(e,r);case"p2pkh":return new tr(e,r)}}function tB(t,e,r){if(t.length<1)return null;switch(t[0]-0x80000000){case 44:return new tr(e,r);case 49:return new ti(e,r);case 84:return new ts(e,r);case 86:return new tn(e,r);default:return null}}async function tU(t,e,r,n){for(let i of e.getKeyDatas(r,e.bip32KeyType)){let s=e.getBip32Derivation(r,i);if(!s||s.masterFingerprint.equals(n))continue;let a=s.path,o=b(await t.getExtendedPubkey(!1,a));if(i.equals(o))return e.setBip32Derivation(r,i,n,a),!0}for(let i of e.getKeyDatas(r,e.tapBip32KeyType)){let s=e.getTapBip32Derivation(r,i);if(!s||s.masterFingerprint.equals(n))continue;let a=s.path,o=b(await t.getExtendedPubkey(!1,a)).subarray(1);if(i.equals(o))return e.setTapBip32Derivation(r,i,s.hashes,n,a),!0}return!1}async function tD(t,e,r,n,i){return tU(t,tv(e,i),r,n)}async function tN(t,e,r,n,i,s){let a=[];for(let o=0;o<r;o++)!s(o)&&(await tD(t,e,o,n,i)||a.push(o));return a}function tM(t,e,r,n,i,s){let a=tv(t,i);for(let i of e){let e=s(i);if(!e)continue;let o=function(t){let e=$(t);if(e)switch(e){case"p2wpkh":case"p2sh":return{hashHex:t.subarray(2,22).toString("hex"),scriptType:e};case"p2pkh":return{hashHex:t.subarray(3,23).toString("hex"),scriptType:e};case"p2tr":return{hashHex:t.subarray(2,34).toString("hex"),scriptType:e};default:return e}}(e);if(!o)continue;let l=r.get(o.hashHex);if(l){let e=tO(o.scriptType,t,n);!function(t,e,r,n,i,s){if(s instanceof tn){let s=r.subarray(1);t.setTapBip32Derivation(e,s,[],n,i)}else t.setBip32Derivation(e,r,n,i)}(a,i,l.pubkey,n,l.path,e)}}}async function tR(t,e,r,n,i,s){if(!tB(i,e,n))return;let a=e.getGlobalOutputCount(),[o,l]=await Promise.all([tN(t,e,r,n,"input",t=>t_(e,t,n).belongsToSigner),tN(t,e,a,n,"output",t=>tx(tv(e,"output"),t,n).belongsToSigner)]);(0!==o.length||0!==l.length)&&0!==s.size&&(tM(e,o,s,n,"input",t=>(function(t,e){let r=t.getInputWitnessUtxo(e);return r?r.scriptPubKey:function(t,e){let r=t.getInputNonWitnessUtxo(e);if(r)try{let n=t.getInputOutputIndex(e),i=new U(r);i.readSlice(4);let s=i.readUInt8();if(0===s){let t=i.readUInt8();if(0===t)return}else i.offset-=1;let a=i.readVarInt();for(let t=0;t<a;t++){i.readSlice(32),i.readSlice(4);let t=i.readVarInt();i.readSlice(t),i.readSlice(4)}let o=i.readVarInt();for(let t=0;t<o;t++){i.readSlice(8);let e=i.readVarInt(),r=i.readSlice(e);if(t===n)return r}return}catch{return}}(t,e)})(e,t)),tM(e,l,s,n,"output",t=>e.getOutputScript(t)))}var tA=r(52235).Buffer;class tk{client;constructor(t){this.client=t}async getWalletXpub({path:t,xpubVersion:e}){let r=y(t),n=await this.client.getExtendedPubkey(!1,r),i=T(n);if(i.version!=e)throw Error(`Expected xpub version ${e} doesn't match the xpub version from the device ${i.version}`);return n}async getWalletPublicKey(t,e){if(!function(t){let e=y(t);return!!(e.length>=3&&e.length<=5&&tL.has(e[0])&&tC.has(e[1])&&e[2]>=0x80000000&&tK(e[3])&&tG(e[4]))||!!(e.length>=4&&e.length<=6&&tV.has(e[0])&&tC.has(e[1])&&e[2]>=0x80000000&&e[3]>=0x80000000&&tK(e[4])&&tG(e[5]))}(t))throw Error(`non-standard path: ${t}`);let r=y(t),n=await this.client.getExtendedPubkey(!1,r),i=e?.verify??!1,s=await this.getWalletAddress(r,function(t){if("legacy"==t)return"pkh(@0)";if("p2sh"==t)return"sh(wpkh(@0))";if("bech32"==t)return"wpkh(@0)";if("bech32m"==t)return"tr(@0)";throw Error("Unsupported address format "+t)}(e?.format??"legacy"),i),a=T(n);return{publicKey:tA.from(function(t,e=!0){return h.bI.ProjectivePoint.fromHex(t).toRawBytes(e)}(a.pubkey,!1)).toString("hex"),bitcoinAddress:s,chainCode:a.chaincode.toString("hex")}}async getWalletAddress(t,e,r){let n=function(t){for(let e=t.length-1;e>=0;e--)if(t[e]>=0x80000000)return t.slice(0,e+1);return[]}(t);if(n.length+2!=t.length)return"";let i=await this.client.getExtendedPubkey(!1,n),s=new tp(e,th(await this.client.getMasterFingerprint(),n,i)),a=t.slice(-2,t.length);return this.client.getWalletAddress(s,tA.alloc(32,0),a[0],a[1],r)}async createPaymentTransaction(t){var e,r,n;let i=t.inputs.length;
2if(0==i)throw Error("No inputs");let s=new R,a=await this.client.getMasterFingerprint(),o=(e=t,r=s,n=a,e.additionals.includes("bech32m")?new tn(r,n):e.additionals.includes("bech32")?new ts(r,n):e.segwit?new ti(r,n):new tr(r,n));void 0!=t.lockTime&&s.setGlobalFallbackLocktime(t.lockTime),s.setGlobalInputCount(i),s.setGlobalPsbtVersion(2),s.setGlobalTxVersion(2);let l=0,u=()=>{t.onDeviceStreaming&&t.onDeviceStreaming({total:2*i,index:l,progress:++l/(2*i)})},c="",p=[];for(let e=0;e<i;e++){u();let r=y(t.associatedKeysets[e]);""==c&&(p=r.slice(0,-2),c=await this.client.getExtendedPubkey(!1,p)),await this.setInput(s,e,t.inputs[e],r,o,a,t.sigHashType)}let h=new U(tA.from(t.outputScriptHex,"hex")),f=h.readVarInt();s.setGlobalOutputCount(f);let g=await this.outputScriptAt(p,o,t.changePath),d=!g;for(let e=0;e<f;e++){let r=Number(h.readUInt64()),n=h.readVarSlice();if(s.setOutputAmount(e,r),s.setOutputScript(e,n),g&&n.equals(g?.cond.scriptPubKey)){d=!0;let r=y(t.changePath),n=g.pubkey;o.setOwnOutput(e,g.cond,[n],[r])}}if(!d)throw Error("Change script not found among outputs! "+g?.cond.scriptPubKey.toString("hex"));let I=th(a,p,c),w=new tp(o.getDescriptorTemplate(),I);t.onDeviceSignatureRequested&&t.onDeviceSignatureRequested();let m=!1;return await this.signPsbt(s,w,()=>{!m&&(m=!0,t.onDeviceSignatureGranted&&t.onDeviceSignatureGranted()),u()}),tI(s),tg(s).toString("hex")}async signPsbtBuffer(t,e){let r,n,i=function(t){let e=R.getPsbtVersionNumber(t),r=2===e?new R:R.fromV0(t,!0);return 2===e&&r.deserialize(t),r}(t),s=i.getGlobalInputCount();if(0===s)throw Error("No inputs in PSBT");let a=await this.client.getMasterFingerprint(),o=y(e.accountPath);o.length>0&&await tR(this.client,i,s,a,o,e.knownAddressDerivations);let{accountPath:l,detectedScriptType:u,internalInputIndices:c}=function(t,e,r,n){let i,s=[],a=[];for(let n=0;n<e;n++){let e=function(t,e,r){let n=t_(t,e,r),i=function(t,e){let r=t.getInputWitnessUtxo(e);return r?$(r.scriptPubKey):t.getInputRedeemScript(e)?"p2sh-p2wpkh":void 0}(t,e);return{belongsToSigner:n.belongsToSigner,accountPath:n.accountPath,scriptType:i}}(t,n,r);if(e.belongsToSigner){s.push(n),function(t,e,r){if(t.length>0&&!(t.length===e.length&&t.every((t,r)=>t===e[r])))throw Error(`Mixed accounts detected in PSBT. Input ${r} uses account path ${S(e)} but expected ${S(t)}. All internal inputs must belong to the same account.`)}(a,e.accountPath,n),0===a.length&&(a=e.accountPath);var o=i,l=e.scriptType,u=n;if(o&&l&&o!==l)throw Error(`Mixed input types detected in PSBT. Input ${u} uses ${l} but expected ${o}. All internal inputs must use the same script type.`);i||(i=e.scriptType)}}return 0===s.length&&(a=function(t){if(!t)throw Error("No internal inputs found in PSBT (no BIP32 derivation matching device fingerprint) and no account path provided in options. Please provide accountPath in options (e.g., \"m/84'/0'/0'\" for native segwit)");return y(t)}(n)),{accountPath:a,detectedScriptType:i,internalInputIndices:s}}(i,s,a,e.accountPath),p=await this.client.getExtendedPubkey(!1,l),h=function(t,e,r,n,i,s){if(n)return tO(n,t,r);let a=function(t,e,r){let n=t.getInputWitnessUtxo(e);if(!n)return null;let i=$(n.scriptPubKey);if(!i)throw Error(`Unsupported script type: ${n.scriptPubKey.toString("hex")}`);return tO(i,t,r)}(t,e,r);if(a)return a;if(t.getInputRedeemScript(e))return new ti(t,r);if(s){let e=function(t){switch(t){case"legacy":return"pkh(@0)";case"p2sh":return"sh(wpkh(@0))";case"bech32":return"wpkh(@0)";case"bech32m":return"tr(@0)";default:throw Error("Unsupported address format "+t)}}(s);switch(e){case"pkh(@0)":return new tr(t,r);case"wpkh(@0)":return new ts(t,r);case"sh(wpkh(@0))":return new ti(t,r);case"tr(@0)":return new tn(t,r);default:throw Error(`Unsupported descriptor template: ${e}`)}}let o=tB(i,t,r);return o||new ts(t,r)}(i,c.length>0?c[0]:0,a,u,l,e.addressFormat),f=function(t,e,r,n){let i=th(t,e,r);return new tp(n.getDescriptorTemplate(),i)}(a,l,p,h),g=(r=0,n=!1,e.onDeviceSignatureRequested&&e.onDeviceSignatureRequested(),()=>{!n&&(n=!0,e.onDeviceSignatureGranted&&e.onDeviceSignatureGranted()),e.onDeviceStreaming&&!(s<=0)&&e.onDeviceStreaming({total:2*s,index:r,progress:++r/(2*s)})});return await this.signPsbt(i,f,g),e.finalizePsbt?(tI(i),{psbt:i.seri
2alize(),tx:tg(i).toString("hex")}):{psbt:i.serialize()}}async signMessage({path:t,messageHex:e}){let r=y(t),n=tA.from(e,"hex"),i=await this.client.signMessage(n,r),s=tA.from(i,"base64"),a=s.readUInt8()-27-4;return{v:a,r:s.slice(1,33).toString("hex"),s:s.slice(33,65).toString("hex")}}async outputScriptAt(t,e,r){if(!r)return;let n=y(r);for(let e=0;e<t.length;e++)if(t[e]!=n[e])throw Error(`Path ${r} not in account ${S(t)}`);let i=b(await this.client.getExtendedPubkey(!1,n));return{cond:e.spendingCondition([i]),pubkey:i}}async setInput(t,e,r,n,i,s,a){let o=r[0],l=r[1],u=r[2]?tA.from(r[2],"hex"):void 0,c=r[3];void 0!=c&&t.setInputSequence(e,c),void 0!=a&&t.setInputSighashType(e,a);let h=tT(o,!0),f=p.Et.hash256(h),g=b(await this.client.getExtendedPubkey(!1,n));if(!o.outputs)throw Error("Missing outputs array in transaction to sign");let d=o.outputs[l],I={cond:{scriptPubKey:d.script,redeemScript:u},amount:d.amount};i.setInput(e,h,I,[g],[n]),t.setInputPreviousTxId(e,f),t.setInputOutputIndex(e,l)}async signPsbt(t,e,r){(await this.client.signPsbt(t,e,tA.alloc(32,0),r)).forEach((e,r)=>{let n,s=t.getInputKeyDatas(r,i.BIP32_DERIVATION);if(1!=s.length){if(0==(n=t.getInputKeyDatas(r,i.TAP_BIP32_DERIVATION)).length)throw Error(`Missing pubkey derivation for input ${r}`);t.setInputTapKeySig(r,e)}else n=s[0],t.setInputPartialSig(r,n,e)})}}let tG=t=>void 0===t||t<0x80000000,tK=t=>void 0===t||0===t||1===t,tC=new Set([0,1,88].map(t=>t+0x80000000)),tL=new Set([44,49,84,86].map(t=>t+0x80000000)),tV=new Set([48].map(t=>t+0x80000000));var tF=r(6111),tq=r.n(tF),tW=r(86727);let tH={legacy:0,p2sh:1,bech32:2,cashaddr:3};async function tX(t,e){let{path:r,verify:n,format:i}={verify:!1,format:"legacy",...e};if(!(i in tH))throw Error("btc.getWalletPublicKey invalid format="+i);let s=E(r),a=tH[i],o=await t.send(224,64,+!!n,a,s),l=o[0],u=o[1+l],c=o.slice(1,1+l).toString("hex");return{publicKey:c,bitcoinAddress:o.slice(1+l+1,1+l+1+u).toString("ascii"),chainCode:o.slice(1+l+1+u,1+l+1+u+32).toString("hex")}}var t$=r(17208),tz=r.n(t$),tY=r(52235).Buffer;async function tj(t,e,r){let n,i=!1;if("number"==typeof r){i=!0;let t=tY.alloc(4);t.writeUInt32BE(r,0),n=tY.concat([t,e],e.length+4)}else n=e;let s=await t.send(224,66,128*!i,0,n);return s.slice(0,s.length-2).toString("hex")}async function tQ(t,e,r,n=[]){let i,{inputs:s,outputs:a,locktime:o,nExpiryHeight:l,extraData:u}=r;if(!a||!o)throw Error("getTrustedInput: locktime & outputs is expected");let c=n.includes("decred"),p=async(e,r)=>{let n,i=r||tY.alloc(0),s=[],a=0;for(;a!==e.length;){let t=e.length-a>50?50:e.length-a;a+t!==e.length?s.push(e.slice(a,a+t)):s.push(tY.concat([e.slice(a,a+t),i])),a+=t}for(let r of(0===e.length&&s.push(i),s))n=await tj(t,r);return n},h=e=>tj(t,e),f=n.includes("zcash"),g=r.consensusBranchId||tY.alloc(0);for(let n of(await tj(t,tY.concat([r.version,r.timestamp||tY.alloc(0),r.nVersionGroupId||tY.alloc(0),f?g:tY.alloc(0),tS(s.length)]),e),s)){let e=c?n.tree||tY.from([0]):tY.alloc(0),r=tY.concat([n.prevout,e,tS(n.script.length)]);await tj(t,r),await (c?h(tY.concat([n.script,n.sequence])):p(n.script,n.sequence))}for(let e of(await tj(t,tS(a.length)),a)){let r=tY.concat([e.amount,c?tY.from([0,0]):tY.alloc(0),tS(e.script.length),e.script]);await tj(t,r)}if(f){let e=tY.concat([tS(r.sapling?.vSpendsSapling.length||0),tS(r.sapling?.vOutputSapling.length||0),tS(r.orchard?.vActions.length||0)]);await tj(t,e),r.sapling&&await tZ(t,r.sapling),r.orchard&&await tJ(t,r.orchard)}let d=[];if(l&&l.length>0&&d.push(l),u&&u.length>0&&d.push(u),d.length){let t=tY.concat(d);i=c?t:tY.concat([tS(t.length),t])}let I=await p(tY.concat([o,i||tY.alloc(0)]));return tz()(I,"missing result in processScriptBlocks"),I}async function tZ(t,e){let r=tY.concat([e.valueBalanceSapling,e.anchorSapling]);for(let n of(await tj(t,r),e.vSpendsSapling)){let e=tY.concat([n.cv,n.nullifier,n.rk]);await tj(t,e)}for(let r of e.vOutputSapling){let e=tY.concat([r.cmu,r.ephemeralKey,r.encCiphertext.slice(0,52)]);await tj(t,e)}for(let r of e.vOutputSapling)for(let e=0;e<4;e++){let n=52+128*e,i=n+128,s=tY.concat([r.encCiphertext.slice(n,i)]);await tj(t,s)}for(let r of e.vOutputSapling){let e=tY.concat([r.cv,r.encCiphertext.slice(564,580),r.outCiphertext]);await tj(t,e)}}async function tJ(t,e){for(let r of e.vActions){let e=tY.concat([r.nullifier,r.cmx,r.ephemeralKey,r.encCiphertext.slice(0,52)]);await tj(t,e)}for(let r of e.vActions)for(let e=0;e<4;e++){let n=52+128*e,i=n+128,s=tY.concat([r.encCiphertext.slice(n,i)]);await tj(t,s)}for(let r of e.vActions){let e=tY.concat([r.cv,r.rk,r.encCiphertext.slice(564,580),r.outCiphertext]);await tj(t,e)}let r=tY.concat([e.flags,e.valueBalance,e.anchor]);await tj(t,r)}var t0=r(52235).Buffer;function t2(t,e,r,n,i=!1,s=!1,a=[]){let o=a.includes("cashaddr")?3:i?a.includes("sapling")?5:s?4:2:0;return t.send(224,68,128*!r,e?o:128,n)}async function t3(t,e,r,n,i=!1,s=!1,a=[],o=!1){let l=a.includes("zcash"),u=r.consensusBranchId||t0.alloc(0),c=t0.concat([r.version,r.timestamp||t0.alloc(0),r.nVersionGroupId||t0.alloc(0),l?u:t0.alloc(0),tS(r.inputs.length)]);await t2(t,e,!0,c,i,s,a);let p=0,h=a.includes("decred");for(let l of r.inputs){let r,u=n[p].value;r=i?o&&n[p].trustedInput?t0.from([1,u.length]):t0.from([2]):n[p].trustedInput?t0.from([1,n[p].value.length]):t0.from([0]),c=t0.concat([r,u,h?t0.from([0]):t0.alloc(0),tS(l.script.length)]),await t2(t,e,!1,c,i,s,a);let f=[],g=0;if(0===l.script.length)f.push(l.sequence);else for(;g!==l.script.length;){let t=l.script.length-g>50?50:l.script.length-g;g+t!==l.script.length?f.push(l.script.slice(g,g+t)):f.push(t0.concat([l.script.slice(g,g+t),l.sequence])),g+=t}for(let r of f)await t2(t,e,!1,r,i,s,a);p++}}var t1=r(52235).Buffer;function t4(t,e,r=[]){if(!e)throw Error("getTrustedInputBIP143: missing tx");if(r.includes("decred"))throw Error("Decred does not implement BIP143");let n=t1.from((0,Y.sc)((0,Y.sc)(tT(e,!0)))),i=t1.alloc(4);i.writeUInt32LE(t,0);let{outputs:s,locktime:a}=e;if(!s||!a)throw Error("getTrustedInputBIP143: locktime & outputs is expected");if(!s[t])throw Error("getTrustedInputBIP143: wrong index");return(n=t1.concat([n,i,s[t].amount])).toString("hex")}var t5=r(52235).Buffer,t8=r(52235).Buffer;function t6(t,e,r,n,i,s=[]){let a=s.includes("decred"),o=E(e),l=t8.alloc(4);l.writeUInt32BE(r,0);let u=a?t8.concat([o,l,i||t8.from([0,0,0,0]),t8.from([n])]):t8.concat([o,t8.from([0]),l,t8.from([n])]);return i&&!a&&(u=t8.concat([u,i])),t.send(224,72,0,0,u).then(t=>t.length>0?(t[0]=48,t.slice(0,t.length-2)):t)}function t7(t,e){let r=E(e);return t.send(224,74,255,0,r)}async function t9(t,e,r=[]){let n=0,i=Number(128);
2if(r.includes("decred"))return t.send(224,74,i,0,e);for(;n<e.length;){let r=n+50>=e.length?e.length-n:50,i=128*(n+r===e.length),s=e.slice(n,n+r);await t.send(224,74,i,0,s),n+=r}}let et=async t=>{let e=await t.send(176,1,0,0),r=0,n=e[r++];tz()(1===n,"getAppAndVersion: format not supported");let i=e[r++],s=e.slice(r,r+=i).toString("ascii"),a=e[r++],o=e.slice(r,r+=a).toString("ascii"),l=e[r++];return{name:s,version:o,flags:e.slice(r,r+=l)}},ee=async t=>{try{await t.send(224,196,0,0)}catch{return!1}return!0};var er=r(52235).Buffer;let en={lockTime:0,sigHashType:1,segwit:!1,additionals:[],onDeviceStreaming:t=>{},onDeviceSignatureGranted:()=>{},onDeviceSignatureRequested:()=>{}},ei=t=>{let e=er.alloc(4);return!t||t>=z.NU6_1?e.writeUInt32LE(0x4dec4df0,0):t>=z.NU6?e.writeUInt32LE(0xc8e71055,0):t>=z.NU5?e.writeUInt32LE(0xc2d6d0b4,0):t>=z.CANOPY?e.writeUInt32LE(0xe9ff75a6,0):t>=z.HEARTWOOD?e.writeUInt32LE(0xf5b9230b,0):t>=z.BLOSSOM?e.writeUInt32LE(0x2bb40e60,0):t>=z.SAPLING?e.writeUInt32LE(0x76b809bb,0):e.writeUInt32LE(0x5ba81b19,0),e};async function es(t,e){let r={...en,...e},{inputs:n,associatedKeysets:i,blockHeight:s,changePath:a,outputScriptHex:o,lockTime:l,sigHashType:u,segwit:p,additionals:h,expiryHeight:f,onDeviceStreaming:g,onDeviceSignatureGranted:d,onDeviceSignatureRequested:I}=r,w=r.useTrustedInputForSegwit;if(void 0===w)try{let e=await et(t);w=function({version:t,name:e}){return"Decred"!==e&&("Exchange"===e||c().gte(t,"1.4.0"))}(e)}catch(t){if(27904===t.statusCode)w=!1;else throw t}let m=(t,e)=>{let{length:r}=n;if(r<3)return;let i=r*t+e,s=2*r;g({progress:i/s,total:s,index:i})},E=h.includes("decred"),S=h.includes("zcash"),y=h.includes("sapling"),b=p&&h.includes("bech32"),T=p||!!h&&(h.includes("abc")||h.includes("gold")||h.includes("bip143"))||!!f&&!E,v=er.alloc(4);v.writeUInt32LE(l,0);let x=er.alloc(0),P=er.alloc(0),{defaultVersion:_}=(({isZcash:t,sapling:e,isDecred:r,expiryHeight:n})=>{let i=er.alloc(4);return n&&!r?t?i.writeUInt32LE(0x80000005,0):e?i.writeUInt32LE(0x80000004,0):i.writeUInt32LE(0x80000003,0):i.writeUInt32LE(1,0),{defaultVersion:i}})({isZcash:S,sapling:y,isDecred:E,expiryHeight:f}),O=[],B=[],U=[],D=[],N=!0,M={inputs:[],version:_,timestamp:er.alloc(0)},R=er.from(o,"hex");for(let e of(m(0,0),n)){{S&&(e[0].consensusBranchId=ei(e[4]));let r=T&&!w?t4(e[1],e[0],h):await tQ(t,e[1],e[0],h);(0,tW.Rm)("hw","got trustedInput="+r);let n=er.alloc(4);n.writeUInt32LE(e.length>=4&&"number"==typeof e[3]?e[3]:0xffffffff,0),O.push({trustedInput:!0,value:er.from(r,"hex"),sequence:n})}let{outputs:r}=e[0],n=e[1];r&&n<=r.length-1&&B.push(r[n]),f&&!E?(M.nVersionGroupId=er.from(S?[10,39,167,38]:y?[133,32,47,137]:[112,130,196,3]),M.nExpiryHeight=f,M.extraData=er.from(y?[0,0,0,0,0,0,0,0,0,0,0]:[0])):E&&(M.nExpiryHeight=f)}M.inputs=n.map((t,e)=>{let r=er.alloc(4);return r.writeUInt32LE(t.length>=4&&"number"==typeof t[3]?t[3]:0xffffffff,0),{script:S?B[e].script:x,prevout:P,sequence:r}});{let e=[];for(let r=0;r<n.length;r++){let n=await tX(t,{path:i[r]});m(0,r+1),e.push(n)}for(let t=0;t<e.length;t++)D.push(function(t){let e=(1&t[64])!=0?3:2,r=t5.alloc(1);return r[0]=e,t5.concat([r,t.slice(1,33)])}(er.from(e[t].publicKey,"hex")))}I(),M.consensusBranchId=ei(s),T&&(await t3(t,!0,M,O,!0,!!f,h,w),a&&await t7(t,a),await t9(t,R)),f&&!E&&await t6(t,"",l,1,f);for(let e=0;e<n.length;e++){let r=n[e],s=n[e].length>=3&&"string"==typeof r[2]?er.from(r[2],"hex"):p?er.concat([er.from([118,169,20]),Z(D[e]),er.from([136,172])]):B[e].script,o=Object.assign({},M),c=T?[O[e]]:O;T?o.inputs=[{...o.inputs[e],script:s}]:o.inputs[e].script=s,await t3(t,!T&&N,o,c,T,!!f&&!E,h,w),T||(a&&await t7(t,a),await t9(t,R,h)),N&&(d(),m(1,0));let g=await t6(t,i[e],l,u,f,h);m(1,e+1),U.push(g),M.inputs[e].script=x,N&&(N=!1)}M.version=_,M.consensusBranchId=ei(s);for(let t=0;t<n.length;t++){if(p)M.witness=er.alloc(0),b||(M.inputs[t].script=er.concat([er.from("160014","hex"),Z(D[t])]));else{let e=er.alloc(1),r=er.alloc(1);e[0]=U[t].length,r[0]=D[t].length,M.inputs[t].script=er.concat([e,U[t],r,D[t]])}let e=T&&!w?0:4;M.inputs[t].prevout=O[t].value.slice(e,e+36)}M.locktime=v;let A=er.concat([tT(M,!1,M.timestamp,h),R]);if(p&&!E){let t=er.alloc(0);for(let e=0;e<n.length;e++){let r=er.concat([er.from("02","hex"),er.from([U[e].length]),U[e],er.from([D[e].length]),D[e]]);t=er.concat([t,r])}A=er.concat([A,t])}if(!S&&(A=er.concat([A,v]),f&&(A=er.concat([A,M.nExpiryHeight||er.alloc(0),M.extraData||er.alloc(0)]))),E){let t=er.from([M.inputs.length]);
2n.forEach((e,r)=>{t=er.concat([t,er.from([0,0,0,0,0,0,0,0]),er.from([0,0,0,0]),er.from([255,255,255,255]),er.from([M.inputs[r].script.length]),M.inputs[r].script])}),A=er.concat([A,t])}return S&&(A=er.concat([A,er.from([0,0,0])])),A.toString("hex")}var ea=r(52235).Buffer;async function eo(t,{path:e,messageHex:r}){let n=g().fromString(e).toPathArray(),i=ea.from(r,"hex"),s=0;for(;s!==i.length;){let e=0===s?49-4*n.length-4:50,r=s+e>i.length?i.length-s:e,a=ea.alloc(0===s?1+4*n.length+2+r:r);0===s?(a[0]=n.length,n.forEach((t,e)=>{a.writeUInt32BE(t,1+4*e)}),a.writeUInt16BE(i.length,1+4*n.length),i.copy(a,1+4*n.length+2,s,s+r)):i.copy(a,0,s,s+r),await t.send(224,78,0,0===s?1:128,a),s+=r}let a=await t.send(224,78,128,0,ea.from([0])),o=a[0]-48,l=a.slice(4,4+a[3]);0===l[0]&&(l=l.slice(1)),l=l.toString("hex"),s=4+a[3]+2;let u=a.slice(s,s+a[s-1]);return 0===u[0]&&(u=u.slice(1)),{v:o,r:l,s:u=u.toString("hex")}}var el=r(52235).Buffer;class eu{transport;constructor(t){this.transport=t}derivationsCache={};async derivatePath(t){if(this.derivationsCache[t])return this.derivationsCache[t];let e=await tX(this.transport,{path:t});return this.derivationsCache[t]=e,e}async getWalletXpub({path:t,xpubVersion:e}){var r;let n=y(t),i=n.slice(0,-1),s=await this.derivatePath(S(i)),a=await this.derivatePath(t),o=(r=eh(ep(el.from(s.publicKey,"hex"))),el.from((0,j.H)(r))).slice(0,4);return function(t,e,r,n,i,s){let a=ec(n);a[0]|=128;let o=el.concat([ec(t),el.from([e]),r,a,i,s]),l=eh(eh(o)).slice(0,4);return tq().encode(el.concat([o,l]))}(e,n.length,o,n[n.length-1],el.from(a.chainCode,"hex"),ep(el.from(a.publicKey,"hex")))}getWalletPublicKey(t,e){if(e?.format==="bech32m")throw Error("Unsupported address format bech32m");return tX(this.transport,{...e,path:t})}createPaymentTransaction(t){if(arguments.length>1)throw Error("@ledgerhq/hw-app-btc: createPaymentTransaction multi argument signature is deprecated. please switch to named parameters.");return es(this.transport,t)}async signMessage({path:t,messageHex:e}){return eo(this.transport,{path:t,messageHex:e})}}function ec(t){let e=el.allocUnsafe(4);return e.writeUInt32BE(t,0),e}let ep=t=>el.concat([el.from([2+(1&t[64])]),t.slice(1,33)]);function eh(t){return el.from((0,Y.sc)(t))}var ef=r(52235).Buffer;class eg{keys;keysTree;values;valuesTree;constructor(t,e){if(t.length!=e.length)throw Error("keys and values should have the same length");for(let e=0;e<t.length-1;e++)if(t[e].toString("hex")>=t[e+1].toString("hex"))throw Error("keys must be in strictly increasing order");this.keys=t,this.keysTree=new to(t.map(t=>tl(t))),this.values=e,this.valuesTree=new to(e.map(t=>tl(t)))}commitment(){return ef.concat([tS(this.keys.length),this.keysTree.getRoot(),this.valuesTree.getRoot()])}}var ed=r(52235).Buffer;class eI extends R{globalMerkleMap;inputMerkleMaps=[];outputMerkleMaps=[];inputMapCommitments;outputMapCommitments;constructor(t){super(),t.copy(this),this.globalMerkleMap=eI.createMerkleMap(this.globalMap);for(let t=0;t<this.getGlobalInputCount();t++)this.inputMerkleMaps.push(eI.createMerkleMap(this.inputMaps[t]));this.inputMapCommitments=[...this.inputMerkleMaps.values()].map(t=>t.commitment());for(let t=0;t<this.getGlobalOutputCount();t++)this.outputMerkleMaps.push(eI.createMerkleMap(this.outputMaps[t]));this.outputMapCommitments=[...this.outputMerkleMaps.values()].map(t=>t.commitment())}getGlobalSize(){return this.globalMap.size}getGlobalKeysValuesRoot(){return this.globalMerkleMap.commitment()}static createMerkleMap(t){let e=[...t.keys()].sort(),r=e.map(e=>{let r=t.get(e);if(!r)throw Error("No value for key "+e);return r});return new eg(e.map(t=>ed.from(t,"hex")),r)}}var ew=r(52235).Buffer;!function(t){t[t.YIELD=16]="YIELD",t[t.GET_PREIMAGE=64]="GET_PREIMAGE",t[t.GET_MERKLE_LEAF_PROOF=65]="GET_MERKLE_LEAF_PROOF",t[t.GET_MERKLE_LEAF_INDEX=66]="GET_MERKLE_LEAF_INDEX",t[t.GET_MORE_ELEMENTS=160]="GET_MORE_ELEMENTS"}(a||(a={}));class em{}class eE extends em{progressCallback;results;code=a.YIELD;constructor(t,e){super(),this.progressCallback=e,this.results=t}execute(t){return this.results.push(ew.from(t.subarray(1))),this.progressCallback(),ew.from("")}}class eS extends em{known_preimages;queue;code=a.GET_PREIMAGE;constructor(t,e){super(),this.known_preimages=t,this.queue=e}execute(t){let e=ew.from(t.subarray(1));if(33!=e.length)throw Error("Invalid request, unexpected trailing data");if(0!=e[0])throw Error("Unsupported request, the first byte should be 0");let r=ew.alloc(32);for(let t=0;t<32;t++)r[t]=e[1+t];let n=r.toString("hex"),i=this.known_preimages.get(n);if(void 0!=i){let t=tS(i.length),e=Math.min(255-t.length-1,i.length);if(e<i.length)for(let t=e;t<i.length;t++)this.queue.push(ew.from([i[t]]));return ew.concat([t,ew.from([e]),ew.from(i.subarray(0,e))])}throw Error(`Requested unknown preimage for: ${n}`)}}class ey extends em{known_trees;queue;code=a.GET_MERKLE_LEAF_PROOF;constructor(t,e){super(),this.known_trees=t,this.queue=e}execute(t){let e,r,n=ew.from(t.subarray(1));if(n.length<34)throw Error("Invalid request, expected at least 34 bytes");let i=new U(n),s=i.readSlice(32).toString("hex");try{e=i.readVarInt(),r=i.readVarInt()}catch{throw Error("Invalid request, couldn't parse tree_size or leaf_index")}let a=this.known_trees.get(s);if(!a)throw Error(`Requested Merkle leaf proof for unknown tree: ${s}`);if(r>=e||a.size()!=e)throw Error("Invalid index or tree size.");if(0!=this.queue.length)throw Error("This command should not execute when the queue is not empty.");let o=a.getProof(r),l=Math.min(Math.floor(6.90625),o.length),u=o.length-l;return u>0&&this.queue.push(...o.slice(-u)),ew.concat([a.getLeafHash(r),ew.from([o.length]),ew.from([l]),...o.slice(0,l)])}}class eb extends em{known_trees;code=a.GET_MERKLE_LEAF_INDEX;constructor(t){super(),this.known_trees=t}execute(t){let e=ew.from(t.subarray(1));if(64!=e.length)throw Error("Invalid request, unexpected trailing data");let r=ew.alloc(32);for(let t=0;t<32;t++)r[t]=e.readUInt8(t);let n=r.toString("hex"),i=ew.alloc(32);for(let t=0;t<32;t++)i[t]=e.readUInt8(32+t);let s=i.toString("hex"),a=this.known_trees.get(n);if(!a)throw Error(`Requested Merkle leaf index for unknown root: ${n}`);let o=0,l=0;for(let t=0;t<a.size();t++)if(a.getLeafHash(t).toString("hex")==s){l=1,o=t;break}return ew.concat([ew.from([l]),tS(o)])}}class eT extends em{queue;code=a.GET_MORE_ELEMENTS;constructor(t){super(),this.queue=t}execute(t){if(1!=t.length)throw Error("Invalid request, unexpected trailing data");if(0===this.queue.length)throw Error("No elements to get");let e=this.queue[0].length;if(this.queue.some(t=>t.length!=e))throw Error("The queue contains elements with different byte length, which is not expected");let r=Math.min(Math.floor(253/e),this.queue.length),n=this.queue.splice(0,r);return ew.concat([ew.from([r]),ew.from([e]),...n])}}class ev{roots=new Map;preimages=new Map;yielded=[];queue=[];commands=new Map;constructor(t){for(let e of[new eE(this.yielded,t),new eS(this.preimages,this.queue),new eb(this.roots),new ey(this.roots,this.queue),new eT(this.queue)]){if(this.commands.has(e.code))throw Error(`Multiple commands with code ${e.code}`);this.commands.set(e.code,e)}}getYielded(){return this.yielded}addKnownPreimage(t){this.preimages.set(p.Et.sha256(t).toString("hex"),t)}addKnownList(t){for(let e of t){let t=ew.concat([ew.from([0]),e]);this.addKnownPreimage(t)}let e=new to(t.map(t=>tl(t)));this.roots.set(e.getRoot().toString("hex"),e)}addKnownMapping(t){this.addKnownList(t.keys),this.addKnownList(t.values)}execute(t){if(0==t.length)throw Error("Unexpected empty command");let e=t[0],r=this.commands.get(e);if(!r)throw Error(`Unexpected command code ${e}`);return r.execute(t)}}
2var ex=r(52235).Buffer;!function(t){t[t.GET_PUBKEY=0]="GET_PUBKEY",t[t.REGISTER_WALLET=2]="REGISTER_WALLET",t[t.GET_WALLET_ADDRESS=3]="GET_WALLET_ADDRESS",t[t.SIGN_PSBT=4]="SIGN_PSBT",t[t.GET_MASTER_FINGERPRINT=5]="GET_MASTER_FINGERPRINT",t[t.SIGN_MESSAGE=16]="SIGN_MESSAGE"}(o||(o={})),function(t){t[t.CONTINUE_INTERRUPTED=1]="CONTINUE_INTERRUPTED"}(l||(l={}));class eP{transport;constructor(t){this.transport=t}async makeRequest(t,e,r){let n=await this.transport.send(225,t,0,0,e,[36864,57344]);for(;57344===n.readUInt16BE(n.length-2);){if(!r)throw Error("Unexpected SW_INTERRUPTED_EXECUTION");let t=n.slice(0,-2),e=r.execute(t);n=await this.transport.send(248,l.CONTINUE_INTERRUPTED,0,0,e,[36864,57344])}return n.slice(0,-2)}async getExtendedPubkey(t,e){if(e.length>6)throw Error("Path too long. At most 6 levels allowed.");return(await this.makeRequest(o.GET_PUBKEY,ex.concat([ex.from(t?[1]:[0]),m(e)]))).toString("ascii")}async getWalletAddress(t,e,r,n,i){if(0!==r&&1!==r)throw Error("Change can only be 0 or 1");if(n<0||!Number.isInteger(n))throw Error("Invalid address index");if(null!=e&&32!=e.length)throw Error("Invalid HMAC length");let s=new ev(()=>{});s.addKnownList(t.keys.map(t=>ex.from(t,"ascii"))),s.addKnownPreimage(t.serialize());let a=ex.alloc(4);return a.writeUInt32BE(n,0),(await this.makeRequest(o.GET_WALLET_ADDRESS,ex.concat([ex.from(i?[1]:[0]),t.getWalletId(),e||ex.alloc(32,0),ex.from([r]),a]),s)).toString("ascii")}async signPsbt(t,e,r,n){let i=new eI(t);if(null!=r&&32!=r.length)throw Error("Invalid HMAC length");let s=new ev(n);for(let t of(s.addKnownList(e.keys.map(t=>ex.from(t,"ascii"))),s.addKnownPreimage(e.serialize()),s.addKnownMapping(i.globalMerkleMap),i.inputMerkleMaps))s.addKnownMapping(t);for(let t of i.outputMerkleMaps)s.addKnownMapping(t);s.addKnownList(i.inputMapCommitments);let a=new to(i.inputMapCommitments.map(t=>tl(t))).getRoot();s.addKnownList(i.outputMapCommitments);let l=new to(i.outputMapCommitments.map(t=>tl(t))).getRoot();await this.makeRequest(o.SIGN_PSBT,ex.concat([i.getGlobalKeysValuesRoot(),tS(i.getGlobalInputCount()),a,tS(i.getGlobalOutputCount()),l,e.getWalletId(),r||ex.alloc(32,0)]),s);let u=s.getYielded(),c=new Map;for(let t of u)c.set(t[0],t.slice(1));return c}async getMasterFingerprint(){return this.makeRequest(o.GET_MASTER_FINGERPRINT,ex.from([]))}async signMessage(t,e){if(e.length>6)throw Error("Path too long. At most 6 levels allowed.");let r=new ev(()=>{}),n=Math.ceil(t.length/64),i=[];for(let e=0;e<n;e++)i.push(t.subarray(64*e,64*e+64));r.addKnownList(i);let s=new to(i.map(t=>tl(t))).getRoot();return(await this.makeRequest(o.SIGN_MESSAGE,ex.concat([m(e),tS(t.length),s]),r)).toString("base64")}}var e_=r(52235).Buffer,eO=r(52235).Buffer;let eB={lockTime:0,sigHashType:1,segwit:!1,transactionVersion:1};async function eU(t,e){let{inputs:r,associatedKeysets:n,outputScriptHex:i,lockTime:s,sigHashType:a,segwit:o,transactionVersion:l}={...eB,...e},u=eO.alloc(0),c=eO.alloc(0),p=eO.alloc(4);p.writeUInt32LE(l,0);let h=[],f=[],g=[],d=!0,I={inputs:[],version:p},w=eO.from(i,"hex");for(let e of r){{let r=o?t4(e[1],e[0]):await tQ(t,e[1],e[0]),n=eO.alloc(4);n.writeUInt32LE(e.length>=4&&"number"==typeof e[3]?e[3]:0xffffffff,0),h.push({trustedInput:!1,value:o?eO.from(r,"hex"):eO.from(r,"hex").slice(4,40),sequence:n})}let{outputs:r}=e[0],n=e[1];r&&n<=r.length-1&&f.push(r[n])}for(let t=0;t<r.length;t++){let e=eO.alloc(4);e.writeUInt32LE(r[t].length>=4&&"number"==typeof r[t][3]?r[t][3]:0xffffffff,0),I.inputs.push({script:u,prevout:c,sequence:e})}o&&(await t3(t,!0,I,h,!0),await t9(t,w));for(let e=0;e<r.length;e++){let i=r[e],l=r[e].length>=3&&"string"==typeof i[2]?eO.from(i[2],"hex"):f[e].script,c=Object.assign({},I),p=o?[h[e]]:h;o?c.inputs=[{...c.inputs[e],script:l}]:c.inputs[e].script=l,await t3(t,!o&&d,c,p,o),o||await t9(t,w);let m=await t6(t,n[e],s,a);g.push(o?m.toString("hex"):m.slice(0,m.length-1).toString("hex")),I.inputs[e].script=u,d&&(d=!1)}return g}class eD{_transport;_impl;constructor({transport:t,scrambleKey:e="BTC",currency:r="bitcoin"}){this._transport=t,this._transport.decorateAppAPIMethods(this,["getWalletXpub","getWalletPublicKey","signP2SHTransaction","signMessage","createPaymentTransaction","signPsbtBuffer","getTrustedInput"],e),this._impl=(()=>{switch(r){case"bitcoin":case"bitcoin_testnet":case"bitcoin_regtest":case"qtum":return new tk(new eP(this._transport));default:return new eu(this._transport)}})()}getWalletXpub(t){return this.changeImplIfNecessary().then(e=>e.getWalletXpub(t))}getWalletPublicKey(t,e){let r;return arguments.length>2||"boolean"==typeof e?(console.warn("btc.getWalletPublicKey deprecated signature used. Please switch to getWalletPublicKey(path, { format, verify })"),r={verify:!!e,format:arguments[2]?"p2sh":"legacy"}):r=e||{},this.changeImplIfNecessary().then(e=>e.getWalletPublicKey(t,r))}signMessage(t,e){return this.changeImplIfNecessary().then(r=>r.signMessage({path:t,messageHex:e}))}createPaymentTransaction(t){if(arguments.length>1)throw Error("@ledgerhq/hw-app-btc: createPaymentTransaction multi argument signature is deprecated. please switch to named parameters.");return this.changeImplIfNecessary().then(e=>e.createPaymentTransaction(t))}async signPsbtBuffer(t,e){if(this._impl instanceof eu)throw TypeError("signPsbtBuffer is not supported with the legacy Bitcoin app");return this._impl.signPsbtBuffer(t,e)}signP2SHTransaction(t){return eU(this._transport,t)}splitTransaction(t,e=!1,r=!1,n=[]){return function(t,e=!1,r=!1,n=[]){let i,s,a,o,l,u=[],c=[],p=!1,h=0,f=e_.alloc(0),g=e_.alloc(0),d=e_.alloc(0),I=e_.alloc(0),w=n.includes("decred"),m=n.includes("zencash"),E=n.includes("zcash"),S=e_.from(t,"hex"),y=S.slice(h,h+4),b=y.equals(e_.from([3,0,0,128]))||y.equals(e_.from([4,0,0,128]))||y.equals(e_.from([5,0,0,128])),T=E&&y.equals(e_.from([5,0,0,128]));h+=4,e&&0===S[h]&&0!==S[h+1]&&!m&&(h+=2,p=!0),b&&(d=S.slice(h,4+h),h+=4),T&&(s=S.slice(h+4,h+8),g=S.slice(h+8,h+12),h+=12);let v=tE(S,h),x=v[0];h+=v[1];for(let t=0;t<x;t++){let t=S.slice(h,h+36);h+=36;let e=e_.alloc(0),r=e_.alloc(0);w?(r=S.slice(h,h+1),h+=1):(v=tE(S,h),h+=v[1],e=S.slice(h,h+v[0]),h+=v[0]);let n=S.slice(h,h+4);h+=4,u.push({prevout:t,script:e,sequence:n,tree:r})}let P=(v=tE(S,h))[0];h+=v[1];for(let t=0;t<P;t++){let t=S.slice(h,h+8);h+=8,w&&(h+=2),v=tE(S,h),h+=v[1];let e=S.slice(h,h+v[0]);h+=v[0],c.push({amount:t,script:e})}if(r&&T&&({sapling:a,offset:h}=function(t,e){let r,n=tE(t,e),i=n[0];e+=n[1];let s=[];for(let r=0;r<i;r++){let r=t.slice(e,e+32);e+=32;let n=t.slice(e,e+32);e+=32;let i=t.slice(e,e+32);e+=32,s.push({cv:r,nullifier:n,rk:i})}let a=(n=tE(t,e))[0];e+=n[1];let o=[];for(let r=0;r<a;r++){let r=t.slice(e,e+32);e+=32;let n=t.slice(e,e+32);e+=32;let i=t.slice(e,e+32);e+=32;let s=t.slice(e,e+580);e+=580;let a=t.slice(e,e+80);e+=80,o.push({cv:r,cmu:n,ephemeralKey:i,encCiphertext:s,outCiphertext:a})}let l=e_.alloc(0);i+a>0&&(l=t.slice(e,e+8),e+=8);let u=e_.alloc(0);i>0&&(u=t.slice(e,e+32),e+=32);let c=e_.alloc(0),p=e_.alloc(0);i>0&&(c=t.slice(e,e+192*i),e+=192*i,p=t.slice(e,e+64*i),e+=64*i);let h=e_.alloc(0);a>0&&(h=t.slice(e,e+192*a),e+=192*a);let f=e_.alloc(0);return i+a>0&&(f=t.slice(e,e+64),e+=64),i+a>0&&(r={nSpendsSapling:i,vSpendsSapling:s,nOutputsSapling:a,vOutputSapling:o,valueBalanceSapling:l,anchorSapling:u,vSpendProofsSapling:c,vSpendAuthSigsSapling:p,vOutputProofsSapling:h,bindingSigSapling:f}),{sapling:r,offset:e}}(S,h),{orchard:o,offset:h}=function(t,e){let r,n=tE(t,e),i=n[0];if(e+=n[1],i>0){let s=[];for(let r=0;r<i;r++){let r=t.subarray(e,e+32);e+=32;let n=t.subarray(e,e+32);e+=32;let i=t.subarray(e,e+32);e+=32;let a=t.subarray(e,e+32);e+=32;let o=t.subarray(e,e+32);e+=32;let l=t.subarray(e,e+580);e+=580;let u=t.subarray(e,e+80);e+=80;let c={cv:r,nullifier:n,rk:i,cmx:a,ephemeralKey:o,encCiphertext:l,outCiphertext:u};s.push(c)}let a=t.subarray(e,e+1);e+=1;let o=t.subarray(e,e+8);e+=8;let l=t.subarray(e,e+32);e+=32,n=tE(t,e);let u=t.subarray(e,e+n[1]);e+=n[1];let c=t.subarray(e,e+n[0]);e+=n[0];let p=t.subarray(e,e+64*i);e+=64*i;let h=t.subarray(e,e+64);e+=64,r={vActions:s,flags:a,valueBalance:o,anchor:l,sizeProofs:u,proofs:c,vSpendsAuthSigs:p,bindingSig:h}}return{orchard:r,offset:e}}(S,h),I=S.subarray(h)),p?(i=S.slice(h,-4),s=S.slice(S.length-4)):T||(s=S.slice(h,h+4)),h+=4,(b||w)&&!T&&(g=S.slice(h,h+4),h+=4),r&&!T&&(I=S.slice(h)),w){if(v=tE(S,h),h+=v[1],v[0]!==x)throw Error("splitTransaction: incoherent number of witnesses");for(let t=0;t<x;t++){h+=8,h+=4,h+=4,v=tE(S,h),h+=v[1];let e=S.slice(h,h+v[0]);h+=v[0],u[t].script=e}}let _={version:y,inputs:u,outputs:c,locktime:s,witness:i,timestamp:f,nVersionGroupId:d,nExpiryHeight:g,extraData:I,sapling:a,orchard:o};return(0,tW.Rm)("btc",`splitTransaction ${t}: 3${(l="TX version "+_.version.toString("hex"),_.locktime&&(l+=" locktime "+_.locktime.toString("hex")),_.
3witness&&(l+=" witness "+_.witness.toString("hex")),_.timestamp&&(l+=" timestamp "+_.timestamp.toString("hex")),_.nVersionGroupId&&(l+=" nVersionGroupId "+_.nVersionGroupId.toString("hex")),_.nExpiryHeight&&(l+=" nExpiryHeight "+_.nExpiryHeight.toString("hex")),_.extraData&&(l+=" extraData "+_.extraData.toString("hex")),_.inputs.forEach(({prevout:t,script:e,sequence:r},n)=>{l+=` 4input ${n}: prevout ${t.toString("hex")} script ${e.toString("hex")} sequence ${r.toString("hex")}`}),(_.outputs||[]).forEach(({amount:t,script:e},r)=>{l+=` 5output ${r}: amount ${t.toString("hex")} script ${e.toString("hex")}`}),l)}`),_}(t,e,r,n)}serializeTransactionOutputs(t){return tb(t)}getTrustedInput(t,e,r=[]){return tQ(this._transport,t,e,r)}getTrustedInputBIP143(t,e,r=[]){return t4(t,e,r)}async changeImplIfNecessary(){if(this._impl instanceof eu)return this._impl;let{name:t,version:e}=await et(this._transport);return await (async()=>{switch(t){case"Bitcoin":case"Bitcoin Test":return c().lt(e,"2.1.0");case"Bitcoin Recovery":return!1;case"Bitcoin Legacy":case"Bitcoin Test Legacy":default:return!0;case"Exchange":return await ee(this._transport);case"Qtum":return c().lt(e,"3.0.0")}})()&&(this._impl=new eu(this._transport)),this._impl}}},66486:(t,e,r)=>{"use strict";var n=r(6111),i=r(35834).Buffer;t.exports=function(t){function e(e){var r=e.slice(0,-4),n=e.slice(-4),i=t(r);if(!(n[0]^i[0]|n[1]^i[1]|n[2]^i[2]|n[3]^i[3]))return r}return{encode:function(e){var r=t(e);return n.encode(i.concat([e,r],e.length+4))},decode:function(r){var i=e(n.decode(r),t);if(!i)throw Error("Invalid checksum");return i},decodeUnsafe:function(t){var r=n.decodeUnsafe(t);if(r)return e(r)}}}},74609:(t,e,r)=>{"use strict";var n=r(81725);t.exports=r(66486)(function(t){var e=n("sha256").update(t).digest();return n("sha256").update(e).digest()})},81725:(t,e,r)=>{"use strict";var n=r(27069),i=r(96905),s=r(21177),a=r(71553),o=r(65060);function l(t){o.call(this,"digest"),this._hash=t}n(l,o),l.prototype._update=function(t){this._hash.update(t)},l.prototype._final=function(){return this._hash.digest()},t.exports=function(t){return"md5"===(t=t.toLowerCase())?new i:"rmd160"===t||"ripemd160"===t?new s:new l(a(t))}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.