2014-03-31 05:47:47 +02:00
|
|
|
var convert = require('./convert')
|
|
|
|
var Transaction = require('./transaction').Transaction
|
2014-03-11 16:42:01 +01:00
|
|
|
var HDNode = require('./hdwallet.js')
|
2014-03-31 05:47:47 +02:00
|
|
|
var rng = require('secure-random')
|
2013-11-20 19:00:49 +01:00
|
|
|
|
2014-02-27 04:54:54 +01:00
|
|
|
var Wallet = function (seed, options) {
|
2014-03-31 05:47:47 +02:00
|
|
|
if (!(this instanceof Wallet)) { return new Wallet(seed, options); }
|
|
|
|
|
|
|
|
var options = options || {}
|
|
|
|
var network = options.network || 'mainnet'
|
|
|
|
|
|
|
|
// 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 = []
|
|
|
|
|
|
|
|
// Transaction output data
|
|
|
|
this.outputs = {}
|
|
|
|
|
|
|
|
// Make a new master key
|
|
|
|
this.newMasterKey = function(seed, network) {
|
|
|
|
if (!seed) seed = rng(32, { array: true });
|
|
|
|
masterkey = new HDNode(seed, network)
|
|
|
|
|
|
|
|
// HD first-level child derivation method should be private
|
|
|
|
// See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
|
|
|
|
accountZero = masterkey.derivePrivate(0)
|
|
|
|
externalAccount = accountZero.derive(0)
|
|
|
|
internalAccount = accountZero.derive(1)
|
|
|
|
|
|
|
|
me.addresses = []
|
|
|
|
me.changeAddresses = []
|
|
|
|
|
|
|
|
me.outputs = {}
|
|
|
|
}
|
|
|
|
this.newMasterKey(seed, network)
|
|
|
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
if(!output.spend) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setUnspentOutputsAsync = function(utxo, callback) {
|
|
|
|
var error = null
|
|
|
|
try {
|
|
|
|
this.setUnspentOutputs(utxo)
|
|
|
|
} catch(err) {
|
|
|
|
error = err
|
|
|
|
} finally {
|
|
|
|
process.nextTick(function(){ callback(error) })
|
2014-03-25 04:04:27 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-03-25 04:04:27 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
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],
|
|
|
|
hashLittleEndian: convert.reverseEndian(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) {
|
|
|
|
var hash = o.hash || convert.reverseEndian(o.hashLittleEndian)
|
|
|
|
var key = hash + ":" + o.outputIndex
|
|
|
|
return {
|
|
|
|
receive: key,
|
|
|
|
address: o.address,
|
|
|
|
value: o.value
|
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-03-31 05:47:47 +02:00
|
|
|
if(isNullOrUndefined(uo.hash) && isNullOrUndefined(uo.hashLittleEndian)){
|
|
|
|
missingField = "hash(or hashLittleEndian)"
|
2014-03-22 08:43:55 +01:00
|
|
|
}
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var requiredKeys = ['outputIndex', 'address', 'value']
|
|
|
|
requiredKeys.forEach(function(key){
|
|
|
|
if(isNullOrUndefined(uo[key])){
|
|
|
|
missingField = key
|
2014-03-22 08:37:03 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if(missingField) {
|
|
|
|
var message = [
|
|
|
|
'Invalid unspent output: key', field, 'is missing.',
|
|
|
|
'A valid unspent output must contain'
|
|
|
|
]
|
|
|
|
message.push(requiredKeys.join(', '))
|
|
|
|
message.push("and hash(or hashLittleEndian)")
|
|
|
|
throw new Error(message.join(' '))
|
2014-03-22 08:37:03 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isNullOrUndefined(value){
|
|
|
|
return value == undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
this.processTx = function(tx) {
|
|
|
|
var txhash = convert.bytesToHex(tx.getHash())
|
|
|
|
|
|
|
|
tx.outs.forEach(function(txOut, i){
|
|
|
|
var address = txOut.address.toString()
|
|
|
|
if (isMyAddress(address)) {
|
|
|
|
var output = txhash+':'+i
|
|
|
|
me.outputs[output] = {
|
|
|
|
receive: output,
|
|
|
|
value: txOut.value,
|
|
|
|
address: address,
|
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-03-31 05:47:47 +02:00
|
|
|
tx.ins.forEach(function(txIn, i){
|
|
|
|
var op = txIn.outpoint
|
|
|
|
var o = me.outputs[op.hash+':'+op.index]
|
|
|
|
if (o) {
|
|
|
|
o.spend = txhash+':'+i
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
this.createTx = function(to, value, fixedFee) {
|
|
|
|
checkDust(value)
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(to, value)
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var utxo = getCandidateOutputs(value)
|
|
|
|
var totalInValue = 0
|
|
|
|
for(var i=0; i<utxo.length; i++){
|
|
|
|
var output = utxo[i]
|
|
|
|
tx.addInput(output.receive)
|
2014-03-23 19:19:17 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
totalInValue += output.value
|
|
|
|
if(totalInValue < value) continue
|
2014-03-23 18:29:10 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var fee = fixedFee == undefined ? estimateFeePadChangeOutput(tx) : fixedFee
|
|
|
|
if(totalInValue < value + fee) continue
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
var change = totalInValue - value - fee
|
|
|
|
if(change > 0 && !isDust(change)) {
|
|
|
|
tx.addOutput(getChangeAddress(), change)
|
2014-03-25 06:39:27 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
break
|
2014-03-25 06:39:27 +01:00
|
|
|
}
|
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
checkInsufficientFund(totalInValue, value, fee)
|
2014-03-23 18:42:36 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
this.sign(tx)
|
2014-03-23 19:49:40 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
return tx
|
|
|
|
}
|
2014-03-23 19:49:40 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
this.createTxAsync = function(to, value, fixedFee, callback){
|
|
|
|
if(fixedFee instanceof Function) {
|
|
|
|
callback = fixedFee
|
|
|
|
fixedFee = undefined
|
2014-03-23 19:19:17 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
var tx = null
|
|
|
|
var error = null
|
|
|
|
|
|
|
|
try {
|
|
|
|
tx = this.createTx(to, value, fixedFee)
|
|
|
|
} catch(err) {
|
|
|
|
error = err
|
|
|
|
} finally {
|
|
|
|
process.nextTick(function(){ callback(error, tx) })
|
2014-03-23 18:01:50 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-03-23 18:01:50 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
this.dustThreshold = 5430
|
|
|
|
function isDust(amount) {
|
|
|
|
return amount <= me.dustThreshold
|
|
|
|
}
|
2014-03-23 17:19:46 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
function checkDust(value){
|
|
|
|
if (isNullOrUndefined(value) || isDust(value)) {
|
|
|
|
throw new Error("Value must be above dust threshold")
|
2013-12-03 03:10:14 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2013-12-03 03:10:14 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
function getCandidateOutputs(value){
|
|
|
|
var unspent = []
|
|
|
|
for (var key in me.outputs){
|
|
|
|
var output = me.outputs[key]
|
|
|
|
if(!output.spend) 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()
|
|
|
|
tmpTx.addOutput(getChangeAddress(), 0)
|
|
|
|
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-03-31 05:47:47 +02:00
|
|
|
function checkInsufficientFund(totalInValue, value, fee) {
|
|
|
|
if(totalInValue < value + fee) {
|
|
|
|
throw new Error('Not enough money to send funds including transaction fee. Have: ' +
|
|
|
|
totalInValue + ', needed: ' + (value + fee))
|
2014-03-13 12:05:55 +01:00
|
|
|
}
|
2014-03-31 05:47:47 +02:00
|
|
|
}
|
2014-03-13 12:05:55 +01:00
|
|
|
|
2014-03-31 05:47:47 +02:00
|
|
|
this.sign = function(tx) {
|
|
|
|
tx.ins.forEach(function(inp,i) {
|
|
|
|
var output = me.outputs[inp.outpoint.hash + ':' + inp.outpoint.index]
|
|
|
|
if (output) {
|
|
|
|
tx.sign(i, me.getPrivateKeyForAddress(output.address))
|
2014-03-13 12:05:55 +01: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) {
|
|
|
|
return externalAccount.derive(index).priv
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getInternalPrivateKey = function(index) {
|
|
|
|
return internalAccount.derive(index).priv
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|