Skip to content
Closed
15 changes: 14 additions & 1 deletion key-wallet-ffi/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,20 @@ pub unsafe extern "C" fn wallet_get_account(
}

let wallet = &*wallet;
let account_type_rust = account_type.to_account_type(account_index);
let account_type_rust = match account_type.to_account_type(account_index) {
Ok(t) => t,
Err(mut err) => {
let code = err.code;
let message = if err.message.is_null() {
"Invalid account type".to_string()
} else {
let msg = std::ffi::CStr::from_ptr(err.message).to_string_lossy().to_string();
err.free_message();
msg
};
return FFIAccountResult::error(code, message);
}
};

match wallet.inner().accounts.account_of_type(account_type_rust) {
Some(account) => {
Expand Down
73 changes: 70 additions & 3 deletions key-wallet-ffi/src/address_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ fn get_managed_account_by_type<'a>(
collection.identity_topup_not_bound.as_ref()
}
AccountType::IdentityInvitation => collection.identity_invitation.as_ref(),
AccountType::IdentityAuthenticationEcdsa {
identity_index,
} => collection.identity_authentication_ecdsa.get(identity_index),
AccountType::IdentityAuthenticationBls {
identity_index,
} => collection.identity_authentication_bls.get(identity_index),
AccountType::AssetLockAddressTopUp => collection.asset_lock_address_topup.as_ref(),
AccountType::AssetLockShieldedAddressTopUp => {
collection.asset_lock_shielded_address_topup.as_ref()
Expand Down Expand Up @@ -98,6 +104,12 @@ fn get_managed_account_by_type_mut<'a>(
collection.identity_topup_not_bound.as_mut()
}
AccountType::IdentityInvitation => collection.identity_invitation.as_mut(),
AccountType::IdentityAuthenticationEcdsa {
identity_index,
} => collection.identity_authentication_ecdsa.get_mut(identity_index),
AccountType::IdentityAuthenticationBls {
identity_index,
} => collection.identity_authentication_bls.get_mut(identity_index),
AccountType::AssetLockAddressTopUp => collection.asset_lock_address_topup.as_mut(),
AccountType::AssetLockShieldedAddressTopUp => {
collection.asset_lock_shielded_address_topup.as_mut()
Expand Down Expand Up @@ -298,7 +310,20 @@ pub unsafe extern "C" fn managed_wallet_get_address_pool_info(
let wrapper = &*managed_wallet;
let managed_wallet = wrapper.inner();

let account_type_rust = account_type.to_account_type(account_index);
let account_type_rust = match account_type.to_account_type(account_index) {
Ok(t) => t,
Err(mut e) => {
let msg = if e.message.is_null() {
"Invalid account type".to_string()
} else {
let m = std::ffi::CStr::from_ptr(e.message).to_string_lossy().to_string();
e.free_message();
m
};
FFIError::set_error(error, e.code, msg);
return false;
}
};

// Get the specific managed account
let managed_account =
Expand Down Expand Up @@ -404,7 +429,20 @@ pub unsafe extern "C" fn managed_wallet_set_gap_limit(

let managed_wallet = (&mut *managed_wallet).inner_mut();

let account_type_rust = account_type.to_account_type(account_index);
let account_type_rust = match account_type.to_account_type(account_index) {
Ok(t) => t,
Err(mut e) => {
let msg = if e.message.is_null() {
"Invalid account type".to_string()
} else {
let m = std::ffi::CStr::from_ptr(e.message).to_string_lossy().to_string();
e.free_message();
m
};
FFIError::set_error(error, e.code, msg);
return false;
}
};

// Get the specific managed account
let managed_account =
Expand Down Expand Up @@ -501,7 +539,20 @@ pub unsafe extern "C" fn managed_wallet_generate_addresses_to_index(
let managed_wallet = (&mut *managed_wallet).inner_mut();
let wallet = &*wallet;

let account_type_rust = account_type.to_account_type(account_index);
let account_type_rust = match account_type.to_account_type(account_index) {
Ok(t) => t,
Err(mut e) => {
let msg = if e.message.is_null() {
"Invalid account type".to_string()
} else {
let m = std::ffi::CStr::from_ptr(e.message).to_string_lossy().to_string();
e.free_message();
m
};
FFIError::set_error(error, e.code, msg);
return false;
}
};

let account_type_to_check = match account_type_rust.try_into() {
Ok(check_type) => check_type,
Expand Down Expand Up @@ -746,6 +797,22 @@ pub unsafe extern "C" fn managed_wallet_mark_address_used(
}
}
}
if !found {
for account in collection.identity_authentication_ecdsa.values_mut() {
if account.mark_address_used(&address) {
found = true;
break;
}
}
}
if !found {
for account in collection.identity_authentication_bls.values_mut() {
if account.mark_address_used(&address) {
found = true;
break;
}
}
}
if !found {
if let Some(account) = &mut collection.asset_lock_address_topup {
if account.mark_address_used(&address) {
Expand Down
40 changes: 39 additions & 1 deletion key-wallet-ffi/src/managed_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,25 @@ pub unsafe extern "C" fn managed_wallet_get_account(
}

let managed_wallet = &*managed_wallet_ptr;
let account_type_rust = account_type.to_account_type(account_index);
let account_type_rust = match account_type.to_account_type(account_index) {
Ok(t) => t,
Err(mut e) => {
let code = e.code;
let message = if e.message.is_null() {
"Invalid account type".to_string()
} else {
let m = std::ffi::CStr::from_ptr(e.message).to_string_lossy().to_string();
e.free_message();
m
};
// `wallet_manager_get_managed_wallet_info` allocated `managed_wallet_ptr`
// above; the success path frees it via `managed_wallet_info_free` at the
// bottom of this function. Do the same here before bailing so the
// managed-wallet handle isn't leaked on invalid-account-type errors.
crate::managed_wallet::managed_wallet_info_free(managed_wallet_ptr);
return FFIManagedCoreAccountResult::error(code, message);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
};

let result = {
use key_wallet::account::StandardAccountType;
Expand Down Expand Up @@ -247,6 +265,12 @@ pub unsafe extern "C" fn managed_wallet_get_account(
managed_collection.identity_topup_not_bound.as_ref()
}
AccountType::IdentityInvitation => managed_collection.identity_invitation.as_ref(),
AccountType::IdentityAuthenticationEcdsa {
identity_index,
} => managed_collection.identity_authentication_ecdsa.get(&identity_index),
AccountType::IdentityAuthenticationBls {
identity_index,
} => managed_collection.identity_authentication_bls.get(&identity_index),
AccountType::AssetLockAddressTopUp => {
managed_collection.asset_lock_address_topup.as_ref()
}
Expand Down Expand Up @@ -564,6 +588,12 @@ pub unsafe extern "C" fn managed_core_account_get_account_type(
FFIAccountType::IdentityTopUpNotBoundToIdentity
}
AccountType::IdentityInvitation => FFIAccountType::IdentityInvitation,
AccountType::IdentityAuthenticationEcdsa {
..
} => FFIAccountType::IdentityAuthenticationEcdsa,
AccountType::IdentityAuthenticationBls {
..
} => FFIAccountType::IdentityAuthenticationBls,
AccountType::AssetLockAddressTopUp => FFIAccountType::AssetLockAddressTopUp,
AccountType::AssetLockShieldedAddressTopUp => FFIAccountType::AssetLockShieldedAddressTopUp,
AccountType::ProviderVotingKeys => FFIAccountType::ProviderVotingKeys,
Expand Down Expand Up @@ -1167,6 +1197,14 @@ pub unsafe extern "C" fn managed_core_account_get_address_pool(
addresses,
..
} => addresses,
ManagedAccountType::IdentityAuthenticationEcdsa {
addresses,
..
} => addresses,
ManagedAccountType::IdentityAuthenticationBls {
addresses,
..
} => addresses,
};

let ffi_pool = FFIAddressPool {
Expand Down
Loading
Loading