refactor(pda): use descriptive string seeds for PDA derivation

Replace the hardcoded numeric byte-stream seeds ([0; 32], [1; 32], ...)
used for domain separation in PDA derivation with descriptive byte-string
constants, mirroring the AMM config account's existing b"CONFIG" seed.

  amm:         [0; 32] -> b"LIQUIDITY_TOKEN"
               [1; 32] -> b"LP_LOCK_HOLDING"
  stablecoin:  [0; 32] -> b"POSITION"
               [1; 32] -> b"POSITION_VAULT"
  twap_oracle: [2; 32] -> b"PRICE_OBSERVATIONS"
               [3; 32] -> b"ORACLE_PRICE_ACCOUNT"
               [4; 32] -> b"CURRENT_TICK_ACCOUNT"

Since the seeds are now variable-length, each compute_*_pda_seed function
builds its hash input with a Vec and extend_from_slice instead of a
fixed-size buffer with offset writes.

Closes #146
This commit is contained in:
r4bbit
2026-06-22 14:12:13 +02:00
parent 0e2c5f9329
commit 03345db803
3 changed files with 38 additions and 38 deletions
+17 -17
View File
@@ -211,7 +211,7 @@ impl From<&PriceObservations> for Data {
// PDA helpers
// ──────────────────────────────────────────────────────────────────────────────
const PRICE_OBSERVATIONS_PDA_SEED: [u8; 32] = [2; 32];
const PRICE_OBSERVATIONS_PDA_SEED: &[u8] = b"PRICE_OBSERVATIONS";
/// Derives the [`AccountId`] for a price source's [`PriceObservations`] PDA.
///
@@ -232,7 +232,7 @@ pub fn compute_price_observations_pda(
/// Derives the [`PdaSeed`] for a price source's [`PriceObservations`].
///
/// Hash input: `price_source_id (32 bytes) || window_duration_le (8 bytes) ||
/// PRICE_OBSERVATIONS_PDA_SEED (32 bytes)`.
/// PRICE_OBSERVATIONS_PDA_SEED`.
#[must_use]
pub fn compute_price_observations_pda_seed(
price_source_id: AccountId,
@@ -240,10 +240,10 @@ pub fn compute_price_observations_pda_seed(
) -> PdaSeed {
use risc0_zkvm::sha::{Impl, Sha256};
let mut bytes = [0u8; 72];
bytes[..32].copy_from_slice(&price_source_id.to_bytes());
bytes[32..40].copy_from_slice(&window_duration.to_le_bytes());
bytes[40..72].copy_from_slice(&PRICE_OBSERVATIONS_PDA_SEED);
let mut bytes = Vec::new();
bytes.extend_from_slice(&price_source_id.to_bytes());
bytes.extend_from_slice(&window_duration.to_le_bytes());
bytes.extend_from_slice(PRICE_OBSERVATIONS_PDA_SEED);
PdaSeed::new(
Impl::hash_bytes(&bytes)
@@ -253,7 +253,7 @@ pub fn compute_price_observations_pda_seed(
)
}
const ORACLE_PRICE_ACCOUNT_PDA_SEED: [u8; 32] = [3; 32];
const ORACLE_PRICE_ACCOUNT_PDA_SEED: &[u8] = b"ORACLE_PRICE_ACCOUNT";
/// Derives the [`AccountId`] for a price source's [`OraclePriceAccount`] PDA.
///
@@ -274,7 +274,7 @@ pub fn compute_oracle_price_account_pda(
/// Derives the [`PdaSeed`] for a price source's [`OraclePriceAccount`].
///
/// Hash input: `price_source_id (32 bytes) || window_duration_le (8 bytes) ||
/// ORACLE_PRICE_ACCOUNT_PDA_SEED (32 bytes)`.
/// ORACLE_PRICE_ACCOUNT_PDA_SEED`.
#[must_use]
pub fn compute_oracle_price_account_pda_seed(
price_source_id: AccountId,
@@ -282,10 +282,10 @@ pub fn compute_oracle_price_account_pda_seed(
) -> PdaSeed {
use risc0_zkvm::sha::{Impl, Sha256};
let mut bytes = [0u8; 72];
bytes[..32].copy_from_slice(&price_source_id.to_bytes());
bytes[32..40].copy_from_slice(&window_duration.to_le_bytes());
bytes[40..72].copy_from_slice(&ORACLE_PRICE_ACCOUNT_PDA_SEED);
let mut bytes = Vec::new();
bytes.extend_from_slice(&price_source_id.to_bytes());
bytes.extend_from_slice(&window_duration.to_le_bytes());
bytes.extend_from_slice(ORACLE_PRICE_ACCOUNT_PDA_SEED);
PdaSeed::new(
Impl::hash_bytes(&bytes)
@@ -459,7 +459,7 @@ impl From<&CurrentTickAccount> for Data {
}
}
const CURRENT_TICK_ACCOUNT_PDA_SEED: [u8; 32] = [4; 32];
const CURRENT_TICK_ACCOUNT_PDA_SEED: &[u8] = b"CURRENT_TICK_ACCOUNT";
/// Derives the [`AccountId`] for a price source's [`CurrentTickAccount`] PDA.
#[must_use]
@@ -475,14 +475,14 @@ pub fn compute_current_tick_account_pda(
/// Derives the [`PdaSeed`] for a price source's [`CurrentTickAccount`].
///
/// Hash input: `price_source_id (32 bytes) || CURRENT_TICK_ACCOUNT_PDA_SEED (32 bytes)`.
/// Hash input: `price_source_id (32 bytes) || CURRENT_TICK_ACCOUNT_PDA_SEED`.
#[must_use]
pub fn compute_current_tick_account_pda_seed(price_source_id: AccountId) -> PdaSeed {
use risc0_zkvm::sha::{Impl, Sha256};
let mut bytes = [0u8; 64];
bytes[..32].copy_from_slice(&price_source_id.to_bytes());
bytes[32..64].copy_from_slice(&CURRENT_TICK_ACCOUNT_PDA_SEED);
let mut bytes = Vec::new();
bytes.extend_from_slice(&price_source_id.to_bytes());
bytes.extend_from_slice(CURRENT_TICK_ACCOUNT_PDA_SEED);
PdaSeed::new(
Impl::hash_bytes(&bytes)