Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions programs/amm/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use nssa_core::{
use serde::{Deserialize, Serialize};
use spel_framework_macros::account_type;

// These stable seed bytes are part of the PDA derivation scheme and must stay unchanged for
// compatibility.
const LIQUIDITY_TOKEN_PDA_SEED: [u8; 32] = [0; 32];
const LP_LOCK_HOLDING_PDA_SEED: [u8; 32] = [1; 32];
// These stable domain-separation tags are part of the PDA derivation scheme and must stay
// unchanged for address compatibility.
const LIQUIDITY_TOKEN_PDA_SEED: &[u8] = b"LIQUIDITY_TOKEN";
const LP_LOCK_HOLDING_PDA_SEED: &[u8] = b"LP_LOCK_HOLDING";
Comment on lines +11 to +14

/// AMM Program Instruction.
#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -320,10 +320,9 @@ pub fn compute_liquidity_token_pda(amm_program_id: ProgramId, pool_id: AccountId
pub fn compute_liquidity_token_pda_seed(pool_id: AccountId) -> PdaSeed {
use risc0_zkvm::sha::{Impl, Sha256};

let mut bytes = [0; 64];
let (pool_bytes, seed_bytes) = bytes.split_at_mut(32);
pool_bytes.copy_from_slice(&pool_id.to_bytes());
seed_bytes.copy_from_slice(&LIQUIDITY_TOKEN_PDA_SEED);
let mut bytes = Vec::new();
bytes.extend_from_slice(&pool_id.to_bytes());
bytes.extend_from_slice(LIQUIDITY_TOKEN_PDA_SEED);

PdaSeed::new(
Impl::hash_bytes(&bytes)
Expand All @@ -340,10 +339,9 @@ pub fn compute_lp_lock_holding_pda(amm_program_id: ProgramId, pool_id: AccountId
pub fn compute_lp_lock_holding_pda_seed(pool_id: AccountId) -> PdaSeed {
use risc0_zkvm::sha::{Impl, Sha256};

let mut bytes = [0; 64];
let (pool_bytes, seed_bytes) = bytes.split_at_mut(32);
pool_bytes.copy_from_slice(&pool_id.to_bytes());
seed_bytes.copy_from_slice(&LP_LOCK_HOLDING_PDA_SEED);
let mut bytes = Vec::new();
bytes.extend_from_slice(&pool_id.to_bytes());
bytes.extend_from_slice(LP_LOCK_HOLDING_PDA_SEED);

PdaSeed::new(
Impl::hash_bytes(&bytes)
Expand Down
20 changes: 11 additions & 9 deletions programs/stablecoin/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use nssa_core::{
use serde::{Deserialize, Serialize};
use spel_framework_macros::account_type;

const POSITION_PDA_DOMAIN: [u8; 32] = [0; 32];
const POSITION_VAULT_PDA_DOMAIN: [u8; 32] = [1; 32];
// Stable domain-separation tags for the position PDAs; these must stay unchanged for address
// compatibility.
const POSITION_PDA_DOMAIN: &[u8] = b"POSITION";
const POSITION_VAULT_PDA_DOMAIN: &[u8] = b"POSITION_VAULT";
Comment on lines +11 to +14

/// Stablecoin Program Instruction.
#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -122,10 +124,10 @@ pub fn compute_position_pda_seed(
) -> PdaSeed {
use risc0_zkvm::sha::{Impl, Sha256 as _};

let mut bytes = [0u8; 96];
bytes[0..32].copy_from_slice(&owner_id.to_bytes());
bytes[32..64].copy_from_slice(&collateral_definition_id.to_bytes());
bytes[64..96].copy_from_slice(&POSITION_PDA_DOMAIN);
let mut bytes = Vec::new();
bytes.extend_from_slice(&owner_id.to_bytes());
bytes.extend_from_slice(&collateral_definition_id.to_bytes());
bytes.extend_from_slice(POSITION_PDA_DOMAIN);

let mut out = [0u8; 32];
out.copy_from_slice(Impl::hash_bytes(&bytes).as_bytes());
Expand All @@ -151,9 +153,9 @@ pub fn compute_position_pda(
pub fn compute_position_vault_pda_seed(position_id: AccountId) -> PdaSeed {
use risc0_zkvm::sha::{Impl, Sha256 as _};

let mut bytes = [0u8; 64];
bytes[0..32].copy_from_slice(&position_id.to_bytes());
bytes[32..64].copy_from_slice(&POSITION_VAULT_PDA_DOMAIN);
let mut bytes = Vec::new();
bytes.extend_from_slice(&position_id.to_bytes());
bytes.extend_from_slice(POSITION_VAULT_PDA_DOMAIN);

let mut out = [0u8; 32];
out.copy_from_slice(Impl::hash_bytes(&bytes).as_bytes());
Expand Down
34 changes: 17 additions & 17 deletions programs/twap_oracle/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -232,18 +232,18 @@ 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,
window_duration: u64,
) -> 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)
Expand All @@ -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.
///
Expand All @@ -274,18 +274,18 @@ 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,
window_duration: u64,
) -> 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)
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down
Loading