From ed137a3b9dd0433f14e6b9e117699183c138d710 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 24 Mar 2014 06:01:33 +1100 Subject: [PATCH] Various stylistic changes --- src/base58.js | 6 ++++-- src/convert.js | 4 ++-- src/opcode.js | 2 +- src/transaction.js | 43 ++++++++++++++++++++++++++++--------------- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/base58.js b/src/base58.js index 1264196..2cbb0ae 100644 --- a/src/base58.js +++ b/src/base58.js @@ -36,7 +36,7 @@ module.exports.encode = function (input) { } return chars.reverse().join(''); -}, +} // decode a base58 string into a byte array // input should be a base58 encoded string @@ -88,10 +88,13 @@ module.exports.checkDecode = function(input) { var bytes = module.exports.decode(input), front = bytes.slice(0,bytes.length-4), back = bytes.slice(bytes.length-4); + var checksum = getChecksum(front) + if (""+checksum != ""+back) { throw new Error("Checksum failed"); } + var o = front.slice(1); o.version = front[0]; return o; @@ -103,4 +106,3 @@ function getChecksum(bytes) { } module.exports.getChecksum = getChecksum - diff --git a/src/convert.js b/src/convert.js index 27154be..f042de0 100644 --- a/src/convert.js +++ b/src/convert.js @@ -53,8 +53,8 @@ exports.base64ToBytes = function(base64) { // Remove non-base-64 characters base64 = base64.replace(/[^A-Z0-9+\/]/ig, ''); - var bytes = [] - , imod4 = 0 + var bytes = []; + var imod4 = 0; for (var i = 0; i < base64.length; imod4 = ++i % 4) { if (!imod4) continue diff --git a/src/opcode.js b/src/opcode.js index 209830b..6fc8aad 100644 --- a/src/opcode.js +++ b/src/opcode.js @@ -144,4 +144,4 @@ for(var i in Opcode.map) { Opcode.reverseMap[Opcode.map[i]] = i } -module.exports = Opcode +module.exports = Opcode; diff --git a/src/transaction.js b/src/transaction.js index 176fb74..d01a920 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -13,12 +13,13 @@ var Transaction = function (doc) { this.locktime = 0; this.ins = []; this.outs = []; - this.defaultSequence = [255, 255, 255, 255] // 0xFFFFFFFF + this.defaultSequence = [255, 255, 255, 255]; // 0xFFFFFFFF if (doc) { if (typeof doc == "string" || Array.isArray(doc)) { - doc = Transaction.deserialize(doc) + doc = Transaction.deserialize(doc); } + if (doc.hash) this.hash = doc.hash; if (doc.version) this.version = doc.version; if (doc.locktime) this.locktime = doc.locktime; @@ -27,13 +28,14 @@ var Transaction = function (doc) { this.addInput(new TransactionIn(doc.ins[i])); } } + if (doc.outs && doc.outs.length) { for (var i = 0; i < doc.outs.length; i++) { this.addOutput(new TransactionOut(doc.outs[i])); } } - this.hash = this.hash || this.getHash() + this.hash = this.hash || this.getHash(); } }; @@ -86,11 +88,13 @@ Transaction.prototype.addOutput = function (address, value) { this.outs.push(arguments[0]); return; } + if (arguments[0].indexOf(':') >= 0) { var args = arguments[0].split(':'); address = args[0]; value = parseInt(args[1]); } + this.outs.push(new TransactionOut({ value: value, script: Script.createOutputScript(address) @@ -106,7 +110,7 @@ Transaction.prototype.addOutput = function (address, value) { */ Transaction.prototype.serialize = function () { var buffer = []; - buffer = buffer.concat(convert.numToBytes(parseInt(this.version),4)); + buffer = buffer.concat(convert.numToBytes(parseInt(this.version), 4)); buffer = buffer.concat(convert.numToVarInt(this.ins.length)); for (var i = 0; i < this.ins.length; i++) { var txin = this.ins[i]; @@ -115,7 +119,8 @@ Transaction.prototype.serialize = function () { // else use little-endian hashes? No idea... buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse()); - buffer = buffer.concat(convert.numToBytes(parseInt(txin.outpoint.index),4)); + buffer = buffer.concat(convert.numToBytes(parseInt(txin.outpoint.index), 4)); + var scriptBytes = txin.script.buffer; buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(scriptBytes); @@ -125,11 +130,13 @@ Transaction.prototype.serialize = function () { for (var i = 0; i < this.outs.length; i++) { var txout = this.outs[i]; buffer = buffer.concat(convert.numToBytes(txout.value,8)); + var scriptBytes = txout.script.buffer; buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(scriptBytes); - } - buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime),4)); + }); + + buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime), 4)); return buffer; }; @@ -191,9 +198,9 @@ function (connectedScript, inIndex, hashType) var buffer = txTmp.serialize(); - buffer = buffer.concat(convert.numToBytes(parseInt(hashType),4)); - + buffer = buffer.concat(convert.numToBytes(parseInt(hashType), 4)); buffer = convert.bytesToWordArray(buffer); + return convert.wordArrayToBytes(SHA256(SHA256(buffer))); }; @@ -291,7 +298,7 @@ Transaction.deserialize = function(buffer) { Transaction.prototype.sign = function(index, key, type) { type = type || SIGHASH_ALL; key = new ECKey(key); - + // TODO: getPub is slow, sha256ripe160 probably is too. // This could be sped up a lot by providing these as inputs. var pub = key.getPub().export('bytes'), @@ -305,6 +312,7 @@ Transaction.prototype.sign = function(index, key, type) { // Takes outputs of the form [{ output: 'txhash:index', address: 'address' },...] Transaction.prototype.signWithKeys = function(keys, outputs, type) { type = type || SIGHASH_ALL; + var addrdata = keys.map(function(key) { key = new ECKey(key); return { @@ -312,14 +320,17 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) { address: key.getAddress().toString() } }); + var hmap = {}; for (var o in outputs) { hmap[outputs[o].output] = outputs[o]; } for (var i = 0; i < this.ins.length; i++) { - var outpoint = this.ins[i].outpoint.hash+':'+this.ins[i].outpoint.index, - histItem = hmap[outpoint]; + var outpoint = this.ins[i].outpoint.hash + ':' + this.ins[i].outpoint.index; + var histItem = hmap[outpoint]; + if (!histItem) continue; + var thisInputAddrdata = addrdata.filter(function(a) { return a.address == histItem.address; }); @@ -410,6 +421,8 @@ TransactionOut.prototype.clone = function () return newTxout; }; -module.exports.Transaction = Transaction; -module.exports.TransactionIn = TransactionIn; -module.exports.TransactionOut = TransactionOut; +module.exports = { + Transaction: Transaction, + TransactionIn: TransactionIn, + TransactionOut: TransactionOut +}