New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@secux/app-btc

Package Overview
Dependencies
Maintainers
2
Versions
37
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.3 to 3.1.4

README.hbs

130

lib/app-btc.d.ts

@@ -18,12 +18,2 @@ /// <reference types="node" />

/**
* @typedef PathObject parameters for address generation
* @property {CoinType} coin enum
* @property {ScriptType} script enum
*/
/**
* @typedef AddressOption option for path validation
* @property {CoinType} [coin] enum
* @property {ScriptType} [script] enum
*/
/**
* Prepare data for address generation.

@@ -34,3 +24,3 @@ * @param {string} path BIP32 path, ex: m/44'/0'/0'/0/0

*/
static readonly prepareAddress: typeof SecuxBTC.preparePublickey;
static prepareAddress(path: string, option?: AddressOption): communicationData;
/**

@@ -70,39 +60,2 @@ * Generate address from response data.

/**
* @typedef txInput utxo object
* @property {string} path BIP32 path refer to utxo
* @property {string | Buffer} publickey scep256k1 publickey from `path`
* @property {string} hash referenced transaction hash
* @property {number} vout referenced transaction output index
* @property {number} satoshis referenced transaction output amount
* @property {ScriptType} [script] script type related to `path`
* @property {string} [txHex] referenced raw transaction for validation
*/
/**
* @typedef txOutput output object
* @property {txOutputAddress | txOutputScriptExtened} to receiving address information
* @property {txOutputScriptExtened} [utxo] changes
*/
/**
* @typedef SignOption
* @property {CoinType} [coin] enum
* @property {number} [feeRate] base fee per vbyte
*/
/**
* @typedef txOutputAddress receiving address information
* @property {string} address receiving address
* @property {number} satoshis receiving amount
*/
/**
* @typedef txOutputScriptExtened own address information
* @property {string} path BIP32 path
* @property {string | Buffer} publickey scep256k1 publickey from `path`
* @property {number} satoshis amount
* @property {ScriptType} [script] script type related to `path`
*/
/**
* @typedef prepared
* @property {communicationData} commandData data for sending to device
* @property {string} rawTx unsigned raw transaction
*/
/**
* Prepare data for signing.

@@ -156,1 +109,82 @@ * @param {txInput} inputs array of utxo object

}
/**
* Data type for transmission.
* @typedef {string|Buffer} communicationData
*/
/**
* Script type for input/output.
* @typedef {enum} ScriptType
* @property {number} P2PKH 0
* @property {number} P2WPKH 1
* @property {number} P2SH_P2PKH 2
* @property {number} P2SH_P2WPKH 3
* @property {number} P2TR 4
*/
/**
* Coins that are nearly identical to Bitcoin.
* @typedef {enum} CoinType
* @property {number} BITCOIN 0
* @property {number} TESTNET 1
* @property {number} REGTEST 2
* @property {number} LITECOIN 3
* @property {number} BITCOINCASH 4
* @property {number} GROESTL 5
* @property {number} DIGIBYTE 6
* @property {number} DASH 7
* @property {number} DOGECOIN 8
*/
/**
* Parameters for address generation.
* @typedef {object} PathObject
* @property {CoinType} coin enum
* @property {ScriptType} script enum
*/
/**
* Options for path validation.
* @typedef {object} AddressOption
* @property {CoinType} [coin] enum
* @property {ScriptType} [script] enum
*/
/**
* UTXO.
* @typedef {object} txInput
* @property {string} path BIP32 path refer to utxo
* @property {string | Buffer} publickey scep256k1 publickey from `path`
* @property {string} hash referenced transaction hash
* @property {number} vout referenced transaction output index
* @property {number} satoshis referenced transaction output amount
* @property {ScriptType} [script] script type related to `path`
* @property {string} [txHex] referenced raw transaction for validation
*/
/**
* Outputs consist of one payment and one or no return.
* @typedef {object} txOutput
* @property {txOutputAddress | txOutputScriptExtened} to receiving address information
* @property {txOutputScriptExtened} [utxo] changes
*/
/**
* Receiving address and payment.
* @typedef {object} txOutputAddress
* @property {string} address receiving address
* @property {number} satoshis receiving amount
*/
/**
* Payment for another held account.
* @typedef {object} txOutputScriptExtened
* @property {string} path BIP32 path
* @property {string | Buffer} publickey scep256k1 publickey from `path`
* @property {number} satoshis amount
* @property {ScriptType} [script] script type related to `path`
*/
/**
* Options used during the signing.
* @typedef {object} SignOption
* @property {CoinType} [coin] check cointype for each input
* @property {number} [feeRate] calculate optimal transaction fee and replace it
*/
/**
* Object for the signing and validation.
* @typedef {object} prepared
* @property {communicationData} commandData data for sending to device
* @property {string} rawTx unsigned raw transaction
*/

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

