diff --git a/lib/block.js b/lib/block.js index 7481c1878..4798945ff 100644 --- a/lib/block.js +++ b/lib/block.js @@ -16,6 +16,9 @@ var Block = function Block(magicnum, blocksize, blockheader, txsvi, txs) { txsvi: txsvi, txs: txs }); + } else if (Buffer.isBuffer(magicnum)) { + var blockbuf = magicnum; + this.fromBuffer(blockbuf); } else if (magicnum) { var obj = magicnum; } diff --git a/lib/blockheader.js b/lib/blockheader.js index 0d047a0b7..b24412e5d 100644 --- a/lib/blockheader.js +++ b/lib/blockheader.js @@ -13,6 +13,9 @@ var Blockheader = function Blockheader(version, prevblockidbuf, merklerootbuf, t bits: bits, nonce: nonce }); + } else if (Buffer.isBuffer(version)) { + var bhbuf = version; + this.fromBuffer(bhbuf); } else if (version) { var obj = version; this.set(obj); diff --git a/lib/transaction.js b/lib/transaction.js index 6b824baf2..aad3f4549 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -17,6 +17,9 @@ var Transaction = function Transaction(version, txinsvi, txins, txoutsvi, txouts txouts: txouts, nlocktime: nlocktime }); + } else if (Buffer.isBuffer(version)) { + var txbuf = version; + this.fromBuffer(txbuf); } else if (version) { var obj = version; this.set(obj); diff --git a/lib/txin.js b/lib/txin.js index 044174500..4eba87b4c 100644 --- a/lib/txin.js +++ b/lib/txin.js @@ -7,6 +7,8 @@ var Txin = function Txin(txidbuf, txoutnum, scriptvi, script, seqnum) { if (!(this instanceof Txin)) return new Txin(txidbuf, txoutnum, scriptvi, script, seqnum); if (Buffer.isBuffer(txidbuf)) { + if (txidbuf.length !== 32) + throw new Error('txidbuf must be 32 bytes'); this.txidbuf = txidbuf; this.txoutnum = txoutnum; this.scriptvi = scriptvi; diff --git a/test/block.js b/test/block.js index 2e2040e8b..85de6e97e 100644 --- a/test/block.js +++ b/test/block.js @@ -8,13 +8,6 @@ var Transaction = require('../lib/transaction'); describe('Block', function() { - it('should make a new block', function() { - var block = new Block(); - should.exist(block); - block = Block(); - should.exist(block); - }); - var txidhex = '8c9aa966d35bfeaf031409e0001b90ccdafd8d859799eb945a3c515b8260bcf2'; var txhex = '01000000029e8d016a7b0dc49a325922d05da1f916d1e4d4f0cb840c9727f3d22ce8d1363f000000008c493046022100e9318720bee5425378b4763b0427158b1051eec8b08442ce3fbfbf7b30202a44022100d4172239ebd701dae2fbaaccd9f038e7ca166707333427e3fb2a2865b19a7f27014104510c67f46d2cbb29476d1f0b794be4cb549ea59ab9cc1e731969a7bf5be95f7ad5e7f904e5ccf50a9dc1714df00fbeb794aa27aaff33260c1032d931a75c56f2ffffffffa3195e7a1ab665473ff717814f6881485dc8759bebe97e31c301ffe7933a656f020000008b48304502201c282f35f3e02a1f32d2089265ad4b561f07ea3c288169dedcf2f785e6065efa022100e8db18aadacb382eed13ee04708f00ba0a9c40e3b21cf91da8859d0f7d99e0c50141042b409e1ebbb43875be5edde9c452c82c01e3903d38fa4fd89f3887a52cb8aea9dc8aec7e2c9d5b3609c03eb16259a2537135a1bf0f9c5fbbcbdbaf83ba402442ffffffff02206b1000000000001976a91420bb5c3bfaef0231dc05190e7f1c8e22e098991e88acf0ca0100000000001976a9149e3e2d23973a04ec1b02be97c30ab9f2f27c3b2c88ac00000000'; var txbuf = new Buffer(txhex, 'hex'); @@ -40,6 +33,15 @@ describe('Block', function() { var genesisbuf = new Buffer(genesishex, 'hex'); var genesisidhex = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; + it('should make a new block', function() { + var block = new Block(); + should.exist(block); + block = Block(); + should.exist(block); + block = Block(blockbuf); + block.toBuffer().toString('hex').should.equal(blockhex); + }); + describe('#set', function() { it('should set these known values', function() { diff --git a/test/blockheader.js b/test/blockheader.js index a4d7c24eb..6fec516bc 100644 --- a/test/blockheader.js +++ b/test/blockheader.js @@ -5,13 +5,6 @@ var should = require('chai').should(); describe('Blockheader', function() { - it('should make a new blockheader', function() { - var blockheader = new Blockheader(); - should.exist(blockheader); - blockheader = Blockheader(); - should.exist(blockheader); - }); - var bh = new Blockheader(); var version = 1; var prevblockidbuf = new Buffer(32); @@ -32,6 +25,14 @@ describe('Blockheader', function() { bhhex = '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000'; bhbuf = new Buffer(bhhex, 'hex'); + it('should make a new blockheader', function() { + var blockheader = new Blockheader(); + should.exist(blockheader); + blockheader = Blockheader(); + should.exist(blockheader); + Blockheader(bhbuf).toBuffer().toString('hex').should.equal(bhhex); + }); + describe('#set', function() { it('should set all the variables', function() { diff --git a/test/transaction.js b/test/transaction.js index e43f6e79f..180f10a64 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -8,13 +8,6 @@ var BufferWriter = require('../lib/bufferwriter'); describe('Transaction', function() { - it('should make a new transaction', function() { - var tx = new Transaction(); - should.exist(tx); - tx = Transaction(); - should.exist(tx); - }); - var txin = Txin().fromBuffer(new Buffer('00000000000000000000000000000000000000000000000000000000000000000000000001ae00000000', 'hex')); var txout = Txout().fromBuffer(new Buffer('050000000000000001ae', 'hex')); var tx = Transaction().set({ @@ -32,6 +25,14 @@ describe('Transaction', function() { var tx2hex = '01000000029e8d016a7b0dc49a325922d05da1f916d1e4d4f0cb840c9727f3d22ce8d1363f000000008c493046022100e9318720bee5425378b4763b0427158b1051eec8b08442ce3fbfbf7b30202a44022100d4172239ebd701dae2fbaaccd9f038e7ca166707333427e3fb2a2865b19a7f27014104510c67f46d2cbb29476d1f0b794be4cb549ea59ab9cc1e731969a7bf5be95f7ad5e7f904e5ccf50a9dc1714df00fbeb794aa27aaff33260c1032d931a75c56f2ffffffffa3195e7a1ab665473ff717814f6881485dc8759bebe97e31c301ffe7933a656f020000008b48304502201c282f35f3e02a1f32d2089265ad4b561f07ea3c288169dedcf2f785e6065efa022100e8db18aadacb382eed13ee04708f00ba0a9c40e3b21cf91da8859d0f7d99e0c50141042b409e1ebbb43875be5edde9c452c82c01e3903d38fa4fd89f3887a52cb8aea9dc8aec7e2c9d5b3609c03eb16259a2537135a1bf0f9c5fbbcbdbaf83ba402442ffffffff02206b1000000000001976a91420bb5c3bfaef0231dc05190e7f1c8e22e098991e88acf0ca0100000000001976a9149e3e2d23973a04ec1b02be97c30ab9f2f27c3b2c88ac00000000'; var tx2buf = new Buffer(tx2hex, 'hex'); + it('should make a new transaction', function() { + var tx = new Transaction(); + should.exist(tx); + tx = Transaction(); + should.exist(tx); + Transaction(txbuf).toBuffer().toString('hex').should.equal(txhex); + }); + describe('#set', function() { it('should set all the basic parameters', function() { diff --git a/test/txin.js b/test/txin.js index 92d386ab1..6a4a5b967 100644 --- a/test/txin.js +++ b/test/txin.js @@ -6,13 +6,6 @@ var BufferReader = require('../lib/bufferreader'); describe('Txin', function() { - it('should make a new txin', function() { - var txin = new Txin(); - should.exist(txin); - txin = Txin(); - should.exist(txin); - }); - var txidbuf = new Buffer(32); txidbuf.fill(0); var txoutnum = 0; @@ -27,6 +20,21 @@ describe('Txin', function() { seqnum: seqnum }); + it('should make a new txin', function() { + var txin = new Txin(); + should.exist(txin); + txin = Txin(); + should.exist(txin); + var txidbuf = new Buffer(32); + txidbuf.fill(0); + Txin(txidbuf).txidbuf.length.should.equal(32); + (function() { + var txidbuf2 = new Buffer(33); + txidbuf2.fill(0); + Txin(txidbuf2); + }).should.throw('txidbuf must be 32 bytes'); + }); + describe('#set', function() { it('should set these vars', function() { diff --git a/test/txout.js b/test/txout.js index fc3cd9ab6..2f186306b 100644 --- a/test/txout.js +++ b/test/txout.js @@ -8,13 +8,6 @@ var BufferWriter = require('../lib/bufferwriter'); describe('Txout', function() { - it('should make a new txout', function() { - var txout = new Txout(); - should.exist(txout); - txout = Txout(); - should.exist(txout); - }); - var valuebn = BN(5); var script = Script().fromString("OP_CHECKMULTISIG"); var scriptvi = Varint(script.toBuffer().length); @@ -24,6 +17,14 @@ describe('Txout', function() { script: script }); + it('should make a new txout', function() { + var txout = new Txout(); + should.exist(txout); + txout = Txout(); + should.exist(txout); + Txout(valuebn, scriptvi, script).valuebn.toString().should.equal('5'); + }); + describe('#set', function() { it('should set this object', function() {