Merge pull request #380 from bitcoinjs/magic

Magic constants
This commit is contained in:
Daniel Cousens 2015-03-17 08:01:49 +11:00
commit 01036ee294
4 changed files with 66 additions and 61 deletions

View file

@ -11,11 +11,11 @@ var ecurve = require('ecurve')
var ecparams = ecurve.getCurveByName('secp256k1') var ecparams = ecurve.getCurveByName('secp256k1')
function magicHash (message, network) { function magicHash (message, network) {
var magicPrefix = new Buffer(network.magicPrefix) var messagePrefix = new Buffer(network.messagePrefix)
var messageBuffer = new Buffer(message) var messageBuffer = new Buffer(message)
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length) var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)
var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer]) var buffer = Buffer.concat([messagePrefix, lengthBuffer, messageBuffer])
return crypto.hash256(buffer) return crypto.hash256(buffer)
} }

View file

@ -3,7 +3,8 @@
var networks = { var networks = {
bitcoin: { bitcoin: {
magicPrefix: '\x18Bitcoin Signed Message:\n', magic: 0xd9b4bef9,
messagePrefix: '\x18Bitcoin Signed Message:\n',
bip32: { bip32: {
public: 0x0488b21e, public: 0x0488b21e,
private: 0x0488ade4 private: 0x0488ade4
@ -12,11 +13,11 @@ var networks = {
scriptHash: 0x05, scriptHash: 0x05,
wif: 0x80, wif: 0x80,
dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162 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 feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
estimateFee: estimateFee('bitcoin')
}, },
testnet: { testnet: {
magicPrefix: '\x18Bitcoin Signed Message:\n', magic: 0xd9b4bef9,
messagePrefix: '\x18Bitcoin Signed Message:\n',
bip32: { bip32: {
public: 0x043587cf, public: 0x043587cf,
private: 0x04358394 private: 0x04358394
@ -25,11 +26,11 @@ var networks = {
scriptHash: 0xc4, scriptHash: 0xc4,
wif: 0xef, wif: 0xef,
dustThreshold: 546, dustThreshold: 546,
feePerKb: 10000, feePerKb: 10000
estimateFee: estimateFee('testnet')
}, },
litecoin: { litecoin: {
magicPrefix: '\x19Litecoin Signed Message:\n', magic: 0xd9b4bef9,
messagePrefix: '\x19Litecoin Signed Message:\n',
bip32: { bip32: {
public: 0x019da462, public: 0x019da462,
private: 0x019d9cfe private: 0x019d9cfe
@ -39,11 +40,10 @@ var networks = {
wif: 0xb0, wif: 0xb0,
dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365 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 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 feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
estimateFee: estimateFee('litecoin')
}, },
dogecoin: { dogecoin: {
magicPrefix: '\x19Dogecoin Signed Message:\n', messagePrefix: '\x19Dogecoin Signed Message:\n',
bip32: { bip32: {
public: 0x02facafd, public: 0x02facafd,
private: 0x02fac398 private: 0x02fac398
@ -53,11 +53,10 @@ var networks = {
wif: 0x9e, wif: 0x9e,
dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160 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 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 feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
estimateFee: estimateFee('dogecoin')
}, },
viacoin: { viacoin: {
magicPrefix: '\x18Viacoin Signed Message:\n', messagePrefix: '\x18Viacoin Signed Message:\n',
bip32: { bip32: {
public: 0x0488b21e, public: 0x0488b21e,
private: 0x0488ade4 private: 0x0488ade4
@ -67,11 +66,10 @@ var networks = {
wif: 0xc7, wif: 0xc7,
dustThreshold: 560, dustThreshold: 560,
dustSoftThreshold: 100000, dustSoftThreshold: 100000,
feePerKb: 100000, // feePerKb: 100000
estimateFee: estimateFee('viacoin')
}, },
viacointestnet: { viacointestnet: {
magicPrefix: '\x18Viacoin Signed Message:\n', messagePrefix: '\x18Viacoin Signed Message:\n',
bip32: { bip32: {
public: 0x043587cf, public: 0x043587cf,
private: 0x04358394 private: 0x04358394
@ -81,11 +79,10 @@ var networks = {
wif: 0xff, wif: 0xff,
dustThreshold: 560, dustThreshold: 560,
dustSoftThreshold: 100000, dustSoftThreshold: 100000,
feePerKb: 100000, feePerKb: 100000
estimateFee: estimateFee('viacointestnet')
}, },
gamerscoin: { gamerscoin: {
magicPrefix: '\x19Gamerscoin Signed Message:\n', messagePrefix: '\x19Gamerscoin Signed Message:\n',
bip32: { bip32: {
public: 0x019da462, public: 0x019da462,
private: 0x019d9cfe private: 0x019d9cfe
@ -95,11 +92,10 @@ var networks = {
wif: 0xA6, wif: 0xA6,
dustThreshold: 0, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L358-L363 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 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 feePerKb: 100000 // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
estimateFee: estimateFee('gamerscoin')
}, },
jumbucks: { jumbucks: {
magicPrefix: '\x19Jumbucks Signed Message:\n', messagePrefix: '\x19Jumbucks Signed Message:\n',
bip32: { bip32: {
public: 0x037a689a, public: 0x037a689a,
private: 0x037a6460 private: 0x037a6460
@ -109,11 +105,10 @@ var networks = {
wif: 0xab, wif: 0xab,
dustThreshold: 0, dustThreshold: 0,
dustSoftThreshold: 10000, dustSoftThreshold: 10000,
feePerKb: 10000, feePerKb: 10000
estimateFee: estimateFee('jumbucks')
}, },
zetacoin: { zetacoin: {
magicPrefix: '\x18Zetacoin Signed Message:\n', messagePrefix: '\x18Zetacoin Signed Message:\n',
bip32: { bip32: {
public: 0x0488b21e, public: 0x0488b21e,
private: 0x0488ade4 private: 0x0488ade4
@ -122,28 +117,35 @@ var networks = {
scriptHash: 0x09, scriptHash: 0x09,
wif: 0xe0, wif: 0xe0,
dustThreshold: 546, // https://github.com/zetacoin/zetacoin/blob/master/src/core.h#L159 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 feePerKb: 10000 // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
estimateFee: estimateFee('zetacoin')
} }
} }
function estimateFee (type) { function estimateFee (tx, network) {
return function (tx) {
var network = networks[type]
var baseFee = network.feePerKb var baseFee = network.feePerKb
var byteSize = tx.toBuffer().length var byteSize = tx.byteLength()
var fee = baseFee * Math.ceil(byteSize / 1000) var fee = baseFee * Math.ceil(byteSize / 1000)
if (network.dustSoftThreshold === undefined) return fee if (network.dustSoftThreshold === undefined) return fee
tx.outs.forEach(function (e) { tx.outs.forEach(function (output) {
if (e.value < network.dustSoftThreshold) { if (output.value < network.dustSoftThreshold) {
fee += baseFee fee += baseFee
} }
}) })
return fee 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 module.exports = networks

View file

@ -137,6 +137,22 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
}) - 1) }) - 1)
} }
Transaction.prototype.byteLength = function () {
function scriptSize (script) {
var length = script.buffer.length
return bufferutils.varIntSize(length) + length
}
return (
8 +
bufferutils.varIntSize(this.ins.length) +
bufferutils.varIntSize(this.outs.length) +
this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) +
this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0)
)
}
Transaction.prototype.clone = function () { Transaction.prototype.clone = function () {
var newTx = new Transaction() var newTx = new Transaction()
newTx.version = this.version newTx.version = this.version
@ -215,19 +231,7 @@ Transaction.prototype.getId = function () {
} }
Transaction.prototype.toBuffer = function () { Transaction.prototype.toBuffer = function () {
function scriptSize (script) { var buffer = new Buffer(this.byteLength())
var length = script.buffer.length
return bufferutils.varIntSize(length) + length
}
var buffer = new Buffer(
8 +
bufferutils.varIntSize(this.ins.length) +
bufferutils.varIntSize(this.outs.length) +
this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) +
this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0)
)
var offset = 0 var offset = 0
function writeSlice (slice) { function writeSlice (slice) {

View file

@ -10,13 +10,13 @@ var Transaction = require('../src/transaction')
var fixtures = require('./fixtures/network') var fixtures = require('./fixtures/network')
describe('networks', function () { describe('networks', function () {
var txToBuffer var txByteLength
before(function () { before(function () {
txToBuffer = sinon.stub(Transaction.prototype, 'toBuffer') txByteLength = sinon.stub(Transaction.prototype, 'byteLength')
}) })
after(function () { after(function () {
Transaction.prototype.toBuffer.restore() Transaction.prototype.byteLength.restore()
}) })
describe('constants', function () { describe('constants', function () {
@ -39,8 +39,7 @@ describe('networks', function () {
var network = networks[f.network] var network = networks[f.network]
it('calculates the fee correctly for ' + f.description, function () { it('calculates the fee correctly for ' + f.description, function () {
var buffer = new Buffer(f.txSize) txByteLength.returns(f.txSize)
txToBuffer.returns(buffer)
var estimateFee = network.estimateFee var estimateFee = network.estimateFee
var tx = new Transaction() var tx = new Transaction()