2013-02-17 06:39:15 +01:00
|
|
|
var Script = require('./script');
|
|
|
|
var ECKey = require('./eckey');
|
|
|
|
var conv = require('./convert');
|
|
|
|
var util = require('./util');
|
2011-12-28 13:44:30 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var BigInteger = require('./jsbn/jsbn');
|
2011-12-28 13:44:30 +01:00
|
|
|
|
2013-11-20 19:00:49 +01:00
|
|
|
var BIP32key = require('./bip32');
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var Transaction = require('./transaction').Transaction;
|
|
|
|
var TransactionIn = require('./transaction').TransactionIn;
|
|
|
|
var TransactionOut = require('./transaction').TransactionOut;
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-11-20 19:00:49 +01:00
|
|
|
var SecureRandom = require('./jsbn/rng');
|
|
|
|
var rng = new SecureRandom();
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
var Wallet = function () {
|
|
|
|
// Keychain
|
|
|
|
//
|
|
|
|
// The keychain is stored as a var in this closure to make accidental
|
|
|
|
// serialization less likely.
|
|
|
|
//
|
|
|
|
// Any functions accessing this value therefore have to be defined in
|
|
|
|
// the closure of this constructor.
|
|
|
|
var keys = [];
|
2013-11-20 19:00:49 +01:00
|
|
|
var masterkey = null;
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// Public hashes of our keys
|
|
|
|
this.addressHashes = [];
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// Transaction data
|
|
|
|
this.txIndex = {};
|
|
|
|
this.unspentOuts = [];
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
// Other fields
|
|
|
|
this.addressPointer = 0;
|
2013-11-20 19:00:49 +01:00
|
|
|
|
|
|
|
this.genMasterkey = function(seed) {
|
|
|
|
if (!seed) {
|
|
|
|
var seedBytes = new Array(32);
|
|
|
|
rng.nextBytes(seedBytes);
|
|
|
|
seed = conv.bytesToString(seedBytes)
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2013-11-20 19:00:49 +01:00
|
|
|
masterkey = new BIP32key(seed);
|
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-11-20 19:00:49 +01:00
|
|
|
this.generateAddress = function() {
|
|
|
|
keys.push(masterkey.ckd(keys.length))
|
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Get the key chain.
|
|
|
|
*
|
2013-10-07 21:27:19 +02:00
|
|
|
* Returns an array of hex-encoded private values.
|
2013-02-17 06:39:15 +01:00
|
|
|
*/
|
|
|
|
this.getKeys = function () {
|
2013-11-20 19:00:49 +01:00
|
|
|
var keyExport = [];
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
2013-11-20 19:00:49 +01:00
|
|
|
keyExport.push(keys[i].toString());
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-11-20 19:00:49 +01:00
|
|
|
return keyExport;
|
2013-02-17 06:39:15 +01:00
|
|
|
};
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-11-20 19:00:49 +01:00
|
|
|
this.privateSerialize = function() {
|
|
|
|
return {
|
|
|
|
masterkey: masterkey,
|
|
|
|
keys: this.getKeys()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Get the public keys.
|
|
|
|
*
|
2013-10-07 21:27:19 +02:00
|
|
|
* Returns an array of hex-encoded public keys.
|
2013-02-17 06:39:15 +01:00
|
|
|
*/
|
|
|
|
this.getPubKeys = function () {
|
|
|
|
var pubs = [];
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
2013-10-08 11:42:28 +02:00
|
|
|
pubs.push(conv.bytesToHex(keys[i].getPub()));
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
return pubs;
|
|
|
|
};
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Delete all keys.
|
|
|
|
*/
|
|
|
|
this.clear = function () {
|
|
|
|
keys = [];
|
2013-11-20 19:00:49 +01:00
|
|
|
masterkey = null;
|
2013-02-17 06:39:15 +01:00
|
|
|
};
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Return the number of keys in this wallet.
|
|
|
|
*/
|
|
|
|
this.getLength = function () {
|
|
|
|
return keys.length;
|
|
|
|
};
|
2012-01-11 02:40:45 +01:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Get the addresses for this wallet.
|
|
|
|
*
|
|
|
|
* Returns an array of Address objects.
|
|
|
|
*/
|
|
|
|
this.getAllAddresses = function () {
|
|
|
|
var addresses = [];
|
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
addresses.push(keys[i].getBitcoinAddress());
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
return addresses;
|
2012-01-11 02:40:45 +01:00
|
|
|
};
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
this.getCurAddress = function () {
|
2013-11-20 19:00:49 +01:00
|
|
|
if (keys[keys.length - 1]) {
|
|
|
|
return keys[keys.length - 1].getBitcoinAddress();
|
2013-02-17 06:39:15 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2012-01-11 02:40:45 +01:00
|
|
|
};
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Sign a hash with a key.
|
|
|
|
*
|
|
|
|
* This method expects the pubKeyHash as the first parameter and the hash
|
|
|
|
* to be signed as the second parameter.
|
|
|
|
*/
|
|
|
|
this.signWithKey = function (pubKeyHash, hash) {
|
2013-10-07 21:27:19 +02:00
|
|
|
pubKeyHash = conv.bytesToHex(pubKeyHash);
|
2013-02-17 06:39:15 +01:00
|
|
|
for (var i = 0; i < this.addressHashes.length; i++) {
|
|
|
|
if (this.addressHashes[i] == pubKeyHash) {
|
|
|
|
return keys[i].sign(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error("Missing key for signature");
|
|
|
|
};
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the corresponding pubKey for a pubKeyHash.
|
|
|
|
*
|
|
|
|
* This function only works if the pubKey in question is part of this
|
|
|
|
* wallet.
|
|
|
|
*/
|
|
|
|
this.getPubKeyFromHash = function (pubKeyHash) {
|
2013-10-07 21:27:19 +02:00
|
|
|
pubKeyHash = conv.bytesToHex(pubKeyHash);
|
2013-02-17 06:39:15 +01:00
|
|
|
for (var i = 0; i < this.addressHashes.length; i++) {
|
|
|
|
if (this.addressHashes[i] == pubKeyHash) {
|
|
|
|
return keys[i].getPub();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error("Hash unknown");
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-03-02 18:00:14 +01:00
|
|
|
// return unspent transactions
|
|
|
|
Wallet.prototype.unspentTx = function() {
|
|
|
|
return this.unspentOuts;
|
|
|
|
};
|
|
|
|
|
2013-02-17 06:39:15 +01:00
|
|
|
/**
|
|
|
|
* Add a transaction to the wallet's processed transaction.
|
|
|
|
*
|
|
|
|
* This will add a transaction to the wallet, updating its balance and
|
|
|
|
* available unspent outputs.
|
|
|
|
*/
|
|
|
|
Wallet.prototype.process = function (tx) {
|
|
|
|
if (this.txIndex[tx.hash]) return;
|
|
|
|
|
|
|
|
var j;
|
|
|
|
var k;
|
|
|
|
var hash;
|
|
|
|
// Gather outputs
|
|
|
|
for (j = 0; j < tx.out.length; j++) {
|
2013-03-02 18:00:14 +01:00
|
|
|
var raw_tx = tx.out[j];
|
|
|
|
var txout = new TransactionOut(raw_tx);
|
2013-10-07 21:27:19 +02:00
|
|
|
// this hash is the hash of the pubkey which is the address the output when to
|
|
|
|
hash = conv.bytesToHex(txout.script.simpleOutPubKeyHash());
|
2013-02-17 06:39:15 +01:00
|
|
|
for (k = 0; k < this.addressHashes.length; k++) {
|
2013-03-02 18:00:14 +01:00
|
|
|
// if our address, then we add the unspent out to a list of unspent outputs
|
2013-02-17 06:39:15 +01:00
|
|
|
if (this.addressHashes[k] === hash) {
|
2013-11-20 19:00:49 +01:00
|
|
|
this.unspentOuts.push({tx: tx, index: j, output: txout});
|
2013-02-17 06:39:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove spent outputs
|
|
|
|
for (j = 0; j < tx.in.length; j++) {
|
2013-03-02 18:00:14 +01:00
|
|
|
var raw_tx = tx.in[j];
|
|
|
|
|
|
|
|
// mangle into the format TransactionIn expects
|
|
|
|
raw_tx.outpoint = {
|
|
|
|
hash: raw_tx.prev_out.hash,
|
|
|
|
index: raw_tx.prev_out.n
|
|
|
|
};
|
|
|
|
|
|
|
|
var txin = new TransactionIn(raw_tx);
|
2013-02-17 06:39:15 +01:00
|
|
|
var pubkey = txin.script.simpleInPubKey();
|
2013-10-07 21:27:19 +02:00
|
|
|
hash = conv.bytesToHex(util.sha256ripe160(pubkey));
|
2013-02-17 06:39:15 +01:00
|
|
|
for (k = 0; k < this.addressHashes.length; k++) {
|
|
|
|
if (this.addressHashes[k] === hash) {
|
|
|
|
for (var l = 0; l < this.unspentOuts.length; l++) {
|
|
|
|
if (txin.outpoint.hash == this.unspentOuts[l].tx.hash &&
|
|
|
|
txin.outpoint.index == this.unspentOuts[l].index) {
|
|
|
|
this.unspentOuts.splice(l, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index transaction
|
|
|
|
this.txIndex[tx.hash] = tx;
|
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.prototype.getBalance = function () {
|
2013-11-20 19:00:49 +01:00
|
|
|
return this.unspentOuts.reduce(function(t,o) { return t + o.output.value },0);
|
2013-02-17 06:39:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.prototype.createSend = function (address, sendValue, feeValue) {
|
|
|
|
var selectedOuts = [];
|
2013-10-14 13:41:21 +02:00
|
|
|
var txValue = sendValue + feeValue;
|
|
|
|
var availableValue = 0;
|
2013-02-17 06:39:15 +01:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < this.unspentOuts.length; i++) {
|
|
|
|
var txout = this.unspentOuts[i];
|
|
|
|
selectedOuts.push(txout);
|
2013-11-20 19:00:49 +01:00
|
|
|
availableValue += txout.output.value;
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2013-10-14 13:41:21 +02:00
|
|
|
if (availableValue >= txValue) break;
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:41:21 +02:00
|
|
|
if (availableValue < txValue) {
|
2013-02-17 06:39:15 +01:00
|
|
|
throw new Error('Insufficient funds.');
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:41:21 +02:00
|
|
|
var changeValue = availableValue - txValue;
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
var sendTx = new Transaction();
|
|
|
|
|
|
|
|
for (i = 0; i < selectedOuts.length; i++) {
|
|
|
|
sendTx.addInput(selectedOuts[i].tx, selectedOuts[i].index);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendTx.addOutput(address, sendValue);
|
2013-10-14 13:41:21 +02:00
|
|
|
if (changeValue > 0) {
|
2013-11-20 19:00:49 +01:00
|
|
|
sendTx.addOutput(this.getCurAddress(), changeValue);
|
2013-02-17 06:39:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var hashType = 1; // SIGHASH_ALL
|
|
|
|
|
2013-11-20 19:00:49 +01:00
|
|
|
sendTx.signWithKeys(this.getKeys(), selectedOuts, hashType)
|
2013-02-17 06:39:15 +01:00
|
|
|
|
|
|
|
return sendTx;
|
|
|
|
};
|
|
|
|
|
|
|
|
Wallet.prototype.clearTransactions = function () {
|
|
|
|
this.txIndex = {};
|
|
|
|
this.unspentOuts = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if a pubKeyHash belongs to this wallet.
|
|
|
|
*/
|
|
|
|
Wallet.prototype.hasHash = function (hash) {
|
2013-10-08 12:45:13 +02:00
|
|
|
if (util.isArray(hash)) hash = conv.bytesToHex(hash);
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2013-10-07 21:27:19 +02:00
|
|
|
// TODO: Just create an object with hashes as keys for faster lookup
|
2013-02-17 06:39:15 +01:00
|
|
|
for (var k = 0; k < this.addressHashes.length; k++) {
|
|
|
|
if (this.addressHashes[k] === hash) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Wallet;
|