From 0ee39e04ee159b39c1c9a90180019b56d996fb6a Mon Sep 17 00:00:00 2001 From: Pravdyvy Date: Fri, 26 Jun 2026 18:54:15 +0300 Subject: [PATCH] feat(wallet-ffi): pda helpers --- lez/wallet-ffi/src/lib.rs | 1 + lez/wallet-ffi/src/pda.rs | 45 +++++++++++++++++++++++++++++++++++++ lez/wallet-ffi/src/types.rs | 18 ++++++++++++++- lez/wallet-ffi/wallet_ffi.h | 34 ++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 lez/wallet-ffi/src/pda.rs diff --git a/lez/wallet-ffi/src/lib.rs b/lez/wallet-ffi/src/lib.rs index 361ae672..c91185e6 100644 --- a/lez/wallet-ffi/src/lib.rs +++ b/lez/wallet-ffi/src/lib.rs @@ -47,6 +47,7 @@ pub mod error; pub mod generic_transaction; pub mod keys; pub mod label; +pub mod pda; pub mod pinata; pub mod program_deployment; pub mod sync; diff --git a/lez/wallet-ffi/src/pda.rs b/lez/wallet-ffi/src/pda.rs new file mode 100644 index 00000000..82269e20 --- /dev/null +++ b/lez/wallet-ffi/src/pda.rs @@ -0,0 +1,45 @@ +use lee::AccountId; + +use crate::{FfiBytes32, FfiNullifierPublicKey, FfiPdaSeed, FfiProgramId, FfiU128}; + +/// Produce account id for public PDA. +/// +/// # Parameters +/// - `program_id`: Id of a owner program +/// - `pda_seed`: 32 byte seed +/// +/// # Returns +/// - `FfiBytes32` representing account id bytes +#[no_mangle] +pub unsafe extern "C" fn wallet_ffi_account_id_for_public_pda( + program_id: FfiProgramId, + pda_seed: FfiPdaSeed, +) -> FfiBytes32 { + AccountId::for_public_pda(&program_id.data, &pda_seed.into()).into() +} + +/// Produce account id for public PDA. +/// +/// # Parameters +/// - `program_id`: Id of a owner program +/// - `pda_seed`: 32 byte seed +/// - `npk`: 32 byte nullifier public key(can be get from `wallet_ffi_get_private_account_keys`) +/// - `identifier`: little endian encoded `u128` +/// +/// # Returns +/// - `FfiBytes32` representing account id bytes +#[no_mangle] +pub unsafe extern "C" fn wallet_ffi_account_id_for_private_pda( + program_id: FfiProgramId, + pda_seed: FfiPdaSeed, + npk: FfiNullifierPublicKey, + identifier: FfiU128, +) -> FfiBytes32 { + AccountId::for_private_pda( + &program_id.data, + &pda_seed.into(), + &npk.into(), + identifier.into(), + ) + .into() +} diff --git a/lez/wallet-ffi/src/types.rs b/lez/wallet-ffi/src/types.rs index 98590619..848649b0 100644 --- a/lez/wallet-ffi/src/types.rs +++ b/lez/wallet-ffi/src/types.rs @@ -8,7 +8,7 @@ use std::{ }; use lee::{Data, ProgramId, SharedSecretKey}; -use lee_core::{encryption::MlKem768EncapsulationKey, NullifierPublicKey}; +use lee_core::{encryption::MlKem768EncapsulationKey, program::PdaSeed, NullifierPublicKey}; use wallet::{account::AccountIdWithPrivacy, AccountIdentity}; use crate::error::WalletFfiError; @@ -29,6 +29,22 @@ pub struct FfiBytes32 { pub data: [u8; 32], } +pub type FfiPdaSeed = FfiBytes32; + +impl From for PdaSeed { + fn from(value: FfiPdaSeed) -> Self { + Self::new(value.data) + } +} + +pub type FfiNullifierPublicKey = FfiBytes32; + +impl From for NullifierPublicKey { + fn from(value: FfiNullifierPublicKey) -> Self { + Self(value.data) + } +} + /// Program ID - 8 u32 values (32 bytes total). #[repr(C)] #[derive(Clone, Copy, Default)] diff --git a/lez/wallet-ffi/wallet_ffi.h b/lez/wallet-ffi/wallet_ffi.h index 8f15a888..c5d24af7 100644 --- a/lez/wallet-ffi/wallet_ffi.h +++ b/lez/wallet-ffi/wallet_ffi.h @@ -320,6 +320,10 @@ typedef struct LabelList { enum WalletFfiError error; } LabelList; +typedef struct FfiBytes32 FfiPdaSeed; + +typedef struct FfiBytes32 FfiNullifierPublicKey; + typedef struct FfiCreateWalletOutput { struct WalletHandle *wallet; /** @@ -914,6 +918,36 @@ struct LabelList wallet_ffi_get_all_labels_for_account(struct WalletHandle *hand */ 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 + * - `pda_seed`: 32 byte seed + * + * # Returns + * - `FfiBytes32` representing account id bytes + */ +struct FfiBytes32 wallet_ffi_account_id_for_public_pda(struct FfiProgramId program_id, + FfiPdaSeed pda_seed); + +/** + * Produce account id for public PDA. + * + * # Parameters + * - `program_id`: Id of a owner program + * - `pda_seed`: 32 byte seed + * - `npk`: 32 byte nullifier public key(can be get from `wallet_ffi_get_private_account_keys`) + * - `identifier`: little endian encoded `u128` + * + * # Returns + * - `FfiBytes32` representing account id bytes + */ +struct FfiBytes32 wallet_ffi_account_id_for_private_pda(struct FfiProgramId program_id, + FfiPdaSeed pda_seed, + FfiNullifierPublicKey npk, + struct FfiU128 identifier); + /** * Claim a pinata reward using a public transaction. *