All files LexicographicalSorters.ts

66.67% Statements 10/15
37.5% Branches 3/8
100% Functions 2/2
72.73% Lines 8/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 361x                       1x 3x 3x                         1x   2x 2x     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());
    Eif (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());
}