2014-05-31 12:16:01 +10:00
|
|
|
var assert = require('assert')
|
2014-07-28 13:46:34 +10:00
|
|
|
var crypto = require('crypto')
|
2014-05-05 13:23:22 +10:00
|
|
|
var networks = require('./networks')
|
2014-05-30 18:41:03 +10:00
|
|
|
|
|
|
|
var Address = require('./address')
|
2014-06-03 17:08:42 +10:00
|
|
|
var HDNode = require('./hdnode')
|
2014-05-16 17:12:39 +10:00
|
|
|
var Transaction = require('./transaction')
|
2014-08-13 12:17:12 +10:00
|
|
|
var Script = require('./script')
|
2013-11-20 13:00:49 -05:00
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
function Wallet(seed, network, unspents) {
|
2014-08-14 11:00:18 +10:00
|
|
|
seed = seed || crypto.randomBytes(32)
|
2014-05-30 18:41:03 +10:00
|
|
|
network = network || networks.bitcoin
|
2014-03-31 11:47:47 +08:00
|
|
|
|
|
|
|
// Stored in a closure to make accidental serialization less likely
|
2014-08-14 17:29:14 +10:00
|
|
|
var masterKey = HDNode.fromSeedBuffer(seed, network)
|
2014-03-31 11:47:47 +08:00
|
|
|
var me = this
|
2014-08-14 11:00:18 +10:00
|
|
|
|
|
|
|
// HD first-level child derivation method should be hardened
|
|
|
|
// See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
|
2014-08-14 17:29:14 +10:00
|
|
|
var accountZero = masterKey.deriveHardened(0)
|
2014-08-14 11:00:18 +10:00
|
|
|
var externalAccount = accountZero.derive(0)
|
|
|
|
var internalAccount = accountZero.derive(1)
|
2014-03-31 11:47:47 +08:00
|
|
|
|
|
|
|
// Addresses
|
|
|
|
this.addresses = []
|
|
|
|
this.changeAddresses = []
|
|
|
|
|
2014-08-14 10:39:14 +10:00
|
|
|
this.network = network
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
// Transaction output data
|
2014-08-14 11:03:54 +10:00
|
|
|
this.outputs = unspents ? processUnspentOutputs(unspents) : {}
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2014-08-14 11:00:18 +10:00
|
|
|
// FIXME: remove in 2.x.y
|
|
|
|
this.newMasterKey = function(seed) {
|
|
|
|
console.warn('newMasterKey is deprecated, please make a new Wallet instance instead')
|
|
|
|
|
|
|
|
seed = seed || crypto.randomBytes(32)
|
2014-08-14 17:29:14 +10:00
|
|
|
masterKey = HDNode.fromSeedBuffer(seed, network)
|
2014-08-14 11:00:18 +10:00
|
|
|
|
|
|
|
// HD first-level child derivation method should be hardened
|
|
|
|
// See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
|
2014-08-14 17:29:14 +10:00
|
|
|
accountZero = masterKey.deriveHardened(0)
|
2014-08-14 11:00:18 +10:00
|
|
|
externalAccount = accountZero.derive(0)
|
|
|
|
internalAccount = accountZero.derive(1)
|
|
|
|
|
|
|
|
me.addresses = []
|
|
|
|
me.changeAddresses = []
|
|
|
|
|
|
|
|
me.outputs = {}
|
|
|
|
}
|
|
|
|
|
2014-08-14 17:29:14 +10:00
|
|
|
this.getMasterKey = function() { return masterKey }
|
2014-03-31 11:47:47 +08:00
|
|
|
this.getAccountZero = function() { return accountZero }
|
|
|
|
this.getExternalAccount = function() { return externalAccount }
|
2014-08-14 17:29:14 +10:00
|
|
|
this.getInternalAccount = function() { return internalAccount }
|
2014-03-31 11:47:47 +08:00
|
|
|
|
|
|
|
this.getPrivateKeyForAddress = function(address) {
|
2014-08-16 14:19:19 +08:00
|
|
|
var myAddresses = this.addresses.concat(this.changeAddresses)
|
|
|
|
assert(includeAddress(myAddresses, address),
|
|
|
|
'Unknown address. Make sure the address is from the keychain and has been generated')
|
2014-08-13 13:56:17 +10:00
|
|
|
|
2014-08-16 14:19:19 +08:00
|
|
|
if (includeAddress(this.addresses, address)) {
|
2014-08-13 13:56:17 +10:00
|
|
|
var index = this.addresses.indexOf(address)
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
return this.getPrivateKey(index)
|
2014-08-13 13:56:17 +10:00
|
|
|
}
|
|
|
|
|
2014-08-16 14:19:19 +08:00
|
|
|
if (includeAddress(this.changeAddresses, address)) {
|
2014-08-13 13:56:17 +10:00
|
|
|
var index = this.changeAddresses.indexOf(address)
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
return this.getInternalPrivateKey(index)
|
2014-03-13 19:05:55 +08:00
|
|
|
}
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-17 00:39:15 -05:00
|
|
|
|
2014-08-14 10:39:14 +10:00
|
|
|
Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
|
|
|
|
assert(value > this.network.dustThreshold, value + ' must be above dust threshold (' + this.network.dustThreshold + ' Satoshis)')
|
|
|
|
|
|
|
|
var utxos = getCandidateOutputs(this.outputs, value)
|
|
|
|
var accum = 0
|
|
|
|
var subTotal = value
|
|
|
|
var addresses = []
|
|
|
|
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(to, value)
|
|
|
|
|
|
|
|
for (var i = 0; i < utxos.length; ++i) {
|
|
|
|
var utxo = utxos[i]
|
|
|
|
addresses.push(utxo.address)
|
|
|
|
|
|
|
|
var outpoint = utxo.from.split(':')
|
|
|
|
tx.addInput(outpoint[0], parseInt(outpoint[1]))
|
|
|
|
|
|
|
|
var fee = fixedFee == undefined ? estimatePaddedFee(tx, this.network) : fixedFee
|
|
|
|
|
|
|
|
accum += utxo.value
|
|
|
|
subTotal = value + fee
|
|
|
|
if (accum >= subTotal) {
|
|
|
|
var change = accum - subTotal
|
|
|
|
|
|
|
|
if (change > this.network.dustThreshold) {
|
|
|
|
tx.addOutput(changeAddress || this.getChangeAddress(), change)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(accum >= subTotal, 'Not enough funds (incl. fee): ' + accum + ' < ' + subTotal)
|
|
|
|
|
|
|
|
this.signWith(tx, addresses)
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2014-08-16 14:19:19 +08:00
|
|
|
Wallet.prototype.processPendingTx = function(tx){
|
|
|
|
processTx.bind(this)(tx, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.processConfirmedTx = function(tx){
|
|
|
|
processTx.bind(this)(tx, false)
|
|
|
|
}
|
|
|
|
|
2014-08-14 10:27:14 +10:00
|
|
|
Wallet.prototype.generateAddress = function() {
|
|
|
|
var k = this.addresses.length
|
|
|
|
var address = this.getExternalAccount().derive(k).getAddress()
|
|
|
|
|
|
|
|
this.addresses.push(address.toString())
|
|
|
|
|
|
|
|
return this.getReceiveAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.generateChangeAddress = function() {
|
|
|
|
var k = this.changeAddresses.length
|
|
|
|
var address = this.getInternalAccount().derive(k).getAddress()
|
|
|
|
|
|
|
|
this.changeAddresses.push(address.toString())
|
|
|
|
|
|
|
|
return this.getChangeAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.getBalance = function() {
|
|
|
|
return this.getUnspentOutputs().reduce(function(accum, output) {
|
|
|
|
return accum + output.value
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.getChangeAddress = function() {
|
|
|
|
if (this.changeAddresses.length === 0) {
|
|
|
|
this.generateChangeAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.changeAddresses[this.changeAddresses.length - 1]
|
|
|
|
}
|
|
|
|
|
2014-08-14 10:39:14 +10:00
|
|
|
Wallet.prototype.getInternalPrivateKey = function(index) {
|
|
|
|
return this.getInternalAccount().derive(index).privKey
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.getPrivateKey = function(index) {
|
|
|
|
return this.getExternalAccount().derive(index).privKey
|
|
|
|
}
|
|
|
|
|
2014-08-14 10:27:14 +10:00
|
|
|
Wallet.prototype.getReceiveAddress = function() {
|
|
|
|
if (this.addresses.length === 0) {
|
|
|
|
this.generateAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.addresses[this.addresses.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.getUnspentOutputs = function() {
|
|
|
|
var utxo = []
|
|
|
|
|
|
|
|
for(var key in this.outputs){
|
|
|
|
var output = this.outputs[key]
|
|
|
|
if(!output.to) utxo.push(outputToUnspentOutput(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
return utxo
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.setUnspentOutputs = function(utxo) {
|
|
|
|
console.warn('setUnspentOutputs is deprecated, please use the constructor option instead')
|
|
|
|
|
|
|
|
this.outputs = processUnspentOutputs(utxo)
|
|
|
|
}
|
|
|
|
|
|
|
|
Wallet.prototype.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 = this.getPrivateKeyForAddress(address)
|
|
|
|
|
|
|
|
tx.sign(i, key)
|
|
|
|
}, this)
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2014-08-13 12:03:53 +10:00
|
|
|
function outputToUnspentOutput(output){
|
|
|
|
var hashAndIndex = output.from.split(":")
|
|
|
|
|
|
|
|
return {
|
|
|
|
hash: hashAndIndex[0],
|
2014-08-14 11:03:54 +10:00
|
|
|
index: parseInt(hashAndIndex[1]),
|
2014-08-13 12:03:53 +10:00
|
|
|
address: output.address,
|
|
|
|
value: output.value,
|
|
|
|
pending: output.pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
function estimatePaddedFee(tx, network) {
|
|
|
|
var tmpTx = tx.clone()
|
|
|
|
tmpTx.addOutput(Script.EMPTY, network.dustSoftThreshold || 0)
|
|
|
|
|
|
|
|
return network.estimateFee(tmpTx)
|
2014-08-13 12:03:53 +10:00
|
|
|
}
|
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
function processUnspentOutputs(utxos) {
|
|
|
|
var outputs = {}
|
2014-08-13 12:03:53 +10:00
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
utxos.forEach(function(utxo){
|
|
|
|
var hash = new Buffer(utxo.hash, 'hex')
|
|
|
|
var index = utxo.index
|
|
|
|
var address = utxo.address
|
|
|
|
var value = utxo.value
|
2014-08-13 12:03:53 +10:00
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
// FIXME: remove alternative in 2.x.y
|
|
|
|
if (index === undefined) index = utxo.outputIndex
|
2014-08-13 12:03:53 +10:00
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length)
|
|
|
|
assert.equal(typeof index, 'number', 'Expected number index, got ' + index)
|
|
|
|
assert.doesNotThrow(function() { Address.fromBase58Check(address) }, 'Expected Base58 Address, got ' + address)
|
|
|
|
assert.equal(typeof value, 'number', 'Expected number value, got ' + value)
|
2014-08-13 12:03:53 +10:00
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
var key = utxo.hash + ':' + utxo.index
|
2014-08-13 12:17:12 +10:00
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
outputs[key] = {
|
|
|
|
from: key,
|
|
|
|
address: address,
|
|
|
|
value: value,
|
|
|
|
pending: utxo.pending
|
|
|
|
}
|
|
|
|
})
|
2014-08-13 12:17:12 +10:00
|
|
|
|
2014-08-14 11:03:54 +10:00
|
|
|
return outputs
|
2014-08-13 12:03:53 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCandidateOutputs(outputs/*, value*/) {
|
|
|
|
var unspent = []
|
|
|
|
|
|
|
|
for (var key in outputs) {
|
|
|
|
var output = outputs[key]
|
|
|
|
if (!output.pending) unspent.push(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sortByValueDesc = unspent.sort(function(o1, o2){
|
|
|
|
return o2.value - o1.value
|
|
|
|
})
|
|
|
|
|
|
|
|
return sortByValueDesc
|
|
|
|
}
|
|
|
|
|
2014-08-16 14:19:19 +08:00
|
|
|
function processTx(tx, isPending) {
|
|
|
|
var txid = tx.getId()
|
|
|
|
|
|
|
|
tx.outs.forEach(function(txOut, i) {
|
|
|
|
var address
|
|
|
|
|
|
|
|
try {
|
|
|
|
address = Address.fromOutputScript(txOut.script, this.network).toString()
|
|
|
|
} catch(e) {
|
|
|
|
if (!(e.message.match(/has no matching Address/))) throw e
|
|
|
|
}
|
|
|
|
|
|
|
|
var myAddresses = this.addresses.concat(this.changeAddresses)
|
|
|
|
if (includeAddress(myAddresses, address)) {
|
|
|
|
var output = txid + ':' + i
|
|
|
|
|
|
|
|
this.outputs[output] = {
|
|
|
|
from: output,
|
|
|
|
value: txOut.value,
|
|
|
|
address: address,
|
|
|
|
pending: isPending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this)
|
|
|
|
|
|
|
|
tx.ins.forEach(function(txIn, i) {
|
|
|
|
// copy and convert to big-endian hex
|
|
|
|
var txinId = new Buffer(txIn.hash)
|
|
|
|
Array.prototype.reverse.call(txinId)
|
|
|
|
txinId = txinId.toString('hex')
|
|
|
|
|
|
|
|
var output = txinId + ':' + txIn.index
|
|
|
|
|
|
|
|
if (!(output in this.outputs)) return
|
|
|
|
|
|
|
|
if (isPending) {
|
|
|
|
this.outputs[output].to = txid + ':' + i
|
|
|
|
this.outputs[output].pending = true
|
|
|
|
} else {
|
|
|
|
delete this.outputs[output]
|
|
|
|
}
|
|
|
|
}, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
function includeAddress(addresses, address) {
|
|
|
|
return addresses.indexOf(address) > -1
|
|
|
|
}
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
module.exports = Wallet
|