From 70d3b3a3e6912fb1c787cbe14e5fc930df4079ec Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 4 Feb 2026 14:53:54 -0300 Subject: [PATCH] add FfiU128 struct --- integration_tests/tests/wallet_ffi.rs | 41 ++++++++++++++++++++++++++- wallet-ffi/src/types.rs | 39 ++++++++++++++++++------- wallet-ffi/wallet_ffi.h | 11 +++++-- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/integration_tests/tests/wallet_ffi.rs b/integration_tests/tests/wallet_ffi.rs index 03c6acb9..4599a79e 100644 --- a/integration_tests/tests/wallet_ffi.rs +++ b/integration_tests/tests/wallet_ffi.rs @@ -11,7 +11,8 @@ use nssa::{Account, AccountId, PublicKey, program::Program}; use tempfile::tempdir; use wallet::WalletCore; use wallet_ffi::{ - FfiAccount, FfiAccountList, FfiBytes32, FfiPublicAccountKey, WalletHandle, error, + FfiAccount, FfiAccountList, FfiBytes32, FfiPrivateAccountKeys, FfiPublicAccountKey, + WalletHandle, error, }; unsafe extern "C" { @@ -58,6 +59,12 @@ unsafe extern "C" { account_id: *const FfiBytes32, out_public_key: *mut FfiPublicAccountKey, ) -> error::WalletFfiError; + + fn wallet_ffi_get_private_account_keys( + handle: *mut WalletHandle, + account_id: *const FfiBytes32, + out_keys: *mut FfiPrivateAccountKeys, + ) -> error::WalletFfiError; } fn new_wallet_ffi_with_test_context_config(ctx: &TestContext) -> *mut WalletHandle { @@ -369,3 +376,35 @@ fn test_wallet_ffi_get_public_account_keys() -> Result<()> { Ok(()) } + +// #[test] +// fn test_wallet_ffi_get_private_account_keys() -> Result<()> { +// let ctx = TestContext::new_blocking()?; +// let account_id: AccountId = ACC_SENDER.parse().unwrap(); +// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); +// let mut out_key = FfiPrivateAccountKeys::default(); +// +// let key: PublicKey = unsafe { +// let ffi_account_id = FfiBytes32::from(&account_id); +// let _result = wallet_ffi_get_private_account_keys( +// wallet_ffi_handle, +// (&ffi_account_id) as *const FfiBytes32, +// (&mut out_key) as *mut FfiPublicAccountKey, +// ); +// (&out_key).try_into().unwrap() +// }; +// +// let expected_key = { +// let private_key = ctx +// .wallet() +// .get_account_public_signing_key(&account_id) +// .unwrap(); +// PublicKey::new_from_private_key(private_key) +// }; +// +// assert_eq!(key, expected_key); +// +// info!("Successfully retrieved account key"); +// +// Ok(()) +// } diff --git a/wallet-ffi/src/types.rs b/wallet-ffi/src/types.rs index 0078e09f..dea1ac94 100644 --- a/wallet-ffi/src/types.rs +++ b/wallet-ffi/src/types.rs @@ -30,6 +30,13 @@ pub struct FfiProgramId { pub data: [u32; 8], } +/// U128 - 16 bytes little endian +#[repr(C)] +#[derive(Clone, Copy, Default)] +pub struct FfiU128 { + pub data: [u8; 16], +} + /// Account data structure - C-compatible version of nssa Account. /// /// Note: `balance` and `nonce` are u128 values represented as little-endian @@ -38,23 +45,23 @@ pub struct FfiProgramId { pub struct FfiAccount { pub program_owner: FfiProgramId, /// Balance as little-endian [u8; 16] - pub balance: [u8; 16], + pub balance: FfiU128, /// Pointer to account data bytes pub data: *const u8, /// Length of account data pub data_len: usize, /// Nonce as little-endian [u8; 16] - pub nonce: [u8; 16], + pub nonce: FfiU128, } impl Default for FfiAccount { fn default() -> Self { Self { program_owner: FfiProgramId::default(), - balance: [0u8; 16], + balance: FfiU128::default(), data: std::ptr::null(), data_len: 0, - nonce: [0u8; 16], + nonce: FfiU128::default(), } } } @@ -143,6 +150,20 @@ impl FfiBytes32 { } } +impl From for FfiU128 { + fn from(value: u128) -> Self { + Self { + data: value.to_le_bytes(), + } + } +} + +impl From for u128 { + fn from(value: FfiU128) -> Self { + u128::from_le_bytes(value.data) + } +} + impl From<&nssa::AccountId> for FfiBytes32 { fn from(id: &nssa::AccountId) -> Self { Self::from_account_id(id) @@ -172,10 +193,10 @@ impl From for FfiAccount { }; FfiAccount { program_owner, - balance: value.balance.to_le_bytes(), + balance: value.balance.into(), data, data_len, - nonce: value.nonce.to_le_bytes(), + nonce: value.nonce.into(), } } } @@ -192,13 +213,11 @@ impl TryFrom<&FfiAccount> for nssa::Account { } else { Data::default() }; - let balance = u128::from_le_bytes(value.balance); - let nonce = u128::from_le_bytes(value.nonce); Ok(Account { program_owner: value.program_owner.data, - balance, + balance: value.balance.into(), data, - nonce, + nonce: value.nonce.into(), }) } } diff --git a/wallet-ffi/wallet_ffi.h b/wallet-ffi/wallet_ffi.h index fae24a4d..e731db79 100644 --- a/wallet-ffi/wallet_ffi.h +++ b/wallet-ffi/wallet_ffi.h @@ -145,6 +145,13 @@ typedef struct FfiProgramId { uint32_t data[8]; } FfiProgramId; +/** + * U128 - 16 bytes little endian + */ +typedef struct FfiU128 { + uint8_t data[16]; +} FfiU128; + /** * Account data structure - C-compatible version of nssa Account. * @@ -156,7 +163,7 @@ typedef struct FfiAccount { /** * Balance as little-endian [u8; 16] */ - uint8_t balance[16]; + struct FfiU128 balance; /** * Pointer to account data bytes */ @@ -168,7 +175,7 @@ typedef struct FfiAccount { /** * Nonce as little-endian [u8; 16] */ - uint8_t nonce[16]; + struct FfiU128 nonce; } FfiAccount; /**