# All files LockTime.ts

22.73% Statements 5/22
0% Branches 0/7
0% Functions 0/11
25% Lines 5/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    1031x

    1x

    1x
    1x

    1x

     import { BufferWriter, StreamReader } from '@node-dlc/bufio';

    import { ICloneable } from './ICloneable';
    import { TimeLockMode } from './TimeLockMode';

    const DEFAULT_LOCKTIME = 0xffff_ffff;
    const TIME_THRESHOLD = 500_000_000;

    /**
     * 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.
     */
    export class LockTime implements ICloneable<LockTime> {
      /**
       * Parses a locktime from a reader
       * @param reader
       */
      public static parse(reader: StreamReader): LockTime {
        return new LockTime(reader.readUInt32LE());
      }

      /**
       * Creates an nLockTime of zero which enforces finality for the
       * transaction.
       */
      public static zero(): LockTime {
        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.
       */
      public get value(): number {
        return this._value;
      }

      public set value(val: number) {
        if (val > DEFAULT_LOCKTIME || val < 0) throw new Error('Invalid nLocktime');
        this._value = val;
      }

      private _value: number;

      /**
       * Creates a new locktime instance
       * @param value defaults to 0xffff_ffff
       */
      constructor(value: number = 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.
       */
      public get isEnabled(): boolean {
        return this.value < DEFAULT_LOCKTIME;
      }

      /**
       * Gets the type of lock time: Block or Time based
       */
      public get type(): TimeLockMode {
        if (this.value < TIME_THRESHOLD) return TimeLockMode.Block;
        else return TimeLockMode.Time;
      }

      /**
       * Returns the string value
       */
      public toString(): string {
        return this.value.toString();
      }

      /**
       * Returns the JSON serialized value
       */
      public toJSON(): number {
        return this.value;
      }

      /**
       * Serializes the locktime into a Buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter(Buffer.alloc(4));
        writer.writeUInt32LE(this.value);
        return writer.toBuffer();
      }

      /**
       * Clone via deep copy
       */
      public clone(): LockTime {
        return new LockTime(this._value);
      }
    }