From 7d2e1cd3badd2640a70f63acedb3b9d4d0394107 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 16 Mar 2015 11:38:41 +1100 Subject: [PATCH 1/2] networks: remove exhaustive non-top 5 networks --- src/networks.js | 64 -------------------------------------- test/fixtures/network.json | 7 ----- 2 files changed, 71 deletions(-) diff --git a/src/networks.js b/src/networks.js index f2d6bc5..ddd5c00 100644 --- a/src/networks.js +++ b/src/networks.js @@ -54,70 +54,6 @@ var networks = { dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160 dustSoftThreshold: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.h#L62 feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58 - }, - viacoin: { - messagePrefix: '\x18Viacoin Signed Message:\n', - bip32: { - public: 0x0488b21e, - private: 0x0488ade4 - }, - pubKeyHash: 0x47, - scriptHash: 0x21, - wif: 0xc7, - dustThreshold: 560, - dustSoftThreshold: 100000, - feePerKb: 100000 - }, - viacointestnet: { - messagePrefix: '\x18Viacoin Signed Message:\n', - bip32: { - public: 0x043587cf, - private: 0x04358394 - }, - pubKeyHash: 0x7f, - scriptHash: 0xc4, - wif: 0xff, - dustThreshold: 560, - dustSoftThreshold: 100000, - feePerKb: 100000 - }, - gamerscoin: { - messagePrefix: '\x19Gamerscoin Signed Message:\n', - bip32: { - public: 0x019da462, - private: 0x019d9cfe - }, - pubKeyHash: 0x26, - scriptHash: 0x05, - wif: 0xA6, - dustThreshold: 0, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L358-L363 - dustSoftThreshold: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L51 - feePerKb: 100000 // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54 - }, - jumbucks: { - messagePrefix: '\x19Jumbucks Signed Message:\n', - bip32: { - public: 0x037a689a, - private: 0x037a6460 - }, - pubKeyHash: 0x2b, - scriptHash: 0x05, - wif: 0xab, - dustThreshold: 0, - dustSoftThreshold: 10000, - feePerKb: 10000 - }, - zetacoin: { - messagePrefix: '\x18Zetacoin Signed Message:\n', - bip32: { - public: 0x0488b21e, - private: 0x0488ade4 - }, - pubKeyHash: 0x50, - scriptHash: 0x09, - wif: 0xe0, - dustThreshold: 546, // https://github.com/zetacoin/zetacoin/blob/master/src/core.h#L159 - feePerKb: 10000 // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54 } } diff --git a/test/fixtures/network.json b/test/fixtures/network.json index eae5956..58737ad 100644 --- a/test/fixtures/network.json +++ b/test/fixtures/network.json @@ -28,13 +28,6 @@ "private": "dgpv51eADS3spNJh9Gjth94XcPwAczvQaDJs9rqx11kvxKs6r3Ek8AgERHhjLs6mzXQFHRzQqGwqdeoDkZmr8jQMBfi43b7sT3sx3cCSk5fGeUR", "public": "dgub8kXBZ7ymNWy2S8Q3jNgVjFUm5ZJ3QLLaSTdAA89ukSv7Q6MSXwE14b7Nv6eDpE9JJXinTKc8LeLVu19uDPrm5uJuhpKNzV2kAgncwo6bNpP" } - }, - { - "network": "jumbucks", - "bip32": { - "private": "jprv5eCacBgN4Bz4zYxgVQ7RDt1a3eREhEaj8KjAcJ7YwogxGo2rmBF5kvAQS53JwZpo5wnUmJ9Q7kB6b2gQ1MzC6yaTc188hr6hXZ5t8Ruria1", - "public": "jpub1sBw1hDFtZYND339bReRb1xJbgFj6hJaVYemQgXAW9Dw9bN1JiZLJiUtHLgcTTEs1UgRGFAYm3XQPYsYJbpqj1aYPhrMsNcJHfgdAhvFZBB" - } } ], "estimateFee": [ From bd464d7cb51e21a555b83a177d2a3bc79eccde86 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 16 Mar 2015 12:35:49 +1100 Subject: [PATCH 2/2] networks: remove estimateFee entirely --- src/networks.js | 45 ++---------- test/fixtures/network.json | 144 ++++++++----------------------------- test/networks.js | 45 ++---------- 3 files changed, 41 insertions(+), 193 deletions(-) diff --git a/src/networks.js b/src/networks.js index ddd5c00..d694bf7 100644 --- a/src/networks.js +++ b/src/networks.js @@ -1,7 +1,7 @@ // https://en.bitcoin.it/wiki/List_of_address_prefixes // Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731 -var networks = { +module.exports = { bitcoin: { magic: 0xd9b4bef9, messagePrefix: '\x18Bitcoin Signed Message:\n', @@ -12,8 +12,7 @@ var networks = { pubKeyHash: 0x00, scriptHash: 0x05, wif: 0x80, - dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162 - feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53 + dustThreshold: 546 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162 }, testnet: { magic: 0xd9b4bef9, @@ -25,8 +24,7 @@ var networks = { pubKeyHash: 0x6f, scriptHash: 0xc4, wif: 0xef, - dustThreshold: 546, - feePerKb: 10000 + dustThreshold: 546 }, litecoin: { magic: 0xd9b4bef9, @@ -38,9 +36,7 @@ var networks = { pubKeyHash: 0x30, scriptHash: 0x05, wif: 0xb0, - dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365 - dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53 - feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56 + dustThreshold: 0 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365 }, dogecoin: { messagePrefix: '\x19Dogecoin Signed Message:\n', @@ -51,37 +47,6 @@ var networks = { pubKeyHash: 0x1e, scriptHash: 0x16, wif: 0x9e, - dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160 - dustSoftThreshold: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.h#L62 - feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58 + dustThreshold: 0 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160 } } - -function estimateFee (tx, network) { - var baseFee = network.feePerKb - var byteSize = tx.byteLength() - - var fee = baseFee * Math.ceil(byteSize / 1000) - if (network.dustSoftThreshold === undefined) return fee - - tx.outs.forEach(function (output) { - if (output.value < network.dustSoftThreshold) { - fee += baseFee - } - }) - - return fee -} - -// FIXME: 1.5.3 compatibility patch(s) -function patchEstimateFee (network, tx) { - return estimateFee(tx, network) -} - -for (var networkName in networks) { - var network = networks[networkName] - - network.estimateFee = patchEstimateFee.bind(null, network) -} - -module.exports = networks diff --git a/test/fixtures/network.json b/test/fixtures/network.json index 58737ad..e542aad 100644 --- a/test/fixtures/network.json +++ b/test/fixtures/network.json @@ -1,116 +1,30 @@ -{ - "valid": { - "constants": [ - { - "network": "bitcoin", - "bip32": { - "private": "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi", - "public": "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8" - } - }, - { - "network": "testnet", - "bip32": { - "private": "tprv8ZgxMBicQKsPeDgjzdC36fs6bMjGApWDNLR9erAXMs5skhMv36j9MV5ecvfavji5khqjWaWSFhN3YcCUUdiKH6isR4Pwy3U5y5egddBr16m", - "public": "tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp" - } - }, - { - "network": "litecoin", - "bip32": { - "private": "Ltpv71G8qDifUiNetP6nmxPA5STrUVmv2J9YSmXajv8VsYBUyuPhvN9xCaQrfX2wo5xxJNtEazYCFRUu5FmokYMM79pcqz8pcdo4rNXAFPgyB4k", - "public": "Ltub2SSUS19CirucWFod2ZsYA2J4v4U76YiCXHdcQttnoiy5aGanFHCPDBX7utfG6f95u1cUbZJNafmvzNCzZZJTw1EmyFoL8u1gJbGM8ipu491" - } - }, - { - "network": "dogecoin", - "bip32": { - "private": "dgpv51eADS3spNJh9Gjth94XcPwAczvQaDJs9rqx11kvxKs6r3Ek8AgERHhjLs6mzXQFHRzQqGwqdeoDkZmr8jQMBfi43b7sT3sx3cCSk5fGeUR", - "public": "dgub8kXBZ7ymNWy2S8Q3jNgVjFUm5ZJ3QLLaSTdAA89ukSv7Q6MSXwE14b7Nv6eDpE9JJXinTKc8LeLVu19uDPrm5uJuhpKNzV2kAgncwo6bNpP" - } - } - ], - "estimateFee": [ - { - "description": "when txSize < 1kb", - "network": "bitcoin", - "txSize": 1, - "fee": 10000 - }, - { - "description": "when txSize >= 1kb", - "network": "bitcoin", - "txSize": 1000, - "fee": 10000 - }, - { - "description": "rounding", - "network": "bitcoin", - "txSize": 2800, - "fee": 30000 - }, - { - "description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used", - "network": "dogecoin", - "txSize": 1000, - "outputs": [ - { - "value": 100000000 - } - ], - "fee": 100000000 - }, - { - "description": "when not every outputs.value > DUST_SOFT_LIMIT", - "network": "dogecoin", - "txSize": 1000, - "outputs": [ - { - "value": 99999999 - }, - { - "value": 99999999 - } - ], - "fee": 300000000 - }, - { - "description": "rounding", - "network": "dogecoin", - "txSize": 2800, - "fee": 300000000 - }, - { - "description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used", - "network": "litecoin", - "txSize": 1000, - "outputs": [ - { - "value": 100000 - } - ], - "fee": 100000 - }, - { - "description": "when not every outputs.value > DUST_SOFT_LIMIT", - "network": "litecoin", - "txSize": 1000, - "outputs": [ - { - "value": 99999 - }, - { - "value": 99999 - } - ], - "fee": 300000 - }, - { - "description": "rounding", - "network": "litecoin", - "txSize": 2800, - "fee": 300000 - } - ] +[ + { + "network": "bitcoin", + "bip32": { + "private": "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi", + "public": "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8" + } + }, + { + "network": "testnet", + "bip32": { + "private": "tprv8ZgxMBicQKsPeDgjzdC36fs6bMjGApWDNLR9erAXMs5skhMv36j9MV5ecvfavji5khqjWaWSFhN3YcCUUdiKH6isR4Pwy3U5y5egddBr16m", + "public": "tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp" + } + }, + { + "network": "litecoin", + "bip32": { + "private": "Ltpv71G8qDifUiNetP6nmxPA5STrUVmv2J9YSmXajv8VsYBUyuPhvN9xCaQrfX2wo5xxJNtEazYCFRUu5FmokYMM79pcqz8pcdo4rNXAFPgyB4k", + "public": "Ltub2SSUS19CirucWFod2ZsYA2J4v4U76YiCXHdcQttnoiy5aGanFHCPDBX7utfG6f95u1cUbZJNafmvzNCzZZJTw1EmyFoL8u1gJbGM8ipu491" + } + }, + { + "network": "dogecoin", + "bip32": { + "private": "dgpv51eADS3spNJh9Gjth94XcPwAczvQaDJs9rqx11kvxKs6r3Ek8AgERHhjLs6mzXQFHRzQqGwqdeoDkZmr8jQMBfi43b7sT3sx3cCSk5fGeUR", + "public": "dgub8kXBZ7ymNWy2S8Q3jNgVjFUm5ZJ3QLLaSTdAA89ukSv7Q6MSXwE14b7Nv6eDpE9JJXinTKc8LeLVu19uDPrm5uJuhpKNzV2kAgncwo6bNpP" + } } -} +] diff --git a/test/networks.js b/test/networks.js index ead69d8..3c9762d 100644 --- a/test/networks.js +++ b/test/networks.js @@ -1,52 +1,21 @@ -/* global describe, it, before, after */ +/* global describe, it */ var assert = require('assert') var networks = require('../src/networks') -var sinon = require('sinon') var HDNode = require('../src/hdnode') -var Transaction = require('../src/transaction') var fixtures = require('./fixtures/network') describe('networks', function () { - var txByteLength - before(function () { - txByteLength = sinon.stub(Transaction.prototype, 'byteLength') - }) - - after(function () { - Transaction.prototype.byteLength.restore() - }) - - describe('constants', function () { - fixtures.valid.constants.forEach(function (f) { - var network = networks[f.network] - - Object.keys(f.bip32).forEach(function (name) { - var extb58 = f.bip32[name] - - it('resolves ' + extb58 + ' to ' + f.network, function () { - assert.equal(HDNode.fromBase58(extb58).network, network) - }) - }) - }) - }) - - describe('estimateFee', function () { - fixtures.valid.estimateFee.forEach(function (f) { - describe('(' + f.network + ')', function () { - var network = networks[f.network] - - it('calculates the fee correctly for ' + f.description, function () { - txByteLength.returns(f.txSize) + fixtures.forEach(function (f) { + var network = networks[f.network] - var estimateFee = network.estimateFee - var tx = new Transaction() - tx.outs = f.outputs || [] + Object.keys(f.bip32).forEach(function (name) { + var extb58 = f.bip32[name] - assert.equal(estimateFee(tx), f.fee) - }) + it(extb58 + ' auto-detects ' + f.network, function () { + assert.equal(HDNode.fromBase58(extb58).network, network) }) }) })