Skip to content

Commit 01036ee

Browse files
committed
Merge pull request bitcoinjs#380 from bitcoinjs/magic
Magic constants
2 parents e939aa0 + 063b036 commit 01036ee

File tree

4 files changed

+66
-61
lines changed

4 files changed

+66
-61
lines changed

src/message.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ var ecurve = require('ecurve')
1111
var ecparams = ecurve.getCurveByName('secp256k1')
1212

1313
function magicHash (message, network) {
14-
var magicPrefix = new Buffer(network.magicPrefix)
14+
var messagePrefix = new Buffer(network.messagePrefix)
1515
var messageBuffer = new Buffer(message)
1616
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)
1717

18-
var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer])
18+
var buffer = Buffer.concat([messagePrefix, lengthBuffer, messageBuffer])
1919
return crypto.hash256(buffer)
2020
}
2121

src/networks.js

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
var networks = {
55
bitcoin: {
6-
magicPrefix: '\x18Bitcoin Signed Message:\n',
6+
magic: 0xd9b4bef9,
7+
messagePrefix: '\x18Bitcoin Signed Message:\n',
78
bip32: {
89
public: 0x0488b21e,
910
private: 0x0488ade4
@@ -12,11 +13,11 @@ var networks = {
1213
scriptHash: 0x05,
1314
wif: 0x80,
1415
dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162
15-
feePerKb: 10000, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
16-
estimateFee: estimateFee('bitcoin')
16+
feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
1717
},
1818
testnet: {
19-
magicPrefix: '\x18Bitcoin Signed Message:\n',
19+
magic: 0xd9b4bef9,
20+
messagePrefix: '\x18Bitcoin Signed Message:\n',
2021
bip32: {
2122
public: 0x043587cf,
2223
private: 0x04358394
@@ -25,11 +26,11 @@ var networks = {
2526
scriptHash: 0xc4,
2627
wif: 0xef,
2728
dustThreshold: 546,
28-
feePerKb: 10000,
29-
estimateFee: estimateFee('testnet')
29+
feePerKb: 10000
3030
},
3131
litecoin: {
32-
magicPrefix: '\x19Litecoin Signed Message:\n',
32+
magic: 0xd9b4bef9,
33+
messagePrefix: '\x19Litecoin Signed Message:\n',
3334
bip32: {
3435
public: 0x019da462,
3536
private: 0x019d9cfe
@@ -39,11 +40,10 @@ var networks = {
3940
wif: 0xb0,
4041
dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365
4142
dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53
42-
feePerKb: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
43-
estimateFee: estimateFee('litecoin')
43+
feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
4444
},
4545
dogecoin: {
46-
magicPrefix: '\x19Dogecoin Signed Message:\n',
46+
messagePrefix: '\x19Dogecoin Signed Message:\n',
4747
bip32: {
4848
public: 0x02facafd,
4949
private: 0x02fac398
@@ -53,11 +53,10 @@ var networks = {
5353
wif: 0x9e,
5454
dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160
5555
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-
estimateFee: estimateFee('dogecoin')
56+
feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
5857
},
5958
viacoin: {
60-
magicPrefix: '\x18Viacoin Signed Message:\n',
59+
messagePrefix: '\x18Viacoin Signed Message:\n',
6160
bip32: {
6261
public: 0x0488b21e,
6362
private: 0x0488ade4
@@ -67,11 +66,10 @@ var networks = {
6766
wif: 0xc7,
6867
dustThreshold: 560,
6968
dustSoftThreshold: 100000,
70-
feePerKb: 100000, //
71-
estimateFee: estimateFee('viacoin')
69+
feePerKb: 100000
7270
},
7371
viacointestnet: {
74-
magicPrefix: '\x18Viacoin Signed Message:\n',
72+
messagePrefix: '\x18Viacoin Signed Message:\n',
7573
bip32: {
7674
public: 0x043587cf,
7775
private: 0x04358394
@@ -81,11 +79,10 @@ var networks = {
8179
wif: 0xff,
8280
dustThreshold: 560,
8381
dustSoftThreshold: 100000,
84-
feePerKb: 100000,
85-
estimateFee: estimateFee('viacointestnet')
82+
feePerKb: 100000
8683
},
8784
gamerscoin: {
88-
magicPrefix: '\x19Gamerscoin Signed Message:\n',
85+
messagePrefix: '\x19Gamerscoin Signed Message:\n',
8986
bip32: {
9087
public: 0x019da462,
9188
private: 0x019d9cfe
@@ -95,11 +92,10 @@ var networks = {
9592
wif: 0xA6,
9693
dustThreshold: 0, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L358-L363
9794
dustSoftThreshold: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L51
98-
feePerKb: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
99-
estimateFee: estimateFee('gamerscoin')
95+
feePerKb: 100000 // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
10096
},
10197
jumbucks: {
102-
magicPrefix: '\x19Jumbucks Signed Message:\n',
98+
messagePrefix: '\x19Jumbucks Signed Message:\n',
10399
bip32: {
104100
public: 0x037a689a,
105101
private: 0x037a6460
@@ -109,11 +105,10 @@ var networks = {
109105
wif: 0xab,
110106
dustThreshold: 0,
111107
dustSoftThreshold: 10000,
112-
feePerKb: 10000,
113-
estimateFee: estimateFee('jumbucks')
108+
feePerKb: 10000
114109
},
115110
zetacoin: {
116-
magicPrefix: '\x18Zetacoin Signed Message:\n',
111+
messagePrefix: '\x18Zetacoin Signed Message:\n',
117112
bip32: {
118113
public: 0x0488b21e,
119114
private: 0x0488ade4
@@ -122,28 +117,35 @@ var networks = {
122117
scriptHash: 0x09,
123118
wif: 0xe0,
124119
dustThreshold: 546, // https://github.com/zetacoin/zetacoin/blob/master/src/core.h#L159
125-
feePerKb: 10000, // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
126-
estimateFee: estimateFee('zetacoin')
120+
feePerKb: 10000 // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
127121
}
128122
}
129123

130-
function estimateFee (type) {
131-
return function (tx) {
132-
var network = networks[type]
133-
var baseFee = network.feePerKb
134-
var byteSize = tx.toBuffer().length
124+
function estimateFee (tx, network) {
125+
var baseFee = network.feePerKb
126+
var byteSize = tx.byteLength()
135127

136-
var fee = baseFee * Math.ceil(byteSize / 1000)
137-
if (network.dustSoftThreshold === undefined) return fee
128+
var fee = baseFee * Math.ceil(byteSize / 1000)
129+
if (network.dustSoftThreshold === undefined) return fee
138130

139-
tx.outs.forEach(function (e) {
140-
if (e.value < network.dustSoftThreshold) {
141-
fee += baseFee
142-
}
143-
})
131+
tx.outs.forEach(function (output) {
132+
if (output.value < network.dustSoftThreshold) {
133+
fee += baseFee
134+
}
135+
})
144136

145-
return fee
146-
}
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)
147149
}
148150

149151
module.exports = networks

src/transaction.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
137137
}) - 1)
138138
}
139139

140+
Transaction.prototype.byteLength = function () {
141+
function scriptSize (script) {
142+
var length = script.buffer.length
143+
144+
return bufferutils.varIntSize(length) + length
145+
}
146+
147+
return (
148+
8 +
149+
bufferutils.varIntSize(this.ins.length) +
150+
bufferutils.varIntSize(this.outs.length) +
151+
this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) +
152+
this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0)
153+
)
154+
}
155+
140156
Transaction.prototype.clone = function () {
141157
var newTx = new Transaction()
142158
newTx.version = this.version
@@ -215,19 +231,7 @@ Transaction.prototype.getId = function () {
215231
}
216232

217233
Transaction.prototype.toBuffer = function () {
218-
function scriptSize (script) {
219-
var length = script.buffer.length
220-
221-
return bufferutils.varIntSize(length) + length
222-
}
223-
224-
var buffer = new Buffer(
225-
8 +
226-
bufferutils.varIntSize(this.ins.length) +
227-
bufferutils.varIntSize(this.outs.length) +
228-
this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) +
229-
this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0)
230-
)
234+
var buffer = new Buffer(this.byteLength())
231235

232236
var offset = 0
233237
function writeSlice (slice) {

test/networks.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ var Transaction = require('../src/transaction')
1010
var fixtures = require('./fixtures/network')
1111

1212
describe('networks', function () {
13-
var txToBuffer
13+
var txByteLength
1414
before(function () {
15-
txToBuffer = sinon.stub(Transaction.prototype, 'toBuffer')
15+
txByteLength = sinon.stub(Transaction.prototype, 'byteLength')
1616
})
1717

1818
after(function () {
19-
Transaction.prototype.toBuffer.restore()
19+
Transaction.prototype.byteLength.restore()
2020
})
2121

2222
describe('constants', function () {
@@ -39,8 +39,7 @@ describe('networks', function () {
3939
var network = networks[f.network]
4040

4141
it('calculates the fee correctly for ' + f.description, function () {
42-
var buffer = new Buffer(f.txSize)
43-
txToBuffer.returns(buffer)
42+
txByteLength.returns(f.txSize)
4443

4544
var estimateFee = network.estimateFee
4645
var tx = new Transaction()

0 commit comments

Comments
 (0)