src/wallet.js: retab file

This commit is contained in:
booo 2011-12-28 13:44:30 +01:00
parent 9353e4aaf5
commit 7675cf14e4

View file

@ -1,249 +1,249 @@
Bitcoin.Wallet = (function () { Bitcoin.Wallet = (function () {
var Script = Bitcoin.Script, var Script = Bitcoin.Script,
TransactionIn = Bitcoin.TransactionIn, TransactionIn = Bitcoin.TransactionIn,
TransactionOut = Bitcoin.TransactionOut; TransactionOut = Bitcoin.TransactionOut;
var Wallet = function () { var Wallet = function () {
// Keychain // Keychain
var keys = []; var keys = [];
this.addressHashes = []; this.addressHashes = [];
// Transaction data // Transaction data
this.txIndex = {}; this.txIndex = {};
this.unspentOuts = []; this.unspentOuts = [];
// Other fields // Other fields
this.addressPointer = 0; this.addressPointer = 0;
this.addKey = function (key, pub) { this.addKey = function (key, pub) {
if (!(key instanceof Bitcoin.ECKey)) { if (!(key instanceof Bitcoin.ECKey)) {
key = new Bitcoin.ECKey(key); key = new Bitcoin.ECKey(key);
} }
keys.push(key); keys.push(key);
if (pub) { if (pub) {
if ("string" === typeof pub) { if ("string" === typeof pub) {
pub = Crypto.util.base64ToBytes(pub); pub = Crypto.util.base64ToBytes(pub);
} }
key.pub = pub; key.pub = pub;
} }
this.addressHashes.push(key.getBitcoinAddress().getHashBase64()); this.addressHashes.push(key.getBitcoinAddress().getHashBase64());
}; };
this.addKeys = function (keys, pubs) { this.addKeys = function (keys, pubs) {
if ("string" === typeof keys) { if ("string" === typeof keys) {
keys = keys.split(','); keys = keys.split(',');
} }
if ("string" === typeof pubs) { if ("string" === typeof pubs) {
pubs = pubs.split(','); pubs = pubs.split(',');
} }
console.log(pubs); console.log(pubs);
if (Array.isArray(pubs) && keys.length == pubs.length) { if (Array.isArray(pubs) && keys.length == pubs.length) {
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
this.addKey(keys[i], pubs[i]); this.addKey(keys[i], pubs[i]);
} }
} else { } else {
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
this.addKey(keys[i]); this.addKey(keys[i]);
} }
} }
}; };
this.getKeys = function () { this.getKeys = function () {
var serializedWallet = []; var serializedWallet = [];
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
serializedWallet.push(keys[i].toString('base64')); serializedWallet.push(keys[i].toString('base64'));
} }
return serializedWallet; return serializedWallet;
}; };
this.getPubKeys = function () { this.getPubKeys = function () {
var pubs = []; var pubs = [];
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
pubs.push(Crypto.util.bytesToBase64(keys[i].getPub())); pubs.push(Crypto.util.bytesToBase64(keys[i].getPub()));
} }
return pubs; 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 () {
if (keys.length) {
// TODO: Create new addresses if we run out
this.addressPointer = (this.addressPointer + 1) % keys.length;
return keys[this.addressPointer].getBitcoinAddress();
} else {
return null;
}
};
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) {
console.log(Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(keys[i].getPub())), pubKeyHash);
return keys[i].getPub();
}
}
throw new Error("Hash unknown");
};
}; };
this.clear = function () { Wallet.prototype.generateAddress = function () {
keys = []; this.addKey(new Bitcoin.ECKey());
};
this.getLength = function () {
return keys.length;
}; };
this.getAllAddresses = function () { Wallet.prototype.process = function (tx) {
var addresses = []; if (this.txIndex[tx.hash]) return;
for (var i = 0; i < keys.length; i++) {
addresses.push(keys[i].getBitcoinAddress());
}
return addresses;
};
this.getCurAddress = function () { // Gather outputs
if (keys[this.addressPointer]) { for (var j = 0; j < tx.outs.length; j++) {
return keys[this.addressPointer].getBitcoinAddress(); var txout = new TransactionOut(tx.outs[j]);
} else { var hash = Crypto.util.bytesToBase64(txout.script.simpleOutPubKeyHash());
return null; for (var k = 0; k < this.addressHashes.length; k++) {
} if (this.addressHashes[k] === hash) {
}; this.unspentOuts.push({tx: tx, index: j, out: txout});
break;
}
}
}
this.getNextAddress = function () { // Remove spent outputs
if (keys.length) { for (var j = 0; j < tx.ins.length; j++) {
// TODO: Create new addresses if we run out var txin = new TransactionIn(tx.ins[j]);
this.addressPointer = (this.addressPointer + 1) % keys.length; var pubkey = txin.script.simpleInPubKey();
return keys[this.addressPointer].getBitcoinAddress(); var hash = Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(pubkey));
} else { for (var k = 0; k < this.addressHashes.length; k++) {
return null; 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;
}
}
}
this.signWithKey = function (pubKeyHash, hash) { // Index transaction
pubKeyHash = Crypto.util.bytesToBase64(pubKeyHash); this.txIndex[tx.hash] = tx;
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) { Wallet.prototype.getBalance = function () {
pubKeyHash = Crypto.util.bytesToBase64(pubKeyHash); var balance = BigInteger.valueOf(0);
for (var i = 0; i < this.addressHashes.length; i++) { for (var i = 0; i < this.unspentOuts.length; i++) {
if (this.addressHashes[i] == pubKeyHash) { var txout = this.unspentOuts[i].out;
console.log(Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(keys[i].getPub())), pubKeyHash); balance = balance.add(Bitcoin.Util.valueToBigInt(txout.value));
return keys[i].getPub(); }
} return balance;
} };
throw new Error("Hash unknown");
};
};
Wallet.prototype.generateAddress = function () { Wallet.prototype.createSend = function (address, sendValue, feeValue) {
this.addKey(new Bitcoin.ECKey()); var selectedOuts = [];
}; var txValue = sendValue.add(feeValue);
var availableValue = BigInteger.ZERO;
for (var i = 0; i < this.unspentOuts.length; i++) {
selectedOuts.push(this.unspentOuts[i]);
availableValue = availableValue.add(Bitcoin.Util.valueToBigInt(this.unspentOuts[i].out.value));
Wallet.prototype.process = function (tx) { if (availableValue.compareTo(txValue) >= 0) break;
if (this.txIndex[tx.hash]) return; }
// Gather outputs if (availableValue.compareTo(txValue) < 0) {
for (var j = 0; j < tx.outs.length; j++) { throw new Error('Insufficient funds.');
var txout = new TransactionOut(tx.outs[j]); }
var hash = Crypto.util.bytesToBase64(txout.script.simpleOutPubKeyHash());
for (var k = 0; k < this.addressHashes.length; k++) {
if (this.addressHashes[k] === hash) {
this.unspentOuts.push({tx: tx, index: j, out: txout});
break;
}
}
}
// Remove spent outputs console.log(selectedOuts);
for (var j = 0; j < tx.ins.length; j++) {
var txin = new TransactionIn(tx.ins[j]);
var pubkey = txin.script.simpleInPubKey();
var hash = Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(pubkey));
for (var 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 var changeValue = availableValue.subtract(txValue);
this.txIndex[tx.hash] = tx;
};
Wallet.prototype.getBalance = function () { var sendTx = new Bitcoin.Transaction();
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) { for (var i = 0; i < selectedOuts.length; i++) {
var selectedOuts = []; sendTx.addInput(selectedOuts[i].tx, selectedOuts[i].index);
var txValue = sendValue.add(feeValue); }
var availableValue = BigInteger.ZERO;
for (var i = 0; i < this.unspentOuts.length; i++) {
selectedOuts.push(this.unspentOuts[i]);
availableValue = availableValue.add(Bitcoin.Util.valueToBigInt(this.unspentOuts[i].out.value));
if (availableValue.compareTo(txValue) >= 0) break; sendTx.addOutput(address, sendValue);
} if (changeValue.compareTo(BigInteger.ZERO) > 0) {
sendTx.addOutput(this.getNextAddress(), changeValue);
}
if (availableValue.compareTo(txValue) < 0) { var hashType = 1; // SIGHASH_ALL
throw new Error('Insufficient funds.');
}
console.log(selectedOuts); for (var i = 0; i < sendTx.ins.length; i++) {
var hash = sendTx.hashTransactionForSignature(selectedOuts[i].out.script, i, hashType);
var pubKeyHash = selectedOuts[i].out.script.simpleOutPubKeyHash();
var signature = this.signWithKey(pubKeyHash, hash);
var changeValue = availableValue.subtract(txValue); // Append hash type
signature.push(parseInt(hashType));
var sendTx = new Bitcoin.Transaction(); sendTx.ins[i].script = Script.createInputScript(signature, this.getPubKeyFromHash(pubKeyHash));
}
for (var i = 0; i < selectedOuts.length; i++) { console.log(sendTx);
sendTx.addInput(selectedOuts[i].tx, selectedOuts[i].index);
}
sendTx.addOutput(address, sendValue); console.log("pubkey: "+Crypto.util.bytesToHex(this.getPubKeyFromHash(pubKeyHash)));
if (changeValue.compareTo(BigInteger.ZERO) > 0) {
sendTx.addOutput(this.getNextAddress(), changeValue);
}
var hashType = 1; // SIGHASH_ALL return sendTx;
};
for (var i = 0; i < sendTx.ins.length; i++) { Wallet.prototype.clearTransactions = function () {
var hash = sendTx.hashTransactionForSignature(selectedOuts[i].out.script, i, hashType); this.txIndex = {};
var pubKeyHash = selectedOuts[i].out.script.simpleOutPubKeyHash(); this.unspentOuts = [];
var signature = this.signWithKey(pubKeyHash, hash); };
// Append hash type /**
signature.push(parseInt(hashType)); * 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);
sendTx.ins[i].script = Script.createInputScript(signature, this.getPubKeyFromHash(pubKeyHash)); // 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;
};
console.log(sendTx); return Wallet;
console.log("pubkey: "+Crypto.util.bytesToHex(this.getPubKeyFromHash(pubKeyHash)));
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) {
if (Bitcoin.Util.isArray(hash)) hash = Crypto.util.bytesToBase64(hash);
// 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;
};
return Wallet;
})(); })();