All files / lib/lightning ChannelId.ts

15.38% Statements 2/13
0% Branches 0/2
0% Functions 0/6
15.38% Lines 2/13

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 551x                 1x                                                                                          
import { HashByteOrder, OutPoint } from '@node-dlc/bitcoin';
 
/**
 * ChannelId type that that encapsulates an outpoint as a 32-byte value
 * and is used to identify a channel. This type is defined in BOLT 2
 * under the peer protocol. It is defined as combinging the funding_txid
 * and the funding_output_index, using big-endian XOR (meaning the
 * output_index modifies the last two bytes).
 */
export class ChannelId {
  public static fromOutPoint(outpoint: OutPoint): ChannelId {
    const txid = outpoint.txid.serialize(HashByteOrder.RPC);
 
    if (outpoint.outputIndex > 0xffff) {
      throw new Error('Invalid output index length');
    }
 
    txid[30] ^= outpoint.outputIndex >> 8;
    txid[31] ^= outpoint.outputIndex & 0xff;
 
    return new ChannelId(txid);
  }
 
  constructor(readonly value: Buffer) {}
 
  /**
   * Converts the ChannelId into a buffer
   */
  public toBuffer(): Buffer {
    return Buffer.from(this.value);
  }
 
  /**
   * Converts the ChannelId into a hex-encoded string
   */
  public toString(): string {
    return this.toHex();
  }
 
  /**
   * Converts the ChannelId to a hex-encoded string
   */
  public toHex(): string {
    return this.value.toString('hex');
  }
 
  /**
   * Returns true if the ChannelIds are byte-wise equal
   * @param other
   */
  public equals(other: ChannelId): boolean {
    return this.value.equals(other.value);
  }
}