2014-02-27 04:54:54 +01:00
|
|
|
var Wallet = require('../src/wallet.js')
|
2014-03-11 16:42:01 +01:00
|
|
|
var HDNode = require('../src/hdwallet.js')
|
2014-03-23 17:19:46 +01:00
|
|
|
var T = require('../src/transaction.js')
|
|
|
|
var Transaction = T.Transaction
|
|
|
|
var TransactionOut = T.TransactionOut
|
|
|
|
var Script = require('../src/script.js')
|
2014-03-11 16:42:01 +01:00
|
|
|
var convert = require('../src/convert.js')
|
2014-02-27 04:54:54 +01:00
|
|
|
var assert = require('assert')
|
2014-03-11 16:42:01 +01:00
|
|
|
var SHA256 = require('crypto-js/sha256')
|
|
|
|
var Crypto = require('crypto-js')
|
2014-02-27 04:54:54 +01:00
|
|
|
|
2014-03-22 14:10:56 +01:00
|
|
|
var fixtureTxes = require('./fixtures/mainnet_tx')
|
|
|
|
var fixtureTx1Hex = fixtureTxes.prevTx
|
|
|
|
var fixtureTx2Hex = fixtureTxes.tx
|
|
|
|
|
2014-03-23 18:29:10 +01:00
|
|
|
var sinon = require('sinon')
|
|
|
|
|
2014-02-27 04:54:54 +01:00
|
|
|
describe('Wallet', function() {
|
2014-03-22 07:37:09 +01:00
|
|
|
var seed, wallet;
|
2014-03-11 16:42:01 +01:00
|
|
|
beforeEach(function(){
|
|
|
|
seed = convert.wordArrayToBytes(SHA256("don't use a string seed like this in real life"))
|
2014-03-22 07:37:09 +01:00
|
|
|
wallet = new Wallet(seed)
|
2014-03-11 16:42:01 +01:00
|
|
|
})
|
2014-02-27 04:54:54 +01:00
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
describe('constructor', function() {
|
2014-02-28 07:27:31 +01:00
|
|
|
it('defaults to Bitcoin mainnet', function() {
|
2014-02-28 09:57:44 +01:00
|
|
|
assert.equal(wallet.getMasterKey().network, 'mainnet')
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
it("generates m/0' as the main account", function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var mainAccount = wallet.getAccountZero()
|
2014-03-11 16:42:01 +01:00
|
|
|
assert.equal(mainAccount.index, 0 + HDNode.HIGHEST_BIT)
|
|
|
|
assert.equal(mainAccount.depth, 1)
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
it("generates m/0'/0 as the external account", function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var account = wallet.getExternalAccount()
|
2014-03-11 16:42:01 +01:00
|
|
|
assert.equal(account.index, 0)
|
|
|
|
assert.equal(account.depth, 2)
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
it("generates m/0'/1 as the internal account", function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var account = wallet.getInternalAccount()
|
2014-03-11 16:42:01 +01:00
|
|
|
assert.equal(account.index, 1)
|
|
|
|
assert.equal(account.depth, 2)
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-15 03:18:59 +01:00
|
|
|
describe('when seed is not specified', function(){
|
|
|
|
it('generates a seed', function(){
|
|
|
|
var wallet = new Wallet()
|
|
|
|
assert.ok(wallet.getMasterKey())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
describe('constructor options', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('uses the network if specified', function() {
|
|
|
|
assert.equal(wallet.getMasterKey().network, 'testnet')
|
|
|
|
})
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
})
|
2014-02-28 05:05:48 +01:00
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
describe('newMasterKey', function(){
|
|
|
|
it('resets accounts', function(){
|
|
|
|
var wallet = new Wallet()
|
|
|
|
var oldAccountZero = wallet.getAccountZero()
|
|
|
|
var oldExternalAccount = wallet.getExternalAccount()
|
|
|
|
var oldInternalAccount = wallet.getInternalAccount()
|
|
|
|
|
|
|
|
wallet.newMasterKey(seed)
|
|
|
|
assertNotEqual(wallet.getAccountZero(), oldAccountZero)
|
|
|
|
assertNotEqual(wallet.getExternalAccount(), oldExternalAccount)
|
|
|
|
assertNotEqual(wallet.getInternalAccount(), oldInternalAccount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('resets addresses', function(){
|
|
|
|
var wallet = new Wallet()
|
|
|
|
wallet.generateAddress()
|
|
|
|
wallet.generateChangeAddress()
|
|
|
|
var oldAddresses = wallet.addresses
|
|
|
|
var oldChangeAddresses = wallet.changeAddresses
|
|
|
|
assert.notDeepEqual(oldAddresses, [])
|
|
|
|
assert.notDeepEqual(oldChangeAddresses, [])
|
|
|
|
|
|
|
|
wallet.newMasterKey(seed)
|
|
|
|
assert.deepEqual(wallet.addresses, [])
|
|
|
|
assert.deepEqual(wallet.changeAddresses, [])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
describe('generateAddress', function(){
|
2014-03-13 10:43:35 +01:00
|
|
|
it('generate receiving addresses', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
var expectedAddresses = [
|
|
|
|
"n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa",
|
|
|
|
"n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"
|
|
|
|
]
|
|
|
|
|
|
|
|
assert.equal(wallet.generateAddress(), expectedAddresses[0])
|
|
|
|
assert.equal(wallet.generateAddress(), expectedAddresses[1])
|
|
|
|
assert.deepEqual(wallet.addresses, expectedAddresses)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('generateChangeAddress', function(){
|
|
|
|
it('generates change addresses', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
var expectedAddresses = ["mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"]
|
2014-03-11 16:42:01 +01:00
|
|
|
|
2014-03-13 10:43:35 +01:00
|
|
|
assert.equal(wallet.generateChangeAddress(), expectedAddresses[0])
|
|
|
|
assert.deepEqual(wallet.changeAddresses, expectedAddresses)
|
2014-03-11 16:42:01 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-13 12:05:55 +01:00
|
|
|
|
|
|
|
describe('getPrivateKey', function(){
|
|
|
|
it('returns the private key at the given index of external account', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).priv)
|
|
|
|
assertEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).priv)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getInternalPrivateKey', function(){
|
|
|
|
it('returns the private key at the given index of internal account', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).priv)
|
|
|
|
assertEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).priv)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPrivateKeyForAddress', function(){
|
|
|
|
it('returns the private key for the given address', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
wallet.generateChangeAddress()
|
|
|
|
wallet.generateAddress()
|
|
|
|
wallet.generateAddress()
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
2014-03-14 03:31:26 +01:00
|
|
|
wallet.getExternalAccount().derive(1).priv)
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
2014-03-14 03:31:26 +01:00
|
|
|
wallet.getInternalAccount().derive(0).priv)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('raises an error when address is not found', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X")
|
|
|
|
}, Error, 'Unknown address. Make sure the address is from the keychain and has been generated.')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-22 07:37:09 +01:00
|
|
|
describe('Unspent Outputs', function(){
|
|
|
|
var expectedUtxo, expectedOutputKey;
|
|
|
|
beforeEach(function(){
|
2014-03-25 04:04:27 +01:00
|
|
|
expectedUtxo = {
|
|
|
|
"hash":"6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7",
|
|
|
|
"hashLittleEndian":"c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a",
|
|
|
|
"outputIndex": 0,
|
|
|
|
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv",
|
|
|
|
"value": 20000
|
|
|
|
}
|
|
|
|
expectedOutputKey = expectedUtxo.hash + ":" + expectedUtxo.outputIndex
|
|
|
|
})
|
|
|
|
|
|
|
|
function addUtxoToOutput(utxo){
|
|
|
|
var key = utxo.hash + ":" + utxo.outputIndex
|
|
|
|
wallet.outputs[key] = {
|
|
|
|
receive: key,
|
|
|
|
address: utxo.address,
|
|
|
|
value: utxo.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('getBalance', function(){
|
|
|
|
var utxo1
|
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
utxo1 = cloneObject(expectedUtxo)
|
|
|
|
utxo1.hash = utxo1.hash.replace('7', 'l')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sums over utxo values', function(){
|
|
|
|
addUtxoToOutput(expectedUtxo)
|
|
|
|
addUtxoToOutput(utxo1)
|
|
|
|
|
|
|
|
assert.deepEqual(wallet.getBalance(), 40000)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('excludes spent outputs', function(){
|
|
|
|
addUtxoToOutput(expectedUtxo)
|
|
|
|
addUtxoToOutput(utxo1)
|
|
|
|
wallet.outputs[utxo1.hash + ':' + utxo1.outputIndex].spend = "sometxn:m"
|
|
|
|
|
|
|
|
assert.deepEqual(wallet.getBalance(), 20000)
|
|
|
|
})
|
2014-03-22 07:37:09 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('getUnspentOutputs', function(){
|
2014-03-25 03:41:03 +01:00
|
|
|
beforeEach(function(){
|
2014-03-25 04:04:27 +01:00
|
|
|
addUtxoToOutput(expectedUtxo)
|
2014-03-25 03:41:03 +01:00
|
|
|
})
|
2014-03-22 07:37:09 +01:00
|
|
|
|
2014-03-25 03:41:03 +01:00
|
|
|
it('parses wallet outputs to the expect format', function(){
|
2014-03-25 04:04:27 +01:00
|
|
|
assert.deepEqual(wallet.getUnspentOutputs(), [expectedUtxo])
|
2014-03-22 07:37:09 +01:00
|
|
|
})
|
2014-03-25 03:41:03 +01:00
|
|
|
|
|
|
|
it('excludes spent outputs', function(){
|
|
|
|
wallet.outputs[expectedOutputKey].spend = "sometxn:m"
|
|
|
|
assert.deepEqual(wallet.getUnspentOutputs(), [])
|
|
|
|
})
|
2014-03-22 07:37:09 +01:00
|
|
|
})
|
2014-03-22 08:09:00 +01:00
|
|
|
|
|
|
|
describe('setUnspentOutputs', function(){
|
|
|
|
var utxo;
|
|
|
|
beforeEach(function(){
|
2014-03-25 04:04:27 +01:00
|
|
|
utxo = cloneObject([expectedUtxo])
|
2014-03-22 08:09:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('uses hashLittleEndian when hash is not present', function(){
|
|
|
|
delete utxo[0]['hash']
|
|
|
|
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
verifyOutputs()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('uses hash when hashLittleEndian is not present', function(){
|
|
|
|
delete utxo[0]['hashLittleEndian']
|
|
|
|
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
verifyOutputs()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('uses hash when both hash and hashLittleEndian are present', function(){
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
verifyOutputs()
|
|
|
|
})
|
|
|
|
|
2014-03-22 08:37:03 +01:00
|
|
|
describe('required fields', function(){
|
|
|
|
it("throws an error when hash and hashLittleEndian are both missing", function(){
|
|
|
|
delete utxo[0]['hash']
|
|
|
|
delete utxo[0]['hashLittleEndian']
|
|
|
|
|
|
|
|
var errorMessage = 'Invalid unspent output: key hash(or hashLittleEndian) is missing. ' +
|
2014-03-25 05:25:25 +01:00
|
|
|
'A valid unspent output must contain outputIndex, address, value and hash(or hashLittleEndian)'
|
2014-03-22 08:37:03 +01:00
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
}, Error, errorMessage)
|
|
|
|
});
|
|
|
|
|
2014-03-25 05:25:25 +01:00
|
|
|
['outputIndex', 'address', 'value'].forEach(function(field){
|
2014-03-22 08:37:03 +01:00
|
|
|
it("throws an error when " + field + " is missing", function(){
|
|
|
|
delete utxo[0][field]
|
|
|
|
var errorMessage = 'Invalid unspent output: key ' + field +
|
2014-03-25 05:25:25 +01:00
|
|
|
' is missing. A valid unspent output must contain outputIndex, address, value and hash(or hashLittleEndian)'
|
2014-03-22 08:37:03 +01:00
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
}, Error, errorMessage)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-22 08:09:00 +01:00
|
|
|
function verifyOutputs() {
|
|
|
|
var output = wallet.outputs[expectedOutputKey]
|
|
|
|
assert(output)
|
|
|
|
assert.equal(output.value, utxo[0].value)
|
|
|
|
assert.equal(output.address, utxo[0].address)
|
|
|
|
}
|
|
|
|
})
|
2014-03-22 07:37:09 +01:00
|
|
|
})
|
|
|
|
|
2014-03-22 14:10:56 +01:00
|
|
|
describe('processTx', function(){
|
|
|
|
var tx;
|
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
tx = Transaction.deserialize(fixtureTx1Hex)
|
|
|
|
})
|
|
|
|
|
|
|
|
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 = [tx.outs[0].address.toString()]
|
|
|
|
|
|
|
|
wallet.processTx(tx)
|
|
|
|
|
|
|
|
assert.equal(outputCount(), totalOuts + 1)
|
|
|
|
verifyOutputAdded(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("works for change address", function(){
|
|
|
|
var totalOuts = outputCount()
|
|
|
|
wallet.changeAddresses = [tx.outs[1].address.toString()]
|
|
|
|
|
|
|
|
wallet.processTx(tx)
|
|
|
|
|
|
|
|
assert.equal(outputCount(), totalOuts + 1)
|
|
|
|
verifyOutputAdded(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
function outputCount(){
|
|
|
|
return Object.keys(wallet.outputs).length
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyOutputAdded(index) {
|
|
|
|
var txOut = tx.outs[index]
|
|
|
|
var key = convert.bytesToHex(tx.getHash()) + ":" + index
|
|
|
|
var output = wallet.outputs[key]
|
2014-03-22 14:19:50 +01:00
|
|
|
assert.equal(output.receive, key)
|
2014-03-22 14:10:56 +01:00
|
|
|
assert.equal(output.value, txOut.value)
|
|
|
|
assert.equal(output.address, txOut.address)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("when tx ins outpoint contains a known txhash:i, the corresponding 'output' gets updated", function(){
|
|
|
|
beforeEach(function(){
|
2014-03-23 17:19:46 +01:00
|
|
|
wallet.addresses = [tx.outs[0].address.toString()] // the address fixtureTx2 used as input
|
2014-03-22 14:10:56 +01:00
|
|
|
wallet.processTx(tx)
|
|
|
|
|
|
|
|
tx = Transaction.deserialize(fixtureTx2Hex)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does not add to wallet.outputs", function(){
|
|
|
|
var outputs = wallet.outputs
|
|
|
|
wallet.processTx(tx)
|
|
|
|
assert.deepEqual(wallet.outputs, outputs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("sets spend with the transaction hash and input index", function(){
|
|
|
|
wallet.processTx(tx)
|
|
|
|
|
|
|
|
var txIn = tx.ins[0]
|
|
|
|
var key = txIn.outpoint.hash + ":" + txIn.outpoint.index
|
|
|
|
var output = wallet.outputs[key]
|
|
|
|
|
|
|
|
assert.equal(output.spend, convert.bytesToHex(tx.getHash()) + ':' + 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("does nothing when none of the involved addresses belong to the wallet", function(){
|
|
|
|
var outputs = wallet.outputs
|
|
|
|
wallet.processTx(tx)
|
|
|
|
assert.deepEqual(wallet.outputs, outputs)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-23 17:19:46 +01:00
|
|
|
describe('createTx', function(){
|
2014-03-23 18:29:10 +01:00
|
|
|
var to, value;
|
|
|
|
var address1, address2;
|
2014-03-23 17:19:46 +01:00
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
to = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'
|
|
|
|
value = 500000
|
|
|
|
|
|
|
|
// generate 2 addresses
|
2014-03-23 18:29:10 +01:00
|
|
|
address1 = wallet.generateAddress()
|
|
|
|
address2 = wallet.generateAddress()
|
2014-03-23 17:19:46 +01:00
|
|
|
|
|
|
|
// set up 3 utxo
|
|
|
|
utxo = [
|
|
|
|
{
|
|
|
|
"hash": fakeTxHash(1),
|
|
|
|
"outputIndex": 0,
|
2014-03-23 18:29:10 +01:00
|
|
|
"address" : address1,
|
2014-03-23 17:19:46 +01:00
|
|
|
"value": 400000 // not enough for value
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hash": fakeTxHash(2),
|
|
|
|
"outputIndex": 1,
|
2014-03-23 18:29:10 +01:00
|
|
|
"address" : address1,
|
2014-03-23 17:19:46 +01:00
|
|
|
"value": 500000 // enough for only value
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hash": fakeTxHash(3),
|
|
|
|
"outputIndex": 0,
|
2014-03-23 18:29:10 +01:00
|
|
|
"address" : address2,
|
2014-03-23 17:19:46 +01:00
|
|
|
"value": 520000 // enough for value and fee
|
|
|
|
}
|
|
|
|
]
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('choosing utxo', function(){
|
|
|
|
it('calculates fees', function(){
|
|
|
|
var tx = wallet.createTx(to, value)
|
|
|
|
|
|
|
|
assert.equal(tx.ins.length, 1)
|
|
|
|
assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('allows fee to be specified', function(){
|
|
|
|
var fee = 30000
|
|
|
|
var tx = wallet.createTx(to, value, fee)
|
|
|
|
|
|
|
|
assert.equal(tx.ins.length, 2)
|
|
|
|
assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 })
|
|
|
|
assert.deepEqual(tx.ins[1].outpoint, { hash: fakeTxHash(2), index: 1 })
|
|
|
|
})
|
2014-03-25 03:35:33 +01:00
|
|
|
|
|
|
|
it('ignores spent outputs', function(){
|
|
|
|
utxo.push(
|
|
|
|
{
|
|
|
|
"hash": fakeTxHash(4),
|
|
|
|
"outputIndex": 0,
|
|
|
|
"address" : address2,
|
|
|
|
"value": 530000 // enough but spent before createTx
|
|
|
|
}
|
|
|
|
)
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
wallet.outputs[fakeTxHash(4) + ":" + 0].spend = fakeTxHash(5) + ":" + 0
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, value)
|
|
|
|
|
|
|
|
assert.equal(tx.ins.length, 1)
|
|
|
|
assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 })
|
|
|
|
})
|
2014-03-23 17:19:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('transaction outputs', function(){
|
|
|
|
it('includes the specified address and amount', function(){
|
|
|
|
var tx = wallet.createTx(to, value)
|
|
|
|
|
|
|
|
assert.equal(tx.outs.length, 1)
|
|
|
|
var out = tx.outs[0]
|
|
|
|
assert.equal(out.address, to)
|
|
|
|
assert.equal(out.value, value)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('change', function(){
|
|
|
|
it('uses the last change address if there is any', function(){
|
2014-03-23 19:42:41 +01:00
|
|
|
var fee = 5000
|
2014-03-23 17:19:46 +01:00
|
|
|
wallet.generateChangeAddress()
|
|
|
|
wallet.generateChangeAddress()
|
|
|
|
var tx = wallet.createTx(to, value, fee)
|
|
|
|
|
|
|
|
assert.equal(tx.outs.length, 2)
|
|
|
|
var out = tx.outs[1]
|
|
|
|
assert.equal(out.address, wallet.changeAddresses[1])
|
2014-03-23 19:42:41 +01:00
|
|
|
assert.equal(out.value, 15000)
|
2014-03-23 17:19:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('generates a change address if there is not any', function(){
|
2014-03-23 19:42:41 +01:00
|
|
|
var fee = 5000
|
2014-03-23 17:19:46 +01:00
|
|
|
assert.equal(wallet.changeAddresses.length, 0)
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, value, fee)
|
|
|
|
|
|
|
|
assert.equal(wallet.changeAddresses.length, 1)
|
|
|
|
var out = tx.outs[1]
|
|
|
|
assert.equal(out.address, wallet.changeAddresses[0])
|
2014-03-23 19:42:41 +01:00
|
|
|
assert.equal(out.value, 15000)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2014-03-23 17:19:46 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-03-23 18:29:10 +01:00
|
|
|
|
|
|
|
describe('signing', function(){
|
|
|
|
afterEach(function(){
|
|
|
|
Transaction.prototype.sign.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('signes the inputs with respective keys', function(){
|
|
|
|
var fee = 30000
|
|
|
|
sinon.stub(Transaction.prototype, "sign")
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, value, fee)
|
|
|
|
|
|
|
|
assert(Transaction.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2)))
|
|
|
|
assert(Transaction.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1)))
|
|
|
|
})
|
|
|
|
})
|
2014-03-23 18:42:36 +01:00
|
|
|
|
|
|
|
describe('when value is below dust threshold', function(){
|
|
|
|
it('throws an error', function(){
|
2014-03-23 19:42:41 +01:00
|
|
|
var value = 5430
|
2014-03-23 18:42:36 +01:00
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.createTx(to, value)
|
2014-03-23 19:42:41 +01:00
|
|
|
}, /Value must be above dust threshold/)
|
2014-03-23 18:42:36 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-23 19:19:17 +01:00
|
|
|
describe('when there is not enough money', function(){
|
|
|
|
it('throws an error', function(){
|
|
|
|
var value = 1400001
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.createTx(to, value)
|
|
|
|
}, Error, 'Not enough money to send funds including transaction fee. Have: 1420000, needed: 1420001')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-23 18:42:36 +01:00
|
|
|
function fakeTxHash(i) {
|
|
|
|
return "txtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtx" + i
|
|
|
|
}
|
2014-03-23 17:19:46 +01:00
|
|
|
})
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
function assertEqual(obj1, obj2){
|
|
|
|
assert.equal(obj1.toString(), obj2.toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertNotEqual(obj1, obj2){
|
|
|
|
assert.notEqual(obj1.toString(), obj2.toString())
|
2014-03-13 12:05:55 +01:00
|
|
|
}
|
2014-03-22 08:09:00 +01:00
|
|
|
|
|
|
|
// quick and dirty: does not deal with functions on object
|
|
|
|
function cloneObject(obj){
|
|
|
|
return JSON.parse(JSON.stringify(obj))
|
|
|
|
}
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|