Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions key-wallet-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ crate-type = ["cdylib", "staticlib", "lib"]

[features]
default = []
bip38 = ["key-wallet/bip38"]

[dependencies]
key-wallet = { path = "../key-wallet", default-features = false, features = ["std"] }
dash-network-ffi = { path = "../dash-network-ffi" }
key-wallet-manager = { path = "../key-wallet-manager", features = ["std"] }
dashcore = { path = "../dash", features = ["std"] }
dash-network = { path = "../dash-network" }
Comment on lines +21 to +23

Copy link
Copy Markdown
Contributor

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, and dash-network path deps is consistent with the new C-ABI FFI approach. Make sure there are no lingering UniFFI artifacts.

Run this to verify no uniffi references remain:


🏁 Script executed:

#!/bin/bash
# Search for lingering UniFFI references across the repo
rg -nP -C2 '\buniffi\b|UniFFI'

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/cli and 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 UniFFI

Make sure all build scripts, docs, generated code, and Cargo.toml entries no longer reference UniFFI or uniffi-bindgen.

🤖 Prompt for AI Agents
In key-wallet-ffi/Cargo.toml around lines 21-23 and across the repo, remove all
UniFFI/uniffi-bindgen references and generated artifacts: delete uniffi
dependencies and any features like uniffi/cli from Cargo.toml entries; remove
uniffi-bindgen.rs, uniffi::generate_scaffolding and uniffi::include_scaffolding!
usage in build.rs and lib.rs; eliminate generated .h/.swift FFI files and the
swift KeyWalletFFI sources; update/remove UniFFI sections and generate commands
in README.md and INTEGRATION_NOTES.md, strip uniffi flags from build-ios.sh, and
delete mentions in CLAUDE.md so no build scripts, docs, or manifests reference
UniFFI anymore.

secp256k1 = { version = "0.30.0", features = ["global-context"] }
uniffi = { version = "0.29.3", features = ["cli"] }
thiserror = "2.0.12"

[build-dependencies]
uniffi = { version = "0.29.3", features = ["build"] }
libc = "0.2"
sha2 = "0.10"
hex = "0.4"

[dev-dependencies]
uniffi = { version = "0.29.3", features = ["bindgen-tests"] }
tempfile = "3.0"
hex = "0.4"
19 changes: 17 additions & 2 deletions key-wallet-ffi/build.rs
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");
}
_ => {}
}
}
131 changes: 131 additions & 0 deletions key-wallet-ffi/examples/check_transaction.c
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
In key-wallet-ffi/examples/check_transaction.c around lines 8 to 33 (and also
applicable to lines 3-7), the example incorrectly redefines FFI enums and
structs; remove these duplicated FFI type definitions and instead add an
#include for the public installed header that declares FFINetwork,
FFITransactionContext, FFITransactionCheckResult and FFIError; ensure the
example uses the header's types only (no local typedefs), update any include
path or build instruction so the compiler picks up the installed header, and
delete the redundant definitions to avoid ABI drift.

// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Free 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:

  • A function to free char* messages (e.g., ffi_free_string(char*)).
  • If present, call it after printing any error message and before reusing the FFIError struct.

Also guard against NULL before printing:

  • Only print error.message if not NULL; otherwise, print the error code.

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/include

Length 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 -C3

Length of output: 243


Free error messages after printing to prevent leaks

The FFIError.message buffer must be released with error_message_free (or string_free) once you’ve printed it—and reset the pointer—otherwise every failing call leaks. Also guard against NULL before printing. Apply the same pattern in all example blocks (lines 62–70, 94–106, 109–121).

• File: key-wallet-ffi/examples/check_transaction.c
• Lines 94–106 (and similarly 62–70, 109–121):

-    } 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 error.message frees it immediately after use.
• Always check error.message != NULL before calling printf("%s").

🤖 Prompt for AI Agents
In key-wallet-ffi/examples/check_transaction.c around lines 62-70, 94-106, and
109-121, the code prints error.message without checking for NULL and without
freeing the allocated buffer, which leaks memory on failures; update each
error-handling block to (1) check error.message != NULL before passing it to
printf, (2) after printing call the appropriate free function
(error_message_free or string_free) to release the buffer, and (3) set
error.message = NULL after freeing to avoid dangling pointers.

// 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;
}
Loading