@xchainjs/xchain-bitcoin
Advanced tools
Comparing version 0.22.4 to 0.23.0
@@ -1,2 +0,3 @@ | ||
import { AssetInfo, Fee, FeeRate, TxHash, TxParams, UTXO, UTXOClient, UtxoClientParams } from '@xchainjs/xchain-client'; | ||
/// <reference types="node" /> | ||
import { AssetInfo, FeeRate, TxHash, TxParams, UTXO, UTXOClient, UtxoClientParams } from '@xchainjs/xchain-client'; | ||
import { Address } from '@xchainjs/xchain-util'; | ||
@@ -53,4 +54,19 @@ import * as Bitcoin from 'bitcoinjs-lib'; | ||
protected getSuggestedFeeRate(): Promise<FeeRate>; | ||
protected calcFee(feeRate: FeeRate, memo?: string): Promise<Fee>; | ||
/** | ||
* Compile memo. | ||
* | ||
* @param {string} memo The memo to be compiled. | ||
* @returns {Buffer} The compiled memo. | ||
*/ | ||
protected compileMemo(memo: string): Buffer; | ||
/** | ||
* Get the transaction fee. | ||
* | ||
* @param {UTXO[]} inputs The UTXOs. | ||
* @param {FeeRate} feeRate The fee rate. | ||
* @param {Buffer} data The compiled memo (Optional). | ||
* @returns {number} The fee amount. | ||
*/ | ||
protected getFeeFromUtxos(inputs: UTXO[], feeRate: FeeRate, data?: Buffer | null): number; | ||
/** | ||
* Transfer BTC. | ||
@@ -57,0 +73,0 @@ * |
export * from './types'; | ||
export * from './client'; | ||
export * from './const'; | ||
export { getDefaultFees, getDefaultFeesWithRates, getPrefix, validateAddress, calcFee } from './utils'; | ||
export { getPrefix, validateAddress } from './utils'; |
@@ -1,2 +0,2 @@ | ||
import { ExplorerProvider, Network, standardFeeRates, FeeOption, calcFees, UTXOClient, checkFeeBounds } from '@xchainjs/xchain-client'; | ||
import { ExplorerProvider, Network, UTXOClient, FeeOption, checkFeeBounds } from '@xchainjs/xchain-client'; | ||
import { getSeed } from '@xchainjs/xchain-crypto'; | ||
@@ -7,3 +7,2 @@ import axios from 'axios'; | ||
import { SochainProvider, SochainNetwork, HaskoinProvider, HaskoinNetwork, BlockcypherProvider, BlockcypherNetwork } from '@xchainjs/xchain-utxo-providers'; | ||
import { baseAmount } from '@xchainjs/xchain-util'; | ||
@@ -102,39 +101,6 @@ /****************************************************************************** | ||
const inputBytes = (input) => { | ||
return TX_INPUT_BASE + (input.witnessUtxo.script ? input.witnessUtxo.script.length : TX_INPUT_PUBKEYHASH); | ||
var _a, _b; | ||
return TX_INPUT_BASE + (((_a = input.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script) ? (_b = input.witnessUtxo) === null || _b === void 0 ? void 0 : _b.script.length : TX_INPUT_PUBKEYHASH); | ||
}; | ||
/** | ||
* Compile memo. | ||
* | ||
* @param {string} memo The memo to be compiled. | ||
* @returns {Buffer} The compiled memo. | ||
*/ | ||
const compileMemo = (memo) => { | ||
const data = Buffer.from(memo, 'utf8'); // converts MEMO to buffer | ||
return Bitcoin.script.compile([Bitcoin.opcodes.OP_RETURN, data]); // Compile OP_RETURN script | ||
}; | ||
/** | ||
* Get the transaction fee. | ||
* | ||
* @param {UTXO[]} inputs The UTXOs. | ||
* @param {FeeRate} feeRate The fee rate. | ||
* @param {Buffer} data The compiled memo (Optional). | ||
* @returns {number} The fee amount. | ||
*/ | ||
const getFee = (inputs, feeRate, data = null) => { | ||
const inputSizeBasedOnInputs = inputs.length > 0 | ||
? inputs.reduce((a, x) => a + inputBytes(x), 0) + inputs.length // +1 byte for each input signature | ||
: (TX_INPUT_BASE + TX_INPUT_PUBKEYHASH) * 2 + 2; // By default 2 UTXOs // Temporal solution until issue addressed https://github.com/xchainjs/xchainjs-lib/issues/850 | ||
let sum = TX_EMPTY_SIZE + | ||
inputSizeBasedOnInputs + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH; | ||
if (data) { | ||
sum += TX_OUTPUT_BASE + data.length; | ||
} | ||
const fee = sum * feeRate; | ||
return fee > MIN_TX_FEE ? fee : MIN_TX_FEE; | ||
}; | ||
/** | ||
* Get Bitcoin network to be used with bitcoinjs. | ||
@@ -171,35 +137,2 @@ * | ||
/** | ||
* Calculate fees based on fee rate and memo. | ||
* | ||
* @param {FeeRate} feeRate | ||
* @param {string} memo | ||
* @returns {BaseAmount} The calculated fees based on fee rate and the memo. | ||
*/ | ||
const calcFee = (feeRate, memo) => { | ||
const compiledMemo = memo ? compileMemo(memo) : null; | ||
const fee = getFee([], feeRate, compiledMemo); | ||
return baseAmount(fee); | ||
}; | ||
/** | ||
* Get the default fees with rates. | ||
* | ||
* @returns {FeesWithRates} The default fees and rates. | ||
*/ | ||
const getDefaultFeesWithRates = () => { | ||
const rates = Object.assign(Object.assign({}, standardFeeRates(20)), { [FeeOption.Fastest]: 50 }); | ||
return { | ||
fees: calcFees(rates, calcFee), | ||
rates, | ||
}; | ||
}; | ||
/** | ||
* Get the default fees. | ||
* | ||
* @returns {Fees} The default fees. | ||
*/ | ||
const getDefaultFees = () => { | ||
const { fees } = getDefaultFeesWithRates(); | ||
return fees; | ||
}; | ||
/** | ||
* Get address prefix based on the network. | ||
@@ -339,8 +272,37 @@ * | ||
} | ||
calcFee(feeRate, memo) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return calcFee(feeRate, memo); | ||
}); | ||
/** | ||
* Compile memo. | ||
* | ||
* @param {string} memo The memo to be compiled. | ||
* @returns {Buffer} The compiled memo. | ||
*/ | ||
compileMemo(memo) { | ||
const data = Buffer.from(memo, 'utf8'); // converts MEMO to buffer | ||
return Bitcoin.script.compile([Bitcoin.opcodes.OP_RETURN, data]); // Compile OP_RETURN script | ||
} | ||
/** | ||
* Get the transaction fee. | ||
* | ||
* @param {UTXO[]} inputs The UTXOs. | ||
* @param {FeeRate} feeRate The fee rate. | ||
* @param {Buffer} data The compiled memo (Optional). | ||
* @returns {number} The fee amount. | ||
*/ | ||
getFeeFromUtxos(inputs, feeRate, data = null) { | ||
const inputSizeBasedOnInputs = inputs.length > 0 | ||
? inputs.reduce((a, x) => a + inputBytes(x), 0) + inputs.length // +1 byte for each input signature | ||
: 0; | ||
let sum = TX_EMPTY_SIZE + | ||
inputSizeBasedOnInputs + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH; | ||
if (data) { | ||
sum += TX_OUTPUT_BASE + data.length; | ||
} | ||
const fee = sum * feeRate; | ||
return fee > MIN_TX_FEE ? fee : MIN_TX_FEE; | ||
} | ||
/** | ||
* Transfer BTC. | ||
@@ -389,3 +351,3 @@ * | ||
const feeRateWhole = Math.ceil(feeRate); | ||
const compiledMemo = memo ? compileMemo(memo) : null; | ||
const compiledMemo = memo ? this.compileMemo(memo) : null; | ||
const targetOutputs = []; | ||
@@ -434,3 +396,3 @@ //1. add output amount and recipient to targets | ||
export { AssetBTC, BTCChain, BTC_DECIMAL, BTC_SATOSHI_SYMBOL, BTC_SYMBOL, BlockcypherDataProviders, Client, HaskoinDataProviders, LOWER_FEE_BOUND, MIN_TX_FEE, SochainDataProviders, UPPER_FEE_BOUND, blockstreamExplorerProviders, calcFee, defaultBTCParams, getDefaultFees, getDefaultFeesWithRates, getPrefix, validateAddress }; | ||
export { AssetBTC, BTCChain, BTC_DECIMAL, BTC_SATOSHI_SYMBOL, BTC_SYMBOL, BlockcypherDataProviders, Client, HaskoinDataProviders, LOWER_FEE_BOUND, MIN_TX_FEE, SochainDataProviders, UPPER_FEE_BOUND, blockstreamExplorerProviders, defaultBTCParams, getPrefix, validateAddress }; | ||
//# sourceMappingURL=index.esm.js.map |
113
lib/index.js
@@ -11,3 +11,2 @@ 'use strict'; | ||
var xchainUtxoProviders = require('@xchainjs/xchain-utxo-providers'); | ||
var xchainUtil = require('@xchainjs/xchain-util'); | ||
@@ -130,39 +129,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
const inputBytes = (input) => { | ||
return TX_INPUT_BASE + (input.witnessUtxo.script ? input.witnessUtxo.script.length : TX_INPUT_PUBKEYHASH); | ||
var _a, _b; | ||
return TX_INPUT_BASE + (((_a = input.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script) ? (_b = input.witnessUtxo) === null || _b === void 0 ? void 0 : _b.script.length : TX_INPUT_PUBKEYHASH); | ||
}; | ||
/** | ||
* Compile memo. | ||
* | ||
* @param {string} memo The memo to be compiled. | ||
* @returns {Buffer} The compiled memo. | ||
*/ | ||
const compileMemo = (memo) => { | ||
const data = Buffer.from(memo, 'utf8'); // converts MEMO to buffer | ||
return Bitcoin__namespace.script.compile([Bitcoin__namespace.opcodes.OP_RETURN, data]); // Compile OP_RETURN script | ||
}; | ||
/** | ||
* Get the transaction fee. | ||
* | ||
* @param {UTXO[]} inputs The UTXOs. | ||
* @param {FeeRate} feeRate The fee rate. | ||
* @param {Buffer} data The compiled memo (Optional). | ||
* @returns {number} The fee amount. | ||
*/ | ||
const getFee = (inputs, feeRate, data = null) => { | ||
const inputSizeBasedOnInputs = inputs.length > 0 | ||
? inputs.reduce((a, x) => a + inputBytes(x), 0) + inputs.length // +1 byte for each input signature | ||
: (TX_INPUT_BASE + TX_INPUT_PUBKEYHASH) * 2 + 2; // By default 2 UTXOs // Temporal solution until issue addressed https://github.com/xchainjs/xchainjs-lib/issues/850 | ||
let sum = TX_EMPTY_SIZE + | ||
inputSizeBasedOnInputs + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH; | ||
if (data) { | ||
sum += TX_OUTPUT_BASE + data.length; | ||
} | ||
const fee = sum * feeRate; | ||
return fee > MIN_TX_FEE ? fee : MIN_TX_FEE; | ||
}; | ||
/** | ||
* Get Bitcoin network to be used with bitcoinjs. | ||
@@ -199,35 +165,2 @@ * | ||
/** | ||
* Calculate fees based on fee rate and memo. | ||
* | ||
* @param {FeeRate} feeRate | ||
* @param {string} memo | ||
* @returns {BaseAmount} The calculated fees based on fee rate and the memo. | ||
*/ | ||
const calcFee = (feeRate, memo) => { | ||
const compiledMemo = memo ? compileMemo(memo) : null; | ||
const fee = getFee([], feeRate, compiledMemo); | ||
return xchainUtil.baseAmount(fee); | ||
}; | ||
/** | ||
* Get the default fees with rates. | ||
* | ||
* @returns {FeesWithRates} The default fees and rates. | ||
*/ | ||
const getDefaultFeesWithRates = () => { | ||
const rates = Object.assign(Object.assign({}, xchainClient.standardFeeRates(20)), { [xchainClient.FeeOption.Fastest]: 50 }); | ||
return { | ||
fees: xchainClient.calcFees(rates, calcFee), | ||
rates, | ||
}; | ||
}; | ||
/** | ||
* Get the default fees. | ||
* | ||
* @returns {Fees} The default fees. | ||
*/ | ||
const getDefaultFees = () => { | ||
const { fees } = getDefaultFeesWithRates(); | ||
return fees; | ||
}; | ||
/** | ||
* Get address prefix based on the network. | ||
@@ -367,8 +300,37 @@ * | ||
} | ||
calcFee(feeRate, memo) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return calcFee(feeRate, memo); | ||
}); | ||
/** | ||
* Compile memo. | ||
* | ||
* @param {string} memo The memo to be compiled. | ||
* @returns {Buffer} The compiled memo. | ||
*/ | ||
compileMemo(memo) { | ||
const data = Buffer.from(memo, 'utf8'); // converts MEMO to buffer | ||
return Bitcoin__namespace.script.compile([Bitcoin__namespace.opcodes.OP_RETURN, data]); // Compile OP_RETURN script | ||
} | ||
/** | ||
* Get the transaction fee. | ||
* | ||
* @param {UTXO[]} inputs The UTXOs. | ||
* @param {FeeRate} feeRate The fee rate. | ||
* @param {Buffer} data The compiled memo (Optional). | ||
* @returns {number} The fee amount. | ||
*/ | ||
getFeeFromUtxos(inputs, feeRate, data = null) { | ||
const inputSizeBasedOnInputs = inputs.length > 0 | ||
? inputs.reduce((a, x) => a + inputBytes(x), 0) + inputs.length // +1 byte for each input signature | ||
: 0; | ||
let sum = TX_EMPTY_SIZE + | ||
inputSizeBasedOnInputs + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH + | ||
TX_OUTPUT_BASE + | ||
TX_OUTPUT_PUBKEYHASH; | ||
if (data) { | ||
sum += TX_OUTPUT_BASE + data.length; | ||
} | ||
const fee = sum * feeRate; | ||
return fee > MIN_TX_FEE ? fee : MIN_TX_FEE; | ||
} | ||
/** | ||
* Transfer BTC. | ||
@@ -417,3 +379,3 @@ * | ||
const feeRateWhole = Math.ceil(feeRate); | ||
const compiledMemo = memo ? compileMemo(memo) : null; | ||
const compiledMemo = memo ? this.compileMemo(memo) : null; | ||
const targetOutputs = []; | ||
@@ -475,8 +437,5 @@ //1. add output amount and recipient to targets | ||
exports.blockstreamExplorerProviders = blockstreamExplorerProviders; | ||
exports.calcFee = calcFee; | ||
exports.defaultBTCParams = defaultBTCParams; | ||
exports.getDefaultFees = getDefaultFees; | ||
exports.getDefaultFeesWithRates = getDefaultFeesWithRates; | ||
exports.getPrefix = getPrefix; | ||
exports.validateAddress = validateAddress; | ||
//# sourceMappingURL=index.js.map |
@@ -1,22 +0,11 @@ | ||
/// <reference types="node" /> | ||
import { FeeRate, Fees, FeesWithRates, Network, UTXO } from '@xchainjs/xchain-client'; | ||
import { Address, BaseAmount } from '@xchainjs/xchain-util'; | ||
import { Network, UTXO } from '@xchainjs/xchain-client'; | ||
import { Address } from '@xchainjs/xchain-util'; | ||
import * as Bitcoin from 'bitcoinjs-lib'; | ||
export declare const TX_EMPTY_SIZE: number; | ||
export declare const TX_INPUT_BASE: number; | ||
export declare const TX_INPUT_PUBKEYHASH = 107; | ||
export declare const TX_OUTPUT_BASE: number; | ||
export declare const TX_OUTPUT_PUBKEYHASH = 25; | ||
export declare const inputBytes: (input: UTXO) => number; | ||
/** | ||
* Compile memo. | ||
* | ||
* @param {string} memo The memo to be compiled. | ||
* @returns {Buffer} The compiled memo. | ||
*/ | ||
export declare const compileMemo: (memo: string) => Buffer; | ||
/** | ||
* Get the transaction fee. | ||
* | ||
* @param {UTXO[]} inputs The UTXOs. | ||
* @param {FeeRate} feeRate The fee rate. | ||
* @param {Buffer} data The compiled memo (Optional). | ||
* @returns {number} The fee amount. | ||
*/ | ||
export declare const getFee: (inputs: UTXO[], feeRate: FeeRate, data?: Buffer | null) => number; | ||
/** | ||
* Get the average value of an array. | ||
@@ -44,22 +33,2 @@ * | ||
/** | ||
* Calculate fees based on fee rate and memo. | ||
* | ||
* @param {FeeRate} feeRate | ||
* @param {string} memo | ||
* @returns {BaseAmount} The calculated fees based on fee rate and the memo. | ||
*/ | ||
export declare const calcFee: (feeRate: FeeRate, memo?: string) => BaseAmount; | ||
/** | ||
* Get the default fees with rates. | ||
* | ||
* @returns {FeesWithRates} The default fees and rates. | ||
*/ | ||
export declare const getDefaultFeesWithRates: () => FeesWithRates; | ||
/** | ||
* Get the default fees. | ||
* | ||
* @returns {Fees} The default fees. | ||
*/ | ||
export declare const getDefaultFees: () => Fees; | ||
/** | ||
* Get address prefix based on the network. | ||
@@ -66,0 +35,0 @@ * |
{ | ||
"name": "@xchainjs/xchain-bitcoin", | ||
"version": "0.22.4", | ||
"version": "0.23.0", | ||
"description": "Custom Bitcoin client and utilities used by XChainJS clients", | ||
@@ -37,6 +37,6 @@ "keywords": [ | ||
"@types/wif": "^2.0.2", | ||
"@xchainjs/xchain-client": "^0.14.2", | ||
"@xchainjs/xchain-client": "^0.15.0", | ||
"@xchainjs/xchain-crypto": "^0.3.0", | ||
"@xchainjs/xchain-util": "^0.13.1", | ||
"@xchainjs/xchain-utxo-providers": "^0.2.4", | ||
"@xchainjs/xchain-utxo-providers": "^0.2.5", | ||
"axios": "^1.3.6", | ||
@@ -49,6 +49,6 @@ "axios-mock-adapter": "^1.20.0", | ||
"peerDependencies": { | ||
"@xchainjs/xchain-client": "^0.14.2", | ||
"@xchainjs/xchain-client": "^0.15.0", | ||
"@xchainjs/xchain-crypto": "^0.3.0", | ||
"@xchainjs/xchain-util": "^0.13.1", | ||
"@xchainjs/xchain-utxo-providers": "^0.2.4", | ||
"@xchainjs/xchain-utxo-providers": "^0.2.5", | ||
"axios": "^1.3.6", | ||
@@ -55,0 +55,0 @@ "bitcoinjs-lib": "5.2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
97659
993