# All files TxIn.ts

20.83% Statements 5/24
0% Branches 0/3
0% Functions 0/8
20.83% Lines 5/24

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
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    1161x

    1x
    1x
    1x

    1x

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

    import { ICloneable } from './ICloneable';
    import { OutPoint } from './OutPoint';
    import { Script } from './Script';
    import { Sequence } from './Sequence';
    import { Witness } from './Witness';

    export class TxIn implements ICloneable<TxIn> {
      /**
       * Parses a TxIn from a a stream reader.
       * @param stream
       */
      public static parse(reader: StreamReader): TxIn {
        const outpoint = OutPoint.parse(reader);
        const scriptSig = Script.parse(reader);
        const sequence = new Sequence(reader.readUInt32LE());
        return new TxIn(outpoint, scriptSig, sequence);
      }

      /**
       * Parses a hex string serialization of a transaction input. This
       * is a helper function instead of having to do `StreamReader.fromHex`
       * on a string directly.
       * @param hex
       */
      public static fromHex(hex: string): TxIn {
        const reader = StreamReader.fromHex(hex);
        return this.parse(reader);
      }

      /**
       * The previous transaction output tuple
       */
      public outpoint: OutPoint;

      /**
       * ScriptSig for the input
       */
      public scriptSig: Script;

      /**
       * nSequence value for the transaction. Defaults to 0xffffffff which
       * disables the absolute timelock.
       */
      public sequence: Sequence;

      /**
       * Witness data that is required by the input
       */
      public witness: Witness[];

      /**
       * Constructs a new transaction input from the values
       * @param outpoint
       * @param scriptSig
       * @param sequence
       * @param witness
       */
      constructor(
        outpoint: OutPoint,
        scriptSig: Script = new Script(),
        sequence: Sequence = new Sequence(),
        witness: Witness[] = [],
      ) {
        this.outpoint = outpoint;
        this.scriptSig = scriptSig;
        this.sequence = sequence;
        this.witness = witness;
      }

      /**
       * Creates a string of the transaction input that includes all of the
       * properties.
       */
      public toString(): string {
        return `prev=${this.outpoint.toString()}, scriptSig=${this.scriptSig.toString()}, sequence=${this.sequence.toString()}`; // prettier-ignore
      }

      /**
       * Creates a JSON object of the transaction input that includes all
       * of the properties.
       */
      // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
      public toJSON() {
        return {
          outpoint: this.outpoint.toJSON(),
          scriptSig: this.scriptSig.toJSON(),
          sequence: this.sequence.toJSON(),
        };
      }

      /**
       * Returns the byte serialization of the transaction input
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeBytes(this.outpoint.serialize());
        writer.writeBytes(this.scriptSig.serialize());
        writer.writeBytes(this.sequence.serialize());
        return writer.toBuffer();
      }

      /**
       * Clone via deep copy
       */
      public clone(): TxIn {
        return new TxIn(
          this.outpoint.clone(),
          this.scriptSig.clone(),
          this.sequence.clone(),
          this.witness.map((p) => p.clone()),
        );
      }
    }