Skip to content
Closed
14 changes: 13 additions & 1 deletion key-wallet-ffi/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ 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(err) => {
let code = err.code;
let message = if err.message.is_null() {
"Invalid account type".to_string()
} else {
std::ffi::CStr::from_ptr(err.message).to_string_lossy().into_owned()
};
// `err` dropped here; its original message is freed by `Drop`.
return FFIAccountResult::error(code, message);
}
};

match wallet.inner().accounts.account_of_type(account_type_rust) {
Some(account) => {
Expand Down
62 changes: 59 additions & 3 deletions key-wallet-ffi/src/address_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ fn get_managed_account_by_type<'a>(
collection.identity_topup_not_bound.as_ref()
}
AccountType::IdentityInvitation => collection.identity_invitation.as_ref(),
AccountType::IdentityAuthenticationEcdsa {
..
}
| AccountType::IdentityAuthenticationBls {
..
} => {
// DIP-13 per-identity authentication accounts are Platform-only
// and have no managed-side representation.
None
}
AccountType::AssetLockAddressTopUp => collection.asset_lock_address_topup.as_ref(),
AccountType::AssetLockShieldedAddressTopUp => {
collection.asset_lock_shielded_address_topup.as_ref()
Expand Down Expand Up @@ -99,6 +109,16 @@ fn get_managed_account_by_type_mut<'a>(
collection.identity_topup_not_bound.as_mut()
}
AccountType::IdentityInvitation => collection.identity_invitation.as_mut(),
AccountType::IdentityAuthenticationEcdsa {
..
}
| AccountType::IdentityAuthenticationBls {
..
} => {
// DIP-13 per-identity authentication accounts are Platform-only
// and have no managed-side representation.
None
}
AccountType::AssetLockAddressTopUp => collection.asset_lock_address_topup.as_mut(),
AccountType::AssetLockShieldedAddressTopUp => {
collection.asset_lock_shielded_address_topup.as_mut()
Expand Down Expand Up @@ -295,7 +315,19 @@ pub unsafe extern "C" fn managed_wallet_get_address_pool_info(
check_ptr!(info_out, error);
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(e) => {
let msg = if e.message.is_null() {
"Invalid account type".to_string()
} else {
std::ffi::CStr::from_ptr(e.message).to_string_lossy().into_owned()
};
(*error).set(e.code, &msg);
// `e` dropped here; its original message is freed by `Drop`.
return false;
}
};

// Get the specific managed account
let managed_account =
Expand Down Expand Up @@ -384,7 +416,19 @@ pub unsafe extern "C" fn managed_wallet_set_gap_limit(
) -> bool {
let managed_wallet = deref_ptr_mut!(managed_wallet, error).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(e) => {
let msg = if e.message.is_null() {
"Invalid account type".to_string()
} else {
std::ffi::CStr::from_ptr(e.message).to_string_lossy().into_owned()
};
(*error).set(e.code, &msg);
// `e` dropped here; its original message is freed by `Drop`.
return false;
}
};

// Get the specific managed account
let managed_account =
Expand Down Expand Up @@ -464,7 +508,19 @@ pub unsafe extern "C" fn managed_wallet_generate_addresses_to_index(
let managed_wallet = deref_ptr_mut!(managed_wallet, error).inner_mut();
let wallet = deref_ptr!(wallet, error);

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(e) => {
let msg = if e.message.is_null() {
"Invalid account type".to_string()
} else {
std::ffi::CStr::from_ptr(e.message).to_string_lossy().into_owned()
};
(*error).set(e.code, &msg);
// `e` dropped here; its original message is freed by `Drop`.
return false;
}
};

let account_type_to_check = match account_type_rust.try_into() {
Ok(check_type) => check_type,
Expand Down
36 changes: 35 additions & 1 deletion key-wallet-ffi/src/managed_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,24 @@ 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(e) => {
let code = e.code;
let message = if e.message.is_null() {
"Invalid account type".to_string()
} else {
std::ffi::CStr::from_ptr(e.message).to_string_lossy().into_owned()
};
// `e` dropped here; its original message is freed by `Drop`.
// `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 @@ -250,6 +267,17 @@ 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 {
..
}
| AccountType::IdentityAuthenticationBls {
..
} => {
// DIP-13 per-identity authentication accounts are Platform-only
// and have no managed-side representation. Signing keys are
// derived from the immutable `Account` directly.
None
}
AccountType::AssetLockAddressTopUp => {
managed_collection.asset_lock_address_topup.as_ref()
}
Expand Down Expand Up @@ -567,6 +595,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
Loading
Loading