From fdeb1f971ca2fc83acbb72597218c2cc2b4852cd Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 4 Feb 2026 12:11:43 -0300 Subject: [PATCH] add get account keys test --- integration_tests/tests/wallet_ffi.rs | 44 +++++++++++++++++++++++++-- wallet-ffi/src/error.rs | 4 +-- wallet-ffi/src/keys.rs | 2 +- wallet-ffi/src/types.rs | 20 +++++++++++- wallet-ffi/wallet_ffi.h | 4 +-- 5 files changed, 66 insertions(+), 8 deletions(-) diff --git a/integration_tests/tests/wallet_ffi.rs b/integration_tests/tests/wallet_ffi.rs index a95ab319..03c6acb9 100644 --- a/integration_tests/tests/wallet_ffi.rs +++ b/integration_tests/tests/wallet_ffi.rs @@ -7,10 +7,12 @@ use std::{ use anyhow::Result; use integration_tests::{ACC_SENDER, TestContext}; use log::info; -use nssa::{Account, AccountId, program::Program}; +use nssa::{Account, AccountId, PublicKey, program::Program}; use tempfile::tempdir; use wallet::WalletCore; -use wallet_ffi::{FfiAccount, FfiAccountList, FfiBytes32, WalletHandle, error}; +use wallet_ffi::{ + FfiAccount, FfiAccountList, FfiBytes32, FfiPublicAccountKey, WalletHandle, error, +}; unsafe extern "C" { fn wallet_ffi_create_new( @@ -50,6 +52,12 @@ unsafe extern "C" { ) -> error::WalletFfiError; fn wallet_ffi_free_account_data(account: *mut FfiAccount); + + fn wallet_ffi_get_public_account_key( + handle: *mut WalletHandle, + account_id: *const FfiBytes32, + out_public_key: *mut FfiPublicAccountKey, + ) -> error::WalletFfiError; } fn new_wallet_ffi_with_test_context_config(ctx: &TestContext) -> *mut WalletHandle { @@ -329,3 +337,35 @@ fn test_wallet_ffi_get_account_public() -> Result<()> { Ok(()) } + +#[test] +fn test_wallet_ffi_get_public_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 = FfiPublicAccountKey::default(); + + let key: PublicKey = unsafe { + let ffi_account_id = FfiBytes32::from(&account_id); + let _result = wallet_ffi_get_public_account_key( + 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/error.rs b/wallet-ffi/src/error.rs index c57505a8..3cae2f25 100644 --- a/wallet-ffi/src/error.rs +++ b/wallet-ffi/src/error.rs @@ -36,8 +36,8 @@ pub enum WalletFfiError { SyncError = 13, /// Serialization/deserialization error SerializationError = 14, - /// Found invalid data for account - InvalidAccountData, + /// Invalid conversion from FFI types to NSSA types + InvalidTypeConversion, /// Internal error (catch-all) InternalError = 99, } diff --git a/wallet-ffi/src/keys.rs b/wallet-ffi/src/keys.rs index e8309a81..08661a50 100644 --- a/wallet-ffi/src/keys.rs +++ b/wallet-ffi/src/keys.rs @@ -65,7 +65,7 @@ pub unsafe extern "C" fn wallet_ffi_get_public_account_key( let public_key = PublicKey::new_from_private_key(private_key); unsafe { - (*out_public_key).public_key.data = *public_key.value(); + *out_public_key = public_key.into(); } WalletFfiError::Success diff --git a/wallet-ffi/src/types.rs b/wallet-ffi/src/types.rs index e7629afc..0078e09f 100644 --- a/wallet-ffi/src/types.rs +++ b/wallet-ffi/src/types.rs @@ -187,7 +187,7 @@ impl TryFrom<&FfiAccount> for nssa::Account { let data = if value.data_len > 0 { unsafe { let slice = slice::from_raw_parts(value.data, value.data_len); - Data::try_from(slice.to_vec()).map_err(|_| WalletFfiError::InvalidAccountData)? + Data::try_from(slice.to_vec()).map_err(|_| WalletFfiError::InvalidTypeConversion)? } } else { Data::default() @@ -202,3 +202,21 @@ impl TryFrom<&FfiAccount> for nssa::Account { }) } } + +impl From for FfiPublicAccountKey { + fn from(value: nssa::PublicKey) -> Self { + Self { + public_key: FfiBytes32::from_bytes(*value.value()), + } + } +} + +impl TryFrom<&FfiPublicAccountKey> for nssa::PublicKey { + type Error = WalletFfiError; + + fn try_from(value: &FfiPublicAccountKey) -> Result { + let public_key = nssa::PublicKey::try_new(value.public_key.data) + .map_err(|_| WalletFfiError::InvalidTypeConversion)?; + Ok(public_key) + } +} diff --git a/wallet-ffi/wallet_ffi.h b/wallet-ffi/wallet_ffi.h index 9b1850d8..fae24a4d 100644 --- a/wallet-ffi/wallet_ffi.h +++ b/wallet-ffi/wallet_ffi.h @@ -96,9 +96,9 @@ typedef enum WalletFfiError { */ SERIALIZATION_ERROR = 14, /** - * Found invalid data for account + * Invalid conversion from FFI types to NSSA types */ - INVALID_ACCOUNT_DATA, + INVALID_TYPE_CONVERSION, /** * Internal error (catch-all) */