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