Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ var ecurve = require('ecurve')
var ecparams = ecurve.getCurveByName('secp256k1')

function magicHash (message, network) {
var magicPrefix = new Buffer(network.magicPrefix)
var messagePrefix = new Buffer(network.messagePrefix)
var messageBuffer = new Buffer(message)
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)

var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer])
var buffer = Buffer.concat([messagePrefix, lengthBuffer, messageBuffer])
return crypto.hash256(buffer)
}

Expand Down
84 changes: 43 additions & 41 deletions src/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

var networks = {
bitcoin: {
magicPrefix: '\x18Bitcoin Signed Message:\n',
magic: 0xd9b4bef9,
messagePrefix: '\x18Bitcoin Signed Message:\n',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
Expand All @@ -12,11 +13,11 @@ var networks = {
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
estimateFee: estimateFee('bitcoin')
feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
},
testnet: {
magicPrefix: '\x18Bitcoin Signed Message:\n',
magic: 0xd9b4bef9,
messagePrefix: '\x18Bitcoin Signed Message:\n',
bip32: {
public: 0x043587cf,
private: 0x04358394
Expand All @@ -25,11 +26,11 @@ var networks = {
scriptHash: 0xc4,
wif: 0xef,
dustThreshold: 546,
feePerKb: 10000,
estimateFee: estimateFee('testnet')
feePerKb: 10000
},
litecoin: {
magicPrefix: '\x19Litecoin Signed Message:\n',
magic: 0xd9b4bef9,
messagePrefix: '\x19Litecoin Signed Message:\n',
bip32: {
public: 0x019da462,
private: 0x019d9cfe
Expand All @@ -39,11 +40,10 @@ var networks = {
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
estimateFee: estimateFee('litecoin')
feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
},
dogecoin: {
magicPrefix: '\x19Dogecoin Signed Message:\n',
messagePrefix: '\x19Dogecoin Signed Message:\n',
bip32: {
public: 0x02facafd,
private: 0x02fac398
Expand All @@ -53,11 +53,10 @@ var networks = {
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
estimateFee: estimateFee('dogecoin')
feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
},
viacoin: {
magicPrefix: '\x18Viacoin Signed Message:\n',
messagePrefix: '\x18Viacoin Signed Message:\n',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
Expand All @@ -67,11 +66,10 @@ var networks = {
wif: 0xc7,
dustThreshold: 560,
dustSoftThreshold: 100000,
feePerKb: 100000, //
estimateFee: estimateFee('viacoin')
feePerKb: 100000
},
viacointestnet: {
magicPrefix: '\x18Viacoin Signed Message:\n',
messagePrefix: '\x18Viacoin Signed Message:\n',
bip32: {
public: 0x043587cf,
private: 0x04358394
Expand All @@ -81,11 +79,10 @@ var networks = {
wif: 0xff,
dustThreshold: 560,
dustSoftThreshold: 100000,
feePerKb: 100000,
estimateFee: estimateFee('viacointestnet')
feePerKb: 100000
},
gamerscoin: {
magicPrefix: '\x19Gamerscoin Signed Message:\n',
messagePrefix: '\x19Gamerscoin Signed Message:\n',
bip32: {
public: 0x019da462,
private: 0x019d9cfe
Expand All @@ -95,11 +92,10 @@ var networks = {
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
estimateFee: estimateFee('gamerscoin')
feePerKb: 100000 // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
},
jumbucks: {
magicPrefix: '\x19Jumbucks Signed Message:\n',
messagePrefix: '\x19Jumbucks Signed Message:\n',
bip32: {
public: 0x037a689a,
private: 0x037a6460
Expand All @@ -109,11 +105,10 @@ var networks = {
wif: 0xab,
dustThreshold: 0,
dustSoftThreshold: 10000,
feePerKb: 10000,
estimateFee: estimateFee('jumbucks')
feePerKb: 10000
},
zetacoin: {
magicPrefix: '\x18Zetacoin Signed Message:\n',
messagePrefix: '\x18Zetacoin Signed Message:\n',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
Expand All @@ -122,28 +117,35 @@ var networks = {
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
estimateFee: estimateFee('zetacoin')
feePerKb: 10000 // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
}
}

function estimateFee (type) {
return function (tx) {
var network = networks[type]
var baseFee = network.feePerKb
var byteSize = tx.toBuffer().length
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
var fee = baseFee * Math.ceil(byteSize / 1000)
if (network.dustSoftThreshold === undefined) return fee

tx.outs.forEach(function (e) {
if (e.value < network.dustSoftThreshold) {
fee += baseFee
}
})
tx.outs.forEach(function (output) {
if (output.value < network.dustSoftThreshold) {
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
30 changes: 17 additions & 13 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
}) - 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 () {
var newTx = new Transaction()
newTx.version = this.version
Expand Down Expand Up @@ -215,19 +231,7 @@ Transaction.prototype.getId = function () {
}

Transaction.prototype.toBuffer = function () {
function scriptSize (script) {
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 buffer = new Buffer(this.byteLength())

var offset = 0
function writeSlice (slice) {
Expand Down
9 changes: 4 additions & 5 deletions test/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ var Transaction = require('../src/transaction')
var fixtures = require('./fixtures/network')

describe('networks', function () {
var txToBuffer
var txByteLength
before(function () {
txToBuffer = sinon.stub(Transaction.prototype, 'toBuffer')
txByteLength = sinon.stub(Transaction.prototype, 'byteLength')
})

after(function () {
Transaction.prototype.toBuffer.restore()
Transaction.prototype.byteLength.restore()
})

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

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

var estimateFee = network.estimateFee
var tx = new Transaction()
Expand Down