Browse Source

transaction: refactor witness properties

patch-2
Braydon Fuller 9 years ago
committed by Braydon Fuller
parent
commit
1e749e0055
  1. 26
      lib/transaction/transaction.js
  2. 7
      test/transaction/transaction.js

26
lib/transaction/transaction.js

@ -39,7 +39,6 @@ function Transaction(serialized) {
this.inputs = [];
this.outputs = [];
this.witness = [];
this._hasWitness = false;
this._inputAmount = undefined;
this._outputAmount = undefined;
@ -59,7 +58,6 @@ function Transaction(serialized) {
this._newTransaction();
}
}
var SERIALIZE_TRANSACTION_WITNESS = 0x40000000;
var CURRENT_VERSION = 1;
var DEFAULT_NLOCKTIME = 0;
var MAX_BLOCK_SIZE = 1000000;
@ -293,15 +291,18 @@ Transaction.prototype.inspect = function() {
return '<Transaction: ' + this.uncheckedSerialize() + '>';
};
Transaction.prototype.toBuffer = function(nonWitness) {
Transaction.prototype.toBuffer = function(noWitness) {
var writer = new BufferWriter();
return this.toBufferWriter(writer, nonWitness).toBuffer();
return this.toBufferWriter(writer, noWitness).toBuffer();
};
Transaction.prototype.toBufferWriter = function(writer, nonWitness) {
writer.writeInt32LE(this.version);
writer.writeUInt32LE(this.version);
if (this._hasWitness && !nonWitness) {
Transaction.prototype.hasWitness = function() {
return this.witness && this.witness.length > 0;
};
Transaction.prototype.toBufferWriter = function(writer, noWitness) {
writer.writeInt32LE(this.version);
if (this.hasWitness() && !noWitness) {
writer.write(new Buffer('0001', 'hex'));
}
writer.writeVarintNum(this.inputs.length);
@ -312,7 +313,7 @@ Transaction.prototype.toBufferWriter = function(writer, nonWitness) {
_.each(this.outputs, function(output) {
output.toBufferWriter(writer);
});
if (this._hasWitness && !nonWitness) {
if (this.hasWitness() && !noWitness) {
this._toBufferWriterWitness(writer);
}
writer.writeUInt32LE(this.nLockTime);
@ -339,10 +340,10 @@ Transaction.prototype.fromBufferReader = function(reader) {
var sizeTxIns = reader.readVarintNum();
// check for segwit
this._hasWitness = false;
var hasWitness = false;
if (sizeTxIns === 0 && reader.buf[reader.pos] !== 0) {
reader.pos += 1;
this._hasWitness = true;
hasWitness = true;
sizeTxIns = reader.readVarintNum();
}
@ -356,7 +357,7 @@ Transaction.prototype.fromBufferReader = function(reader) {
this.outputs.push(Output.fromBufferReader(reader));
}
if (this._hasWitness) {
if (hasWitness) {
this._fromBufferReaderScriptWitnesses(reader);
}
@ -366,6 +367,7 @@ Transaction.prototype.fromBufferReader = function(reader) {
Transaction.prototype._fromBufferReaderScriptWitnesses = function(reader) {
var itemCount = reader.readVarintNum();
this.witness = [];
for (var i = 0; i < itemCount; i++) {
var size = reader.readVarintNum();
var item = reader.read(size);

7
test/transaction/transaction.js

@ -1235,11 +1235,12 @@ describe('Transaction', function() {
var flag = new Buffer('01', 'hex'); //non zero
var inputCount = new Buffer('00', 'hex');
var outputCount = new Buffer('00', 'hex');
var witness = new Buffer('00', 'hex');
var witness = new Buffer('01', 'hex');
var witnessItems = new Buffer('00', 'hex');
var locktime = new Buffer('00000000', 'hex');
var txBuffer = Buffer.concat([version, marker, flag, inputCount, outputCount, witness, locktime]);
var txBuffer = Buffer.concat([version, marker, flag, inputCount, outputCount, witness, witnessItems, locktime]);
var tx = bitcore.Transaction().fromBuffer(txBuffer);
tx._hasWitness.should.equal(true);
tx.hasWitness().should.equal(true);
});
it('correctly calculate hash for segwit transaction', function() {
var txBuffer = new Buffer('01000000000101b0e5caa7e37d4b8530c3e1071a36dd5e05d1065cf7224ddff42c69e3387689870000000000ffffffff017b911100000000001600144ff831574da8bef07f8bc97244a1666147b071570247304402203fcbcfddbd6ca3a90252610dd63f1be50b2d926b8d87c912da0a3e42bb03fba002202a90c8aad75da22b0549c72618b754114583e934c0b0d2ccd6c13fcd859ba4ed01210363f3f47f4555779de405eab8d0dc8c2a4f3e09f4171a3fa47c7a77715795319800000000', 'hex');

Loading…
Cancel
Save