@secux/app-btc
Advanced tools
Comparing version 2.0.0-rc2 to 2.0.0-rc3
@@ -1,1 +0,1 @@ | ||
"use strict";var t=this&&this.__awaiter||function(t,e,i,r){return new(i||(i=Promise))((function(o,n){function p(t){try{c(r.next(t))}catch(t){n(t)}}function s(t){try{c(r.throw(t))}catch(t){n(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(p,s)}c((r=r.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxBTC=exports.ScriptType=exports.CoinType=void 0;const e=require("@secux/transport/lib/ITransaction"),i=require("@secux/utility"),r=require("ow"),o=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return o.CoinType}}),Object.defineProperty(exports,"ScriptType",{enumerable:!0,get:function(){return o.ScriptType}});const n=require("./psbt"),p=require("./utils");exports.SecuxBTC=class{static getAddress(n,s,c){var u,a;return t(this,void 0,void 0,(function*(){c&&(0,r.default)(c,o.ow_AddressOption);const t=void 0===(null==c?void 0:c.coin)?o.btcCoinTypes:o.coinmap[null==c?void 0:c.coin].coinType,d=void 0===(null==c?void 0:c.script)?null===(u=i.supported_coin.find((e=>e.cointype===t)))||void 0===u?void 0:u.purpose:(0,p.getPurpose)(null==c?void 0:c.script);(0,r.default)(s,i.ow_strictPath(t,d));const l=yield n.getPublickey(s,e.EllipticCurve.SECP256K1,!0),y=(0,p.getCoinType)(s),f=(0,p.getPayment)(y);switch(null!==(a=null==c?void 0:c.script)&&void 0!==a?a:(0,p.getDefaultScript)(s)){case o.ScriptType.P2SH_P2WPKH:const t=f.p2wpkh(y,{publickey:l});return f.p2sh(y,t.redeemHash).address;case o.ScriptType.P2SH_P2PKH:const e=f.p2pkh(y,{publickey:l});return f.p2sh(y,e.redeemHash).address;case o.ScriptType.P2PKH:return f.p2pkh(y,{publickey:l}).address;case o.ScriptType.P2WPKH:return f.p2wpkh(y,{publickey:l}).address;default:throw Error("Invalid or unsupported ScriptType")}}))}static signTransaction(e,s,c){return t(this,void 0,void 0,(function*(){(0,r.default)(s,r.default.array.ofType(o.ow_txInput).minLength(1));const t=(0,p.getCoinType)(s[0].path);(0,r.default)(c,o.ow_txOutput),s.map((e=>{const n=e.script?(0,p.getPurpose)(e.script):o.btcPurposes;(0,r.default)(e.path,i.ow_strictPath(o.coinmap[t].coinType,n))}));let u=(0,o.isOutuptScriptExtended)(c.to);if(u){const e=u.script?(0,p.getPurpose)(u.script):o.btcPurposes;(0,r.default)(u.path,i.ow_strictPath(o.coinmap[t].coinType,e))}if(c.utxo){const e=c.utxo.script?(0,p.getPurpose)(c.utxo.script):o.btcPurposes;(0,r.default)(c.utxo.path,i.ow_strictPath(o.coinmap[t].coinType,e))}const a=new n.SecuxPsbt(e,t);yield a.AddInputs(s),yield a.AddOutputs(c.utxo?[c.to,c.utxo]:[c.to]);const d=yield a.Sign();return{raw_tx:a.finalizeAllInputs().extractTransaction().toHex(),signature:d}}))}}; | ||
"use strict";var t=this&&this.__awaiter||function(t,e,r,i){return new(r||(r=Promise))((function(o,n){function s(t){try{p(i.next(t))}catch(t){n(t)}}function c(t){try{p(i.throw(t))}catch(t){n(t)}}function p(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,c)}p((i=i.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxBTC=exports.ScriptType=exports.CoinType=void 0;const e=require("@secux/transport/lib/ITransaction"),r=require("@secux/utility"),i=require("ow"),o=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return o.CoinType}}),Object.defineProperty(exports,"ScriptType",{enumerable:!0,get:function(){return o.ScriptType}});const n=require("./psbt"),s=require("./utils");exports.SecuxBTC=class{static getAddress(n,c,p){var u,a;return t(this,void 0,void 0,(function*(){(0,i.default)(c,r.ow_strictPath(o.btcCoinTypes,o.btcPurposes)),p&&(0,i.default)(p,o.ow_AddressOption);const t=null!==(u=null==p?void 0:p.coin)&&void 0!==u?u:(0,s.getCoinType)(c),d=o.coinmap[t].coinType,l=void 0===(null==p?void 0:p.script)?void 0:(0,s.getPurpose)(null==p?void 0:p.script);(0,i.default)(c,r.ow_strictPath(d,l));const y=yield n.getPublickey(c,e.EllipticCurve.SECP256K1,!0),P=(0,s.getPayment)(t);switch(null!==(a=null==p?void 0:p.script)&&void 0!==a?a:(0,s.getDefaultScript)(c)){case o.ScriptType.P2SH_P2WPKH:const e=P.p2wpkh(t,{publickey:y});return P.p2sh(t,e.redeemHash).address;case o.ScriptType.P2SH_P2PKH:const r=P.p2pkh(t,{publickey:y});return P.p2sh(t,r.redeemHash).address;case o.ScriptType.P2PKH:return P.p2pkh(t,{publickey:y}).address;case o.ScriptType.P2WPKH:return P.p2wpkh(t,{publickey:y}).address;default:throw Error("Invalid or unsupported ScriptType")}}))}static signTransaction(e,c,p){return t(this,void 0,void 0,(function*(){(0,i.default)(c,i.default.array.ofType(o.ow_txInput).minLength(1));const t=(0,s.getCoinType)(c[0].path);(0,i.default)(p,o.ow_txOutput),c.map((e=>{const n=e.script?(0,s.getPurpose)(e.script):o.btcPurposes;(0,i.default)(e.path,r.ow_strictPath(o.coinmap[t].coinType,n))}));let u=(0,o.isOutuptScriptExtended)(p.to);if(u){const e=u.script?(0,s.getPurpose)(u.script):o.btcPurposes;(0,i.default)(u.path,r.ow_strictPath(o.coinmap[t].coinType,e))}if(p.utxo){const e=p.utxo.script?(0,s.getPurpose)(p.utxo.script):o.btcPurposes;(0,i.default)(p.utxo.path,r.ow_strictPath(o.coinmap[t].coinType,e))}const a=new n.SecuxPsbt(e,t);yield a.AddInputs(c),yield a.AddOutputs(p.utxo?[p.to,p.utxo]:[p.to]);const d=yield a.Sign();return{raw_tx:a.finalizeAllInputs().extractTransaction().toHex(),signature:d}}))}}; |
@@ -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 w=require("bip174"),g=require("bip174/src/lib/utils"),y=require("./script"),P=require("@secux/utility"),T=require("@secux/transport"),_=require("./interface"),v=require("./utils"),m=require("./transaction"),b=null===T.Logger||void 0===T.Logger?void 0:T.Logger.child({id:"psbt"});function k(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(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 w.Psbt(new H),"f"),d(this,s,f,"f"),d(this,r,(0,v.getPayment)(S(this,s,"f")),"f"),d(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===_.CoinType.BITCOINCASH&&(S(this,a,"f").__TX.version=1)}AddInput(i){var n;return l(this,void 0,void 0,(function*(){const h=yield S(this,t,"m",c).call(this,i.path),u={},f={},l=null!==(n=i.script)&&void 0!==n?n:(0,v.getDefaultScript)(i.path);switch(l){case _.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 n=m.Transaction.fromBuffer(u.nonWitnessUtxo);S(this,a,"f").__NON_WITNESS_UTXO_TX_CACHE[t]=n;break;case _.ScriptType.P2SH_P2PKH:const o=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:h});u.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),o.redeemHash).scriptPublickey,value:i.satoshis},f.redeemScript=o.scriptPublickey;break;case _.ScriptType.P2SH_P2WPKH:const p=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:h});u.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),p.redeemHash).scriptPublickey,value:i.satoshis},f.redeemScript=p.scriptPublickey;break;case _.ScriptType.P2WPKH:u.witnessUtxo={script:S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:h}).scriptPublickey,value:i.satoshis};break;default:throw Error(`ArgumentError: Invalid ScriptType of input#${S(this,e,"f").inputs.length}, got "${_.ScriptType[l]}"`)}const d=Object.assign(Object.assign({hash:i.hash,index:i.vout},u),f);return S(this,e,"f").addInput(d),S(this,p,"f").push(l),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,P.BigIntToBuffer)(i.satoshis,8);let a,p,f,l,w=i.satoshis;if(a=(0,_.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,v.getDefaultScript)(l);switch(o){case _.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 _.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 _.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 _.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 "${_.ScriptType[o]}"`)}}else if(a=(0,_.isOutputAddress)(i))p=S(this,r,"f").decode(S(this,s,"f"),a.address),f=a.address;else{if(!(a=(0,_.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*w).toString()):d(this,h,Buffer.concat([S(this,h,"f"),o,(0,P.buildPathBuffer)(l).pathBuffer]),"f"),S(this,e,"f").addOutput({script:p,value:w}),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||m.Transaction.SIGHASH_ALL,a=(0,v.getSerializer)(t);let h;if(e.nonWitnessUtxo){const t=k(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 p=n.ins[i].index;h=t.outs[p]}else{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 _.ScriptType.P2SH_P2PKH:case _.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==b||b.debug(`input #${i} script type: ${_.ScriptType[s]}`),null==b||b.debug(`script: ${p.toString("hex")}`),s){case _.ScriptType.P2WPKH:case _.ScriptType.P2SH_P2WPKH:case _.ScriptType.P2SH_P2PKH:null==b||b.debug(_.ScriptType[s]);const r=(0,v.getPayment)(t).p2pkh(t,{hash:p.slice(2)}).scriptPublickey;u=a.dataForWitnessV0(n,i,r,h.value,o);break;default:if(t===_.CoinType.BITCOINCASH)null==b||b.debug("bch using bip143"),u=a.dataForWitnessV0(n,i,p,h.value,64|o);else{if(null==b||b.debug("non-segwit"),!e.nonWitnessUtxo)throw Error(`Input #${i} has witnessUtxo but non-segwit script: ${p.toString("hex")}`);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")!==_.CoinType.BITCOINCASH&&r===_.ScriptType.P2PKH?{data:n,confirm:Buffer.alloc(0)}:{data:n,confirm:S(this,h,"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)),u=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=u[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")===_.CoinType.BITCOINCASH&&(a|=64);const h=[{pubkey:yield S(this,t,"m",c).call(this,S(this,o,"f")[i]),signature:y.encode(n,a)}];S(this,e,"f").updateInput(i,{partialSig:h}),f.push(h[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=k(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 _.ScriptType.P2PKH:e=y.compile([r,n]);break;case _.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 _.ScriptType.P2SH_P2WPKH:case _.ScriptType.P2WPKH:s=(0,v.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,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,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 H{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){f.set(this,void 0),d(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,c,p,u,h,f,l=this&&this.__awaiter||function(t,i,e,s){return new(e||(e=Promise))((function(r,n){function o(t){try{c(s.next(t))}catch(t){n(t)}}function a(t){try{c(s.throw(t))}catch(t){n(t)}}function c(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(o,a)}c((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 y=require("bip174"),w=require("bip174/src/lib/utils"),g=require("./script"),T=require("@secux/utility"),P=require("@secux/transport"),_=require("./interface"),m=require("./utils"),b=require("./payment"),v=require("./payment_bch"),k=require("./transaction"),H=require("./coindef"),W=null===P.Logger||void 0===P.Logger?void 0:P.Logger.child({id:"psbt"});function x(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=k.Transaction.fromBuffer(i.nonWitnessUtxo);t.__NON_WITNESS_UTXO_TX_CACHE[e]=s}return s[e]}exports.SecuxPsbt=class{constructor(h,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),c.set(this,Buffer.alloc(0)),p.set(this,[]),u.set(this,{address:"",value:""}),d(this,i,h,"f"),d(this,e,new y.Psbt(new E),"f"),d(this,s,f,"f"),d(this,r,(0,m.getPayment)(S(this,s,"f")),"f"),d(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===_.CoinType.BITCOINCASH&&(S(this,a,"f").__TX.version=1)}AddInput(i){var n;return l(this,void 0,void 0,(function*(){const c=yield S(this,t,"m",h).call(this,i.path),u={},f={},l=null!==(n=i.script)&&void 0!==n?n:(0,m.getDefaultScript)(i.path);switch(l){case _.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 n=k.Transaction.fromBuffer(u.nonWitnessUtxo);S(this,a,"f").__NON_WITNESS_UTXO_TX_CACHE[t]=n;break;case _.ScriptType.P2SH_P2PKH:const o=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:c});u.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),o.redeemHash).scriptPublickey,value:i.satoshis},f.redeemScript=o.scriptPublickey;break;case _.ScriptType.P2SH_P2WPKH:const p=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:c});u.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),p.redeemHash).scriptPublickey,value:i.satoshis},f.redeemScript=p.scriptPublickey;break;case _.ScriptType.P2WPKH:u.witnessUtxo={script:S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:c}).scriptPublickey,value:i.satoshis};break;default:throw Error(`ArgumentError: Invalid ScriptType of input#${S(this,e,"f").inputs.length}, got "${_.ScriptType[l]}"`)}const d=Object.assign(Object.assign({hash:i.hash,index:i.vout},u),f);return S(this,e,"f").addInput(d),S(this,p,"f").push(l),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,T.BigIntToBuffer)(i.satoshis,8);let a,p,f,l,y=i.satoshis;if(a=(0,_.isOutuptScriptExtended)(i)){const i=yield S(this,t,"m",h).call(this,a.path);l=a.path;const o=null!==(n=a.script)&&void 0!==n?n:(0,m.getDefaultScript)(l);switch(o){case _.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 _.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 _.ScriptType.P2PKH:if(!a.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");const c=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i});p=c.scriptPublickey,f=c.address;break;case _.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 "${_.ScriptType[o]}"`)}}else if(a=(0,_.isOutputAddress)(i))p=S(this,r,"f").decode(S(this,s,"f"),a.address),f=a.address;else{if(!(a=(0,_.isOutuptScript)(i)))throw Error("Invalid parameter of output");p=Buffer.from(a.scriptHex,"hex")}return 0===S(this,e,"f").outputs.length?(d(this,c,Buffer.concat([S(this,c,"f"),o,Buffer.from([p.length]),p]),"f"),S(this,u,"f").address=f,S(this,u,"f").value=(1e-8*y).toString()):d(this,c,Buffer.concat([S(this,c,"f"),o,(0,T.buildPathBuffer)(l).pathBuffer]),"f"),S(this,e,"f").addOutput({script:p,value:y}),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,c,Buffer.concat([Buffer.from([S(this,e,"f").outputs.length]),S(this,c,"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||k.Transaction.SIGHASH_ALL,a=(0,m.getSerializer)(t);let c;if(e.nonWitnessUtxo){const t=x(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 p=n.ins[i].index;c=t.outs[p]}else{if(!e.witnessUtxo)throw new Error("Need a Utxo input item for signing");c=e.witnessUtxo}const p=function(t,i,e,s){let r;switch(i){case _.ScriptType.P2SH_P2PKH:case _.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}(c.script,s,e.redeemScript,e.witnessScript);let u;switch(null==W||W.debug(`input #${i} script type: ${_.ScriptType[s]}`),null==W||W.debug(`script: ${p.toString("hex")}`),s){case _.ScriptType.P2WPKH:case _.ScriptType.P2SH_P2WPKH:case _.ScriptType.P2SH_P2PKH:null==W||W.debug(_.ScriptType[s]);const r=(0,m.getPayment)(t).p2pkh(t,{hash:p.slice(2)}).scriptPublickey;u=a.dataForWitnessV0(n,i,r,c.value,o);break;default:if(t===_.CoinType.BITCOINCASH)null==W||W.debug("bch using bip143"),u=a.dataForWitnessV0(n,i,p,c.value,64|o);else{if(null==W||W.debug("non-segwit"),!e.nonWitnessUtxo)throw Error(`Input #${i} has witnessUtxo but non-segwit script: ${p.toString("hex")}`);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")!==_.CoinType.BITCOINCASH&&r===_.ScriptType.P2PKH?{data:n,confirm:Buffer.alloc(0)}:{data:n,confirm:S(this,c,"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)),u=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=u[i];r.trimZero();const n=Buffer.concat([r.r,r.s]);let a=S(this,e,"f").inputs[i].sighashType||k.Transaction.SIGHASH_ALL;S(this,s,"f")===_.CoinType.BITCOINCASH&&(a|=64);const c=[{pubkey:yield S(this,t,"m",h).call(this,S(this,o,"f")[i]),signature:g.encode(n,a)}];S(this,e,"f").updateInput(i,{partialSig:c}),f.push(c[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,w.checkForInput)(S(this,e,"f").inputs,i);const r=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=x(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(!r)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}=g.decode(t.signature);if(i!==e)throw new Error("Signature sighash does not match input sighash type")}}const{finalScriptSig:n,finalScriptWitness:o}=function(t,i){let e,s;const{signature:r,pubkey:n}=i[0];switch(t){case _.ScriptType.P2PKH:e=g.compile([r,n]);break;case _.ScriptType.P2SH_P2PKH:e=(()=>{const t=g.compile([r,n]),i=g.decompile(t),e={output:i[i.length-1],input:g.compile(i.slice(0,-1))};return g.compile([].concat(g.decompile(e.input),e.output))})();break;case _.ScriptType.P2SH_P2WPKH:s=(0,m.witnessStackToScriptWitness)([r,n]),e=(()=>{const t=Buffer.alloc(0),i=(0,b.Hash160)(n),e=g.compile([H.OPCODES.OP_0,i]);return g.compile([].concat(g.decompile(t),e))})();break;case _.ScriptType.P2WPKH:s=(0,m.witnessStackToScriptWitness)([r,n])}return{finalScriptSig:e,finalScriptWitness:s}}(S(this,p,"f")[i],t.partialSig);if(n)if(S(this,s,"f")!==_.CoinType.BITCOINCASH)S(this,e,"f").updateInput(i,{finalScriptSig:n});else{const s=v.PaymentBCH.p2pkh(_.CoinType.BITCOINCASH,{publickey:t.partialSig[0].pubkey}).scriptPublickey;S(this,e,"f").updateInput(i,{finalScriptSig:Buffer.concat([n,Buffer.from([s.length]),s])})}if(o&&S(this,e,"f").updateInput(i,{finalScriptWitness:o}),!n&&!o)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,m.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,c=new WeakMap,p=new WeakMap,u=new WeakMap,t=new WeakSet,h=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,k.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; |
/// <reference types="node" /> | ||
export declare function compile(chunks: any[]): Buffer | undefined; | ||
export declare function decompile(buffer: Buffer): (number | Buffer)[] | null; | ||
export declare function decompile(buffer: Buffer): (number | Buffer)[]; | ||
export declare function decode(buffer: Buffer): { | ||
@@ -5,0 +5,0 @@ signature: Buffer; |
@@ -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=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}}} | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.encode=exports.decode=exports.decompile=exports.compile=void 0;const e=require("bip66"),t=require("./coindef"),n=require("@secux/transport"),r=Buffer.alloc(1,0),o=null===n.Logger||void 0===n.Logger?void 0:n.Logger.child({id:"script"});function i(e){let t=0;for(;0===e[t];)++t;return t===e.length?r:128&(e=e.slice(t))[0]?Buffer.concat([r,e],1+e.length):e}function c(e){0===e[0]&&(e=e.slice(1));const t=Buffer.alloc(32,0),n=Math.max(0,32-e.length);return e.copy(t,n),t}function l(e){return 0===e.length?t.OPCODES.OP_0:1===e.length?e[0]>=1&&e[0]<=16?t.OPCODES.OP_INT_BASE+e[0]:129===e[0]?t.OPCODES.OP_1NEGATE:void 0:void 0}exports.compile=function(e){const t=e.reduce(((e,t)=>Buffer.isBuffer(t)?1===t.length&&void 0!==l(t)?e+1:e+s.encodingLength(t.length)+t.length:e+1),0),n=Buffer.allocUnsafe(t);let r=0;for(const t of e)if(Buffer.isBuffer(t)){const e=l(t);if(void 0!==e)return n.writeUInt8(e,r),void(r+=1);r+=s.encode(n,t.length,r),t.copy(n,r),r+=t.length}else n.writeUInt8(t,r),r+=1;if(r!==n.length)throw new Error("Could not decode chunks");return n},exports.decompile=function(e){const n=[];let r=0;for(;r<e.length;){const i=e[r];if(i>t.OPCODES.OP_0&&i<=t.OPCODES.OP_PUSHDATA4){const t=s.decode(e,r);if(null===t)return null==o||o.warn(`decompile error: reading a pushDataInt fail, got ${e.toString("binary")}, index:${r}`),[];if(r+=t.size,r+t.number>e.length)return null==o||o.warn(`decompile error: attempt to read too much data, got ${e.slice(r).toString("binary")}, desired length:${t.number}`),[];const i=e.slice(r,r+t.number);r+=t.number;const c=l(i);void 0!==c?n.push(c):n.push(i)}else n.push(i),r+=1}return n},exports.decode=function(t){const n=t.readUInt8(t.length-1),r=-129&n;if(r<=0||r>=4)throw new Error("Invalid hashType "+n);const o=e.decode(t.slice(0,-1)),i=c(o.r),l=c(o.s);return{signature:Buffer.concat([i,l],64),hashType:n}},exports.encode=function(t,n){const r=Buffer.allocUnsafe(1);r.writeUInt8(n,0);const o=i(t.slice(0,32)),c=i(t.slice(32,64));return Buffer.concat([e.encode(o,c),r])};class s{static encodingLength(e){return e<t.OPCODES.OP_PUSHDATA1?1:e<=255?2:e<=65535?3:5}static encode(e,n,r){var o=this.encodingLength(n);return 1===o?e.writeUInt8(n,r):2===o?(e.writeUInt8(t.OPCODES.OP_PUSHDATA1,r),e.writeUInt8(n,r+1)):3===o?(e.writeUInt8(t.OPCODES.OP_PUSHDATA2,r),e.writeUInt16LE(n,r+1)):(e.writeUInt8(t.OPCODES.OP_PUSHDATA4,r),e.writeUInt32LE(n,r+1)),o}static decode(e,n){var r,o,i=e.readUInt8(n);if(i<t.OPCODES.OP_PUSHDATA1)r=i,o=1;else if(i===t.OPCODES.OP_PUSHDATA1){if(n+2>e.length)return null;r=e.readUInt8(n+1),o=2}else if(i===t.OPCODES.OP_PUSHDATA2){if(n+3>e.length)return null;r=e.readUInt16LE(n+1),o=3}else{if(n+5>e.length)return null;if(i!==t.OPCODES.OP_PUSHDATA4)throw new Error("Unexpected opcode");r=e.readUInt32LE(n+1),o=5}return{opcode:i,number:r,size:o}}} |
{ | ||
"name": "@secux/app-btc", | ||
"version": "2.0.0-rc2", | ||
"version": "2.0.0-rc3", | ||
"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
59320
0