"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");
"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 prepareAddress(e,t){return this.preparePublickey(e,t)}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?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?void 0:t.script)?void 0:(0,f.getPurpose)(null===t||void 0===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?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?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?void 0:i.coin)&&void 0!==o?o:(0,f.getCoinType)(t),script:null!==(u=null===i||void 0===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?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?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?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?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?void 0:o.script)}const{publickey:a}=(0,n.deriveKey)(p.publickey,p.chaincode,[r,i]),d=null!==(u=null===o||void 0===o?void 0:o.coin)&&void 0!==u?u:l.CoinType.BITCOIN,y=null!==(s=null===o||void 0===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=e=t([(0,y.staticImplements)()],v),exports.SecuxBTC=v,(0,y.loadPlugin)(v,"SecuxBTC");

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.taprootVerify=exports.taprootConvert=void 0;const e=require("./utils"),r=require("bigi"),o=require("ecurve"),t=o.getCurveByName("secp256k1"),f=t.G,n=t.p,u=t.n,i=r.ZERO,a=r.ONE,c=r.valueOf(2),s=r.valueOf(3),l=r.valueOf(4),d=r.valueOf(7);function m(e){const f=r.fromBuffer(e),u=f.pow(s).add(d).mod(n),i=u.modPow(n.add(a).divide(l),n);if(0!==u.compareTo(i.modPow(c,n)))throw new Error("c is not equal to y^2");let m=o.Point.fromAffine(t,f,i);return p(m)||(m=o.Point.fromAffine(t,f,n.subtract(i))),m}function p(e){return e.affineY.mod(c).equals(i)}exports.taprootConvert=function(e,o){const t=m(e),n=r.fromBuffer(o);return t.add(f.multiply(n)).affineX.toBuffer(32)},exports.taprootVerify=function(o,t,i){const a=m(i),c=a.affineX.toBuffer(32),s=r.fromBuffer(o.slice(0,32)),l=r.fromBuffer(o.slice(32,64));if(s.compareTo(n)>=0)return!1;if(l.compareTo(u)>=0)return!1;const d=function(o,t,f){const n=(0,e.taggedHash)("BIP0340/challenge",Buffer.concat([o,t,f]));return r.fromBuffer(n).mod(u)}(s.toBuffer(32),c,t),v=function(e,r,o){const t=f.multiply(e),n=o.multiply(r);return t.add(n.negate())}(l,d,a);return!(v.curve.isInfinity(v)||!p(v)||!v.affineX.equals(s))};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.taprootVerify=exports.taprootConvert=void 0;const e=require("./utils"),r=require("bigi"),o=require("ecurve"),t=o.getCurveByName("secp256k1"),f=t.G,n=t.p,u=t.n,i=r.ZERO,a=r.ONE,c=r.valueOf(2),s=r.valueOf(3),l=r.valueOf(4),d=r.valueOf(7);function m(e){const f=r.fromBuffer(e),u=f.pow(s).add(d).mod(n),i=u.modPow(n.add(a).divide(l),n);if(0!==u.compareTo(i.modPow(c,n)))throw new Error("c is not equal to y^2");let m=o.Point.fromAffine(t,f,i);return p(m)||(m=o.Point.fromAffine(t,f,n.subtract(i))),m}function p(e){return e.affineY.mod(c).equals(i)}exports.taprootConvert=function(e,o){const t=m(e),n=r.fromBuffer(o);return t.add(f.multiply(n)).affineX.toBuffer(32)},exports.taprootVerify=function(o,t,i){const a=m(i),c=a.affineX.toBuffer(32),s=r.fromBuffer(o.slice(0,32)),l=r.fromBuffer(o.slice(32,64));if(s.compareTo(n)>=0)return!1;if(l.compareTo(u)>=0)return!1;const d=function(e,r,o){const t=f.multiply(e),n=o.multiply(r);return t.add(n.negate())}(l,function(o,t,f){const n=(0,e.taggedHash)("BIP0340/challenge",Buffer.concat([o,t,f]));return r.fromBuffer(n).mod(u)}(s.toBuffer(32),c,t),a);return!(d.curve.isInfinity(d)||!p(d)||!d.affineX.equals(s))};

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BufferWriter=exports.BufferReader=void 0;const t=require("varuint-bitcoin");exports.BufferWriter=class{constructor(t,e=0){this.buffer=t,this.offset=e}writeUInt8(t){this.offset=this.buffer.writeUInt8(t,this.offset)}writeInt32(t){this.offset=this.buffer.writeInt32LE(t,this.offset)}writeUInt32(t){this.offset=this.buffer.writeUInt32LE(t,this.offset)}writeUInt64(t){var r,s,f;this.offset=(r=this.buffer,s=t,f=this.offset,e(s,9007199254740991),r.writeInt32LE(-1&s,f),r.writeUInt32LE(Math.floor(s/4294967296),f+4),f+8)}writeVarInt(e){t.encode(e,this.buffer,this.offset),this.offset+=t.encode.bytes}writeSlice(t){if(this.buffer.length<this.offset+t.length)throw new Error("Cannot write slice out of bounds");this.offset+=t.copy(this.buffer,this.offset)}writeVarSlice(t){this.writeVarInt(t.length),this.writeSlice(t)}writeVector(t){this.writeVarInt(t.length),t.forEach((t=>this.writeVarSlice(t)))}};function e(t,e){if("number"!=typeof t)throw new Error("cannot write a non-number as a number");if(t<0)throw new Error("specified a negative value for writing an unsigned value");if(Math.floor(t)!==t)throw new Error("value has a fractional component")}exports.BufferReader=class{constructor(t,e=0){this.buffer=t,this.offset=e}readUInt8(){const t=this.buffer.readUInt8(this.offset);return this.offset++,t}readInt32(){const t=this.buffer.readInt32LE(this.offset);return this.offset+=4,t}readUInt32(){const t=this.buffer.readUInt32LE(this.offset);return this.offset+=4,t}readUInt64(){const t=function(t,r){const s=t.readUInt32LE(r);let f=t.readUInt32LE(r+4);return f*=4294967296,e(f+s,9007199254740991),f+s}(this.buffer,this.offset);return this.offset+=8,t}readVarInt(){const e=t.decode(this.buffer,this.offset);return this.offset+=t.decode.bytes,e}readSlice(t){if(this.buffer.length<this.offset+t)throw new Error("Cannot read slice out of bounds");const e=this.buffer.slice(this.offset,this.offset+t);return this.offset+=t,e}readVarSlice(){return this.readSlice(this.readVarInt())}readVector(){const t=this.readVarInt(),e=[];for(let r=0;r<t;r++)e.push(this.readVarSlice());return e}};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BufferWriter=exports.BufferReader=void 0;const t=require("varuint-bitcoin");exports.BufferWriter=class{constructor(t,e=0){this.buffer=t,this.offset=e}writeUInt8(t){this.offset=this.buffer.writeUInt8(t,this.offset)}writeInt32(t){this.offset=this.buffer.writeInt32LE(t,this.offset)}writeUInt32(t){this.offset=this.buffer.writeUInt32LE(t,this.offset)}writeUInt64(t){var r,s,f;this.offset=(r=this.buffer,s=t,f=this.offset,e(s,9007199254740991),r.writeInt32LE(-1&s,f),r.writeUInt32LE(Math.floor(s/4294967296),f+4),f+8)}writeVarInt(e){t.encode(e,this.buffer,this.offset),this.offset+=t.encode.bytes}writeSlice(t){if(this.buffer.length<this.offset+t.length)throw new Error("Cannot write slice out of bounds");this.offset+=t.copy(this.buffer,this.offset)}writeVarSlice(t){this.writeVarInt(t.length),this.writeSlice(t)}writeVector(t){this.writeVarInt(t.length),t.forEach(t=>this.writeVarSlice(t))}};function e(t,e){if("number"!=typeof t)throw new Error("cannot write a non-number as a number");if(t<0)throw new Error("specified a negative value for writing an unsigned value");if(Math.floor(t)!==t)throw new Error("value has a fractional component")}exports.BufferReader=class{constructor(t,e=0){this.buffer=t,this.offset=e}readUInt8(){const t=this.buffer.readUInt8(this.offset);return this.offset++,t}readInt32(){const t=this.buffer.readInt32LE(this.offset);return this.offset+=4,t}readUInt32(){const t=this.buffer.readUInt32LE(this.offset);return this.offset+=4,t}readUInt64(){const t=function(t,r){const s=t.readUInt32LE(r);let f=t.readUInt32LE(r+4);return e((f*=4294967296)+s,9007199254740991),f+s}(this.buffer,this.offset);return this.offset+=8,t}readVarInt(){const e=t.decode(this.buffer,this.offset);return this.offset+=t.decode.bytes,e}readSlice(t){if(this.buffer.length<this.offset+t)throw new Error("Cannot read slice out of bounds");const e=this.buffer.slice(this.offset,this.offset+t);return this.offset+=t,e}readVarSlice(){return this.readSlice(this.readVarInt())}readVector(){const t=this.readVarInt(),e=[];for(let r=0;r<t;r++)e.push(this.readVarSlice());return e}};

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ow_SignOption=exports.ow_AddressOption=exports.isOutuptScriptExtended=exports.isOutuptScript=exports.isOutputAddress=exports.ow_txOutput=exports.ow_txOutputScriptExtened=exports.ow_txOutputScript=exports.ow_txOutputAddress=exports.ow_txInput=exports.ow_PathObject=exports.ow_accountPath=exports.ow_path=exports.ow_balance=exports.btcPurposes=exports.btcCoinTypes=exports.coinmap=exports.CoinType=exports.ScriptType=exports.OPCODES=void 0;const e=require("ow"),t=require("./coindef"),o=require("@secux/utility");var r,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.P2TR=4]="P2TR",e[e.__LENGTH=5]="__LENGTH"}(r=exports.ScriptType||(exports.ScriptType={})),function(e){e[e.BITCOIN=0]="BITCOIN",e[e.TESTNET=1]="TESTNET",e[e.REGTEST=2]="REGTEST",e[e.LITECOIN=3]="LITECOIN",e[e.BITCOINCASH=4]="BITCOINCASH",e[e.GROESTL=5]="GROESTL",e[e.DIGIBYTE=6]="DIGIBYTE",e[e.DASH=7]="DASH",e[e.DOGECOIN=8]="DOGECOIN",e[e.__LENGTH=9]="__LENGTH"}(p=exports.CoinType||(exports.CoinType={})),exports.coinmap=Object.freeze(Object.values(p).slice(0,p.__LENGTH).map((e=>t[e.toLowerCase()]))),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),Object.freeze(86)]),exports.ow_balance=e.default.number.integer.positive.lessThanOrEqual(9007199254740991),exports.ow_path=o.ow_strictPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_accountPath=o.ow_accountPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_PathObject=e.default.object.exactShape({coin:e.default.number.inRange(0,p.__LENGTH-1),script:e.default.number.inRange(0,r.__LENGTH-1)}),exports.ow_txInput=e.default.object.exactShape({hash:o.ow_hashString,vout:e.default.number.greaterThanOrEqual(0),txHex:e.default.any(e.default.undefined,o.ow_hexString),script:e.default.optional.number.inRange(0,r.__LENGTH-1),satoshis:exports.ow_balance,path:exports.ow_path,publickey:e.default.any(e.default.undefined,o.ow_hexString,e.default.buffer)}),exports.ow_txOutputAddress=e.default.object.exactShape({address:o.ow_hashString,satoshis:exports.ow_balance}),exports.ow_txOutputScript=e.default.object.exactShape({scriptHex:o.ow_hexString,satoshis:exports.ow_balance}),exports.ow_txOutputScriptExtened=e.default.object.exactShape({publickey:e.default.any(e.default.undefined,o.ow_hexString,e.default.buffer),path:exports.ow_path,satoshis:exports.ow_balance,script:e.default.optional.number.inRange(0,r.__LENGTH-1)}),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){}},exports.ow_AddressOption=e.default.object.exactShape({coin:e.default.optional.number.inRange(0,p.__LENGTH-1),script:e.default.optional.number.inRange(0,r.__LENGTH-1)}),exports.ow_SignOption=e.default.object.exactShape({coin:e.default.optional.number.inRange(0,p.__LENGTH-1),feeRate:e.default.optional.number.greaterThanOrEqual(1)});
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ow_SignOption=exports.ow_AddressOption=exports.isOutuptScriptExtended=exports.isOutuptScript=exports.isOutputAddress=exports.ow_txOutput=exports.ow_txOutputScriptExtened=exports.ow_txOutputScript=exports.ow_txOutputAddress=exports.ow_txInput=exports.ow_PathObject=exports.ow_accountPath=exports.ow_path=exports.ow_balance=exports.btcPurposes=exports.btcCoinTypes=exports.coinmap=exports.CoinType=exports.ScriptType=exports.OPCODES=void 0;const e=require("ow"),t=require("./coindef"),o=require("@secux/utility");var r,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.P2TR=4]="P2TR",e[e.__LENGTH=5]="__LENGTH"}(r=exports.ScriptType||(exports.ScriptType={})),function(e){e[e.BITCOIN=0]="BITCOIN",e[e.TESTNET=1]="TESTNET",e[e.REGTEST=2]="REGTEST",e[e.LITECOIN=3]="LITECOIN",e[e.BITCOINCASH=4]="BITCOINCASH",e[e.GROESTL=5]="GROESTL",e[e.DIGIBYTE=6]="DIGIBYTE",e[e.DASH=7]="DASH",e[e.DOGECOIN=8]="DOGECOIN",e[e.__LENGTH=9]="__LENGTH"}(p=exports.CoinType||(exports.CoinType={})),exports.coinmap=Object.freeze(Object.values(p).slice(0,p.__LENGTH).map(e=>t[e.toLowerCase()])),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),Object.freeze(86)]),exports.ow_balance=e.default.number.integer.positive.lessThanOrEqual(9007199254740991),exports.ow_path=o.ow_strictPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_accountPath=o.ow_accountPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_PathObject=e.default.object.exactShape({coin:e.default.number.inRange(0,p.__LENGTH-1),script:e.default.number.inRange(0,r.__LENGTH-1)}),exports.ow_txInput=e.default.object.exactShape({hash:o.ow_hashString,vout:e.default.number.greaterThanOrEqual(0),txHex:e.default.any(e.default.undefined,o.ow_hexString),script:e.default.optional.number.inRange(0,r.__LENGTH-1),satoshis:exports.ow_balance,path:exports.ow_path,publickey:e.default.any(e.default.undefined,o.ow_hexString,e.default.buffer)}),exports.ow_txOutputAddress=e.default.object.exactShape({address:o.ow_hashString,satoshis:exports.ow_balance}),exports.ow_txOutputScript=e.default.object.exactShape({scriptHex:o.ow_hexString,satoshis:exports.ow_balance}),exports.ow_txOutputScriptExtened=e.default.object.exactShape({publickey:e.default.any(e.default.undefined,o.ow_hexString,e.default.buffer),path:exports.ow_path,satoshis:exports.ow_balance,script:e.default.optional.number.inRange(0,r.__LENGTH-1)}),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){}},exports.ow_AddressOption=e.default.object.exactShape({coin:e.default.optional.number.inRange(0,p.__LENGTH-1),script:e.default.optional.number.inRange(0,r.__LENGTH-1)}),exports.ow_SignOption=e.default.object.exactShape({coin:e.default.optional.number.inRange(0,p.__LENGTH-1),feeRate:e.default.optional.number.greaterThanOrEqual(1)});

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Psbtv2=void 0;const e=require("bip174"),r=require("bip174/src/lib/converter"),t=require("./bufferutils");class o extends e.Psbt{static fromBuffer(e,o){const E=function(e,o){if(1886610036!==e.readUInt32BE())throw Error("Format Error: Invalid Magic Number");if(255!==e.readUInt8(4))throw Error("Format Error: Magic Number must be followed by 0xff separator");const E=new t.BufferReader(e);E.offset+=5;const u=()=>({key:E.readVarSlice(),value:E.readVarSlice()}),I=()=>{if(E.offset>=e.length)throw Error("Format Error: Unexpected End of PSBT");const r=0===e.readUInt8(E.offset);return r&&(E.offset+=1),r},T=[],l={};for(;!I();){const e=u(),r=e.key.toString("hex");if(l[r])throw Error("Format Error: Keys must be unique for global keymap: key "+r);l[r]=1,T.push(e)}const p=T.filter((e=>e.key[0]===i.UNSIGNED_TX));if(1!==p.length)throw Error("Format Error: Only one UNSIGNED_TX allowed");const _=o(p[0].value),{inputCount:S,outputCount:c}=_.getInputOutputCounts(),P=[],O=[];for(let e=0;e<S;e++){const r={},t=[];for(;!I();){const o=u(),a=o.key.toString("hex");if(r[a])throw Error(`Format Error: Keys must be unique, got "${a}" from input#${e}`);r[a]=1,t.push(o)}P.push(t)}for(let e=0;e<c;e++){const r={},t=[];for(;!I();){const o=u(),a=o.key.toString("hex");if(r[a])throw Error(`Format Error: Keys must be unique, got "${a}" from output#${e}`);r[a]=1,t.push(o)}O.push(t)}return function(e,{globalMapKeyVals:o,inputKeyVals:E,outputKeyVals:u}){const I={unsignedTx:e};let T,l,p=0;for(const e of o){let o;switch(e.key[0]){case i.UNSIGNED_TX:if(a("global",e.key,i.UNSIGNED_TX),p>0)throw new Error("Format Error: GlobalMap has multiple UNSIGNED_TX");p++;break;case i.GLOBAL_XPUB:void 0===I.globalXpub&&(I.globalXpub=[]),I.globalXpub.push(r.globals.globalXpub.decode(e));break;case i.GLOBAL_TX_VERSION:if(a("global",e.key,i.GLOBAL_TX_VERSION),4!==e.value.length)throw Error("Value Error: Global transaction version is not 4 bytes");I.txVersion=e.value.readUInt32LE();break;case i.GLOBAL_FALLBACK_LOCKTIME:if(a("global",e.key,i.GLOBAL_FALLBACK_LOCKTIME),4!==e.value.length)throw Error("Value Error: Global fallback locktime is not 4 bytes");I.fallbackLocktime=e.value.readUInt32LE();break;case i.GLOBAL_INPUT_COUNT:a("global",e.key,i.GLOBAL_INPUT_COUNT),o=new t.BufferReader(e.value),o.readVarInt(),T=o.readVarInt();break;case i.GLOBAL_OUTPUT_COUNT:a("global",e.key,i.GLOBAL_OUTPUT_COUNT),o=new t.BufferReader(e.value),o.readVarInt(),l=o.readVarInt();break;case i.GLOBAL_TXMODIFIABLE:if(a("global",e.key,i.GLOBAL_TXMODIFIABLE),1!==e.value.length)throw Error("Value Error: Global tx modifiable flags is not 1 bytes");I.txModifiable=1==(1&e.value[0]);break;case i.GLOBAL_VERSION:if(a("global",e.key,i.GLOBAL_VERSION),4!==e.value.length)throw Error("Value Error: Global PSBT version is not 4 bytes");I.version=e.value.readUInt32LE();break;default:I.unknownKeyVals||(I.unknownKeyVals=[]),I.unknownKeyVals.push(e)}}T=null!=T?T:E.length,l=null!=l?l:u.length;const _=[],S=[];for(let e=0;e<T;e++){const o={tapScriptSigs:{},tapScripts:{},tapBip32Paths:{}};for(const i of E[e])switch(r.inputs.checkPubkey(i),i.key[0]){case n.NON_WITNESS_UTXO:if(a("input",i.key,n.NON_WITNESS_UTXO),void 0!==o.nonWitnessUtxo)throw Error("Format Error: Input has multiple NON_WITNESS_UTXO");o.nonWitnessUtxo=r.inputs.nonWitnessUtxo.decode(i);break;case n.WITNESS_UTXO:if(a("input",i.key,n.WITNESS_UTXO),void 0!==o.witnessUtxo)throw Error("Format Error: Input has multiple WITNESS_UTXO");o.witnessUtxo=r.inputs.witnessUtxo.decode(i);break;case n.PARTIAL_SIG:void 0===o.partialSig&&(o.partialSig=[]),o.partialSig.push(r.inputs.partialSig.decode(i));break;case n.SIGHASH_TYPE:if(a("input",i.key,n.SIGHASH_TYPE),void 0!==o.sighashType)throw Error("Format Error: Input has multiple SIGHASH_TYPE");o.sighashType=r.inputs.sighashType.decode(i);break;case n.REDEEM_SCRIPT:if(a("input",i.key,n.REDEEM_SCRIPT),void 0!==o.redeemScript)throw Error("Format Error: Input has multiple REDEEM_SCRIPT");o.redeemScript=r.inputs.redeemScript.decode(i);break;case n.WITNESS_SCRIPT:if(a("input",i.key,n.WITNESS_SCRIPT),void 0!==o.witnessScript)throw Error("Format Error: Input has multiple WITNESS_SCRIPT");o.witnessScript=r.inputs.witnessScript.decode(i);break;case n.BIP32_DERIVATION:void 0===o.bip32Derivation&&(o.bip32Derivation=[]),o.bip32Derivation.push(r.inputs.bip32Derivation.decode(i));break;case n.FINAL_SCRIPTSIG:a("input",i.key,n.FINAL_SCRIPTSIG),o.finalScriptSig=r.inputs.finalScriptSig.decode(i);break;case n.FINAL_SCRIPTWITNESS:a("input",i.key,n.FINAL_SCRIPTWITNESS),o.finalScriptWitness=r.inputs.finalScriptWitness.decode(i);break;case n.POR_COMMITMENT:a("input",i.key,n.POR_COMMITMENT),o.porCommitment=r.inputs.porCommitment.decode(i);break;case n.PREVIOUS_TXID:if(a("input",i.key,n.PREVIOUS_TXID),32!==i.value.length)throw Error("Value Error: Previous txid is not 32 bytes");o.prevTXID=i.value.toString("hex");break;case n.OUTPUT_INDEX:if(a("input",i.key,n.OUTPUT_INDEX),4!==i.value.length)throw Error("Value Error: Previous output index is not 4 bytes");o.prevOutputIndex=i.value[0];break;case n.TAP_KEY_SIG:if(a("input",i.key,n.TAP_KEY_SIG),i.value.length<64)throw Error("Value Error: Input Taproot key path signature is shorter than 64 bytes");if(i.value.length>65)throw Error("Value Error: Input Taproot key path signature is longer than 65 bytes");o.tapKeySig=i.value;break;case n.TAP_SCRIPT_SIG:if(a("input",i.key,n.TAP_SCRIPT_SIG),65!==i.key.length)throw Error("Format Error: Input Taproot script signature key is not 65 bytes");if(i.value.length<64)throw Error("Value Error: Input Taproot script path signature is shorter than 64 bytes");if(i.value.length>65)throw Error("Value Error: Input Taproot script path signature is longer than 65 bytes");const e=i.key.slice(1).toString("hex");o.tapScriptSigs[e]=i.value;break;case n.TAP_LEAF_SCRIPT:if(a("input",i.key,n.TAP_LEAF_SCRIPT),i.key.length<34)throw Error("Format Error: Input Taproot leaf script key is not at least 34 bytes");if(i.key.length%32!=2)throw Error("Format Error: Input Taproot leaf script key's control block is not valid");if(0===i.value.length)throw Error("Value Error: Intput Taproot leaf script cannot be empty");const s=i.value.slice(-1).toString("hex");o.tapScripts[s]||(o.tapScripts[s]=new Set),o.tapScripts[s].add(i.key.slice(1).toString("hex"));break;case n.TAP_BIP32_DERIVATION:if(a("input",i.key,n.TAP_BIP32_DERIVATION),33!==i.key.length)throw Error("Format Error: Input Taproot BIP 32 keypath key is not 33 bytes");const E=i.key.slice(1).toString("hex"),u=new t.BufferReader(i.value),I=u.readVarInt(),T=new Set;for(let e=0;e<I;e++)T.add(u.readSlice(32).toString("hex"));o.tapBip32Paths[E]={leafHashs:T,Bip32Derivation:i.value.slice(u.offset)};break;case n.TAP_INTERNAL_KEY:if(a("input",i.key,n.TAP_INTERNAL_KEY),32!==i.value.length)throw Error("Value Error: Input Taproot internal key is not 32 bytes");o.tapInternalKey=i.value;break;case n.TAP_MERKLE_ROOT:if(a("input",i.key,n.TAP_MERKLE_ROOT),32!==i.value.length)throw Error("Value Error: Input Taproot merkle root is not 32 bytes");o.tapMarkleRoot=i.value.toString("hex");break;default:o.unknownKeyVals||(o.unknownKeyVals=[]),o.unknownKeyVals.push(i)}_.push(o)}for(let e=0;e<l;e++){const o={tapBip32Paths:{}};for(const i of u[e]){let e;switch(r.outputs.checkPubkey(i),i.key[0]){case s.REDEEM_SCRIPT:if(a("output",i.key,s.REDEEM_SCRIPT),void 0!==o.redeemScript)throw Error("Format Error: Output has multiple REDEEM_SCRIPT");o.redeemScript=r.outputs.redeemScript.decode(i);break;case s.WITNESS_SCRIPT:if(a("output",i.key,s.WITNESS_SCRIPT),void 0!==o.witnessScript)throw Error("Format Error: Output has multiple WITNESS_SCRIPT");o.witnessScript=r.outputs.witnessScript.decode(i);break;case s.BIP32_DERIVATION:void 0===o.bip32Derivation&&(o.bip32Derivation=[]),o.bip32Derivation.push(r.outputs.bip32Derivation.decode(i));break;case s.AMOUNT:if(a("output",i.key,s.AMOUNT),8!==i.value.length)throw Error("Value Error: Output amount is not 8 bytes");e=new t.BufferReader(i.value),o.amount=e.readUInt64();break;case s.SCRIPT:a("output",i.key,s.SCRIPT),o.script=i.value;break;case s.TAP_INTERNAL_KEY:if(a("output",i.key,s.TAP_INTERNAL_KEY),32!==i.value.length)throw Error("Value Error: Output Taproot internal key is not 32 bytes");o.tapInternalKey=i.value;break;case s.TAP_TREE:a("output",i.key,s.TAP_TREE),o.tapTree=i.value;break;case s.TAP_BIP32_DERIVATION:if(a("output",i.key,s.TAP_BIP32_DERIVATION),33!==i.key.length)throw Error("Output Taproot BIP 32 keypath key is not 33 bytes");const n=i.key.slice(1).toString("hex"),E=new Set;e=new t.BufferReader(i.value);const u=e.readVarInt();for(let r=0;r<u;r++)E.add(e.readSlice(32).toString("hex"));o.tapBip32Paths[n]={leafHashs:E,Bip32Derivation:i.value.slice(e.offset)};break;default:o.unknownKeyVals||(o.unknownKeyVals=[]),o.unknownKeyVals.push(i)}}S.push(o)}return{globalMap:I,inputs:_,outputs:S}}(_,{globalMapKeyVals:T,inputKeyVals:P,outputKeyVals:O})}(e,o),u=new this(E.globalMap.unsignedTx);return Object.assign(u,E),u}}function a(e,r,t){if(!r.equals(Buffer.from([t])))throw Error(`Format Error: Invalid ${e} key: ${r.toString("hex")}`)}var i,n,s;exports.Psbtv2=o,function(e){e[e.UNSIGNED_TX=0]="UNSIGNED_TX",e[e.GLOBAL_XPUB=1]="GLOBAL_XPUB",e[e.GLOBAL_TX_VERSION=2]="GLOBAL_TX_VERSION",e[e.GLOBAL_FALLBACK_LOCKTIME=3]="GLOBAL_FALLBACK_LOCKTIME",e[e.GLOBAL_INPUT_COUNT=4]="GLOBAL_INPUT_COUNT",e[e.GLOBAL_OUTPUT_COUNT=5]="GLOBAL_OUTPUT_COUNT",e[e.GLOBAL_TXMODIFIABLE=6]="GLOBAL_TXMODIFIABLE",e[e.GLOBAL_VERSION=251]="GLOBAL_VERSION"}(i||(i={})),function(e){e[e.NON_WITNESS_UTXO=0]="NON_WITNESS_UTXO",e[e.WITNESS_UTXO=1]="WITNESS_UTXO",e[e.PARTIAL_SIG=2]="PARTIAL_SIG",e[e.SIGHASH_TYPE=3]="SIGHASH_TYPE",e[e.REDEEM_SCRIPT=4]="REDEEM_SCRIPT",e[e.WITNESS_SCRIPT=5]="WITNESS_SCRIPT",e[e.BIP32_DERIVATION=6]="BIP32_DERIVATION",e[e.FINAL_SCRIPTSIG=7]="FINAL_SCRIPTSIG",e[e.FINAL_SCRIPTWITNESS=8]="FINAL_SCRIPTWITNESS",e[e.POR_COMMITMENT=9]="POR_COMMITMENT",e[e.PREVIOUS_TXID=14]="PREVIOUS_TXID",e[e.OUTPUT_INDEX=15]="OUTPUT_INDEX",e[e.SEQUENCE=16]="SEQUENCE",e[e.REQUIRED_TIME_LOCKTIME=17]="REQUIRED_TIME_LOCKTIME",e[e.REQUIRED_HEIGHT_LOCKTIME=18]="REQUIRED_HEIGHT_LOCKTIME",e[e.TAP_KEY_SIG=19]="TAP_KEY_SIG",e[e.TAP_SCRIPT_SIG=20]="TAP_SCRIPT_SIG",e[e.TAP_LEAF_SCRIPT=21]="TAP_LEAF_SCRIPT",e[e.TAP_BIP32_DERIVATION=22]="TAP_BIP32_DERIVATION",e[e.TAP_INTERNAL_KEY=23]="TAP_INTERNAL_KEY",e[e.TAP_MERKLE_ROOT=24]="TAP_MERKLE_ROOT"}(n||(n={})),function(e){e[e.REDEEM_SCRIPT=0]="REDEEM_SCRIPT",e[e.WITNESS_SCRIPT=1]="WITNESS_SCRIPT",e[e.BIP32_DERIVATION=2]="BIP32_DERIVATION",e[e.AMOUNT=3]="AMOUNT",e[e.SCRIPT=4]="SCRIPT",e[e.TAP_INTERNAL_KEY=5]="TAP_INTERNAL_KEY",e[e.TAP_TREE=6]="TAP_TREE",e[e.TAP_BIP32_DERIVATION=7]="TAP_BIP32_DERIVATION"}(s||(s={}));
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Psbtv2=void 0;const e=require("bip174"),r=require("bip174/src/lib/converter"),t=require("./bufferutils");function o(e,r,t){if(!r.equals(Buffer.from([t])))throw Error(`Format Error: Invalid ${e} key: ${r.toString("hex")}`)}var a,i,n;exports.Psbtv2=class extends e.Psbt{static fromBuffer(e,s){const u=function(e,s){if(1886610036!==e.readUInt32BE())throw Error("Format Error: Invalid Magic Number");if(255!==e.readUInt8(4))throw Error("Format Error: Magic Number must be followed by 0xff separator");const u=new t.BufferReader(e);u.offset+=5;const E=()=>{const e=u.readVarSlice(),r=u.readVarSlice();return{key:e,value:r}},I=()=>{if(u.offset>=e.length)throw Error("Format Error: Unexpected End of PSBT");const r=0===e.readUInt8(u.offset);return r&&(u.offset+=1),r},T=[],l={};for(;!I();){const e=E(),r=e.key.toString("hex");if(l[r])throw Error("Format Error: Keys must be unique for global keymap: key "+r);l[r]=1,T.push(e)}const p=T.filter(e=>e.key[0]===a.UNSIGNED_TX);if(1!==p.length)throw Error("Format Error: Only one UNSIGNED_TX allowed");const _=s(p[0].value),{inputCount:S,outputCount:c}=_.getInputOutputCounts(),P=[],O=[];for(let e=0;e<S;e++){const r={},t=[];for(;!I();){const o=E(),a=o.key.toString("hex");if(r[a])throw Error(`Format Error: Keys must be unique, got "${a}" from input#${e}`);r[a]=1,t.push(o)}P.push(t)}for(let e=0;e<c;e++){const r={},t=[];for(;!I();){const o=E(),a=o.key.toString("hex");if(r[a])throw Error(`Format Error: Keys must be unique, got "${a}" from output#${e}`);r[a]=1,t.push(o)}O.push(t)}return function(e,{globalMapKeyVals:s,inputKeyVals:u,outputKeyVals:E}){const I={unsignedTx:e};let T,l,p=0;for(const e of s){let i;switch(e.key[0]){case a.UNSIGNED_TX:if(o("global",e.key,a.UNSIGNED_TX),p>0)throw new Error("Format Error: GlobalMap has multiple UNSIGNED_TX");p++;break;case a.GLOBAL_XPUB:void 0===I.globalXpub&&(I.globalXpub=[]),I.globalXpub.push(r.globals.globalXpub.decode(e));break;case a.GLOBAL_TX_VERSION:if(o("global",e.key,a.GLOBAL_TX_VERSION),4!==e.value.length)throw Error("Value Error: Global transaction version is not 4 bytes");I.txVersion=e.value.readUInt32LE();break;case a.GLOBAL_FALLBACK_LOCKTIME:if(o("global",e.key,a.GLOBAL_FALLBACK_LOCKTIME),4!==e.value.length)throw Error("Value Error: Global fallback locktime is not 4 bytes");I.fallbackLocktime=e.value.readUInt32LE();break;case a.GLOBAL_INPUT_COUNT:o("global",e.key,a.GLOBAL_INPUT_COUNT),(i=new t.BufferReader(e.value)).readVarInt(),T=i.readVarInt();break;case a.GLOBAL_OUTPUT_COUNT:o("global",e.key,a.GLOBAL_OUTPUT_COUNT),(i=new t.BufferReader(e.value)).readVarInt(),l=i.readVarInt();break;case a.GLOBAL_TXMODIFIABLE:if(o("global",e.key,a.GLOBAL_TXMODIFIABLE),1!==e.value.length)throw Error("Value Error: Global tx modifiable flags is not 1 bytes");I.txModifiable=1==(1&e.value[0]);break;case a.GLOBAL_VERSION:if(o("global",e.key,a.GLOBAL_VERSION),4!==e.value.length)throw Error("Value Error: Global PSBT version is not 4 bytes");I.version=e.value.readUInt32LE();break;default:I.unknownKeyVals||(I.unknownKeyVals=[]),I.unknownKeyVals.push(e)}}T=null!==T&&void 0!==T?T:u.length,l=null!==l&&void 0!==l?l:E.length;const _=[],S=[];for(let e=0;e<T;e++){const a={tapScriptSigs:{},tapScripts:{},tapBip32Paths:{}};for(const n of u[e])switch(r.inputs.checkPubkey(n),n.key[0]){case i.NON_WITNESS_UTXO:if(o("input",n.key,i.NON_WITNESS_UTXO),void 0!==a.nonWitnessUtxo)throw Error("Format Error: Input has multiple NON_WITNESS_UTXO");a.nonWitnessUtxo=r.inputs.nonWitnessUtxo.decode(n);break;case i.WITNESS_UTXO:if(o("input",n.key,i.WITNESS_UTXO),void 0!==a.witnessUtxo)throw Error("Format Error: Input has multiple WITNESS_UTXO");a.witnessUtxo=r.inputs.witnessUtxo.decode(n);break;case i.PARTIAL_SIG:void 0===a.partialSig&&(a.partialSig=[]),a.partialSig.push(r.inputs.partialSig.decode(n));break;case i.SIGHASH_TYPE:if(o("input",n.key,i.SIGHASH_TYPE),void 0!==a.sighashType)throw Error("Format Error: Input has multiple SIGHASH_TYPE");a.sighashType=r.inputs.sighashType.decode(n);break;case i.REDEEM_SCRIPT:if(o("input",n.key,i.REDEEM_SCRIPT),void 0!==a.redeemScript)throw Error("Format Error: Input has multiple REDEEM_SCRIPT");a.redeemScript=r.inputs.redeemScript.decode(n);break;case i.WITNESS_SCRIPT:if(o("input",n.key,i.WITNESS_SCRIPT),void 0!==a.witnessScript)throw Error("Format Error: Input has multiple WITNESS_SCRIPT");a.witnessScript=r.inputs.witnessScript.decode(n);break;case i.BIP32_DERIVATION:void 0===a.bip32Derivation&&(a.bip32Derivation=[]),a.bip32Derivation.push(r.inputs.bip32Derivation.decode(n));break;case i.FINAL_SCRIPTSIG:o("input",n.key,i.FINAL_SCRIPTSIG),a.finalScriptSig=r.inputs.finalScriptSig.decode(n);break;case i.FINAL_SCRIPTWITNESS:o("input",n.key,i.FINAL_SCRIPTWITNESS),a.finalScriptWitness=r.inputs.finalScriptWitness.decode(n);break;case i.POR_COMMITMENT:o("input",n.key,i.POR_COMMITMENT),a.porCommitment=r.inputs.porCommitment.decode(n);break;case i.PREVIOUS_TXID:if(o("input",n.key,i.PREVIOUS_TXID),32!==n.value.length)throw Error("Value Error: Previous txid is not 32 bytes");a.prevTXID=n.value.toString("hex");break;case i.OUTPUT_INDEX:if(o("input",n.key,i.OUTPUT_INDEX),4!==n.value.length)throw Error("Value Error: Previous output index is not 4 bytes");a.prevOutputIndex=n.value[0];break;case i.TAP_KEY_SIG:if(o("input",n.key,i.TAP_KEY_SIG),n.value.length<64)throw Error("Value Error: Input Taproot key path signature is shorter than 64 bytes");if(n.value.length>65)throw Error("Value Error: Input Taproot key path signature is longer than 65 bytes");a.tapKeySig=n.value;break;case i.TAP_SCRIPT_SIG:if(o("input",n.key,i.TAP_SCRIPT_SIG),65!==n.key.length)throw Error("Format Error: Input Taproot script signature key is not 65 bytes");if(n.value.length<64)throw Error("Value Error: Input Taproot script path signature is shorter than 64 bytes");if(n.value.length>65)throw Error("Value Error: Input Taproot script path signature is longer than 65 bytes");const e=n.key.slice(1).toString("hex");a.tapScriptSigs[e]=n.value;break;case i.TAP_LEAF_SCRIPT:if(o("input",n.key,i.TAP_LEAF_SCRIPT),n.key.length<34)throw Error("Format Error: Input Taproot leaf script key is not at least 34 bytes");if(n.key.length%32!=2)throw Error("Format Error: Input Taproot leaf script key's control block is not valid");if(0===n.value.length)throw Error("Value Error: Intput Taproot leaf script cannot be empty");const s=n.value.slice(-1).toString("hex");a.tapScripts[s]||(a.tapScripts[s]=new Set),a.tapScripts[s].add(n.key.slice(1).toString("hex"));break;case i.TAP_BIP32_DERIVATION:if(o("input",n.key,i.TAP_BIP32_DERIVATION),33!==n.key.length)throw Error("Format Error: Input Taproot BIP 32 keypath key is not 33 bytes");const u=n.key.slice(1).toString("hex"),E=new t.BufferReader(n.value),I=E.readVarInt(),T=new Set;for(let e=0;e<I;e++)T.add(E.readSlice(32).toString("hex"));a.tapBip32Paths[u]={leafHashs:T,Bip32Derivation:n.value.slice(E.offset)};break;case i.TAP_INTERNAL_KEY:if(o("input",n.key,i.TAP_INTERNAL_KEY),32!==n.value.length)throw Error("Value Error: Input Taproot internal key is not 32 bytes");a.tapInternalKey=n.value;break;case i.TAP_MERKLE_ROOT:if(o("input",n.key,i.TAP_MERKLE_ROOT),32!==n.value.length)throw Error("Value Error: Input Taproot merkle root is not 32 bytes");a.tapMarkleRoot=n.value.toString("hex");break;default:a.unknownKeyVals||(a.unknownKeyVals=[]),a.unknownKeyVals.push(n)}_.push(a)}for(let e=0;e<l;e++){const a={tapBip32Paths:{}};for(const i of E[e]){let e;switch(r.outputs.checkPubkey(i),i.key[0]){case n.REDEEM_SCRIPT:if(o("output",i.key,n.REDEEM_SCRIPT),void 0!==a.redeemScript)throw Error("Format Error: Output has multiple REDEEM_SCRIPT");a.redeemScript=r.outputs.redeemScript.decode(i);break;case n.WITNESS_SCRIPT:if(o("output",i.key,n.WITNESS_SCRIPT),void 0!==a.witnessScript)throw Error("Format Error: Output has multiple WITNESS_SCRIPT");a.witnessScript=r.outputs.witnessScript.decode(i);break;case n.BIP32_DERIVATION:void 0===a.bip32Derivation&&(a.bip32Derivation=[]),a.bip32Derivation.push(r.outputs.bip32Derivation.decode(i));break;case n.AMOUNT:if(o("output",i.key,n.AMOUNT),8!==i.value.length)throw Error("Value Error: Output amount is not 8 bytes");e=new t.BufferReader(i.value),a.amount=e.readUInt64();break;case n.SCRIPT:o("output",i.key,n.SCRIPT),a.script=i.value;break;case n.TAP_INTERNAL_KEY:if(o("output",i.key,n.TAP_INTERNAL_KEY),32!==i.value.length)throw Error("Value Error: Output Taproot internal key is not 32 bytes");a.tapInternalKey=i.value;break;case n.TAP_TREE:o("output",i.key,n.TAP_TREE),a.tapTree=i.value;break;case n.TAP_BIP32_DERIVATION:if(o("output",i.key,n.TAP_BIP32_DERIVATION),33!==i.key.length)throw Error("Output Taproot BIP 32 keypath key is not 33 bytes");const s=i.key.slice(1).toString("hex"),u=new Set,E=(e=new t.BufferReader(i.value)).readVarInt();for(let r=0;r<E;r++)u.add(e.readSlice(32).toString("hex"));a.tapBip32Paths[s]={leafHashs:u,Bip32Derivation:i.value.slice(e.offset)};break;default:a.unknownKeyVals||(a.unknownKeyVals=[]),a.unknownKeyVals.push(i)}}S.push(a)}return{globalMap:I,inputs:_,outputs:S}}(_,{globalMapKeyVals:T,inputKeyVals:P,outputKeyVals:O})}(e,s),E=new this(u.globalMap.unsignedTx);return Object.assign(E,u),E}},function(e){e[e.UNSIGNED_TX=0]="UNSIGNED_TX",e[e.GLOBAL_XPUB=1]="GLOBAL_XPUB",e[e.GLOBAL_TX_VERSION=2]="GLOBAL_TX_VERSION",e[e.GLOBAL_FALLBACK_LOCKTIME=3]="GLOBAL_FALLBACK_LOCKTIME",e[e.GLOBAL_INPUT_COUNT=4]="GLOBAL_INPUT_COUNT",e[e.GLOBAL_OUTPUT_COUNT=5]="GLOBAL_OUTPUT_COUNT",e[e.GLOBAL_TXMODIFIABLE=6]="GLOBAL_TXMODIFIABLE",e[e.GLOBAL_VERSION=251]="GLOBAL_VERSION"}(a||(a={})),function(e){e[e.NON_WITNESS_UTXO=0]="NON_WITNESS_UTXO",e[e.WITNESS_UTXO=1]="WITNESS_UTXO",e[e.PARTIAL_SIG=2]="PARTIAL_SIG",e[e.SIGHASH_TYPE=3]="SIGHASH_TYPE",e[e.REDEEM_SCRIPT=4]="REDEEM_SCRIPT",e[e.WITNESS_SCRIPT=5]="WITNESS_SCRIPT",e[e.BIP32_DERIVATION=6]="BIP32_DERIVATION",e[e.FINAL_SCRIPTSIG=7]="FINAL_SCRIPTSIG",e[e.FINAL_SCRIPTWITNESS=8]="FINAL_SCRIPTWITNESS",e[e.POR_COMMITMENT=9]="POR_COMMITMENT",e[e.PREVIOUS_TXID=14]="PREVIOUS_TXID",e[e.OUTPUT_INDEX=15]="OUTPUT_INDEX",e[e.SEQUENCE=16]="SEQUENCE",e[e.REQUIRED_TIME_LOCKTIME=17]="REQUIRED_TIME_LOCKTIME",e[e.REQUIRED_HEIGHT_LOCKTIME=18]="REQUIRED_HEIGHT_LOCKTIME",e[e.TAP_KEY_SIG=19]="TAP_KEY_SIG",e[e.TAP_SCRIPT_SIG=20]="TAP_SCRIPT_SIG",e[e.TAP_LEAF_SCRIPT=21]="TAP_LEAF_SCRIPT",e[e.TAP_BIP32_DERIVATION=22]="TAP_BIP32_DERIVATION",e[e.TAP_INTERNAL_KEY=23]="TAP_INTERNAL_KEY",e[e.TAP_MERKLE_ROOT=24]="TAP_MERKLE_ROOT"}(i||(i={})),function(e){e[e.REDEEM_SCRIPT=0]="REDEEM_SCRIPT",e[e.WITNESS_SCRIPT=1]="WITNESS_SCRIPT",e[e.BIP32_DERIVATION=2]="BIP32_DERIVATION",e[e.AMOUNT=3]="AMOUNT",e[e.SCRIPT=4]="SCRIPT",e[e.TAP_INTERNAL_KEY=5]="TAP_INTERNAL_KEY",e[e.TAP_TREE=6]="TAP_TREE",e[e.TAP_BIP32_DERIVATION=7]="TAP_BIP32_DERIVATION"}(n||(n={}));

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CoinType=exports.PaymentGRS=void 0;const e=require("./payment");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return e.CoinType}});const t=require("@secux/utility/lib/bs58"),r=require("groestl-hash-js");class o extends e.PaymentBTC{static CoinSupported(t){if(t!==e.CoinType.GROESTL)throw Error("Not supported cointype")}}exports.PaymentGRS=o,o.bs58check=new t.bs58Check((function(e){return Buffer.from(r.groestl_2(e,1,1))}));
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CoinType=exports.PaymentGRS=void 0;const e=require("./payment");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return e.CoinType}});const t=require("@secux/utility/lib/bs58"),r=require("groestl-hash-js");class o extends e.PaymentBTC{static CoinSupported(t){if(t!==e.CoinType.GROESTL)throw Error("Not supported cointype")}}exports.PaymentGRS=o,o.bs58check=new t.bs58Check(function(e){return Buffer.from(r.groestl_2(e,1,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("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;
"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"});const c=80;class n{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:h(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]),n=Buffer.from([t.OPCODES.OP_EQUALVERIFY,t.OPCODES.OP_CHECKSIG]),a=Buffer.concat([c,o,n]),u=h(a);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${u.toString("hex")}`),{address:i,scriptPublickey:a,redeemHash:u}}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:h(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 n=e.bech32.encode(i.bech32,c),a=Buffer.from([t.OPCODES.OP_0,20]),u=Buffer.concat([a,s]),p=h(u);return null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${p.toString("hex")}`),{address:n,scriptPublickey:u,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=h(i);null===exports.logger||void 0===exports.logger||exports.logger.info(`redeem hash: ${c.toString("hex")}`);const n=Buffer.from([t.OPCODES.OP_HASH160,20]),a=Buffer.from([t.OPCODES.OP_EQUAL]);return{redeem:i,scriptPubicKey:Buffer.concat([n,c,a])}}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 n=t.coinmap[r],h=e.bech32.toWords(s);h.unshift(1);const a=e.bech32m.encode(n.bech32,h),u=Buffer.from([c+1,32]);return{address:a,scriptPublickey:Buffer.concat([u,s])}}static decode(r,o){const s=t.coinmap[r];if(s.bech32&&o.startsWith(s.bech32)){let t;switch(o.slice(s.bech32.length+1)[0]){case"p":t=e.bech32m.decode(o);break;default: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),i=e[0],c=e.slice(1);if(i===s.scriptHash)return this.p2sh(r,c).scriptPublickey;if(i===s.pubKeyHash)return this.p2pkh(r,{hash:c}).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&&e[0]===c+1&&32===e[1]}}function h(e){const t=Buffer.from((0,r.sha256)().update(e).digest());return Buffer.from((0,r.ripemd160)().update(t).digest())}exports.PaymentBTC=n,n.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=h;

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

"use strict";var t,i,e,s,r,n,o,p,a,u,c,h,f=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},l=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 d=require("./parser"),S=require("bip174/src/lib/utils"),g=require("secp256k1/elliptic"),y=require("hash.js"),P=require("./script"),w=require("@secux/utility"),b=require("./interface"),T=require("./utils"),m=require("./payment"),k=require("./transaction"),v=require("./coindef"),H=require("../../protocol-transaction/lib/protocol-transaction"),E=require("./bip340"),x=null===w.Logger||void 0===w.Logger?void 0:w.Logger.child({id:"psbt"});class W{constructor(p,a=new d.Psbtv2(new I)){t.add(this),i.set(this,void 0),e.set(this,void 0),s.set(this,void 0),r.set(this,[]),n.set(this,void 0),o.set(this,{}),f(this,i,a,"f"),f(this,e,p,"f"),f(this,s,(0,T.getPayment)(l(this,e,"f")),"f"),f(this,n,l(this,i,"f").globalMap.unsignedTx.tx,"f"),p===b.CoinType.BITCOINCASH&&(l(this,n,"f").version=1)}static FromBuffer(t,i){const e=d.Psbtv2.fromBuffer(t,(t=>new I(t)));return new W(i,e)}AddInput(t){var n;if(!(0,w.isSupportedCoin)(t.path))throw Error(`ArgumentError: unsupport bip32 path, got "${t.path}"`);const o={},p={},a=(0,T.getPublickey)(t.publickey),u=null!==(n=t.script)&&void 0!==n?n:(0,T.getDefaultScript)(t.path);switch(u){case b.ScriptType.P2PKH:o.witnessUtxo={script:l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:a}).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2SH_P2PKH:const r=l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:a});p.redeemScript=r.scriptPublickey,o.witnessUtxo={script:l(this,s,"f").p2sh(l(this,e,"f"),r.redeemHash).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2SH_P2WPKH:const n=l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:a});p.redeemScript=n.scriptPublickey,o.witnessUtxo={script:l(this,s,"f").p2sh(l(this,e,"f"),n.redeemHash).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2WPKH:o.witnessUtxo={script:l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:a}).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2TR:o.witnessUtxo={script:l(this,s,"f").p2tr(l(this,e,"f"),{publickey:a}).scriptPublickey,value:t.satoshis};break;default:throw Error(`ArgumentError: Invalid ScriptType of input#${l(this,i,"f").inputs.length}, got "${b.ScriptType[u]}"`)}if(t.txHex){const s=k.Transaction.fromBuffer(Buffer.from(t.txHex,"hex"));if((0,T.getSerializer)(l(this,e,"f")).getId(s)!==t.hash)throw Error(`UTXO hash for input #${l(this,i,"f").inputs.length} doesn't match the hash specified in the prevout`);const r=s.outs[t.vout];if(r.value!==t.satoshis)throw Error(`UTXO value for input #${l(this,i,"f").inputs.length} doesn't match the value specified in the prevout`);r.script.equals(o.witnessUtxo.script)||null==x||x.warn(`Input script generation error: ${r.script.toString("hex")}, got "${o.witnessUtxo.script}"`)}const c=Object.assign(Object.assign({hash:t.hash,index:t.vout},o),p);return l(this,i,"f").addInput(c),l(this,r,"f").push(t.path),this}AddInputs(t){for(const i of t)this.AddInput(i);return this}AddOutput(t){var r;let n,o,p,a=t.satoshis;if(n=(0,b.isOutuptScriptExtended)(t)){const t=(0,T.getPublickey)(n.publickey);p=n.path;const a=null!==(r=n.script)&&void 0!==r?r:(0,T.getDefaultScript)(p);let u,c;switch(a){case b.ScriptType.P2SH_P2WPKH:if(!n.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");u=l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:t}).redeemHash,c=l(this,s,"f").p2sh(l(this,e,"f"),u),o=c.scriptPublickey;break;case b.ScriptType.P2SH_P2PKH:if(!n.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");u=l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:t}).redeemHash,c=l(this,s,"f").p2sh(l(this,e,"f"),u),o=c.scriptPublickey;break;case b.ScriptType.P2PKH:if(!n.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");o=l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:t}).scriptPublickey;break;case b.ScriptType.P2WPKH:if(!n.path.startsWith("m/84'/"))throw Error("P2WPKH should use m/84' path");o=l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:t}).scriptPublickey;break;case b.ScriptType.P2TR:if(!n.path.startsWith("m/86'/"))throw Error("P2TR should use m/86' path");o=l(this,s,"f").p2tr(l(this,e,"f"),{publickey:t}).scriptPublickey;break;default:throw Error(`ArgumentError: Invalid ScriptType of output#${l(this,i,"f").outputs.length}, got "${b.ScriptType[a]}"`)}}else if(n=(0,b.isOutputAddress)(t))o=l(this,s,"f").decode(l(this,e,"f"),n.address);else{if(!(n=(0,b.isOutuptScript)(t)))throw Error("Invalid parameter of output");o=Buffer.from(n.scriptHex,"hex")}return l(this,i,"f").addOutput({script:o,value:a,path:p}),this}AddOutputs(t){for(const i of t)this.AddOutput(i);return this}PrepareSign(s){if(s){const e=l(this,t,"m",c).call(this),r=Math.round(e*s);null==x||x.info(`Estimated fee is ${r}, with ${s} fee rates.`);const o=l(this,i,"f").inputs.reduce(((t,i)=>t+i.witnessUtxo.value),0),p=l(this,n,"f").outs[0].value,a=1===l(this,i,"f").outputs.length?0:l(this,n,"f").outs[1].value,u=o-p-a;if(u<r&&(null==x||x.warn(`Estimated fee is ${r}, but got ${u}.`)),u>r||u<e)if(0!==a){const t=o-p-r;if(t<0)throw Error(`Insufficient amount, expect ${p+r}, but got ${o}.`);l(this,n,"f").outs[1].value=t,null==x||x.info(`Modify change amount from ${a} to ${t}.`)}else{const t=o-r;if(t<0)throw Error(`Insufficient amount, expect at least ${r}, but got ${o}.`);l(this,n,"f").outs[0].value=t,null==x||x.info(`Modify spend amount from ${p} to ${t}.`)}}const{type:o}=l(this,t,"m",p).call(this,0),u=l(this,e,"f")!==b.CoinType.BITCOINCASH&&o===b.ScriptType.P2PKH?Buffer.alloc(0):Buffer.from([l(this,i,"f").outputs.length,...(0,w.BigIntToBuffer)(l(this,n,"f").outs[0].value,8),l(this,n,"f").outs[0].script.length,...l(this,n,"f").outs[0].script,...l(this,n,"f").outs.slice(1).reduce(((t,i)=>[...t,...(0,w.BigIntToBuffer)(i.value,8),...(0,w.buildPathBuffer)(i.path).pathBuffer]),[])]),h=l(this,i,"f").inputs.map(((i,e)=>{const s=l(this,t,"m",a).call(this,e);return null==x||x.debug(`tx data [${e}]: ${s.toString("hex")}`),s}));null==x||x.debug(`confirm data: ${u.toString("hex")}`);return{commandData:H.SecuxTransactionTool.signRawTransactionList(l(this,r,"f"),h,u),rawTx:l(this,i,"f").toHex()}}appendSignature(s,r){var n,o;if(s.length!==r.length)throw Error(`ArgumentError: each signature is correspond to one publickey, got ${s.length} signatures and ${r.length} publickeys`);for(let u=0;u<l(this,i,"f").inputs.length;u++){const c=l(this,t,"m",a).call(this,u);if(l(this,t,"m",p).call(this,u).type!==b.ScriptType.P2TR){const t=l(this,e,"f")===b.CoinType.GROESTL?Buffer.from((0,y.sha256)().update(c).digest()):Buffer.from((0,y.sha256)().update((0,y.sha256)().update(c).digest()).digest()),o=r[u];if(!g.ecdsaVerify(s[u],t,o))throw Error(`Signature Error #${u}`);let p=null!==(n=l(this,i,"f").inputs[u].sighashType)&&void 0!==n?n:k.Transaction.SIGHASH_ALL;l(this,e,"f")===b.CoinType.BITCOINCASH&&(p|=64);const a=[{pubkey:o,signature:P.encode(s[u],p)}];l(this,i,"f").updateInput(u,{partialSig:a})}else{const t=(0,T.taggedHash)("TapSighash",c),e=(0,T.toTweakedPublickey)(r[u]),n=s[u].slice(0,64);if(!(0,E.taprootVerify)(n,t,e))throw Error(`Signature Error #${u}`);const p=null!==(o=l(this,i,"f").inputs[u].sighashType)&&void 0!==o?o:k.Transaction.SIGHASH_DEFAULT;l(this,i,"f").inputs[u].partialSig=[{pubkey:e,signature:p===k.Transaction.SIGHASH_DEFAULT?n:Buffer.from([p,...n])}]}}return this}finalizeAllInputs(){if(l(this,i,"f").inputs.length<1)throw Error("utxo input cannot be empty");return l(this,i,"f").inputs.forEach(((t,s)=>{(0,S.checkForInput)(l(this,i,"f").inputs,s);const{script:r,scriptType:n}=$(t,l(this,e,"f"));if(!r)throw new Error(`No script found for input #${s}`);if(t.sighashType&&t.partialSig){const{partialSig:i,sighashType:e}=t;for(const t of i){const{hashType:i}=P.decode(t.signature);if(i!==e)throw new Error("Signature sighash does not match input sighash type")}}const{finalScriptSig:o,finalScriptWitness:p}=function(t,i){let e,s;const{signature:r,pubkey:n}=i[0];switch(t){case b.ScriptType.P2PKH:e=P.compile([r,n]);break;case b.ScriptType.P2SH_P2PKH:e=(()=>{const t=P.compile([r,n]),i=P.decompile(t),e={output:i[i.length-1],input:P.compile(i.slice(0,-1))};return P.compile([].concat(P.decompile(e.input),e.output))})();break;case b.ScriptType.P2SH_P2WPKH:s=(0,T.witnessStackToScriptWitness)([r,n]),e=(()=>{const t=Buffer.alloc(0),i=(0,m.Hash160)(n),e=P.compile([v.OPCODES.OP_0,i]);return P.compile([].concat(P.decompile(t),e))})();break;case b.ScriptType.P2WPKH:s=(0,T.witnessStackToScriptWitness)([r,n]);break;case b.ScriptType.P2TR:s=r}return{finalScriptSig:e,finalScriptWitness:s}}(n,t.partialSig);if(o&&l(this,i,"f").updateInput(s,{finalScriptSig:o}),p&&l(this,i,"f").updateInput(s,{finalScriptWitness:p}),!o&&!p)throw new Error(`Unknown error finalizing input #${s}`)})),this}extractTransaction(){const e=l(this,n,"f").clone();l(this,i,"f").inputs.forEach(((i,s)=>l(this,t,"m",u).call(this,e,s,i)));const s=l(this,i,"f").inputs.reduce(((t,i)=>t+i.witnessUtxo.value),0)-e.outs.reduce(((t,i)=>t+i.value),0),r=e.virtualSize();if(s<r)throw Error(`Transaction fee must >= ${r}, but got ${s}.`);return e}}function $(t,i){let e,s;const r=(0,T.getPayment)(i);if(t.witnessScript)e=t.witnessScript;else if(t.redeemScript)switch(e=t.redeemScript,s=r.classify(e),s){case b.ScriptType.P2PKH:s=b.ScriptType.P2SH_P2PKH;break;case b.ScriptType.P2WPKH:s=b.ScriptType.P2SH_P2WPKH}else e=t.witnessUtxo.script,s=r.classify(e);return{script:e,scriptType:s}}exports.SecuxPsbt=W,i=new WeakMap,e=new WeakMap,s=new WeakMap,r=new WeakMap,n=new WeakMap,o=new WeakMap,t=new WeakSet,p=function(t){if(l(this,o,"f")[t])return l(this,o,"f")[t];const s=l(this,i,"f").inputs[t],r=s.witnessUtxo,n=$(s,l(this,e,"f")).scriptType,p=function(t,i,e,s){let r;switch(i){case b.ScriptType.P2SH_P2PKH:case b.ScriptType.P2SH_P2WPKH:if(!e)throw Error("scriptPubkey is P2SH but redeemScript missing");r=e;break;default:r=t}if(!r)throw Error("cannot extract script");return r}(r.script,n,s.redeemScript,s.witnessScript);null==x||x.debug(`input #${t} script type: ${b.ScriptType[n]}`),null==x||x.debug(`script: ${p.toString("hex")}`);const a={type:n,scriptPubkey:p};return l(this,o,"f")[t]=a,a},a=function(s){var r,o;const a=l(this,i,"f").inputs[s],u=l(this,n,"f");let c=null!==(r=a.sighashType)&&void 0!==r?r:k.Transaction.SIGHASH_ALL;const h=(0,T.getSerializer)(l(this,e,"f"));void 0===a.witnessUtxo&&Error("Need a Utxo input item for signing");const f=a.witnessUtxo,{type:d,scriptPubkey:S}=l(this,t,"m",p).call(this,s);let g;switch(d){case b.ScriptType.P2WPKH:case b.ScriptType.P2SH_P2WPKH:case b.ScriptType.P2SH_P2PKH:null==x||x.debug(b.ScriptType[d]);const r=(0,T.getPayment)(l(this,e,"f")).p2pkh(l(this,e,"f"),{hash:S.slice(2)}).scriptPublickey;g=h.dataForWitnessV0(u,s,r,f.value,c);break;case b.ScriptType.P2TR:null==x||x.debug("p2tr"),c=null!==(o=a.sighashType)&&void 0!==o?o:k.Transaction.SIGHASH_DEFAULT,g=h.dataForWitnessV1(u,s,l(this,i,"f").inputs.map(((i,e)=>l(this,t,"m",p).call(this,e).scriptPubkey)),l(this,i,"f").inputs.map((t=>t.witnessUtxo.value)),c);break;default:l(this,e,"f")===b.CoinType.BITCOINCASH?(null==x||x.debug("bch using bip143"),g=h.dataForWitnessV0(u,s,S,f.value,64|c)):(null==x||x.debug("non-segwit"),g=h.dataForSignature(u,s,S,c))}return g},u=function(i,e,s){if(!s.finalScriptSig&&!s.finalScriptWitness)throw Error(`input#${e} not finalized.`);s.finalScriptSig&&(i.ins[e].script=s.finalScriptSig),s.finalScriptWitness&&(l(this,t,"m",p).call(this,e).type!==b.ScriptType.P2TR?i.ins[e].witness=(0,T.scriptWitnessToWitnessStack)(s.finalScriptWitness):i.ins[e].witness=[s.finalScriptWitness])},c=function(){const e=l(this,n,"f").clone();let s=0,r=2;return l(this,i,"f").inputs.forEach(((i,e)=>{const{type:n}=l(this,t,"m",p).call(this,e);s+=(0,T.getInScriptSize)(n);const o=(0,T.getWitnessSize)(n,i.sighashType);r+=(0,T.vectorSize)(o)})),e.virtualSize()+s+r/4};class I{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){h.set(this,void 0),f(this,h,k.Transaction.fromBuffer(t),"f")}getInputOutputCounts(){return{inputCount:l(this,h,"f").ins.length,outputCount:l(this,h,"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;l(this,h,"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.");l(this,h,"f").addOutput(t.script,t.value,t.path)}toBuffer(){return l(this,h,"f").toBuffer()}get tx(){return l(this,h,"f")}}h=new WeakMap;
"use strict";var t,i,e,s,r,n,o,p,a,u,c,h,f=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},l=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 d=require("./parser"),S=require("bip174/src/lib/utils"),g=require("secp256k1/elliptic"),y=require("hash.js"),P=require("./script"),w=require("@secux/utility"),b=require("./interface"),T=require("./utils"),m=require("./payment"),v=require("./transaction"),k=require("./coindef"),H=require("../../protocol-transaction/lib/protocol-transaction"),E=require("./bip340"),x=null===w.Logger||void 0===w.Logger?void 0:w.Logger.child({id:"psbt"}),W=64;class ${constructor(p,a=new d.Psbtv2(new B)){t.add(this),i.set(this,void 0),e.set(this,void 0),s.set(this,void 0),r.set(this,[]),n.set(this,void 0),o.set(this,{}),f(this,i,a,"f"),f(this,e,p,"f"),f(this,s,(0,T.getPayment)(l(this,e,"f")),"f"),f(this,n,l(this,i,"f").globalMap.unsignedTx.tx,"f"),p===b.CoinType.BITCOINCASH&&(l(this,n,"f").version=1)}static FromBuffer(t,i){const e=d.Psbtv2.fromBuffer(t,t=>new B(t));return new $(i,e)}AddInput(t){var n;if(!(0,w.isSupportedCoin)(t.path))throw Error(`ArgumentError: unsupport bip32 path, got "${t.path}"`);const o={},p={},a=(0,T.getPublickey)(t.publickey),u=null!==(n=t.script)&&void 0!==n?n:(0,T.getDefaultScript)(t.path);switch(u){case b.ScriptType.P2PKH:o.witnessUtxo={script:l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:a}).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2SH_P2PKH:const r=l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:a});p.redeemScript=r.scriptPublickey,o.witnessUtxo={script:l(this,s,"f").p2sh(l(this,e,"f"),r.redeemHash).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2SH_P2WPKH:const n=l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:a});p.redeemScript=n.scriptPublickey,o.witnessUtxo={script:l(this,s,"f").p2sh(l(this,e,"f"),n.redeemHash).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2WPKH:o.witnessUtxo={script:l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:a}).scriptPublickey,value:t.satoshis};break;case b.ScriptType.P2TR:o.witnessUtxo={script:l(this,s,"f").p2tr(l(this,e,"f"),{publickey:a}).scriptPublickey,value:t.satoshis};break;default:throw Error(`ArgumentError: Invalid ScriptType of input#${l(this,i,"f").inputs.length}, got "${b.ScriptType[u]}"`)}if(t.txHex){const s=v.Transaction.fromBuffer(Buffer.from(t.txHex,"hex"));if((0,T.getSerializer)(l(this,e,"f")).getId(s)!==t.hash)throw Error(`UTXO hash for input #${l(this,i,"f").inputs.length} doesn't match the hash specified in the prevout`);const r=s.outs[t.vout];if(r.value!==t.satoshis)throw Error(`UTXO value for input #${l(this,i,"f").inputs.length} doesn't match the value specified in the prevout`);r.script.equals(o.witnessUtxo.script)||null===x||void 0===x||x.warn(`Input script generation error: ${r.script.toString("hex")}, got "${o.witnessUtxo.script}"`)}const c=Object.assign(Object.assign({hash:t.hash,index:t.vout},o),p);return l(this,i,"f").addInput(c),l(this,r,"f").push(t.path),this}AddInputs(t){for(const i of t)this.AddInput(i);return this}AddOutput(t){var r;let n,o,p,a=t.satoshis;if(n=(0,b.isOutuptScriptExtended)(t)){const t=(0,T.getPublickey)(n.publickey);p=n.path;const a=null!==(r=n.script)&&void 0!==r?r:(0,T.getDefaultScript)(p);let u,c;switch(a){case b.ScriptType.P2SH_P2WPKH:if(!n.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");u=l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:t}).redeemHash,o=(c=l(this,s,"f").p2sh(l(this,e,"f"),u)).scriptPublickey;break;case b.ScriptType.P2SH_P2PKH:if(!n.path.startsWith("m/49'/"))throw Error("P2SH(...) should use m/49' path");u=l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:t}).redeemHash,o=(c=l(this,s,"f").p2sh(l(this,e,"f"),u)).scriptPublickey;break;case b.ScriptType.P2PKH:if(!n.path.startsWith("m/44'/"))throw Error("P2PKH should use m/44' path");o=l(this,s,"f").p2pkh(l(this,e,"f"),{publickey:t}).scriptPublickey;break;case b.ScriptType.P2WPKH:if(!n.path.startsWith("m/84'/"))throw Error("P2WPKH should use m/84' path");o=l(this,s,"f").p2wpkh(l(this,e,"f"),{publickey:t}).scriptPublickey;break;case b.ScriptType.P2TR:if(!n.path.startsWith("m/86'/"))throw Error("P2TR should use m/86' path");o=l(this,s,"f").p2tr(l(this,e,"f"),{publickey:t}).scriptPublickey;break;default:throw Error(`ArgumentError: Invalid ScriptType of output#${l(this,i,"f").outputs.length}, got "${b.ScriptType[a]}"`)}}else if(n=(0,b.isOutputAddress)(t))o=l(this,s,"f").decode(l(this,e,"f"),n.address);else{if(!(n=(0,b.isOutuptScript)(t)))throw Error("Invalid parameter of output");o=Buffer.from(n.scriptHex,"hex")}return l(this,i,"f").addOutput({script:o,value:a,path:p}),this}AddOutputs(t){for(const i of t)this.AddOutput(i);return this}PrepareSign(s){if(s){const e=l(this,t,"m",c).call(this),r=Math.round(e*s);null===x||void 0===x||x.info(`Estimated fee is ${r}, with ${s} fee rates.`);const o=l(this,i,"f").inputs.reduce((t,i)=>t+i.witnessUtxo.value,0),p=l(this,n,"f").outs[0].value,a=1===l(this,i,"f").outputs.length?0:l(this,n,"f").outs[1].value,u=o-p-a;if(u<r&&(null===x||void 0===x||x.warn(`Estimated fee is ${r}, but got ${u}.`)),u>r||u<e)if(0!==a){const t=o-p-r;if(t<0)throw Error(`Insufficient amount, expect ${p+r}, but got ${o}.`);l(this,n,"f").outs[1].value=t,null===x||void 0===x||x.info(`Modify change amount from ${a} to ${t}.`)}else{const t=o-r;if(t<0)throw Error(`Insufficient amount, expect at least ${r}, but got ${o}.`);l(this,n,"f").outs[0].value=t,null===x||void 0===x||x.info(`Modify spend amount from ${p} to ${t}.`)}}const{type:o}=l(this,t,"m",p).call(this,0),u=l(this,e,"f")!==b.CoinType.BITCOINCASH&&o===b.ScriptType.P2PKH?Buffer.alloc(0):Buffer.from([l(this,i,"f").outputs.length,...(0,w.BigIntToBuffer)(l(this,n,"f").outs[0].value,8),l(this,n,"f").outs[0].script.length,...l(this,n,"f").outs[0].script,...l(this,n,"f").outs.slice(1).reduce((t,i)=>[...t,...(0,w.BigIntToBuffer)(i.value,8),...(0,w.buildPathBuffer)(i.path).pathBuffer],[])]),h=l(this,i,"f").inputs.map((i,e)=>{const s=l(this,t,"m",a).call(this,e);return null===x||void 0===x||x.debug(`tx data [${e}]: ${s.toString("hex")}`),s});return null===x||void 0===x||x.debug(`confirm data: ${u.toString("hex")}`),{commandData:H.SecuxTransactionTool.signRawTransactionList(l(this,r,"f"),h,u),rawTx:l(this,i,"f").toHex()}}appendSignature(s,r){var n,o;if(s.length!==r.length)throw Error(`ArgumentError: each signature is correspond to one publickey, got ${s.length} signatures and ${r.length} publickeys`);for(let u=0;u<l(this,i,"f").inputs.length;u++){const c=l(this,t,"m",a).call(this,u);if(l(this,t,"m",p).call(this,u).type!==b.ScriptType.P2TR){const t=l(this,e,"f")===b.CoinType.GROESTL?Buffer.from((0,y.sha256)().update(c).digest()):Buffer.from((0,y.sha256)().update((0,y.sha256)().update(c).digest()).digest()),o=r[u];if(!g.ecdsaVerify(s[u],t,o))throw Error(`Signature Error #${u}`);let p=null!==(n=l(this,i,"f").inputs[u].sighashType)&&void 0!==n?n:v.Transaction.SIGHASH_ALL;l(this,e,"f")===b.CoinType.BITCOINCASH&&(p|=W);const a=[{pubkey:o,signature:P.encode(s[u],p)}];l(this,i,"f").updateInput(u,{partialSig:a})}else{const t=(0,T.taggedHash)("TapSighash",c),e=(0,T.toTweakedPublickey)(r[u]),n=s[u].slice(0,64);if(!(0,E.taprootVerify)(n,t,e))throw Error(`Signature Error #${u}`);const p=null!==(o=l(this,i,"f").inputs[u].sighashType)&&void 0!==o?o:v.Transaction.SIGHASH_DEFAULT;l(this,i,"f").inputs[u].partialSig=[{pubkey:e,signature:p===v.Transaction.SIGHASH_DEFAULT?n:Buffer.from([p,...n])}]}}return this}finalizeAllInputs(){if(l(this,i,"f").inputs.length<1)throw Error("utxo input cannot be empty");return l(this,i,"f").inputs.forEach((t,s)=>{(0,S.checkForInput)(l(this,i,"f").inputs,s);const{script:r,scriptType:n}=I(t,l(this,e,"f"));if(!r)throw new Error(`No script found for input #${s}`);if(t.sighashType&&t.partialSig){const{partialSig:i,sighashType:e}=t;for(const t of i){const{hashType:i}=P.decode(t.signature);if(i!==e)throw new Error("Signature sighash does not match input sighash type")}}const{finalScriptSig:o,finalScriptWitness:p}=function(t,i){let e,s;const{signature:r,pubkey:n}=i[0];switch(t){case b.ScriptType.P2PKH:e=P.compile([r,n]);break;case b.ScriptType.P2SH_P2PKH:e=(()=>{const t=P.compile([r,n]),i=P.decompile(t),e={output:i[i.length-1],input:P.compile(i.slice(0,-1))};return P.compile([].concat(P.decompile(e.input),e.output))})();break;case b.ScriptType.P2SH_P2WPKH:s=(0,T.witnessStackToScriptWitness)([r,n]),e=(()=>{const t=Buffer.alloc(0),i=(0,m.Hash160)(n),e=P.compile([k.OPCODES.OP_0,i]);return P.compile([].concat(P.decompile(t),e))})();break;case b.ScriptType.P2WPKH:s=(0,T.witnessStackToScriptWitness)([r,n]);break;case b.ScriptType.P2TR:s=r}return{finalScriptSig:e,finalScriptWitness:s}}(n,t.partialSig);if(o&&l(this,i,"f").updateInput(s,{finalScriptSig:o}),p&&l(this,i,"f").updateInput(s,{finalScriptWitness:p}),!o&&!p)throw new Error(`Unknown error finalizing input #${s}`)}),this}extractTransaction(){const e=l(this,n,"f").clone();l(this,i,"f").inputs.forEach((i,s)=>l(this,t,"m",u).call(this,e,s,i));const s=l(this,i,"f").inputs.reduce((t,i)=>t+i.witnessUtxo.value,0)-e.outs.reduce((t,i)=>t+i.value,0),r=e.virtualSize();if(s<r)throw Error(`Transaction fee must >= ${r}, but got ${s}.`);return e}}function I(t,i){let e,s;const r=(0,T.getPayment)(i);if(t.witnessScript)e=t.witnessScript;else if(t.redeemScript)switch(e=t.redeemScript,s=r.classify(e)){case b.ScriptType.P2PKH:s=b.ScriptType.P2SH_P2PKH;break;case b.ScriptType.P2WPKH:s=b.ScriptType.P2SH_P2WPKH}else e=t.witnessUtxo.script,s=r.classify(e);return{script:e,scriptType:s}}exports.SecuxPsbt=$,i=new WeakMap,e=new WeakMap,s=new WeakMap,r=new WeakMap,n=new WeakMap,o=new WeakMap,t=new WeakSet,p=function(t){if(l(this,o,"f")[t])return l(this,o,"f")[t];const s=l(this,i,"f").inputs[t],r=s.witnessUtxo,n=I(s,l(this,e,"f")).scriptType,p=function(t,i,e,s){let r;switch(i){case b.ScriptType.P2SH_P2PKH:case b.ScriptType.P2SH_P2WPKH:if(!e)throw Error("scriptPubkey is P2SH but redeemScript missing");r=e;break;default:r=t}if(!r)throw Error("cannot extract script");return r}(r.script,n,s.redeemScript,s.witnessScript);null===x||void 0===x||x.debug(`input #${t} script type: ${b.ScriptType[n]}`),null===x||void 0===x||x.debug(`script: ${p.toString("hex")}`);const a={type:n,scriptPubkey:p};return l(this,o,"f")[t]=a,a},a=function(s){var r,o;const a=l(this,i,"f").inputs[s],u=l(this,n,"f");let c=null!==(r=a.sighashType)&&void 0!==r?r:v.Transaction.SIGHASH_ALL;const h=(0,T.getSerializer)(l(this,e,"f"));void 0===a.witnessUtxo&&Error("Need a Utxo input item for signing");const f=a.witnessUtxo,{type:d,scriptPubkey:S}=l(this,t,"m",p).call(this,s);let g;switch(d){case b.ScriptType.P2WPKH:case b.ScriptType.P2SH_P2WPKH:case b.ScriptType.P2SH_P2PKH:null===x||void 0===x||x.debug(b.ScriptType[d]);const r=(0,T.getPayment)(l(this,e,"f")).p2pkh(l(this,e,"f"),{hash:S.slice(2)}).scriptPublickey;g=h.dataForWitnessV0(u,s,r,f.value,c);break;case b.ScriptType.P2TR:null===x||void 0===x||x.debug("p2tr"),c=null!==(o=a.sighashType)&&void 0!==o?o:v.Transaction.SIGHASH_DEFAULT,g=h.dataForWitnessV1(u,s,l(this,i,"f").inputs.map((i,e)=>l(this,t,"m",p).call(this,e).scriptPubkey),l(this,i,"f").inputs.map(t=>t.witnessUtxo.value),c);break;default:l(this,e,"f")===b.CoinType.BITCOINCASH?(null===x||void 0===x||x.debug("bch using bip143"),g=h.dataForWitnessV0(u,s,S,f.value,c|W)):(null===x||void 0===x||x.debug("non-segwit"),g=h.dataForSignature(u,s,S,c))}return g},u=function(i,e,s){if(!s.finalScriptSig&&!s.finalScriptWitness)throw Error(`input#${e} not finalized.`);s.finalScriptSig&&(i.ins[e].script=s.finalScriptSig),s.finalScriptWitness&&(l(this,t,"m",p).call(this,e).type!==b.ScriptType.P2TR?i.ins[e].witness=(0,T.scriptWitnessToWitnessStack)(s.finalScriptWitness):i.ins[e].witness=[s.finalScriptWitness])},c=function(){const e=l(this,n,"f").clone();let s=0,r=2;return l(this,i,"f").inputs.forEach((i,e)=>{const{type:n}=l(this,t,"m",p).call(this,e);s+=(0,T.getInScriptSize)(n);const o=(0,T.getWitnessSize)(n,i.sighashType);r+=(0,T.vectorSize)(o)}),e.virtualSize()+s+r/4};class B{constructor(t=Buffer.from([2,0,0,0,0,0,0,0,0,0])){h.set(this,void 0),f(this,h,v.Transaction.fromBuffer(t),"f")}getInputOutputCounts(){return{inputCount:l(this,h,"f").ins.length,outputCount:l(this,h,"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;l(this,h,"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.");l(this,h,"f").addOutput(t.script,t.value,t.path)}toBuffer(){return l(this,h,"f").toBuffer()}get tx(){return l(this,h,"f")}}h=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/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}}}
"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||void 0===o||o.warn(`decompile error: reading a pushDataInt fail, got ${e.toString("binary")}, index:${r}`),[];if((r+=t.size)+t.number>e.length)return null===o||void 0===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}}}

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TransactionGRS=void 0;const e=require("hash.js"),r=require("./transaction"),t=require("./bufferutils"),i=Buffer.from("0000000000000000000000000000000000000000000000000000000000000000","hex");class s extends r.Transaction{static dataForWitnessV0(s,n,a,f,c){let o,u=Buffer.from([]),l=i,S=i,w=i;if(c&r.Transaction.SIGHASH_ANYONECANPAY||(u=Buffer.allocUnsafe(36*s.ins.length),o=new t.BufferWriter(u,0),s.ins.forEach((e=>{o.writeSlice(e.hash),o.writeUInt32(e.index)})),S=Buffer.from((0,e.sha256)().update(u).digest())),c&r.Transaction.SIGHASH_ANYONECANPAY||(31&c)===r.Transaction.SIGHASH_SINGLE||(31&c)===r.Transaction.SIGHASH_NONE||(u=Buffer.allocUnsafe(4*s.ins.length),o=new t.BufferWriter(u,0),s.ins.forEach((e=>{o.writeUInt32(e.sequence)})),w=Buffer.from((0,e.sha256)().update(u).digest())),(31&c)!==r.Transaction.SIGHASH_SINGLE&&(31&c)!==r.Transaction.SIGHASH_NONE){const i=s.outs.reduce(((e,t)=>e+8+(0,r.varSliceSize)(t.script)),0);u=Buffer.allocUnsafe(i),o=new t.BufferWriter(u,0),s.outs.forEach((e=>{o.writeUInt64(e.value),o.writeVarSlice(e.script)})),l=Buffer.from((0,e.sha256)().update(u).digest())}else if((31&c)===r.Transaction.SIGHASH_SINGLE&&n<s.outs.length){const i=s.outs[n];u=Buffer.allocUnsafe(8+(0,r.varSliceSize)(i.script)),o=new t.BufferWriter(u,0),o.writeUInt64(i.value),o.writeVarSlice(i.script),l=Buffer.from((0,e.sha256)().update(u).digest())}u=Buffer.allocUnsafe(156+(0,r.varSliceSize)(a)),o=new t.BufferWriter(u,0);const d=s.ins[n];return o.writeUInt32(s.version),o.writeSlice(S),o.writeSlice(w),o.writeSlice(d.hash),o.writeUInt32(d.index),o.writeVarSlice(a),o.writeUInt64(f),o.writeUInt32(d.sequence),o.writeSlice(l),o.writeUInt32(s.locktime),o.writeUInt32(c),u}static getHash(r,t){if(t&&r.isCoinbase())return Buffer.alloc(32,0);const i=r.toBuffer(void 0,void 0,t);return Buffer.from((0,e.sha256)().update(i).digest())}}exports.TransactionGRS=s;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TransactionGRS=void 0;const e=require("hash.js"),r=require("./transaction"),t=require("./bufferutils"),i=Buffer.from("0000000000000000000000000000000000000000000000000000000000000000","hex");exports.TransactionGRS=class extends r.Transaction{static dataForWitnessV0(s,n,a,f,c){let o,u=Buffer.from([]),l=i,S=i,w=i;if(c&r.Transaction.SIGHASH_ANYONECANPAY||(u=Buffer.allocUnsafe(36*s.ins.length),o=new t.BufferWriter(u,0),s.ins.forEach(e=>{o.writeSlice(e.hash),o.writeUInt32(e.index)}),S=Buffer.from((0,e.sha256)().update(u).digest())),c&r.Transaction.SIGHASH_ANYONECANPAY||(31&c)===r.Transaction.SIGHASH_SINGLE||(31&c)===r.Transaction.SIGHASH_NONE||(u=Buffer.allocUnsafe(4*s.ins.length),o=new t.BufferWriter(u,0),s.ins.forEach(e=>{o.writeUInt32(e.sequence)}),w=Buffer.from((0,e.sha256)().update(u).digest())),(31&c)!==r.Transaction.SIGHASH_SINGLE&&(31&c)!==r.Transaction.SIGHASH_NONE){const i=s.outs.reduce((e,t)=>e+8+(0,r.varSliceSize)(t.script),0);u=Buffer.allocUnsafe(i),o=new t.BufferWriter(u,0),s.outs.forEach(e=>{o.writeUInt64(e.value),o.writeVarSlice(e.script)}),l=Buffer.from((0,e.sha256)().update(u).digest())}else if((31&c)===r.Transaction.SIGHASH_SINGLE&&n<s.outs.length){const i=s.outs[n];u=Buffer.allocUnsafe(8+(0,r.varSliceSize)(i.script)),(o=new t.BufferWriter(u,0)).writeUInt64(i.value),o.writeVarSlice(i.script),l=Buffer.from((0,e.sha256)().update(u).digest())}u=Buffer.allocUnsafe(156+(0,r.varSliceSize)(a)),o=new t.BufferWriter(u,0);const d=s.ins[n];return o.writeUInt32(s.version),o.writeSlice(S),o.writeSlice(w),o.writeSlice(d.hash),o.writeUInt32(d.index),o.writeVarSlice(a),o.writeUInt64(f),o.writeUInt32(d.sequence),o.writeSlice(l),o.writeUInt32(s.locktime),o.writeUInt32(c),u}static getHash(r,t){if(t&&r.isCoinbase())return Buffer.alloc(32,0);const i=r.toBuffer(void 0,void 0,t);return Buffer.from((0,e.sha256)().update(i).digest())}};

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.varSliceSize=exports.Transaction=void 0;const e=require("hash.js"),t=require("varuint-bitcoin"),r=require("./bufferutils"),i=require("./script"),n=require("./coindef"),s=Buffer.from("0000000000000000000000000000000000000000000000000000000000000000","hex"),f=Buffer.from("0000000000000000000000000000000000000000000000000000000000000001","hex"),c=Buffer.alloc(0),o=Buffer.from("ffffffffffffffff","hex"),u={script:c,valueBuffer:o,value:0};class a{constructor(){this.ins=[],this.outs=[],this.version=2,this.locktime=0}static fromBuffer(e){const t=new r.BufferReader(e),i=new a;i.version=t.readInt32();const n=t.readUInt8(),s=t.readUInt8();let f=!1;n===a.ADVANCED_TRANSACTION_MARKER&&s===a.ADVANCED_TRANSACTION_FLAG?f=!0:t.offset-=2;const c=t.readVarInt();for(let e=0;e<c;++e)i.ins.push({hash:t.readSlice(32),index:t.readUInt32(),script:t.readVarSlice(),sequence:t.readUInt32(),witness:[]});const o=t.readVarInt();for(let e=0;e<o;++e)i.outs.push({value:t.readUInt64(),script:t.readVarSlice()});if(f){for(let e=0;e<c;++e)i.ins[e].witness=t.readVector();if(!i.hasWitnesses())throw new Error("Transaction has superfluous witness data")}if(i.locktime=t.readUInt32(),t.offset!==e.length)throw new Error("Transaction has unexpected data");return i}static dataForSignature(e,t,r,s){if(t>=e.ins.length)return f;const o=i.compile(i.decompile(r).filter((e=>e!==n.OPCODES.OP_CODESEPARATOR))),l=e.clone();if((31&s)===a.SIGHASH_NONE)l.outs=[],l.ins.forEach(((e,r)=>{r!==t&&(e.sequence=0)}));else if((31&s)===a.SIGHASH_SINGLE){if(t>=e.outs.length)return f;l.outs.length=t+1;for(let e=0;e<t;e++)l.outs[e]=u;l.ins.forEach(((e,r)=>{r!==t&&(e.sequence=0)}))}s&a.SIGHASH_ANYONECANPAY?(l.ins=[l.ins[t]],l.ins[0].script=o):(l.ins.forEach((e=>{e.script=c})),l.ins[t].script=o);const h=Buffer.allocUnsafe(l.byteLength(!1)+4);return h.writeInt32LE(s,h.length-4),l.toBuffer(h,0,!1),h}static dataForWitnessV0(e,t,i,n,f){let c,o=Buffer.from([]),u=s,l=s,w=s;if(f&a.SIGHASH_ANYONECANPAY||(o=Buffer.allocUnsafe(36*e.ins.length),c=new r.BufferWriter(o,0),e.ins.forEach((e=>{c.writeSlice(e.hash),c.writeUInt32(e.index)})),l=h(o)),f&a.SIGHASH_ANYONECANPAY||(31&f)===a.SIGHASH_SINGLE||(31&f)===a.SIGHASH_NONE||(o=Buffer.allocUnsafe(4*e.ins.length),c=new r.BufferWriter(o,0),e.ins.forEach((e=>{c.writeUInt32(e.sequence)})),w=h(o)),(31&f)!==a.SIGHASH_SINGLE&&(31&f)!==a.SIGHASH_NONE){const t=e.outs.reduce(((e,t)=>e+8+S(t.script)),0);o=Buffer.allocUnsafe(t),c=new r.BufferWriter(o,0),e.outs.forEach((e=>{c.writeUInt64(e.value),c.writeVarSlice(e.script)})),u=h(o)}else if((31&f)===a.SIGHASH_SINGLE&&t<e.outs.length){const i=e.outs[t];o=Buffer.allocUnsafe(8+S(i.script)),c=new r.BufferWriter(o,0),c.writeUInt64(i.value),c.writeVarSlice(i.script),u=h(o)}o=Buffer.allocUnsafe(156+S(i)),c=new r.BufferWriter(o,0);const I=e.ins[t];return c.writeUInt32(e.version),c.writeSlice(l),c.writeSlice(w),c.writeSlice(I.hash),c.writeUInt32(I.index),c.writeVarSlice(i),c.writeUInt64(n),c.writeUInt32(I.sequence),c.writeSlice(u),c.writeUInt32(e.locktime),c.writeUInt32(f),o}static dataForWitnessV1(e,t,i,n,s,f,o){if(n.length!==e.ins.length||i.length!==e.ins.length)throw new Error("Must supply prevout script and value for all inputs");const u=s===a.SIGHASH_DEFAULT?a.SIGHASH_ALL:s&a.SIGHASH_OUTPUT_MASK,h=(s&a.SIGHASH_INPUT_MASK)===a.SIGHASH_ANYONECANPAY,w=u===a.SIGHASH_NONE,I=u===a.SIGHASH_SINGLE;let A=c,d=c,E=c,U=c,p=c;if(!h){let t=Buffer.alloc(36*e.ins.length),s=new r.BufferWriter(t);e.ins.forEach((e=>{s.writeSlice(e.hash),s.writeUInt32(e.index)})),A=l(t),t=Buffer.alloc(8*e.ins.length),s=new r.BufferWriter(t),n.forEach((e=>s.writeUInt64(e))),d=l(t),t=Buffer.alloc(i.map(S).reduce(((e,t)=>e+t))),s=new r.BufferWriter(t),i.forEach((e=>s.writeVarSlice(e))),E=l(t),t=Buffer.alloc(4*e.ins.length),s=new r.BufferWriter(t),e.ins.forEach((e=>s.writeUInt32(e.sequence))),U=l(t)}if(w||I){if(I&&t<e.outs.length){const i=e.outs[t],n=Buffer.alloc(8+S(i.script)),s=new r.BufferWriter(n);s.writeUInt64(i.value),s.writeVarSlice(i.script),p=l(n)}}else{const t=e.outs.map((e=>8+S(e.script))).reduce(((e,t)=>e+t)),i=Buffer.alloc(t),n=new r.BufferWriter(i);e.outs.forEach((e=>{n.writeUInt64(e.value),n.writeVarSlice(e.script)})),p=l(i)}const N=(f?2:0)+(o?1:0),H=174-(h?49:0)-(w?32:0)+(o?32:0)+(f?37:0),g=Buffer.alloc(H),B=new r.BufferWriter(g);if(B.writeUInt8(s),B.writeInt32(e.version),B.writeUInt32(e.locktime),B.writeSlice(A),B.writeSlice(d),B.writeSlice(E),B.writeSlice(U),w||I||B.writeSlice(p),B.writeUInt8(N),h){const r=e.ins[t];B.writeSlice(r.hash),B.writeUInt32(r.index),B.writeUInt64(n[t]),B.writeVarSlice(i[t]),B.writeUInt32(r.sequence)}else B.writeUInt32(t);if(o){const e=Buffer.alloc(S(o));new r.BufferWriter(e).writeVarSlice(o),B.writeSlice(l(e))}return I&&B.writeSlice(p),f&&(B.writeSlice(f),B.writeUInt8(0),B.writeUInt32(4294967295)),Buffer.concat([Buffer.from([0]),g])}static getHash(e,t){return t&&e.isCoinbase()?Buffer.alloc(32,0):h(e.toBuffer(void 0,void 0,t))}static getId(e){return this.getHash(e).reverse().toString("hex")}addInput(e,t,r,i){r&&0!==r||(r=a.DEFAULT_SEQUENCE),this.ins.push({hash:e,index:t,script:null!=i?i:c,sequence:r,witness:[]})}addOutput(e,t,r){this.outs.push({script:e,value:t,path:r})}toBuffer(e,t,i=!1){e||(e=Buffer.allocUnsafe(this.byteLength(i)));const n=new r.BufferWriter(e,null!=t?t:0);n.writeInt32(this.version);const s=i&&this.hasWitnesses();return s&&(n.writeUInt8(a.ADVANCED_TRANSACTION_MARKER),n.writeUInt8(a.ADVANCED_TRANSACTION_FLAG)),n.writeVarInt(this.ins.length),this.ins.forEach((e=>{n.writeSlice(e.hash),n.writeUInt32(e.index),n.writeVarSlice(e.script),n.writeUInt32(e.sequence)})),n.writeVarInt(this.outs.length),this.outs.forEach((e=>{void 0!==e.value?n.writeUInt64(e.value):n.writeSlice(e.valueBuffer),n.writeVarSlice(e.script)})),s&&this.ins.forEach((e=>{n.writeVector(e.witness)})),n.writeUInt32(this.locktime),void 0!==t?e.slice(t,n.offset):e}toHex(){return this.toBuffer(void 0,void 0,!0).toString("hex")}hasWitnesses(){return this.ins.some((e=>0!==e.witness.length))}isCoinbase(){if(1!==this.ins.length)return!1;const e=this.ins[0].hash;for(let t=0;t<32;++t)if(0!==e[t])return!1;return!0}clone(){const e=new a;return e.version=this.version,e.locktime=this.locktime,e.ins=this.ins.map((e=>({hash:e.hash,index:e.index,script:e.script,sequence:e.sequence,witness:e.witness}))),e.outs=this.outs.map((e=>({script:e.script,value:e.value}))),e}weight(){return 3*this.byteLength(!1)+this.byteLength(!0)}virtualSize(){return Math.ceil(this.weight()/4)}byteLength(e=!0){const r=e&&this.hasWitnesses();return(r?10:8)+t.encodingLength(this.ins.length)+t.encodingLength(this.outs.length)+this.ins.reduce(((e,t)=>e+40+S(t.script)),0)+this.outs.reduce(((e,t)=>e+8+S(t.script)),0)+(r?this.ins.reduce(((e,r)=>e+function(e){const r=e.length;return t.encodingLength(r)+e.reduce(((e,t)=>e+S(t)),0)}(r.witness)),0):0)}}function l(t){return Buffer.from((0,e.sha256)().update(t).digest())}function h(t){const r=(0,e.sha256)().update(t).digest(),i=(0,e.sha256)().update(r).digest();return Buffer.from(i)}function S(e){const r=e.length;return t.encodingLength(r)+r}exports.Transaction=a,a.DEFAULT_SEQUENCE=4294967295,a.SIGHASH_DEFAULT=0,a.SIGHASH_ALL=1,a.SIGHASH_NONE=2,a.SIGHASH_SINGLE=3,a.SIGHASH_ANYONECANPAY=128,a.SIGHASH_OUTPUT_MASK=3,a.SIGHASH_INPUT_MASK=128,a.ADVANCED_TRANSACTION_MARKER=0,a.ADVANCED_TRANSACTION_FLAG=1,exports.varSliceSize=S;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.varSliceSize=exports.Transaction=void 0;const e=require("hash.js"),t=require("varuint-bitcoin"),i=require("./bufferutils"),r=require("./script"),n=require("./coindef"),s=require("@secux/utility"),o=null===s.Logger||void 0===s.Logger?void 0:s.Logger.child({id:"transaction"}),u=Buffer.from("0000000000000000000000000000000000000000000000000000000000000000","hex"),f=Buffer.from("0000000000000000000000000000000000000000000000000000000000000001","hex"),c=Buffer.alloc(0),l=Buffer.from("ffffffffffffffff","hex"),a={script:c,valueBuffer:l,value:0};class h{constructor(){this.ins=[],this.outs=[],this.version=2,this.locktime=0}static fromBuffer(e){const t=new i.BufferReader(e),r=new h;r.version=t.readInt32();const n=t.readUInt8(),s=t.readUInt8();let o=!1;n===h.ADVANCED_TRANSACTION_MARKER&&s===h.ADVANCED_TRANSACTION_FLAG?o=!0:t.offset-=2;const u=t.readVarInt();for(let e=0;e<u;++e)r.ins.push({hash:t.readSlice(32),index:t.readUInt32(),script:t.readVarSlice(),sequence:t.readUInt32(),witness:[]});const f=t.readVarInt();for(let e=0;e<f;++e)r.outs.push({value:t.readUInt64(),script:t.readVarSlice()});if(o){for(let e=0;e<u;++e)r.ins[e].witness=t.readVector();if(!r.hasWitnesses())throw new Error("Transaction has superfluous witness data")}if(r.locktime=t.readUInt32(),t.offset!==e.length)throw new Error("Transaction has unexpected data");return r}static dataForSignature(e,t,i,s){if(t>=e.ins.length)return f;const o=r.compile(r.decompile(i).filter(e=>e!==n.OPCODES.OP_CODESEPARATOR)),u=e.clone();if((31&s)===h.SIGHASH_NONE)u.outs=[],u.ins.forEach((e,i)=>{i!==t&&(e.sequence=0)});else if((31&s)===h.SIGHASH_SINGLE){if(t>=e.outs.length)return f;u.outs.length=t+1;for(let e=0;e<t;e++)u.outs[e]=a;u.ins.forEach((e,i)=>{i!==t&&(e.sequence=0)})}s&h.SIGHASH_ANYONECANPAY?(u.ins=[u.ins[t]],u.ins[0].script=o):(u.ins.forEach(e=>{e.script=c}),u.ins[t].script=o);const l=Buffer.allocUnsafe(u.byteLength(!1)+4);return l.writeInt32LE(s,l.length-4),u.toBuffer(l,0,!1),l}static dataForWitnessV0(e,t,r,n,s){let f,c=Buffer.from([]),l=u,a=u,S=u;if(null===o||void 0===o||o.debug("begin dataForWitnessV0"),null===o||void 0===o||o.debug(`hashType: 0x${s.toString(16)}`),s&h.SIGHASH_ANYONECANPAY||(c=Buffer.allocUnsafe(36*e.ins.length),f=new i.BufferWriter(c,0),e.ins.forEach(e=>{f.writeSlice(e.hash),f.writeUInt32(e.index)}),a=w(c)),s&h.SIGHASH_ANYONECANPAY||(31&s)===h.SIGHASH_SINGLE||(31&s)===h.SIGHASH_NONE||(c=Buffer.allocUnsafe(4*e.ins.length),f=new i.BufferWriter(c,0),e.ins.forEach(e=>{f.writeUInt32(e.sequence)}),S=w(c)),(31&s)!==h.SIGHASH_SINGLE&&(31&s)!==h.SIGHASH_NONE){const t=e.outs.reduce((e,t)=>e+8+d(t.script),0);c=Buffer.allocUnsafe(t),f=new i.BufferWriter(c,0),e.outs.forEach(e=>{f.writeUInt64(e.value),f.writeVarSlice(e.script)}),null===o||void 0===o||o.debug(`outputs: ${c.toString("hex")}`),l=w(c)}else if((31&s)===h.SIGHASH_SINGLE&&t<e.outs.length){const r=e.outs[t];c=Buffer.allocUnsafe(8+d(r.script)),(f=new i.BufferWriter(c,0)).writeUInt64(r.value),f.writeVarSlice(r.script),null===o||void 0===o||o.debug(`single output: ${c.toString("hex")}`),l=w(c)}c=Buffer.allocUnsafe(156+d(r)),f=new i.BufferWriter(c,0);const I=e.ins[t];return f.writeUInt32(e.version),f.writeSlice(a),f.writeSlice(S),f.writeSlice(I.hash),f.writeUInt32(I.index),f.writeVarSlice(r),f.writeUInt64(n),f.writeUInt32(I.sequence),f.writeSlice(l),f.writeUInt32(e.locktime),f.writeUInt32(s),null===o||void 0===o||o.debug("end dataForWitnessV0"),c}static dataForWitnessV1(e,t,r,n,s,u,f){if(n.length!==e.ins.length||r.length!==e.ins.length)throw new Error("Must supply prevout script and value for all inputs");const l=s===h.SIGHASH_DEFAULT?h.SIGHASH_ALL:s&h.SIGHASH_OUTPUT_MASK,a=(s&h.SIGHASH_INPUT_MASK)===h.SIGHASH_ANYONECANPAY,w=l===h.SIGHASH_NONE,I=l===h.SIGHASH_SINGLE;let A=c,g=c,p=c,E=c,U=c;if(null===o||void 0===o||o.debug("begin dataForWitnessV1"),null===o||void 0===o||o.debug(`hashType: 0x${s.toString(16)}`),!a){let t=Buffer.alloc(36*e.ins.length),s=new i.BufferWriter(t);e.ins.forEach(e=>{s.writeSlice(e.hash),s.writeUInt32(e.index)}),A=S(t),t=Buffer.alloc(8*e.ins.length),s=new i.BufferWriter(t),n.forEach(e=>s.writeUInt64(e)),g=S(t),t=Buffer.alloc(r.map(d).reduce((e,t)=>e+t)),s=new i.BufferWriter(t),r.forEach(e=>s.writeVarSlice(e)),p=S(t),t=Buffer.alloc(4*e.ins.length),s=new i.BufferWriter(t),e.ins.forEach(e=>s.writeUInt32(e.sequence)),E=S(t)}if(w||I){if(I&&t<e.outs.length){const r=e.outs[t],n=Buffer.alloc(8+d(r.script)),s=new i.BufferWriter(n);s.writeUInt64(r.value),s.writeVarSlice(r.script),null===o||void 0===o||o.debug(`single output: ${n.toString("hex")}`),U=S(n)}}else{const t=e.outs.map(e=>8+d(e.script)).reduce((e,t)=>e+t),r=Buffer.alloc(t),n=new i.BufferWriter(r);e.outs.forEach(e=>{n.writeUInt64(e.value),n.writeVarSlice(e.script)}),null===o||void 0===o||o.debug(`outputs: ${r.toString("hex")}`),U=S(r)}const N=(u?2:0)+(f?1:0),H=174-(a?49:0)-(w?32:0)+(f?32:0)+(u?37:0),v=Buffer.alloc(H),B=new i.BufferWriter(v);if(B.writeUInt8(s),B.writeInt32(e.version),B.writeUInt32(e.locktime),B.writeSlice(A),B.writeSlice(g),B.writeSlice(p),B.writeSlice(E),w||I||B.writeSlice(U),B.writeUInt8(N),a){const i=e.ins[t];B.writeSlice(i.hash),B.writeUInt32(i.index),B.writeUInt64(n[t]),B.writeVarSlice(r[t]),B.writeUInt32(i.sequence)}else B.writeUInt32(t);if(f){const e=Buffer.alloc(d(f));new i.BufferWriter(e).writeVarSlice(f),B.writeSlice(S(e))}return I&&B.writeSlice(U),u&&(B.writeSlice(u),B.writeUInt8(0),B.writeUInt32(4294967295)),null===o||void 0===o||o.debug("end dataForWitnessV1"),Buffer.concat([Buffer.from([0]),v])}static getHash(e,t){return t&&e.isCoinbase()?Buffer.alloc(32,0):w(e.toBuffer(void 0,void 0,t))}static getId(e){return this.getHash(e).reverse().toString("hex")}addInput(e,t,i,r){i&&0!==i||(i=h.DEFAULT_SEQUENCE),this.ins.push({hash:e,index:t,script:null!==r&&void 0!==r?r:c,sequence:i,witness:[]})}addOutput(e,t,i){this.outs.push({script:e,value:t,path:i})}toBuffer(e,t,r=!1){e||(e=Buffer.allocUnsafe(this.byteLength(r)));const n=new i.BufferWriter(e,null!==t&&void 0!==t?t:0);n.writeInt32(this.version);const s=r&&this.hasWitnesses();return s&&(n.writeUInt8(h.ADVANCED_TRANSACTION_MARKER),n.writeUInt8(h.ADVANCED_TRANSACTION_FLAG)),n.writeVarInt(this.ins.length),this.ins.forEach(e=>{n.writeSlice(e.hash),n.writeUInt32(e.index),n.writeVarSlice(e.script),n.writeUInt32(e.sequence)}),n.writeVarInt(this.outs.length),this.outs.forEach(e=>{void 0!==e.value?n.writeUInt64(e.value):n.writeSlice(e.valueBuffer),n.writeVarSlice(e.script)}),s&&this.ins.forEach(e=>{n.writeVector(e.witness)}),n.writeUInt32(this.locktime),void 0!==t?e.slice(t,n.offset):e}toHex(){return this.toBuffer(void 0,void 0,!0).toString("hex")}hasWitnesses(){return this.ins.some(e=>0!==e.witness.length)}isCoinbase(){if(1!==this.ins.length)return!1;const e=this.ins[0].hash;for(let t=0;t<32;++t)if(0!==e[t])return!1;return!0}clone(){const e=new h;return e.version=this.version,e.locktime=this.locktime,e.ins=this.ins.map(e=>({hash:e.hash,index:e.index,script:e.script,sequence:e.sequence,witness:e.witness})),e.outs=this.outs.map(e=>({script:e.script,value:e.value})),e}weight(){return 3*this.byteLength(!1)+this.byteLength(!0)}virtualSize(){return Math.ceil(this.weight()/4)}byteLength(e=!0){const i=e&&this.hasWitnesses();return(i?10:8)+t.encodingLength(this.ins.length)+t.encodingLength(this.outs.length)+this.ins.reduce((e,t)=>e+40+d(t.script),0)+this.outs.reduce((e,t)=>e+8+d(t.script),0)+(i?this.ins.reduce((e,i)=>e+function(e){const i=e.length;return t.encodingLength(i)+e.reduce((e,t)=>e+d(t),0)}(i.witness),0):0)}}function S(t){return Buffer.from((0,e.sha256)().update(t).digest())}function w(t){const i=(0,e.sha256)().update(t).digest(),r=(0,e.sha256)().update(i).digest();return Buffer.from(r)}function d(e){const i=e.length;return t.encodingLength(i)+i}exports.Transaction=h,h.DEFAULT_SEQUENCE=4294967295,h.SIGHASH_DEFAULT=0,h.SIGHASH_ALL=1,h.SIGHASH_NONE=2,h.SIGHASH_SINGLE=3,h.SIGHASH_ANYONECANPAY=128,h.SIGHASH_OUTPUT_MASK=3,h.SIGHASH_INPUT_MASK=128,h.ADVANCED_TRANSACTION_MARKER=0,h.ADVANCED_TRANSACTION_FLAG=1,exports.varSliceSize=d;

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.taggedHash=exports.toTweakedPublickey=exports.getPublickey=exports.scriptWitnessToWitnessStack=exports.witnessStackToScriptWitness=exports.vectorSize=exports.sliceSize=exports.getOutScriptSize=exports.getWitnessSize=exports.getInScriptSize=exports.getSerializer=exports.getDefaultScript=exports.getPurpose=exports.getCoinType=exports.getPayment=void 0;const e=require("secp256k1/elliptic"),t=require("varuint-bitcoin"),r=require("./interface"),n=require("./payment"),c=require("./payment_bch"),i=require("./payment_grs"),o=require("./transaction"),s=require("./transaction_grs"),p=require("ow"),u=require("@secux/utility"),a=require("hash.js"),f=require("./bip340");function S(e){const t=e.match(/\d+/g),n=parseInt(t[1],10);for(let e=0;e<r.coinmap.length;e++)if(n===r.coinmap[e].coinType)return e;throw Error(`ArgumentError: unsupport cointype of BIP32 path, got ${e}`)}function P(e){return t.encodingLength(e)+e}function T(t){(0,p.default)(t,p.default.any(u.ow_hexString,p.default.buffer));const r="string"==typeof t?Buffer.from(t,"hex"):t;if((0,p.default)(r,p.default.buffer.is((e=>33===e.length||65===e.length))),!e.publicKeyVerify(r))throw Error(`ArgumentError: invalid secp256k1 publickey, got "${r.toString("hex")}"`);return r}exports.getPayment=function(e){switch(e){case r.CoinType.BITCOINCASH:return c.PaymentBCH;case r.CoinType.GROESTL:return i.PaymentGRS;default:return n.PaymentBTC}},exports.getCoinType=S,exports.getPurpose=function(e){switch(e){case r.ScriptType.P2PKH:return 44;case r.ScriptType.P2SH_P2PKH:case r.ScriptType.P2SH_P2WPKH:return 49;case r.ScriptType.P2WPKH:return 84;case r.ScriptType.P2TR:return 86}throw Error(`ArgumentError: unsupport ScriptType, got ${e}`)},exports.getDefaultScript=function(e){const t=e.match(/\d+/g),n=parseInt(t[0],10),c=t[1]?S(e):r.CoinType.BITCOIN;switch(n){case 44:return r.ScriptType.P2PKH;case 49:return c!==r.CoinType.BITCOINCASH?r.ScriptType.P2SH_P2WPKH:r.ScriptType.P2SH_P2PKH;case 84:return r.ScriptType.P2WPKH;case 86:return r.ScriptType.P2TR}throw Error(`ArgumentError: unsupport purpose of path, got "${n}" from ${e}`)},exports.getSerializer=function(e){return e===r.CoinType.GROESTL?s.TransactionGRS:o.Transaction},exports.getInScriptSize=function(e){switch(e){case r.ScriptType.P2PKH:case r.ScriptType.P2SH_P2PKH:return 107;case r.ScriptType.P2SH_P2WPKH:return 23}return 0},exports.getWitnessSize=function(e,t=o.Transaction.SIGHASH_DEFAULT){switch(e){case r.ScriptType.P2SH_P2WPKH:case r.ScriptType.P2WPKH:return[72,33];case r.ScriptType.P2TR:return t===o.Transaction.SIGHASH_DEFAULT?[64]:[65]}return[]},exports.getOutScriptSize=function(e){switch(e){case r.ScriptType.P2PKH:return 25;case r.ScriptType.P2SH_P2PKH:case r.ScriptType.P2SH_P2WPKH:return 23;case r.ScriptType.P2WPKH:return 22;case r.ScriptType.P2TR:return 34}return 0},exports.sliceSize=P,exports.vectorSize=function(e){return t.encodingLength(e.length)+e.reduce(((e,t)=>e+P(t)),0)},exports.witnessStackToScriptWitness=function(e){let r=Buffer.allocUnsafe(0);const n=e=>{const n=r.length,c=t.encodingLength(e);r=Buffer.concat([r,Buffer.allocUnsafe(c)]),t.encode(e,r,n)};n(e.length);for(const t of e)n(t.length),r=Buffer.concat([r,Buffer.from(t)]);return r},exports.scriptWitnessToWitnessStack=function(e){let r=0;const n=()=>{const n=t.decode(e,r);return r+=t.decode.bytes,n},c=()=>{const t=n();return r+=t,e.slice(r-t,r)},i=n(),o=[];for(let e=0;e<i;e++)o.push(c());return o},exports.getPublickey=T,exports.toTweakedPublickey=function(e){const t=T(e).slice(1,33),r=y("TapTweak",t);return(0,f.taprootConvert)(t,r)};const g=Object.fromEntries(["BIP0340/challenge","BIP0340/aux","BIP0340/nonce","TapLeaf","TapBranch","TapSighash","TapTweak","KeyAgg list","KeyAgg coefficient"].map((e=>{const t=function(e){return Buffer.from((0,a.sha256)().update(e).digest())}(e);return[e,Buffer.concat([t,t])]})));function y(e,t){const r=Buffer.concat([g[e],t]);return Buffer.from((0,a.sha256)().update(r).digest())}exports.taggedHash=y;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.taggedHash=exports.toTweakedPublickey=exports.getPublickey=exports.scriptWitnessToWitnessStack=exports.witnessStackToScriptWitness=exports.vectorSize=exports.sliceSize=exports.getOutScriptSize=exports.getWitnessSize=exports.getInScriptSize=exports.getSerializer=exports.getDefaultScript=exports.getPurpose=exports.getCoinType=exports.getPayment=void 0;const e=require("secp256k1/elliptic"),t=require("varuint-bitcoin"),r=require("./interface"),n=require("./payment"),c=require("./payment_bch"),i=require("./payment_grs"),o=require("./transaction"),s=require("./transaction_grs"),p=require("ow"),u=require("@secux/utility"),a=require("hash.js"),f=require("./bip340");function S(e){const t=e.match(/\d+/g),n=parseInt(t[1],10);for(let e=0;e<r.coinmap.length;e++)if(n===r.coinmap[e].coinType)return e;throw Error(`ArgumentError: unsupport cointype of BIP32 path, got ${e}`)}function P(e){return t.encodingLength(e)+e}function T(t){(0,p.default)(t,p.default.any(u.ow_hexString,p.default.buffer));const r="string"==typeof t?Buffer.from(t,"hex"):t;if((0,p.default)(r,p.default.buffer.is(e=>33===e.length||65===e.length)),!e.publicKeyVerify(r))throw Error(`ArgumentError: invalid secp256k1 publickey, got "${r.toString("hex")}"`);return r}exports.getPayment=function(e){switch(e){case r.CoinType.BITCOINCASH:return c.PaymentBCH;case r.CoinType.GROESTL:return i.PaymentGRS;default:return n.PaymentBTC}},exports.getCoinType=S,exports.getPurpose=function(e){switch(e){case r.ScriptType.P2PKH:return 44;case r.ScriptType.P2SH_P2PKH:case r.ScriptType.P2SH_P2WPKH:return 49;case r.ScriptType.P2WPKH:return 84;case r.ScriptType.P2TR:return 86}throw Error(`ArgumentError: unsupport ScriptType, got ${e}`)},exports.getDefaultScript=function(e){const t=e.match(/\d+/g),n=parseInt(t[0],10),c=t[1]?S(e):r.CoinType.BITCOIN;switch(n){case 44:return r.ScriptType.P2PKH;case 49:return c!==r.CoinType.BITCOINCASH?r.ScriptType.P2SH_P2WPKH:r.ScriptType.P2SH_P2PKH;case 84:return r.ScriptType.P2WPKH;case 86:return r.ScriptType.P2TR}throw Error(`ArgumentError: unsupport purpose of path, got "${n}" from ${e}`)},exports.getSerializer=function(e){switch(e){case r.CoinType.GROESTL:return s.TransactionGRS;default:return o.Transaction}},exports.getInScriptSize=function(e){switch(e){case r.ScriptType.P2PKH:case r.ScriptType.P2SH_P2PKH:return 107;case r.ScriptType.P2SH_P2WPKH:return 23}return 0},exports.getWitnessSize=function(e,t=o.Transaction.SIGHASH_DEFAULT){switch(e){case r.ScriptType.P2SH_P2WPKH:case r.ScriptType.P2WPKH:return[72,33];case r.ScriptType.P2TR:return t===o.Transaction.SIGHASH_DEFAULT?[64]:[65]}return[]},exports.getOutScriptSize=function(e){switch(e){case r.ScriptType.P2PKH:return 25;case r.ScriptType.P2SH_P2PKH:case r.ScriptType.P2SH_P2WPKH:return 23;case r.ScriptType.P2WPKH:return 22;case r.ScriptType.P2TR:return 34}return 0},exports.sliceSize=P,exports.vectorSize=function(e){return t.encodingLength(e.length)+e.reduce((e,t)=>e+P(t),0)},exports.witnessStackToScriptWitness=function(e){let r=Buffer.allocUnsafe(0);const n=e=>{const n=r.length,c=t.encodingLength(e);r=Buffer.concat([r,Buffer.allocUnsafe(c)]),t.encode(e,r,n)};n(e.length);for(const t of e)n(t.length),r=Buffer.concat([r,Buffer.from(t)]);return r},exports.scriptWitnessToWitnessStack=function(e){let r=0;const n=()=>{const n=t.decode(e,r);return r+=t.decode.bytes,n},c=()=>{const t=n();return r+=t,e.slice(r-t,r)},i=n(),o=[];for(let e=0;e<i;e++)o.push(c());return o},exports.getPublickey=T,exports.toTweakedPublickey=function(e){const t=T(e).slice(1,33),r=l("TapTweak",t);return(0,f.taprootConvert)(t,r)};const g=Object.fromEntries(["BIP0340/challenge","BIP0340/aux","BIP0340/nonce","TapLeaf","TapBranch","TapSighash","TapTweak","KeyAgg list","KeyAgg coefficient"].map(e=>{const t=(e=e,Buffer.from((0,a.sha256)().update(e).digest()));return[e,Buffer.concat([t,t])]}));var y;function l(e,t){const r=Buffer.concat([g[e],t]);return Buffer.from((0,a.sha256)().update(r).digest())}exports.taggedHash=l;
{
"name": "@secux/app-btc",
"version": "3.1.3",
"version": "3.1.4",
"description": "SecuX Hardware Wallet BTC API",

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

"build": "sh ../../build.sh",
"docs": "npx tsc && jsdoc2md --template ./README.hbs ./lib/app-btc.js > README.md",
"api-docs": "npx tsc && jsdoc2md --template ./api-docs.hbs ./lib/app-btc.js > app-btc.md",
"doc": "npx tsc && jsdoc2md --template ./README.hbs ./lib/app-btc.js > README.md",
"doc:api": "sh ../../api_doc.sh ./src/app-btc.ts ./lib/app-btc.js",
"test:webusb": "webpack serve --progress --config ../../webpack.config.js --output-path ./__tests__ --entry ./__tests__/usb.js",

@@ -39,3 +39,2 @@ "test:webble": "webpack serve --progress --config ../../webpack.config.js --output-path ./__tests__ --entry ./__tests__/ble.js",

"@secux/protocol-transaction": "^3.0.0",
"@secux/transport": "^3.0.1",
"@secux/utility": "^3.0.2",

@@ -53,2 +52,5 @@ "bech32": "^2.0.0",

},
"peerDependencies": {
"@secux/transport": "^3.0.1"
},
"devDependencies": {

@@ -55,0 +57,0 @@ "@bitgo/utxo-lib": "^1.9.6",

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