|
| 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. |
0 commit comments