From e61306e498d59c7420f2e4a3b292728726504c35 Mon Sep 17 00:00:00 2001 From: Pravdyvy Date: Wed, 8 Jul 2026 14:33:32 +0300 Subject: [PATCH] fix(wallet_ffi): suggestions 2 --- lez/wallet-ffi/src/pda.rs | 45 ++++++++++++++++++++++++++++++++++--- lez/wallet-ffi/src/types.rs | 8 +++++++ lez/wallet-ffi/wallet_ffi.h | 7 +++--- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lez/wallet-ffi/src/pda.rs b/lez/wallet-ffi/src/pda.rs index 7144fe71..0c72c128 100644 --- a/lez/wallet-ffi/src/pda.rs +++ b/lez/wallet-ffi/src/pda.rs @@ -5,7 +5,7 @@ use crate::{FfiBytes32, FfiNullifierPublicKey, FfiPdaSeed, FfiProgramId, FfiU128 /// Produce account id for public PDA. /// /// # Parameters -/// - `program_id`: Id of a owner program +/// - `program_id`: Id of the owner program /// - `pda_seed`: 32 byte seed /// /// # Returns @@ -21,9 +21,10 @@ pub extern "C" fn wallet_ffi_account_id_for_public_pda( /// Produce account id for private PDA. /// /// # Parameters -/// - `program_id`: Id of a owner program +/// - `program_id`: Id of the owner program /// - `pda_seed`: 32 byte seed -/// - `npk`: 32 byte nullifier public key(can be get from `wallet_ffi_get_private_account_keys`) +/// - `npk`: 32 byte nullifier public key (can be obtained from +/// `wallet_ffi_get_private_account_keys`) /// - `identifier`: little endian encoded `u128` /// /// # Returns @@ -43,3 +44,41 @@ pub extern "C" fn wallet_ffi_account_id_for_private_pda( ) .into() } + +#[cfg(test)] +mod tests { + use lee::AccountId; + use lee_core::NullifierPublicKey; + use vault_core::PdaSeed; + + use crate::pda::{wallet_ffi_account_id_for_private_pda, wallet_ffi_account_id_for_public_pda}; + + #[test] + fn public_pda_consistent_derivation() { + let program_id = [100_u32, 101, 102, 103, 104, 105, 106, 107]; + let pda_seed = PdaSeed::new([42; 32]); + + let pda_id = AccountId::for_public_pda(&program_id, &pda_seed); + let ffi_pda_id = wallet_ffi_account_id_for_public_pda(program_id.into(), pda_seed.into()); + + assert_eq!(pda_id.into_value(), ffi_pda_id.data); + } + + #[test] + fn private_pda_consistent_derivation() { + let program_id = [100_u32, 101, 102, 103, 104, 105, 106, 107]; + let pda_seed = PdaSeed::new([42; 32]); + let npk = NullifierPublicKey([43; 32]); + let identifier = 100_000_u128; + + let pda_id = AccountId::for_private_pda(&program_id, &pda_seed, &npk, identifier); + let ffi_pda_id = wallet_ffi_account_id_for_private_pda( + program_id.into(), + pda_seed.into(), + npk.into(), + identifier.into(), + ); + + assert_eq!(pda_id.into_value(), ffi_pda_id.data); + } +} diff --git a/lez/wallet-ffi/src/types.rs b/lez/wallet-ffi/src/types.rs index 6ada99f7..39e1a5c9 100644 --- a/lez/wallet-ffi/src/types.rs +++ b/lez/wallet-ffi/src/types.rs @@ -37,6 +37,14 @@ impl From for PdaSeed { } } +impl From for FfiPdaSeed { + fn from(value: PdaSeed) -> Self { + Self { + data: *value.as_bytes(), + } + } +} + pub type FfiNullifierPublicKey = FfiBytes32; impl From for NullifierPublicKey { diff --git a/lez/wallet-ffi/wallet_ffi.h b/lez/wallet-ffi/wallet_ffi.h index 8f3ad95f..f84b4868 100644 --- a/lez/wallet-ffi/wallet_ffi.h +++ b/lez/wallet-ffi/wallet_ffi.h @@ -922,7 +922,7 @@ enum WalletFfiError wallet_ffi_free_label_list(struct LabelList *label_list); * Produce account id for public PDA. * * # Parameters - * - `program_id`: Id of a owner program + * - `program_id`: Id of the owner program * - `pda_seed`: 32 byte seed * * # Returns @@ -935,9 +935,10 @@ struct FfiBytes32 wallet_ffi_account_id_for_public_pda(struct FfiProgramId progr * Produce account id for private PDA. * * # Parameters - * - `program_id`: Id of a owner program + * - `program_id`: Id of the owner program * - `pda_seed`: 32 byte seed - * - `npk`: 32 byte nullifier public key(can be get from `wallet_ffi_get_private_account_keys`) + * - `npk`: 32 byte nullifier public key (can be obtained from + * `wallet_ffi_get_private_account_keys`) * - `identifier`: little endian encoded `u128` * * # Returns