diff --git a/src/transaction.js b/src/transaction.js index cf7160d..afd21da 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -12,7 +12,7 @@ var SHA256 = require('crypto-js/sha256'); var Transaction = function (doc) { if (!(this instanceof Transaction)) { return new Transaction(doc); } this.version = 1; - this.lock_time = 0; + this.locktime = 0; this.ins = []; this.outs = []; this.timestamp = null; @@ -24,7 +24,7 @@ var Transaction = function (doc) { } if (doc.hash) this.hash = doc.hash; if (doc.version) this.version = doc.version; - if (doc.lock_time) this.lock_time = doc.lock_time; + if (doc.locktime) this.locktime = doc.locktime; if (doc.ins && doc.ins.length) { for (var i = 0; i < doc.ins.length; i++) { this.addInput(new TransactionIn(doc.ins[i])); @@ -37,6 +37,8 @@ var Transaction = function (doc) { } if (doc.timestamp) this.timestamp = doc.timestamp; if (doc.block) this.block = doc.block; + + this.hash = this.hash || this.getHash() } }; @@ -144,7 +146,7 @@ Transaction.prototype.serialize = function () { buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(scriptBytes); } - buffer = buffer.concat(convert.numToBytes(parseInt(this.lock_time),4)); + buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime),4)); return buffer; }; @@ -230,7 +232,7 @@ Transaction.prototype.clone = function () { var newTx = new Transaction(); newTx.version = this.version; - newTx.lock_time = this.lock_time; + newTx.locktime = this.locktime; for (var i = 0; i < this.ins.length; i++) { var txin = this.ins[i].clone(); newTx.addInput(txin); diff --git a/test/transaction.js b/test/transaction.js index 53e9fd5..7f1cd30 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -14,13 +14,52 @@ describe('Transaction', function() { '3901ffffffff0100f2052a010000001976a914dd40dedd8f7e3746662', '4c4dacc6362d8e7be23dd88ac00000000' ].join('') + tx = Transaction.deserialize(serializedTx) }) - it('works', function() { - var actual = Transaction.deserialize(serializedTx).serialize() + it('returns the original after serialized again', function() { + var actual = tx.serialize() var expected = convert.hexToBytes(serializedTx) assert.deepEqual(actual, expected) }) + + it('decodes version correctly', function(){ + assert.equal(tx.version, 1) + }) + + it('decodes locktime correctly', function(){ + assert.equal(tx.locktime, 0) + }) + + it('decodes inputs correctly', function(){ + assert.equal(tx.ins.length, 1) + + var input = tx.ins[0] + assert.equal(input.sequence, 4294967295) + + assert.equal(input.outpoint.index, 0) + assert.equal(input.outpoint.hash, "69d02fc05c4e0ddc87e796eee42693c244a3112fffe1f762c3fb61ffcb304634") + + assert.equal(convert.bytesToHex(input.script.buffer), + "493046022100ef89701f460e8660c80808a162bbf2d676f40a331a243592c36d6bd1f81d6bdf022100d29c072f1b18e59caba6e1f0b8cadeb373fd33a25feded746832ec179880c23901") + }) + + it('decodes outputs correctly', function(){ + assert.equal(tx.outs.length, 1) + + var output = tx.outs[0] + + assert.equal(output.value, 5000000000) + assert.equal(convert.bytesToHex(output.script.toScriptHash()), "dd40dedd8f7e37466624c4dacc6362d8e7be23dd") + // assert.equal(output.address.toString(), "n1gqLjZbRH1biT5o4qiVMiNig8wcCPQeB9") + // TODO: address is wrong because it's a testnet transaction. Transaction needs to support testnet + }) + + it('assigns hash to deserialized object', function(){ + var hashHex = "a9d4599e15b53f3eb531608ddb31f48c695c3d0b3538a6bda871e8b34f2f430c" + assert.deepEqual(tx.hash, convert.hexToBytes(hashHex)) + }) }) }) +