mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-02-17 11:53:14 +00:00
add wallet ffi get private keys test
This commit is contained in:
parent
70d3b3a3e6
commit
7c3182d903
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -6745,6 +6745,7 @@ dependencies = [
|
||||
"cbindgen",
|
||||
"common",
|
||||
"nssa",
|
||||
"nssa_core",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"wallet",
|
||||
|
||||
@ -5,7 +5,7 @@ use std::{
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use integration_tests::{ACC_SENDER, TestContext};
|
||||
use integration_tests::{ACC_SENDER, ACC_SENDER_PRIVATE, TestContext};
|
||||
use log::info;
|
||||
use nssa::{Account, AccountId, PublicKey, program::Program};
|
||||
use tempfile::tempdir;
|
||||
@ -65,6 +65,8 @@ unsafe extern "C" {
|
||||
account_id: *const FfiBytes32,
|
||||
out_keys: *mut FfiPrivateAccountKeys,
|
||||
) -> error::WalletFfiError;
|
||||
|
||||
fn wallet_ffi_free_private_account_keys(keys: *mut FfiPrivateAccountKeys);
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
// #[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(())
|
||||
// }
|
||||
#[test]
|
||||
fn test_wallet_ffi_get_private_account_keys() -> Result<()> {
|
||||
let ctx = TestContext::new_blocking()?;
|
||||
let account_id: AccountId = ACC_SENDER_PRIVATE.parse().unwrap();
|
||||
let wallet_ffi_handle = new_wallet_ffi_with_test_context_config(&ctx);
|
||||
let mut keys = FfiPrivateAccountKeys::default();
|
||||
|
||||
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 keys) as *mut FfiPrivateAccountKeys,
|
||||
);
|
||||
};
|
||||
|
||||
let key_chain = &ctx
|
||||
.wallet()
|
||||
.storage()
|
||||
.user_data
|
||||
.get_private_account(&account_id)
|
||||
.unwrap()
|
||||
.0;
|
||||
|
||||
let expected_npk = &key_chain.nullifer_public_key;
|
||||
let expected_ivk = &key_chain.incoming_viewing_public_key;
|
||||
|
||||
assert_eq!(&keys.npk(), expected_npk);
|
||||
assert_eq!(&keys.ivk().unwrap(), expected_ivk);
|
||||
|
||||
unsafe {
|
||||
wallet_ffi_free_private_account_keys((&mut keys) as *mut FfiPrivateAccountKeys);
|
||||
}
|
||||
|
||||
info!("Successfully retrieved account keys");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ crate-type = ["rlib", "cdylib", "staticlib"]
|
||||
wallet.workspace = true
|
||||
nssa.workspace = true
|
||||
common.workspace = true
|
||||
nssa_core.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
|
||||
@ -38,6 +38,8 @@ pub enum WalletFfiError {
|
||||
SerializationError = 14,
|
||||
/// Invalid conversion from FFI types to NSSA types
|
||||
InvalidTypeConversion,
|
||||
/// Invalid Key value
|
||||
InvalidKeyValue,
|
||||
/// Internal error (catch-all)
|
||||
InternalError = 99,
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ use core::slice;
|
||||
use std::{ffi::c_char, ptr};
|
||||
|
||||
use nssa::{Account, Data};
|
||||
use nssa_core::encryption::shared_key_derivation::Secp256k1Point;
|
||||
|
||||
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 {
|
||||
fn from(value: u128) -> Self {
|
||||
Self {
|
||||
|
||||
@ -99,6 +99,10 @@ typedef enum WalletFfiError {
|
||||
* Invalid conversion from FFI types to NSSA types
|
||||
*/
|
||||
INVALID_TYPE_CONVERSION,
|
||||
/**
|
||||
* Invalid Key value
|
||||
*/
|
||||
INVALID_KEY_VALUE,
|
||||
/**
|
||||
* Internal error (catch-all)
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user