Skip to main content

rialo_validator_registry_interface/
pda.rs

1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Helper functions to derive the Program Derived Addresses used by the Validator Registry program.
5
6use rialo_s_pubkey::Pubkey;
7
8/// The seed prefix used for deriving validator info PDAs.
9pub const VALIDATOR_INFO_SEED: &[u8] = b"validator-info";
10
11/// Seed prefix for self-bond PDAs.
12pub const SELF_BOND_SEED: &[u8] = b"self-bond";
13
14/// Derives the self-bond stake account address from a validator's info account.
15///
16/// The self-bond PDA is owned by the ValidatorRegistry program (uses `crate::ID` as the
17/// program ID for derivation), which is why this function lives here rather than in
18/// `stake-cache-interface`.
19pub fn derive_self_bond_address(validator_info_pubkey: &Pubkey) -> Pubkey {
20    let (address, _bump) = Pubkey::find_program_address(
21        &[SELF_BOND_SEED, validator_info_pubkey.as_ref()],
22        &crate::ID,
23    );
24    address
25}
26
27/// Derives the self-bond address with bump seed (for PDA signing).
28pub fn derive_self_bond_address_with_bump(validator_info_pubkey: &Pubkey) -> (Pubkey, u8) {
29    Pubkey::find_program_address(
30        &[SELF_BOND_SEED, validator_info_pubkey.as_ref()],
31        &crate::ID,
32    )
33}
34
35/// Derives the address for the account where a validator is registered.
36pub fn derive_validator_info_address(authority_key: &[u8; 96]) -> Pubkey {
37    let (address, _bump) = derive_validator_info_address_with_bump(authority_key);
38    address
39}
40
41/// Derives the address and bump seed for the account where a validator is registered.
42///
43/// Returns a tuple of (address, bump_seed) which can be used for PDA signing.
44pub fn derive_validator_info_address_with_bump(authority_key: &[u8; 96]) -> (Pubkey, u8) {
45    let authority_key_hash = rialo_s_sha256_hasher::hash(authority_key);
46
47    Pubkey::find_program_address(
48        &[VALIDATOR_INFO_SEED, authority_key_hash.as_ref()],
49        &crate::ID,
50    )
51}