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 3.1.2 to 3.1.3

2

lib/app-btc.js

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

"use strict";var e,t=this&&this.__decorate||function(e,t,r,i){var o,u=arguments.length,n=u<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,r,i);else for(var c=e.length-1;c>=0;c--)(o=e[c])&&(n=(u<3?o(n):u>3?o(t,r,n):o(t,r))||n);return u>3&&n&&Object.defineProperty(t,r,n),n},r=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))((function(o,u){function n(e){try{s(i.next(e))}catch(e){u(e)}}function c(e){try{s(i.throw(e))}catch(e){u(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(n,c)}s((i=i.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxBTC=exports.ScriptType=exports.CoinType=void 0;const i=require("secp256k1/elliptic"),o=require("varuint-bitcoin"),u=require("@secux/utility"),n=require("@secux/utility/lib/xpub"),c=require("ow"),s=require("@secux/protocol-transaction"),p=require("@secux/utility/lib/communication"),a=require("@secux/protocol-transaction/lib/interface"),l=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return l.CoinType}}),Object.defineProperty(exports,"ScriptType",{enumerable:!0,get:function(){return l.ScriptType}});const d=require("./psbt"),f=require("./utils"),y=require("@secux/transport");let v=e=class{static addressConvert(e,t){const r=(0,f.getPublickey)(e);(0,c.default)(t,c.default.any(l.ow_path,l.ow_PathObject));const o=Buffer.from(i.publicKeyConvert(r,!0)),u="string"==typeof t?(0,f.getCoinType)(t):t.coin,n="string"==typeof t?(0,f.getDefaultScript)(t):t.script,s=(0,f.getPayment)(u);switch(n){case l.ScriptType.P2SH_P2WPKH:const e=s.p2wpkh(u,{publickey:o});return s.p2sh(u,e.redeemHash).address;case l.ScriptType.P2SH_P2PKH:const t=s.p2pkh(u,{publickey:o});return s.p2sh(u,t.redeemHash).address;case l.ScriptType.P2PKH:return s.p2pkh(u,{publickey:o}).address;case l.ScriptType.P2WPKH:return s.p2wpkh(u,{publickey:o}).address;case l.ScriptType.P2TR:return s.p2tr(u,{publickey:o}).address;default:throw Error(`Invalid or unsupported ScriptType, got ${n} of ${u}`)}}static resolveAddress(t,r){const i=e.resolvePublickey(t);return e.addressConvert(i,r)}static preparePublickey(e,t){var r;(0,c.default)(e,l.ow_path),t&&(0,c.default)(t,l.ow_AddressOption);const i=null!==(r=null==t?void 0:t.coin)&&void 0!==r?r:(0,f.getCoinType)(e),o=l.coinmap[i].coinType,n=void 0===(null==t?void 0:t.script)?void 0:(0,f.getPurpose)(null==t?void 0:t.script);return(0,c.default)(e,u.ow_strictPath(o,n)),s.SecuxTransactionTool.getPublickey(e,a.EllipticCurve.SECP256K1)}static resolvePublickey(e){const t=s.SecuxTransactionTool.resolvePublickey(e,a.EllipticCurve.SECP256K1,!0);return Buffer.from(t,"base64").toString("hex")}static prepareXPublickey(e){return(0,c.default)(e,l.ow_accountPath),s.SecuxTransactionTool.getXPublickey(e)}static resolveXPublickey(e,t){return(0,c.default)(t,l.ow_accountPath),s.SecuxTransactionTool.resolveXPublickey(e,t)}static prepareSign(e,t,r){var i;(0,c.default)(e,c.default.array.ofType(l.ow_txInput).minLength(1)),(0,c.default)(r,c.default.any(c.default.undefined,l.ow_SignOption));const o=null!==(i=null==r?void 0:r.coin)&&void 0!==i?i:(0,f.getCoinType)(e[0].path);(0,c.default)(t,l.ow_txOutput),e.map((e=>{const t=e.script?(0,f.getPurpose)(e.script):l.btcPurposes;(0,c.default)(e.path,u.ow_strictPath(l.coinmap[o].coinType,t))}));let n=(0,l.isOutuptScriptExtended)(t.to);if(n){const e=n.script?(0,f.getPurpose)(n.script):l.btcPurposes;(0,c.default)(n.path,u.ow_strictPath(l.coinmap[o].coinType,e))}if(t.utxo){const e=t.utxo.script?(0,f.getPurpose)(t.utxo.script):l.btcPurposes;(0,c.default)(t.utxo.path,u.ow_strictPath(l.coinmap[o].coinType,e))}const s=new d.SecuxPsbt(o);return s.AddInputs(e),s.AddOutputs(t.utxo?[t.to,t.utxo]:[t.to]),s.PrepareSign(null==r?void 0:r.feeRate)}static resolveSignatureList(e){return s.SecuxTransactionTool.resolveSignatureList(e).map((e=>Buffer.from(e,"base64"))).map((e=>u.Signature.fromSignature(e))).map((e=>Buffer.concat([e.r,e.s]).toString("hex")))}static resolveTransaction(t,r,i,o=l.CoinType.BITCOIN){(0,c.default)(t,p.ow_communicationData),(0,c.default)(r,u.ow_hexString),(0,c.default)(i,c.default.array.ofType(c.default.any(u.ow_hexString,c.default.buffer))),(0,c.default)(o,c.default.number.inRange(0,l.CoinType.__LENGTH-1));const n=e.resolveSignatureList(t).map((e=>Buffer.from(e,"hex"))),s=i.map((e=>(0,f.getPublickey)(e)));return d.SecuxPsbt.FromBuffer(Buffer.from(r,"hex"),o).appendSignature(n,s).finalizeAllInputs().extractTransaction().toHex()}static getAddress(t,i){var o,u;return r(this,void 0,void 0,(function*(){const r=e.prepareAddress(t,i),n=yield this.Exchange((0,p.getBuffer)(r));return e.resolveAddress(n,{coin:null!==(o=null==i?void 0:i.coin)&&void 0!==o?o:(0,f.getCoinType)(t),script:null!==(u=null==i?void 0:i.script)&&void 0!==u?u:(0,f.getDefaultScript)(t)})}))}static getPublickey(t,i){return r(this,void 0,void 0,(function*(){const r=e.preparePublickey(t,i),o=yield this.Exchange((0,p.getBuffer)(r));return e.resolvePublickey(o)}))}static getXPublickey(t){return r(this,void 0,void 0,(function*(){const r=e.prepareXPublickey(t),i=yield this.Exchange((0,p.getBuffer)(r));return e.resolveXPublickey(i,t)}))}static sign(t,i,o){var u,n;return r(this,void 0,void 0,(function*(){const c={},s=t=>r(this,void 0,void 0,(function*(){if(void 0!==c[t])return c[t];const r=yield e.getPublickey.call(this,t,{coin:a}),i=Buffer.from(r,"hex");return c[t]=i,i})),a=null!==(u=null==o?void 0:o.coin)&&void 0!==u?u:(0,f.getCoinType)(t[0].path);for(const e of t)void 0===e.publickey&&(e.publickey=yield s(e.path));i.to.path&&void 0===i.to.publickey&&(i.to.publickey=yield s(i.to.path)),(null===(n=i.utxo)||void 0===n?void 0:n.path)&&void 0===i.utxo.publickey&&(i.utxo.publickey=yield s(i.utxo.path));const{commandData:l,rawTx:d}=e.prepareSign(t,i,{coin:a,feeRate:null==o?void 0:o.feeRate}),y=yield this.Exchange((0,p.getBuffer)(l));return{raw_tx:e.resolveTransaction(y,d,t.map((e=>e.publickey)),a)}}))}static deriveAddress(t,r,i,o){var u,s;(0,c.default)(r,c.default.number.uint8),(0,c.default)(i,c.default.number.uint8),(0,c.default)(o,c.default.any(c.default.undefined,l.ow_AddressOption));const p=(0,n.decodeXPUB)(t);if(3!==p.depth)throw Error(`ArgumentError: expect depth from xpub is 3, but got ${p.depth}`);if(null==o?void 0:o.script)if(o.script in[l.ScriptType.P2PKH,l.ScriptType.P2SH_P2PKH,l.ScriptType.P2SH_P2WPKH,l.ScriptType.P2WPKH]){const e=(0,f.getPurpose)(null==o?void 0:o.script);if(p.purpose!==e)throw Error(`ArgumentError: expect purpose from xpub is ${e}, but got ${p.purpose}`)}else{if(44!==p.purpose)throw Error(`ArgumentError: expect purpose from xpub is 44, but got ${p.purpose}`);p.purpose,(0,f.getPurpose)(null==o?void 0:o.script)}const{publickey:a}=(0,n.deriveKey)(p.publickey,p.chaincode,[r,i]),d=null!==(u=null==o?void 0:o.coin)&&void 0!==u?u:l.CoinType.BITCOIN,y=null!==(s=null==o?void 0:o.script)&&void 0!==s?s:(0,f.getDefaultScript)(`m/${p.purpose}'`);return e.addressConvert(a,{coin:d,script:y})}static getVirtualSize(e,t){const r=8+o.encodingLength(e.length)+o.encodingLength(t.length)+e.reduce(((e,t)=>e+40+(0,f.sliceSize)((0,f.getInScriptSize)(t))),0)+t.reduce(((e,t)=>e+8+(0,f.sliceSize)((0,f.getOutScriptSize)(t))),0),i=e.map((e=>(0,f.getWitnessSize)(e)));return(4*r+(i.some((e=>0!==e.length))?2+i.reduce(((e,t)=>e+(0,f.vectorSize)(t)),0):0))/4}};v.prepareAddress=e.preparePublickey,v=e=t([(0,y.staticImplements)()],v),exports.SecuxBTC=v,(0,y.loadPlugin)(v,"SecuxBTC");
"use strict";var e=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))((function(o,u){function n(e){try{s(i.next(e))}catch(e){u(e)}}function c(e){try{s(i.throw(e))}catch(e){u(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(n,c)}s((i=i.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxBTC=exports.ScriptType=exports.CoinType=void 0;const t=require("secp256k1/elliptic"),r=require("varuint-bitcoin"),i=require("@secux/utility"),o=require("@secux/utility/lib/xpub"),u=require("ow"),n=require("@secux/protocol-transaction"),c=require("@secux/utility/lib/communication"),s=require("@secux/protocol-transaction/lib/interface"),p=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return p.CoinType}}),Object.defineProperty(exports,"ScriptType",{enumerable:!0,get:function(){return p.ScriptType}});const a=require("./psbt"),l=require("./utils"),d=require("@secux/transport");class f{static addressConvert(e,r){const i=(0,l.getPublickey)(e);(0,u.default)(r,u.default.any(p.ow_path,p.ow_PathObject));const o=Buffer.from(t.publicKeyConvert(i,!0)),n="string"==typeof r?(0,l.getCoinType)(r):r.coin,c="string"==typeof r?(0,l.getDefaultScript)(r):r.script,s=(0,l.getPayment)(n);switch(c){case p.ScriptType.P2SH_P2WPKH:const e=s.p2wpkh(n,{publickey:o});return s.p2sh(n,e.redeemHash).address;case p.ScriptType.P2SH_P2PKH:const t=s.p2pkh(n,{publickey:o});return s.p2sh(n,t.redeemHash).address;case p.ScriptType.P2PKH:return s.p2pkh(n,{publickey:o}).address;case p.ScriptType.P2WPKH:return s.p2wpkh(n,{publickey:o}).address;case p.ScriptType.P2TR:return s.p2tr(n,{publickey:o}).address;default:throw Error(`Invalid or unsupported ScriptType, got ${c} of ${n}`)}}static resolveAddress(e,t){const r=f.resolvePublickey(e);return f.addressConvert(r,t)}static preparePublickey(e,t){var r;(0,u.default)(e,p.ow_path),t&&(0,u.default)(t,p.ow_AddressOption);const o=null!==(r=null==t?void 0:t.coin)&&void 0!==r?r:(0,l.getCoinType)(e),c=p.coinmap[o].coinType,a=void 0===(null==t?void 0:t.script)?void 0:(0,l.getPurpose)(null==t?void 0:t.script);return(0,u.default)(e,i.ow_strictPath(c,a)),n.SecuxTransactionTool.getPublickey(e,s.EllipticCurve.SECP256K1)}static resolvePublickey(e){const t=n.SecuxTransactionTool.resolvePublickey(e,s.EllipticCurve.SECP256K1,!0);return Buffer.from(t,"base64").toString("hex")}static prepareXPublickey(e){return(0,u.default)(e,p.ow_accountPath),n.SecuxTransactionTool.getXPublickey(e)}static resolveXPublickey(e,t){return(0,u.default)(t,p.ow_accountPath),n.SecuxTransactionTool.resolveXPublickey(e,t)}static prepareSign(e,t,r){var o;(0,u.default)(e,u.default.array.ofType(p.ow_txInput).minLength(1)),(0,u.default)(r,u.default.any(u.default.undefined,p.ow_SignOption));const n=null!==(o=null==r?void 0:r.coin)&&void 0!==o?o:(0,l.getCoinType)(e[0].path);(0,u.default)(t,p.ow_txOutput),e.map((e=>{const t=e.script?(0,l.getPurpose)(e.script):p.btcPurposes;(0,u.default)(e.path,i.ow_strictPath(p.coinmap[n].coinType,t))}));let c=(0,p.isOutuptScriptExtended)(t.to);if(c){const e=c.script?(0,l.getPurpose)(c.script):p.btcPurposes;(0,u.default)(c.path,i.ow_strictPath(p.coinmap[n].coinType,e))}if(t.utxo){const e=t.utxo.script?(0,l.getPurpose)(t.utxo.script):p.btcPurposes;(0,u.default)(t.utxo.path,i.ow_strictPath(p.coinmap[n].coinType,e))}const s=new a.SecuxPsbt(n);return s.AddInputs(e),s.AddOutputs(t.utxo?[t.to,t.utxo]:[t.to]),s.PrepareSign(null==r?void 0:r.feeRate)}static resolveSignatureList(e){return n.SecuxTransactionTool.resolveSignatureList(e).map((e=>Buffer.from(e,"base64"))).map((e=>i.Signature.fromSignature(e))).map((e=>Buffer.concat([e.r,e.s]).toString("hex")))}static resolveTransaction(e,t,r,o=p.CoinType.BITCOIN){(0,u.default)(e,c.ow_communicationData),(0,u.default)(t,i.ow_hexString),(0,u.default)(r,u.default.array.ofType(u.default.any(i.ow_hexString,u.default.buffer))),(0,u.default)(o,u.default.number.inRange(0,p.CoinType.__LENGTH-1));const n=f.resolveSignatureList(e).map((e=>Buffer.from(e,"hex"))),s=r.map((e=>(0,l.getPublickey)(e)));return a.SecuxPsbt.FromBuffer(Buffer.from(t,"hex"),o).appendSignature(n,s).finalizeAllInputs().extractTransaction().toHex()}static getAddress(t,r){var i,o;return e(this,void 0,void 0,(function*(){const e=f.prepareAddress(t,r),u=yield this.Exchange((0,c.getBuffer)(e));return f.resolveAddress(u,{coin:null!==(i=null==r?void 0:r.coin)&&void 0!==i?i:(0,l.getCoinType)(t),script:null!==(o=null==r?void 0:r.script)&&void 0!==o?o:(0,l.getDefaultScript)(t)})}))}static getPublickey(t,r){return e(this,void 0,void 0,(function*(){const e=f.preparePublickey(t,r),i=yield this.Exchange((0,c.getBuffer)(e));return f.resolvePublickey(i)}))}static getXPublickey(t){return e(this,void 0,void 0,(function*(){const e=f.prepareXPublickey(t),r=yield this.Exchange((0,c.getBuffer)(e));return f.resolveXPublickey(r,t)}))}static sign(t,r,i){var o,u;return e(this,void 0,void 0,(function*(){const n={},s=t=>e(this,void 0,void 0,(function*(){if(void 0!==n[t])return n[t];const e=yield f.getPublickey.call(this,t,{coin:p}),r=Buffer.from(e,"hex");return n[t]=r,r})),p=null!==(o=null==i?void 0:i.coin)&&void 0!==o?o:(0,l.getCoinType)(t[0].path);for(const e of t)void 0===e.publickey&&(e.publickey=yield s(e.path));r.to.path&&void 0===r.to.publickey&&(r.to.publickey=yield s(r.to.path)),(null===(u=r.utxo)||void 0===u?void 0:u.path)&&void 0===r.utxo.publickey&&(r.utxo.publickey=yield s(r.utxo.path));const{commandData:a,rawTx:d}=f.prepareSign(t,r,{coin:p,feeRate:null==i?void 0:i.feeRate}),y=yield this.Exchange((0,c.getBuffer)(a));return{raw_tx:f.resolveTransaction(y,d,t.map((e=>e.publickey)),p)}}))}static deriveAddress(e,t,r,i){var n,c;(0,u.default)(t,u.default.number.uint8),(0,u.default)(r,u.default.number.uint8),(0,u.default)(i,u.default.any(u.default.undefined,p.ow_AddressOption));const s=(0,o.decodeXPUB)(e);if(3!==s.depth)throw Error(`ArgumentError: expect depth from xpub is 3, but got ${s.depth}`);if(null==i?void 0:i.script)if(i.script in[p.ScriptType.P2PKH,p.ScriptType.P2SH_P2PKH,p.ScriptType.P2SH_P2WPKH,p.ScriptType.P2WPKH]){const e=(0,l.getPurpose)(null==i?void 0:i.script);if(s.purpose!==e)throw Error(`ArgumentError: expect purpose from xpub is ${e}, but got ${s.purpose}`)}else{if(44!==s.purpose)throw Error(`ArgumentError: expect purpose from xpub is 44, but got ${s.purpose}`);s.purpose,(0,l.getPurpose)(null==i?void 0:i.script)}const{publickey:a}=(0,o.deriveKey)(s.publickey,s.chaincode,[t,r]),d=null!==(n=null==i?void 0:i.coin)&&void 0!==n?n:p.CoinType.BITCOIN,y=null!==(c=null==i?void 0:i.script)&&void 0!==c?c:(0,l.getDefaultScript)(`m/${s.purpose}'`);return f.addressConvert(a,{coin:d,script:y})}static getVirtualSize(e,t){const i=8+r.encodingLength(e.length)+r.encodingLength(t.length)+e.reduce(((e,t)=>e+40+(0,l.sliceSize)((0,l.getInScriptSize)(t))),0)+t.reduce(((e,t)=>e+8+(0,l.sliceSize)((0,l.getOutScriptSize)(t))),0),o=e.map((e=>(0,l.getWitnessSize)(e)));return(4*i+(o.some((e=>0!==e.length))?2+o.reduce(((e,t)=>e+(0,l.vectorSize)(t)),0):0))/4}}exports.SecuxBTC=f,f.prepareAddress=f.preparePublickey,(0,d.loadPlugin)(f,"SecuxBTC");

@@ -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("hash.js"),t=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return t.CoinType}});const o=require("@secux/utility/lib/bs58"),s=require("@secux/utility"),i=require("./utils");exports.logger=null===s.Logger||void 0===s.Logger?void 0:s.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]),u=Buffer.concat([c,o,h]),a=n(u);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${a.toString("hex")}`),{address:i,scriptPublickey:u,redeemHash:a}}static p2sh(e,r){this.CoinSupported(e);const o=t.coinmap[e],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),u=Buffer.from([t.OPCODES.OP_0,20]),a=Buffer.concat([u,s]),p=n(a);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${p.toString("hex")}`),{address:h,scriptPublickey:a,redeemHash:p}}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]),u=Buffer.from([t.OPCODES.OP_EQUAL]);return{redeem:i,scriptPubicKey:Buffer.concat([h,c,u])}}static p2tr(r,o){if(this.CoinSupported(r),!o.publickey&&!o.hash)throw Error("Invalid Parameters");if(o.publickey&&o.hash)throw Error("Invalid Parameters");let s=o.hash;void 0===s&&(s=(0,i.toTweakedPublickey)(o.publickey));const c=t.coinmap[r],n=e.bech32.toWords(s);n.unshift(1);const h=e.bech32m.encode(c.bech32,n),u=Buffer.from([81,32]);return{address:h,scriptPublickey:Buffer.concat([u,s])}}static decode(r,o){const s=t.coinmap[r];if(s.bech32&&o.startsWith(s.bech32)){let t;if("p"===o.slice(s.bech32.length+1)[0])t=e.bech32m.decode(o);else t=e.bech32.decode(o);const i=t.words.shift();switch(i){case 0:const s=Buffer.from(e.bech32.fromWords(t.words));return null===exports.logger||void 0===exports.logger||exports.logger.debug(`bech32 address: ${o}\nbech32 decoded: ${s.toString("hex")}`),this.p2wpkh(r,{hash:s}).scriptPublickey;case 1:const c=Buffer.from(e.bech32m.fromWords(t.words));return null===exports.logger||void 0===exports.logger||exports.logger.debug(`bech32m address: ${o}\nbech32m decoded: ${c.toString("hex")}`),this.p2tr(r,{hash:c}).scriptPublickey;default:throw Error(`ArgumentError: unsupported witness version, got "${i}" from address "${o}"`)}}const i=this.bs58check.decode(o),c=i[0],n=i.slice(1);if(c===s.scriptHash)return this.p2sh(r,n).scriptPublickey;if(c===s.pubKeyHash)return this.p2pkh(r,{hash:n}).scriptPublickey;throw Error("Invalid Output ScriptPubKey")}static classify(e){if(this.isP2WPKH(e))return t.ScriptType.P2WPKH;if(this.isP2PKH(e))return t.ScriptType.P2PKH;if(this.isP2TR(e))return t.ScriptType.P2TR;throw Error(`non-standard script: ${e.toString("hex")}`)}static isP2PKH(e){return 25===e.length&&e[0]===t.OPCODES.OP_DUP&&e[1]===t.OPCODES.OP_HASH160&&20===e[2]&&e[23]===t.OPCODES.OP_EQUALVERIFY&&e[24]===t.OPCODES.OP_CHECKSIG}static isP2SH(e){return 23===e.length&&e[0]===t.OPCODES.OP_HASH160&&20===e[1]&&e[22]===t.OPCODES.OP_EQUAL}static isP2WPKH(e){return 22===e.length&&e[0]===t.OPCODES.OP_0&&20===e[1]}static isP2TR(e){return 34===e.length&&81===e[0]&&32===e[1]}}function n(e){const t=Buffer.from((0,r.sha256)().update(e).digest());return Buffer.from((0,r.ripemd160)().update(t).digest())}exports.PaymentBTC=c,c.bs58check=new o.bs58Check((function(e){const t=(0,r.sha256)().update(e).digest(),o=(0,r.sha256)().update(t).digest();return Buffer.from(o)})),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("hash.js"),t=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return t.CoinType}});const o=require("@secux/utility/lib/bs58"),s=require("@secux/utility"),i=require("./utils");exports.logger=null===s.Logger||void 0===s.Logger?void 0:s.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]),u=Buffer.concat([c,o,h]),a=n(u);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${a.toString("hex")}`),{address:i,scriptPublickey:u,redeemHash:a}}static p2sh(e,r){this.CoinSupported(e);const o=t.coinmap[e],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),u=Buffer.from([t.OPCODES.OP_0,20]),a=Buffer.concat([u,s]),p=n(a);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${p.toString("hex")}`),{address:h,scriptPublickey:a,redeemHash:p}}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]),u=Buffer.from([t.OPCODES.OP_EQUAL]);return{redeem:i,scriptPubicKey:Buffer.concat([h,c,u])}}static p2tr(r,o){if(this.CoinSupported(r),!o.publickey&&!o.hash)throw Error("Invalid Parameters");if(o.publickey&&o.hash)throw Error("Invalid Parameters");let s=o.hash;void 0===s&&(s=(0,i.toTweakedPublickey)(o.publickey));const c=t.coinmap[r],n=e.bech32.toWords(s);n.unshift(1);const h=e.bech32m.encode(c.bech32,n),u=Buffer.from([81,32]);return{address:h,scriptPublickey:Buffer.concat([u,s])}}static decode(r,o){const s=t.coinmap[r];if(s.bech32&&o.startsWith(s.bech32)){let t;if("p"===o.slice(s.bech32.length+1)[0])t=e.bech32m.decode(o);else t=e.bech32.decode(o);const i=t.words.shift();switch(i){case 0:const s=Buffer.from(e.bech32.fromWords(t.words));return null===exports.logger||void 0===exports.logger||exports.logger.debug(`bech32 address: ${o}\nbech32 decoded: ${s.toString("hex")}`),this.p2wpkh(r,{hash:s}).scriptPublickey;case 1:const c=Buffer.from(e.bech32m.fromWords(t.words));return null===exports.logger||void 0===exports.logger||exports.logger.debug(`bech32m address: ${o}\nbech32m decoded: ${c.toString("hex")}`),this.p2tr(r,{hash:c}).scriptPublickey;default:throw Error(`ArgumentError: unsupported witness version, got "${i}" from address "${o}"`)}}try{const e=this.bs58check.decode(o),t=e[0],i=e.slice(1);if(t===s.scriptHash)return this.p2sh(r,i).scriptPublickey;if(t===s.pubKeyHash)return this.p2pkh(r,{hash:i}).scriptPublickey}catch(e){null===exports.logger||void 0===exports.logger||exports.logger.debug(`${e.toString()}, cointype: ${t.CoinType[r]}, address: ${o}`)}throw Error(`ArgumentError: invalid address for ${t.CoinType[r]}, got ${o}`)}static classify(e){if(this.isP2WPKH(e))return t.ScriptType.P2WPKH;if(this.isP2PKH(e))return t.ScriptType.P2PKH;if(this.isP2TR(e))return t.ScriptType.P2TR;throw Error(`non-standard script: ${e.toString("hex")}`)}static isP2PKH(e){return 25===e.length&&e[0]===t.OPCODES.OP_DUP&&e[1]===t.OPCODES.OP_HASH160&&20===e[2]&&e[23]===t.OPCODES.OP_EQUALVERIFY&&e[24]===t.OPCODES.OP_CHECKSIG}static isP2SH(e){return 23===e.length&&e[0]===t.OPCODES.OP_HASH160&&20===e[1]&&e[22]===t.OPCODES.OP_EQUAL}static isP2WPKH(e){return 22===e.length&&e[0]===t.OPCODES.OP_0&&20===e[1]}static isP2TR(e){return 34===e.length&&81===e[0]&&32===e[1]}}function n(e){const t=Buffer.from((0,r.sha256)().update(e).digest());return Buffer.from((0,r.ripemd160)().update(t).digest())}exports.PaymentBTC=c,c.bs58check=new o.bs58Check((function(e){const t=(0,r.sha256)().update(e).digest(),o=(0,r.sha256)().update(t).digest();return Buffer.from(o)})),exports.Hash160=n;
{
"name": "@secux/app-btc",
"version": "3.1.2",
"version": "3.1.3",
"description": "SecuX Hardware Wallet BTC API",

@@ -5,0 +5,0 @@ "keywords": [

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