2014-05-31 04:16:01 +02:00
|
|
|
var assert = require('assert')
|
2014-05-05 05:23:22 +02:00
|
|
|
var networks = require('./networks')
|
2014-05-06 08:52:31 +02:00
|
|
|
var rng = require('secure-random')
|
2014-05-30 10:41:03 +02:00
|
|
|
|
|
|
|
var Address = require('./address')
|
2014-06-03 09:08:42 +02:00
|
|
|
var HDNode = require('./hdnode')
|
2014-05-16 09:12:39 +02:00
|
|
|
var Transaction = require('./transaction')
|
2013-11-20 19:00:49 +01:00
|
|
|
|
2014-05-30 10:41:03 +02:00
|
|
|
function Wallet(seed, network) {
|
|
|
|
network = network || networks.bitcoin
|
2014-03-31 05:47:47 +02:00
|
|
|
|
|
|
|
// Stored in a closure to make accidental serialization less likely
|
|
|
|
var masterkey = null
|
|
|
|
var me = this
|
|
|
|
var accountZero = null
|
|
|
|
var internalAccount = null
|
|
|
|
var externalAccount = null
|
|
|
|
|
|
|
|
// Addresses
|
|
|
|
this.addresses = []
|
|
|
|
this.changeAddresses = []
|
|
|
|
|
2014-05-20 08:17:44 +02:00
|
|
|
// Dust value
|
|
|
|
this.dustThreshold = 5430
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
// Transaction output data
|
|
|
|
this.outputs = {}
|
|
|
|
|
|
|
|
// Make a new master key
|
2014-05-30 10:41:03 +02:00
|
|
|
this.newMasterKey = function(seed) {
|
2014-05-10 01:41:00 +02:00
|
|
|
seed = seed || new Buffer(rng(32))
|
2014-05-31 12:16:42 +02:00
|
|
|
masterkey = HDNode.fromSeedBuffer(seed, network)
|
2014-03-31 05:47:47 +02:00
|
|
|
|
2014-06-03 08:14:15 +02:00
|
|
|
// HD first-level child derivation method should be hardened
|
2014-03-31 05:47:47 +02:00
|
|
|
// See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
|
2014-06-03 08:14:15 +02:00
|
|
|
accountZero = masterkey.deriveHardened(0)
|
2014-03-31 05:47:47 +02:00
|
|
|
externalAccount = accountZero.derive(0)
|
|
|
|
internalAccount = accountZero.derive(1)
|
|
|
|
|
|
|
|
me.addresses = []
|
|
|
|
me.changeAddresses = []
|
|
|
|
|
|
|
|
me.outputs = {}
|
|
|
|
}
|
2014-05-30 10:41:03 +02:00
|
|
|
|
|
|
|
this.newMasterKey(seed)
|
2014-03-31 05:47:47 +02:00
|
|
|
|
|
|
|
this.generateAddress = function() {
|
|
|
|
var key = externalAccount.derive(this.addresses.length)
|
|
|
|
this.addresses.push(key.getAddress().toString())
|
|
|
|
return this.addresses[this.addresses.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
this.generateChangeAddress = function() {
|
|
|
|
var key = internalAccount.derive(this.changeAddresses.length)
|
|
|
|
this.changeAddresses.push(key.getAddress().toString())
|
|
|
|
return this.changeAddresses[this.changeAddresses.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getBalance = function() {
|
|
|
|
return this.getUnspentOutputs().reduce(function(memo, output){
|
|
|
|
return memo + output.value
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getUnspentOutputs = function() {
|
|
|
|
var utxo = []
|
|
|
|
|
|
|
|
for(var key in this.outputs){
|
|
|
|
var output = this.outputs[key]
|
2014-06-12 10:47:27 +02:00
|
|
|
utxo.push(outputToUnspentOutput(output))
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2014-01-16 10:06:58 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return utxo
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setUnspentOutputs = function(utxo) {
|
|
|
|
var outputs = {}
|
|
|
|
|
|
|
|
utxo.forEach(function(uo){
|
|
|
|
validateUnspentOutput(uo)
|
|
|
|
var o = unspentOutputToOutput(uo)
|
|
|
|
outputs[o.receive] = o
|
|
|
|
})
|
|
|
|
|
|
|
|
this.outputs = outputs
|
|
|
|
}
|
|
|
|
|
|
|
|
function outputToUnspentOutput(output){
|
|
|
|
var hashAndIndex = output.receive.split(":")
|
2014-03-22 07:37:09 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return {
|
|
|
|
hash: hashAndIndex[0],
|
|
|
|
outputIndex: parseInt(hashAndIndex[1]),
|
|
|
|
address: output.address,
|
|
|
|
value: output.value
|
2014-03-22 07:37:09 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function unspentOutputToOutput(o) {
|
2014-05-21 05:18:33 +02:00
|
|
|
var hash = o.hash
|
2014-03-31 05:47:47 +02:00
|
|
|
var key = hash + ":" + o.outputIndex
|
|
|
|
return {
|
|
|
|
receive: key,
|
|
|
|
address: o.address,
|
2014-06-12 07:11:28 +02:00
|
|
|
value: o.value,
|
|
|
|
pending: o.pending
|
2014-03-22 08:09:00 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-03-22 08:09:00 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
function validateUnspentOutput(uo) {
|
|
|
|
var missingField
|
2014-03-25 06:22:54 +01:00
|
|
|
|
2014-05-21 05:18:33 +02:00
|
|
|
if (isNullOrUndefined(uo.hash)) {
|
|
|
|
missingField = "hash"
|
2014-03-22 08:43:55 +01:00
|
|
|
}
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var requiredKeys = ['outputIndex', 'address', 'value']
|
2014-04-01 21:03:41 +02:00
|
|
|
requiredKeys.forEach(function (key) {
|
|
|
|
if (isNullOrUndefined(uo[key])){
|
2014-03-31 05:47:47 +02:00
|
|
|
missingField = key
|
2014-03-22 08:37:03 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
|
|
|
|
2014-04-01 21:03:41 +02:00
|
|
|
if (missingField) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var message = [
|
2014-04-01 21:03:41 +02:00
|
|
|
'Invalid unspent output: key', missingField, 'is missing.',
|
2014-03-31 05:47:47 +02:00
|
|
|
'A valid unspent output must contain'
|
|
|
|
]
|
|
|
|
message.push(requiredKeys.join(', '))
|
2014-05-21 05:18:33 +02:00
|
|
|
message.push("and hash")
|
2014-03-31 05:47:47 +02:00
|
|
|
throw new Error(message.join(' '))
|
2014-03-22 08:37:03 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-01 21:03:41 +02:00
|
|
|
function isNullOrUndefined(value) {
|
2014-03-31 05:47:47 +02:00
|
|
|
return value == undefined
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:42:10 +02:00
|
|
|
this.processPendingTx = function(tx){
|
|
|
|
processTx(tx, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.processConfirmedTx = function(tx){
|
|
|
|
processTx(tx, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
function processTx(tx, isPending) {
|
2014-05-20 06:07:22 +02:00
|
|
|
var txid = tx.getId()
|
2014-03-31 05:47:47 +02:00
|
|
|
|
2014-05-20 06:07:22 +02:00
|
|
|
tx.outs.forEach(function(txOut, i) {
|
2014-05-06 08:52:31 +02:00
|
|
|
var address
|
|
|
|
|
|
|
|
try {
|
2014-06-13 03:30:07 +02:00
|
|
|
address = Address.fromOutputScript(txOut.script, network).toString()
|
2014-05-06 08:52:31 +02:00
|
|
|
} catch(e) {
|
2014-05-28 05:17:04 +02:00
|
|
|
if (!(e.message.match(/has no matching Address/))) throw e
|
2014-05-06 08:52:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
if (isMyAddress(address)) {
|
2014-05-20 06:07:22 +02:00
|
|
|
var output = txid + ':' + i
|
2014-05-06 08:52:31 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
me.outputs[output] = {
|
|
|
|
receive: output,
|
|
|
|
value: txOut.value,
|
|
|
|
address: address,
|
2014-06-12 06:56:50 +02:00
|
|
|
pending: isPending
|
2014-03-22 08:37:03 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-06-14 16:03:17 +02:00
|
|
|
tx.ins.forEach(function(txIn, i) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var op = txIn.outpoint
|
2014-06-14 16:03:17 +02:00
|
|
|
|
|
|
|
// copy and convert to big-endian hex
|
|
|
|
var txinHash = new Buffer(op.hash)
|
|
|
|
Array.prototype.reverse.call(txinHash)
|
|
|
|
txinHash = txinHash.toString('hex')
|
|
|
|
|
|
|
|
var output = txinHash + ':' + op.index
|
2014-05-21 05:18:33 +02:00
|
|
|
|
2014-06-12 10:47:27 +02:00
|
|
|
if(me.outputs[output]) delete me.outputs[output]
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
|
|
|
}
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-04-25 21:49:51 +02:00
|
|
|
this.createTx = function(to, value, fixedFee, changeAddress) {
|
2014-05-31 04:16:01 +02:00
|
|
|
assert(value > this.dustThreshold, value + ' must be above dust threshold (' + this.dustThreshold + ' Satoshis)')
|
2014-05-20 07:11:17 +02:00
|
|
|
|
|
|
|
var utxos = getCandidateOutputs(value)
|
|
|
|
var accum = 0
|
|
|
|
var subTotal = value
|
2014-06-14 16:04:02 +02:00
|
|
|
var addresses = []
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var tx = new Transaction()
|
2014-05-06 08:52:31 +02:00
|
|
|
tx.addOutput(to, value)
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-05-20 07:11:17 +02:00
|
|
|
for (var i = 0; i < utxos.length; ++i) {
|
|
|
|
var utxo = utxos[i]
|
2014-06-14 16:04:02 +02:00
|
|
|
addresses.push(utxo.address)
|
2014-03-23 19:19:17 +01:00
|
|
|
|
2014-05-19 01:31:16 +02:00
|
|
|
var outpoint = utxo.receive.split(':')
|
|
|
|
tx.addInput(outpoint[0], parseInt(outpoint[1]))
|
2014-03-23 18:29:10 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var fee = fixedFee == undefined ? estimateFeePadChangeOutput(tx) : fixedFee
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-05-19 01:31:16 +02:00
|
|
|
accum += utxo.value
|
2014-05-20 07:11:17 +02:00
|
|
|
subTotal = value + fee
|
|
|
|
if (accum >= subTotal) {
|
|
|
|
var change = accum - subTotal
|
|
|
|
|
2014-05-20 08:17:44 +02:00
|
|
|
if (change > this.dustThreshold) {
|
2014-05-20 07:11:17 +02:00
|
|
|
tx.addOutput(changeAddress || getChangeAddress(), change)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
2014-03-25 06:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 04:16:01 +02:00
|
|
|
assert(accum >= subTotal, 'Not enough funds (incl. fee): ' + accum + ' < ' + subTotal)
|
2014-03-23 18:42:36 +01:00
|
|
|
|
2014-06-14 16:04:02 +02:00
|
|
|
this.signWith(tx, addresses)
|
2014-03-31 05:47:47 +02:00
|
|
|
return tx
|
|
|
|
}
|
2014-03-23 19:49:40 +01:00
|
|
|
|
2014-06-04 06:07:29 +02:00
|
|
|
function getCandidateOutputs() {
|
2014-03-31 05:47:47 +02:00
|
|
|
var unspent = []
|
2014-06-04 06:07:29 +02:00
|
|
|
|
|
|
|
for (var key in me.outputs) {
|
2014-03-31 05:47:47 +02:00
|
|
|
var output = me.outputs[key]
|
2014-06-12 10:47:27 +02:00
|
|
|
if (!output.pending) unspent.push(output)
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2013-03-02 18:00:14 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var sortByValueDesc = unspent.sort(function(o1, o2){
|
|
|
|
return o2.value - o1.value
|
|
|
|
})
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return sortByValueDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
function estimateFeePadChangeOutput(tx){
|
|
|
|
var tmpTx = tx.clone()
|
2014-05-06 08:52:31 +02:00
|
|
|
tmpTx.addOutput(getChangeAddress(), 0)
|
2014-03-31 05:47:47 +02:00
|
|
|
return tmpTx.estimateFee()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChangeAddress() {
|
|
|
|
if(me.changeAddresses.length === 0) me.generateChangeAddress();
|
|
|
|
return me.changeAddresses[me.changeAddresses.length - 1]
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-06-14 16:04:02 +02:00
|
|
|
this.signWith = function(tx, addresses) {
|
|
|
|
assert.equal(tx.ins.length, addresses.length, 'Number of addresses must match number of transaction inputs')
|
|
|
|
|
|
|
|
addresses.forEach(function(address, i) {
|
|
|
|
var key = me.getPrivateKeyForAddress(address)
|
|
|
|
|
|
|
|
tx.sign(i, key)
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-06-14 16:04:02 +02:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getMasterKey = function() { return masterkey }
|
|
|
|
this.getAccountZero = function() { return accountZero }
|
|
|
|
this.getInternalAccount = function() { return internalAccount }
|
|
|
|
this.getExternalAccount = function() { return externalAccount }
|
|
|
|
|
|
|
|
this.getPrivateKey = function(index) {
|
2014-06-03 09:30:05 +02:00
|
|
|
return externalAccount.derive(index).privKey
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.getInternalPrivateKey = function(index) {
|
2014-06-03 09:30:05 +02:00
|
|
|
return internalAccount.derive(index).privKey
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.getPrivateKeyForAddress = function(address) {
|
|
|
|
var index
|
|
|
|
if((index = this.addresses.indexOf(address)) > -1) {
|
|
|
|
return this.getPrivateKey(index)
|
|
|
|
} else if((index = this.changeAddresses.indexOf(address)) > -1) {
|
|
|
|
return this.getInternalPrivateKey(index)
|
|
|
|
} else {
|
|
|
|
throw new Error('Unknown address. Make sure the address is from the keychain and has been generated.')
|
2014-03-13 12:05:55 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
function isReceiveAddress(address){
|
|
|
|
return me.addresses.indexOf(address) > -1
|
|
|
|
}
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
function isChangeAddress(address){
|
|
|
|
return me.changeAddresses.indexOf(address) > -1
|
|
|
|
}
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
function isMyAddress(address) {
|
|
|
|
return isReceiveAddress(address) || isChangeAddress(address)
|
|
|
|
}
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
module.exports = Wallet
|