deriveSendAddress static method

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

Derive a send address at index (Alice -> Bob).

myKey is the sender's notification private key (a_0). Bob's child pubkey B_n is derived from theirPaymentCode at index, then tweaked with SHA256(ECDH(a_0, B_n)).

Implementation

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

  final tweaked = theirChildPubKey.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);
}