tests: code formatting
This commit is contained in:
parent
43d135c4b3
commit
0f9674e65f
1 changed files with 82 additions and 82 deletions
164
test/wallet.js
164
test/wallet.js
|
@ -28,13 +28,13 @@ function fakeTxId(i) {
|
|||
|
||||
describe('Wallet', function() {
|
||||
var seed
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
seed = crypto.sha256("don't use a string seed like this in real life")
|
||||
})
|
||||
|
||||
describe('constructor', function() {
|
||||
var wallet
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
wallet = new Wallet(seed)
|
||||
})
|
||||
|
||||
|
@ -60,8 +60,8 @@ describe('Wallet', function() {
|
|||
assert.equal(account.depth, 2)
|
||||
})
|
||||
|
||||
describe('when seed is not specified', function(){
|
||||
it('generates a seed', function(){
|
||||
describe('when seed is not specified', function() {
|
||||
it('generates a seed', function() {
|
||||
var wallet = new Wallet()
|
||||
assert(wallet.getMasterKey())
|
||||
})
|
||||
|
@ -78,8 +78,8 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('newMasterKey', function(){
|
||||
it('resets accounts', function(){
|
||||
describe('newMasterKey', function() {
|
||||
it('resets accounts', function() {
|
||||
var wallet = new Wallet()
|
||||
var oldAccountZero = wallet.getAccountZero()
|
||||
var oldExternalAccount = wallet.getExternalAccount()
|
||||
|
@ -91,7 +91,7 @@ describe('Wallet', function() {
|
|||
assertNotEqual(wallet.getInternalAccount(), oldInternalAccount)
|
||||
})
|
||||
|
||||
it('resets addresses', function(){
|
||||
it('resets addresses', function() {
|
||||
var wallet = new Wallet()
|
||||
wallet.generateAddress()
|
||||
wallet.generateChangeAddress()
|
||||
|
@ -106,8 +106,8 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('generateAddress', function(){
|
||||
it('generate receiving addresses', function(){
|
||||
describe('generateAddress', function() {
|
||||
it('generate receiving addresses', function() {
|
||||
var wallet = new Wallet(seed, networks.testnet)
|
||||
var expectedAddresses = [
|
||||
"n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa",
|
||||
|
@ -120,13 +120,13 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('generateChangeAddress', function(){
|
||||
describe('generateChangeAddress', function() {
|
||||
var wallet
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
wallet = new Wallet(seed)
|
||||
})
|
||||
|
||||
it('generates change addresses', function(){
|
||||
it('generates change addresses', function() {
|
||||
var wallet = new Wallet(seed, networks.testnet)
|
||||
var expectedAddresses = ["mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"]
|
||||
|
||||
|
@ -135,13 +135,13 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('getPrivateKey', function(){
|
||||
describe('getPrivateKey', function() {
|
||||
var wallet
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
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)
|
||||
|
||||
assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).privKey)
|
||||
|
@ -149,13 +149,13 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('getInternalPrivateKey', function(){
|
||||
describe('getInternalPrivateKey', function() {
|
||||
var wallet
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
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)
|
||||
|
||||
assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).privKey)
|
||||
|
@ -163,13 +163,13 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('getPrivateKeyForAddress', function(){
|
||||
describe('getPrivateKeyForAddress', function() {
|
||||
var wallet
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
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)
|
||||
wallet.generateChangeAddress()
|
||||
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)
|
||||
|
||||
assert.throws(function() {
|
||||
|
@ -194,11 +194,11 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('Unspent Outputs', function(){
|
||||
describe('Unspent Outputs', function() {
|
||||
var utxo, expectedOutputKey
|
||||
var wallet
|
||||
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
utxo = {
|
||||
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv",
|
||||
"hash": fakeTxId(6),
|
||||
|
@ -210,12 +210,12 @@ describe('Wallet', function() {
|
|||
expectedOutputKey = utxo.hash + ":" + utxo.index
|
||||
})
|
||||
|
||||
describe('on construction', function(){
|
||||
beforeEach(function(){
|
||||
describe('on construction', function() {
|
||||
beforeEach(function() {
|
||||
wallet = new Wallet(seed, networks.bitcoin, [utxo])
|
||||
})
|
||||
|
||||
it('matches the expected behaviour', function(){
|
||||
it('matches the expected behaviour', function() {
|
||||
var output = wallet.outputs[expectedOutputKey]
|
||||
|
||||
assert(output)
|
||||
|
@ -224,29 +224,29 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('getBalance', function(){
|
||||
beforeEach(function(){
|
||||
describe('getBalance', function() {
|
||||
beforeEach(function() {
|
||||
var utxo1 = cloneObject(utxo)
|
||||
utxo1.hash = fakeTxId(5)
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getUnspentOutputs', function(){
|
||||
beforeEach(function(){
|
||||
describe('getUnspentOutputs', function() {
|
||||
beforeEach(function() {
|
||||
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])
|
||||
})
|
||||
|
||||
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]
|
||||
output.to = fakeTxId(0) + ':' + 0
|
||||
output.pending = true
|
||||
|
@ -256,11 +256,11 @@ describe('Wallet', function() {
|
|||
})
|
||||
|
||||
// FIXME: remove in 2.x.y
|
||||
describe('setUnspentOutputs', function(){
|
||||
describe('setUnspentOutputs', function() {
|
||||
var utxo
|
||||
var expectedOutputKey
|
||||
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
utxo = {
|
||||
hash: fakeTxId(0),
|
||||
index: 0,
|
||||
|
@ -273,7 +273,7 @@ describe('Wallet', function() {
|
|||
wallet = new Wallet(seed, networks.bitcoin)
|
||||
})
|
||||
|
||||
it('matches the expected behaviour', function(){
|
||||
it('matches the expected behaviour', function() {
|
||||
wallet.setUnspentOutputs([utxo])
|
||||
|
||||
var output = wallet.outputs[expectedOutputKey]
|
||||
|
@ -282,9 +282,9 @@ describe('Wallet', function() {
|
|||
assert.equal(output.address, utxo.address)
|
||||
})
|
||||
|
||||
describe('required fields', function(){
|
||||
describe('required fields', function() {
|
||||
['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]
|
||||
|
||||
assert.throws(function() {
|
||||
|
@ -295,16 +295,16 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('Process transaction', function(){
|
||||
describe('Process transaction', function() {
|
||||
var wallet
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
wallet = new Wallet(seed)
|
||||
})
|
||||
|
||||
var addresses
|
||||
var tx
|
||||
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
addresses = [
|
||||
'115qa7iPZqn6as57hxLL8E9VUnhmGQxKWi',
|
||||
'1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd',
|
||||
|
@ -314,24 +314,24 @@ describe('Wallet', function() {
|
|||
tx = Transaction.fromHex(fixtureTx1Hex)
|
||||
})
|
||||
|
||||
describe("processPendingTx", function(){
|
||||
it("incoming: sets the pending flag on output", function(){
|
||||
describe("processPendingTx", function() {
|
||||
it("incoming: sets the pending flag on output", function() {
|
||||
wallet.addresses = [addresses[0]]
|
||||
wallet.processPendingTx(tx)
|
||||
|
||||
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
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
wallet.addresses = [addresses[0]]
|
||||
wallet.processConfirmedTx(tx)
|
||||
|
||||
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 txInId = new Buffer(txIn.hash)
|
||||
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() {
|
||||
var pubKey = wallet.getPrivateKey(0).pub
|
||||
var script = scripts.pubKeyOutput(pubKey)
|
||||
|
@ -359,8 +359,8 @@ describe('Wallet', function() {
|
|||
wallet.processConfirmedTx(tx2)
|
||||
})
|
||||
|
||||
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(){
|
||||
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() {
|
||||
var totalOuts = outputCount()
|
||||
|
||||
wallet.addresses = [addresses[0]]
|
||||
|
@ -370,7 +370,7 @@ describe('Wallet', function() {
|
|||
verifyOutputAdded(0, false)
|
||||
})
|
||||
|
||||
it("works for change address", function(){
|
||||
it("works for change address", function() {
|
||||
var totalOuts = outputCount()
|
||||
wallet.changeAddresses = [addresses[1]]
|
||||
|
||||
|
@ -380,26 +380,26 @@ describe('Wallet', function() {
|
|||
verifyOutputAdded(1, false)
|
||||
})
|
||||
|
||||
function outputCount(){
|
||||
function outputCount() {
|
||||
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
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
|
||||
wallet.processConfirmedTx(tx)
|
||||
|
||||
spendTx = Transaction.fromHex(fixtureTx2Hex)
|
||||
})
|
||||
|
||||
it("does not add to wallet.outputs", function(){
|
||||
it("does not add to wallet.outputs", function() {
|
||||
wallet.processConfirmedTx(spendTx)
|
||||
assert.deepEqual(wallet.outputs, {})
|
||||
})
|
||||
|
||||
it("deletes corresponding 'output'", function(){
|
||||
it("deletes corresponding 'output'", function() {
|
||||
var txIn = spendTx.ins[0]
|
||||
var txInId = new Buffer(txIn.hash)
|
||||
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)
|
||||
assert.deepEqual(wallet.outputs, {})
|
||||
})
|
||||
|
@ -432,12 +432,12 @@ describe('Wallet', function() {
|
|||
}
|
||||
})
|
||||
|
||||
describe('createTx', function(){
|
||||
describe('createTx', function() {
|
||||
var wallet
|
||||
var address1, address2
|
||||
var to, value
|
||||
|
||||
beforeEach(function(){
|
||||
beforeEach(function() {
|
||||
to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
||||
value = 500000
|
||||
|
||||
|
@ -471,15 +471,15 @@ describe('Wallet', function() {
|
|||
wallet.generateAddress()
|
||||
})
|
||||
|
||||
describe('transaction fee', function(){
|
||||
it('allows fee to be specified', function(){
|
||||
describe('transaction fee', function() {
|
||||
it('allows fee to be specified', function() {
|
||||
var fee = 30000
|
||||
var tx = wallet.createTx(to, value, 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
|
||||
var fee = 0
|
||||
var tx = wallet.createTx(to, value, fee)
|
||||
|
@ -487,7 +487,7 @@ describe('Wallet', function() {
|
|||
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 = {
|
||||
hash: fakeTxId(0),
|
||||
index: 0,
|
||||
|
@ -516,8 +516,8 @@ describe('Wallet', function() {
|
|||
}
|
||||
})
|
||||
|
||||
describe('choosing utxo', function(){
|
||||
it('takes fees into account', function(){
|
||||
describe('choosing utxo', function() {
|
||||
it('takes fees into account', function() {
|
||||
var tx = wallet.createTx(to, value)
|
||||
|
||||
assert.equal(tx.ins.length, 1)
|
||||
|
@ -525,7 +525,7 @@ describe('Wallet', function() {
|
|||
assert.equal(tx.ins[0].index, 0)
|
||||
})
|
||||
|
||||
it('uses confirmed outputs', function(){
|
||||
it('uses confirmed outputs', function() {
|
||||
var tx2 = new Transaction()
|
||||
tx2.addInput(fakeTxId(4), 0)
|
||||
tx2.addOutput(address2, 530000)
|
||||
|
@ -538,7 +538,7 @@ describe('Wallet', function() {
|
|||
assert.equal(tx.ins[0].index, 0)
|
||||
})
|
||||
|
||||
it('ignores pending outputs', function(){
|
||||
it('ignores pending outputs', function() {
|
||||
var tx2 = new Transaction()
|
||||
tx2.addInput(fakeTxId(4), 0)
|
||||
tx2.addOutput(address2, 530000)
|
||||
|
@ -552,8 +552,8 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('changeAddress', function(){
|
||||
it('should allow custom changeAddress', function(){
|
||||
describe('changeAddress', function() {
|
||||
it('should allow custom changeAddress', function() {
|
||||
var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk'
|
||||
var fromValue = 510000
|
||||
var toValue = fromValue / 2
|
||||
|
@ -573,8 +573,8 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('transaction outputs', function(){
|
||||
it('includes the specified address and amount', function(){
|
||||
describe('transaction outputs', function() {
|
||||
it('includes the specified address and amount', function() {
|
||||
var tx = wallet.createTx(to, value)
|
||||
|
||||
assert.equal(tx.outs.length, 1)
|
||||
|
@ -585,8 +585,8 @@ describe('Wallet', function() {
|
|||
assert.equal(out.value, value)
|
||||
})
|
||||
|
||||
describe('change', function(){
|
||||
it('uses the last change address if there is any', function(){
|
||||
describe('change', function() {
|
||||
it('uses the last change address if there is any', function() {
|
||||
var fee = 0
|
||||
wallet.generateChangeAddress()
|
||||
wallet.generateChangeAddress()
|
||||
|
@ -600,7 +600,7 @@ describe('Wallet', function() {
|
|||
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
|
||||
assert.equal(wallet.changeAddresses.length, 0)
|
||||
|
||||
|
@ -614,7 +614,7 @@ describe('Wallet', function() {
|
|||
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 tx = wallet.createTx(to, value)
|
||||
assert.equal(tx.outs.length, 1)
|
||||
|
@ -622,12 +622,12 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('signing', function(){
|
||||
afterEach(function(){
|
||||
describe('signing', function() {
|
||||
afterEach(function() {
|
||||
TransactionBuilder.prototype.sign.restore()
|
||||
})
|
||||
|
||||
it('signs the inputs with respective keys', function(){
|
||||
it('signs the inputs with respective keys', function() {
|
||||
var fee = 30000
|
||||
sinon.spy(TransactionBuilder.prototype, "sign")
|
||||
|
||||
|
@ -638,8 +638,8 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('when value is below dust threshold', function(){
|
||||
it('throws an error', function(){
|
||||
describe('when value is below dust threshold', function() {
|
||||
it('throws an error', function() {
|
||||
var value = 546
|
||||
|
||||
assert.throws(function() {
|
||||
|
@ -648,8 +648,8 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('when there is not enough money', function(){
|
||||
it('throws an error', function(){
|
||||
describe('when there is not enough money', function() {
|
||||
it('throws an error', function() {
|
||||
var value = 1400001
|
||||
|
||||
assert.throws(function() {
|
||||
|
|
Loading…
Reference in a new issue