@secux/app-btc
Advanced tools
Comparing version 3.2.9 to 3.2.10
@@ -105,2 +105,10 @@ /*! | ||
/** | ||
* Derive public key from xpub. | ||
* @param {string} xpub extended publickey (base58 encoded), depth must be 3 | ||
* @param {number} change BIP44 change field | ||
* @param {number} addressIndex BIP44 address_index field | ||
* @returns {communicationData} publickey | ||
*/ | ||
static derivePublicKey(xpub: string | any, change: number, addressIndex: number): communicationData; | ||
/** | ||
* Derive xpub and generate BTC address. | ||
@@ -211,2 +219,3 @@ * @param {string} xpub extended publickey (base58 encoded), depth must be 3 | ||
* @property {boolean} [isRBF] make Replace-by-Fee transaction | ||
* @property {string} [xpub] account's extended publickey | ||
*/ | ||
@@ -213,0 +222,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"),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"); | ||
*/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"),BIP32Path_1=require("@secux/utility/lib/BIP32Path"),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);const setPublickey=data=>{const xpub=null==option?void 0:option.xpub;if(!xpub)return;if(data.publickey)return;const bip32=(0,BIP32Path_1.splitPath)(data.path);data.publickey=SecuxBTC.derivePublicKey(xpub,bip32.change.value,bip32.addressIndex.value)};inputs.forEach((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)),setPublickey(input)}));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)),setPublickey(outputs.to)}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)),setPublickey(outputs.utxo)}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);if(!(null==option?void 0:option.xpub)){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 derivePublicKey(xpub,change,addressIndex){(0,ow_1.default)(change,ow_1.default.number.uint32),(0,ow_1.default)(addressIndex,ow_1.default.number.uint32);const _xpub="string"==typeof xpub?(0,xpub_1.decodeXPUB)(xpub):xpub;if(3!==_xpub.depth)throw Error(`ArgumentError: expect depth from xpub is 3, but got ${_xpub.depth}`);const{publickey}=(0,xpub_1.deriveKey)(_xpub.publickey,_xpub.chaincode,[change,addressIndex]);return(0,communication_1.toCommunicationData)(publickey)}static deriveAddress(xpub,change,addressIndex,option){var _a,_b;(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(null==option?void 0:option.script)if([interface_2.ScriptType.P2PKH,interface_2.ScriptType.P2SH_P2PKH,interface_2.ScriptType.P2SH_P2WPKH,interface_2.ScriptType.P2WPKH].includes(option.script)){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}`);const publickey=SecuxBTC.derivePublicKey(_xpub,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"); |
@@ -145,2 +145,3 @@ /*! | ||
isRBF?: boolean; | ||
xpub?: string; | ||
}; | ||
@@ -147,0 +148,0 @@ export declare const ow_AddressOption: import("ow").ObjectPredicate<{ |
@@ -18,2 +18,2 @@ "use strict"; | ||
limitations under the License. | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.ow_SignOption=exports.ow_AddressOption=exports.ow_TransactionObject=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_hashString=exports.ow_hexString=exports.ow_accountPath=exports.ow_path=exports.ow_balance=exports.btcPurposes=exports.btcCoinTypes=exports.coinmap=exports.CoinType=exports.ScriptType=exports.OPCODES=void 0;const ow_1=require("ow"),constants=require("./coindef"),utils=require("@secux/utility");var ScriptType,CoinType,coindef_1=require("./coindef");Object.defineProperty(exports,"OPCODES",{enumerable:!0,get:function(){return coindef_1.OPCODES}}),function(ScriptType){ScriptType[ScriptType.P2PKH=0]="P2PKH",ScriptType[ScriptType.P2WPKH=1]="P2WPKH",ScriptType[ScriptType.P2SH_P2PKH=2]="P2SH_P2PKH",ScriptType[ScriptType.P2SH_P2WPKH=3]="P2SH_P2WPKH",ScriptType[ScriptType.P2TR=4]="P2TR",ScriptType[ScriptType.__LENGTH=5]="__LENGTH"}(ScriptType=exports.ScriptType||(exports.ScriptType={})),function(CoinType){CoinType[CoinType.BITCOIN=0]="BITCOIN",CoinType[CoinType.TESTNET=1]="TESTNET",CoinType[CoinType.REGTEST=2]="REGTEST",CoinType[CoinType.LITECOIN=3]="LITECOIN",CoinType[CoinType.BITCOINCASH=4]="BITCOINCASH",CoinType[CoinType.GROESTL=5]="GROESTL",CoinType[CoinType.DIGIBYTE=6]="DIGIBYTE",CoinType[CoinType.DASH=7]="DASH",CoinType[CoinType.DOGECOIN=8]="DOGECOIN",CoinType[CoinType.__LENGTH=9]="__LENGTH"}(CoinType=exports.CoinType||(exports.CoinType={})),exports.coinmap=Object.freeze(Object.values(CoinType).slice(0,CoinType.__LENGTH).map((x=>constants[x.toLowerCase()]))),exports.btcCoinTypes=Object.freeze(exports.coinmap.map((x=>Object.freeze(x.coinType)))),exports.btcPurposes=Object.freeze([Object.freeze(44),Object.freeze(49),Object.freeze(84),Object.freeze(86)]),exports.ow_balance=ow_1.default.any(ow_1.default.number.integer.positive,utils.owTool.numberString),exports.ow_path=utils.ow_strictPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_accountPath=utils.ow_accountPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_hexString=utils.owTool.hexString,exports.ow_hashString=utils.owTool.hashString,exports.ow_PathObject=ow_1.default.object.exactShape({coin:ow_1.default.number.inRange(0,CoinType.__LENGTH-1),script:ow_1.default.number.inRange(0,ScriptType.__LENGTH-1)}),exports.ow_txInput=ow_1.default.object.exactShape({hash:exports.ow_hashString,vout:ow_1.default.number.greaterThanOrEqual(0),txHex:ow_1.default.any(ow_1.default.undefined,exports.ow_hexString),script:ow_1.default.optional.number.inRange(0,ScriptType.__LENGTH-1),satoshis:exports.ow_balance,path:exports.ow_path,publickey:ow_1.default.any(ow_1.default.undefined,exports.ow_hexString,ow_1.default.buffer)}),exports.ow_txOutputAddress=ow_1.default.object.exactShape({address:exports.ow_hashString,satoshis:exports.ow_balance}),exports.ow_txOutputScript=ow_1.default.object.exactShape({scriptHex:exports.ow_hexString,satoshis:exports.ow_balance}),exports.ow_txOutputScriptExtened=ow_1.default.object.exactShape({publickey:ow_1.default.any(ow_1.default.undefined,exports.ow_hexString,ow_1.default.buffer),path:exports.ow_path,satoshis:exports.ow_balance,script:ow_1.default.optional.number.inRange(0,ScriptType.__LENGTH-1)}),exports.ow_txOutput=ow_1.default.object.exactShape({to:ow_1.default.any(exports.ow_txOutputAddress,exports.ow_txOutputScriptExtened),utxo:ow_1.default.any(ow_1.default.undefined,exports.ow_txOutputScriptExtened)}),exports.isOutputAddress=function(output){const out=output;if(out.address)return out},exports.isOutuptScript=function(output){const out=output;if(out.scriptHex)return out},exports.isOutuptScriptExtended=function(output){try{return(0,ow_1.default)(output,exports.ow_txOutputScriptExtened),output}catch(error){}},exports.ow_TransactionObject=ow_1.default.object.partialShape({rawTx:exports.ow_hexString,publickeys:ow_1.default.array.ofType(ow_1.default.any(exports.ow_hexString,ow_1.default.buffer)),coin:ow_1.default.optional.number.inRange(0,CoinType.__LENGTH-1)}),exports.ow_AddressOption=ow_1.default.object.exactShape({coin:ow_1.default.optional.number.inRange(0,CoinType.__LENGTH-1),script:ow_1.default.optional.number.inRange(0,ScriptType.__LENGTH-1)}),exports.ow_SignOption=ow_1.default.object.exactShape({coin:ow_1.default.optional.number.inRange(0,CoinType.__LENGTH-1),feeRate:ow_1.default.optional.number.greaterThanOrEqual(1),isRBF:ow_1.default.optional.boolean}); | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.ow_SignOption=exports.ow_AddressOption=exports.ow_TransactionObject=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_hashString=exports.ow_hexString=exports.ow_accountPath=exports.ow_path=exports.ow_balance=exports.btcPurposes=exports.btcCoinTypes=exports.coinmap=exports.CoinType=exports.ScriptType=exports.OPCODES=void 0;const ow_1=require("ow"),constants=require("./coindef"),utils=require("@secux/utility");var ScriptType,CoinType,coindef_1=require("./coindef");Object.defineProperty(exports,"OPCODES",{enumerable:!0,get:function(){return coindef_1.OPCODES}}),function(ScriptType){ScriptType[ScriptType.P2PKH=0]="P2PKH",ScriptType[ScriptType.P2WPKH=1]="P2WPKH",ScriptType[ScriptType.P2SH_P2PKH=2]="P2SH_P2PKH",ScriptType[ScriptType.P2SH_P2WPKH=3]="P2SH_P2WPKH",ScriptType[ScriptType.P2TR=4]="P2TR",ScriptType[ScriptType.__LENGTH=5]="__LENGTH"}(ScriptType=exports.ScriptType||(exports.ScriptType={})),function(CoinType){CoinType[CoinType.BITCOIN=0]="BITCOIN",CoinType[CoinType.TESTNET=1]="TESTNET",CoinType[CoinType.REGTEST=2]="REGTEST",CoinType[CoinType.LITECOIN=3]="LITECOIN",CoinType[CoinType.BITCOINCASH=4]="BITCOINCASH",CoinType[CoinType.GROESTL=5]="GROESTL",CoinType[CoinType.DIGIBYTE=6]="DIGIBYTE",CoinType[CoinType.DASH=7]="DASH",CoinType[CoinType.DOGECOIN=8]="DOGECOIN",CoinType[CoinType.__LENGTH=9]="__LENGTH"}(CoinType=exports.CoinType||(exports.CoinType={})),exports.coinmap=Object.freeze(Object.values(CoinType).slice(0,CoinType.__LENGTH).map((x=>constants[x.toLowerCase()]))),exports.btcCoinTypes=Object.freeze(exports.coinmap.map((x=>Object.freeze(x.coinType)))),exports.btcPurposes=Object.freeze([Object.freeze(44),Object.freeze(49),Object.freeze(84),Object.freeze(86)]),exports.ow_balance=ow_1.default.any(ow_1.default.number.integer.positive,utils.owTool.numberString),exports.ow_path=utils.ow_strictPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_accountPath=utils.ow_accountPath(exports.btcCoinTypes,exports.btcPurposes),exports.ow_hexString=utils.owTool.hexString,exports.ow_hashString=utils.owTool.hashString,exports.ow_PathObject=ow_1.default.object.exactShape({coin:ow_1.default.number.inRange(0,CoinType.__LENGTH-1),script:ow_1.default.number.inRange(0,ScriptType.__LENGTH-1)}),exports.ow_txInput=ow_1.default.object.exactShape({hash:exports.ow_hashString,vout:ow_1.default.number.greaterThanOrEqual(0),txHex:ow_1.default.any(ow_1.default.undefined,exports.ow_hexString),script:ow_1.default.optional.number.inRange(0,ScriptType.__LENGTH-1),satoshis:exports.ow_balance,path:exports.ow_path,publickey:ow_1.default.any(ow_1.default.undefined,exports.ow_hexString,ow_1.default.buffer)}),exports.ow_txOutputAddress=ow_1.default.object.exactShape({address:exports.ow_hashString,satoshis:exports.ow_balance}),exports.ow_txOutputScript=ow_1.default.object.exactShape({scriptHex:exports.ow_hexString,satoshis:exports.ow_balance}),exports.ow_txOutputScriptExtened=ow_1.default.object.exactShape({publickey:ow_1.default.any(ow_1.default.undefined,exports.ow_hexString,ow_1.default.buffer),path:exports.ow_path,satoshis:exports.ow_balance,script:ow_1.default.optional.number.inRange(0,ScriptType.__LENGTH-1)}),exports.ow_txOutput=ow_1.default.object.exactShape({to:ow_1.default.any(exports.ow_txOutputAddress,exports.ow_txOutputScriptExtened),utxo:ow_1.default.any(ow_1.default.undefined,exports.ow_txOutputScriptExtened)}),exports.isOutputAddress=function(output){const out=output;if(out.address)return out},exports.isOutuptScript=function(output){const out=output;if(out.scriptHex)return out},exports.isOutuptScriptExtended=function(output){try{return(0,ow_1.default)(output,exports.ow_txOutputScriptExtened),output}catch(error){}},exports.ow_TransactionObject=ow_1.default.object.partialShape({rawTx:exports.ow_hexString,publickeys:ow_1.default.array.ofType(ow_1.default.any(exports.ow_hexString,ow_1.default.buffer)),coin:ow_1.default.optional.number.inRange(0,CoinType.__LENGTH-1)}),exports.ow_AddressOption=ow_1.default.object.exactShape({coin:ow_1.default.optional.number.inRange(0,CoinType.__LENGTH-1),script:ow_1.default.optional.number.inRange(0,ScriptType.__LENGTH-1)}),exports.ow_SignOption=ow_1.default.object.partialShape({coin:ow_1.default.optional.number.inRange(0,CoinType.__LENGTH-1),feeRate:ow_1.default.optional.number.greaterThanOrEqual(1),isRBF:ow_1.default.optional.boolean}); |
@@ -19,3 +19,3 @@ /*! | ||
/// <reference types="node" /> | ||
export declare function compile(chunks: any[]): Buffer | undefined; | ||
export declare function compile(chunks: any[]): Buffer; | ||
export declare function decompile(buffer: Buffer): (number | Buffer)[]; | ||
@@ -22,0 +22,0 @@ export declare function decode(buffer: Buffer): { |
@@ -18,2 +18,2 @@ "use strict"; | ||
limitations under the License. | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.encode=exports.decode=exports.decompile=exports.compile=void 0;const bip66=require("bip66"),coindef_1=require("./coindef"),utility_1=require("@secux/utility"),ZERO=Buffer.alloc(1,0),logger=null===utility_1.Logger||void 0===utility_1.Logger?void 0:utility_1.Logger.child({id:"script"});function toDER(x){let i=0;for(;0===x[i];)++i;return i===x.length?ZERO:128&(x=x.slice(i))[0]?Buffer.concat([ZERO,x],1+x.length):x}function fromDER(x){0===x[0]&&(x=x.slice(1));const buffer=Buffer.alloc(32,0),bstart=Math.max(0,32-x.length);return x.copy(buffer,bstart),buffer}function asMinimalOP(buffer){return 0===buffer.length?coindef_1.OPCODES.OP_0:1===buffer.length?buffer[0]>=1&&buffer[0]<=16?coindef_1.OPCODES.OP_INT_BASE+buffer[0]:129===buffer[0]?coindef_1.OPCODES.OP_1NEGATE:void 0:void 0}exports.compile=function(chunks){const bufferSize=chunks.reduce(((accum,chunk)=>Buffer.isBuffer(chunk)?1===chunk.length&&void 0!==asMinimalOP(chunk)?accum+1:accum+pushdata.encodingLength(chunk.length)+chunk.length:accum+1),0),buffer=Buffer.allocUnsafe(bufferSize);let offset=0;for(const chunk of chunks)if(Buffer.isBuffer(chunk)){const opcode=asMinimalOP(chunk);if(void 0!==opcode)return buffer.writeUInt8(opcode,offset),void(offset+=1);offset+=pushdata.encode(buffer,chunk.length,offset),chunk.copy(buffer,offset),offset+=chunk.length}else buffer.writeUInt8(chunk,offset),offset+=1;if(offset!==buffer.length)throw new Error("Could not decode chunks");return buffer},exports.decompile=function(buffer){const chunks=[];let i=0;for(;i<buffer.length;){const opcode=buffer[i];if(opcode>coindef_1.OPCODES.OP_0&&opcode<=coindef_1.OPCODES.OP_PUSHDATA4){const d=pushdata.decode(buffer,i);if(null===d)return null==logger||logger.warn(`decompile error: reading a pushDataInt fail, got ${buffer.toString("binary")}, index:${i}`),[];if(i+=d.size,i+d.number>buffer.length)return null==logger||logger.warn(`decompile error: attempt to read too much data, got ${buffer.slice(i).toString("binary")}, desired length:${d.number}`),[];const data=buffer.slice(i,i+d.number);i+=d.number;const op=asMinimalOP(data);void 0!==op?chunks.push(op):chunks.push(data)}else chunks.push(opcode),i+=1}return chunks},exports.decode=function(buffer){const hashType=buffer.readUInt8(buffer.length-1),hashTypeMod=-129&hashType;if(hashTypeMod<=0||hashTypeMod>=4)throw new Error("Invalid hashType "+hashType);const decoded=bip66.decode(buffer.slice(0,-1)),r=fromDER(decoded.r),s=fromDER(decoded.s);return{signature:Buffer.concat([r,s],64),hashType}},exports.encode=function(signature,hashType){const hashTypeBuffer=Buffer.allocUnsafe(1);hashTypeBuffer.writeUInt8(hashType,0);const r=toDER(signature.slice(0,32)),s=toDER(signature.slice(32,64));return Buffer.concat([bip66.encode(r,s),hashTypeBuffer])};class pushdata{static encodingLength(i){return i<coindef_1.OPCODES.OP_PUSHDATA1?1:i<=255?2:i<=65535?3:5}static encode(buffer,number,offset){var size=this.encodingLength(number);return 1===size?buffer.writeUInt8(number,offset):2===size?(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA1,offset),buffer.writeUInt8(number,offset+1)):3===size?(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA2,offset),buffer.writeUInt16LE(number,offset+1)):(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA4,offset),buffer.writeUInt32LE(number,offset+1)),size}static decode(buffer,offset){var number,size,opcode=buffer.readUInt8(offset);if(opcode<coindef_1.OPCODES.OP_PUSHDATA1)number=opcode,size=1;else if(opcode===coindef_1.OPCODES.OP_PUSHDATA1){if(offset+2>buffer.length)return null;number=buffer.readUInt8(offset+1),size=2}else if(opcode===coindef_1.OPCODES.OP_PUSHDATA2){if(offset+3>buffer.length)return null;number=buffer.readUInt16LE(offset+1),size=3}else{if(offset+5>buffer.length)return null;if(opcode!==coindef_1.OPCODES.OP_PUSHDATA4)throw new Error("Unexpected opcode");number=buffer.readUInt32LE(offset+1),size=5}return{opcode,number,size}}} | ||
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.encode=exports.decode=exports.decompile=exports.compile=void 0;const bip66=require("bip66"),coindef_1=require("./coindef"),utility_1=require("@secux/utility"),ZERO=Buffer.alloc(1,0),logger=null===utility_1.Logger||void 0===utility_1.Logger?void 0:utility_1.Logger.child({id:"script"});function toDER(x){let i=0;for(;0===x[i];)++i;return i===x.length?ZERO:128&(x=x.slice(i))[0]?Buffer.concat([ZERO,x],1+x.length):x}function fromDER(x){0===x[0]&&(x=x.slice(1));const buffer=Buffer.alloc(32,0),bstart=Math.max(0,32-x.length);return x.copy(buffer,bstart),buffer}function asMinimalOP(buffer){return 0===buffer.length?coindef_1.OPCODES.OP_0:1===buffer.length?buffer[0]>=1&&buffer[0]<=16?coindef_1.OPCODES.OP_INT_BASE+buffer[0]:129===buffer[0]?coindef_1.OPCODES.OP_1NEGATE:void 0:void 0}exports.compile=function(chunks){const bufferSize=chunks.reduce(((accum,chunk)=>Buffer.isBuffer(chunk)?1===chunk.length&&void 0!==asMinimalOP(chunk)?accum+1:accum+pushdata.encodingLength(chunk.length)+chunk.length:accum+1),0),buffer=Buffer.allocUnsafe(bufferSize);let offset=0;for(const chunk of chunks)if(Buffer.isBuffer(chunk)){const opcode=asMinimalOP(chunk);if(void 0!==opcode){buffer.writeUInt8(opcode,offset),offset+=1;continue}offset+=pushdata.encode(buffer,chunk.length,offset),chunk.copy(buffer,offset),offset+=chunk.length}else buffer.writeUInt8(chunk,offset),offset+=1;if(offset!==buffer.length)throw new Error("Could not decode chunks");return buffer},exports.decompile=function(buffer){const chunks=[];let i=0;for(;i<buffer.length;){const opcode=buffer[i];if(opcode>coindef_1.OPCODES.OP_0&&opcode<=coindef_1.OPCODES.OP_PUSHDATA4){const d=pushdata.decode(buffer,i);if(null===d)return null==logger||logger.warn(`decompile error: reading a pushDataInt fail, got ${buffer.toString("binary")}, index:${i}`),[];if(i+=d.size,i+d.number>buffer.length)return null==logger||logger.warn(`decompile error: attempt to read too much data, got ${buffer.slice(i).toString("binary")}, desired length:${d.number}`),[];const data=buffer.slice(i,i+d.number);i+=d.number;const op=asMinimalOP(data);void 0!==op?chunks.push(op):chunks.push(data)}else chunks.push(opcode),i+=1}return chunks},exports.decode=function(buffer){const hashType=buffer.readUInt8(buffer.length-1),hashTypeMod=-129&hashType;if(hashTypeMod<=0||hashTypeMod>=4)throw new Error("Invalid hashType "+hashType);const decoded=bip66.decode(buffer.slice(0,-1)),r=fromDER(decoded.r),s=fromDER(decoded.s);return{signature:Buffer.concat([r,s],64),hashType}},exports.encode=function(signature,hashType){const hashTypeBuffer=Buffer.allocUnsafe(1);hashTypeBuffer.writeUInt8(hashType,0);const r=toDER(signature.slice(0,32)),s=toDER(signature.slice(32,64));return Buffer.concat([bip66.encode(r,s),hashTypeBuffer])};class pushdata{static encodingLength(i){return i<coindef_1.OPCODES.OP_PUSHDATA1?1:i<=255?2:i<=65535?3:5}static encode(buffer,number,offset){const size=this.encodingLength(number);return 1===size?buffer.writeUInt8(number,offset):2===size?(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA1,offset),buffer.writeUInt8(number,offset+1)):3===size?(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA2,offset),buffer.writeUInt16LE(number,offset+1)):(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA4,offset),buffer.writeUInt32LE(number,offset+1)),size}static decode(buffer,offset){const opcode=buffer.readUInt8(offset);let number,size;if(opcode<coindef_1.OPCODES.OP_PUSHDATA1)number=opcode,size=1;else if(opcode===coindef_1.OPCODES.OP_PUSHDATA1){if(offset+2>buffer.length)return null;number=buffer.readUInt8(offset+1),size=2}else if(opcode===coindef_1.OPCODES.OP_PUSHDATA2){if(offset+3>buffer.length)return null;number=buffer.readUInt16LE(offset+1),size=3}else{if(offset+5>buffer.length)return null;if(opcode!==coindef_1.OPCODES.OP_PUSHDATA4)throw new Error("Unexpected opcode");number=buffer.readUInt32LE(offset+1),size=5}return{opcode,number,size}}} |
{ | ||
"name": "@secux/app-btc", | ||
"version": "3.2.9", | ||
"version": "3.2.10", | ||
"description": "SecuX Hardware Wallet BTC API", | ||
@@ -13,3 +13,3 @@ "keywords": [ | ||
], | ||
"homepage": "https://github.com/secuxtech/secux-js/tree/master/packages/app-btc", | ||
"homepage": "https://github.com/SecuX/secux-js/tree/master/packages/app-btc", | ||
"author": "SecuX Technology Inc.", | ||
@@ -46,5 +46,3 @@ "license": "Apache-2.0", | ||
"bip66": "^1.1.5", | ||
"bitcoin-address-validation": "^2.2.1", | ||
"cashaddrjs": "^0.4.4", | ||
"coinselect": "^3.1.13", | ||
"ecurve": "^1.0.6", | ||
@@ -51,0 +49,0 @@ "groestl-hash-js": "^1.0.0", |
Sorry, the diff of this file is too big to display
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
673083
13
1089
13
- Removedbitcoin-address-validation@^2.2.1
- Removedcoinselect@^3.1.13
- Removedbase58-js@1.0.5(transitive)
- Removedbitcoin-address-validation@2.2.3(transitive)
- Removedcoinselect@3.1.13(transitive)
- Removedsha256-uint8array@0.10.7(transitive)