Repository navigation
feat: build out key-wallet ffi and improve ci #108
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
Changes from 11 commits
3c35153
7f8f19d
90e0442
ea9bbe1
e2c33ff
97e6b59
461baeb
678a120
c90f618
b49ec4a
c743035
6a62a2d
34154d4
34427ee
36d7bc8
25de03f
43d87b4
ba9c28f
2e3064d
2d0039e
6cf2223
bdd57dc
352fe89
e9871fd
51c06da
7f9799f
6e3b0b9
07bcdb4
88ccbe2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,19 @@ | ||
| // Build script for key-wallet-ffi | ||
| // No longer uses uniffi - just standard FFI compilation | ||
|
|
||
| fn main() { | ||
| println!("cargo:rerun-if-changed=src/key_wallet.udl"); | ||
| uniffi::generate_scaffolding("src/key_wallet.udl").unwrap(); | ||
| // Add any necessary build configuration here | ||
| // For example, linking flags for different platforms | ||
|
|
||
| let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); | ||
|
|
||
| match target_os.as_str() { | ||
| "ios" => { | ||
| println!("cargo:rustc-link-lib=framework=Security"); | ||
| } | ||
| "macos" => { | ||
| println!("cargo:rustc-link-lib=framework=Security"); | ||
| } | ||
| _ => {} | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Example of using wallet_check_transaction FFI function | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdint.h> | ||
| #include <stdbool.h> | ||
| #include <string.h> | ||
|
|
||
| // FFI type definitions (normally these would be in a header file) | ||
| typedef enum { | ||
| Dash = 0, | ||
| Testnet = 1, | ||
| Regtest = 2, | ||
| Devnet = 3 | ||
| } FFINetwork; | ||
|
|
||
| typedef enum { | ||
| Mempool = 0, | ||
| InBlock = 1, | ||
| InChainLockedBlock = 2 | ||
| } FFITransactionContext; | ||
|
|
||
| typedef struct { | ||
| bool is_relevant; | ||
| uint64_t total_received; | ||
| uint64_t total_sent; | ||
| uint32_t affected_accounts_count; | ||
| } FFITransactionCheckResult; | ||
|
|
||
| typedef struct { | ||
| int32_t code; | ||
| char* message; | ||
| } FFIError; | ||
|
|
||
|
Comment on lines
+8
to
+33
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 Do not redefine FFI types in the example; include the public header instead Manually duplicating FFI type definitions risks divergence from the library ABI and breaks on future changes. Include the installed header to guarantee consistency. Apply: #include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
+#include <inttypes.h>
-// FFI type definitions (normally these would be in a header file)
-typedef enum {
- Dash = 0,
- Testnet = 1,
- Regtest = 2,
- Devnet = 3
-} FFINetwork;
-
-typedef enum {
- Mempool = 0,
- InBlock = 1,
- InChainLockedBlock = 2
-} FFITransactionContext;
-
-typedef struct {
- bool is_relevant;
- uint64_t total_received;
- uint64_t total_sent;
- uint32_t affected_accounts_count;
-} FFITransactionCheckResult;
-
-typedef struct {
- int32_t code;
- char* message;
-} FFIError;
+// Prefer including the SDK's public header. Adjust the path as needed when building standalone.
+#include "../include/key_wallet_ffi.h"Also applies to: 3-7 🤖 Prompt for AI Agents |
||
| // External function declarations | ||
| extern void* wallet_create_from_mnemonic( | ||
| const char* mnemonic, | ||
| const char* passphrase, | ||
| FFINetwork network, | ||
| FFIError* error | ||
| ); | ||
|
|
||
| extern bool wallet_check_transaction( | ||
| void* wallet, | ||
| FFINetwork network, | ||
| const uint8_t* tx_bytes, | ||
| size_t tx_len, | ||
| FFITransactionContext context_type, | ||
| uint32_t block_height, | ||
| const uint8_t* block_hash, // 32 bytes if not null | ||
| uint64_t timestamp, | ||
| bool update_state, | ||
| FFITransactionCheckResult* result_out, | ||
| FFIError* error | ||
| ); | ||
|
|
||
| extern void wallet_free(void* wallet); | ||
|
|
||
| int main() { | ||
| // Example mnemonic (DO NOT USE IN PRODUCTION) | ||
| const char* mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; | ||
|
|
||
| FFIError error = {0}; | ||
| FFINetwork network = Testnet; | ||
|
|
||
| // Create wallet | ||
| void* wallet = wallet_create_from_mnemonic(mnemonic, NULL, network, &error); | ||
| if (!wallet) { | ||
| printf("Failed to create wallet: %s\n", error.message); | ||
| return 1; | ||
| } | ||
|
|
||
| printf("Wallet created successfully\n"); | ||
|
|
||
| // Example transaction bytes (this would be a real transaction in practice) | ||
| uint8_t tx_bytes[] = { /* ... transaction data ... */ }; | ||
| size_t tx_len = sizeof(tx_bytes); | ||
|
|
||
| // Check if transaction belongs to wallet | ||
| FFITransactionCheckResult result = {0}; | ||
| bool success = wallet_check_transaction( | ||
| wallet, | ||
| network, | ||
| tx_bytes, | ||
| tx_len, | ||
| Mempool, // Transaction is in mempool | ||
| 0, // No block height for mempool tx | ||
| NULL, // No block hash for mempool tx | ||
| 0, // No timestamp | ||
| false, // Don't update wallet state | ||
| &result, | ||
| &error | ||
| ); | ||
|
|
||
| if (success) { | ||
| if (result.is_relevant) { | ||
| printf("Transaction belongs to wallet!\n"); | ||
| printf(" Total received: %llu\n", (unsigned long long)result.total_received); | ||
| printf(" Total sent: %llu\n", (unsigned long long)result.total_sent); | ||
| printf(" Affected accounts: %u\n", result.affected_accounts_count); | ||
| } else { | ||
| printf("Transaction does not belong to wallet\n"); | ||
| } | ||
| } else { | ||
| printf("Failed to check transaction: %s\n", error.message); | ||
| } | ||
|
|
||
|
Comment on lines
+94
to
+106
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. 💡 Verification agent 🧩 Analysis chainFree error message buffers between calls to prevent leaks FFIError.message is allocated on the Rust side (CString::into_raw). The example prints messages but never frees them, and also reuses the same FFIError across calls. This leaks memory and may overwrite pointers. Search for the correct deallocation routine provided by the FFI (e.g., ffi_free_string, ffi_error_free, error_free_message): Expected outcome:
Also guard against NULL before printing:
Also applies to: 62-70, 109-121 🏁 Script executed: #!/bin/bash
# Find available error/message free helpers
rg -nP --type=rs -C2 'extern\s+"C"\s+fn\s+.*free|extern\s+"C"\s+fn\s+.*error.*free|free_string|string_free|error_free' key-wallet-ffi/src
rg -nP --type=c -C2 'void\s+\w*free\w*\(' key-wallet-ffi/includeLength of output: 3541 🏁 Script executed: #!/bin/bash
# Locate the FFIError struct to confirm the message field name and type
rg -nP --type=c 'struct\s+FFIError' key-wallet-ffi/include/key_wallet_ffi.h -C3Length of output: 243 Free error messages after printing to prevent leaks The • File: key-wallet-ffi/examples/check_transaction.c - } else {
- printf("Failed to check transaction: %s\n", error.message);
- }
+ } else {
+ if (error.message) {
+ printf("Failed to check transaction: %s\n", error.message);
+ error_message_free(error.message);
+ error.message = NULL;
+ } else {
+ printf("Failed to check transaction: error code %d\n", error.code);
+ }
+ }• Ensure each example that prints 🤖 Prompt for AI Agents |
||
| // Check a confirmed transaction | ||
| uint8_t block_hash[32] = { /* ... block hash ... */ }; | ||
| success = wallet_check_transaction( | ||
| wallet, | ||
| network, | ||
| tx_bytes, | ||
| tx_len, | ||
| InBlock, // Transaction is in a block | ||
| 650000, // Block height | ||
| block_hash, // Block hash | ||
| 1234567890, // Unix timestamp | ||
| true, // Update wallet state | ||
| &result, | ||
| &error | ||
| ); | ||
|
|
||
| if (success && result.is_relevant) { | ||
| printf("Confirmed transaction processed and wallet state updated\n"); | ||
| } | ||
|
|
||
| // Clean up | ||
| wallet_free(wallet); | ||
|
|
||
| return 0; | ||
| } | ||
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.
💡 Verification agent
🧩 Analysis chain
Dependency switch aligns with UniFFI removal
Switching to
key-wallet-manager,dashcore, anddash-networkpath deps is consistent with the new C-ABI FFI approach. Make sure there are no lingering UniFFI artifacts.Run this to verify no
uniffireferences remain:🏁 Script executed:
Length of output: 13368
Lingering UniFFI artifacts detected – please remove across project
A repo-wide search still shows numerous UniFFI references and generated bindings. To fully complete the switch to the new C-ABI FFI approach, please remove or replace all of the following:
• key-wallet-ffi/build-ios.sh
– remove
--features uniffi/cliand uniffi-bindgen commands• key-wallet-ffi/README.md
– drop “UniFFI bindings” sections and related generate commands
• dash-network-ffi/
– build.rs (
uniffi::generate_scaffolding)– README.md (UniFFI-based features and examples)
– src/lib.rs (
uniffi::include_scaffolding!)– uniffi-bindgen.rs binary
– Cargo.toml (uniffi dependencies)
– autogenerated .h/.swift FFI files
• swift-dash-core-sdk/INTEGRATION_NOTES.md
– references to key-wallet-ffi via UniFFI
• swift-dash-core-sdk/Sources/KeyWalletFFI/*
– autogenerated FFI headers and Swift files containing “UniFFI”
• CLAUDE.md
– mentions of
dash-network-ffi/using UniFFIMake sure all build scripts, docs, generated code, and Cargo.toml entries no longer reference UniFFI or uniffi-bindgen.
🤖 Prompt for AI Agents