# All files TxOut.ts

22.22% Statements 4/18
100% Branches 0/0
0% Functions 0/7
22.22% Lines 4/18

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
    881x

    1x
    1x

    1x

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

    import { ICloneable } from './ICloneable';
    import { Script } from './Script';
    import { Value } from './Value';

    export class TxOut implements ICloneable<TxOut> {
      /**
       * Parses a TxOut from a stream and returns a new instance of TxOut
       * @param reader
       */
      public static parse(reader: StreamReader): TxOut {
        const value = Value.fromSats(reader.readBigUInt64LE());
        const scriptPubKey = Script.parse(reader);
        return new TxOut(value, scriptPubKey);
      }

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

      /**
       * Value (often in satoshi or bitcoin) that will be locked into the
       * output using the provided scriptPubKey. The combined outputs must
       * be lte the combined input value for in transaction.
       */
      public value: Value;

      /**
       * The locking script used to encumbered the value in the output.
       * To claim these funds as an input in another transaction requires
       * providing a ScriptSig that successfully evaluates when combined
       * with the ScriptPubKey.
       */
      public scriptPubKey: Script;

      /**
       * Constructs a new TxOut from the supplied arguments
       * @param value
       * @param scriptPubKey
       */
      constructor(value: Value, scriptPubKey: Script) {
        this.value = value;
        this.scriptPubKey = scriptPubKey;
      }

      /**
       * Returns the TxOut as a string
       */
      public toString(): string {
        return `value="${this.value.sats}", scriptPubKey="${this.scriptPubKey}"`;
      }

      /**
       * Returns the TxOut as a JSON object
       */
      public toJSON(): { value: string; scriptPubKey: string } {
        return {
          value: this.value.sats.toString(),
          scriptPubKey: this.scriptPubKey.toJSON(),
        };
      }

      /**
       * Returns the serialization of the transaction output into a buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeUInt64LE(this.value.sats);
        writer.writeBytes(this.scriptPubKey.serialize());
        return writer.toBuffer();
      }

      /**
       * Clone via deep copy
       */
      public clone(): TxOut {
        return new TxOut(this.value.clone(), this.scriptPubKey.clone());
      }
    }