mirror of
https://github.com/logos-blockchain/lez-programs.git
synced 2026-08-25 14:11:09 +00:00
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:
@@ -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";
|
||||
|
||||
/// Stablecoin Program Instruction.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@@ -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());
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user