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-rc5 to 2.0.0-rc6

2

lib/interface.js

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ow_AddressOption=exports.btcPurposes=exports.btcCoinTypes=exports.coinmap=exports.CoinType=exports.isOutuptScriptExtended=exports.isOutuptScript=exports.isOutputAddress=exports.ow_txOutput=exports.ow_txOutputScriptExtened=exports.ow_txOutputScript=exports.ow_txOutputAddress=exports.ow_txInput=exports.ow_balance=exports.ScriptType=exports.OPCODES=void 0;const e=require("ow"),t=require("./coindef"),r=require("@secux/utility");var o,p,s=require("./coindef");Object.defineProperty(exports,"OPCODES",{enumerable:!0,get:function(){return s.OPCODES}}),function(e){e[e.P2PKH=0]="P2PKH",e[e.P2WPKH=1]="P2WPKH",e[e.P2SH_P2PKH=2]="P2SH_P2PKH",e[e.P2SH_P2WPKH=3]="P2SH_P2WPKH",e[e.__LENGTH=4]="__LENGTH"}(o=exports.ScriptType||(exports.ScriptType={})),exports.ow_balance=e.default.number.integer.positive.lessThanOrEqual(9007199254740991),exports.ow_txInput=e.default.object.exactShape({hash:r.ow_hashString,vout:e.default.number.greaterThanOrEqual(0),txHex:e.default.any(e.default.undefined,r.ow_hexString),script:e.default.optional.number.inRange(0,o.__LENGTH-1),satoshis:exports.ow_balance,path:r.ow_bip32String}),exports.ow_txOutputAddress=e.default.object.exactShape({address:r.ow_hashString,satoshis:exports.ow_balance}),exports.ow_txOutputScript=e.default.object.exactShape({scriptHex:r.ow_hexString,satoshis:exports.ow_balance}),exports.ow_txOutputScriptExtened=e.default.object.exactShape({script:e.default.optional.number.inRange(0,o.__LENGTH-1),path:r.ow_bip32String,satoshis:exports.ow_balance}),exports.ow_txOutput=e.default.object.exactShape({to:e.default.any(exports.ow_txOutputAddress,exports.ow_txOutputScriptExtened),utxo:e.default.any(e.default.undefined,exports.ow_txOutputScriptExtened)}),exports.isOutputAddress=function(e){const t=e;if(t.address)return t},exports.isOutuptScript=function(e){const t=e;if(t.scriptHex)return t},exports.isOutuptScriptExtended=function(t){try{return(0,e.default)(t,exports.ow_txOutputScriptExtened),t}catch(e){}},function(e){e[e.BITCOIN=0]="BITCOIN",e[e.TESTNET=1]="TESTNET",e[e.LITECOIN=2]="LITECOIN",e[e.BITCOINCASH=3]="BITCOINCASH",e[e.GROESTL=4]="GROESTL",e[e.DIGIBYTE=5]="DIGIBYTE",e[e.DASH=6]="DASH",e[e.DOGECOIN=7]="DOGECOIN",e[e.__LENGTH=8]="__LENGTH"}(p=exports.CoinType||(exports.CoinType={})),exports.coinmap=Object.freeze([Object.freeze(t.bitcoin),Object.freeze(t.testnet),Object.freeze(t.litecoin),Object.freeze(t.bitcoincash),Object.freeze(t.groestl),Object.freeze(t.digibyte),Object.freeze(t.dash),Object.freeze(t.dogecoin)]),exports.btcCoinTypes=Object.freeze(exports.coinmap.map((e=>Object.freeze(e.coinType)))),exports.btcPurposes=Object.freeze([Object.freeze(44),Object.freeze(49),Object.freeze(84)]),exports.ow_AddressOption=e.default.object.exactShape({coin:e.default.optional.number.inRange(0,p.__LENGTH-1),script:e.default.optional.number.inRange(0,o.__LENGTH-1)});
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ow_AddressOption=exports.btcPurposes=exports.btcCoinTypes=exports.coinmap=exports.CoinType=exports.isOutuptScriptExtended=exports.isOutuptScript=exports.isOutputAddress=exports.ow_txOutput=exports.ow_txOutputScriptExtened=exports.ow_txOutputScript=exports.ow_txOutputAddress=exports.ow_txInput=exports.ow_balance=exports.ScriptType=exports.OPCODES=void 0;const e=require("ow"),t=require("./coindef"),r=require("@secux/utility");var o,p,s=require("./coindef");Object.defineProperty(exports,"OPCODES",{enumerable:!0,get:function(){return s.OPCODES}}),function(e){e[e.P2PKH=0]="P2PKH",e[e.P2WPKH=1]="P2WPKH",e[e.P2SH_P2PKH=2]="P2SH_P2PKH",e[e.P2SH_P2WPKH=3]="P2SH_P2WPKH",e[e.__LENGTH=4]="__LENGTH"}(o=exports.ScriptType||(exports.ScriptType={})),exports.ow_balance=e.default.number.integer.positive.lessThanOrEqual(9007199254740991),exports.ow_txInput=e.default.object.exactShape({hash:r.ow_hashString,vout:e.default.number.greaterThanOrEqual(0),txHex:e.default.any(e.default.undefined,r.ow_hexString),script:e.default.optional.number.inRange(0,o.__LENGTH-1),satoshis:exports.ow_balance,path:r.ow_bip32String}),exports.ow_txOutputAddress=e.default.object.exactShape({address:r.ow_hashString,satoshis:exports.ow_balance}),exports.ow_txOutputScript=e.default.object.exactShape({scriptHex:r.ow_hexString,satoshis:exports.ow_balance}),exports.ow_txOutputScriptExtened=e.default.object.exactShape({script:e.default.optional.number.inRange(0,o.__LENGTH-1),path:r.ow_bip32String,satoshis:exports.ow_balance}),exports.ow_txOutput=e.default.object.exactShape({to:e.default.any(exports.ow_txOutputAddress,exports.ow_txOutputScriptExtened),utxo:e.default.any(exports.ow_txOutputScriptExtened,e.default.undefined)}),exports.isOutputAddress=function(e){const t=e;if(t.address)return t},exports.isOutuptScript=function(e){const t=e;if(t.scriptHex)return t},exports.isOutuptScriptExtended=function(t){try{return(0,e.default)(t,exports.ow_txOutputScriptExtened),t}catch(e){}},function(e){e[e.BITCOIN=0]="BITCOIN",e[e.TESTNET=1]="TESTNET",e[e.LITECOIN=2]="LITECOIN",e[e.BITCOINCASH=3]="BITCOINCASH",e[e.GROESTL=4]="GROESTL",e[e.DIGIBYTE=5]="DIGIBYTE",e[e.DASH=6]="DASH",e[e.DOGECOIN=7]="DOGECOIN",e[e.__LENGTH=8]="__LENGTH"}(p=exports.CoinType||(exports.CoinType={})),exports.coinmap=Object.freeze([Object.freeze(t.bitcoin),Object.freeze(t.testnet),Object.freeze(t.litecoin),Object.freeze(t.bitcoincash),Object.freeze(t.groestl),Object.freeze(t.digibyte),Object.freeze(t.dash),Object.freeze(t.dogecoin)]),exports.btcCoinTypes=Object.freeze(exports.coinmap.map((e=>Object.freeze(e.coinType)))),exports.btcPurposes=Object.freeze([Object.freeze(44),Object.freeze(49),Object.freeze(84)]),exports.ow_AddressOption=e.default.object.exactShape({coin:e.default.optional.number.inRange(0,p.__LENGTH-1),script:e.default.optional.number.inRange(0,o.__LENGTH-1)});

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.logger=exports.Hash160=exports.CoinType=exports.PaymentBTC=void 0;const e=require("bech32"),r=require("ripemd160"),o=require("js-sha256"),t=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return t.CoinType}});const s=require("@secux/utility/lib/bs58"),i=require("@secux/transport");exports.logger=null===i.Logger||void 0===i.Logger?void 0:i.Logger.child({id:"payment"});class c{static CoinSupported(e){if(e===t.CoinType.BITCOINCASH)throw Error("Please use class PaymentBCH instead");if(e===t.CoinType.GROESTL)throw Error("Please use class PaymentGRS instead")}static p2pkh(e,r){if(this.CoinSupported(e),!r.publickey&&!r.hash)throw Error("Invalid Parameters");if(r.publickey&&r.hash)throw Error("Invalid Parameters");const o=r.hash?r.hash:n(r.publickey);null===exports.logger||void 0===exports.logger||exports.logger.info(`publickey hash: ${o.toString("hex")}`);const s=t.coinmap[e],i=this.bs58check.encode(o,Buffer.from([s.pubKeyHash])),c=Buffer.from([t.OPCODES.OP_DUP,t.OPCODES.OP_HASH160,20]),h=Buffer.from([t.OPCODES.OP_EQUALVERIFY,t.OPCODES.OP_CHECKSIG]),p=Buffer.concat([c,o,h]),u=n(p);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${u.toString("hex")}`),{address:i,scriptPublickey:p,redeemHash:u}}static p2sh(e,r){this.CoinSupported(e);let o=t.coinmap[e];const s=this.bs58check.encode(r,Buffer.from([o.scriptHash])),i=Buffer.from([t.OPCODES.OP_HASH160,20]),c=Buffer.from([t.OPCODES.OP_EQUAL]);return{address:s,scriptPublickey:Buffer.concat([i,r,c])}}static p2wpkh(r,o){if(this.CoinSupported(r),!o.publickey&&!o.hash)throw Error("Invalid Parameters");if(o.publickey&&o.hash)throw Error("Invalid Parameters");const s=o.hash?o.hash:n(o.publickey);null===exports.logger||void 0===exports.logger||exports.logger.info(`publickey hash: ${s.toString("hex")}`);let i=t.coinmap[r];const c=e.bech32.toWords(s);c.unshift(0);const h=e.bech32.encode(i.bech32,c),p=Buffer.from([t.OPCODES.OP_0,20]),u=Buffer.concat([p,s]),a=n(u);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${a.toString("hex")}`),{address:h,scriptPublickey:u,redeemHash:a}}static p2ms(e,r){if(e<=0)throw Error('Invalid paramter "m"');e+=t.OPCODES.OP_INT_BASE;const o=r.length+t.OPCODES.OP_INT_BASE,s=Buffer.concat(r),i=Buffer.concat([Buffer.from([e]),s,Buffer.from([o]),Buffer.from([t.OPCODES.OP_CHECKMULTISIG])]),c=n(i);null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${c.toString("hex")}`);const h=Buffer.from([t.OPCODES.OP_HASH160,20]),p=Buffer.from([t.OPCODES.OP_EQUAL]);return{redeem:i,scriptPubicKey:Buffer.concat([h,c,p])}}static decode(r,o){let s;try{s=this.bs58check.decode(o)}catch(t){const i=e.bech32.decode(o);i.words.shift();return s=Buffer.from(e.bech32.fromWords(i.words)),null===exports.logger||void 0===exports.logger||exports.logger.debug(`bech32 address: ${o}\nbech32 decoded: ${s.toString("hex")}`),c.p2wpkh(r,{hash:s}).scriptPublickey}const i=s[0],n=s.slice(1),h=t.coinmap[r];if(i===h.scriptHash)return c.p2sh(r,n).scriptPublickey;if(i===h.pubKeyHash)return c.p2pkh(r,{hash:n}).scriptPublickey;throw Error("Invalid Output ScriptPubKey")}}function n(e){const t=Buffer.from(o.sha256.update(e).digest());return(new r).update(t).digest()}exports.PaymentBTC=c,c.bs58check=new s.bs58Check((function(e){const r=o.sha256.update(e).digest(),t=o.sha256.update(r).digest();return Buffer.from(t)})),exports.Hash160=n;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.logger=exports.Hash160=exports.CoinType=exports.PaymentBTC=void 0;const e=require("bech32"),r=require("ripemd160"),o=require("js-sha256"),t=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return t.CoinType}});const s=require("@secux/utility/lib/bs58"),i=require("@secux/utility");exports.logger=null===i.Logger||void 0===i.Logger?void 0:i.Logger.child({id:"payment"});class c{static CoinSupported(e){if(e===t.CoinType.BITCOINCASH)throw Error("Please use class PaymentBCH instead");if(e===t.CoinType.GROESTL)throw Error("Please use class PaymentGRS instead")}static p2pkh(e,r){if(this.CoinSupported(e),!r.publickey&&!r.hash)throw Error("Invalid Parameters");if(r.publickey&&r.hash)throw Error("Invalid Parameters");const o=r.hash?r.hash:n(r.publickey);null===exports.logger||void 0===exports.logger||exports.logger.info(`publickey hash: ${o.toString("hex")}`);const s=t.coinmap[e],i=this.bs58check.encode(o,Buffer.from([s.pubKeyHash])),c=Buffer.from([t.OPCODES.OP_DUP,t.OPCODES.OP_HASH160,20]),u=Buffer.from([t.OPCODES.OP_EQUALVERIFY,t.OPCODES.OP_CHECKSIG]),h=Buffer.concat([c,o,u]),p=n(h);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${p.toString("hex")}`),{address:i,scriptPublickey:h,redeemHash:p}}static p2sh(e,r){this.CoinSupported(e);let o=t.coinmap[e];const s=this.bs58check.encode(r,Buffer.from([o.scriptHash])),i=Buffer.from([t.OPCODES.OP_HASH160,20]),c=Buffer.from([t.OPCODES.OP_EQUAL]);return{address:s,scriptPublickey:Buffer.concat([i,r,c])}}static p2wpkh(r,o){if(this.CoinSupported(r),!o.publickey&&!o.hash)throw Error("Invalid Parameters");if(o.publickey&&o.hash)throw Error("Invalid Parameters");const s=o.hash?o.hash:n(o.publickey);null===exports.logger||void 0===exports.logger||exports.logger.info(`publickey hash: ${s.toString("hex")}`);let i=t.coinmap[r];const c=e.bech32.toWords(s);c.unshift(0);const u=e.bech32.encode(i.bech32,c),h=Buffer.from([t.OPCODES.OP_0,20]),p=Buffer.concat([h,s]),a=n(p);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${a.toString("hex")}`),{address:u,scriptPublickey:p,redeemHash:a}}static p2ms(e,r){if(e<=0)throw Error('Invalid paramter "m"');e+=t.OPCODES.OP_INT_BASE;const o=r.length+t.OPCODES.OP_INT_BASE,s=Buffer.concat(r),i=Buffer.concat([Buffer.from([e]),s,Buffer.from([o]),Buffer.from([t.OPCODES.OP_CHECKMULTISIG])]),c=n(i);null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${c.toString("hex")}`);const u=Buffer.from([t.OPCODES.OP_HASH160,20]),h=Buffer.from([t.OPCODES.OP_EQUAL]);return{redeem:i,scriptPubicKey:Buffer.concat([u,c,h])}}static decode(r,o){let s;try{s=this.bs58check.decode(o)}catch(t){const i=e.bech32.decode(o);i.words.shift();return s=Buffer.from(e.bech32.fromWords(i.words)),null===exports.logger||void 0===exports.logger||exports.logger.debug(`bech32 address: ${o}\nbech32 decoded: ${s.toString("hex")}`),c.p2wpkh(r,{hash:s}).scriptPublickey}const i=s[0],n=s.slice(1),u=t.coinmap[r];if(i===u.scriptHash)return c.p2sh(r,n).scriptPublickey;if(i===u.pubKeyHash)return c.p2pkh(r,{hash:n}).scriptPublickey;throw Error("Invalid Output ScriptPubKey")}}function n(e){const t=Buffer.from(o.sha256.update(e).digest());return(new r).update(t).digest()}exports.PaymentBTC=c,c.bs58check=new s.bs58Check((function(e){const r=o.sha256.update(e).digest(),t=o.sha256.update(r).digest();return Buffer.from(t)})),exports.Hash160=n;

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

