|
@ -1,4 +1,5 @@ |
|
|
var assert = require('assert') |
|
|
var assert = require('assert') |
|
|
|
|
|
var bufferutils = require('../src/bufferutils') |
|
|
var crypto = require('../src/crypto') |
|
|
var crypto = require('../src/crypto') |
|
|
var networks = require('../src/networks') |
|
|
var networks = require('../src/networks') |
|
|
var sinon = require('sinon') |
|
|
var sinon = require('sinon') |
|
@ -201,26 +202,25 @@ describe('Wallet', function() { |
|
|
beforeEach(function() { |
|
|
beforeEach(function() { |
|
|
utxo = { |
|
|
utxo = { |
|
|
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv", |
|
|
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv", |
|
|
"hash": fakeTxId(6), |
|
|
"confirmations": 1, |
|
|
"index": 0, |
|
|
"index": 0, |
|
|
"pending": true, |
|
|
"txId": fakeTxId(6), |
|
|
"value": 20000 |
|
|
"value": 20000, |
|
|
|
|
|
"pending": false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
expectedOutputKey = utxo.hash + ":" + utxo.index |
|
|
|
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
describe('on construction', function() { |
|
|
describe('on construction', function() { |
|
|
beforeEach(function() { |
|
|
beforeEach(function() { |
|
|
wallet = new Wallet(seed, networks.bitcoin, [utxo]) |
|
|
wallet = new Wallet(seed, networks.bitcoin) |
|
|
|
|
|
wallet.setUnspentOutputs([utxo]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('matches the expected behaviour', function() { |
|
|
it('matches the expected behaviour', function() { |
|
|
var output = wallet.outputs[expectedOutputKey] |
|
|
var output = wallet.unspents[0] |
|
|
|
|
|
|
|
|
assert(output) |
|
|
|
|
|
assert.equal(output.value, utxo.value) |
|
|
|
|
|
assert.equal(output.address, utxo.address) |
|
|
assert.equal(output.address, utxo.address) |
|
|
|
|
|
assert.equal(output.value, utxo.value) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
@ -229,7 +229,8 @@ describe('Wallet', function() { |
|
|
var utxo1 = cloneObject(utxo) |
|
|
var utxo1 = cloneObject(utxo) |
|
|
utxo1.hash = fakeTxId(5) |
|
|
utxo1.hash = fakeTxId(5) |
|
|
|
|
|
|
|
|
wallet = new Wallet(seed, networks.bitcoin, [utxo, utxo1]) |
|
|
wallet = new Wallet(seed, networks.bitcoin) |
|
|
|
|
|
wallet.setUnspentOutputs([utxo, utxo1]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('sums over utxo values', function() { |
|
|
it('sums over utxo values', function() { |
|
@ -239,23 +240,36 @@ describe('Wallet', function() { |
|
|
|
|
|
|
|
|
describe('getUnspentOutputs', function() { |
|
|
describe('getUnspentOutputs', function() { |
|
|
beforeEach(function() { |
|
|
beforeEach(function() { |
|
|
wallet = new Wallet(seed, networks.bitcoin, [utxo]) |
|
|
wallet = new Wallet(seed, networks.bitcoin) |
|
|
|
|
|
wallet.setUnspentOutputs([utxo]) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('parses wallet outputs to the expected format', function(){ |
|
|
it('parses wallet unspents to the expected format', function() { |
|
|
assert.deepEqual(wallet.getUnspentOutputs(), [utxo]) |
|
|
var outputs = wallet.getUnspentOutputs() |
|
|
|
|
|
var output = outputs[0] |
|
|
|
|
|
|
|
|
|
|
|
assert.equal(utxo.address, output.address) |
|
|
|
|
|
assert.equal(utxo.index, output.index) |
|
|
|
|
|
assert.equal(utxo.value, output.value) |
|
|
|
|
|
|
|
|
|
|
|
// FIXME: remove in 2.0.0
|
|
|
|
|
|
assert.equal(utxo.txId, output.hash) |
|
|
|
|
|
assert.equal(utxo.pending, output.pending) |
|
|
|
|
|
|
|
|
|
|
|
// new in 2.0.0
|
|
|
|
|
|
assert.equal(utxo.txId, output.txId) |
|
|
|
|
|
assert.equal(utxo.confirmations, output.confirmations) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it("ignores pending spending outputs (outputs with 'to' property)", function(){ |
|
|
it("ignores spent unspents (outputs with 'spent' property)", function() { |
|
|
var output = wallet.outputs[expectedOutputKey] |
|
|
var unspent = wallet.unspents[0] |
|
|
output.to = fakeTxId(0) + ':' + 0 |
|
|
unspent.pending = true |
|
|
output.pending = true |
|
|
unspent.spent = true |
|
|
assert.deepEqual(wallet.getUnspentOutputs(), []) |
|
|
assert.deepEqual(wallet.getUnspentOutputs(), []) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// FIXME: remove in 2.x.y
|
|
|
|
|
|
describe('setUnspentOutputs', function() { |
|
|
describe('setUnspentOutputs', function() { |
|
|
var utxo |
|
|
var utxo |
|
|
var expectedOutputKey |
|
|
var expectedOutputKey |
|
@ -268,16 +282,13 @@ describe('Wallet', function() { |
|
|
value: 500000 |
|
|
value: 500000 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
expectedOutputKey = utxo.hash + ":" + utxo.index |
|
|
|
|
|
|
|
|
|
|
|
wallet = new Wallet(seed, networks.bitcoin) |
|
|
wallet = new Wallet(seed, networks.bitcoin) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('matches the expected behaviour', function() { |
|
|
it('matches the expected behaviour', function() { |
|
|
wallet.setUnspentOutputs([utxo]) |
|
|
wallet.setUnspentOutputs([utxo]) |
|
|
|
|
|
|
|
|
var output = wallet.outputs[expectedOutputKey] |
|
|
var output = wallet.unspents[0] |
|
|
assert(output) |
|
|
|
|
|
assert.equal(output.value, utxo.value) |
|
|
assert.equal(output.value, utxo.value) |
|
|
assert.equal(output.address, utxo.address) |
|
|
assert.equal(output.address, utxo.address) |
|
|
}) |
|
|
}) |
|
@ -331,18 +342,18 @@ describe('Wallet', function() { |
|
|
spendTx = Transaction.fromHex(fixtureTx2Hex) |
|
|
spendTx = Transaction.fromHex(fixtureTx2Hex) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it("outgoing: sets the pending flag and 'to' on output", function(){ |
|
|
it("outgoing: sets the pending flag and 'spent' on output", function() { |
|
|
var txIn = spendTx.ins[0] |
|
|
var txIn = spendTx.ins[0] |
|
|
var txInId = new Buffer(txIn.hash) |
|
|
var txInId = new Buffer(txIn.hash) |
|
|
Array.prototype.reverse.call(txInId) |
|
|
Array.prototype.reverse.call(txInId) |
|
|
txInId = txInId.toString('hex') |
|
|
txInId = txInId.toString('hex') |
|
|
|
|
|
|
|
|
var key = txInId + ':' + txIn.index |
|
|
var unspent = wallet.unspents[0] |
|
|
assert(!wallet.outputs[key].pending) |
|
|
assert(!unspent.pending) |
|
|
|
|
|
|
|
|
wallet.processPendingTx(spendTx) |
|
|
wallet.processPendingTx(spendTx) |
|
|
assert(wallet.outputs[key].pending) |
|
|
assert(unspent.pending) |
|
|
assert.equal(wallet.outputs[key].to, spendTx.getId() + ':' + 0) |
|
|
assert(unspent.spent, true) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
@ -359,7 +370,7 @@ describe('Wallet', function() { |
|
|
wallet.processConfirmedTx(tx2) |
|
|
wallet.processConfirmedTx(tx2) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
describe("when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.outputs", function(){ |
|
|
describe("when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.unspentMap", function() { |
|
|
it("works for receive address", function() { |
|
|
it("works for receive address", function() { |
|
|
var totalOuts = outputCount() |
|
|
var totalOuts = outputCount() |
|
|
|
|
|
|
|
@ -381,11 +392,11 @@ describe('Wallet', function() { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
function outputCount() { |
|
|
function outputCount() { |
|
|
return Object.keys(wallet.outputs).length |
|
|
return Object.keys(wallet.unspentMap).length |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
describe("when tx ins outpoint contains a known txhash:i", function(){ |
|
|
describe("when tx ins contains a known txhash:i", function() { |
|
|
var spendTx |
|
|
var spendTx |
|
|
beforeEach(function() { |
|
|
beforeEach(function() { |
|
|
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
|
|
|
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
|
|
@ -394,36 +405,36 @@ describe('Wallet', function() { |
|
|
spendTx = Transaction.fromHex(fixtureTx2Hex) |
|
|
spendTx = Transaction.fromHex(fixtureTx2Hex) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it("does not add to wallet.outputs", function(){ |
|
|
it("does not add to wallet.unspentMap", function() { |
|
|
wallet.processConfirmedTx(spendTx) |
|
|
wallet.processConfirmedTx(spendTx) |
|
|
assert.deepEqual(wallet.outputs, {}) |
|
|
assert.deepEqual(wallet.unspentMap, {}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it("deletes corresponding 'output'", function(){ |
|
|
it("deletes corresponding 'unspent'", function() { |
|
|
var txIn = spendTx.ins[0] |
|
|
var txIn = spendTx.ins[0] |
|
|
var txInId = new Buffer(txIn.hash) |
|
|
var txInId = bufferutils.reverse(txIn.hash).toString('hex') |
|
|
Array.prototype.reverse.call(txInId) |
|
|
|
|
|
txInId = txInId.toString('hex') |
|
|
|
|
|
|
|
|
|
|
|
var expected = txInId + ':' + txIn.index |
|
|
var expected = txInId + ':' + txIn.index |
|
|
assert(expected in wallet.outputs) |
|
|
assert(expected in wallet.unspentMap) |
|
|
|
|
|
|
|
|
wallet.processConfirmedTx(spendTx) |
|
|
wallet.processConfirmedTx(spendTx) |
|
|
assert(!(expected in wallet.outputs)) |
|
|
assert(!(expected in wallet.unspentMap)) |
|
|
|
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it("does nothing when none of the involved addresses belong to the wallet", function() { |
|
|
it("does nothing when none of the involved addresses belong to the wallet", function() { |
|
|
wallet.processConfirmedTx(tx) |
|
|
wallet.processConfirmedTx(tx) |
|
|
assert.deepEqual(wallet.outputs, {}) |
|
|
assert.deepEqual(wallet.unspentMap, {}) |
|
|
}) |
|
|
|
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function verifyOutputAdded(index, pending) { |
|
|
function verifyOutputAdded(index, pending) { |
|
|
var txOut = tx.outs[index] |
|
|
var txOut = tx.outs[index] |
|
|
|
|
|
|
|
|
var key = tx.getId() + ":" + index |
|
|
var key = tx.getId() + ":" + index |
|
|
var output = wallet.outputs[key] |
|
|
var output = wallet.unspentMap[key] |
|
|
assert.equal(output.from, key) |
|
|
assert.deepEqual(output.txHash, tx.getHash()) |
|
|
assert.equal(output.value, txOut.value) |
|
|
assert.equal(output.value, txOut.value) |
|
|
assert.equal(output.pending, pending) |
|
|
assert.equal(output.pending, pending) |
|
|
|
|
|
|
|
@ -447,26 +458,27 @@ describe('Wallet', function() { |
|
|
// set up 3 utxos
|
|
|
// set up 3 utxos
|
|
|
var utxos = [ |
|
|
var utxos = [ |
|
|
{ |
|
|
{ |
|
|
"hash": fakeTxId(1), |
|
|
"txId": fakeTxId(1), |
|
|
"index": 0, |
|
|
"index": 0, |
|
|
"address": address1, |
|
|
"address": address1, |
|
|
"value": 400000 // not enough for value
|
|
|
"value": 400000 // not enough for value
|
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"hash": fakeTxId(2), |
|
|
"txId": fakeTxId(2), |
|
|
"index": 1, |
|
|
"index": 1, |
|
|
"address": address1, |
|
|
"address": address1, |
|
|
"value": 500000 // enough for only value
|
|
|
"value": 500000 // enough for only value
|
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"hash": fakeTxId(3), |
|
|
"txId": fakeTxId(3), |
|
|
"index": 0, |
|
|
"index": 0, |
|
|
"address" : address2, |
|
|
"address" : address2, |
|
|
"value": 510000 // enough for value and fee
|
|
|
"value": 510000 // enough for value and fee
|
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
wallet = new Wallet(seed, networks.testnet, utxos) |
|
|
wallet = new Wallet(seed, networks.testnet) |
|
|
|
|
|
wallet.setUnspentOutputs(utxos) |
|
|
wallet.generateAddress() |
|
|
wallet.generateAddress() |
|
|
wallet.generateAddress() |
|
|
wallet.generateAddress() |
|
|
}) |
|
|
}) |
|
@ -474,7 +486,7 @@ describe('Wallet', function() { |
|
|
describe('transaction fee', function() { |
|
|
describe('transaction fee', function() { |
|
|
it('allows fee to be specified', function() { |
|
|
it('allows fee to be specified', function() { |
|
|
var fee = 30000 |
|
|
var fee = 30000 |
|
|
var tx = wallet.createTx(to, value, fee) |
|
|
var tx = wallet.createTx(to, value, { fixedFee: fee }) |
|
|
|
|
|
|
|
|
assert.equal(getFee(wallet, tx), fee) |
|
|
assert.equal(getFee(wallet, tx), fee) |
|
|
}) |
|
|
}) |
|
@ -482,20 +494,21 @@ describe('Wallet', function() { |
|
|
it('allows fee to be set to zero', function() { |
|
|
it('allows fee to be set to zero', function() { |
|
|
value = 510000 |
|
|
value = 510000 |
|
|
var fee = 0 |
|
|
var fee = 0 |
|
|
var tx = wallet.createTx(to, value, fee) |
|
|
var tx = wallet.createTx(to, value, { fixedFee: fee }) |
|
|
|
|
|
|
|
|
assert.equal(getFee(wallet, tx), fee) |
|
|
assert.equal(getFee(wallet, tx), fee) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('does not overestimate fees when network has dustSoftThreshold', function() { |
|
|
it('does not overestimate fees when network has dustSoftThreshold', function() { |
|
|
var utxo = { |
|
|
var utxo = { |
|
|
hash: fakeTxId(0), |
|
|
txId: fakeTxId(0), |
|
|
index: 0, |
|
|
index: 0, |
|
|
address: "LeyySKbQrRRwodKEj1W4a8y3YQupPLw5os", |
|
|
address: "LeyySKbQrRRwodKEj1W4a8y3YQupPLw5os", |
|
|
value: 500000 |
|
|
value: 500000 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var wallet = new Wallet(seed, networks.litecoin, [utxo]) |
|
|
var wallet = new Wallet(seed, networks.litecoin) |
|
|
|
|
|
wallet.setUnspentOutputs([utxo]) |
|
|
wallet.generateAddress() |
|
|
wallet.generateAddress() |
|
|
|
|
|
|
|
|
value = 200000 |
|
|
value = 200000 |
|
@ -505,13 +518,14 @@ describe('Wallet', function() { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
function getFee(wallet, tx) { |
|
|
function getFee(wallet, tx) { |
|
|
var inputValue = tx.ins.reduce(function(memo, input){ |
|
|
var inputValue = tx.ins.reduce(function(accum, input) { |
|
|
var id = Array.prototype.reverse.call(input.hash).toString('hex') |
|
|
var txId = bufferutils.reverse(input.hash).toString('hex') |
|
|
return memo + wallet.outputs[id + ':' + input.index].value |
|
|
|
|
|
|
|
|
return accum + wallet.unspentMap[txId + ':' + input.index].value |
|
|
}, 0) |
|
|
}, 0) |
|
|
|
|
|
|
|
|
return tx.outs.reduce(function(memo, output){ |
|
|
return tx.outs.reduce(function(accum, output) { |
|
|
return memo - output.value |
|
|
return accum - output.value |
|
|
}, inputValue) |
|
|
}, inputValue) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
@ -559,7 +573,10 @@ describe('Wallet', function() { |
|
|
var toValue = fromValue / 2 |
|
|
var toValue = fromValue / 2 |
|
|
var fee = 1e3 |
|
|
var fee = 1e3 |
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, toValue, fee, changeAddress) |
|
|
var tx = wallet.createTx(to, toValue, { |
|
|
|
|
|
fixedFee: fee, |
|
|
|
|
|
changeAddress: changeAddress |
|
|
|
|
|
}) |
|
|
assert.equal(tx.outs.length, 2) |
|
|
assert.equal(tx.outs.length, 2) |
|
|
|
|
|
|
|
|
var outAddress0 = Address.fromOutputScript(tx.outs[0].script, networks.testnet) |
|
|
var outAddress0 = Address.fromOutputScript(tx.outs[0].script, networks.testnet) |
|
@ -590,7 +607,7 @@ describe('Wallet', function() { |
|
|
var fee = 0 |
|
|
var fee = 0 |
|
|
wallet.generateChangeAddress() |
|
|
wallet.generateChangeAddress() |
|
|
wallet.generateChangeAddress() |
|
|
wallet.generateChangeAddress() |
|
|
var tx = wallet.createTx(to, value, fee) |
|
|
var tx = wallet.createTx(to, value, { fixedFee: fee }) |
|
|
|
|
|
|
|
|
assert.equal(tx.outs.length, 2) |
|
|
assert.equal(tx.outs.length, 2) |
|
|
var out = tx.outs[1] |
|
|
var out = tx.outs[1] |
|
@ -604,7 +621,7 @@ describe('Wallet', function() { |
|
|
var fee = 0 |
|
|
var fee = 0 |
|
|
assert.equal(wallet.changeAddresses.length, 0) |
|
|
assert.equal(wallet.changeAddresses.length, 0) |
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, value, fee) |
|
|
var tx = wallet.createTx(to, value, { fixedFee: fee }) |
|
|
|
|
|
|
|
|
assert.equal(wallet.changeAddresses.length, 1) |
|
|
assert.equal(wallet.changeAddresses.length, 1) |
|
|
var out = tx.outs[1] |
|
|
var out = tx.outs[1] |
|
@ -631,7 +648,7 @@ describe('Wallet', function() { |
|
|
var fee = 30000 |
|
|
var fee = 30000 |
|
|
sinon.spy(TransactionBuilder.prototype, "sign") |
|
|
sinon.spy(TransactionBuilder.prototype, "sign") |
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, value, fee) |
|
|
var tx = wallet.createTx(to, value, { fixedFee: fee }) |
|
|
|
|
|
|
|
|
assert(TransactionBuilder.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2))) |
|
|
assert(TransactionBuilder.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2))) |
|
|
assert(TransactionBuilder.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1))) |
|
|
assert(TransactionBuilder.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1))) |
|
|