add wallet ffi get private keys test

This commit is contained in:
Sergio Chouhy 2026-02-04 15:27:39 -03:00
parent 70d3b3a3e6
commit 7c3182d903
6 changed files with 70 additions and 32 deletions

1
Cargo.lock generated
View File

@ -6745,6 +6745,7 @@ dependencies = [
"cbindgen", "cbindgen",
"common", "common",
"nssa", "nssa",
"nssa_core",
"tempfile", "tempfile",
"tokio", "tokio",
"wallet", "wallet",

View File

@ -5,7 +5,7 @@ use std::{
}; };
use anyhow::Result; use anyhow::Result;
use integration_tests::{ACC_SENDER, TestContext}; use integration_tests::{ACC_SENDER, ACC_SENDER_PRIVATE, TestContext};
use log::info; use log::info;
use nssa::{Account, AccountId, PublicKey, program::Program}; use nssa::{Account, AccountId, PublicKey, program::Program};
use tempfile::tempdir; use tempfile::tempdir;
@ -65,6 +65,8 @@ unsafe extern "C" {
account_id: *const FfiBytes32, account_id: *const FfiBytes32,
out_keys: *mut FfiPrivateAccountKeys, out_keys: *mut FfiPrivateAccountKeys,
) -> error::WalletFfiError; ) -> error::WalletFfiError;
fn wallet_ffi_free_private_account_keys(keys: *mut FfiPrivateAccountKeys);
} }
fn new_wallet_ffi_with_test_context_config(ctx: &TestContext) -> *mut WalletHandle { fn new_wallet_ffi_with_test_context_config(ctx: &TestContext) -> *mut WalletHandle {
@ -377,34 +379,41 @@ fn test_wallet_ffi_get_public_account_keys() -> Result<()> {
Ok(()) Ok(())
} }
// #[test] #[test]
// fn test_wallet_ffi_get_private_account_keys() -> Result<()> { fn test_wallet_ffi_get_private_account_keys() -> Result<()> {
// let ctx = TestContext::new_blocking()?; let ctx = TestContext::new_blocking()?;
// let account_id: AccountId = ACC_SENDER.parse().unwrap(); let account_id: AccountId = ACC_SENDER_PRIVATE.parse().unwrap();
// let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx); let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx);
// let mut out_key = FfiPrivateAccountKeys::default(); let mut keys = FfiPrivateAccountKeys::default();
//
// let key: PublicKey = unsafe { unsafe {
// let ffi_account_id = FfiBytes32::from(&account_id); let ffi_account_id = FfiBytes32::from(&account_id);
// let _result = wallet_ffi_get_private_account_keys( let _result = wallet_ffi_get_private_account_keys(
// wallet_ffi_handle, wallet_ffi_handle,
// (&ffi_account_id) as *const FfiBytes32, (&ffi_account_id) as *const FfiBytes32,
// (&mut out_key) as *mut FfiPublicAccountKey, (&mut keys) as *mut FfiPrivateAccountKeys,
// ); );
// (&out_key).try_into().unwrap() };
// };
// let key_chain = &ctx
// let expected_key = { .wallet()
// let private_key = ctx .storage()
// .wallet() .user_data
// .get_account_public_signing_key(&account_id) .get_private_account(&account_id)
// .unwrap(); .unwrap()
// PublicKey::new_from_private_key(private_key) .0;
// };
// let expected_npk = &key_chain.nullifer_public_key;
// assert_eq!(key, expected_key); let expected_ivk = &key_chain.incoming_viewing_public_key;
//
// info!("Successfully retrieved account key"); assert_eq!(&keys.npk(), expected_npk);
// assert_eq!(&keys.ivk().unwrap(), expected_ivk);
// Ok(())
// } unsafe {
wallet_ffi_free_private_account_keys((&mut keys) as *mut FfiPrivateAccountKeys);
}
info!("Successfully retrieved account keys");
Ok(())
}

View File

@ -10,6 +10,7 @@ crate-type = ["rlib", "cdylib", "staticlib"]
wallet.workspace = true wallet.workspace = true
nssa.workspace = true nssa.workspace = true
common.workspace = true common.workspace = true
nssa_core.workspace = true
tokio.workspace = true tokio.workspace = true
[build-dependencies] [build-dependencies]

View File

@ -38,6 +38,8 @@ pub enum WalletFfiError {
SerializationError = 14, SerializationError = 14,
/// Invalid conversion from FFI types to NSSA types /// Invalid conversion from FFI types to NSSA types
InvalidTypeConversion, InvalidTypeConversion,
/// Invalid Key value
InvalidKeyValue,
/// Internal error (catch-all) /// Internal error (catch-all)
InternalError = 99, InternalError = 99,
} }

View File

@ -4,6 +4,7 @@ use core::slice;
use std::{ffi::c_char, ptr}; use std::{ffi::c_char, ptr};
use nssa::{Account, Data}; use nssa::{Account, Data};
use nssa_core::encryption::shared_key_derivation::Secp256k1Point;
use crate::error::WalletFfiError; use crate::error::WalletFfiError;
@ -150,6 +151,26 @@ impl FfiBytes32 {
} }
} }
impl FfiPrivateAccountKeys {
pub fn npk(&self) -> nssa_core::NullifierPublicKey {
nssa_core::NullifierPublicKey(self.nullifier_public_key.data)
}
pub fn ivk(&self) -> Result<nssa_core::encryption::IncomingViewingPublicKey, WalletFfiError> {
if self.incoming_viewing_public_key_len == 33 {
let slice = unsafe {
slice::from_raw_parts(
self.incoming_viewing_public_key,
self.incoming_viewing_public_key_len,
)
};
Ok(Secp256k1Point(slice.to_vec()))
} else {
Err(WalletFfiError::InvalidKeyValue)
}
}
}
impl From<u128> for FfiU128 { impl From<u128> for FfiU128 {
fn from(value: u128) -> Self { fn from(value: u128) -> Self {
Self { Self {

View File

@ -99,6 +99,10 @@ typedef enum WalletFfiError {
* Invalid conversion from FFI types to NSSA types * Invalid conversion from FFI types to NSSA types
*/ */
INVALID_TYPE_CONVERSION, INVALID_TYPE_CONVERSION,
/**
* Invalid Key value
*/
INVALID_KEY_VALUE,
/** /**
* Internal error (catch-all) * Internal error (catch-all)
*/ */