2013-02-17 06:39:15 +01:00
|
|
|
var Script = require('./script');
|
2014-01-04 19:26:03 +01:00
|
|
|
var ECKey = require('./eckey').ECKey;
|
2014-03-11 02:52:48 +01:00
|
|
|
var convert = require('./convert');
|
2014-02-27 02:13:51 +01:00
|
|
|
var assert = require('assert');
|
2013-02-17 06:39:15 +01:00
|
|
|
var BigInteger = require('./jsbn/jsbn');
|
|
|
|
var Transaction = require('./transaction').Transaction;
|
|
|
|
var TransactionIn = require('./transaction').TransactionIn;
|
|
|
|
var TransactionOut = require('./transaction').TransactionOut;
|
2014-03-11 16:42:01 +01:00
|
|
|
var HDNode = require('./hdwallet.js')
|
2014-03-20 10:18:45 +01: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) {
|
|
|
|
if (!(this instanceof Wallet)) { return new Wallet(seed, options); }
|
|
|
|
|
|
|
|
var options = options || {}
|
2014-02-28 09:57:44 +01:00
|
|
|
var network = options.network || 'mainnet'
|
2014-02-27 04:54:54 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
// Stored in a closure to make accidental serialization less likely
|
|
|
|
var masterkey = null;
|
|
|
|
var me = this;
|
2014-03-14 03:31:26 +01:00
|
|
|
var accountZero = null;
|
|
|
|
var internalAccount = null;
|
|
|
|
var externalAccount = null;
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
// Addresses
|
|
|
|
this.addresses = [];
|
2014-03-13 10:43:35 +01:00
|
|
|
this.changeAddresses = [];
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
// Transaction output data
|
|
|
|
this.outputs = {};
|
2014-01-16 10:06:58 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
// Make a new master key
|
2014-02-27 01:56:37 +01:00
|
|
|
this.newMasterKey = function(seed, network) {
|
2014-03-20 10:18:45 +01:00
|
|
|
if (!seed) seed= rng(32, { array: true })
|
2014-03-11 16:42:01 +01:00
|
|
|
masterkey = new HDNode(seed, network);
|
2014-03-16 06:31:46 +01:00
|
|
|
|
|
|
|
// 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 = {};
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2014-02-27 01:56:37 +01:00
|
|
|
this.newMasterKey(seed, network)
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
this.generateAddress = function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var key = externalAccount.derive(this.addresses.length)
|
2014-03-22 08:17:54 +01:00
|
|
|
this.addresses.push(key.getAddress().toString())
|
2013-12-02 03:52:07 +01:00
|
|
|
return this.addresses[this.addresses.length - 1]
|
2014-03-13 10:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.generateChangeAddress = function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var key = internalAccount.derive(this.changeAddresses.length)
|
2014-03-22 08:17:54 +01:00
|
|
|
this.changeAddresses.push(key.getAddress().toString())
|
2014-03-13 10:43:35 +01:00
|
|
|
return this.changeAddresses[this.changeAddresses.length - 1]
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2014-01-16 10:06:58 +01:00
|
|
|
|
2014-03-22 07:37:09 +01:00
|
|
|
this.getUnspentOutputs = function() {
|
|
|
|
var utxo = []
|
|
|
|
|
|
|
|
for(var key in this.outputs){
|
2014-03-22 08:43:55 +01:00
|
|
|
utxo.push(outputToUnspentOutput(this.outputs[key]))
|
2014-03-22 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return utxo
|
|
|
|
}
|
|
|
|
|
2014-03-22 08:09:00 +01:00
|
|
|
this.setUnspentOutputs = function(utxo) {
|
|
|
|
var outputs = {}
|
|
|
|
|
2014-03-22 08:37:03 +01:00
|
|
|
utxo.forEach(function(uo){
|
|
|
|
validateUnspentOutput(uo)
|
|
|
|
var o = unspentOutputToOutput(uo)
|
2014-03-22 14:19:50 +01:00
|
|
|
outputs[o.receive] = o
|
2014-03-22 08:09:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
this.outputs = outputs
|
|
|
|
}
|
|
|
|
|
2014-03-22 08:43:55 +01:00
|
|
|
function outputToUnspentOutput(output){
|
2014-03-22 14:19:50 +01:00
|
|
|
var hashAndIndex = output.receive.split(":")
|
2014-03-22 08:43:55 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
hash: hashAndIndex[0],
|
|
|
|
hashLittleEndian: convert.reverseEndian(hashAndIndex[0]),
|
|
|
|
outputIndex: parseInt(hashAndIndex[1]),
|
|
|
|
scriptPubKey: output.scriptPubKey,
|
|
|
|
address: output.address,
|
|
|
|
value: output.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 08:37:03 +01:00
|
|
|
function unspentOutputToOutput(o) {
|
|
|
|
var hash = o.hash || convert.reverseEndian(o.hashLittleEndian)
|
|
|
|
var key = hash + ":" + o.outputIndex
|
|
|
|
return {
|
2014-03-22 14:19:50 +01:00
|
|
|
receive: key,
|
2014-03-22 08:37:03 +01:00
|
|
|
scriptPubKey: o.scriptPubKey,
|
|
|
|
address: o.address,
|
|
|
|
value: o.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateUnspentOutput(uo) {
|
|
|
|
var missingField;
|
|
|
|
|
|
|
|
if(isNullOrUndefined(uo.hash) && isNullOrUndefined(uo.hashLittleEndian)){
|
|
|
|
missingField = "hash(or hashLittleEndian)"
|
|
|
|
}
|
|
|
|
|
|
|
|
var requiredKeys = ['outputIndex', 'scriptPubKey', 'address', 'value']
|
|
|
|
requiredKeys.forEach(function(key){
|
|
|
|
if(isNullOrUndefined(uo[key])){
|
|
|
|
missingField = key
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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(' '))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNullOrUndefined(value){
|
|
|
|
return value == undefined
|
|
|
|
}
|
|
|
|
|
2014-03-22 14:10:56 +01:00
|
|
|
this.processTx = function(tx) {
|
2014-03-11 02:52:48 +01:00
|
|
|
var txhash = convert.bytesToHex(tx.getHash())
|
2014-03-22 14:10:56 +01:00
|
|
|
|
|
|
|
tx.outs.forEach(function(txOut, i){
|
|
|
|
var address = txOut.address.toString()
|
|
|
|
if (isMyAddress(address)) {
|
|
|
|
var output = txhash+':'+i
|
|
|
|
me.outputs[output] = {
|
2014-03-22 14:19:50 +01:00
|
|
|
receive: output,
|
2014-03-22 14:10:56 +01:00
|
|
|
value: txOut.value,
|
|
|
|
address: address,
|
|
|
|
scriptPubKey: convert.bytesToHex(txOut.script.buffer) //TODO: txOut.scriptPubKey()
|
|
|
|
// timestamp: new Date().getTime() / 1000,
|
|
|
|
// pending: true
|
2013-12-02 03:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-22 14:10:56 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
tx.ins.forEach(function(txIn, i){
|
|
|
|
var op = txIn.outpoint
|
2013-12-02 03:52:07 +01:00
|
|
|
var o = me.outputs[op.hash+':'+op.index]
|
|
|
|
if (o) {
|
|
|
|
o.spend = txhash+':'+i
|
2014-03-22 14:10:56 +01:00
|
|
|
// o.spendpending = true
|
|
|
|
// o.timestamp = new Date().getTime() / 1000
|
2013-12-02 03:52:07 +01:00
|
|
|
}
|
2014-03-22 14:10:56 +01:00
|
|
|
})
|
2013-11-20 19:00:49 +01:00
|
|
|
}
|
2014-03-22 14:10:56 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
this.getUtxoToPay = function(value) {
|
|
|
|
var h = []
|
|
|
|
for (var out in this.outputs) h.push(this.outputs[out])
|
|
|
|
var utxo = h.filter(function(x) { return !x.spend });
|
|
|
|
var valuecompare = function(a,b) { return a.value > b.value; }
|
|
|
|
var high = utxo.filter(function(o) { return o.value >= value; })
|
|
|
|
.sort(valuecompare);
|
|
|
|
if (high.length > 0) return [high[0]];
|
|
|
|
utxo.sort(valuecompare);
|
|
|
|
var totalval = 0;
|
|
|
|
for (var i = 0; i < utxo.length; i++) {
|
|
|
|
totalval += utxo[i].value;
|
|
|
|
if (totalval >= value) return utxo.slice(0,i+1);
|
|
|
|
}
|
|
|
|
throw ("Not enough money to send funds including transaction fee. Have: "
|
2014-01-16 10:06:58 +01:00
|
|
|
+ (totalval / 100000000) + ", needed: " + (value / 100000000));
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2014-01-16 10:06:58 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
this.mkSend = function(to, value, fee) {
|
|
|
|
var utxo = this.getUtxoToPay(value + fee)
|
|
|
|
var sum = utxo.reduce(function(t,o) { return t + o.value },0),
|
|
|
|
remainder = sum - value - fee
|
|
|
|
if (value < 5430) throw new Error("Amount below dust threshold!")
|
|
|
|
var unspentOuts = 0;
|
|
|
|
for (var o in this.outputs) {
|
|
|
|
if (!this.outputs[o].spend) unspentOuts += 1
|
|
|
|
if (unspentOuts >= 5) return
|
|
|
|
}
|
|
|
|
var change = this.addresses[this.addresses.length - 1]
|
|
|
|
var toOut = { address: to, value: value },
|
|
|
|
changeOut = { address: change, value: remainder }
|
|
|
|
halfChangeOut = { address: change, value: Math.floor(remainder/2) };
|
|
|
|
|
|
|
|
var outs =
|
|
|
|
remainder < 5430 ? [toOut]
|
|
|
|
: remainder < 10860 ? [toOut, changeOut]
|
|
|
|
: unspentOuts == 5 ? [toOut, changeOut]
|
|
|
|
: [toOut, halfChangeOut, halfChangeOut]
|
|
|
|
|
|
|
|
var tx = new Bitcoin.Transaction({
|
2014-03-22 14:19:50 +01:00
|
|
|
ins: utxo.map(function(x) { return x.receive }),
|
2013-12-02 03:52:07 +01:00
|
|
|
outs: outs
|
|
|
|
})
|
|
|
|
this.sign(tx)
|
|
|
|
return tx
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-12-03 03:10:14 +01:00
|
|
|
this.mkSendToOutputs = function(outputs, changeIndex, fee) {
|
|
|
|
var value = outputs.reduce(function(t,o) { return t + o.value },0),
|
|
|
|
utxo = this.getUtxoToPay(value + fee),
|
|
|
|
sum = utxo.reduce(function(t,p) { return t + o.value },0);
|
|
|
|
utxo[changeIndex].value += sum - value - fee;
|
|
|
|
var tx = new Bitcoin.Transaction({
|
2014-03-22 14:19:50 +01:00
|
|
|
ins: utxo.map(function(x) { return x.receive }),
|
2013-12-03 03:10:14 +01:00
|
|
|
outs: outputs
|
|
|
|
})
|
|
|
|
this.sign(tx)
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
this.sign = function(tx) {
|
|
|
|
tx.ins.map(function(inp,i) {
|
|
|
|
var inp = inp.outpoint.hash+':'+inp.outpoint.index;
|
|
|
|
if (me.outputs[inp]) {
|
2014-03-14 03:12:44 +01:00
|
|
|
var address = me.outputs[inp].address
|
|
|
|
tx.sign(i, me.getPrivateKeyForAddress(address))
|
2013-12-02 03:52:07 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return tx;
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2013-03-02 18:00:14 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
this.getMasterKey = function() { return masterkey }
|
2014-03-14 03:31:26 +01:00
|
|
|
this.getAccountZero = function() { return accountZero }
|
|
|
|
this.getInternalAccount = function() { return internalAccount }
|
|
|
|
this.getExternalAccount = function() { return externalAccount }
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2013-12-02 03:52:07 +01:00
|
|
|
this.getPrivateKey = function(index) {
|
2014-03-14 03:31:26 +01:00
|
|
|
return externalAccount.derive(index).priv
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
|
|
|
|
2014-03-13 12:05:55 +01:00
|
|
|
this.getInternalPrivateKey = function(index) {
|
2014-03-14 03:31:26 +01:00
|
|
|
return internalAccount.derive(index).priv
|
2014-03-13 12:05:55 +01: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-22 14:10:56 +01:00
|
|
|
|
|
|
|
function isReceiveAddress(address){
|
|
|
|
return me.addresses.indexOf(address) > -1
|
|
|
|
}
|
|
|
|
|
|
|
|
function isChangeAddress(address){
|
|
|
|
return me.changeAddresses.indexOf(address) > -1
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMyAddress(address) {
|
|
|
|
return isReceiveAddress(address) || isChangeAddress(address)
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Wallet;
|