diff --git a/src/transaction.js b/src/transaction.js index 45a8b12..2eda549 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -125,7 +125,11 @@ Transaction.prototype.serialize = function () buffer = buffer.concat(util.numToVarInt(this.ins.length)); for (var i = 0; i < this.ins.length; i++) { var txin = this.ins[i]; - buffer = buffer.concat(conv.base64ToBytes(txin.outpoint.hash)); + + // the hash is flipped to what the fuck here? + // this seems to be the only thing that I don't understand + buffer = buffer.concat(conv.hexToBytes(txin.outpoint.hash)); + buffer = buffer.concat(wordsToBytes([parseInt(txin.outpoint.index)]).reverse()); var scriptBytes = txin.script.buffer; buffer = buffer.concat(util.numToVarInt(scriptBytes.length)); @@ -415,8 +419,12 @@ var TransactionIn = function (data) if (data.script instanceof Script) { this.script = data.script; } else { - //this.script = new Script(data.script); - this.script = Script.fromScriptSig(data.scriptSig); + if (data.scriptSig) { + this.script = Script.fromScriptSig(data.scriptSig); + } + else { + this.script = new Script(data.script); + } } this.sequence = data.sequence; }; @@ -439,8 +447,12 @@ var TransactionOut = function (data) if (data.script instanceof Script) { this.script = data.script; } else { - //this.script = new Script(data.script); - this.script = Script.fromPubKey(data.scriptPubKey); + if (data.scriptPubKey) { + this.script = Script.fromScriptSig(data.scriptPubKey); + } + else { + this.script = new Script(data.script); + } } if (util.isArray(data.value)) { diff --git a/src/wallet.js b/src/wallet.js index 40d1891..a9d1847 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -201,6 +201,11 @@ Wallet.prototype.generateAddress = function () { this.addKey(new ECKey()); }; +// return unspent transactions +Wallet.prototype.unspentTx = function() { + return this.unspentOuts; +}; + /** * Add a transaction to the wallet's processed transaction. * @@ -215,9 +220,12 @@ Wallet.prototype.process = function (tx) { var hash; // Gather outputs for (j = 0; j < tx.out.length; j++) { - var txout = new TransactionOut(tx.out[j]); + var raw_tx = tx.out[j]; + var txout = new TransactionOut(raw_tx); + // this hash is the base64 hash of the pubkey which is the address the output when to hash = conv.bytesToBase64(txout.script.simpleOutPubKeyHash()); for (k = 0; k < this.addressHashes.length; k++) { + // if our address, then we add the unspent out to a list of unspent outputs if (this.addressHashes[k] === hash) { this.unspentOuts.push({tx: tx, index: j, out: txout}); break; @@ -227,7 +235,15 @@ Wallet.prototype.process = function (tx) { // Remove spent outputs for (j = 0; j < tx.in.length; j++) { - var txin = new TransactionIn(tx.in[j]); + var raw_tx = tx.in[j]; + + // mangle into the format TransactionIn expects + raw_tx.outpoint = { + hash: raw_tx.prev_out.hash, + index: raw_tx.prev_out.n + }; + + var txin = new TransactionIn(raw_tx); var pubkey = txin.script.simpleInPubKey(); hash = conv.bytesToBase64(util.sha256ripe160(pubkey)); for (k = 0; k < this.addressHashes.length; k++) { @@ -291,6 +307,8 @@ Wallet.prototype.createSend = function (address, sendValue, feeValue) { 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(); + + // this changes because signing uses a random number generator var signature = this.signWithKey(pubKeyHash, hash); // Append hash type