Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'tx-block-ver-signed'
  • Loading branch information
gabegattis committed Feb 16, 2017
2 parents 3cd8de1 + 2bfb4ce commit 6d2472b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/block/blockheader.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions lib/encoding/bufferreader.js
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/transaction/transaction.js
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/block/blockheader.js
Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions test/transaction/transaction.js
Expand Up @@ -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);
Expand Down

0 comments on commit 6d2472b

Please sign in to comment.