@secux/app-btc
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -1,1 +0,1 @@ | ||
"use strict";var t,i,e,s,r,n,o,a,h,p,u,c,f,l=this&&this.__awaiter||function(t,i,e,s){return new(e||(e=Promise))((function(r,n){function o(t){try{h(s.next(t))}catch(t){n(t)}}function a(t){try{h(s.throw(t))}catch(t){n(t)}}function h(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(o,a)}h((s=s.apply(t,i||[])).next())}))},d=this&&this.__classPrivateFieldSet||function(t,i,e,s,r){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof i?t!==i||!r:!i.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?r.call(t,e):r?r.value=e:i.set(t,e),e},S=this&&this.__classPrivateFieldGet||function(t,i,e,s){if("a"===e&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof i?t!==i||!s:!i.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===e?s:"a"===e?s.call(t):s?s.value:i.get(t)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxPsbt=void 0;const g=require("bip174"),y=require("bip174/src/lib/utils"),w=require("secp256k1/elliptic"),P=require("js-sha256"),v=require("./script"),m=require("@secux/utility"),b=require("./interface"),k=require("./utils"),T=require("./payment"),H=require("./transaction"),x=require("./coindef"),W=null===m.Logger||void 0===m.Logger?void 0:m.Logger.child({id:"psbt"});exports.SecuxPsbt=class{constructor(c,f){t.add(this),i.set(this,void 0),e.set(this,void 0),s.set(this,void 0),r.set(this,void 0),n.set(this,{}),o.set(this,[]),a.set(this,void 0),h.set(this,Buffer.alloc(0)),p.set(this,[]),u.set(this,{address:"",value:""}),d(this,i,c,"f"),d(this,e,new g.Psbt(new E),"f"),d(this,s,f,"f"),d(this,r,(0,k.getPayment)(S(this,s,"f")),"f"),d(this,a,{__TX:S(this,e,"f").globalMap.unsignedTx.tx},"f"),f===b.CoinType.BITCOINCASH&&(S(this,a,"f").__TX.version=1)}AddInput(i){var n;return l(this,void 0,void 0,(function*(){const a=yield S(this,t,"m",c).call(this,i.path),h={},u={},f=null!==(n=i.script)&&void 0!==n?n:(0,k.getDefaultScript)(i.path);switch(f){case b.ScriptType.P2PKH:h.witnessUtxo={script:S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:a}).scriptPublickey,value:i.satoshis};break;case b.ScriptType.P2SH_P2PKH:const t=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:a});u.redeemScript=t.scriptPublickey,h.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),t.redeemHash).scriptPublickey,value:i.satoshis};break;case b.ScriptType.P2SH_P2WPKH:const n=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:a});u.redeemScript=n.scriptPublickey,h.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),n.redeemHash).scriptPublickey,value:i.satoshis};break;case b.ScriptType.P2WPKH:h.witnessUtxo={script:S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:a}).scriptPublickey,value:i.satoshis};break;default:throw Error(`ArgumentError: Invalid ScriptType of input#${S(this,e,"f").inputs.length}, got "${b.ScriptType[f]}"`)}if(i.txHex){const t=H.Transaction.fromBuffer(Buffer.from(i.txHex,"hex"));if((0,k.getSerializer)(S(this,s,"f")).getId(t)!==i.hash)throw Error(`UTXO hash for input #${S(this,e,"f").inputs.length} doesn't match the hash specified in the prevout`);const r=t.outs[i.vout];if(r.value!==i.satoshis)throw Error(`UTXO value for input #${S(this,e,"f").inputs.length} doesn't match the value specified in the prevout`);r.script.equals(h.witnessUtxo.script)||null==W||W.warn(`Input script generation error: ${r.script.toString("hex")}, got "${h.witnessUtxo.script}"`)}const l=Object.assign(Object.assign({hash:i.hash,index:i.vout},h),u);return S(this,e,"f").addInput(l),S(this,p,"f").push(f),S(this,o,"f").push(i.path),this}))}AddInputs(t){return l(this,void 0,void 0,(function*(){for(const i of t)yield this.AddInput(i);return this}))}AddOutput(i){var n;return l(this,void 0,void 0,(function*(){const o=(0,m.BigIntToBuffer)(i.satoshis,8);let a,p,f,l,g=i.satoshis;if(a=(0,b.isOutuptScriptExtended)(i)){const i=yield S(this,t,"m",c).call(this,a.path);l=a.path;const o=null!==(n=a.script)&&void 0!==n?n:(0,k.getDefaultScript)(l);switch(o){case b.ScriptType.P2SH_P2WPKH:if(!a.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");let t=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:i}).redeemHash,n=S(this,r,"f").p2sh(S(this,s,"f"),t);p=n.scriptPublickey,f=n.address;break;case b.ScriptType.P2SH_P2PKH:if(!a.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");t=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i}).redeemHash,n=S(this,r,"f").p2sh(S(this,s,"f"),t),p=n.scriptPublickey,f=n.address;break;case b.ScriptType.P2PKH:if(!a.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");const h=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i});p=h.scriptPublickey,f=h.address;break;case b.ScriptType.P2WPKH:if(!a.path.startsWith("m/84'/"))throw Error("P2WPKH should use m/84' path");const u=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:i});p=u.scriptPublickey,f=u.address;break;default:throw Error(`ArgumentError: Invalid ScriptType of output#${S(this,e,"f").outputs.length}, got "${b.ScriptType[o]}"`)}}else if(a=(0,b.isOutputAddress)(i))p=S(this,r,"f").decode(S(this,s,"f"),a.address),f=a.address;else{if(!(a=(0,b.isOutuptScript)(i)))throw Error("Invalid parameter of output");p=Buffer.from(a.scriptHex,"hex")}return 0===S(this,e,"f").outputs.length?(d(this,h,Buffer.concat([S(this,h,"f"),o,Buffer.from([p.length]),p]),"f"),S(this,u,"f").address=f,S(this,u,"f").value=(1e-8*g).toString()):d(this,h,Buffer.concat([S(this,h,"f"),o,(0,m.buildPathBuffer)(l).pathBuffer]),"f"),S(this,e,"f").addOutput({script:p,value:g}),this}))}AddOutputs(t){return l(this,void 0,void 0,(function*(){for(const i of t)yield this.AddOutput(i);return this}))}Sign(){return l(this,void 0,void 0,(function*(){d(this,h,Buffer.concat([Buffer.from([S(this,e,"f").outputs.length]),S(this,h,"f")]),"f");const r=S(this,e,"f").inputs.map(((t,i)=>{const r=S(this,p,"f")[i],{data:n}=function(t,i,e,s,r){const n=r.__TX,o=e.sighashType||H.Transaction.SIGHASH_ALL,a=(0,k.getSerializer)(t);let h;if(!e.witnessUtxo)throw new Error("Need a Utxo input item for signing");h=e.witnessUtxo;const p=function(t,i,e,s){let r;switch(i){case b.ScriptType.P2SH_P2PKH:case b.ScriptType.P2SH_P2WPKH:if(!e)throw Error("scriptPubkey is P2SH but redeemScript missing");r=e;break;default:r=t}if(!r)throw Error("cannot extract script");return r}(h.script,s,e.redeemScript,e.witnessScript);let u;switch(null==W||W.debug(`input #${i} script type: ${b.ScriptType[s]}`),null==W||W.debug(`script: ${p.toString("hex")}`),s){case b.ScriptType.P2WPKH:case b.ScriptType.P2SH_P2WPKH:case b.ScriptType.P2SH_P2PKH:null==W||W.debug(b.ScriptType[s]);const e=(0,k.getPayment)(t).p2pkh(t,{hash:p.slice(2)}).scriptPublickey;u=a.dataForWitnessV0(n,i,e,h.value,o);break;default:t===b.CoinType.BITCOINCASH?(null==W||W.debug("bch using bip143"),u=a.dataForWitnessV0(n,i,p,h.value,64|o)):(null==W||W.debug("non-segwit"),u=a.dataForSignature(n,i,p,o))}return{script:p,sighashType:o,data:u}}(S(this,s,"f"),i,S(this,e,"f").inputs[i],r,S(this,a,"f"));return S(this,s,"f")!==b.CoinType.BITCOINCASH&&r===b.ScriptType.P2PKH?{data:n,confirm:Buffer.alloc(0)}:{data:n,confirm:S(this,h,"f")}}));for(let t=0;t<r.length;t++)null==W||W.debug(`tx data [${t}]: ${r[t].data.toString("hex")}`),null==W||W.debug(`confirm data [${t}]: ${r[t].confirm.toString("hex")}`);const n=r.map((t=>t.data));let f;if(S(this,e,"f").inputs.length+S(this,e,"f").outputs.length<12)f=yield S(this,i,"f").SignRawTransactionGroup(S(this,o,"f"),n,r[0].confirm);else{yield S(this,i,"f").txEnd();for(let t=0;t<S(this,o,"f").length;t++)yield S(this,i,"f").txPrepare(S(this,o,"f")[t],t,r[t].data,r[t].confirm);yield S(this,i,"f").txBegin(S(this,u,"f").value,S(this,u,"f").address),f=[];for(let t=0;t<S(this,o,"f").length;t++){const e=yield S(this,i,"f").txSign(t);f.push(e)}yield S(this,i,"f").txEnd()}let l=[];for(let i=0;i<S(this,e,"f").inputs.length;i++){const n=f[i];n.trimZero();const a=Buffer.concat([n.r,n.s]),h=S(this,s,"f")===b.CoinType.GROESTL?Buffer.from(P.sha256.update(r[i].data).digest()):Buffer.from(P.sha256.update(P.sha256.update(r[i].data).digest()).digest()),p=yield S(this,t,"m",c).call(this,S(this,o,"f")[i]);if(!w.ecdsaVerify(a,h,p))throw Error(`Signature Error #${i}`);let u=S(this,e,"f").inputs[i].sighashType||H.Transaction.SIGHASH_ALL;S(this,s,"f")===b.CoinType.BITCOINCASH&&(u|=64);const d=[{pubkey:p,signature:v.encode(a,u)}];S(this,e,"f").updateInput(i,{partialSig:d}),l.push(d[0].signature)}return l}))}finalizeAllInputs(){if(S(this,e,"f").inputs.length<1)throw Error("utxo input cannot be empty");return S(this,e,"f").inputs.forEach(((t,i)=>{(0,y.checkForInput)(S(this,e,"f").inputs,i);const s=function(t){var i;let e;e=t.witnessScript?t.witnessScript:t.redeemScript?t.redeemScript:null===(i=t.witnessUtxo)||void 0===i?void 0:i.script;return e}(t);if(!s)throw new Error(`No script found for input #${i}`);if(t.sighashType&&t.partialSig){const{partialSig:i,sighashType:e}=t;for(const t of i){const{hashType:i}=v.decode(t.signature);if(i!==e)throw new Error("Signature sighash does not match input sighash type")}}const{finalScriptSig:r,finalScriptWitness:n}=function(t,i){let e,s;const{signature:r,pubkey:n}=i[0];switch(t){case b.ScriptType.P2PKH:e=v.compile([r,n]);break;case b.ScriptType.P2SH_P2PKH:e=(()=>{const t=v.compile([r,n]),i=v.decompile(t),e={output:i[i.length-1],input:v.compile(i.slice(0,-1))};return v.compile([].concat(v.decompile(e.input),e.output))})();break;case b.ScriptType.P2SH_P2WPKH:s=(0,k.witnessStackToScriptWitness)([r,n]),e=(()=>{const t=Buffer.alloc(0),i=(0,T.Hash160)(n),e=v.compile([x.OPCODES.OP_0,i]);return v.compile([].concat(v.decompile(t),e))})();break;case b.ScriptType.P2WPKH:s=(0,k.witnessStackToScriptWitness)([r,n])}return{finalScriptSig:e,finalScriptWitness:s}}(S(this,p,"f")[i],t.partialSig);if(r&&S(this,e,"f").updateInput(i,{finalScriptSig:r}),n&&S(this,e,"f").updateInput(i,{finalScriptWitness:n}),!r&&!n)throw new Error(`Unknown error finalizing input #${i}`)})),this}extractTransaction(){if(!S(this,e,"f").inputs.every((t=>!!t.finalScriptSig||!!t.finalScriptWitness)))throw new Error("Not finalized");const t=S(this,a,"f").__TX.clone();return S(this,e,"f").inputs.forEach(((i,e)=>{i.finalScriptSig&&(t.ins[e].script=i.finalScriptSig),i.finalScriptWitness&&(t.ins[e].witness=(0,k.scriptWitnessToWitnessStack)(i.finalScriptWitness))})),t}},i=new WeakMap,e=new WeakMap,s=new WeakMap,r=new WeakMap,n=new WeakMap,o=new WeakMap,a=new WeakMap,h=new WeakMap,p=new WeakMap,u=new WeakMap,t=new WeakSet,c=function(t){var e;return l(this,void 0,void 0,(function*(){let s=null===(e=S(this,n,"f")[t])||void 0===e?void 0:e.publicKey;if(!s){const e=yield S(this,i,"f").getXPublickey(t,!0);s=e.publicKey,S(this,n,"f")[t]=e}return s}))};class E{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){f.set(this,void 0),d(this,f,H.Transaction.fromBuffer(t),"f")}getInputOutputCounts(){return{inputCount:S(this,f,"f").ins.length,outputCount:S(this,f,"f").outs.length}}addInput(t){if(void 0===t.hash||void 0===t.index||!Buffer.isBuffer(t.hash)&&"string"!=typeof t.hash||"number"!=typeof t.index)throw new Error("Error adding input.");const i="string"==typeof t.hash?Buffer.from(Buffer.from(t.hash,"hex").reverse()):t.hash;S(this,f,"f").addInput(i,t.index,t.sequence)}addOutput(t){if(void 0===t.script||void 0===t.value||!Buffer.isBuffer(t.script)||"number"!=typeof t.value)throw new Error("Error adding output.");S(this,f,"f").addOutput(t.script,t.value)}toBuffer(){return S(this,f,"f").toBuffer()}get tx(){return S(this,f,"f")}}f=new WeakMap; | ||
"use strict";var t,i,e,s,r,n,o,a,h,p,u,c,f,l=this&&this.__awaiter||function(t,i,e,s){return new(e||(e=Promise))((function(r,n){function o(t){try{h(s.next(t))}catch(t){n(t)}}function a(t){try{h(s.throw(t))}catch(t){n(t)}}function h(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(o,a)}h((s=s.apply(t,i||[])).next())}))},d=this&&this.__classPrivateFieldSet||function(t,i,e,s,r){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof i?t!==i||!r:!i.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?r.call(t,e):r?r.value=e:i.set(t,e),e},S=this&&this.__classPrivateFieldGet||function(t,i,e,s){if("a"===e&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof i?t!==i||!s:!i.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===e?s:"a"===e?s.call(t):s?s.value:i.get(t)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxPsbt=void 0;const g=require("bip174"),y=require("bip174/src/lib/utils"),w=require("secp256k1/elliptic"),P=require("js-sha256"),v=require("./script"),m=require("@secux/utility"),b=require("./interface"),k=require("./utils"),T=require("./payment"),H=require("./transaction"),x=require("./coindef"),W=null===m.Logger||void 0===m.Logger?void 0:m.Logger.child({id:"psbt"});exports.SecuxPsbt=class{constructor(c,f){t.add(this),i.set(this,void 0),e.set(this,void 0),s.set(this,void 0),r.set(this,void 0),n.set(this,{}),o.set(this,[]),a.set(this,void 0),h.set(this,Buffer.alloc(0)),p.set(this,[]),u.set(this,{address:"",value:""}),d(this,i,c,"f"),d(this,e,new g.Psbt(new E),"f"),d(this,s,f,"f"),d(this,r,(0,k.getPayment)(S(this,s,"f")),"f"),d(this,a,{__TX:S(this,e,"f").globalMap.unsignedTx.tx},"f"),f===b.CoinType.BITCOINCASH&&(S(this,a,"f").__TX.version=1)}AddInput(i){var n;return l(this,void 0,void 0,(function*(){const a=yield S(this,t,"m",c).call(this,i.path),h={},u={},f=null!==(n=i.script)&&void 0!==n?n:(0,k.getDefaultScript)(i.path);switch(f){case b.ScriptType.P2PKH:h.witnessUtxo={script:S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:a}).scriptPublickey,value:i.satoshis};break;case b.ScriptType.P2SH_P2PKH:const t=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:a});u.redeemScript=t.scriptPublickey,h.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),t.redeemHash).scriptPublickey,value:i.satoshis};break;case b.ScriptType.P2SH_P2WPKH:const n=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:a});u.redeemScript=n.scriptPublickey,h.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),n.redeemHash).scriptPublickey,value:i.satoshis};break;case b.ScriptType.P2WPKH:h.witnessUtxo={script:S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:a}).scriptPublickey,value:i.satoshis};break;default:throw Error(`ArgumentError: Invalid ScriptType of input#${S(this,e,"f").inputs.length}, got "${b.ScriptType[f]}"`)}if(i.txHex){const t=H.Transaction.fromBuffer(Buffer.from(i.txHex,"hex"));if((0,k.getSerializer)(S(this,s,"f")).getId(t)!==i.hash)throw Error(`UTXO hash for input #${S(this,e,"f").inputs.length} doesn't match the hash specified in the prevout`);const r=t.outs[i.vout];if(r.value!==i.satoshis)throw Error(`UTXO value for input #${S(this,e,"f").inputs.length} doesn't match the value specified in the prevout`);r.script.equals(h.witnessUtxo.script)||null==W||W.warn(`Input script generation error: ${r.script.toString("hex")}, got "${h.witnessUtxo.script}"`)}const l=Object.assign(Object.assign({hash:i.hash,index:i.vout},h),u);return S(this,e,"f").addInput(l),S(this,p,"f").push(f),S(this,o,"f").push(i.path),this}))}AddInputs(t){return l(this,void 0,void 0,(function*(){for(const i of t)yield this.AddInput(i);return this}))}AddOutput(i){var n;return l(this,void 0,void 0,(function*(){const o=(0,m.BigIntToBuffer)(i.satoshis,8);let a,p,f,l,g=i.satoshis;if(a=(0,b.isOutuptScriptExtended)(i)){const i=yield S(this,t,"m",c).call(this,a.path);l=a.path;const o=null!==(n=a.script)&&void 0!==n?n:(0,k.getDefaultScript)(l);switch(o){case b.ScriptType.P2SH_P2WPKH:if(!a.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");let t=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:i}).redeemHash,n=S(this,r,"f").p2sh(S(this,s,"f"),t);p=n.scriptPublickey,f=n.address;break;case b.ScriptType.P2SH_P2PKH:if(!a.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");t=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i}).redeemHash,n=S(this,r,"f").p2sh(S(this,s,"f"),t),p=n.scriptPublickey,f=n.address;break;case b.ScriptType.P2PKH:if(!a.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");const h=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i});p=h.scriptPublickey,f=h.address;break;case b.ScriptType.P2WPKH:if(!a.path.startsWith("m/84'/"))throw Error("P2WPKH should use m/84' path");const u=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:i});p=u.scriptPublickey,f=u.address;break;default:throw Error(`ArgumentError: Invalid ScriptType of output#${S(this,e,"f").outputs.length}, got "${b.ScriptType[o]}"`)}}else if(a=(0,b.isOutputAddress)(i))p=S(this,r,"f").decode(S(this,s,"f"),a.address),f=a.address;else{if(!(a=(0,b.isOutuptScript)(i)))throw Error("Invalid parameter of output");p=Buffer.from(a.scriptHex,"hex")}return 0===S(this,e,"f").outputs.length?(d(this,h,Buffer.concat([S(this,h,"f"),o,Buffer.from([p.length]),p]),"f"),S(this,u,"f").address=f,S(this,u,"f").value=(1e-8*g).toString()):d(this,h,Buffer.concat([S(this,h,"f"),o,(0,m.buildPathBuffer)(l).pathBuffer]),"f"),S(this,e,"f").addOutput({script:p,value:g}),this}))}AddOutputs(t){return l(this,void 0,void 0,(function*(){for(const i of t)yield this.AddOutput(i);return this}))}Sign(){return l(this,void 0,void 0,(function*(){d(this,h,Buffer.concat([Buffer.from([S(this,e,"f").outputs.length]),S(this,h,"f")]),"f");const r=S(this,e,"f").inputs.map(((t,i)=>{const r=S(this,p,"f")[i],{data:n}=function(t,i,e,s,r){const n=r.__TX,o=e.sighashType||H.Transaction.SIGHASH_ALL,a=(0,k.getSerializer)(t);let h;if(!e.witnessUtxo)throw new Error("Need a Utxo input item for signing");h=e.witnessUtxo;const p=function(t,i,e,s){let r;switch(i){case b.ScriptType.P2SH_P2PKH:case b.ScriptType.P2SH_P2WPKH:if(!e)throw Error("scriptPubkey is P2SH but redeemScript missing");r=e;break;default:r=t}if(!r)throw Error("cannot extract script");return r}(h.script,s,e.redeemScript,e.witnessScript);let u;switch(null==W||W.debug(`input #${i} script type: ${b.ScriptType[s]}`),null==W||W.debug(`script: ${p.toString("hex")}`),s){case b.ScriptType.P2WPKH:case b.ScriptType.P2SH_P2WPKH:case b.ScriptType.P2SH_P2PKH:null==W||W.debug(b.ScriptType[s]);const e=(0,k.getPayment)(t).p2pkh(t,{hash:p.slice(2)}).scriptPublickey;u=a.dataForWitnessV0(n,i,e,h.value,o);break;default:t===b.CoinType.BITCOINCASH?(null==W||W.debug("bch using bip143"),u=a.dataForWitnessV0(n,i,p,h.value,64|o)):(null==W||W.debug("non-segwit"),u=a.dataForSignature(n,i,p,o))}return{script:p,sighashType:o,data:u}}(S(this,s,"f"),i,S(this,e,"f").inputs[i],r,S(this,a,"f"));return S(this,s,"f")!==b.CoinType.BITCOINCASH&&r===b.ScriptType.P2PKH?{data:n,confirm:Buffer.alloc(0)}:{data:n,confirm:S(this,h,"f")}}));for(let t=0;t<r.length;t++)null==W||W.debug(`tx data [${t}]: ${r[t].data.toString("hex")}`),null==W||W.debug(`confirm data [${t}]: ${r[t].confirm.toString("hex")}`);const n=r.map((t=>t.data));let f;if(n.reduce(((t,i)=>t+i.length),0)<3500)f=yield S(this,i,"f").SignRawTransactionGroup(S(this,o,"f"),n,r[0].confirm);else{yield S(this,i,"f").txEnd();for(let t=0;t<S(this,o,"f").length;t++)yield S(this,i,"f").txPrepare(S(this,o,"f")[t],t,r[t].data,r[t].confirm);yield S(this,i,"f").txBegin(S(this,u,"f").value,S(this,u,"f").address),f=[];for(let t=0;t<S(this,o,"f").length;t++){const e=yield S(this,i,"f").txSign(t);f.push(e)}yield S(this,i,"f").txEnd()}let l=[];for(let i=0;i<S(this,e,"f").inputs.length;i++){const n=f[i];n.trimZero();const a=Buffer.concat([n.r,n.s]),h=S(this,s,"f")===b.CoinType.GROESTL?Buffer.from(P.sha256.update(r[i].data).digest()):Buffer.from(P.sha256.update(P.sha256.update(r[i].data).digest()).digest()),p=yield S(this,t,"m",c).call(this,S(this,o,"f")[i]);if(!w.ecdsaVerify(a,h,p))throw Error(`Signature Error #${i}`);let u=S(this,e,"f").inputs[i].sighashType||H.Transaction.SIGHASH_ALL;S(this,s,"f")===b.CoinType.BITCOINCASH&&(u|=64);const d=[{pubkey:p,signature:v.encode(a,u)}];S(this,e,"f").updateInput(i,{partialSig:d}),l.push(d[0].signature)}return l}))}finalizeAllInputs(){if(S(this,e,"f").inputs.length<1)throw Error("utxo input cannot be empty");return S(this,e,"f").inputs.forEach(((t,i)=>{(0,y.checkForInput)(S(this,e,"f").inputs,i);const s=function(t){var i;let e;e=t.witnessScript?t.witnessScript:t.redeemScript?t.redeemScript:null===(i=t.witnessUtxo)||void 0===i?void 0:i.script;return e}(t);if(!s)throw new Error(`No script found for input #${i}`);if(t.sighashType&&t.partialSig){const{partialSig:i,sighashType:e}=t;for(const t of i){const{hashType:i}=v.decode(t.signature);if(i!==e)throw new Error("Signature sighash does not match input sighash type")}}const{finalScriptSig:r,finalScriptWitness:n}=function(t,i){let e,s;const{signature:r,pubkey:n}=i[0];switch(t){case b.ScriptType.P2PKH:e=v.compile([r,n]);break;case b.ScriptType.P2SH_P2PKH:e=(()=>{const t=v.compile([r,n]),i=v.decompile(t),e={output:i[i.length-1],input:v.compile(i.slice(0,-1))};return v.compile([].concat(v.decompile(e.input),e.output))})();break;case b.ScriptType.P2SH_P2WPKH:s=(0,k.witnessStackToScriptWitness)([r,n]),e=(()=>{const t=Buffer.alloc(0),i=(0,T.Hash160)(n),e=v.compile([x.OPCODES.OP_0,i]);return v.compile([].concat(v.decompile(t),e))})();break;case b.ScriptType.P2WPKH:s=(0,k.witnessStackToScriptWitness)([r,n])}return{finalScriptSig:e,finalScriptWitness:s}}(S(this,p,"f")[i],t.partialSig);if(r&&S(this,e,"f").updateInput(i,{finalScriptSig:r}),n&&S(this,e,"f").updateInput(i,{finalScriptWitness:n}),!r&&!n)throw new Error(`Unknown error finalizing input #${i}`)})),this}extractTransaction(){if(!S(this,e,"f").inputs.every((t=>!!t.finalScriptSig||!!t.finalScriptWitness)))throw new Error("Not finalized");const t=S(this,a,"f").__TX.clone();return S(this,e,"f").inputs.forEach(((i,e)=>{i.finalScriptSig&&(t.ins[e].script=i.finalScriptSig),i.finalScriptWitness&&(t.ins[e].witness=(0,k.scriptWitnessToWitnessStack)(i.finalScriptWitness))})),t}},i=new WeakMap,e=new WeakMap,s=new WeakMap,r=new WeakMap,n=new WeakMap,o=new WeakMap,a=new WeakMap,h=new WeakMap,p=new WeakMap,u=new WeakMap,t=new WeakSet,c=function(t){var e;return l(this,void 0,void 0,(function*(){let s=null===(e=S(this,n,"f")[t])||void 0===e?void 0:e.publicKey;if(!s){const e=yield S(this,i,"f").getXPublickey(t,!0);s=e.publicKey,S(this,n,"f")[t]=e}return s}))};class E{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){f.set(this,void 0),d(this,f,H.Transaction.fromBuffer(t),"f")}getInputOutputCounts(){return{inputCount:S(this,f,"f").ins.length,outputCount:S(this,f,"f").outs.length}}addInput(t){if(void 0===t.hash||void 0===t.index||!Buffer.isBuffer(t.hash)&&"string"!=typeof t.hash||"number"!=typeof t.index)throw new Error("Error adding input.");const i="string"==typeof t.hash?Buffer.from(Buffer.from(t.hash,"hex").reverse()):t.hash;S(this,f,"f").addInput(i,t.index,t.sequence)}addOutput(t){if(void 0===t.script||void 0===t.value||!Buffer.isBuffer(t.script)||"number"!=typeof t.value)throw new Error("Error adding output.");S(this,f,"f").addOutput(t.script,t.value)}toBuffer(){return S(this,f,"f").toBuffer()}get tx(){return S(this,f,"f")}}f=new WeakMap; |
{ | ||
"name": "@secux/app-btc", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "SecuX Hardware Wallet BTC API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
0
60708