deriveReceiveAddress static method

String deriveReceiveAddress({
  1. required SecretKey myKey,
  2. required PaymentCode theirPaymentCode,
  3. required int index,
  4. required Chain chain,
  5. bool segwit = false,
})

Derive a receive address at index (Bob <- Alice).

myKey is the receiver's private key at index (b_n). The sender's notification pubkey A_0 is derived from theirPaymentCode, then ECDH(b_n, A_0) == ECDH(a_0, B_n).

Implementation

static String deriveReceiveAddress({
  required SecretKey myKey,
  required PaymentCode theirPaymentCode,
  required int index,
  required Chain chain,
  bool segwit = false,
}) {
  final theirNotificationPubKey = theirPaymentCode.derivePublicKey(0);
  final S = SecretPoint(myKey, theirNotificationPubKey);
  final s = sha256(S.ecdhSecret);

  final tweaked = myKey.publicKey.tweak(s);
  if (tweaked == null) {
    throw StateError('Invalid tweaked key at index $index');
  }

  final pkHash = hash160(tweaked.bytes);
  if (segwit) {
    return P2wpkhAddr(pkHash).encode(chain);
  }
  return P2pkhAddr(pkHash).encode(chain);
}