bitcoinjs-lib/src/wallet.js

246 lines
7.6 KiB
JavaScript
Raw Normal View History

2011-05-04 18:02:56 +02:00
Bitcoin.Wallet = (function () {
2011-12-28 13:44:30 +01:00
var Script = Bitcoin.Script,
TransactionIn = Bitcoin.TransactionIn,
TransactionOut = Bitcoin.TransactionOut;
var Wallet = function () {
// Keychain
var keys = [];
this.addressHashes = [];
// Transaction data
this.txIndex = {};
this.unspentOuts = [];
// Other fields
this.addressPointer = 0;
this.addKey = function (key, pub) {
if (!(key instanceof Bitcoin.ECKey)) {
key = new Bitcoin.ECKey(key);
}
keys.push(key);
if (pub) {
if ("string" === typeof pub) {
pub = Crypto.util.base64ToBytes(pub);
}
key.pub = pub;
}
this.addressHashes.push(key.getBitcoinAddress().getHashBase64());
};
this.addKeys = function (keys, pubs) {
if ("string" === typeof keys) {
keys = keys.split(',');
}
if ("string" === typeof pubs) {
pubs = pubs.split(',');
}
2011-12-28 13:50:58 +01:00
var i;
2011-12-28 13:44:30 +01:00
if (Array.isArray(pubs) && keys.length == pubs.length) {
2011-12-28 13:50:58 +01:00
for (i = 0; i < keys.length; i++) {
2011-12-28 13:44:30 +01:00
this.addKey(keys[i], pubs[i]);
}
} else {
2011-12-28 13:50:58 +01:00
for (i = 0; i < keys.length; i++) {
2011-12-28 13:44:30 +01:00
this.addKey(keys[i]);
}
}
};
this.getKeys = function () {
var serializedWallet = [];
for (var i = 0; i < keys.length; i++) {
serializedWallet.push(keys[i].toString('base64'));
}
return serializedWallet;
};
this.getPubKeys = function () {
var pubs = [];
for (var i = 0; i < keys.length; i++) {
pubs.push(Crypto.util.bytesToBase64(keys[i].getPub()));
}
return pubs;
};
this.clear = function () {
keys = [];
};
this.getLength = function () {
return keys.length;
};
this.getAllAddresses = function () {
var addresses = [];
for (var i = 0; i < keys.length; i++) {
addresses.push(keys[i].getBitcoinAddress());
}
return addresses;
};
this.getCurAddress = function () {
if (keys[this.addressPointer]) {
return keys[this.addressPointer].getBitcoinAddress();
} else {
return null;
}
};
this.getNextAddress = function () {
this.addressPointer++;
if(!keys[this.addressPointer]) {
this.generateAddress();
2011-12-28 13:44:30 +01:00
}
return keys[this.addressPointer].getBitcoinAddress();
2011-12-28 13:44:30 +01:00
};
this.signWithKey = function (pubKeyHash, hash) {
pubKeyHash = Crypto.util.bytesToBase64(pubKeyHash);
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");
};
this.getPubKeyFromHash = function (pubKeyHash) {
pubKeyHash = Crypto.util.bytesToBase64(pubKeyHash);
for (var i = 0; i < this.addressHashes.length; i++) {
if (this.addressHashes[i] == pubKeyHash) {
return keys[i].getPub();
}
}
throw new Error("Hash unknown");
};
};
Wallet.prototype.generateAddress = function () {
this.addKey(new Bitcoin.ECKey());
};
Wallet.prototype.process = function (tx) {
if (this.txIndex[tx.hash]) return;
2011-12-28 13:50:58 +01:00
var j;
var k;
var hash;
2011-12-28 13:44:30 +01:00
// Gather outputs
2011-12-28 13:50:58 +01:00
for (j = 0; j < tx.outs.length; j++) {
2011-12-28 13:44:30 +01:00
var txout = new TransactionOut(tx.outs[j]);
2011-12-28 13:50:58 +01:00
hash = Crypto.util.bytesToBase64(txout.script.simpleOutPubKeyHash());
for (k = 0; k < this.addressHashes.length; k++) {
2011-12-28 13:44:30 +01:00
if (this.addressHashes[k] === hash) {
this.unspentOuts.push({tx: tx, index: j, out: txout});
break;
}
}
}
// Remove spent outputs
2011-12-28 13:50:58 +01:00
for (j = 0; j < tx.ins.length; j++) {
2011-12-28 13:44:30 +01:00
var txin = new TransactionIn(tx.ins[j]);
var pubkey = txin.script.simpleInPubKey();
2011-12-28 13:50:58 +01:00
hash = Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(pubkey));
for (k = 0; k < this.addressHashes.length; k++) {
2011-12-28 13:44:30 +01:00
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 () {
var balance = BigInteger.valueOf(0);
for (var i = 0; i < this.unspentOuts.length; i++) {
var txout = this.unspentOuts[i].out;
balance = balance.add(Bitcoin.Util.valueToBigInt(txout.value));
}
return balance;
};
Wallet.prototype.createSend = function (address, sendValue, feeValue) {
var selectedOuts = [];
var txValue = sendValue.add(feeValue);
var availableValue = BigInteger.ZERO;
2011-12-28 13:50:58 +01:00
var i;
for (i = 0; i < this.unspentOuts.length; i++) {
2011-12-28 13:44:30 +01:00
selectedOuts.push(this.unspentOuts[i]);
availableValue = availableValue.add(Bitcoin.Util.valueToBigInt(this.unspentOuts[i].out.value));
if (availableValue.compareTo(txValue) >= 0) break;
}
2011-12-28 13:44:30 +01:00
if (availableValue.compareTo(txValue) < 0) {
throw new Error('Insufficient funds.');
}
2011-12-28 13:44:30 +01:00
var changeValue = availableValue.subtract(txValue);
var sendTx = new Bitcoin.Transaction();
2011-12-28 13:50:58 +01:00
for (i = 0; i < selectedOuts.length; i++) {
2011-12-28 13:44:30 +01:00
sendTx.addInput(selectedOuts[i].tx, selectedOuts[i].index);
}
2011-05-04 18:02:56 +02:00
2011-12-28 13:44:30 +01:00
sendTx.addOutput(address, sendValue);
if (changeValue.compareTo(BigInteger.ZERO) > 0) {
sendTx.addOutput(this.getNextAddress(), changeValue);
}
2011-05-04 18:02:56 +02:00
2011-12-28 13:44:30 +01:00
var hashType = 1; // SIGHASH_ALL
2011-05-04 18:02:56 +02:00
2011-12-28 13:50:58 +01:00
for (i = 0; i < sendTx.ins.length; i++) {
2011-12-28 13:44:30 +01:00
var hash = sendTx.hashTransactionForSignature(selectedOuts[i].out.script, i, hashType);
var pubKeyHash = selectedOuts[i].out.script.simpleOutPubKeyHash();
var signature = this.signWithKey(pubKeyHash, hash);
2011-05-04 18:02:56 +02:00
2011-12-28 13:44:30 +01:00
// Append hash type
2011-12-28 13:50:58 +01:00
signature.push(parseInt(hashType, 10));
2011-12-28 13:44:30 +01:00
sendTx.ins[i].script = Script.createInputScript(signature, this.getPubKeyFromHash(pubKeyHash));
}
return sendTx;
};
2011-12-28 13:44:30 +01:00
Wallet.prototype.clearTransactions = function () {
this.txIndex = {};
this.unspentOuts = [];
};
/**
* Check to see if a pubKeyHash belongs to this wallet.
*/
Wallet.prototype.hasHash = function (hash) {
if (Bitcoin.Util.isArray(hash)) hash = Crypto.util.bytesToBase64(hash);
2011-05-04 18:02:56 +02:00
2011-12-28 13:44:30 +01:00
// TODO: Just create an object with base64 hashes as keys for faster lookup
for (var k = 0; k < this.addressHashes.length; k++) {
if (this.addressHashes[k] === hash) return true;
}
return false;
};
2011-12-28 13:44:30 +01:00
return Wallet;
2011-05-04 18:02:56 +02:00
})();