Skip to content

Commit 03f3ccf

Browse files
committed
Merge pull request bitcoinjs#381 from bitcoinjs/networkexhaust
Remove exhaustive network functionality
2 parents 01036ee + bd464d7 commit 03f3ccf

File tree

3 files changed

+41
-264
lines changed

3 files changed

+41
-264
lines changed

src/networks.js

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://en.bitcoin.it/wiki/List_of_address_prefixes
22
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
33

4-
var networks = {
4+
module.exports = {
55
bitcoin: {
66
magic: 0xd9b4bef9,
77
messagePrefix: '\x18Bitcoin Signed Message:\n',
@@ -12,8 +12,7 @@ var networks = {
1212
pubKeyHash: 0x00,
1313
scriptHash: 0x05,
1414
wif: 0x80,
15-
dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162
16-
feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
15+
dustThreshold: 546 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162
1716
},
1817
testnet: {
1918
magic: 0xd9b4bef9,
@@ -25,8 +24,7 @@ var networks = {
2524
pubKeyHash: 0x6f,
2625
scriptHash: 0xc4,
2726
wif: 0xef,
28-
dustThreshold: 546,
29-
feePerKb: 10000
27+
dustThreshold: 546
3028
},
3129
litecoin: {
3230
magic: 0xd9b4bef9,
@@ -38,9 +36,7 @@ var networks = {
3836
pubKeyHash: 0x30,
3937
scriptHash: 0x05,
4038
wif: 0xb0,
41-
dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365
42-
dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53
43-
feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
39+
dustThreshold: 0 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365
4440
},
4541
dogecoin: {
4642
messagePrefix: '\x19Dogecoin Signed Message:\n',
@@ -51,101 +47,6 @@ var networks = {
5147
pubKeyHash: 0x1e,
5248
scriptHash: 0x16,
5349
wif: 0x9e,
54-
dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160
55-
dustSoftThreshold: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.h#L62
56-
feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
57-
},
58-
viacoin: {
59-
messagePrefix: '\x18Viacoin Signed Message:\n',
60-
bip32: {
61-
public: 0x0488b21e,
62-
private: 0x0488ade4
63-
},
64-
pubKeyHash: 0x47,
65-
scriptHash: 0x21,
66-
wif: 0xc7,
67-
dustThreshold: 560,
68-
dustSoftThreshold: 100000,
69-
feePerKb: 100000
70-
},
71-
viacointestnet: {
72-
messagePrefix: '\x18Viacoin Signed Message:\n',
73-
bip32: {
74-
public: 0x043587cf,
75-
private: 0x04358394
76-
},
77-
pubKeyHash: 0x7f,
78-
scriptHash: 0xc4,
79-
wif: 0xff,
80-
dustThreshold: 560,
81-
dustSoftThreshold: 100000,
82-
feePerKb: 100000
83-
},
84-
gamerscoin: {
85-
messagePrefix: '\x19Gamerscoin Signed Message:\n',
86-
bip32: {
87-
public: 0x019da462,
88-
private: 0x019d9cfe
89-
},
90-
pubKeyHash: 0x26,
91-
scriptHash: 0x05,
92-
wif: 0xA6,
93-
dustThreshold: 0, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L358-L363
94-
dustSoftThreshold: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L51
95-
feePerKb: 100000 // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
96-
},
97-
jumbucks: {
98-
messagePrefix: '\x19Jumbucks Signed Message:\n',
99-
bip32: {
100-
public: 0x037a689a,
101-
private: 0x037a6460
102-
},
103-
pubKeyHash: 0x2b,
104-
scriptHash: 0x05,
105-
wif: 0xab,
106-
dustThreshold: 0,
107-
dustSoftThreshold: 10000,
108-
feePerKb: 10000
109-
},
110-
zetacoin: {
111-
messagePrefix: '\x18Zetacoin Signed Message:\n',
112-
bip32: {
113-
public: 0x0488b21e,
114-
private: 0x0488ade4
115-
},
116-
pubKeyHash: 0x50,
117-
scriptHash: 0x09,
118-
wif: 0xe0,
119-
dustThreshold: 546, // https://github.com/zetacoin/zetacoin/blob/master/src/core.h#L159
120-
feePerKb: 10000 // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
50+
dustThreshold: 0 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160
12151
}
12252
}
123-
124-
function estimateFee (tx, network) {
125-
var baseFee = network.feePerKb
126-
var byteSize = tx.byteLength()
127-
128-
var fee = baseFee * Math.ceil(byteSize / 1000)
129-
if (network.dustSoftThreshold === undefined) return fee
130-
131-
tx.outs.forEach(function (output) {
132-
if (output.value < network.dustSoftThreshold) {
133-
fee += baseFee
134-
}
135-
})
136-
137-
return fee
138-
}
139-
140-
// FIXME: 1.5.3 compatibility patch(s)
141-
function patchEstimateFee (network, tx) {
142-
return estimateFee(tx, network)
143-
}
144-
145-
for (var networkName in networks) {
146-
var network = networks[networkName]
147-
148-
network.estimateFee = patchEstimateFee.bind(null, network)
149-
}
150-
151-
module.exports = networks

test/fixtures/network.json

Lines changed: 29 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,30 @@
1-
{
2-
"valid": {
3-
"constants": [
4-
{
5-
"network": "bitcoin",
6-
"bip32": {
7-
"private": "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
8-
"public": "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
9-
}
10-
},
11-
{
12-
"network": "testnet",
13-
"bip32": {
14-
"private": "tprv8ZgxMBicQKsPeDgjzdC36fs6bMjGApWDNLR9erAXMs5skhMv36j9MV5ecvfavji5khqjWaWSFhN3YcCUUdiKH6isR4Pwy3U5y5egddBr16m",
15-
"public": "tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp"
16-
}
17-
},
18-
{
19-
"network": "litecoin",
20-
"bip32": {
21-
"private": "Ltpv71G8qDifUiNetP6nmxPA5STrUVmv2J9YSmXajv8VsYBUyuPhvN9xCaQrfX2wo5xxJNtEazYCFRUu5FmokYMM79pcqz8pcdo4rNXAFPgyB4k",
22-
"public": "Ltub2SSUS19CirucWFod2ZsYA2J4v4U76YiCXHdcQttnoiy5aGanFHCPDBX7utfG6f95u1cUbZJNafmvzNCzZZJTw1EmyFoL8u1gJbGM8ipu491"
23-
}
24-
},
25-
{
26-
"network": "dogecoin",
27-
"bip32": {
28-
"private": "dgpv51eADS3spNJh9Gjth94XcPwAczvQaDJs9rqx11kvxKs6r3Ek8AgERHhjLs6mzXQFHRzQqGwqdeoDkZmr8jQMBfi43b7sT3sx3cCSk5fGeUR",
29-
"public": "dgub8kXBZ7ymNWy2S8Q3jNgVjFUm5ZJ3QLLaSTdAA89ukSv7Q6MSXwE14b7Nv6eDpE9JJXinTKc8LeLVu19uDPrm5uJuhpKNzV2kAgncwo6bNpP"
30-
}
31-
},
32-
{
33-
"network": "jumbucks",
34-
"bip32": {
35-
"private": "jprv5eCacBgN4Bz4zYxgVQ7RDt1a3eREhEaj8KjAcJ7YwogxGo2rmBF5kvAQS53JwZpo5wnUmJ9Q7kB6b2gQ1MzC6yaTc188hr6hXZ5t8Ruria1",
36-
"public": "jpub1sBw1hDFtZYND339bReRb1xJbgFj6hJaVYemQgXAW9Dw9bN1JiZLJiUtHLgcTTEs1UgRGFAYm3XQPYsYJbpqj1aYPhrMsNcJHfgdAhvFZBB"
37-
}
38-
}
39-
],
40-
"estimateFee": [
41-
{
42-
"description": "when txSize < 1kb",
43-
"network": "bitcoin",
44-
"txSize": 1,
45-
"fee": 10000
46-
},
47-
{
48-
"description": "when txSize >= 1kb",
49-
"network": "bitcoin",
50-
"txSize": 1000,
51-
"fee": 10000
52-
},
53-
{
54-
"description": "rounding",
55-
"network": "bitcoin",
56-
"txSize": 2800,
57-
"fee": 30000
58-
},
59-
{
60-
"description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used",
61-
"network": "dogecoin",
62-
"txSize": 1000,
63-
"outputs": [
64-
{
65-
"value": 100000000
66-
}
67-
],
68-
"fee": 100000000
69-
},
70-
{
71-
"description": "when not every outputs.value > DUST_SOFT_LIMIT",
72-
"network": "dogecoin",
73-
"txSize": 1000,
74-
"outputs": [
75-
{
76-
"value": 99999999
77-
},
78-
{
79-
"value": 99999999
80-
}
81-
],
82-
"fee": 300000000
83-
},
84-
{
85-
"description": "rounding",
86-
"network": "dogecoin",
87-
"txSize": 2800,
88-
"fee": 300000000
89-
},
90-
{
91-
"description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used",
92-
"network": "litecoin",
93-
"txSize": 1000,
94-
"outputs": [
95-
{
96-
"value": 100000
97-
}
98-
],
99-
"fee": 100000
100-
},
101-
{
102-
"description": "when not every outputs.value > DUST_SOFT_LIMIT",
103-
"network": "litecoin",
104-
"txSize": 1000,
105-
"outputs": [
106-
{
107-
"value": 99999
108-
},
109-
{
110-
"value": 99999
111-
}
112-
],
113-
"fee": 300000
114-
},
115-
{
116-
"description": "rounding",
117-
"network": "litecoin",
118-
"txSize": 2800,
119-
"fee": 300000
120-
}
121-
]
1+
[
2+
{
3+
"network": "bitcoin",
4+
"bip32": {
5+
"private": "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
6+
"public": "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
7+
}
8+
},
9+
{
10+
"network": "testnet",
11+
"bip32": {
12+
"private": "tprv8ZgxMBicQKsPeDgjzdC36fs6bMjGApWDNLR9erAXMs5skhMv36j9MV5ecvfavji5khqjWaWSFhN3YcCUUdiKH6isR4Pwy3U5y5egddBr16m",
13+
"public": "tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp"
14+
}
15+
},
16+
{
17+
"network": "litecoin",
18+
"bip32": {
19+
"private": "Ltpv71G8qDifUiNetP6nmxPA5STrUVmv2J9YSmXajv8VsYBUyuPhvN9xCaQrfX2wo5xxJNtEazYCFRUu5FmokYMM79pcqz8pcdo4rNXAFPgyB4k",
20+
"public": "Ltub2SSUS19CirucWFod2ZsYA2J4v4U76YiCXHdcQttnoiy5aGanFHCPDBX7utfG6f95u1cUbZJNafmvzNCzZZJTw1EmyFoL8u1gJbGM8ipu491"
21+
}
22+
},
23+
{
24+
"network": "dogecoin",
25+
"bip32": {
26+
"private": "dgpv51eADS3spNJh9Gjth94XcPwAczvQaDJs9rqx11kvxKs6r3Ek8AgERHhjLs6mzXQFHRzQqGwqdeoDkZmr8jQMBfi43b7sT3sx3cCSk5fGeUR",
27+
"public": "dgub8kXBZ7ymNWy2S8Q3jNgVjFUm5ZJ3QLLaSTdAA89ukSv7Q6MSXwE14b7Nv6eDpE9JJXinTKc8LeLVu19uDPrm5uJuhpKNzV2kAgncwo6bNpP"
28+
}
12229
}
123-
}
30+
]

test/networks.js

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,21 @@
1-
/* global describe, it, before, after */
1+
/* global describe, it */
22

33
var assert = require('assert')
44
var networks = require('../src/networks')
5-
var sinon = require('sinon')
65

76
var HDNode = require('../src/hdnode')
8-
var Transaction = require('../src/transaction')
97

108
var fixtures = require('./fixtures/network')
119

1210
describe('networks', function () {
13-
var txByteLength
14-
before(function () {
15-
txByteLength = sinon.stub(Transaction.prototype, 'byteLength')
16-
})
17-
18-
after(function () {
19-
Transaction.prototype.byteLength.restore()
20-
})
21-
22-
describe('constants', function () {
23-
fixtures.valid.constants.forEach(function (f) {
24-
var network = networks[f.network]
25-
26-
Object.keys(f.bip32).forEach(function (name) {
27-
var extb58 = f.bip32[name]
28-
29-
it('resolves ' + extb58 + ' to ' + f.network, function () {
30-
assert.equal(HDNode.fromBase58(extb58).network, network)
31-
})
32-
})
33-
})
34-
})
35-
36-
describe('estimateFee', function () {
37-
fixtures.valid.estimateFee.forEach(function (f) {
38-
describe('(' + f.network + ')', function () {
39-
var network = networks[f.network]
40-
41-
it('calculates the fee correctly for ' + f.description, function () {
42-
txByteLength.returns(f.txSize)
11+
fixtures.forEach(function (f) {
12+
var network = networks[f.network]
4313

44-
var estimateFee = network.estimateFee
45-
var tx = new Transaction()
46-
tx.outs = f.outputs || []
14+
Object.keys(f.bip32).forEach(function (name) {
15+
var extb58 = f.bip32[name]
4716

48-
assert.equal(estimateFee(tx), f.fee)
49-
})
17+
it(extb58 + ' auto-detects ' + f.network, function () {
18+
assert.equal(HDNode.fromBase58(extb58).network, network)
5019
})
5120
})
5221
})

0 commit comments

Comments
 (0)