rialo-validator-registry-interface 0.4.2

Instructions and constructors for a registry containing validator identities
Documentation
// Copyright (c) Subzero Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Helper functions to derive the Program Derived Addresses used by the Validator Registry program.

use rialo_s_pubkey::Pubkey;

/// The seed prefix used for deriving validator info PDAs.
pub const VALIDATOR_INFO_SEED: &[u8] = b"validator-info";

/// Seed prefix for self-bond PDAs.
pub const SELF_BOND_SEED: &[u8] = b"self-bond";

/// Derives the self-bond stake account address from a validator's info account.
///
/// The self-bond PDA is owned by the ValidatorRegistry program (uses `crate::ID` as the
/// program ID for derivation), which is why this function lives here rather than in
/// `stake-cache-interface`.
pub fn derive_self_bond_address(validator_info_pubkey: &Pubkey) -> Pubkey {
    let (address, _bump) = Pubkey::find_program_address(
        &[SELF_BOND_SEED, validator_info_pubkey.as_ref()],
        &crate::ID,
    );
    address
}

/// Derives the self-bond address with bump seed (for PDA signing).
pub fn derive_self_bond_address_with_bump(validator_info_pubkey: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[SELF_BOND_SEED, validator_info_pubkey.as_ref()],
        &crate::ID,
    )
}

/// Derives the address for the account where a validator is registered.
pub fn derive_validator_info_address(authority_key: &[u8; 96]) -> Pubkey {
    let (address, _bump) = derive_validator_info_address_with_bump(authority_key);
    address
}

/// Derives the address and bump seed for the account where a validator is registered.
///
/// Returns a tuple of (address, bump_seed) which can be used for PDA signing.
pub fn derive_validator_info_address_with_bump(authority_key: &[u8; 96]) -> (Pubkey, u8) {
    let authority_key_hash = rialo_s_sha256_hasher::hash(authority_key);

    Pubkey::find_program_address(
        &[VALIDATOR_INFO_SEED, authority_key_hash.as_ref()],
        &crate::ID,
    )
}