Skip to content

Commit

Permalink
Merge pull request #1324 from zmanian/SetLockTimeSeqNumber
Browse files Browse the repository at this point in the history
Fix SequenceNumber for nLockTime transactions
  • Loading branch information
Braydon Fuller committed Aug 28, 2015
2 parents dc93b6c + c9980dc commit 721f54f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/transaction/input/input.js
Expand Up @@ -13,6 +13,7 @@ var Output = require('../output');


var DEFAULT_SEQNUMBER = 0xFFFFFFFF;
var DEFAULT_LOCKTIME_SEQNUMBER = 0x00000000;

function Input(params) {
if (!(this instanceof Input)) {
Expand All @@ -24,6 +25,7 @@ function Input(params) {
}

Input.DEFAULT_SEQNUMBER = DEFAULT_SEQNUMBER;
Input.DEFAULT_LOCKTIME_SEQNUMBER = DEFAULT_LOCKTIME_SEQNUMBER;

Object.defineProperty(Input.prototype, 'script', {
configurable: false,
Expand Down
15 changes: 15 additions & 0 deletions lib/transaction/transaction.js
Expand Up @@ -414,6 +414,13 @@ Transaction.prototype.lockUntilDate = function(time) {
if (_.isDate(time)) {
time = time.getTime() / 1000;
}

for (var i = 0; i < this.inputs.length; i++) {
if (this.inputs[i].sequenceNumber === Input.DEFAULT_SEQNUMBER){
this.inputs[i].sequenceNumber = Input.DEFAULT_LOCKTIME_SEQNUMBER;
}
}

this.nLockTime = time;
return this;
};
Expand All @@ -433,6 +440,14 @@ Transaction.prototype.lockUntilBlockHeight = function(height) {
if (height < 0) {
throw new errors.Transaction.NLockTimeOutOfRange();
}

for (var i = 0; i < this.inputs.length; i++) {
if (this.inputs[i].sequenceNumber === Input.DEFAULT_SEQNUMBER){
this.inputs[i].sequenceNumber = Input.DEFAULT_LOCKTIME_SEQNUMBER;
}
}


this.nLockTime = height;
return this;
};
Expand Down
34 changes: 34 additions & 0 deletions test/transaction/transaction.js
Expand Up @@ -748,6 +748,40 @@ describe('Transaction', function() {
return new Transaction().lockUntilBlockHeight(-1);
}).to.throw(errors.Transaction.NLockTimeOutOfRange);
});
it('has a non-max sequenceNumber for effective date locktime tx', function() {
var transaction = new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilDate(date);
transaction.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER);
});
it('has a non-max sequenceNumber for effective blockheight locktime tx', function() {
var transaction = new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilBlockHeight(blockHeight);
transaction.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER);
});
it('should serialize correctly for date locktime ', function() {
var transaction= new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilDate(date);
var serialized_tx = transaction.uncheckedSerialize();
var copy = new Transaction(serialized_tx);
serialized_tx.should.equal(copy.uncheckedSerialize());
copy.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER)
});
it('should serialize correctly for a block height locktime', function() {
var transaction= new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilBlockHeight(blockHeight);
var serialized_tx = transaction.uncheckedSerialize();
var copy = new Transaction(serialized_tx);
serialized_tx.should.equal(copy.uncheckedSerialize());
copy.inputs[0].sequenceNumber
.should.equal(Transaction.Input.DEFAULT_LOCKTIME_SEQNUMBER)
});
});

it('handles anyone-can-spend utxo', function() {
Expand Down

0 comments on commit 721f54f

Please sign in to comment.