Browse Source

Merge pull request #3 from booo/master

Cleanup and getNextAddress.
hk-custom-address
Stefan Thomas 13 years ago
parent
commit
a5f9afde5a
  1. 46
      src/wallet.js

46
src/wallet.js

@ -38,13 +38,13 @@ Bitcoin.Wallet = (function () {
if ("string" === typeof pubs) { if ("string" === typeof pubs) {
pubs = pubs.split(','); pubs = pubs.split(',');
} }
console.log(pubs); var i;
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 (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 (i = 0; i < keys.length; i++) {
this.addKey(keys[i]); this.addKey(keys[i]);
} }
} }
@ -95,13 +95,11 @@ Bitcoin.Wallet = (function () {
}; };
this.getNextAddress = function () { this.getNextAddress = function () {
if (keys.length) { this.addressPointer++;
// TODO: Create new addresses if we run out if(!keys[this.addressPointer]) {
this.addressPointer = (this.addressPointer + 1) % keys.length; this.generateAddress();
return keys[this.addressPointer].getBitcoinAddress();
} else {
return null;
} }
return keys[this.addressPointer].getBitcoinAddress();
}; };
this.signWithKey = function (pubKeyHash, hash) { this.signWithKey = function (pubKeyHash, hash) {
@ -118,7 +116,6 @@ Bitcoin.Wallet = (function () {
pubKeyHash = Crypto.util.bytesToBase64(pubKeyHash); pubKeyHash = Crypto.util.bytesToBase64(pubKeyHash);
for (var i = 0; i < this.addressHashes.length; i++) { for (var i = 0; i < this.addressHashes.length; i++) {
if (this.addressHashes[i] == pubKeyHash) { if (this.addressHashes[i] == pubKeyHash) {
console.log(Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(keys[i].getPub())), pubKeyHash);
return keys[i].getPub(); return keys[i].getPub();
} }
} }
@ -133,11 +130,14 @@ Bitcoin.Wallet = (function () {
Wallet.prototype.process = function (tx) { Wallet.prototype.process = function (tx) {
if (this.txIndex[tx.hash]) return; if (this.txIndex[tx.hash]) return;
var j;
var k;
var hash;
// Gather outputs // Gather outputs
for (var j = 0; j < tx.outs.length; j++) { for (j = 0; j < tx.outs.length; j++) {
var txout = new TransactionOut(tx.outs[j]); var txout = new TransactionOut(tx.outs[j]);
var hash = Crypto.util.bytesToBase64(txout.script.simpleOutPubKeyHash()); hash = Crypto.util.bytesToBase64(txout.script.simpleOutPubKeyHash());
for (var k = 0; k < this.addressHashes.length; k++) { for (k = 0; k < this.addressHashes.length; k++) {
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, out: txout});
break; break;
@ -146,11 +146,11 @@ Bitcoin.Wallet = (function () {
} }
// Remove spent outputs // Remove spent outputs
for (var j = 0; j < tx.ins.length; j++) { for (j = 0; j < tx.ins.length; j++) {
var txin = new TransactionIn(tx.ins[j]); var txin = new TransactionIn(tx.ins[j]);
var pubkey = txin.script.simpleInPubKey(); var pubkey = txin.script.simpleInPubKey();
var hash = Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(pubkey)); hash = Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(pubkey));
for (var k = 0; k < this.addressHashes.length; k++) { for (k = 0; k < this.addressHashes.length; k++) {
if (this.addressHashes[k] === hash) { if (this.addressHashes[k] === hash) {
for (var l = 0; l < this.unspentOuts.length; l++) { for (var l = 0; l < this.unspentOuts.length; l++) {
if (txin.outpoint.hash == this.unspentOuts[l].tx.hash && if (txin.outpoint.hash == this.unspentOuts[l].tx.hash &&
@ -180,7 +180,8 @@ Bitcoin.Wallet = (function () {
var selectedOuts = []; var selectedOuts = [];
var txValue = sendValue.add(feeValue); var txValue = sendValue.add(feeValue);
var availableValue = BigInteger.ZERO; var availableValue = BigInteger.ZERO;
for (var i = 0; i < this.unspentOuts.length; i++) { var i;
for (i = 0; i < this.unspentOuts.length; i++) {
selectedOuts.push(this.unspentOuts[i]); selectedOuts.push(this.unspentOuts[i]);
availableValue = availableValue.add(Bitcoin.Util.valueToBigInt(this.unspentOuts[i].out.value)); availableValue = availableValue.add(Bitcoin.Util.valueToBigInt(this.unspentOuts[i].out.value));
@ -191,13 +192,12 @@ Bitcoin.Wallet = (function () {
throw new Error('Insufficient funds.'); throw new Error('Insufficient funds.');
} }
console.log(selectedOuts);
var changeValue = availableValue.subtract(txValue); var changeValue = availableValue.subtract(txValue);
var sendTx = new Bitcoin.Transaction(); var sendTx = new Bitcoin.Transaction();
for (var i = 0; i < selectedOuts.length; i++) { for (i = 0; i < selectedOuts.length; i++) {
sendTx.addInput(selectedOuts[i].tx, selectedOuts[i].index); sendTx.addInput(selectedOuts[i].tx, selectedOuts[i].index);
} }
@ -208,21 +208,17 @@ Bitcoin.Wallet = (function () {
var hashType = 1; // SIGHASH_ALL var hashType = 1; // SIGHASH_ALL
for (var i = 0; i < sendTx.ins.length; i++) { for (i = 0; i < sendTx.ins.length; i++) {
var hash = sendTx.hashTransactionForSignature(selectedOuts[i].out.script, i, hashType); var hash = sendTx.hashTransactionForSignature(selectedOuts[i].out.script, i, hashType);
var pubKeyHash = selectedOuts[i].out.script.simpleOutPubKeyHash(); var pubKeyHash = selectedOuts[i].out.script.simpleOutPubKeyHash();
var signature = this.signWithKey(pubKeyHash, hash); var signature = this.signWithKey(pubKeyHash, hash);
// Append hash type // Append hash type
signature.push(parseInt(hashType)); signature.push(parseInt(hashType, 10));
sendTx.ins[i].script = Script.createInputScript(signature, this.getPubKeyFromHash(pubKeyHash)); sendTx.ins[i].script = Script.createInputScript(signature, this.getPubKeyFromHash(pubKeyHash));
} }
console.log(sendTx);
console.log("pubkey: "+Crypto.util.bytesToHex(this.getPubKeyFromHash(pubKeyHash)));
return sendTx; return sendTx;
}; };

Loading…
Cancel
Save