Browse Source

tests: code formatting

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
0f9674e65f
  1. 164
      test/wallet.js

164
test/wallet.js

@ -28,13 +28,13 @@ function fakeTxId(i) {
describe('Wallet', function() { describe('Wallet', function() {
var seed var seed
beforeEach(function(){ beforeEach(function() {
seed = crypto.sha256("don't use a string seed like this in real life") seed = crypto.sha256("don't use a string seed like this in real life")
}) })
describe('constructor', function() { describe('constructor', function() {
var wallet var wallet
beforeEach(function(){ beforeEach(function() {
wallet = new Wallet(seed) wallet = new Wallet(seed)
}) })
@ -60,8 +60,8 @@ describe('Wallet', function() {
assert.equal(account.depth, 2) assert.equal(account.depth, 2)
}) })
describe('when seed is not specified', function(){ describe('when seed is not specified', function() {
it('generates a seed', function(){ it('generates a seed', function() {
var wallet = new Wallet() var wallet = new Wallet()
assert(wallet.getMasterKey()) assert(wallet.getMasterKey())
}) })
@ -78,8 +78,8 @@ describe('Wallet', function() {
}) })
}) })
describe('newMasterKey', function(){ describe('newMasterKey', function() {
it('resets accounts', function(){ it('resets accounts', function() {
var wallet = new Wallet() var wallet = new Wallet()
var oldAccountZero = wallet.getAccountZero() var oldAccountZero = wallet.getAccountZero()
var oldExternalAccount = wallet.getExternalAccount() var oldExternalAccount = wallet.getExternalAccount()
@ -91,7 +91,7 @@ describe('Wallet', function() {
assertNotEqual(wallet.getInternalAccount(), oldInternalAccount) assertNotEqual(wallet.getInternalAccount(), oldInternalAccount)
}) })
it('resets addresses', function(){ it('resets addresses', function() {
var wallet = new Wallet() var wallet = new Wallet()
wallet.generateAddress() wallet.generateAddress()
wallet.generateChangeAddress() wallet.generateChangeAddress()
@ -106,8 +106,8 @@ describe('Wallet', function() {
}) })
}) })
describe('generateAddress', function(){ describe('generateAddress', function() {
it('generate receiving addresses', function(){ it('generate receiving addresses', function() {
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
var expectedAddresses = [ var expectedAddresses = [
"n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa", "n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa",
@ -120,13 +120,13 @@ describe('Wallet', function() {
}) })
}) })
describe('generateChangeAddress', function(){ describe('generateChangeAddress', function() {
var wallet var wallet
beforeEach(function(){ beforeEach(function() {
wallet = new Wallet(seed) wallet = new Wallet(seed)
}) })
it('generates change addresses', function(){ it('generates change addresses', function() {
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
var expectedAddresses = ["mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"] var expectedAddresses = ["mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"]
@ -135,13 +135,13 @@ describe('Wallet', function() {
}) })
}) })
describe('getPrivateKey', function(){ describe('getPrivateKey', function() {
var wallet var wallet
beforeEach(function(){ beforeEach(function() {
wallet = new Wallet(seed) wallet = new Wallet(seed)
}) })
it('returns the private key at the given index of external account', function(){ it('returns the private key at the given index of external account', function() {
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).privKey) assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).privKey)
@ -149,13 +149,13 @@ describe('Wallet', function() {
}) })
}) })
describe('getInternalPrivateKey', function(){ describe('getInternalPrivateKey', function() {
var wallet var wallet
beforeEach(function(){ beforeEach(function() {
wallet = new Wallet(seed) wallet = new Wallet(seed)
}) })
it('returns the private key at the given index of internal account', function(){ it('returns the private key at the given index of internal account', function() {
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).privKey) assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).privKey)
@ -163,13 +163,13 @@ describe('Wallet', function() {
}) })
}) })
describe('getPrivateKeyForAddress', function(){ describe('getPrivateKeyForAddress', function() {
var wallet var wallet
beforeEach(function(){ beforeEach(function() {
wallet = new Wallet(seed) wallet = new Wallet(seed)
}) })
it('returns the private key for the given address', function(){ it('returns the private key for the given address', function() {
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
wallet.generateChangeAddress() wallet.generateChangeAddress()
wallet.generateAddress() wallet.generateAddress()
@ -185,7 +185,7 @@ describe('Wallet', function() {
) )
}) })
it('raises an error when address is not found', function(){ it('raises an error when address is not found', function() {
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
assert.throws(function() { assert.throws(function() {
@ -194,11 +194,11 @@ describe('Wallet', function() {
}) })
}) })
describe('Unspent Outputs', function(){ describe('Unspent Outputs', function() {
var utxo, expectedOutputKey var utxo, expectedOutputKey
var wallet var wallet
beforeEach(function(){ beforeEach(function() {
utxo = { utxo = {
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv", "address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv",
"hash": fakeTxId(6), "hash": fakeTxId(6),
@ -210,12 +210,12 @@ describe('Wallet', function() {
expectedOutputKey = utxo.hash + ":" + utxo.index 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, [utxo])
}) })
it('matches the expected behaviour', function(){ it('matches the expected behaviour', function() {
var output = wallet.outputs[expectedOutputKey] var output = wallet.outputs[expectedOutputKey]
assert(output) assert(output)
@ -224,29 +224,29 @@ describe('Wallet', function() {
}) })
}) })
describe('getBalance', function(){ describe('getBalance', function() {
beforeEach(function(){ beforeEach(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, [utxo, utxo1])
}) })
it('sums over utxo values', function(){ it('sums over utxo values', function() {
assert.equal(wallet.getBalance(), 40000) assert.equal(wallet.getBalance(), 40000)
}) })
}) })
describe('getUnspentOutputs', function(){ describe('getUnspentOutputs', function() {
beforeEach(function(){ beforeEach(function() {
wallet = new Wallet(seed, networks.bitcoin, [utxo]) wallet = new Wallet(seed, networks.bitcoin, [utxo])
}) })
it('parses wallet outputs to the expected format', function(){ it('parses wallet outputs to the expected format', function() {
assert.deepEqual(wallet.getUnspentOutputs(), [utxo]) assert.deepEqual(wallet.getUnspentOutputs(), [utxo])
}) })
it("ignores pending spending outputs (outputs with 'to' property)", function(){ it("ignores pending spending outputs (outputs with 'to' property)", function() {
var output = wallet.outputs[expectedOutputKey] var output = wallet.outputs[expectedOutputKey]
output.to = fakeTxId(0) + ':' + 0 output.to = fakeTxId(0) + ':' + 0
output.pending = true output.pending = true
@ -256,11 +256,11 @@ describe('Wallet', function() {
}) })
// FIXME: remove in 2.x.y // FIXME: remove in 2.x.y
describe('setUnspentOutputs', function(){ describe('setUnspentOutputs', function() {
var utxo var utxo
var expectedOutputKey var expectedOutputKey
beforeEach(function(){ beforeEach(function() {
utxo = { utxo = {
hash: fakeTxId(0), hash: fakeTxId(0),
index: 0, index: 0,
@ -273,7 +273,7 @@ describe('Wallet', function() {
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.outputs[expectedOutputKey]
@ -282,9 +282,9 @@ describe('Wallet', function() {
assert.equal(output.address, utxo.address) assert.equal(output.address, utxo.address)
}) })
describe('required fields', function(){ describe('required fields', function() {
['index', 'address', 'hash', 'value'].forEach(function(field){ ['index', 'address', 'hash', 'value'].forEach(function(field){
it("throws an error when " + field + " is missing", function(){ it("throws an error when " + field + " is missing", function() {
delete utxo[field] delete utxo[field]
assert.throws(function() { assert.throws(function() {
@ -295,16 +295,16 @@ describe('Wallet', function() {
}) })
}) })
describe('Process transaction', function(){ describe('Process transaction', function() {
var wallet var wallet
beforeEach(function(){ beforeEach(function() {
wallet = new Wallet(seed) wallet = new Wallet(seed)
}) })
var addresses var addresses
var tx var tx
beforeEach(function(){ beforeEach(function() {
addresses = [ addresses = [
'115qa7iPZqn6as57hxLL8E9VUnhmGQxKWi', '115qa7iPZqn6as57hxLL8E9VUnhmGQxKWi',
'1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd', '1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd',
@ -314,24 +314,24 @@ describe('Wallet', function() {
tx = Transaction.fromHex(fixtureTx1Hex) tx = Transaction.fromHex(fixtureTx1Hex)
}) })
describe("processPendingTx", function(){ describe("processPendingTx", function() {
it("incoming: sets the pending flag on output", function(){ it("incoming: sets the pending flag on output", function() {
wallet.addresses = [addresses[0]] wallet.addresses = [addresses[0]]
wallet.processPendingTx(tx) wallet.processPendingTx(tx)
verifyOutputAdded(0, true) verifyOutputAdded(0, true)
}) })
describe("when tx ins outpoint contains a known txhash:i", function(){ describe("when tx ins outpoint contains a known txhash:i", function() {
var spendTx var spendTx
beforeEach(function(){ beforeEach(function() {
wallet.addresses = [addresses[0]] wallet.addresses = [addresses[0]]
wallet.processConfirmedTx(tx) wallet.processConfirmedTx(tx)
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 'to' 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)
@ -347,7 +347,7 @@ describe('Wallet', function() {
}) })
}) })
describe('processConfirmedTx', function(){ describe('processConfirmedTx', function() {
it('does not throw on scripts with no corresponding Address', function() { it('does not throw on scripts with no corresponding Address', function() {
var pubKey = wallet.getPrivateKey(0).pub var pubKey = wallet.getPrivateKey(0).pub
var script = scripts.pubKeyOutput(pubKey) var script = scripts.pubKeyOutput(pubKey)
@ -359,8 +359,8 @@ 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.outputs", function() {
it("works for receive address", function(){ it("works for receive address", function() {
var totalOuts = outputCount() var totalOuts = outputCount()
wallet.addresses = [addresses[0]] wallet.addresses = [addresses[0]]
@ -370,7 +370,7 @@ describe('Wallet', function() {
verifyOutputAdded(0, false) verifyOutputAdded(0, false)
}) })
it("works for change address", function(){ it("works for change address", function() {
var totalOuts = outputCount() var totalOuts = outputCount()
wallet.changeAddresses = [addresses[1]] wallet.changeAddresses = [addresses[1]]
@ -380,26 +380,26 @@ describe('Wallet', function() {
verifyOutputAdded(1, false) verifyOutputAdded(1, false)
}) })
function outputCount(){ function outputCount() {
return Object.keys(wallet.outputs).length return Object.keys(wallet.outputs).length
} }
}) })
describe("when tx ins outpoint contains a known txhash:i", function(){ describe("when tx ins outpoint 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
wallet.processConfirmedTx(tx) wallet.processConfirmedTx(tx)
spendTx = Transaction.fromHex(fixtureTx2Hex) spendTx = Transaction.fromHex(fixtureTx2Hex)
}) })
it("does not add to wallet.outputs", function(){ it("does not add to wallet.outputs", function() {
wallet.processConfirmedTx(spendTx) wallet.processConfirmedTx(spendTx)
assert.deepEqual(wallet.outputs, {}) assert.deepEqual(wallet.outputs, {})
}) })
it("deletes corresponding 'output'", function(){ it("deletes corresponding '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)
@ -413,7 +413,7 @@ describe('Wallet', function() {
}) })
}) })
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.outputs, {})
}) })
@ -432,12 +432,12 @@ describe('Wallet', function() {
} }
}) })
describe('createTx', function(){ describe('createTx', function() {
var wallet var wallet
var address1, address2 var address1, address2
var to, value var to, value
beforeEach(function(){ beforeEach(function() {
to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue' to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
value = 500000 value = 500000
@ -471,15 +471,15 @@ describe('Wallet', function() {
wallet.generateAddress() wallet.generateAddress()
}) })
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, fee)
assert.equal(getFee(wallet, tx), fee) assert.equal(getFee(wallet, tx), fee)
}) })
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, fee)
@ -487,7 +487,7 @@ describe('Wallet', function() {
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), hash: fakeTxId(0),
index: 0, index: 0,
@ -516,8 +516,8 @@ describe('Wallet', function() {
} }
}) })
describe('choosing utxo', function(){ describe('choosing utxo', function() {
it('takes fees into account', function(){ it('takes fees into account', function() {
var tx = wallet.createTx(to, value) var tx = wallet.createTx(to, value)
assert.equal(tx.ins.length, 1) assert.equal(tx.ins.length, 1)
@ -525,7 +525,7 @@ describe('Wallet', function() {
assert.equal(tx.ins[0].index, 0) assert.equal(tx.ins[0].index, 0)
}) })
it('uses confirmed outputs', function(){ it('uses confirmed outputs', function() {
var tx2 = new Transaction() var tx2 = new Transaction()
tx2.addInput(fakeTxId(4), 0) tx2.addInput(fakeTxId(4), 0)
tx2.addOutput(address2, 530000) tx2.addOutput(address2, 530000)
@ -538,7 +538,7 @@ describe('Wallet', function() {
assert.equal(tx.ins[0].index, 0) assert.equal(tx.ins[0].index, 0)
}) })
it('ignores pending outputs', function(){ it('ignores pending outputs', function() {
var tx2 = new Transaction() var tx2 = new Transaction()
tx2.addInput(fakeTxId(4), 0) tx2.addInput(fakeTxId(4), 0)
tx2.addOutput(address2, 530000) tx2.addOutput(address2, 530000)
@ -552,8 +552,8 @@ describe('Wallet', function() {
}) })
}) })
describe('changeAddress', function(){ describe('changeAddress', function() {
it('should allow custom changeAddress', function(){ it('should allow custom changeAddress', function() {
var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk' var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk'
var fromValue = 510000 var fromValue = 510000
var toValue = fromValue / 2 var toValue = fromValue / 2
@ -573,8 +573,8 @@ describe('Wallet', function() {
}) })
}) })
describe('transaction outputs', function(){ describe('transaction outputs', function() {
it('includes the specified address and amount', function(){ it('includes the specified address and amount', function() {
var tx = wallet.createTx(to, value) var tx = wallet.createTx(to, value)
assert.equal(tx.outs.length, 1) assert.equal(tx.outs.length, 1)
@ -585,8 +585,8 @@ describe('Wallet', function() {
assert.equal(out.value, value) assert.equal(out.value, value)
}) })
describe('change', function(){ describe('change', function() {
it('uses the last change address if there is any', function(){ it('uses the last change address if there is any', function() {
var fee = 0 var fee = 0
wallet.generateChangeAddress() wallet.generateChangeAddress()
wallet.generateChangeAddress() wallet.generateChangeAddress()
@ -600,7 +600,7 @@ describe('Wallet', function() {
assert.equal(out.value, 10000) assert.equal(out.value, 10000)
}) })
it('generates a change address if there is not any', function(){ it('generates a change address if there is not any', function() {
var fee = 0 var fee = 0
assert.equal(wallet.changeAddresses.length, 0) assert.equal(wallet.changeAddresses.length, 0)
@ -614,7 +614,7 @@ describe('Wallet', function() {
assert.equal(out.value, 10000) assert.equal(out.value, 10000)
}) })
it('skips change if it is not above dust threshold', function(){ it('skips change if it is not above dust threshold', function() {
var fee = 14570 var fee = 14570
var tx = wallet.createTx(to, value) var tx = wallet.createTx(to, value)
assert.equal(tx.outs.length, 1) assert.equal(tx.outs.length, 1)
@ -622,12 +622,12 @@ describe('Wallet', function() {
}) })
}) })
describe('signing', function(){ describe('signing', function() {
afterEach(function(){ afterEach(function() {
TransactionBuilder.prototype.sign.restore() TransactionBuilder.prototype.sign.restore()
}) })
it('signs the inputs with respective keys', function(){ it('signs the inputs with respective keys', function() {
var fee = 30000 var fee = 30000
sinon.spy(TransactionBuilder.prototype, "sign") sinon.spy(TransactionBuilder.prototype, "sign")
@ -638,8 +638,8 @@ describe('Wallet', function() {
}) })
}) })
describe('when value is below dust threshold', function(){ describe('when value is below dust threshold', function() {
it('throws an error', function(){ it('throws an error', function() {
var value = 546 var value = 546
assert.throws(function() { assert.throws(function() {
@ -648,8 +648,8 @@ describe('Wallet', function() {
}) })
}) })
describe('when there is not enough money', function(){ describe('when there is not enough money', function() {
it('throws an error', function(){ it('throws an error', function() {
var value = 1400001 var value = 1400001
assert.throws(function() { assert.throws(function() {

Loading…
Cancel
Save