To be effective within the current Bitcoin network, the inputs to a transaction

with an nLocktime must be not the standard max value.

We set the sequence number of 0 if the value is max.

Currently sequence numbers other than MAX_INT32 have no meaning in the Bitcoin protocol
but this may change in future BIPS
This commit is contained in:
Zaki Manian 2015-08-14 11:26:17 -07:00
parent d7cd12b9d6
commit aa8e548800
3 changed files with 47 additions and 0 deletions

View File

@ -13,6 +13,7 @@ var Output = require('../output');
var DEFAULT_SEQNUMBER = 0xFFFFFFFF;
var DEFAULT_LOCKTIME_SEQNUMBER = 0x00000000;
function Input(params) {
if (!(this instanceof Input)) {
@ -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,

View File

@ -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;
};
@ -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;
};

View File

@ -748,6 +748,36 @@ 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);
expect(transaction.inputs[0].sequenceNumber
.should.not.equal(Transaction.Input.DEFAULT_SEQNUMBER));
});
it('has a non-max sequenceNumber for effective blockheight locktime tx', function() {
var transaction = new Transaction()
.from(simpleUtxoWith1BTC)
.lockUntilBlockHeight(blockHeight);
expect(transaction.inputs[0].sequenceNumber
.should.not.equal(Transaction.Input.DEFAULT_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);
expect(serialized_tx.should.equal(copy.uncheckedSerialize()));
});
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);
expect(serialized_tx.should.equal(copy.uncheckedSerialize()));
});
});
it('handles anyone-can-spend utxo', function() {