All files / lib/lightning ChannelKeys.ts

10% Statements 2/20
100% Branches 0/0
0% Functions 0/4
10% Lines 2/20

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 1101x   1x                                                                                                                                                                                                                      
import * as crypto from '@node-dlc/crypto';
 
export class ChannelKeys {
  /**
   * Derives a public key that can be used for local or remote
   * pubkey, htlc_pubkey, or delayed_pubkey
   *
   * Derived via:
   *  pubkey = basepoint + SHA256(per_commitment_point || basepoint) * G
   *
   * @param perCommitmentPoint 33-byte buffer
   * @param basePoint 33-byte base point
   * @returns 33-byte buffer
   */
  public static derivePubKey(
    perCommitmentPoint: Buffer,
    basePoint: Buffer,
  ): Buffer {
    const tweak = crypto.sha256(Buffer.concat([perCommitmentPoint, basePoint]));
    return crypto.publicKeyTweakAdd(basePoint, tweak, true);
  }
 
  /**
   * Derives the private keys based on local basepoint secrets and can
   * be used to derive per-commitment
   * - payment_scret
   * - delayedpayment_secret
   * - htlc_secret
   *
   * Derived via:
   *  privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)
   *
   * @param perCommitmentPoint 32-byte buffer
   * @param basePointSecret 32-byte buffer
   * @return 32-byte buffer
   */
  public static derivePrivKey(
    perCommitmentPoint: Buffer,
    basePointSecret: Buffer,
  ): Buffer {
    const basePoint = crypto.getPublicKey(basePointSecret, true);
    const tweak = crypto.sha256(Buffer.concat([perCommitmentPoint, basePoint]));
    return crypto.privateKeyTweakAdd(basePointSecret, tweak);
  }
 
  /**
   * Derives the revocationpubkey blinded key for either local or
   * remote revocation pubkey generation.
   *
   * Derived via:
   *  revocationpubkey = revocation_basepoint * SHA256(revocation_basepoint || per_commitment_point) +
   *                     per_commitment_point * SHA256(per_commitment_point || revocation_basepoint)
   *
   * @param perCommitmentPoint 33-byte buffer
   * @param basePoint 33-byte buffer
   * @returns 33-byte buffer
   */
  public static deriveRevocationPubKey(
    perCommitmentPoint: Buffer,
    basePoint: Buffer,
  ): Buffer {
    const tweakA = crypto.sha256(
      Buffer.concat([basePoint, perCommitmentPoint]),
    );
    const a = crypto.publicKeyTweakMul(basePoint, tweakA, true);
 
    const tweakB = crypto.sha256(
      Buffer.concat([perCommitmentPoint, basePoint]),
    );
    const b = crypto.publicKeyTweakMul(perCommitmentPoint, tweakB, true);
 
    return crypto.publicKeyCombine([a, b], true);
  }
 
  /**
   * Derives the revocationprivkey which is used by the local node
   * and with a per_commitment_secret that was revealed by the
   * counterparty.
   *
   * Derived via:
   * revocationprivkey = revocation_basepoint_secret * SHA256(revocation_basepoint || per_commitment_point) +
   *                     per_commitment_secret * SHA256(per_commitment_point || revocation_basepoint)
   * @param perCommitmentPointSecret 32-byte buffer
   * @param basePointSecret 32-byte buffer
   */
  public static deriveRevocationPrivKey(
    perCommitmentPointSecret: Buffer,
    basePointSecret: Buffer,
  ): Buffer {
    const basePoint = crypto.getPublicKey(basePointSecret, true);
    const perCommitmentPoint = crypto.getPublicKey(
      perCommitmentPointSecret,
      true,
    );
 
    const tweakA = crypto.sha256(
      Buffer.concat([basePoint, perCommitmentPoint]),
    );
    const a = crypto.privateKeyTweakMul(basePointSecret, tweakA);
 
    const tweakB = crypto.sha256(
      Buffer.concat([perCommitmentPoint, basePoint]),
    );
    const b = crypto.privateKeyTweakMul(perCommitmentPointSecret, tweakB);
 
    const result = crypto.privateKeyTweakAdd(a, b);
    return result;
  }
}