findProgramAddress static method
Finds a valid program address for the given seeds
and programId
.
Valid program addresses must fall off the ed25519 curve.
This function iterates a nonce
until it finds one that can be combined with the seeds to produce a valid program address.
Throws an AssertionError if seeds
contains an invalid seed.
Throws a PubkeyException if a valid program address could not be found.
Implementation
static ProgramAddress findProgramAddress(
final List<List<int>> seeds,
final Pubkey programId,
) {
for (int nonce = 255; nonce > 0; --nonce) {
try {
final List<List<int>> seedsWithNonce = seeds +
[
[nonce]
];
final Pubkey address = createProgramAddress(seedsWithNonce, programId);
return ProgramAddress(address, nonce);
} on AssertionError {
rethrow;
} catch (_) {}
}
throw const PubkeyException(
'Unable to find a viable program address nonce.');
}