Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@secux/app-btc

Package Overview
Dependencies
Maintainers
2
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@secux/app-btc - npm Package Compare versions

Comparing version 2.0.0-rc0 to 2.0.0-rc1

2

lib/psbt.js

@@ -1,1 +0,1 @@

"use strict";var t,i,e,s,r,n,o,a,u,c,h,p,f,d=this&&this.__awaiter||function(t,i,e,s){return new(e||(e=Promise))((function(r,n){function o(t){try{u(s.next(t))}catch(t){n(t)}}function a(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(o,a)}u((s=s.apply(t,i||[])).next())}))},l=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 w=require("bip174"),g=require("bip174/src/lib/utils"),y=require("./script"),_=require("@secux/utility"),T=require("@secux/transport"),P=require("./interface"),v=require("./utils"),m=require("./transaction"),b=null===T.Logger||void 0===T.Logger?void 0:T.Logger.child({id:"psbt"});function W(t,i,e){const s=t.__NON_WITNESS_UTXO_TX_CACHE;if(!s[e]){t.__NON_WITNESS_UTXO_BUF_CACHE[e]=i.nonWitnessUtxo;const s=m.Transaction.fromBuffer(i.nonWitnessUtxo);t.__NON_WITNESS_UTXO_TX_CACHE[e]=s}return s[e]}exports.SecuxPsbt=class{constructor(p,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),u.set(this,Buffer.alloc(0)),c.set(this,[]),h.set(this,{address:"",value:""}),l(this,i,p,"f"),l(this,e,new w.Psbt(new x),"f"),l(this,s,f,"f"),l(this,r,(0,v.getPayment)(S(this,s,"f")),"f"),l(this,a,{__NON_WITNESS_UTXO_TX_CACHE:[],__NON_WITNESS_UTXO_BUF_CACHE:[],__TX_IN_CACHE:{},__TX:S(this,e,"f").globalMap.unsignedTx.tx},"f")}AddInput(i){return d(this,void 0,void 0,(function*(){const n=yield S(this,t,"m",p).call(this,i.path),u={},h={};switch(i.script){case P.ScriptType.P2PKH:if(!i.txHex)throw Error("ArgumentError: Previous transaction raw data needed");u.nonWitnessUtxo=Buffer.from(i.txHex,"hex");const t=S(this,e,"f").inputs.length;S(this,a,"f").__NON_WITNESS_UTXO_BUF_CACHE[t]=u.nonWitnessUtxo;const o=m.Transaction.fromBuffer(u.nonWitnessUtxo);S(this,a,"f").__NON_WITNESS_UTXO_TX_CACHE[t]=o;break;case P.ScriptType.P2SH_P2WPKH:const c=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:n});u.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),c.redeemHash).scriptPublickey,value:i.satoshis},h.redeemScript=c.scriptPublickey;break;case P.ScriptType.P2WPKH:u.witnessUtxo={script:S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:n}).scriptPublickey,value:i.satoshis};break;default:throw Error("Invalid ScriptType")}const f=Object.assign(Object.assign({hash:i.hash,index:i.vout},u),h);return S(this,e,"f").addInput(f),S(this,c,"f").push(i.script),S(this,o,"f").push(i.path),this}))}AddInputs(t){return d(this,void 0,void 0,(function*(){for(const i of t)yield this.AddInput(i);return this}))}AddOutput(i){return d(this,void 0,void 0,(function*(){const n=(0,_.BigIntToBuffer)(i.satoshis,8);let o,a,c,f,d=i.satoshis;if(o=(0,P.isOutuptScriptExtended)(i)){const i=yield S(this,t,"m",p).call(this,o.path);switch(f=o.path,o.script){case P.ScriptType.P2SH_P2WPKH:if(!o.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");const t=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:i}).redeemHash,e=S(this,r,"f").p2sh(S(this,s,"f"),t);a=e.scriptPublickey,c=e.address;break;case P.ScriptType.P2PKH:if(!o.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");const n=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i});a=n.scriptPublickey,c=n.address;break;case P.ScriptType.P2WPKH:if(!o.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});a=u.scriptPublickey,c=u.address;break;default:throw Error("Invalid ScriptType")}}else if(o=(0,P.isOutputAddress)(i))a=S(this,r,"f").decode(S(this,s,"f"),o.address),c=o.address;else{if(!(o=(0,P.isOutuptScript)(i)))throw Error("Invalid parameter of output");a=Buffer.from(o.scriptHex,"hex")}return 0===S(this,e,"f").outputs.length?(l(this,u,Buffer.concat([S(this,u,"f"),n,Buffer.from([a.length]),a]),"f"),S(this,h,"f").address=c,S(this,h,"f").value=(1e-8*d).toString()):l(this,u,Buffer.concat([S(this,u,"f"),n,(0,_.buildPathBuffer)(f).pathBuffer]),"f"),S(this,e,"f").addOutput({script:a,value:d}),this}))}AddOutputs(t){return d(this,void 0,void 0,(function*(){for(const i of t)yield this.AddOutput(i);return this}))}Sign(){return d(this,void 0,void 0,(function*(){l(this,u,Buffer.concat([Buffer.from([S(this,e,"f").outputs.length]),S(this,u,"f")]),"f");const r=S(this,e,"f").inputs.map(((t,i)=>{const r=S(this,c,"f")[i],{data:n}=function(t,i,e,s,r){const n=r.__TX,o=e.sighashType||m.Transaction.SIGHASH_ALL,a=(0,v.getSerializer)(t);let u;if(e.nonWitnessUtxo){const t=W(r,e,i),s=n.ins[i].hash,o=a.getHash(t);if(!s.equals(o))throw new Error(`Non-witness UTXO hash for input #${i} doesn't match the hash specified in the prevout`);const c=n.ins[i].index;u=t.outs[c]}else{if(!e.witnessUtxo)throw new Error("Need a Utxo input item for signing");u=e.witnessUtxo}const c=function(t,i,e,s){let r;switch(i){case P.ScriptType.P2SH_P2PKH:case P.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}(u.script,s,e.redeemScript,e.witnessScript);let h;switch(null==b||b.debug(`input #${i} script type: ${P.ScriptType[s]}`),null==b||b.debug(`script: ${c.toString("hex")}`),s){case P.ScriptType.P2WPKH:case P.ScriptType.P2SH_P2WPKH:null==b||b.debug("p2wpkh");const s=(0,v.getPayment)(t).p2pkh(t,{hash:c.slice(2)}).scriptPublickey;h=a.dataForWitnessV0(n,i,s,u.value,o);break;default:if(t===P.CoinType.BITCOINCASH)null==b||b.debug("bch using bip143"),h=a.dataForWitnessV0(n,i,c,u.value,o);else{if(null==b||b.debug("non-segwit"),!e.nonWitnessUtxo)throw Error(`Input #${i} has witnessUtxo but non-segwit script: ${c.toString("hex")}`);h=a.dataForSignature(n,i,c,o)}}return{script:c,sighashType:o,data:h}}(S(this,s,"f"),i,S(this,e,"f").inputs[i],r,S(this,a,"f"));return S(this,s,"f")!==P.CoinType.BITCOINCASH&&r===P.ScriptType.P2PKH?{data:n,confirm:Buffer.alloc(0)}:{data:n,confirm:S(this,u,"f")}}));for(let t=0;t<r.length;t++)null==b||b.debug(`tx data [${t}]: ${r[t].data.toString("hex")}`),null==b||b.debug(`confirm data [${t}]: ${r[t].confirm.toString("hex")}`);const n=r.map((t=>t.data)),h=yield S(this,i,"f").SignRawTransactionGroup(S(this,o,"f"),n,r[0].confirm);let f=[];for(let i=0;i<S(this,e,"f").inputs.length;i++){const s=h[i];s.trimZero();const r=Buffer.concat([s.r,s.s]),n=S(this,e,"f").inputs[i].sighashType||m.Transaction.SIGHASH_ALL,a=[{pubkey:yield S(this,t,"m",p).call(this,S(this,o,"f")[i]),signature:y.encode(r,n)}];S(this,e,"f").updateInput(i,{partialSig:a}),f.push(a[0].signature)}return f}))}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,g.checkForInput)(S(this,e,"f").inputs,i);const s=function(t,i,e){var s;const r=e.__TX;let n;if(i.witnessScript)n=i.witnessScript;else if(i.redeemScript)n=i.redeemScript;else if(i.nonWitnessUtxo){const s=W(e,i,t),o=r.ins[t].index;n=s.outs[o].script}else n=null===(s=i.witnessUtxo)||void 0===s?void 0:s.script;return n}(i,t,S(this,a,"f"));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}=y.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 P.ScriptType.P2PKH:e=y.compile([r,n]);break;case P.ScriptType.P2SH_P2PKH:e=(()=>{const t=y.compile([r,n]),i=y.decompile(t),e={output:i[i.length-1],input:y.compile(i.slice(0,-1))};return y.compile([y.decompile(e.input),e.output])})();break;case P.ScriptType.P2SH_P2WPKH:case P.ScriptType.P2WPKH:s=(0,v.witnessStackToScriptWitness)([r,n])}return{finalScriptSig:e,finalScriptWitness:s}}(S(this,c,"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,v.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,u=new WeakMap,c=new WeakMap,h=new WeakMap,t=new WeakSet,p=function(t){var e;return d(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 x{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){f.set(this,void 0),l(this,f,m.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,u,h,c,p,f,d=this&&this.__awaiter||function(t,i,e,s){return new(e||(e=Promise))((function(r,n){function o(t){try{u(s.next(t))}catch(t){n(t)}}function a(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(o,a)}u((s=s.apply(t,i||[])).next())}))},l=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 w=require("bip174"),g=require("bip174/src/lib/utils"),y=require("./script"),T=require("@secux/utility"),_=require("@secux/transport"),P=require("./interface"),v=require("./utils"),m=require("./transaction"),b=null===_.Logger||void 0===_.Logger?void 0:_.Logger.child({id:"psbt"});function W(t,i,e){const s=t.__NON_WITNESS_UTXO_TX_CACHE;if(!s[e]){t.__NON_WITNESS_UTXO_BUF_CACHE[e]=i.nonWitnessUtxo;const s=m.Transaction.fromBuffer(i.nonWitnessUtxo);t.__NON_WITNESS_UTXO_TX_CACHE[e]=s}return s[e]}exports.SecuxPsbt=class{constructor(p,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),u.set(this,Buffer.alloc(0)),h.set(this,[]),c.set(this,{address:"",value:""}),l(this,i,p,"f"),l(this,e,new w.Psbt(new x),"f"),l(this,s,f,"f"),l(this,r,(0,v.getPayment)(S(this,s,"f")),"f"),l(this,a,{__NON_WITNESS_UTXO_TX_CACHE:[],__NON_WITNESS_UTXO_BUF_CACHE:[],__TX_IN_CACHE:{},__TX:S(this,e,"f").globalMap.unsignedTx.tx},"f"),f===P.CoinType.BITCOINCASH&&(S(this,a,"f").__TX.version=1)}AddInput(i){return d(this,void 0,void 0,(function*(){const n=yield S(this,t,"m",p).call(this,i.path),u={},c={};switch(i.script){case P.ScriptType.P2PKH:if(!i.txHex)throw Error("ArgumentError: Previous transaction raw data needed");u.nonWitnessUtxo=Buffer.from(i.txHex,"hex");const t=S(this,e,"f").inputs.length;S(this,a,"f").__NON_WITNESS_UTXO_BUF_CACHE[t]=u.nonWitnessUtxo;const o=m.Transaction.fromBuffer(u.nonWitnessUtxo);S(this,a,"f").__NON_WITNESS_UTXO_TX_CACHE[t]=o;break;case P.ScriptType.P2SH_P2WPKH:const h=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:n});u.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),h.redeemHash).scriptPublickey,value:i.satoshis},c.redeemScript=h.scriptPublickey;break;case P.ScriptType.P2WPKH:u.witnessUtxo={script:S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:n}).scriptPublickey,value:i.satoshis};break;default:throw Error("Invalid ScriptType")}const f=Object.assign(Object.assign({hash:i.hash,index:i.vout},u),c);return S(this,e,"f").addInput(f),S(this,h,"f").push(i.script),S(this,o,"f").push(i.path),this}))}AddInputs(t){return d(this,void 0,void 0,(function*(){for(const i of t)yield this.AddInput(i);return this}))}AddOutput(i){return d(this,void 0,void 0,(function*(){const n=(0,T.BigIntToBuffer)(i.satoshis,8);let o,a,h,f,d=i.satoshis;if(o=(0,P.isOutuptScriptExtended)(i)){const i=yield S(this,t,"m",p).call(this,o.path);switch(f=o.path,o.script){case P.ScriptType.P2SH_P2WPKH:if(!o.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");const t=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:i}).redeemHash,e=S(this,r,"f").p2sh(S(this,s,"f"),t);a=e.scriptPublickey,h=e.address;break;case P.ScriptType.P2PKH:if(!o.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");const n=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i});a=n.scriptPublickey,h=n.address;break;case P.ScriptType.P2WPKH:if(!o.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});a=u.scriptPublickey,h=u.address;break;default:throw Error("Invalid ScriptType")}}else if(o=(0,P.isOutputAddress)(i))a=S(this,r,"f").decode(S(this,s,"f"),o.address),h=o.address;else{if(!(o=(0,P.isOutuptScript)(i)))throw Error("Invalid parameter of output");a=Buffer.from(o.scriptHex,"hex")}return 0===S(this,e,"f").outputs.length?(l(this,u,Buffer.concat([S(this,u,"f"),n,Buffer.from([a.length]),a]),"f"),S(this,c,"f").address=h,S(this,c,"f").value=(1e-8*d).toString()):l(this,u,Buffer.concat([S(this,u,"f"),n,(0,T.buildPathBuffer)(f).pathBuffer]),"f"),S(this,e,"f").addOutput({script:a,value:d}),this}))}AddOutputs(t){return d(this,void 0,void 0,(function*(){for(const i of t)yield this.AddOutput(i);return this}))}Sign(){return d(this,void 0,void 0,(function*(){l(this,u,Buffer.concat([Buffer.from([S(this,e,"f").outputs.length]),S(this,u,"f")]),"f");const r=S(this,e,"f").inputs.map(((t,i)=>{const r=S(this,h,"f")[i],{data:n}=function(t,i,e,s,r){const n=r.__TX,o=e.sighashType||m.Transaction.SIGHASH_ALL,a=(0,v.getSerializer)(t);let u;if(e.nonWitnessUtxo){const t=W(r,e,i),s=n.ins[i].hash,o=a.getHash(t);if(!s.equals(o))throw new Error(`Non-witness UTXO hash for input #${i} doesn't match the hash specified in the prevout`);const h=n.ins[i].index;u=t.outs[h]}else{if(!e.witnessUtxo)throw new Error("Need a Utxo input item for signing");u=e.witnessUtxo}const h=function(t,i,e,s){let r;switch(i){case P.ScriptType.P2SH_P2PKH:case P.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}(u.script,s,e.redeemScript,e.witnessScript);let c;switch(null==b||b.debug(`input #${i} script type: ${P.ScriptType[s]}`),null==b||b.debug(`script: ${h.toString("hex")}`),s){case P.ScriptType.P2WPKH:case P.ScriptType.P2SH_P2WPKH:null==b||b.debug("p2wpkh");const s=(0,v.getPayment)(t).p2pkh(t,{hash:h.slice(2)}).scriptPublickey;c=a.dataForWitnessV0(n,i,s,u.value,o);break;default:if(t===P.CoinType.BITCOINCASH)null==b||b.debug("bch using bip143"),c=a.dataForWitnessV0(n,i,h,u.value,64|o);else{if(null==b||b.debug("non-segwit"),!e.nonWitnessUtxo)throw Error(`Input #${i} has witnessUtxo but non-segwit script: ${h.toString("hex")}`);c=a.dataForSignature(n,i,h,o)}}return{script:h,sighashType:o,data:c}}(S(this,s,"f"),i,S(this,e,"f").inputs[i],r,S(this,a,"f"));return S(this,s,"f")!==P.CoinType.BITCOINCASH&&r===P.ScriptType.P2PKH?{data:n,confirm:Buffer.alloc(0)}:{data:n,confirm:S(this,u,"f")}}));for(let t=0;t<r.length;t++)null==b||b.debug(`tx data [${t}]: ${r[t].data.toString("hex")}`),null==b||b.debug(`confirm data [${t}]: ${r[t].confirm.toString("hex")}`);const n=r.map((t=>t.data)),c=yield S(this,i,"f").SignRawTransactionGroup(S(this,o,"f"),n,r[0].confirm);let f=[];for(let i=0;i<S(this,e,"f").inputs.length;i++){const r=c[i];r.trimZero();const n=Buffer.concat([r.r,r.s]);let a=S(this,e,"f").inputs[i].sighashType||m.Transaction.SIGHASH_ALL;S(this,s,"f")===P.CoinType.BITCOINCASH&&(a|=64);const u=[{pubkey:yield S(this,t,"m",p).call(this,S(this,o,"f")[i]),signature:y.encode(n,a)}];S(this,e,"f").updateInput(i,{partialSig:u}),f.push(u[0].signature)}return f}))}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,g.checkForInput)(S(this,e,"f").inputs,i);const s=function(t,i,e){var s;const r=e.__TX;let n;if(i.witnessScript)n=i.witnessScript;else if(i.redeemScript)n=i.redeemScript;else if(i.nonWitnessUtxo){const s=W(e,i,t),o=r.ins[t].index;n=s.outs[o].script}else n=null===(s=i.witnessUtxo)||void 0===s?void 0:s.script;return n}(i,t,S(this,a,"f"));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}=y.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 P.ScriptType.P2PKH:e=y.compile([r,n]);break;case P.ScriptType.P2SH_P2PKH:e=(()=>{const t=y.compile([r,n]),i=y.decompile(t),e={output:i[i.length-1],input:y.compile(i.slice(0,-1))};return y.compile([y.decompile(e.input),e.output])})();break;case P.ScriptType.P2SH_P2WPKH:case P.ScriptType.P2WPKH:s=(0,v.witnessStackToScriptWitness)([r,n])}return{finalScriptSig:e,finalScriptWitness:s}}(S(this,h,"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,v.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,u=new WeakMap,h=new WeakMap,c=new WeakMap,t=new WeakSet,p=function(t){var e;return d(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 x{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){f.set(this,void 0),l(this,f,m.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;

@@ -1,1 +0,1 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.encode=exports.decode=exports.decompile=exports.compile=void 0;const e=require("bip66"),n=require("./coindef"),t=Buffer.alloc(1,0);function r(e){let n=0;for(;0===e[n];)++n;return n===e.length?t:128&(e=e.slice(n))[0]?Buffer.concat([t,e],1+e.length):e}function o(e){0===e[0]&&(e=e.slice(1));const n=Buffer.alloc(32,0),t=Math.max(0,32-e.length);return e.copy(n,t),n}function c(e){return 0===e.length?n.OPCODES.OP_0:1===e.length?e[0]>=1&&e[0]<=16?n.OPCODES.OP_INT_BASE+e[0]:129===e[0]?n.OPCODES.OP_1NEGATE:void 0:void 0}exports.compile=function(e){const n=e.reduce(((e,n)=>Buffer.isBuffer(n)?1===n.length&&void 0!==c(n)?e+1:e+i.encodingLength(n.length)+n.length:e+1),0),t=Buffer.allocUnsafe(n);let r=0;for(const n of e)if(Buffer.isBuffer(n)){const e=c(n);if(void 0!==e)return t.writeUInt8(e,r),void(r+=1);r+=i.encode(t,n.length,r),n.copy(t,r),r+=n.length}else t.writeUInt8(n,r),r+=1;if(r!==t.length)throw new Error("Could not decode chunks");return t},exports.decompile=function(e){const t=[];let r=0;for(;r<e.length;){const o=e[r];if(o>n.OPCODES.OP_0&&o<=n.OPCODES.OP_PUSHDATA4){const n=i.decode(e,r);if(null===n)return null;if(r+=n.size,r+n.number>e.length)return null;const o=e.slice(r,r+n.number);r+=n.number;const l=c(o);void 0!==l?t.push(l):t.push(o)}else t.push(o),r+=1}return t},exports.decode=function(n){const t=n.readUInt8(n.length-1),r=-129&t;if(r<=0||r>=4)throw new Error("Invalid hashType "+t);const c=e.decode(n.slice(0,-1)),i=o(c.r),l=o(c.s);return{signature:Buffer.concat([i,l],64),hashType:t}},exports.encode=function(n,t){const o=-129&t;if(o<=0||o>=4)throw new Error("Invalid hashType "+t);const c=Buffer.allocUnsafe(1);c.writeUInt8(t,0);const i=r(n.slice(0,32)),l=r(n.slice(32,64));return Buffer.concat([e.encode(i,l),c])};class i{static encodingLength(e){return e<n.OPCODES.OP_PUSHDATA1?1:e<=255?2:e<=65535?3:5}static encode(e,t,r){var o=this.encodingLength(t);return 1===o?e.writeUInt8(t,r):2===o?(e.writeUInt8(n.OPCODES.OP_PUSHDATA1,r),e.writeUInt8(t,r+1)):3===o?(e.writeUInt8(n.OPCODES.OP_PUSHDATA2,r),e.writeUInt16LE(t,r+1)):(e.writeUInt8(n.OPCODES.OP_PUSHDATA4,r),e.writeUInt32LE(t,r+1)),o}static decode(e,t){var r,o,c=e.readUInt8(t);if(c<n.OPCODES.OP_PUSHDATA1)r=c,o=1;else if(c===n.OPCODES.OP_PUSHDATA1){if(t+2>e.length)return null;r=e.readUInt8(t+1),o=2}else if(c===n.OPCODES.OP_PUSHDATA2){if(t+3>e.length)return null;r=e.readUInt16LE(t+1),o=3}else{if(t+5>e.length)return null;if(c!==n.OPCODES.OP_PUSHDATA4)throw new Error("Unexpected opcode");r=e.readUInt32LE(t+1),o=5}return{opcode:c,number:r,size:o}}}
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.encode=exports.decode=exports.decompile=exports.compile=void 0;const e=require("bip66"),n=require("./coindef"),t=Buffer.alloc(1,0);function r(e){let n=0;for(;0===e[n];)++n;return n===e.length?t:128&(e=e.slice(n))[0]?Buffer.concat([t,e],1+e.length):e}function o(e){0===e[0]&&(e=e.slice(1));const n=Buffer.alloc(32,0),t=Math.max(0,32-e.length);return e.copy(n,t),n}function c(e){return 0===e.length?n.OPCODES.OP_0:1===e.length?e[0]>=1&&e[0]<=16?n.OPCODES.OP_INT_BASE+e[0]:129===e[0]?n.OPCODES.OP_1NEGATE:void 0:void 0}exports.compile=function(e){const n=e.reduce(((e,n)=>Buffer.isBuffer(n)?1===n.length&&void 0!==c(n)?e+1:e+i.encodingLength(n.length)+n.length:e+1),0),t=Buffer.allocUnsafe(n);let r=0;for(const n of e)if(Buffer.isBuffer(n)){const e=c(n);if(void 0!==e)return t.writeUInt8(e,r),void(r+=1);r+=i.encode(t,n.length,r),n.copy(t,r),r+=n.length}else t.writeUInt8(n,r),r+=1;if(r!==t.length)throw new Error("Could not decode chunks");return t},exports.decompile=function(e){const t=[];let r=0;for(;r<e.length;){const o=e[r];if(o>n.OPCODES.OP_0&&o<=n.OPCODES.OP_PUSHDATA4){const n=i.decode(e,r);if(null===n)return null;if(r+=n.size,r+n.number>e.length)return null;const o=e.slice(r,r+n.number);r+=n.number;const l=c(o);void 0!==l?t.push(l):t.push(o)}else t.push(o),r+=1}return t},exports.decode=function(n){const t=n.readUInt8(n.length-1),r=-129&t;if(r<=0||r>=4)throw new Error("Invalid hashType "+t);const c=e.decode(n.slice(0,-1)),i=o(c.r),l=o(c.s);return{signature:Buffer.concat([i,l],64),hashType:t}},exports.encode=function(n,t){const o=Buffer.allocUnsafe(1);o.writeUInt8(t,0);const c=r(n.slice(0,32)),i=r(n.slice(32,64));return Buffer.concat([e.encode(c,i),o])};class i{static encodingLength(e){return e<n.OPCODES.OP_PUSHDATA1?1:e<=255?2:e<=65535?3:5}static encode(e,t,r){var o=this.encodingLength(t);return 1===o?e.writeUInt8(t,r):2===o?(e.writeUInt8(n.OPCODES.OP_PUSHDATA1,r),e.writeUInt8(t,r+1)):3===o?(e.writeUInt8(n.OPCODES.OP_PUSHDATA2,r),e.writeUInt16LE(t,r+1)):(e.writeUInt8(n.OPCODES.OP_PUSHDATA4,r),e.writeUInt32LE(t,r+1)),o}static decode(e,t){var r,o,c=e.readUInt8(t);if(c<n.OPCODES.OP_PUSHDATA1)r=c,o=1;else if(c===n.OPCODES.OP_PUSHDATA1){if(t+2>e.length)return null;r=e.readUInt8(t+1),o=2}else if(c===n.OPCODES.OP_PUSHDATA2){if(t+3>e.length)return null;r=e.readUInt16LE(t+1),o=3}else{if(t+5>e.length)return null;if(c!==n.OPCODES.OP_PUSHDATA4)throw new Error("Unexpected opcode");r=e.readUInt32LE(t+1),o=5}return{opcode:c,number:r,size:o}}}
{
"name": "@secux/app-btc",
"version": "2.0.0-rc0",
"version": "2.0.0-rc1",
"description": "SecuX Hardware Wallet BTC API",

@@ -35,4 +35,4 @@ "keywords": [

"dependencies": {
"@secux/transport": "^2.1.1",
"@secux/utility": "^2.0.3",
"@secux/transport": "~2.1",
"@secux/utility": "^2.0.4",
"bech32": "^2.0.0",

@@ -39,0 +39,0 @@ "bip174": "^2.0.1",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc