Skip to content

Commit 3b035d8

Browse files
refactor: clean up key wallet some more (#121)
* clean up of key-wallet * more work * more work on clean up * fix * more work * more work * more work * small fix * fixes
1 parent 0100516 commit 3b035d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+10399
-2902
lines changed

dash-spv/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,9 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
228228
ManagedWalletInfo,
229229
>::new());
230230
spv_wallet.base.create_wallet_from_mnemonic(
231-
WalletId::default(),
232-
"Default".to_string(),
233231
"enemy check owner stumble unaware debris suffer peanut good fabric bleak outside",
234232
"",
235-
Some(network),
233+
&[network],
236234
None,
237235
key_wallet::wallet::initialization::WalletAccountCreationOptions::default(),
238236
)?;

key-wallet-ffi/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ name = "key_wallet_ffi"
1313
crate-type = ["cdylib", "staticlib", "lib"]
1414

1515
[features]
16-
default = []
16+
default = ["bincode", "eddsa", "bls"]
1717
bip38 = ["key-wallet/bip38"]
18+
bincode = ["key-wallet-manager/bincode", "key-wallet/bincode"]
19+
eddsa = ["dashcore/eddsa", "key-wallet/eddsa"]
20+
bls = ["dashcore/bls", "key-wallet/bls"]
1821

1922
[dependencies]
2023
key-wallet = { path = "../key-wallet", default-features = false, features = ["std"] }
2124
key-wallet-manager = { path = "../key-wallet-manager", features = ["std"] }
2225
dashcore = { path = "../dash", features = ["std"] }
2326
dash-network = { path = "../dash-network" }
2427
secp256k1 = { version = "0.30.0", features = ["global-context"] }
25-
thiserror = "2.0.12"
2628
libc = "0.2"
27-
sha2 = "0.10"
2829
hex = "0.4"
2930

3031
[build-dependencies]
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Wallet Import FFI Binding
2+
3+
## Overview
4+
5+
The `wallet_manager_import_wallet_from_bytes` FFI function allows importing a previously serialized wallet from bincode bytes into a wallet manager instance.
6+
7+
## Function Signature
8+
9+
```c
10+
bool wallet_manager_import_wallet_from_bytes(
11+
FFIWalletManager *manager,
12+
const uint8_t *wallet_bytes,
13+
size_t wallet_bytes_len,
14+
uint8_t *wallet_id_out,
15+
FFIError *error
16+
);
17+
```
18+
19+
## Parameters
20+
21+
- `manager`: Pointer to an FFIWalletManager instance
22+
- `wallet_bytes`: Pointer to bincode-serialized wallet bytes
23+
- `wallet_bytes_len`: Length of the wallet bytes
24+
- `wallet_id_out`: Pointer to a 32-byte buffer that will receive the wallet ID
25+
- `error`: Pointer to an FFIError structure for error reporting (can be NULL)
26+
27+
## Return Value
28+
29+
- `true`: Wallet imported successfully
30+
- `false`: Import failed (check error for details)
31+
32+
## Error Codes
33+
34+
The function may set the following error codes:
35+
36+
- `InvalidInput` (1): Null pointer or invalid length provided
37+
- `SerializationError` (9): Failed to deserialize wallet from bincode
38+
- `InvalidState` (11): Wallet already exists in the manager
39+
- `WalletError` (8): Other wallet-related errors
40+
41+
## Usage Example
42+
43+
```c
44+
#include "key_wallet_ffi.h"
45+
46+
// Load wallet bytes from file or network
47+
uint8_t *wallet_bytes = load_wallet_bytes();
48+
size_t bytes_len = get_wallet_bytes_length();
49+
50+
// Prepare output buffer for wallet ID
51+
uint8_t wallet_id[32];
52+
53+
// Import the wallet
54+
FFIError error = {0};
55+
bool success = wallet_manager_import_wallet_from_bytes(
56+
manager,
57+
wallet_bytes,
58+
bytes_len,
59+
wallet_id,
60+
&error
61+
);
62+
63+
if (success) {
64+
printf("Wallet imported with ID: ");
65+
for (int i = 0; i < 32; i++) {
66+
printf("%02x", wallet_id[i]);
67+
}
68+
printf("\n");
69+
} else {
70+
printf("Import failed: %s\n", error.message);
71+
if (error.message) {
72+
error_message_free(error.message);
73+
}
74+
}
75+
```
76+
77+
## Building with Bincode Support
78+
79+
To use this function, the FFI library must be built with the `bincode` feature enabled:
80+
81+
```bash
82+
cargo build --features bincode
83+
```
84+
85+
## Serialization Format
86+
87+
The wallet bytes must be in bincode format (version 2.0.0-rc.3). The serialization includes:
88+
- Wallet seed and key material
89+
- Account information
90+
- Address pools and indices
91+
- Transaction history
92+
- Other wallet metadata
93+
94+
## Safety Considerations
95+
96+
1. The `wallet_bytes` pointer must remain valid for the duration of the function call
97+
2. The `wallet_id_out` buffer must be at least 32 bytes
98+
3. Do not use the wallet_id_out buffer if the function returns false
99+
4. Always free error messages using `error_message_free()` when done
100+
5. The imported wallet must not already exist in the manager (will fail with InvalidState)
101+
102+
## Thread Safety
103+
104+
The wallet manager uses internal locking, so this function is thread-safe with respect to other wallet manager operations on the same instance.

key-wallet-ffi/cbindgen.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ item_types = ["functions", "enums", "structs", "typedefs", "opaque", "constants"
4242
"FFIExtendedPublicKey" = ""
4343
"FFIManagedWalletInfo" = ""
4444
"FFIWalletManager" = ""
45+
"FFIWallet" = ""
46+
"FFIManagedWallet" = ""
47+
"FFIAccount" = ""
48+
"FFIBLSAccount" = ""
49+
"FFIEdDSAAccount" = ""
50+
"FFIManagedAccount" = ""
51+
"FFIAccountCollection" = ""
52+
"FFIManagedAccountCollection" = ""
53+
"FFIAddressPool" = ""
4554

4655
[export.rename]
4756
# Rename types to match C conventions

0 commit comments

Comments
 (0)