# All files LexicographicalSorters.ts

20% Statements 3/15
0% Branches 0/8
0% Functions 0/2
27.27% Lines 3/11

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

    1x

    1x

     import { HashByteOrder } from './HashByteOrder';
    import { TxIn } from './TxIn';
    import { TxOut } from './TxOut';

    /**
     * Compares two transaction inputs and lexicographically sorted in
     * ascending order using the reversed byte order (RPC) of the txids.
     * If the same txids are in both TxIn, then the output index is sorted
     * in ascending order.
     * @param a
     * @param b
     */
    export function bip69InputSorter(a: TxIn, b: TxIn): number {
      const txid = a.outpoint.txid
        .serialize(HashByteOrder.RPC)
        .compare(b.outpoint.txid.serialize());
      if (txid !== 0) return txid;

      if (a.outpoint.outputIndex < b.outpoint.outputIndex) return -1;
      if (a.outpoint.outputIndex > b.outpoint.outputIndex) return 1;
      return 0;
    }

    /**
     * Sort transaction amounts in ascending order. Then ScriptPubKey values
     * will be sorted in ascending byte order next.
     * @param a
     * @param b
     */
    export function bip69OutputSorter(a: TxOut, b: TxOut): number {
      // sort by amount desc first
      const amount = Number(a.value.sats - b.value.sats);
      if (amount !== 0) return amount;

      // then sort by script pub key
      return a.scriptPubKey.serializeCmds().compare(b.scriptPubKey.serializeCmds());
    }