findProgramAddress static method

Future<Ed25519HDPublicKey> findProgramAddress({
  1. required Iterable<Iterable<int>> seeds,
  2. required Ed25519HDPublicKey programId,
})

Finds a valid program address.

Valid program addresses must fall off the ed25519 curve. This function iterates a nonce until it finds one that when combined with the seeds results in a valid program address.

Implementation

static Future<Ed25519HDPublicKey> findProgramAddress({
  required Iterable<Iterable<int>> seeds,
  required Ed25519HDPublicKey programId,
}) async {
  if (seeds.length > _maxSeeds) {
    throw const FormatException('you can give me up to $_maxSeeds seeds');
  }
  final overflowingSeed = seeds.where((s) => s.length > _maxSeedLength);
  if (overflowingSeed.isNotEmpty) {
    throw const FormatException(
      'one or more of the seeds provided is too big',
    );
  }
  final flatSeeds = seeds.fold(<int>[], _flatten);
  int bumpSeed = _maxBumpSeed;
  while (bumpSeed >= 0) {
    try {
      return await createProgramAddress(
        seeds: [...flatSeeds, bumpSeed],
        programId: programId,
      );
    } on FormatException {
      bumpSeed -= 1;
    }
  }

  throw const FormatException('cannot find program address with these seeds');
}