"use strict";var t,i,e,s,r,n,o,a,p,u,h,c,f,l=this&&this.__awaiter||function(t,i,e,s){return new(e||(e=Promise))((function(r,n){function o(t){try{p(s.next(t))}catch(t){n(t)}}function a(t){try{p(s.throw(t))}catch(t){n(t)}}function p(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(o,a)}p((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"),w=require("bip174/src/lib/utils"),y=require("secp256k1/elliptic"),P=require("js-sha256"),v=require("./script"),m=require("@secux/utility"),b=require("@secux/transport"),k=require("./interface"),T=require("./utils"),H=require("./payment"),x=require("./transaction"),W=require("./coindef"),E=null===b.Logger||void 0===b.Logger?void 0:b.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),p.set(this,Buffer.alloc(0)),u.set(this,[]),h.set(this,{address:"",value:""}),d(this,i,c,"f"),d(this,e,new g.Psbt(new B),"f"),d(this,s,f,"f"),d(this,r,(0,T.getPayment)(S(this,s,"f")),"f"),d(this,a,{__TX:S(this,e,"f").globalMap.unsignedTx.tx},"f"),f===k.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),p={},h={},f=null!==(n=i.script)&&void 0!==n?n:(0,T.getDefaultScript)(i.path);switch(f){case k.ScriptType.P2PKH:p.witnessUtxo={script:S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:a}).scriptPublickey,value:i.satoshis};break;case k.ScriptType.P2SH_P2PKH:const t=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:a});h.redeemScript=t.scriptPublickey,p.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),t.redeemHash).scriptPublickey,value:i.satoshis};break;case k.ScriptType.P2SH_P2WPKH:const n=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:a});h.redeemScript=n.scriptPublickey,p.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),n.redeemHash).scriptPublickey,value:i.satoshis};break;case k.ScriptType.P2WPKH:p.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 "${k.ScriptType[f]}"`)}if(i.txHex){const t=x.Transaction.fromBuffer(Buffer.from(i.txHex,"hex"));if((0,T.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(p.witnessUtxo.script)||null==E||E.warn(`Input script generation error: ${r.script.toString("hex")}, got "${p.witnessUtxo.script}"`)}const l=Object.assign(Object.assign({hash:i.hash,index:i.vout},p),h);return S(this,e,"f").addInput(l),S(this,u,"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,u,f,l,g=i.satoshis;if(a=(0,k.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,T.getDefaultScript)(l);switch(o){case k.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);u=n.scriptPublickey,f=n.address;break;case k.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),u=n.scriptPublickey,f=n.address;break;case k.ScriptType.P2PKH:if(!a.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");const p=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i});u=p.scriptPublickey,f=p.address;break;case k.ScriptType.P2WPKH:if(!a.path.startsWith("m/84'/"))throw Error("P2WPKH should use m/84' path");const h=S(this,r,"f").p2wpkh(S(this,s,"f"),{publickey:i});u=h.scriptPublickey,f=h.address;break;default:throw Error(`ArgumentError: Invalid ScriptType of output#${S(this,e,"f").outputs.length}, got "${k.ScriptType[o]}"`)}}else if(a=(0,k.isOutputAddress)(i))u=S(this,r,"f").decode(S(this,s,"f"),a.address),f=a.address;else{if(!(a=(0,k.isOutuptScript)(i)))throw Error("Invalid parameter of output");u=Buffer.from(a.scriptHex,"hex")}return 0===S(this,e,"f").outputs.length?(d(this,p,Buffer.concat([S(this,p,"f"),o,Buffer.from([u.length]),u]),"f"),S(this,h,"f").address=f,S(this,h,"f").value=(1e-8*g).toString()):d(this,p,Buffer.concat([S(this,p,"f"),o,(0,m.buildPathBuffer)(l).pathBuffer]),"f"),S(this,e,"f").addOutput({script:u,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,p,Buffer.concat([Buffer.from([S(this,e,"f").outputs.length]),S(this,p,"f")]),"f");const r=S(this,e,"f").inputs.map(((t,i)=>{const r=S(this,u,"f")[i],{data:n}=function(t,i,e,s,r){const n=r.__TX,o=e.sighashType||x.Transaction.SIGHASH_ALL,a=(0,T.getSerializer)(t);let p;if(!e.witnessUtxo)throw new Error("Need a Utxo input item for signing");p=e.witnessUtxo;const u=function(t,i,e,s){let r;switch(i){case k.ScriptType.P2SH_P2PKH:case k.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}(p.script,s,e.redeemScript,e.witnessScript);let h;switch(null==E||E.debug(`input #${i} script type: ${k.ScriptType[s]}`),null==E||E.debug(`script: ${u.toString("hex")}`),s){case k.ScriptType.P2WPKH:case k.ScriptType.P2SH_P2WPKH:case k.ScriptType.P2SH_P2PKH:null==E||E.debug(k.ScriptType[s]);const e=(0,T.getPayment)(t).p2pkh(t,{hash:u.slice(2)}).scriptPublickey;h=a.dataForWitnessV0(n,i,e,p.value,o);break;default:t===k.CoinType.BITCOINCASH?(null==E||E.debug("bch using bip143"),h=a.dataForWitnessV0(n,i,u,p.value,64|o)):(null==E||E.debug("non-segwit"),h=a.dataForSignature(n,i,u,o))}return{script:u,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")!==k.CoinType.BITCOINCASH&&r===k.ScriptType.P2PKH?{data:n,confirm:Buffer.alloc(0)}:{data:n,confirm:S(this,p,"f")}}));for(let t=0;t<r.length;t++)null==E||E.debug(`tx data [${t}]: ${r[t].data.toString("hex")}`),null==E||E.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 n=h[i];n.trimZero();const a=Buffer.concat([n.r,n.s]),p=S(this,s,"f")===k.CoinType.GROESTL?Buffer.from(P.sha256.update(r[i].data).digest()):Buffer.from(P.sha256.update(P.sha256.update(r[i].data).digest()).digest()),u=yield S(this,t,"m",c).call(this,S(this,o,"f")[i]);if(!y.ecdsaVerify(a,p,u))throw Error(`Signature Error #${i}`);let l=S(this,e,"f").inputs[i].sighashType||x.Transaction.SIGHASH_ALL;S(this,s,"f")===k.CoinType.BITCOINCASH&&(l|=64);const d=[{pubkey:u,signature:v.encode(a,l)}];S(this,e,"f").updateInput(i,{partialSig:d}),f.push(d[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 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 k.ScriptType.P2PKH:e=v.compile([r,n]);break;case k.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 k.ScriptType.P2SH_P2WPKH:s=(0,T.witnessStackToScriptWitness)([r,n]),e=(()=>{const t=Buffer.alloc(0),i=(0,H.Hash160)(n),e=v.compile([W.OPCODES.OP_0,i]);return v.compile([].concat(v.decompile(t),e))})();break;case k.ScriptType.P2WPKH:s=(0,T.witnessStackToScriptWitness)([r,n])}return{finalScriptSig:e,finalScriptWitness:s}}(S(this,u,"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,T.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,p=new WeakMap,u=new WeakMap,h=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 B{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){f.set(this,void 0),d(this,f,x.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,p,h,u,c,f,l=this&&this.__awaiter||function(t,i,e,s){return new(e||(e=Promise))((function(r,n){function o(t){try{p(s.next(t))}catch(t){n(t)}}function a(t){try{p(s.throw(t))}catch(t){n(t)}}function p(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(o,a)}p((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"),w=require("bip174/src/lib/utils"),y=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),p.set(this,Buffer.alloc(0)),h.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),p={},u={},f=null!==(n=i.script)&&void 0!==n?n:(0,k.getDefaultScript)(i.path);switch(f){case b.ScriptType.P2PKH:p.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,p.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,p.witnessUtxo={script:S(this,r,"f").p2sh(S(this,s,"f"),n.redeemHash).scriptPublickey,value:i.satoshis};break;case b.ScriptType.P2WPKH:p.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(p.witnessUtxo.script)||null==W||W.warn(`Input script generation error: ${r.script.toString("hex")}, got "${p.witnessUtxo.script}"`)}const l=Object.assign(Object.assign({hash:i.hash,index:i.vout},p),u);return S(this,e,"f").addInput(l),S(this,h,"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,h,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);h=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),h=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 p=S(this,r,"f").p2pkh(S(this,s,"f"),{publickey:i});h=p.scriptPublickey,f=p.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});h=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))h=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");h=Buffer.from(a.scriptHex,"hex")}return 0===S(this,e,"f").outputs.length?(d(this,p,Buffer.concat([S(this,p,"f"),o,Buffer.from([h.length]),h]),"f"),S(this,u,"f").address=f,S(this,u,"f").value=(1e-8*g).toString()):d(this,p,Buffer.concat([S(this,p,"f"),o,(0,m.buildPathBuffer)(l).pathBuffer]),"f"),S(this,e,"f").addOutput({script:h,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,p,Buffer.concat([Buffer.from([S(this,e,"f").outputs.length]),S(this,p,"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||H.Transaction.SIGHASH_ALL,a=(0,k.getSerializer)(t);let p;if(!e.witnessUtxo)throw new Error("Need a Utxo input item for signing");p=e.witnessUtxo;const h=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}(p.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: ${h.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:h.slice(2)}).scriptPublickey;u=a.dataForWitnessV0(n,i,e,p.value,o);break;default:t===b.CoinType.BITCOINCASH?(null==W||W.debug("bch using bip143"),u=a.dataForWitnessV0(n,i,h,p.value,64|o)):(null==W||W.debug("non-segwit"),u=a.dataForSignature(n,i,h,o))}return{script:h,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,p,"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 n=u[i];n.trimZero();const a=Buffer.concat([n.r,n.s]),p=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()),h=yield S(this,t,"m",c).call(this,S(this,o,"f")[i]);if(!y.ecdsaVerify(a,p,h))throw Error(`Signature Error #${i}`);let l=S(this,e,"f").inputs[i].sighashType||H.Transaction.SIGHASH_ALL;S(this,s,"f")===b.CoinType.BITCOINCASH&&(l|=64);const d=[{pubkey:h,signature:v.encode(a,l)}];S(this,e,"f").updateInput(i,{partialSig:d}),f.push(d[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 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,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,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,p=new WeakMap,h=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;

@@ -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"),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}}}
"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/utility"),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+u.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+=u.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=u.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 u{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-rc5",
"version": "2.0.0-rc6",
"description": "SecuX Hardware Wallet BTC API",

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

"dependencies": {
"@secux/transport": "~2.1",
"@secux/utility": "^2.0.4",
"@secux/transport": "^2.1.3",
"@secux/utility": "^2.0.6",
"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