Started work on wallet, and added hex serialization

This commit is contained in:
vub 2013-11-20 13:00:49 -05:00
parent 5e2a9f0c26
commit 93e50e9433
3 changed files with 41 additions and 95 deletions

2
bitcoinjs-min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -163,6 +163,10 @@ Transaction.prototype.serialize = function ()
return buffer; return buffer;
}; };
Transaction.prototype.serializeHex = function() {
return conv.bytesToHex(this.serialize());
}
var OP_CODESEPARATOR = 171; var OP_CODESEPARATOR = 171;
var SIGHASH_ALL = 1; var SIGHASH_ALL = 1;

View file

@ -5,10 +5,15 @@ var util = require('./util');
var BigInteger = require('./jsbn/jsbn'); var BigInteger = require('./jsbn/jsbn');
var BIP32key = require('./bip32');
var Transaction = require('./transaction').Transaction; var Transaction = require('./transaction').Transaction;
var TransactionIn = require('./transaction').TransactionIn; var TransactionIn = require('./transaction').TransactionIn;
var TransactionOut = require('./transaction').TransactionOut; var TransactionOut = require('./transaction').TransactionOut;
var SecureRandom = require('./jsbn/rng');
var rng = new SecureRandom();
var Wallet = function () { var Wallet = function () {
// Keychain // Keychain
// //
@ -18,6 +23,7 @@ var Wallet = function () {
// Any functions accessing this value therefore have to be defined in // Any functions accessing this value therefore have to be defined in
// the closure of this constructor. // the closure of this constructor.
var keys = []; var keys = [];
var masterkey = null;
// Public hashes of our keys // Public hashes of our keys
this.addressHashes = []; this.addressHashes = [];
@ -29,50 +35,18 @@ var Wallet = function () {
// Other fields // Other fields
this.addressPointer = 0; this.addressPointer = 0;
/** this.genMasterkey = function(seed) {
* Add a key to the keychain. if (!seed) {
* var seedBytes = new Array(32);
* The corresponding public key can be provided as a second parameter. This rng.nextBytes(seedBytes);
* adds it to the cache in the ECKey object and avoid the need to seed = conv.bytesToString(seedBytes)
* expensively calculate it later.
*/
this.addKey = function (key, pub) {
if (!(key instanceof ECKey)) {
key = new ECKey(key);
} }
keys.push(key); masterkey = new BIP32key(seed);
if (pub) {
if ("string" === typeof pub) {
pub = conv.hexToBytes(pub);
}
key.setPub(pub);
} }
this.addressHashes.push(key.getBitcoinAddress().getHash()); this.generateAddress = function() {
}; keys.push(masterkey.ckd(keys.length))
/**
* Add multiple keys at once.
*/
this.addKeys = function (keys, pubs) {
if ("string" === typeof keys) {
keys = keys.split(',');
} }
if ("string" === typeof pubs) {
pubs = pubs.split(',');
}
var i;
if (Array.isArray(pubs) && keys.length == pubs.length) {
for (i = 0; i < keys.length; i++) {
this.addKey(keys[i], pubs[i]);
}
} else {
for (i = 0; i < keys.length; i++) {
this.addKey(keys[i]);
}
}
};
/** /**
* Get the key chain. * Get the key chain.
@ -80,15 +54,22 @@ var Wallet = function () {
* Returns an array of hex-encoded private values. * Returns an array of hex-encoded private values.
*/ */
this.getKeys = function () { this.getKeys = function () {
var serializedWallet = []; var keyExport = [];
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
serializedWallet.push(keys[i].toString()); keyExport.push(keys[i].toString());
} }
return serializedWallet; return keyExport;
}; };
this.privateSerialize = function() {
return {
masterkey: masterkey,
keys: this.getKeys()
}
}
/** /**
* Get the public keys. * Get the public keys.
* *
@ -109,6 +90,7 @@ var Wallet = function () {
*/ */
this.clear = function () { this.clear = function () {
keys = []; keys = [];
masterkey = null;
}; };
/** /**
@ -132,38 +114,13 @@ var Wallet = function () {
}; };
this.getCurAddress = function () { this.getCurAddress = function () {
if (keys[this.addressPointer]) { if (keys[keys.length - 1]) {
return keys[this.addressPointer].getBitcoinAddress(); return keys[keys.length - 1].getBitcoinAddress();
} else { } else {
return null; return null;
} }
}; };
/**
* Go to the next address.
*
* If there are no more new addresses available, one will be generated
* automatically.
*/
this.getNextAddress = function () {
if (keys.length === 0) {
this.generateAddress();
}
else {
}
/*
this.addressPointer++;
if (!keys[this.addressPointer]) {
this.generateAddress();
}
*/
// TODO(shtylman) this shit is trying to be too smart
// it is making a new address when it shouldn't
// it should just stop being so "smart" and just do what it is told
return keys[this.addressPointer].getBitcoinAddress();
};
/** /**
* Sign a hash with a key. * Sign a hash with a key.
* *
@ -197,10 +154,6 @@ var Wallet = function () {
}; };
}; };
Wallet.prototype.generateAddress = function () {
this.addKey(new ECKey());
};
// return unspent transactions // return unspent transactions
Wallet.prototype.unspentTx = function() { Wallet.prototype.unspentTx = function() {
return this.unspentOuts; return this.unspentOuts;
@ -227,7 +180,7 @@ Wallet.prototype.process = function (tx) {
for (k = 0; k < this.addressHashes.length; k++) { for (k = 0; k < this.addressHashes.length; k++) {
// if our address, then we add the unspent out to a list of unspent outputs // if our address, then we add the unspent out to a list of unspent outputs
if (this.addressHashes[k] === hash) { if (this.addressHashes[k] === hash) {
this.unspentOuts.push({tx: tx, index: j, out: txout}); this.unspentOuts.push({tx: tx, index: j, output: txout});
break; break;
} }
} }
@ -264,7 +217,7 @@ Wallet.prototype.process = function (tx) {
}; };
Wallet.prototype.getBalance = function () { Wallet.prototype.getBalance = function () {
return this.unspentOuts.reduce(function(t,o) { return t + o.out.value },0); return this.unspentOuts.reduce(function(t,o) { return t + o.output.value },0);
}; };
Wallet.prototype.createSend = function (address, sendValue, feeValue) { Wallet.prototype.createSend = function (address, sendValue, feeValue) {
@ -275,7 +228,7 @@ Wallet.prototype.createSend = function (address, sendValue, feeValue) {
for (i = 0; i < this.unspentOuts.length; i++) { for (i = 0; i < this.unspentOuts.length; i++) {
var txout = this.unspentOuts[i]; var txout = this.unspentOuts[i];
selectedOuts.push(txout); selectedOuts.push(txout);
availableValue += txout.out.value; availableValue += txout.output.value;
if (availableValue >= txValue) break; if (availableValue >= txValue) break;
} }
@ -294,23 +247,12 @@ Wallet.prototype.createSend = function (address, sendValue, feeValue) {
sendTx.addOutput(address, sendValue); sendTx.addOutput(address, sendValue);
if (changeValue > 0) { if (changeValue > 0) {
sendTx.addOutput(this.getNextAddress(), changeValue); sendTx.addOutput(this.getCurAddress(), changeValue);
} }
var hashType = 1; // SIGHASH_ALL var hashType = 1; // SIGHASH_ALL
for (i = 0; i < sendTx.ins.length; i++) { sendTx.signWithKeys(this.getKeys(), selectedOuts, hashType)
var hash = sendTx.hashTransactionForSignature(selectedOuts[i].out.script, i, hashType);
var pubKeyHash = selectedOuts[i].out.script.simpleOutPubKeyHash();
// this changes because signing uses a random number generator
var signature = this.signWithKey(pubKeyHash, hash);
// Append hash type
signature.push(parseInt(hashType, 10));
sendTx.ins[i].script = Script.createInputScript(signature, this.getPubKeyFromHash(pubKeyHash));
}
return sendTx; return sendTx;
}; };