Browse Source

Merge branch 'tx-block-ver-signed'

patch-2
Gabe Gattis 8 years ago
parent
commit
6d2472b782
No known key found for this signature in database GPG Key ID: 441430987182732C
  1. 4
      lib/block/blockheader.js
  2. 6
      lib/encoding/bufferreader.js
  3. 4
      lib/transaction/transaction.js
  4. 10
      test/block/blockheader.js
  5. 6
      test/transaction/transaction.js

4
lib/block/blockheader.js

@ -136,7 +136,7 @@ BlockHeader.fromString = function fromString(str) {
*/ */
BlockHeader._fromBufferReader = function _fromBufferReader(br) { BlockHeader._fromBufferReader = function _fromBufferReader(br) {
var info = {}; var info = {};
info.version = br.readUInt32LE(); info.version = br.readInt32LE();
info.prevHash = br.read(32); info.prevHash = br.read(32);
info.merkleRoot = br.read(32); info.merkleRoot = br.read(32);
info.time = br.readUInt32LE(); info.time = br.readUInt32LE();
@ -191,7 +191,7 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
if (!bw) { if (!bw) {
bw = new BufferWriter(); bw = new BufferWriter();
} }
bw.writeUInt32LE(this.version); bw.writeInt32LE(this.version);
bw.write(this.prevHash); bw.write(this.prevHash);
bw.write(this.merkleRoot); bw.write(this.merkleRoot);
bw.writeUInt32LE(this.time); bw.writeUInt32LE(this.time);

6
lib/encoding/bufferreader.js

@ -83,6 +83,12 @@ BufferReader.prototype.readUInt32LE = function() {
return val; return val;
}; };
BufferReader.prototype.readInt32LE = function() {
var val = this.buf.readInt32LE(this.pos);
this.pos = this.pos + 4;
return val;
};
BufferReader.prototype.readUInt64BEBN = function() { BufferReader.prototype.readUInt64BEBN = function() {
var buf = this.buf.slice(this.pos, this.pos + 8); var buf = this.buf.slice(this.pos, this.pos + 8);
var bn = BN.fromBuffer(buf); var bn = BN.fromBuffer(buf);

4
lib/transaction/transaction.js

@ -279,7 +279,7 @@ Transaction.prototype.toBuffer = function() {
}; };
Transaction.prototype.toBufferWriter = function(writer) { Transaction.prototype.toBufferWriter = function(writer) {
writer.writeUInt32LE(this.version); writer.writeInt32LE(this.version);
writer.writeVarintNum(this.inputs.length); writer.writeVarintNum(this.inputs.length);
_.each(this.inputs, function(input) { _.each(this.inputs, function(input) {
input.toBufferWriter(writer); input.toBufferWriter(writer);
@ -301,7 +301,7 @@ Transaction.prototype.fromBufferReader = function(reader) {
$.checkArgument(!reader.finished(), 'No transaction data received'); $.checkArgument(!reader.finished(), 'No transaction data received');
var i, sizeTxIns, sizeTxOuts; var i, sizeTxIns, sizeTxOuts;
this.version = reader.readUInt32LE(); this.version = reader.readInt32LE();
sizeTxIns = reader.readVarintNum(); sizeTxIns = reader.readVarintNum();
for (i = 0; i < sizeTxIns; i++) { for (i = 0; i < sizeTxIns; i++) {
var input = Input.fromBufferReader(reader); var input = Input.fromBufferReader(reader);

10
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() { describe('#fromObject', function() {
it('should set all the variables', function() { it('should set all the variables', function() {

6
test/transaction/transaction.js

@ -28,6 +28,12 @@ describe('Transaction', function() {
transaction.uncheckedSerialize().should.equal(tx_1_hex); 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() { it('fails if an invalid parameter is passed to constructor', function() {
expect(function() { expect(function() {
return new Transaction(1); return new Transaction(1);

Loading…
Cancel
Save