2014-05-06 08:52:31 +02:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var crypto = require('../src/crypto')
|
2014-05-30 10:41:03 +02:00
|
|
|
var networks = require('../src/networks')
|
2014-05-06 08:52:31 +02:00
|
|
|
var sinon = require('sinon')
|
2014-06-13 01:58:52 +02:00
|
|
|
var scripts = require('../src/scripts')
|
2014-05-06 08:52:31 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var Address = require('../src/address')
|
2014-06-03 09:08:42 +02:00
|
|
|
var HDNode = require('../src/hdnode')
|
2014-05-16 09:12:39 +02:00
|
|
|
var Transaction = require('../src/transaction')
|
2014-05-13 09:55:53 +02:00
|
|
|
var Wallet = require('../src/wallet')
|
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-05-28 05:17:04 +02:00
|
|
|
function fakeTxHash(i) {
|
2014-06-14 16:03:17 +02:00
|
|
|
var hash = new Buffer(32)
|
|
|
|
hash.fill(i)
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
function fakeTxId(i) {
|
|
|
|
var hash = fakeTxHash(i)
|
|
|
|
Array.prototype.reverse.call(hash)
|
|
|
|
return hash.toString('hex')
|
2014-05-28 05:17:04 +02:00
|
|
|
}
|
|
|
|
|
2014-02-27 04:54:54 +01:00
|
|
|
describe('Wallet', function() {
|
2014-03-31 05:47:47 +02:00
|
|
|
var seed, wallet
|
2014-03-11 16:42:01 +01:00
|
|
|
beforeEach(function(){
|
2014-04-08 14:13:03 +02:00
|
|
|
seed = crypto.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-04-16 21:43:34 +02:00
|
|
|
it('defaults to Bitcoin network', function() {
|
2014-05-30 10:41:03 +02:00
|
|
|
assert.equal(wallet.getMasterKey().network, networks.bitcoin)
|
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-06-03 09:08:42 +02:00
|
|
|
assert.equal(mainAccount.index, 0 + HDNode.HIGHEST_BIT)
|
2014-03-11 16:42:01 +01:00
|
|
|
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() {
|
2014-05-30 10:41:03 +02:00
|
|
|
wallet = new Wallet(seed, networks.testnet)
|
2014-03-11 16:42:01 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('uses the network if specified', function() {
|
2014-05-30 10:41:03 +02:00
|
|
|
assert.equal(wallet.getMasterKey().network, networks.testnet)
|
2014-03-11 16:42:01 +01:00
|
|
|
})
|
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(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-03-13 10:43:35 +01:00
|
|
|
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(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-03-13 10:43:35 +01:00
|
|
|
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(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-03-13 12:05:55 +01:00
|
|
|
|
2014-06-03 09:30:05 +02:00
|
|
|
assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).privKey)
|
|
|
|
assertEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).privKey)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getInternalPrivateKey', function(){
|
|
|
|
it('returns the private key at the given index of internal account', function(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-03-13 12:05:55 +01:00
|
|
|
|
2014-06-03 09:30:05 +02:00
|
|
|
assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).privKey)
|
|
|
|
assertEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).privKey)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPrivateKeyForAddress', function(){
|
|
|
|
it('returns the private key for the given address', function(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-03-13 12:05:55 +01:00
|
|
|
wallet.generateChangeAddress()
|
|
|
|
wallet.generateAddress()
|
|
|
|
wallet.generateAddress()
|
|
|
|
|
2014-05-06 06:13:00 +02:00
|
|
|
assertEqual(
|
|
|
|
wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
2014-06-03 09:30:05 +02:00
|
|
|
wallet.getExternalAccount().derive(1).privKey
|
2014-05-06 06:13:00 +02:00
|
|
|
)
|
|
|
|
assertEqual(
|
|
|
|
wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
2014-06-03 09:30:05 +02:00
|
|
|
wallet.getInternalAccount().derive(0).privKey
|
2014-05-06 06:13:00 +02:00
|
|
|
)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('raises an error when address is not found', function(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-03-13 12:05:55 +01:00
|
|
|
assert.throws(function() {
|
|
|
|
wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X")
|
2014-04-02 06:43:34 +02:00
|
|
|
}, /Unknown address. Make sure the address is from the keychain and has been generated./)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-22 07:37:09 +01:00
|
|
|
describe('Unspent Outputs', function(){
|
2014-03-31 05:47:47 +02:00
|
|
|
var expectedUtxo, expectedOutputKey
|
2014-03-22 07:37:09 +01:00
|
|
|
beforeEach(function(){
|
2014-03-25 04:04:27 +01:00
|
|
|
expectedUtxo = {
|
|
|
|
"hash":"6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7",
|
|
|
|
"outputIndex": 0,
|
|
|
|
"address" : "1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv",
|
2014-06-18 08:29:02 +02:00
|
|
|
"value": 20000,
|
|
|
|
"pending": true
|
2014-03-25 04:04:27 +01:00
|
|
|
}
|
|
|
|
expectedOutputKey = expectedUtxo.hash + ":" + expectedUtxo.outputIndex
|
|
|
|
})
|
|
|
|
|
|
|
|
function addUtxoToOutput(utxo){
|
|
|
|
var key = utxo.hash + ":" + utxo.outputIndex
|
|
|
|
wallet.outputs[key] = {
|
2014-06-18 08:34:53 +02:00
|
|
|
from: key,
|
2014-03-25 04:04:27 +01:00
|
|
|
address: utxo.address,
|
2014-06-18 08:29:02 +02:00
|
|
|
value: utxo.value,
|
|
|
|
pending: utxo.pending
|
2014-03-25 04:04:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-03-26 13:44:07 +01:00
|
|
|
assert.equal(wallet.getBalance(), 40000)
|
2014-03-25 04:04:27 +01:00
|
|
|
})
|
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-06-23 02:30:57 +02:00
|
|
|
|
|
|
|
it("ignores pending spending outputs (outputs with 'to' property)", function(){
|
|
|
|
var output = wallet.outputs[expectedOutputKey]
|
|
|
|
output.to = fakeTxId(0) + ':' + 0
|
|
|
|
output.pending = true
|
|
|
|
assert.deepEqual(wallet.getUnspentOutputs(), [])
|
|
|
|
})
|
2014-03-22 07:37:09 +01:00
|
|
|
})
|
2014-03-22 08:09:00 +01:00
|
|
|
|
|
|
|
describe('setUnspentOutputs', function(){
|
2014-03-31 05:47:47 +02:00
|
|
|
var utxo
|
2014-03-22 08:09:00 +01:00
|
|
|
beforeEach(function(){
|
2014-03-25 04:04:27 +01:00
|
|
|
utxo = cloneObject([expectedUtxo])
|
2014-03-22 08:09:00 +01:00
|
|
|
})
|
|
|
|
|
2014-05-21 05:18:33 +02:00
|
|
|
it('matches the expected behaviour', function(){
|
2014-03-22 08:09:00 +01:00
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
verifyOutputs()
|
|
|
|
})
|
|
|
|
|
2014-03-22 08:37:03 +01:00
|
|
|
describe('required fields', function(){
|
2014-05-21 05:18:33 +02:00
|
|
|
['outputIndex', 'address', 'hash', '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]
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
2014-04-02 06:43:34 +02:00
|
|
|
}, new RegExp('Invalid unspent output: key ' + field + ' is missing'))
|
2014-03-22 08:37:03 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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-06-12 09:42:10 +02:00
|
|
|
describe('Process transaction', function(){
|
2014-05-06 08:52:31 +02:00
|
|
|
var addresses
|
2014-03-31 05:47:47 +02:00
|
|
|
var tx
|
2014-03-22 14:10:56 +01:00
|
|
|
|
|
|
|
beforeEach(function(){
|
2014-05-06 08:52:31 +02:00
|
|
|
addresses = [
|
|
|
|
'115qa7iPZqn6as57hxLL8E9VUnhmGQxKWi',
|
|
|
|
'1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd',
|
|
|
|
'1BBjuhF2jHxu7tPinyQGCuaNhEs6f5u59u'
|
|
|
|
]
|
|
|
|
|
2014-05-08 02:44:35 +02:00
|
|
|
tx = Transaction.fromHex(fixtureTx1Hex)
|
2014-03-22 14:10:56 +01:00
|
|
|
})
|
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
describe("processPendingTx", function(){
|
2014-06-21 08:26:38 +02:00
|
|
|
it("incoming: sets the pending flag on output", function(){
|
2014-06-12 09:42:10 +02:00
|
|
|
wallet.addresses = [addresses[0]]
|
|
|
|
wallet.processPendingTx(tx)
|
2014-05-28 05:17:04 +02:00
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
verifyOutputAdded(0, true)
|
|
|
|
})
|
2014-06-21 08:26:38 +02:00
|
|
|
|
|
|
|
describe("when tx ins outpoint contains a known txhash:i", function(){
|
|
|
|
var spendTx
|
|
|
|
beforeEach(function(){
|
|
|
|
wallet.addresses = [addresses[0]]
|
|
|
|
wallet.processConfirmedTx(tx)
|
|
|
|
|
|
|
|
spendTx = Transaction.fromHex(fixtureTx2Hex)
|
|
|
|
})
|
|
|
|
|
2014-06-23 02:30:57 +02:00
|
|
|
it("outgoing: sets the pending flag and 'to' on output", function(){
|
2014-06-21 08:26:38 +02:00
|
|
|
var txIn = spendTx.ins[0]
|
|
|
|
var txInId = new Buffer(txIn.hash)
|
|
|
|
Array.prototype.reverse.call(txInId)
|
|
|
|
txInId = txInId.toString('hex')
|
|
|
|
|
|
|
|
var key = txInId + ':' + txIn.index
|
|
|
|
assert(!wallet.outputs[key].pending)
|
|
|
|
|
|
|
|
wallet.processPendingTx(spendTx)
|
|
|
|
assert(wallet.outputs[key].pending)
|
2014-06-23 02:30:57 +02:00
|
|
|
assert.equal(wallet.outputs[key].to, spendTx.getId() + ':' + 0)
|
2014-06-21 08:26:38 +02:00
|
|
|
})
|
|
|
|
})
|
2014-05-28 05:17:04 +02:00
|
|
|
})
|
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
describe('processConfirmedTx', function(){
|
|
|
|
it('does not fail on scripts with no corresponding Address', function() {
|
|
|
|
var pubKey = wallet.getPrivateKey(0).pub
|
2014-06-14 04:30:42 +02:00
|
|
|
var script = scripts.pubKeyOutput(pubKey)
|
2014-06-12 09:42:10 +02:00
|
|
|
var tx2 = new Transaction()
|
2014-06-14 16:03:17 +02:00
|
|
|
tx2.addInput(fakeTxId(1), 0)
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
// FIXME: Transaction doesn't support custom ScriptPubKeys... yet
|
|
|
|
// So for now, we hijack the script with our own, and undefine the cached address
|
|
|
|
tx2.addOutput(addresses[0], 10000)
|
|
|
|
tx2.outs[0].script = script
|
|
|
|
tx2.outs[0].address = undefined
|
|
|
|
|
|
|
|
wallet.processConfirmedTx(tx2)
|
2014-03-22 14:10:56 +01:00
|
|
|
})
|
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
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()
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
wallet.addresses = [addresses[0]]
|
|
|
|
wallet.processConfirmedTx(tx)
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
assert.equal(outputCount(), totalOuts + 1)
|
|
|
|
verifyOutputAdded(0, false)
|
|
|
|
})
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
it("works for change address", function(){
|
|
|
|
var totalOuts = outputCount()
|
|
|
|
wallet.changeAddresses = [addresses[1]]
|
2014-06-12 06:56:50 +02:00
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
wallet.processConfirmedTx(tx)
|
|
|
|
|
|
|
|
assert.equal(outputCount(), totalOuts + 1)
|
|
|
|
verifyOutputAdded(1, false)
|
2014-06-12 06:56:50 +02:00
|
|
|
})
|
2014-06-12 09:42:10 +02:00
|
|
|
|
|
|
|
function outputCount(){
|
|
|
|
return Object.keys(wallet.outputs).length
|
|
|
|
}
|
2014-06-12 06:56:50 +02:00
|
|
|
})
|
|
|
|
|
2014-06-12 10:47:27 +02:00
|
|
|
describe("when tx ins outpoint contains a known txhash:i", function(){
|
2014-06-16 05:58:16 +02:00
|
|
|
var spendTx
|
2014-06-12 09:42:10 +02:00
|
|
|
beforeEach(function(){
|
|
|
|
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
|
|
|
|
wallet.processConfirmedTx(tx)
|
|
|
|
|
2014-06-16 05:58:16 +02:00
|
|
|
spendTx = Transaction.fromHex(fixtureTx2Hex)
|
2014-06-12 09:42:10 +02:00
|
|
|
})
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
it("does not add to wallet.outputs", function(){
|
2014-06-16 05:58:16 +02:00
|
|
|
wallet.processConfirmedTx(spendTx)
|
|
|
|
assert.deepEqual(wallet.outputs, {})
|
2014-06-12 09:42:10 +02:00
|
|
|
})
|
2014-05-06 08:52:31 +02:00
|
|
|
|
2014-06-12 10:47:27 +02:00
|
|
|
it("deletes corresponding 'output'", function(){
|
2014-06-16 05:58:16 +02:00
|
|
|
var txIn = spendTx.ins[0]
|
2014-06-16 06:08:43 +02:00
|
|
|
var txInId = new Buffer(txIn.hash)
|
2014-06-16 05:58:16 +02:00
|
|
|
Array.prototype.reverse.call(txInId)
|
|
|
|
txInId = txInId.toString('hex')
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-06-16 06:08:43 +02:00
|
|
|
var expected = txInId + ':' + txIn.index
|
2014-06-16 05:58:16 +02:00
|
|
|
assert(expected in wallet.outputs)
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-06-16 05:58:16 +02:00
|
|
|
wallet.processConfirmedTx(spendTx)
|
|
|
|
assert(!(expected in wallet.outputs))
|
2014-06-12 09:42:10 +02:00
|
|
|
})
|
2014-03-22 14:10:56 +01:00
|
|
|
})
|
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
it("does nothing when none of the involved addresses belong to the wallet", function(){
|
|
|
|
wallet.processConfirmedTx(tx)
|
2014-06-16 05:58:16 +02:00
|
|
|
assert.deepEqual(wallet.outputs, {})
|
2014-03-22 14:10:56 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
function verifyOutputAdded(index, pending) {
|
|
|
|
var txOut = tx.outs[index]
|
2014-05-20 06:07:22 +02:00
|
|
|
var key = tx.getId() + ":" + index
|
2014-06-12 09:42:10 +02:00
|
|
|
var output = wallet.outputs[key]
|
2014-06-18 08:34:53 +02:00
|
|
|
assert.equal(output.from, key)
|
2014-06-12 09:42:10 +02:00
|
|
|
assert.equal(output.value, txOut.value)
|
|
|
|
assert.equal(output.pending, pending)
|
|
|
|
|
2014-06-14 04:30:42 +02:00
|
|
|
var txOutAddress = Address.fromOutputScript(txOut.script).toString()
|
2014-06-12 09:42:10 +02:00
|
|
|
assert.equal(output.address, txOutAddress)
|
|
|
|
}
|
2014-03-22 14:10:56 +01:00
|
|
|
})
|
|
|
|
|
2014-03-23 17:19:46 +01:00
|
|
|
describe('createTx', function(){
|
2014-03-31 05:47:47 +02: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 = [
|
|
|
|
{
|
2014-06-14 16:03:17 +02:00
|
|
|
"hash": fakeTxId(1),
|
2014-04-01 06:52:40 +02:00
|
|
|
"outputIndex": 0,
|
|
|
|
"address" : address1,
|
|
|
|
"value": 400000 // not enough for value
|
|
|
|
},
|
|
|
|
{
|
2014-06-14 16:03:17 +02:00
|
|
|
"hash": fakeTxId(2),
|
2014-04-01 06:52:40 +02:00
|
|
|
"outputIndex": 1,
|
|
|
|
"address" : address1,
|
|
|
|
"value": 500000 // enough for only value
|
|
|
|
},
|
|
|
|
{
|
2014-06-14 16:03:17 +02:00
|
|
|
"hash": fakeTxId(3),
|
2014-04-01 06:52:40 +02:00
|
|
|
"outputIndex": 0,
|
|
|
|
"address" : address2,
|
2014-06-17 08:36:57 +02:00
|
|
|
"value": 510000 // enough for value and fee
|
2014-04-01 06:52:40 +02:00
|
|
|
}
|
2014-03-23 17:19:46 +01:00
|
|
|
]
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
})
|
|
|
|
|
2014-06-18 15:16:17 +02:00
|
|
|
describe('transaction fee', function(){
|
2014-03-23 17:19:46 +01:00
|
|
|
it('allows fee to be specified', function(){
|
|
|
|
var fee = 30000
|
|
|
|
var tx = wallet.createTx(to, value, fee)
|
|
|
|
|
2014-06-18 15:16:17 +02:00
|
|
|
assert.equal(getFee(wallet, tx), fee)
|
2014-03-23 17:19:46 +01:00
|
|
|
})
|
2014-03-25 03:35:33 +01:00
|
|
|
|
2014-03-26 13:20:58 +01:00
|
|
|
it('allows fee to be set to zero', function(){
|
2014-06-17 08:36:57 +02:00
|
|
|
value = 510000
|
2014-03-26 13:20:58 +01:00
|
|
|
var fee = 0
|
|
|
|
var tx = wallet.createTx(to, value, fee)
|
|
|
|
|
2014-06-18 15:16:17 +02:00
|
|
|
assert.equal(getFee(wallet, tx), fee)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not overestimate fees when network has dustSoftThreshold', function(){
|
|
|
|
var wallet = new Wallet(seed, networks.litecoin)
|
|
|
|
var address = wallet.generateAddress()
|
|
|
|
wallet.setUnspentOutputs([{
|
|
|
|
hash: fakeTxId(0),
|
|
|
|
outputIndex: 0,
|
|
|
|
address: address,
|
|
|
|
value: 500000
|
|
|
|
}])
|
|
|
|
|
|
|
|
value = 200000
|
|
|
|
var tx = wallet.createTx(address, value)
|
|
|
|
|
|
|
|
assert.equal(getFee(wallet, tx), 100000)
|
|
|
|
})
|
|
|
|
|
|
|
|
function getFee(wallet, tx) {
|
|
|
|
var inputValue = tx.ins.reduce(function(memo, input){
|
|
|
|
var id = Array.prototype.reverse.call(input.hash).toString('hex')
|
|
|
|
return memo + wallet.outputs[id + ':' + input.index].value
|
|
|
|
}, 0)
|
|
|
|
|
|
|
|
return tx.outs.reduce(function(memo, output){
|
|
|
|
return memo - output.value
|
|
|
|
}, inputValue)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('choosing utxo', function(){
|
|
|
|
it('takes fees into account', function(){
|
|
|
|
var tx = wallet.createTx(to, value)
|
|
|
|
|
2014-03-26 13:20:58 +01:00
|
|
|
assert.equal(tx.ins.length, 1)
|
2014-06-16 06:08:43 +02:00
|
|
|
assert.deepEqual(tx.ins[0].hash, fakeTxHash(3))
|
|
|
|
assert.equal(tx.ins[0].index, 0)
|
2014-03-26 13:20:58 +01:00
|
|
|
})
|
|
|
|
|
2014-06-12 07:11:28 +02:00
|
|
|
it('ignores pending outputs', function(){
|
|
|
|
utxo.push(
|
|
|
|
{
|
2014-06-14 16:03:17 +02:00
|
|
|
"hash": fakeTxId(4),
|
2014-06-12 07:11:28 +02:00
|
|
|
"outputIndex": 0,
|
|
|
|
"address" : address2,
|
|
|
|
"value": 530000,
|
|
|
|
"pending": true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
wallet.setUnspentOutputs(utxo)
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, value)
|
|
|
|
|
|
|
|
assert.equal(tx.ins.length, 1)
|
2014-06-16 06:08:43 +02:00
|
|
|
assert.deepEqual(tx.ins[0].hash, fakeTxHash(3))
|
|
|
|
assert.equal(tx.ins[0].index, 0)
|
2014-06-12 07:11:28 +02:00
|
|
|
})
|
2014-03-23 17:19:46 +01:00
|
|
|
})
|
|
|
|
|
2014-06-18 15:16:17 +02:00
|
|
|
describe('works for testnet', function(){
|
2014-04-25 22:43:26 +02:00
|
|
|
it('should create transaction', function(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-05-20 07:11:17 +02:00
|
|
|
var address = wallet.generateAddress()
|
2014-05-06 06:12:37 +02:00
|
|
|
|
2014-05-20 07:11:17 +02:00
|
|
|
wallet.setUnspentOutputs([{
|
2014-06-14 16:03:17 +02:00
|
|
|
hash: fakeTxId(0),
|
2014-05-20 07:11:17 +02:00
|
|
|
outputIndex: 0,
|
|
|
|
address: address,
|
|
|
|
value: value
|
|
|
|
}])
|
|
|
|
|
|
|
|
var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
2014-06-17 08:36:57 +02:00
|
|
|
var toValue = value - 10000
|
2014-05-20 07:11:17 +02:00
|
|
|
|
|
|
|
var tx = wallet.createTx(to, toValue)
|
2014-04-25 22:43:26 +02:00
|
|
|
assert.equal(tx.outs.length, 1)
|
2014-05-21 03:38:03 +02:00
|
|
|
|
|
|
|
var outAddress = Address.fromOutputScript(tx.outs[0].script, networks.testnet)
|
|
|
|
assert.equal(outAddress.toString(), to)
|
2014-05-20 07:11:17 +02:00
|
|
|
assert.equal(tx.outs[0].value, toValue)
|
2014-04-25 22:43:26 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('changeAddress', function(){
|
|
|
|
it('should allow custom changeAddress', function(){
|
2014-05-30 10:41:03 +02:00
|
|
|
var wallet = new Wallet(seed, networks.testnet)
|
2014-04-26 01:30:36 +02:00
|
|
|
var address = wallet.generateAddress()
|
2014-05-06 06:10:25 +02:00
|
|
|
|
|
|
|
wallet.setUnspentOutputs([{
|
2014-06-14 16:03:17 +02:00
|
|
|
hash: fakeTxId(0),
|
2014-05-06 06:10:25 +02:00
|
|
|
outputIndex: 0,
|
|
|
|
address: address,
|
|
|
|
value: value
|
|
|
|
}])
|
|
|
|
assert.equal(wallet.getBalance(), value)
|
|
|
|
|
2014-04-25 22:43:26 +02:00
|
|
|
var changeAddress = 'mfrFjnKZUvTcvdAK2fUX5D8v1Epu5H8JCk'
|
2014-05-06 06:10:25 +02:00
|
|
|
var to = 'mt7MyTVVEWnbwpF5hBn6fgnJcv95Syk2ue'
|
|
|
|
var toValue = value / 2
|
|
|
|
var fee = 1e3
|
|
|
|
|
|
|
|
var tx = wallet.createTx(to, toValue, fee, changeAddress)
|
2014-04-26 01:30:36 +02:00
|
|
|
assert.equal(tx.outs.length, 2)
|
2014-05-21 03:38:03 +02:00
|
|
|
|
|
|
|
var outAddress0 = Address.fromOutputScript(tx.outs[0].script, networks.testnet)
|
|
|
|
var outAddress1 = Address.fromOutputScript(tx.outs[1].script, networks.testnet)
|
|
|
|
|
|
|
|
assert.equal(outAddress0.toString(), to)
|
2014-05-06 06:10:25 +02:00
|
|
|
assert.equal(tx.outs[0].value, toValue)
|
|
|
|
|
2014-05-21 03:38:03 +02:00
|
|
|
assert.equal(outAddress1.toString(), changeAddress)
|
2014-05-06 06:10:25 +02:00
|
|
|
assert.equal(tx.outs[1].value, value - (toValue + fee))
|
2014-04-25 22:43:26 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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]
|
2014-05-21 03:38:03 +02:00
|
|
|
var outAddress = Address.fromOutputScript(out.script)
|
|
|
|
|
|
|
|
assert.equal(outAddress.toString(), to)
|
2014-03-23 17:19:46 +01:00
|
|
|
assert.equal(out.value, value)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('change', function(){
|
|
|
|
it('uses the last change address if there is any', function(){
|
2014-06-17 08:36:57 +02:00
|
|
|
var fee = 0
|
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]
|
2014-05-21 03:38:03 +02:00
|
|
|
var outAddress = Address.fromOutputScript(out.script)
|
|
|
|
|
|
|
|
assert.equal(outAddress.toString(), wallet.changeAddresses[1])
|
2014-06-17 08:36:57 +02:00
|
|
|
assert.equal(out.value, 10000)
|
2014-03-23 17:19:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('generates a change address if there is not any', function(){
|
2014-06-17 08:36:57 +02:00
|
|
|
var fee = 0
|
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]
|
2014-05-21 03:38:03 +02:00
|
|
|
var outAddress = Address.fromOutputScript(out.script)
|
|
|
|
|
|
|
|
assert.equal(outAddress.toString(), wallet.changeAddresses[0])
|
2014-06-17 08:36:57 +02:00
|
|
|
assert.equal(out.value, 10000)
|
2014-03-23 19:42:41 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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-06-17 08:36:57 +02:00
|
|
|
var value = 546
|
2014-03-23 18:42:36 +01:00
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.createTx(to, value)
|
2014-06-17 08:36:57 +02:00
|
|
|
}, /546 must be above dust threshold \(546 Satoshis\)/)
|
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)
|
2014-06-17 08:36:57 +02:00
|
|
|
}, /Not enough funds \(incl. fee\): 1410000 < 1410001/)
|
2014-03-23 19:19:17 +01:00
|
|
|
})
|
|
|
|
})
|
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
|
|
|
})
|