Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions dash-spv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,9 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
ManagedWalletInfo,
>::new());
spv_wallet.base.create_wallet_from_mnemonic(
WalletId::default(),
"Default".to_string(),
"enemy check owner stumble unaware debris suffer peanut good fabric bleak outside",
"",
Some(network),
&[network],
None,
key_wallet::wallet::initialization::WalletAccountCreationOptions::default(),
)?;
Expand Down
7 changes: 4 additions & 3 deletions key-wallet-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ name = "key_wallet_ffi"
crate-type = ["cdylib", "staticlib", "lib"]

[features]
default = []
default = ["bincode", "eddsa", "bls"]
bip38 = ["key-wallet/bip38"]
bincode = ["key-wallet-manager/bincode", "key-wallet/bincode"]
eddsa = ["dashcore/eddsa", "key-wallet/eddsa"]
bls = ["dashcore/bls", "key-wallet/bls"]

[dependencies]
key-wallet = { path = "../key-wallet", default-features = false, features = ["std"] }
key-wallet-manager = { path = "../key-wallet-manager", features = ["std"] }
dashcore = { path = "../dash", features = ["std"] }
dash-network = { path = "../dash-network" }
secp256k1 = { version = "0.30.0", features = ["global-context"] }
thiserror = "2.0.12"
libc = "0.2"
sha2 = "0.10"
hex = "0.4"

[build-dependencies]
Expand Down
104 changes: 104 additions & 0 deletions key-wallet-ffi/IMPORT_WALLET_FFI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Wallet Import FFI Binding

## Overview

The `wallet_manager_import_wallet_from_bytes` FFI function allows importing a previously serialized wallet from bincode bytes into a wallet manager instance.

## Function Signature

```c
bool wallet_manager_import_wallet_from_bytes(
FFIWalletManager *manager,
const uint8_t *wallet_bytes,
size_t wallet_bytes_len,
uint8_t *wallet_id_out,
FFIError *error
);
```

## Parameters

- `manager`: Pointer to an FFIWalletManager instance
- `wallet_bytes`: Pointer to bincode-serialized wallet bytes
- `wallet_bytes_len`: Length of the wallet bytes
- `wallet_id_out`: Pointer to a 32-byte buffer that will receive the wallet ID
- `error`: Pointer to an FFIError structure for error reporting (can be NULL)

## Return Value

- `true`: Wallet imported successfully
- `false`: Import failed (check error for details)

## Error Codes

The function may set the following error codes:

- `InvalidInput` (1): Null pointer or invalid length provided
- `SerializationError` (9): Failed to deserialize wallet from bincode
- `InvalidState` (11): Wallet already exists in the manager
- `WalletError` (8): Other wallet-related errors

## Usage Example

```c
#include "key_wallet_ffi.h"

// Load wallet bytes from file or network
uint8_t *wallet_bytes = load_wallet_bytes();
size_t bytes_len = get_wallet_bytes_length();

// Prepare output buffer for wallet ID
uint8_t wallet_id[32];

// Import the wallet
FFIError error = {0};
bool success = wallet_manager_import_wallet_from_bytes(
manager,
wallet_bytes,
bytes_len,
wallet_id,
&error
);

if (success) {
printf("Wallet imported with ID: ");
for (int i = 0; i < 32; i++) {
printf("%02x", wallet_id[i]);
}
printf("\n");
} else {
printf("Import failed: %s\n", error.message);
if (error.message) {
error_message_free(error.message);
}
}
```

## Building with Bincode Support

To use this function, the FFI library must be built with the `bincode` feature enabled:

```bash
cargo build --features bincode
```

## Serialization Format

The wallet bytes must be in bincode format (version 2.0.0-rc.3). The serialization includes:
- Wallet seed and key material
- Account information
- Address pools and indices
- Transaction history
- Other wallet metadata

## Safety Considerations

1. The `wallet_bytes` pointer must remain valid for the duration of the function call
2. The `wallet_id_out` buffer must be at least 32 bytes
3. Do not use the wallet_id_out buffer if the function returns false
4. Always free error messages using `error_message_free()` when done
5. The imported wallet must not already exist in the manager (will fail with InvalidState)

## Thread Safety

The wallet manager uses internal locking, so this function is thread-safe with respect to other wallet manager operations on the same instance.
9 changes: 9 additions & 0 deletions key-wallet-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ item_types = ["functions", "enums", "structs", "typedefs", "opaque", "constants"
"FFIExtendedPublicKey" = ""
"FFIManagedWalletInfo" = ""
"FFIWalletManager" = ""
"FFIWallet" = ""
"FFIManagedWallet" = ""
"FFIAccount" = ""
"FFIBLSAccount" = ""
"FFIEdDSAAccount" = ""
"FFIManagedAccount" = ""
"FFIAccountCollection" = ""
"FFIManagedAccountCollection" = ""
"FFIAddressPool" = ""

[export.rename]
# Rename types to match C conventions
Expand Down
Loading
Loading