diff --git a/src/networks.js b/src/networks.js index fdb3706..eedb66a 100644 --- a/src/networks.js +++ b/src/networks.js @@ -1,6 +1,11 @@ // https://en.bitcoin.it/wiki/List_of_address_prefixes // Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731 -module.exports = { + +function bitcoinEstimateFee(txByteSize) { + return networks.bitcoin.feePerKb * Math.ceil(txByteSize / 1000) +} + +var networks = { bitcoin: { magicPrefix: '\x18Bitcoin Signed Message:\n', bip32: { @@ -10,8 +15,9 @@ module.exports = { pubKeyHash: 0x00, scriptHash: 0x05, wif: 0x80, - dustThreshold: 546, - feePerKb: 10000 + dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/529047fcd18acd1b64dc95d6eb69edeaad75d405/src/core.h#L176-L188 + feePerKb: 10000, // https://github.com/bitcoin/bitcoin/blob/3f39b9d4551d729c3a2e4decd810ac6887cfaeb3/src/main.cpp#L52 + estimateFee: bitcoinEstimateFee }, dogecoin: { magicPrefix: '\x19Dogecoin Signed Message:\n', @@ -47,6 +53,8 @@ module.exports = { scriptHash: 0xc4, wif: 0xef, dustThreshold: 546, - feePerKb: 10000 + feePerKb: 10000, + estimateFee: bitcoinEstimateFee } } +module.exports = networks diff --git a/src/wallet.js b/src/wallet.js index 1e484f2..b15bcfd 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -237,7 +237,7 @@ function Wallet(seed, network) { tmpTx.addOutput(getChangeAddress(), 0) var byteSize = tmpTx.toBuffer().length - return network.feePerKb * Math.ceil(byteSize / 1000) + return network.estimateFee(byteSize) } function getChangeAddress() { diff --git a/test/network.js b/test/network.js new file mode 100644 index 0000000..ee0d6d4 --- /dev/null +++ b/test/network.js @@ -0,0 +1,23 @@ +var assert = require('assert') +var networks = require('../src/networks') +var Transaction = require('../src/transaction') + +var fixtureTxes = require('./fixtures/mainnet_tx') +var fixtureTx1Hex = fixtureTxes.prevTx +var fixtureTxBigHex = fixtureTxes.bigTx + +describe('bitcoin', function() { + describe('estimateFee', function() { + var estimateFee = networks.bitcoin.estimateFee + + it('works for fixture tx 1', function() { + var tx = Transaction.fromHex(fixtureTx1Hex) + assert.equal(estimateFee(tx.toBuffer().length), 10000) + }) + + it('works for fixture big tx', function() { + var tx = Transaction.fromHex(fixtureTxBigHex) + assert.equal(estimateFee(tx.toBuffer().length), 30000) + }) + }) +})