@node-dlc/bitcoin
Version:
94 lines • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LockTime = void 0;
const bufio_1 = require("@node-dlc/bufio");
const TimeLockMode_1 = require("./TimeLockMode");
const DEFAULT_LOCKTIME = 4294967295;
const TIME_THRESHOLD = 500000000;
/**
* Specifies the absolute lock time for a transaction referred in the
* reference implementation as nLocktime. An absolute locktime is only
* active when its value is less than 0xffff_ffff and at least one
* transaction input has a non-0xffff_ffff nSequence.
*/
class LockTime {
/**
* Parses a locktime from a reader
* @param reader
*/
static parse(reader) {
return new LockTime(reader.readUInt32LE());
}
/**
* Creates an nLockTime of zero which enforces finality for the
* transaction.
*/
static zero() {
return new LockTime(0);
}
/**
* Gets or sets the value of the timelock. The value must be less than
* the maximum allowed value of 0xffff_ffff. When set to the max
* value, the locktime is disabled.
*/
get value() {
return this._value;
}
set value(val) {
if (val > DEFAULT_LOCKTIME || val < 0)
throw new Error('Invalid nLocktime');
this._value = val;
}
/**
* Creates a new locktime instance
* @param value defaults to 0xffff_ffff
*/
constructor(value = DEFAULT_LOCKTIME) {
this.value = value;
}
/**
* True when a non-default is configured. This flag has no
* knowledge if the locktime is fully enabled with the requirement
* that at least one tx input has an nSequence value that non-default.
*/
get isEnabled() {
return this.value < DEFAULT_LOCKTIME;
}
/**
* Gets the type of lock time: Block or Time based
*/
get type() {
if (this.value < TIME_THRESHOLD)
return TimeLockMode_1.TimeLockMode.Block;
else
return TimeLockMode_1.TimeLockMode.Time;
}
/**
* Returns the string value
*/
toString() {
return this.value.toString();
}
/**
* Returns the JSON serialized value
*/
toJSON() {
return this.value;
}
/**
* Serializes the locktime into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter(Buffer.alloc(4));
writer.writeUInt32LE(this.value);
return writer.toBuffer();
}
/**
* Clone via deep copy
*/
clone() {
return new LockTime(this._value);
}
}
exports.LockTime = LockTime;
//# sourceMappingURL=LockTime.js.map