diff --git a/lib/block/blockheader.js b/lib/block/blockheader.js index d79a984..45b1b13 100644 --- a/lib/block/blockheader.js +++ b/lib/block/blockheader.js @@ -136,7 +136,7 @@ BlockHeader.fromString = function fromString(str) { */ BlockHeader._fromBufferReader = function _fromBufferReader(br) { var info = {}; - info.version = br.readUInt32LE(); + info.version = br.readInt32LE(); info.prevHash = br.read(32); info.merkleRoot = br.read(32); info.time = br.readUInt32LE(); @@ -191,7 +191,7 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) { if (!bw) { bw = new BufferWriter(); } - bw.writeUInt32LE(this.version); + bw.writeInt32LE(this.version); bw.write(this.prevHash); bw.write(this.merkleRoot); bw.writeUInt32LE(this.time); diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index d7ca157..644cafb 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -83,6 +83,12 @@ BufferReader.prototype.readUInt32LE = function() { return val; }; +BufferReader.prototype.readInt32LE = function() { + var val = this.buf.readInt32LE(this.pos); + this.pos = this.pos + 4; + return val; +}; + BufferReader.prototype.readUInt64BEBN = function() { var buf = this.buf.slice(this.pos, this.pos + 8); var bn = BN.fromBuffer(buf); diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 9766024..276b7fb 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -279,7 +279,7 @@ Transaction.prototype.toBuffer = function() { }; Transaction.prototype.toBufferWriter = function(writer) { - writer.writeUInt32LE(this.version); + writer.writeInt32LE(this.version); writer.writeVarintNum(this.inputs.length); _.each(this.inputs, function(input) { input.toBufferWriter(writer); @@ -301,7 +301,7 @@ Transaction.prototype.fromBufferReader = function(reader) { $.checkArgument(!reader.finished(), 'No transaction data received'); var i, sizeTxIns, sizeTxOuts; - this.version = reader.readUInt32LE(); + this.version = reader.readInt32LE(); sizeTxIns = reader.readVarintNum(); for (i = 0; i < sizeTxIns; i++) { var input = Input.fromBufferReader(reader); diff --git a/test/block/blockheader.js b/test/block/blockheader.js index 4bf53ae..07e5008 100644 --- a/test/block/blockheader.js +++ b/test/block/blockheader.js @@ -79,6 +79,16 @@ describe('BlockHeader', function() { }); + describe('version', function() { + it('is interpreted as an int32le', function() { + var hex = 'ffffffff00000000000000000000000000000000000000000000000000000000000000004141414141414141414141414141414141414141414141414141414141414141010000000200000003000000'; + var header = BlockHeader.fromBuffer(new Buffer(hex, 'hex')); + header.version.should.equal(-1); + header.timestamp.should.equal(1); + }); + }); + + describe('#fromObject', function() { it('should set all the variables', function() { diff --git a/test/transaction/transaction.js b/test/transaction/transaction.js index 9709176..994150d 100644 --- a/test/transaction/transaction.js +++ b/test/transaction/transaction.js @@ -28,6 +28,12 @@ describe('Transaction', function() { transaction.uncheckedSerialize().should.equal(tx_1_hex); }); + it('should parse the version as a signed integer', function () { + var transaction = Transaction('ffffffff0000ffffffff') + transaction.version.should.equal(-1); + transaction.nLockTime.should.equal(0xffffffff); + }); + it('fails if an invalid parameter is passed to constructor', function() { expect(function() { return new Transaction(1);