@secux/app-btc
Advanced tools
Comparing version 3.2.6 to 3.2.7
@@ -127,2 +127,9 @@ /*! | ||
static getDustThreshold(output: ScriptType, dustRelayFee?: number): number; | ||
/** | ||
* Validate address. | ||
* @param {string} address | ||
* @param {CoinType} [coin] default: CoinType.BITCOIN | ||
* @returns {boolean} true if address is valid | ||
*/ | ||
static validateAddress(address: string, coin?: CoinType): boolean; | ||
} | ||
@@ -129,0 +136,0 @@ /** |
@@ -18,2 +18,2 @@ "use strict"; | ||
limitations under the License. | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxBTC=exports.ScriptType=exports.CoinType=void 0;const secp256k1=require("secp256k1/elliptic"),varuint=require("varuint-bitcoin"),utility_1=require("@secux/utility"),xpub_1=require("@secux/utility/lib/xpub"),ow_1=require("ow"),protocol_transaction_1=require("@secux/protocol-transaction"),communication_1=require("@secux/utility/lib/communication"),interface_1=require("@secux/protocol-transaction/lib/interface"),interface_2=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return interface_2.CoinType}}),Object.defineProperty(exports,"ScriptType",{enumerable:!0,get:function(){return interface_2.ScriptType}});const psbt_1=require("./psbt"),utils_1=require("./utils");class SecuxBTC{static addressConvert(publickey,path){const pk=(0,utils_1.getPublickey)(publickey);(0,ow_1.default)(path,ow_1.default.any(interface_2.ow_path,interface_2.ow_PathObject));const compressed=Buffer.from(secp256k1.publicKeyConvert(pk,!0)),coin="string"==typeof path?(0,utils_1.getCoinType)(path):path.coin,script="string"==typeof path?(0,utils_1.getDefaultScript)(path):path.script,payment=(0,utils_1.getPayment)(coin);switch(script){case interface_2.ScriptType.P2SH_P2WPKH:const p2wpkh=payment.p2wpkh(coin,{publickey:compressed});return payment.p2sh(coin,p2wpkh.redeemHash).address;case interface_2.ScriptType.P2SH_P2PKH:const p2pkh=payment.p2pkh(coin,{publickey:compressed});return payment.p2sh(coin,p2pkh.redeemHash).address;case interface_2.ScriptType.P2PKH:return payment.p2pkh(coin,{publickey:compressed}).address;case interface_2.ScriptType.P2WPKH:return payment.p2wpkh(coin,{publickey:compressed}).address;case interface_2.ScriptType.P2TR:return payment.p2tr(coin,{publickey:compressed}).address;default:throw Error(`Invalid or unsupported ScriptType, got ${script} of ${coin}`)}}static prepareAddress(path,option){return this.preparePublickey(path,option)}static resolveAddress(response,path){const pk=SecuxBTC.resolvePublickey(response);return SecuxBTC.addressConvert(pk,path)}static preparePublickey(path,option){var _a;(0,ow_1.default)(path,interface_2.ow_path),option&&(0,ow_1.default)(option,interface_2.ow_AddressOption);const coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(path),cointype=interface_2.coinmap[coin].coinType,purpose=void 0===(null==option?void 0:option.script)?void 0:(0,utils_1.getPurpose)(null==option?void 0:option.script);return(0,ow_1.default)(path,(0,utility_1.ow_strictPath)(cointype,purpose)),protocol_transaction_1.SecuxTransactionTool.getPublickey(path,interface_1.EllipticCurve.SECP256K1)}static resolvePublickey(response){const pk=protocol_transaction_1.SecuxTransactionTool.resolvePublickey(response,interface_1.EllipticCurve.SECP256K1,!0);return Buffer.from(pk,"base64").toString("hex")}static prepareXPublickey(path){return(0,ow_1.default)(path,interface_2.ow_accountPath),protocol_transaction_1.SecuxTransactionTool.getXPublickey(path)}static resolveXPublickey(response,path){return(0,ow_1.default)(path,interface_2.ow_accountPath),protocol_transaction_1.SecuxTransactionTool.resolveXPublickey(response,path)}static prepareSign(inputs,outputs,option){var _a;(0,ow_1.default)(inputs,ow_1.default.array.ofType(interface_2.ow_txInput).minLength(1)),(0,ow_1.default)(option,ow_1.default.any(ow_1.default.undefined,interface_2.ow_SignOption));const coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(inputs[0].path);(0,ow_1.default)(outputs,interface_2.ow_txOutput),inputs.map((input=>{const purpose=input.script?(0,utils_1.getPurpose)(input.script):interface_2.btcPurposes;(0,ow_1.default)(input.path,(0,utility_1.ow_strictPath)(interface_2.coinmap[coin].coinType,purpose))}));let _=(0,interface_2.isOutuptScriptExtended)(outputs.to);if(_){const purpose=_.script?(0,utils_1.getPurpose)(_.script):interface_2.btcPurposes;(0,ow_1.default)(_.path,(0,utility_1.ow_strictPath)(interface_2.coinmap[coin].coinType,purpose))}if(outputs.utxo){const purpose=outputs.utxo.script?(0,utils_1.getPurpose)(outputs.utxo.script):interface_2.btcPurposes;(0,ow_1.default)(outputs.utxo.path,(0,utility_1.ow_strictPath)(interface_2.coinmap[coin].coinType,purpose))}const psbt=new psbt_1.SecuxPsbt(coin,null==option?void 0:option.isRBF);return psbt.AddInputs(inputs),psbt.AddOutputs(outputs.utxo?[outputs.to,outputs.utxo]:[outputs.to]),(0,communication_1.wrapResult)(psbt.PrepareSign(null==option?void 0:option.feeRate))}static resolveSignatureList(response){return protocol_transaction_1.SecuxTransactionTool.resolveSignatureList(response).map((x=>Buffer.from(x,"base64"))).map((x=>utility_1.Signature.fromSignature(x))).map((x=>Buffer.concat([x.r,x.s]).toString("hex")))}static resolveTransaction(response,params){var _a;(0,ow_1.default)(response,ow_1.default.any(communication_1.ow_communicationData,ow_1.default.array.ofType(communication_1.ow_communicationData))),(0,ow_1.default)(params,interface_2.ow_TransactionObject),response=Array.isArray(response)?response:[response];const signatures=[];for(const rsp of response){const sigList=SecuxBTC.resolveSignatureList(rsp).map((x=>Buffer.from(x,"hex")));signatures.push(...sigList)}const pks=params.publickeys.map((x=>(0,utils_1.getPublickey)(x)));return psbt_1.SecuxPsbt.FromBuffer(Buffer.from(params.rawTx,"hex"),null!==(_a=params.coin)&&void 0!==_a?_a:interface_2.CoinType.BITCOIN).appendSignature(signatures,pks).finalizeAllInputs().extractTransaction().toHex()}static async getAddress(path,option){var _a,_b;const data=SecuxBTC.prepareAddress(path,option),rsp=await this.Exchange((0,communication_1.getBuffer)(data));return SecuxBTC.resolveAddress(rsp,{coin:null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(path),script:null!==(_b=null==option?void 0:option.script)&&void 0!==_b?_b:(0,utils_1.getDefaultScript)(path)})}static async getPublickey(path,option){const data=SecuxBTC.preparePublickey(path,option),rsp=await this.Exchange((0,communication_1.getBuffer)(data));return SecuxBTC.resolvePublickey(rsp)}static async getXPublickey(path){const data=SecuxBTC.prepareXPublickey(path),rsp=await this.Exchange((0,communication_1.getBuffer)(data));return SecuxBTC.resolveXPublickey(rsp,path)}static async sign(inputs,outputs,option){var _a,_b;const cache={},getPK=async path=>{if(void 0!==cache[path])return cache[path];const publickey=await SecuxBTC.getPublickey.call(this,path,{coin}),pk=Buffer.from(publickey,"hex");return cache[path]=pk,pk},coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(inputs[0].path);for(const txIn of inputs)void 0===txIn.publickey&&(txIn.publickey=await getPK(txIn.path));outputs.to.path&&void 0===outputs.to.publickey&&(outputs.to.publickey=await getPK(outputs.to.path)),(null===(_b=outputs.utxo)||void 0===_b?void 0:_b.path)&&void 0===outputs.utxo.publickey&&(outputs.utxo.publickey=await getPK(outputs.utxo.path));const{commands,rawTx}=SecuxBTC.prepareSign(inputs,outputs,Object.assign(Object.assign({},option),{coin}));return{multi_command:commands,rawTx,publickeys:inputs.map((x=>x.publickey)),coin}}static deriveAddress(xpub,change,addressIndex,option){var _a,_b;(0,ow_1.default)(change,ow_1.default.number.uint8),(0,ow_1.default)(addressIndex,ow_1.default.number.uint8),(0,ow_1.default)(option,ow_1.default.any(ow_1.default.undefined,interface_2.ow_AddressOption));const _xpub=(0,xpub_1.decodeXPUB)(xpub);if(3!==_xpub.depth)throw Error(`ArgumentError: expect depth from xpub is 3, but got ${_xpub.depth}`);if(null==option?void 0:option.script)if(option.script in[interface_2.ScriptType.P2PKH,interface_2.ScriptType.P2SH_P2PKH,interface_2.ScriptType.P2SH_P2WPKH,interface_2.ScriptType.P2WPKH]){const purpose=(0,utils_1.getPurpose)(null==option?void 0:option.script);if(_xpub.purpose!==purpose)throw Error(`ArgumentError: expect purpose from xpub is ${purpose}, but got ${_xpub.purpose}`)}else{if(44!==_xpub.purpose)throw Error(`ArgumentError: expect purpose from xpub is 44, but got ${_xpub.purpose}`);_xpub.purpose,(0,utils_1.getPurpose)(null==option?void 0:option.script)}const{publickey}=(0,xpub_1.deriveKey)(_xpub.publickey,_xpub.chaincode,[change,addressIndex]),coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:interface_2.CoinType.BITCOIN,script=null!==(_b=null==option?void 0:option.script)&&void 0!==_b?_b:(0,utils_1.getDefaultScript)(`m/${_xpub.purpose}'`);return SecuxBTC.addressConvert(publickey,{coin,script})}static getVirtualSize(inputs,outputs){const baseSize=8+varuint.encodingLength(inputs.length)+varuint.encodingLength(outputs.length)+inputs.reduce(((sum,input)=>sum+40+(0,utils_1.sliceSize)((0,utils_1.getInScriptSize)(input))),0)+outputs.reduce(((sum,output)=>sum+8+(0,utils_1.sliceSize)((0,utils_1.getOutScriptSize)(output))),0),inputsWitness=inputs.map((x=>(0,utils_1.getWitnessSize)(x)));return(4*baseSize+(inputsWitness.some((x=>0!==x.length))?2+inputsWitness.reduce(((sum,w)=>sum+(0,utils_1.vectorSize)(w)),0):0))/4}static getDustThreshold(output,dustRelayFee=3){return(0,utils_1.getDustThreshold)(output,dustRelayFee)}}exports.SecuxBTC=SecuxBTC,(0,utility_1.loadPlugin)(SecuxBTC,"SecuxBTC"); | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecuxBTC=exports.ScriptType=exports.CoinType=void 0;const secp256k1=require("secp256k1/elliptic"),varuint=require("varuint-bitcoin"),utility_1=require("@secux/utility"),xpub_1=require("@secux/utility/lib/xpub"),ow_1=require("ow"),protocol_transaction_1=require("@secux/protocol-transaction"),communication_1=require("@secux/utility/lib/communication"),interface_1=require("@secux/protocol-transaction/lib/interface"),interface_2=require("./interface");Object.defineProperty(exports,"CoinType",{enumerable:!0,get:function(){return interface_2.CoinType}}),Object.defineProperty(exports,"ScriptType",{enumerable:!0,get:function(){return interface_2.ScriptType}});const psbt_1=require("./psbt"),utils_1=require("./utils"),payment_1=require("./payment");class SecuxBTC{static addressConvert(publickey,path){const pk=(0,utils_1.getPublickey)(publickey);(0,ow_1.default)(path,ow_1.default.any(interface_2.ow_path,interface_2.ow_PathObject));const compressed=Buffer.from(secp256k1.publicKeyConvert(pk,!0)),coin="string"==typeof path?(0,utils_1.getCoinType)(path):path.coin,script="string"==typeof path?(0,utils_1.getDefaultScript)(path):path.script,payment=(0,utils_1.getPayment)(coin);switch(script){case interface_2.ScriptType.P2SH_P2WPKH:const p2wpkh=payment.p2wpkh(coin,{publickey:compressed});return payment.p2sh(coin,p2wpkh.redeemHash).address;case interface_2.ScriptType.P2SH_P2PKH:const p2pkh=payment.p2pkh(coin,{publickey:compressed});return payment.p2sh(coin,p2pkh.redeemHash).address;case interface_2.ScriptType.P2PKH:return payment.p2pkh(coin,{publickey:compressed}).address;case interface_2.ScriptType.P2WPKH:return payment.p2wpkh(coin,{publickey:compressed}).address;case interface_2.ScriptType.P2TR:return payment.p2tr(coin,{publickey:compressed}).address;default:throw Error(`Invalid or unsupported ScriptType, got ${script} of ${coin}`)}}static prepareAddress(path,option){return this.preparePublickey(path,option)}static resolveAddress(response,path){const pk=SecuxBTC.resolvePublickey(response);return SecuxBTC.addressConvert(pk,path)}static preparePublickey(path,option){var _a;(0,ow_1.default)(path,interface_2.ow_path),option&&(0,ow_1.default)(option,interface_2.ow_AddressOption);const coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(path),cointype=interface_2.coinmap[coin].coinType,purpose=void 0===(null==option?void 0:option.script)?void 0:(0,utils_1.getPurpose)(null==option?void 0:option.script);return(0,ow_1.default)(path,(0,utility_1.ow_strictPath)(cointype,purpose)),protocol_transaction_1.SecuxTransactionTool.getPublickey(path,interface_1.EllipticCurve.SECP256K1)}static resolvePublickey(response){const pk=protocol_transaction_1.SecuxTransactionTool.resolvePublickey(response,interface_1.EllipticCurve.SECP256K1,!0);return Buffer.from(pk,"base64").toString("hex")}static prepareXPublickey(path){return(0,ow_1.default)(path,interface_2.ow_accountPath),protocol_transaction_1.SecuxTransactionTool.getXPublickey(path)}static resolveXPublickey(response,path){return(0,ow_1.default)(path,interface_2.ow_accountPath),protocol_transaction_1.SecuxTransactionTool.resolveXPublickey(response,path)}static prepareSign(inputs,outputs,option){var _a;(0,ow_1.default)(inputs,ow_1.default.array.ofType(interface_2.ow_txInput).minLength(1)),(0,ow_1.default)(option,ow_1.default.any(ow_1.default.undefined,interface_2.ow_SignOption));const coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(inputs[0].path);(0,ow_1.default)(outputs,interface_2.ow_txOutput),inputs.map((input=>{const purpose=input.script?(0,utils_1.getPurpose)(input.script):interface_2.btcPurposes;(0,ow_1.default)(input.path,(0,utility_1.ow_strictPath)(interface_2.coinmap[coin].coinType,purpose))}));let _=(0,interface_2.isOutuptScriptExtended)(outputs.to);if(_){const purpose=_.script?(0,utils_1.getPurpose)(_.script):interface_2.btcPurposes;(0,ow_1.default)(_.path,(0,utility_1.ow_strictPath)(interface_2.coinmap[coin].coinType,purpose))}if(outputs.utxo){const purpose=outputs.utxo.script?(0,utils_1.getPurpose)(outputs.utxo.script):interface_2.btcPurposes;(0,ow_1.default)(outputs.utxo.path,(0,utility_1.ow_strictPath)(interface_2.coinmap[coin].coinType,purpose))}const psbt=new psbt_1.SecuxPsbt(coin,null==option?void 0:option.isRBF);return psbt.AddInputs(inputs),psbt.AddOutputs(outputs.utxo?[outputs.to,outputs.utxo]:[outputs.to]),(0,communication_1.wrapResult)(psbt.PrepareSign(null==option?void 0:option.feeRate))}static resolveSignatureList(response){return protocol_transaction_1.SecuxTransactionTool.resolveSignatureList(response).map((x=>Buffer.from(x,"base64"))).map((x=>utility_1.Signature.fromSignature(x))).map((x=>Buffer.concat([x.r,x.s]).toString("hex")))}static resolveTransaction(response,params){var _a;(0,ow_1.default)(response,ow_1.default.any(communication_1.ow_communicationData,ow_1.default.array.ofType(communication_1.ow_communicationData))),(0,ow_1.default)(params,interface_2.ow_TransactionObject),response=Array.isArray(response)?response:[response];const signatures=[];for(const rsp of response){const sigList=SecuxBTC.resolveSignatureList(rsp).map((x=>Buffer.from(x,"hex")));signatures.push(...sigList)}const pks=params.publickeys.map((x=>(0,utils_1.getPublickey)(x)));return psbt_1.SecuxPsbt.FromBuffer(Buffer.from(params.rawTx,"hex"),null!==(_a=params.coin)&&void 0!==_a?_a:interface_2.CoinType.BITCOIN).appendSignature(signatures,pks).finalizeAllInputs().extractTransaction().toHex()}static async getAddress(path,option){var _a,_b;const data=SecuxBTC.prepareAddress(path,option),rsp=await this.Exchange((0,communication_1.getBuffer)(data));return SecuxBTC.resolveAddress(rsp,{coin:null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(path),script:null!==(_b=null==option?void 0:option.script)&&void 0!==_b?_b:(0,utils_1.getDefaultScript)(path)})}static async getPublickey(path,option){const data=SecuxBTC.preparePublickey(path,option),rsp=await this.Exchange((0,communication_1.getBuffer)(data));return SecuxBTC.resolvePublickey(rsp)}static async getXPublickey(path){const data=SecuxBTC.prepareXPublickey(path),rsp=await this.Exchange((0,communication_1.getBuffer)(data));return SecuxBTC.resolveXPublickey(rsp,path)}static async sign(inputs,outputs,option){var _a,_b;const cache={},getPK=async path=>{if(void 0!==cache[path])return cache[path];const publickey=await SecuxBTC.getPublickey.call(this,path,{coin}),pk=Buffer.from(publickey,"hex");return cache[path]=pk,pk},coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:(0,utils_1.getCoinType)(inputs[0].path);for(const txIn of inputs)void 0===txIn.publickey&&(txIn.publickey=await getPK(txIn.path));outputs.to.path&&void 0===outputs.to.publickey&&(outputs.to.publickey=await getPK(outputs.to.path)),(null===(_b=outputs.utxo)||void 0===_b?void 0:_b.path)&&void 0===outputs.utxo.publickey&&(outputs.utxo.publickey=await getPK(outputs.utxo.path));const{commands,rawTx}=SecuxBTC.prepareSign(inputs,outputs,Object.assign(Object.assign({},option),{coin}));return{multi_command:commands,rawTx,publickeys:inputs.map((x=>x.publickey)),coin}}static deriveAddress(xpub,change,addressIndex,option){var _a,_b;(0,ow_1.default)(change,ow_1.default.number.uint8),(0,ow_1.default)(addressIndex,ow_1.default.number.uint8),(0,ow_1.default)(option,ow_1.default.any(ow_1.default.undefined,interface_2.ow_AddressOption));const _xpub=(0,xpub_1.decodeXPUB)(xpub);if(3!==_xpub.depth)throw Error(`ArgumentError: expect depth from xpub is 3, but got ${_xpub.depth}`);if(null==option?void 0:option.script)if(option.script in[interface_2.ScriptType.P2PKH,interface_2.ScriptType.P2SH_P2PKH,interface_2.ScriptType.P2SH_P2WPKH,interface_2.ScriptType.P2WPKH]){const purpose=(0,utils_1.getPurpose)(null==option?void 0:option.script);if(_xpub.purpose!==purpose)throw Error(`ArgumentError: expect purpose from xpub is ${purpose}, but got ${_xpub.purpose}`)}else{if(44!==_xpub.purpose)throw Error(`ArgumentError: expect purpose from xpub is 44, but got ${_xpub.purpose}`);_xpub.purpose,(0,utils_1.getPurpose)(null==option?void 0:option.script)}const{publickey}=(0,xpub_1.deriveKey)(_xpub.publickey,_xpub.chaincode,[change,addressIndex]),coin=null!==(_a=null==option?void 0:option.coin)&&void 0!==_a?_a:interface_2.CoinType.BITCOIN,script=null!==(_b=null==option?void 0:option.script)&&void 0!==_b?_b:(0,utils_1.getDefaultScript)(`m/${_xpub.purpose}'`);return SecuxBTC.addressConvert(publickey,{coin,script})}static getVirtualSize(inputs,outputs){const baseSize=8+varuint.encodingLength(inputs.length)+varuint.encodingLength(outputs.length)+inputs.reduce(((sum,input)=>sum+40+(0,utils_1.sliceSize)((0,utils_1.getInScriptSize)(input))),0)+outputs.reduce(((sum,output)=>sum+8+(0,utils_1.sliceSize)((0,utils_1.getOutScriptSize)(output))),0),inputsWitness=inputs.map((x=>(0,utils_1.getWitnessSize)(x)));return(4*baseSize+(inputsWitness.some((x=>0!==x.length))?2+inputsWitness.reduce(((sum,w)=>sum+(0,utils_1.vectorSize)(w)),0):0))/4}static getDustThreshold(output,dustRelayFee=3){return(0,utils_1.getDustThreshold)(output,dustRelayFee)}static validateAddress(address,coin=interface_2.CoinType.BITCOIN){try{payment_1.PaymentBTC.decode(coin,address)}catch(error){return!1}return!0}}exports.SecuxBTC=SecuxBTC,(0,utility_1.loadPlugin)(SecuxBTC,"SecuxBTC"); |
@@ -18,2 +18,2 @@ "use strict"; | ||
limitations under the License. | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.taggedHash=exports.toTweakedPublickey=exports.getPublickey=exports.scriptWitnessToWitnessStack=exports.witnessStackToScriptWitness=exports.vectorSize=exports.sliceSize=exports.getDustThreshold=exports.getOutScriptSize=exports.getWitnessSize=exports.getInScriptSize=exports.getSerializer=exports.getDefaultScript=exports.getPurpose=exports.getCoinType=exports.getPayment=void 0;const secp256k1=require("secp256k1/elliptic"),varuint=require("varuint-bitcoin"),interface_1=require("./interface"),payment_1=require("./payment"),payment_bch_1=require("./payment_bch"),payment_grs_1=require("./payment_grs"),transaction_1=require("./transaction"),transaction_grs_1=require("./transaction_grs"),ow_1=require("ow"),hash_js_1=require("hash.js"),bip340_1=require("./bip340");function getCoinType(path){const bip32=path.match(/\d+/g),cointype=parseInt(bip32[1],10);for(let i=0;i<interface_1.coinmap.length;i++)if(cointype===interface_1.coinmap[i].coinType)return i;throw Error(`ArgumentError: unsupport cointype of BIP32 path, got ${path}`)}function getWitnessSize(type,sighashType=transaction_1.Transaction.SIGHASH_DEFAULT){switch(type){case interface_1.ScriptType.P2SH_P2WPKH:case interface_1.ScriptType.P2WPKH:return[72,33];case interface_1.ScriptType.P2TR:return sighashType===transaction_1.Transaction.SIGHASH_DEFAULT?[64]:[65]}return[]}function getOutScriptSize(type){switch(type){case interface_1.ScriptType.P2PKH:return 25;case interface_1.ScriptType.P2SH_P2PKH:case interface_1.ScriptType.P2SH_P2WPKH:return 23;case interface_1.ScriptType.P2WPKH:return 22;case interface_1.ScriptType.P2TR:return 34}return 0}function sliceSize(size){return varuint.encodingLength(size)+size}function getPublickey(data){(0,ow_1.default)(data,ow_1.default.any(interface_1.ow_hexString,ow_1.default.buffer));const pk="string"==typeof data?Buffer.from(data,"hex"):data;if((0,ow_1.default)(pk,ow_1.default.buffer.is((x=>33===x.length||65===x.length))),!secp256k1.publicKeyVerify(pk))throw Error(`ArgumentError: invalid secp256k1 publickey, got "${pk.toString("hex")}"`);return pk}exports.getPayment=function(coin){switch(coin){case interface_1.CoinType.BITCOINCASH:return payment_bch_1.PaymentBCH;case interface_1.CoinType.GROESTL:return payment_grs_1.PaymentGRS;default:return payment_1.PaymentBTC}},exports.getCoinType=getCoinType,exports.getPurpose=function(script){switch(script){case interface_1.ScriptType.P2PKH:return 44;case interface_1.ScriptType.P2SH_P2PKH:case interface_1.ScriptType.P2SH_P2WPKH:return 49;case interface_1.ScriptType.P2WPKH:return 84;case interface_1.ScriptType.P2TR:return 86}throw Error(`ArgumentError: unsupport ScriptType, got ${script}`)},exports.getDefaultScript=function(path){const bip32=path.match(/\d+/g),purpose=parseInt(bip32[0],10),coin=bip32[1]?getCoinType(path):interface_1.CoinType.BITCOIN;switch(purpose){case 44:return interface_1.ScriptType.P2PKH;case 49:return coin!==interface_1.CoinType.BITCOINCASH?interface_1.ScriptType.P2SH_P2WPKH:interface_1.ScriptType.P2SH_P2PKH;case 84:return interface_1.ScriptType.P2WPKH;case 86:return interface_1.ScriptType.P2TR}throw Error(`ArgumentError: unsupport purpose of path, got "${purpose}" from ${path}`)},exports.getSerializer=function(coin){return coin===interface_1.CoinType.GROESTL?transaction_grs_1.TransactionGRS:transaction_1.Transaction},exports.getInScriptSize=function(type){switch(type){case interface_1.ScriptType.P2PKH:case interface_1.ScriptType.P2SH_P2PKH:return 107;case interface_1.ScriptType.P2SH_P2WPKH:return 23}return 0},exports.getWitnessSize=getWitnessSize,exports.getOutScriptSize=getOutScriptSize,exports.getDustThreshold=function(output,dustRelayFee){return(8+varuint.encodingLength(1)+getOutScriptSize(output)+(0!==getWitnessSize(output).length?67:148))*dustRelayFee},exports.sliceSize=sliceSize,exports.vectorSize=function(sizes){return varuint.encodingLength(sizes.length)+sizes.reduce(((sum,size)=>sum+sliceSize(size)),0)},exports.witnessStackToScriptWitness=function(witness){let buffer=Buffer.allocUnsafe(0);const writeVarInt=i=>{const currentLen=buffer.length,varintLen=varuint.encodingLength(i);buffer=Buffer.concat([buffer,Buffer.allocUnsafe(varintLen)]),varuint.encode(i,buffer,currentLen)};writeVarInt(witness.length);for(const w of witness)writeVarInt(w.length),buffer=Buffer.concat([buffer,Buffer.from(w)]);return buffer},exports.scriptWitnessToWitnessStack=function(buffer){let offset=0;const readVarInt=()=>{const vi=varuint.decode(buffer,offset);return offset+=varuint.decode.bytes,vi},readVarSlice=()=>{const n=readVarInt();return offset+=n,buffer.slice(offset-n,offset)},count=readVarInt(),vector=[];for(let i=0;i<count;i++)vector.push(readVarSlice());return vector},exports.getPublickey=getPublickey,exports.toTweakedPublickey=function(data){const XOnlyPubkey=getPublickey(data).slice(1,33),commitHash=taggedHash("TapTweak",XOnlyPubkey);return(0,bip340_1.taprootConvert)(XOnlyPubkey,commitHash)};const TAGGED_HASH_PREFIXES=Object.fromEntries(["BIP0340/challenge","BIP0340/aux","BIP0340/nonce","TapLeaf","TapBranch","TapSighash","TapTweak","KeyAgg list","KeyAgg coefficient"].map((tag=>{const tagHash=function(tag){return Buffer.from((0,hash_js_1.sha256)().update(tag).digest())}(tag);return[tag,Buffer.concat([tagHash,tagHash])]})));function taggedHash(prefix,data){const buf=Buffer.concat([TAGGED_HASH_PREFIXES[prefix],data]);return Buffer.from((0,hash_js_1.sha256)().update(buf).digest())}exports.taggedHash=taggedHash; | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.taggedHash=exports.toTweakedPublickey=exports.getPublickey=exports.scriptWitnessToWitnessStack=exports.witnessStackToScriptWitness=exports.vectorSize=exports.sliceSize=exports.getDustThreshold=exports.getOutScriptSize=exports.getWitnessSize=exports.getInScriptSize=exports.getSerializer=exports.getDefaultScript=exports.getPurpose=exports.getCoinType=exports.getPayment=void 0;const secp256k1=require("secp256k1/elliptic"),varuint=require("varuint-bitcoin"),interface_1=require("./interface"),payment_1=require("./payment"),payment_bch_1=require("./payment_bch"),payment_grs_1=require("./payment_grs"),transaction_1=require("./transaction"),transaction_grs_1=require("./transaction_grs"),ow_1=require("ow"),hash_js_1=require("hash.js"),bip340_1=require("./bip340");function getCoinType(path){const bip32=path.match(/\d+/g),cointype=parseInt(bip32[1],10);for(let i=0;i<interface_1.coinmap.length;i++)if(cointype===interface_1.coinmap[i].coinType)return i;throw Error(`ArgumentError: unsupport cointype of BIP32 path, got ${path}`)}function getWitnessSize(type,sighashType=transaction_1.Transaction.SIGHASH_DEFAULT){switch(type){case interface_1.ScriptType.P2SH_P2WPKH:case interface_1.ScriptType.P2WPKH:return[72,33];case interface_1.ScriptType.P2TR:return sighashType===transaction_1.Transaction.SIGHASH_DEFAULT?[64]:[65]}return[]}function getOutScriptSize(type){switch(type){case interface_1.ScriptType.P2PKH:return 25;case interface_1.ScriptType.P2SH_P2PKH:case interface_1.ScriptType.P2SH_P2WPKH:return 23;case interface_1.ScriptType.P2WPKH:return 22;case interface_1.ScriptType.P2TR:return 34}return 0}function sliceSize(size){return varuint.encodingLength(size)+size}function getPublickey(data){(0,ow_1.default)(data,ow_1.default.any(interface_1.ow_hexString,ow_1.default.buffer));const pk="string"==typeof data?Buffer.from(data,"hex"):data;if(!secp256k1.publicKeyVerify(pk))throw Error(`ArgumentError: invalid secp256k1 publickey, got "${pk.toString("hex")}"`);return pk}exports.getPayment=function(coin){switch(coin){case interface_1.CoinType.BITCOINCASH:return payment_bch_1.PaymentBCH;case interface_1.CoinType.GROESTL:return payment_grs_1.PaymentGRS;default:return payment_1.PaymentBTC}},exports.getCoinType=getCoinType,exports.getPurpose=function(script){switch(script){case interface_1.ScriptType.P2PKH:return 44;case interface_1.ScriptType.P2SH_P2PKH:case interface_1.ScriptType.P2SH_P2WPKH:return 49;case interface_1.ScriptType.P2WPKH:return 84;case interface_1.ScriptType.P2TR:return 86}throw Error(`ArgumentError: unsupport ScriptType, got ${script}`)},exports.getDefaultScript=function(path){const bip32=path.match(/\d+/g),purpose=parseInt(bip32[0],10),coin=bip32[1]?getCoinType(path):interface_1.CoinType.BITCOIN;switch(purpose){case 44:return interface_1.ScriptType.P2PKH;case 49:return coin!==interface_1.CoinType.BITCOINCASH?interface_1.ScriptType.P2SH_P2WPKH:interface_1.ScriptType.P2SH_P2PKH;case 84:return interface_1.ScriptType.P2WPKH;case 86:return interface_1.ScriptType.P2TR}throw Error(`ArgumentError: unsupport purpose of path, got "${purpose}" from ${path}`)},exports.getSerializer=function(coin){return coin===interface_1.CoinType.GROESTL?transaction_grs_1.TransactionGRS:transaction_1.Transaction},exports.getInScriptSize=function(type){switch(type){case interface_1.ScriptType.P2PKH:case interface_1.ScriptType.P2SH_P2PKH:return 107;case interface_1.ScriptType.P2SH_P2WPKH:return 23}return 0},exports.getWitnessSize=getWitnessSize,exports.getOutScriptSize=getOutScriptSize,exports.getDustThreshold=function(output,dustRelayFee){return(8+varuint.encodingLength(1)+getOutScriptSize(output)+(0!==getWitnessSize(output).length?67:148))*dustRelayFee},exports.sliceSize=sliceSize,exports.vectorSize=function(sizes){return varuint.encodingLength(sizes.length)+sizes.reduce(((sum,size)=>sum+sliceSize(size)),0)},exports.witnessStackToScriptWitness=function(witness){let buffer=Buffer.allocUnsafe(0);const writeVarInt=i=>{const currentLen=buffer.length,varintLen=varuint.encodingLength(i);buffer=Buffer.concat([buffer,Buffer.allocUnsafe(varintLen)]),varuint.encode(i,buffer,currentLen)};writeVarInt(witness.length);for(const w of witness)writeVarInt(w.length),buffer=Buffer.concat([buffer,Buffer.from(w)]);return buffer},exports.scriptWitnessToWitnessStack=function(buffer){let offset=0;const readVarInt=()=>{const vi=varuint.decode(buffer,offset);return offset+=varuint.decode.bytes,vi},readVarSlice=()=>{const n=readVarInt();return offset+=n,buffer.slice(offset-n,offset)},count=readVarInt(),vector=[];for(let i=0;i<count;i++)vector.push(readVarSlice());return vector},exports.getPublickey=getPublickey,exports.toTweakedPublickey=function(data){const XOnlyPubkey=getPublickey(data).slice(1,33),commitHash=taggedHash("TapTweak",XOnlyPubkey);return(0,bip340_1.taprootConvert)(XOnlyPubkey,commitHash)};const TAGGED_HASH_PREFIXES=Object.fromEntries(["BIP0340/challenge","BIP0340/aux","BIP0340/nonce","TapLeaf","TapBranch","TapSighash","TapTweak","KeyAgg list","KeyAgg coefficient"].map((tag=>{const tagHash=function(tag){return Buffer.from((0,hash_js_1.sha256)().update(tag).digest())}(tag);return[tag,Buffer.concat([tagHash,tagHash])]})));function taggedHash(prefix,data){const buf=Buffer.concat([TAGGED_HASH_PREFIXES[prefix],data]);return Buffer.from((0,hash_js_1.sha256)().update(buf).digest())}exports.taggedHash=taggedHash; |
{ | ||
"name": "@secux/app-btc", | ||
"version": "3.2.6", | ||
"version": "3.2.7", | ||
"description": "SecuX Hardware Wallet BTC API", | ||
@@ -46,2 +46,3 @@ "keywords": [ | ||
"cashaddrjs": "^0.4.4", | ||
"coinselect": "^3.1.13", | ||
"ecurve": "^1.0.6", | ||
@@ -48,0 +49,0 @@ "groestl-hash-js": "^1.0.0", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
666946
1079
4
14
+ Addedcoinselect@^3.1.13
+ Addedcoinselect@3.1.13(transitive)