-
Notifications
You must be signed in to change notification settings - Fork 10
fix: clean up ffi methods #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ use std::ptr; | |
|
|
||
| use crate::account::FFIAccount; | ||
| use crate::error::{FFIError, FFIErrorCode}; | ||
| use crate::types::{FFINetwork, FFIWallet}; | ||
| use crate::types::{FFINetworks, FFIWallet}; | ||
|
|
||
| /// Opaque handle to an account collection | ||
| pub struct FFIAccountCollection { | ||
|
|
@@ -83,7 +83,7 @@ pub struct FFIAccountCollectionSummary { | |
| #[no_mangle] | ||
| pub unsafe extern "C" fn wallet_get_account_collection( | ||
| wallet: *const FFIWallet, | ||
| network: FFINetwork, | ||
| network: FFINetworks, | ||
| error: *mut FFIError, | ||
| ) -> *mut FFIAccountCollection { | ||
| if wallet.is_null() { | ||
|
|
@@ -606,39 +606,38 @@ pub unsafe extern "C" fn account_collection_has_provider_owner_keys( | |
| } | ||
|
|
||
| /// Get the provider operator keys account if it exists | ||
| /// Note: This function is only available when the `bls` feature is enabled | ||
| /// Note: Returns null if the `bls` feature is not enabled | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// - `collection` must be a valid pointer to an FFIAccountCollection | ||
| /// - The returned pointer must be freed with `bls_account_free` when no longer needed | ||
| #[cfg(feature = "bls")] | ||
| /// - The returned pointer must be freed with `bls_account_free` when no longer needed (when BLS is enabled) | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn account_collection_get_provider_operator_keys( | ||
| collection: *const FFIAccountCollection, | ||
| ) -> *mut crate::account::FFIBLSAccount { | ||
| if collection.is_null() { | ||
| return ptr::null_mut(); | ||
| } | ||
| ) -> *mut std::os::raw::c_void { | ||
| #[cfg(feature = "bls")] | ||
| { | ||
| if collection.is_null() { | ||
| return ptr::null_mut(); | ||
| } | ||
|
|
||
| let collection = &*collection; | ||
| match &collection.collection.provider_operator_keys { | ||
| Some(account) => { | ||
| let ffi_account = crate::account::FFIBLSAccount::new(account); | ||
| Box::into_raw(Box::new(ffi_account)) | ||
| let collection = &*collection; | ||
| match &collection.collection.provider_operator_keys { | ||
| Some(account) => { | ||
| let ffi_account = crate::account::FFIBLSAccount::new(account); | ||
| Box::into_raw(Box::new(ffi_account)) as *mut std::os::raw::c_void | ||
| } | ||
| None => ptr::null_mut(), | ||
| } | ||
| None => ptr::null_mut(), | ||
| } | ||
| } | ||
|
|
||
| /// Get the provider operator keys account if it exists (stub when BLS is disabled) | ||
| #[cfg(not(feature = "bls"))] | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn account_collection_get_provider_operator_keys( | ||
| _collection: *const FFIAccountCollection, | ||
| ) -> *mut std::os::raw::c_void { | ||
| // BLS feature not enabled, always return null | ||
| ptr::null_mut() | ||
| #[cfg(not(feature = "bls"))] | ||
| { | ||
| // BLS feature not enabled, always return null | ||
| let _ = collection; // Avoid unused parameter warning | ||
| ptr::null_mut() | ||
| } | ||
| } | ||
|
|
||
| /// Check if provider operator keys account exists | ||
|
|
@@ -667,39 +666,38 @@ pub unsafe extern "C" fn account_collection_has_provider_operator_keys( | |
| } | ||
|
|
||
| /// Get the provider platform keys account if it exists | ||
| /// Note: This function is only available when the `eddsa` feature is enabled | ||
| /// Note: Returns null if the `eddsa` feature is not enabled | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// - `collection` must be a valid pointer to an FFIAccountCollection | ||
| /// - The returned pointer must be freed with `eddsa_account_free` when no longer needed | ||
| #[cfg(feature = "eddsa")] | ||
| /// - The returned pointer must be freed with `eddsa_account_free` when no longer needed (when EdDSA is enabled) | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn account_collection_get_provider_platform_keys( | ||
| collection: *const FFIAccountCollection, | ||
| ) -> *mut crate::account::FFIEdDSAAccount { | ||
| if collection.is_null() { | ||
| return ptr::null_mut(); | ||
| } | ||
| ) -> *mut std::os::raw::c_void { | ||
| #[cfg(feature = "eddsa")] | ||
| { | ||
| if collection.is_null() { | ||
| return ptr::null_mut(); | ||
| } | ||
|
|
||
| let collection = &*collection; | ||
| match &collection.collection.provider_platform_keys { | ||
| Some(account) => { | ||
| let ffi_account = crate::account::FFIEdDSAAccount::new(account); | ||
| Box::into_raw(Box::new(ffi_account)) | ||
| let collection = &*collection; | ||
| match &collection.collection.provider_platform_keys { | ||
| Some(account) => { | ||
| let ffi_account = crate::account::FFIEdDSAAccount::new(account); | ||
| Box::into_raw(Box::new(ffi_account)) as *mut std::os::raw::c_void | ||
| } | ||
| None => ptr::null_mut(), | ||
| } | ||
| None => ptr::null_mut(), | ||
| } | ||
| } | ||
|
|
||
| /// Get the provider platform keys account if it exists (stub when EdDSA is disabled) | ||
| #[cfg(not(feature = "eddsa"))] | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn account_collection_get_provider_platform_keys( | ||
| _collection: *const FFIAccountCollection, | ||
| ) -> *mut std::os::raw::c_void { | ||
| // EdDSA feature not enabled, always return null | ||
| ptr::null_mut() | ||
| #[cfg(not(feature = "eddsa"))] | ||
| { | ||
| // EdDSA feature not enabled, always return null | ||
| let _ = collection; // Avoid unused parameter warning | ||
| ptr::null_mut() | ||
| } | ||
| } | ||
|
Comment on lines
676
to
701
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Same concern for EdDSA platform account void* Mirror the fix for operator keys to keep the ABI type-safe. 🤖 Prompt for AI Agents |
||
|
|
||
| /// Check if provider platform keys account exists | ||
|
|
@@ -1100,7 +1098,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null(), | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1109,7 +1107,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
| assert!(!collection.is_null()); | ||
|
|
@@ -1158,7 +1156,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| &options, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1167,7 +1165,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
| assert!(!collection.is_null()); | ||
|
|
@@ -1179,7 +1177,9 @@ mod tests { | |
| assert!(!operator_account.is_null()); | ||
|
|
||
| // Free the BLS account | ||
| crate::account::bls_account_free(operator_account); | ||
| crate::account::bls_account_free( | ||
| operator_account as *mut crate::account::FFIBLSAccount, | ||
| ); | ||
| } | ||
|
|
||
| // Clean up | ||
|
|
@@ -1206,7 +1206,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| &options, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1215,7 +1215,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
| assert!(!collection.is_null()); | ||
|
|
@@ -1227,7 +1227,9 @@ mod tests { | |
| assert!(!platform_account.is_null()); | ||
|
|
||
| // Free the EdDSA account | ||
| crate::account::eddsa_account_free(platform_account); | ||
| crate::account::eddsa_account_free( | ||
| platform_account as *mut crate::account::FFIEdDSAAccount, | ||
| ); | ||
| } | ||
|
|
||
| // Clean up | ||
|
|
@@ -1278,7 +1280,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| &options, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1287,7 +1289,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
| assert!(!collection.is_null()); | ||
|
|
@@ -1334,7 +1336,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| &options, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1343,7 +1345,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
||
|
|
@@ -1422,7 +1424,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| &options, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1431,7 +1433,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
| assert!(!collection.is_null()); | ||
|
|
@@ -1512,7 +1514,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| &options, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1521,7 +1523,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
||
|
|
@@ -1586,7 +1588,7 @@ mod tests { | |
| let wallet = wallet_create_from_mnemonic_with_options( | ||
| mnemonic.as_ptr(), | ||
| ptr::null(), | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null(), | ||
| ptr::null_mut(), | ||
| ); | ||
|
|
@@ -1595,7 +1597,7 @@ mod tests { | |
| // Get account collection | ||
| let collection = wallet_get_account_collection( | ||
| wallet, | ||
| crate::types::FFINetwork::Testnet, | ||
| crate::types::FFINetworks::Testnet, | ||
| ptr::null_mut(), | ||
| ); | ||
| assert!(!collection.is_null()); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Returning void for BLS operator account weakens type safety*
ABI now requires consumers to cast and call the correct free, risking UB if mismatched. Prefer typed return with a stable opaque type and return null when the feature is disabled.
If keeping this ABI, at minimum add a runtime type tag alongside the pointer or provide feature-gated typed getters (and deprecate the void* versions). I can draft a safe-ABI variant if desired.
🤖 Prompt for AI Agents