mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-02-18 04:13:07 +00:00
add get account keys test
This commit is contained in:
parent
5ac9953488
commit
fdeb1f971c
@ -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(())
|
||||
}
|
||||
|
||||
@ -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,
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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<nssa::PublicKey> 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<Self, Self::Error> {
|
||||
let public_key = nssa::PublicKey::try_new(value.public_key.data)
|
||||
.map_err(|_| WalletFfiError::InvalidTypeConversion)?;
|
||||
Ok(public_key)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user