## Overview
The Wallet module provides comprehensive cryptocurrency wallet functionality for the Cellframe SDK, including key management, transaction creation, balance tracking, and secure storage operations. This module handles multiple wallet types, cryptocurrency operations, and integrates with various blockchain networks for complete wallet management.
*Based on: `dap_chain_wallet.h`, `dap_chain_wallet.c`, `dap_chain_wallet_cache.h`, `dap_chain_wallet_cache.c`, `dap_chain_wallet_internal.h`, `dap_chain_wallet_internal.c`*
## Document Structure
- [[#Overview|Overview]]
- [[#Module Structures|Module Structures]]
- [[#dap_chain_wallet_t|dap_chain_wallet_t - Wallet Core Structure]]
- [[#dap_chain_wallet_cache_iter_t|dap_chain_wallet_cache_iter_t - Cache Iterator]]
- [[#dap_chain_wallet_internal_t|dap_chain_wallet_internal_t - Internal Wallet Structure]]
- [[#dap_chain_wallet_file_t|dap_chain_wallet_file_t - Wallet File Structure]]
- [[#dap_chain_wallet_n_pass_t|dap_chain_wallet_n_pass_t - Wallet name and password]]
- [[#dap_chain_wallet_cert_hdr_t|dap_chain_wallet_cert_hdr_t - Certificate header]]
- [[#dap_chain_wallet_cert_t|dap_chain_wallet_cert_t - Certificate structure]]
- [[#dap_chain_wallet_file_hdr_t|dap_chain_wallet_file_hdr_t - File header]]
- [[#dap_chain_wallet_getting_type_t|dap_chain_wallet_getting_type_t - Cache Getting Types]]
- [[#Module Functions|Module Functions]]
- [[#Core Wallet Functions|Core Wallet Functions]]
- [[#Wallet Cache Functions|Wallet Cache Functions]]
- [[#Wallet Operations Functions|Wallet Operations Functions]]
- [[#Wallet Shared Functions|Wallet Shared Functions]]
- [[#Error Codes|Error Codes]]
- [[#Typical Examples|Typical Examples]]
## Module Structures
### dap_chain_wallet_t
Core wallet structure containing wallet metadata and control information.
```c
typedef struct dap_chain_wallet {
char name[DAP_WALLET$SZ_NAME + 1]; // Human readable name of wallet
uint64_t flags; // Wallet flags (protected, active)
void *_internal; // Internal wallet data
void *_inheritor; // Inheritor-specific data
} dap_chain_wallet_t;
```
**Public Fields:**
- `name` - Human-readable wallet name (max 64 characters)
- `flags` - Wallet status flags (DAP_WALLET$M_FL_PROTECTED, DAP_WALLET$M_FL_ACTIVE)
**Wallet Flags:**
- `DAP_WALLET$M_FL_PROTECTED` - Wallet is password protected
- `DAP_WALLET$M_FL_ACTIVE` - Wallet has been activated with password
### dap_chain_wallet_cache_iter_t
Iterator structure for wallet transaction cache operations.
```c
typedef struct dap_chain_wallet_cache_iter {
dap_chain_datum_tx_t *cur_tx; // Current transaction
dap_chain_hash_fast_t *cur_hash; // Current transaction hash
dap_chain_hash_fast_t *cur_atom_hash; // Current atom hash
uint32_t action; // Transaction action
dap_chain_net_srv_uid_t uid; // Service UID
int ret_code; // Return code
char *token_ticker; // Token ticker
void *cur_item; // Current item pointer
void *cur_addr_cache; // Current address cache
} dap_chain_wallet_cache_iter_t;
```
**Public Fields:**
- `cur_tx` - Pointer to current transaction in iteration
- `cur_hash` - Hash of current transaction
- `action` - Transaction action type
- `token_ticker` - Token ticker for filtering
### dap_chain_wallet_internal_t
Internal wallet structure for file management and certificate storage.
```c
typedef struct dap_chain_wallet_internal {
char file_name[MAX_PATH]; // Wallet file path
size_t certs_count; // Number of certificates
dap_cert_t **certs; // Certificate array
} dap_chain_wallet_internal_t;
```
**Public Fields:**
- `file_name` - Full path to wallet file
- `certs_count` - Number of stored certificates
- `certs` - Array of certificate pointers
### dap_chain_wallet_file_t
On-disk wallet file structure with header and data sections.
```c
typedef struct dap_chain_wallet_file {
dap_chain_wallet_file_hdr_t header; // File header
uint8_t data[]; // Variable wallet data
} DAP_ALIGN_PACKED dap_chain_wallet_file_t;
```
**Public Fields:**
- `header` - Wallet file header with signature and version
- `data` - Variable-length wallet data section
### dap_chain_wallet_n_pass_t
Structure for storing wallet name and password information with expiration.
```c
typedef struct dap_chain_wallet_n_pass {
uint16_t name_len; // Length of wallet name string
char name[DAP_WALLET$SZ_NAME + 1]; // Wallet name string
uint16_t pass_len; // Length of password string
char pass[DAP_WALLET$SZ_PASS + 1]; // Password string
struct timespec exptm; // Expiration time for record
UT_hash_handle hh; // Hash table context
} dap_chain_wallet_n_pass_t;
```
**Fields:**
- `name_len` - Length of the wallet name string
- `name` - Wallet name (null-terminated)
- `pass_len` - Length of the password string
- `pass` - Password (null-terminated)
- `exptm` - Expiration timestamp for this record
- `hh` - Hash table handle for efficient lookup
### dap_chain_wallet_cert_hdr_t
Header structure for wallet certificate records.
```c
typedef struct dap_chain_wallet_cert_hdr {
uint32_t type; // Record type (CERT/MAGIC)
uint32_t cert_raw_size; // Certificate size in bytes
} DAP_ALIGN_PACKED dap_chain_wallet_cert_hdr_t;
```
**Fields:**
- `type` - Record type (DAP_WALLET$K_CERT or DAP_WALLET$K_MAGIC)
- `cert_raw_size` - Size of the certificate data in bytes
### dap_chain_wallet_cert_t
Complete certificate structure with header and raw certificate data.
```c
typedef struct dap_chain_wallet_cert {
dap_chain_wallet_cert_hdr_t header; // Certificate header
dap_cert_file_t cert_raw; // Raw certificate data
} DAP_ALIGN_PACKED dap_chain_wallet_cert_t;
```
**Fields:**
- `header` - Certificate header with type and size information
- `cert_raw` - Raw certificate file data
### dap_chain_wallet_file_hdr_t
File header structure for wallet files on disk.
```c
typedef struct dap_chain_wallet_file_hdr {
uint64_t signature; // File signature for validation
uint32_t version; // Wallet file format version
uint8_t type; // Encryption type (PLAIN/GOST89)
uint64_t padding; // Padding for alignment
uint16_t wallet_len; // Length of wallet name
char wallet_name[]; // Variable wallet name
} DAP_ALIGN_PACKED dap_chain_wallet_file_hdr_t;
```
**Fields:**
- `signature` - File signature (DAP_CHAIN_WALLETS_FILE_SIGNATURE)
- `version` - File format version (DAP_WALLET$K_VER_1/2)
- `type` - Encryption type (DAP_WALLET$K_TYPE_PLAIN/GOST89)
- `padding` - Alignment padding
- `wallet_len` - Length of the wallet name string
- `wallet_name` - Variable-length wallet name
### dap_chain_wallet_getting_type_t
Enumeration for wallet cache retrieval types.
```c
typedef enum dap_chain_wallet_getting_type {
DAP_CHAIN_WALLET_CACHE_GET_FIRST = 0, // Get first transaction
DAP_CHAIN_WALLET_CACHE_GET_LAST, // Get last transaction
DAP_CHAIN_WALLET_CACHE_GET_NEXT, // Get next transaction
DAP_CHAIN_WALLET_CACHE_GET_PREVIOUS, // Get previous transaction
} dap_chain_wallet_getting_type_t;
```
**Cache Navigation:**
- `GET_FIRST` → `GET_NEXT` - Forward iteration
- `GET_LAST` → `GET_PREVIOUS` - Backward iteration
## Module Functions
### Core Wallet Functions
#### `dap_chain_wallet_init()`
Initialize the wallet module subsystem.
```c
int dap_chain_wallet_init();
```
**Returns:**
- `0` - Success
- `negative value` - Error code
**Error Conditions:**
- Returns non-zero if initialization fails
**Description:** Initializes the wallet module, sets up internal structures, and prepares the wallet system for operation.
#### `dap_chain_wallet_create()`
Create a new wallet with specified parameters.
```c
dap_chain_wallet_t *dap_chain_wallet_create(const char *a_wallet_name,
const char *a_wallets_path,
dap_sign_type_t a_sig_type,
const char *a_pass);
```
**Parameters:**
- `a_wallet_name` (const char*) - Name of the wallet to create
- `a_wallets_path` (const char*) - Path where wallet files are stored
- `a_sig_type` (dap_sign_type_t) - Signature type for the wallet
- `a_pass` (const char*) - Password for wallet protection
**Returns:**
- `wallet pointer` - Successfully created wallet
- `NULL` - Creation failed
**Error Conditions:**
- Returns NULL if wallet name is invalid
- Returns NULL if path is inaccessible
- Returns NULL if signature type is unsupported
**Description:** Creates a new wallet with the specified name, signature type, and password protection.
#### `dap_chain_wallet_open()`
Open an existing wallet from the wallet directory.
```c
dap_chain_wallet_t *dap_chain_wallet_open(const char *a_wallet_name,
const char *a_wallets_path,
unsigned int *a_out_stat);
```
**Parameters:**
- `a_wallet_name` (const char*) - Name of wallet to open
- `a_wallets_path` (const char*) - Path to wallet directory
- `a_out_stat` (unsigned int*) - Output status code
**Returns:**
- `wallet pointer` - Successfully opened wallet
- `NULL` - Open failed
**Error Conditions:**
- Returns NULL if wallet file not found
- Returns NULL if wallet is corrupted
**Description:** Opens an existing wallet file and loads it into memory for use.
#### `dap_chain_wallet_get_balance()`
Get the balance of a specific token in the wallet.
```c
uint256_t dap_chain_wallet_get_balance(dap_chain_wallet_t *a_wallet,
dap_chain_net_id_t a_net_id,
const char *a_token_ticker);
```
**Parameters:**
- `a_wallet` (dap_chain_wallet_t*) - Wallet to check balance
- `a_net_id` (dap_chain_net_id_t) - Network ID
- `a_token_ticker` (const char*) - Token ticker to check balance
**Returns:**
- `uint256_t` - Current balance of specified token
- `0` - No balance or error
**Error Conditions:**
- Returns 0 if wallet is NULL
- Returns 0 if token not found
**Description:** Calculates and returns the current balance of a specified token in the wallet.
### Wallet Cache Functions
#### `dap_chain_wallet_cache_init()`
Initialize the wallet cache subsystem.
```c
int dap_chain_wallet_cache_init();
```
**Returns:**
- `0` - Success
- `negative value` - Error code
**Error Conditions:**
- Returns non-zero if cache initialization fails
**Description:** Initializes the wallet transaction cache system for performance optimization.
#### `dap_chain_wallet_cache_tx_find()`
Find transactions in the wallet cache by address and token.
```c
int dap_chain_wallet_cache_tx_find(dap_chain_addr_t *a_addr,
char *a_token,
dap_chain_datum_tx_t **a_tx,
dap_hash_fast_t *a_tx_hash_curr,
int *a_ret_code);
```
**Parameters:**
- `a_addr` (dap_chain_addr_t*) - Wallet address to search
- `a_token` (char*) - Token ticker for filtering
- `a_tx` (dap_chain_datum_tx_t**) - Output transaction pointer
- `a_tx_hash_curr` (dap_hash_fast_t*) - Current transaction hash
- `a_ret_code` (int*) - Output return code
**Returns:**
- `0` - Transaction found successfully
- `-100` - Wrong arguments
- `-101` - Address not found in cache
**Error Conditions:**
- Returns -100 if any parameter is NULL
- Returns -101 if address not cached
**Description:** Searches the wallet cache for transactions matching the specified address and token.
#### `dap_chain_wallet_cache_iter_create()`
Create iterator for wallet cache traversal.
```c
dap_chain_wallet_cache_iter_t *dap_chain_wallet_cache_iter_create(dap_chain_addr_t a_addr);
```
**Parameters:**
- `a_addr` (dap_chain_addr_t) - Address to create iterator for
**Returns:**
- `iterator pointer` - Successfully created iterator
- `NULL` - Creation failed
**Error Conditions:**
- Returns NULL if address is invalid
- Returns NULL if memory allocation fails
**Description:** Creates an iterator for traversing transactions in the wallet cache.
### Wallet Operations Functions
#### `dap_chain_wallet_op_tx_request()`
Request a transaction operation from the wallet.
```c
void dap_chain_wallet_op_tx_request(dap_chain_wallet_t *a_wallet,
uint32_t a_wallet_key_idx,
dap_chain_t *a_chain_source,
uint64_t a_value,
dap_chain_t *a_chain_tx_request,
dap_chain_addr_t a_destination);
```
**Parameters:**
- `a_wallet` (dap_chain_wallet_t*) - Sender's wallet
- `a_wallet_key_idx` (uint32_t) - Key index in wallet
- `a_chain_source` (dap_chain_t*) - Source chain for tokens
- `a_value` (uint64_t) - Value in daptoshi units
- `a_chain_tx_request` (dap_chain_t*) - Target chain for transaction
- `a_destination` (dap_chain_addr_t) - Destination address
**Returns:**
- No return value (void function)
**Error Conditions:**
- Function may log errors internally
**Description:** Initiates a transaction request using the specified wallet and parameters.
#### `dap_chain_wallet_shared_taking_tx_create()`
Create shared wallet transaction.
```c
dap_chain_datum_tx_t *dap_chain_wallet_shared_taking_tx_create(dap_chain_wallet_t *a_wallet,
uint32_t a_wallet_key_idx,
dap_chain_net_id_t a_net_id,
const char *a_token_ticker,
uint256_t a_value);
```
**Parameters:**
- `a_wallet` (`dap_chain_wallet_t *`) - Source wallet
- `a_wallet_key_idx` (`uint32_t`) - Wallet key index
- `a_net_id` (`dap_chain_net_id_t`) - Network identifier
- `a_token_ticker` (`const char *`) - Token ticker symbol
- `a_value` (`uint256_t`) - Transaction value
**Returns:**
- `dap_chain_datum_tx_t *` - Created transaction
- `NULL` - Transaction creation failed
**Description:** Creates a transaction for taking funds from a shared wallet.
#### `dap_chain_wallet_shared_deinit()`
Deinitialize shared wallet subsystem.
```c
void dap_chain_wallet_shared_deinit();
```
**Description:** Cleans up the shared wallet subsystem and frees all associated resources.
#### `dap_chain_wallet_shared_cli()`
Handle shared wallet CLI commands.
```c
int dap_chain_wallet_shared_cli(int a_argc, char **a_argv, void **a_str_reply);
```
**Parameters:**
- `a_argc` (`int`) - Argument count
- `a_argv` (`char **`) - Argument array
- `a_str_reply` (`void **`) - Reply string pointer
**Returns:**
- `0` - Command executed successfully
- `non-zero` - Command execution failed
**Description:** Processes shared wallet command-line interface commands.
#### `dap_chain_wallet_deinit()`
Deinitialize wallet subsystem.
```c
void dap_chain_wallet_deinit(void);
```
**Description:** Cleans up the wallet subsystem and frees all wallet resources.
#### `dap_chain_wallet_save()`
Save wallet to file.
```c
int dap_chain_wallet_save(dap_chain_wallet_t *a_wallet, const char *a_pass);
```
**Parameters:**
- `a_wallet` (`dap_chain_wallet_t *`) - Wallet to save
- `a_pass` (`const char *`) - Password for encryption
**Returns:**
- `0` - Save successful
- `-1` - Invalid parameters
- `-3` - Permission denied
**Description:** Saves wallet data to file with optional password encryption.
#### `dap_chain_wallet_close()`
Close wallet and free resources.
```c
void dap_chain_wallet_close(dap_chain_wallet_t *a_wallet);
```
**Parameters:**
- `a_wallet` (`dap_chain_wallet_t *`) - Wallet to close
**Description:** Closes wallet, frees memory, and releases associated resources.
#### `dap_chain_wallet_get_certs_number()`
Get number of certificates in wallet.
```c
size_t dap_chain_wallet_get_certs_number(dap_chain_wallet_t *a_wallet);
```
**Parameters:**
- `a_wallet` (`dap_chain_wallet_t *`) - Wallet to query
**Returns:**
- `size_t` - Number of certificates
**Description:** Returns the total number of certificates stored in the wallet.
#### `dap_chain_wallet_save_file()`
Save wallet to file without password.
```c
int dap_chain_wallet_save_file(dap_chain_wallet_t *a_wallet);
```
**Parameters:**
- `a_wallet` (`dap_chain_wallet_t *`) - Wallet to save
**Returns:**
- `0` - Save successful
- `non-zero` - Save failed
**Description:** Saves wallet data to file without password encryption.
#### `dap_chain_wallet_activate()`
Activate wallet for use.
```c
int dap_chain_wallet_activate(const char *a_name,
ssize_t a_name_len,
const char *a_path,
const char *a_pass,
ssize_t a_pass_len,
unsigned a_ttl);
```
**Parameters:**
- `a_name` (`const char *`) - Wallet name
- `a_name_len` (`ssize_t`) - Name length
- `a_path` (`const char *`) - Wallet path
- `a_pass` (`const char *`) - Password
- `a_pass_len` (`ssize_t`) - Password length
- `a_ttl` (`unsigned`) - Time to live
**Returns:**
- `0` - Activation successful
- `non-zero` - Activation failed
**Description:** Activates a wallet for use with specified parameters and TTL.
#### `dap_chain_wallet_deactivate()`
Deactivate wallet.
```c
int dap_chain_wallet_deactivate(const char *a_name, ssize_t a_name_len);
```
**Parameters:**
- `a_name` (`const char *`) - Wallet name
- `a_name_len` (`ssize_t`) - Name length
**Returns:**
- `0` - Deactivation successful
- `non-zero` - Deactivation failed
**Description:** Deactivates a wallet and removes it from active state.
#### `dap_chain_wallet_get_pkey_hash()`
Get wallet public key hash.
```c
int dap_chain_wallet_get_pkey_hash(dap_chain_wallet_t *a_wallet, dap_hash_fast_t *a_out_hash);
```
**Parameters:**
- `a_wallet` (`dap_chain_wallet_t *`) - Wallet to query
- `a_out_hash` (`dap_hash_fast_t *`) - Output hash buffer
**Returns:**
- `0` - Hash retrieved successfully
- `non-zero` - Hash retrieval failed
**Description:** Calculates and returns the hash of the wallet's public key.
#### `dap_chain_wallet_add_wallet_opened_notify()`
Add wallet opened notification callback.
```c
int dap_chain_wallet_add_wallet_opened_notify(dap_chain_wallet_opened_callback_t a_callback,
void *a_arg);
```
**Parameters:**
- `a_callback` (`dap_chain_wallet_opened_callback_t`) - Callback function
- `a_arg` (`void *`) - User data
**Returns:**
- `0` - Callback added successfully
- `non-zero` - Callback addition failed
**Description:** Registers a callback to be notified when wallets are opened.
#### `dap_chain_wallet_add_wallet_created_notify()`
Add wallet created notification callback.
```c
int dap_chain_wallet_add_wallet_created_notify(dap_chain_wallet_opened_callback_t a_callback,
void *a_arg);
```
**Parameters:**
- `a_callback` (`dap_chain_wallet_opened_callback_t`) - Callback function
- `a_arg` (`void *`) - User data
**Returns:**
- `0` - Callback added successfully
- `non-zero` - Callback addition failed
**Description:** Registers a callback to be notified when wallets are created.
#### `dap_chain_wallet_cache_deinit()`
Deinitialize wallet cache.
```c
int dap_chain_wallet_cache_deinit();
```
**Returns:**
- `0` - Deinitialization successful
- `non-zero` - Deinitialization failed
**Description:** Cleans up the wallet cache subsystem and frees all cache resources.
#### `dap_chain_wallet_cache_tx_find_in_history()`
Find transaction in wallet cache history.
```c
int dap_chain_wallet_cache_tx_find_in_history(dap_chain_addr_t *a_addr,
char **a_token,
int *a_ret_code,
dap_chain_tx_tag_action_type_t *a_action,
dap_chain_datum_tx_t **a_tx,
dap_hash_fast_t *a_tx_hash);
```
**Parameters:**
- `a_addr` (`dap_chain_addr_t *`) - Address to search
- `a_token` (`char **`) - Token ticker
- `a_ret_code` (`int *`) - Return code
- `a_action` (`dap_chain_tx_tag_action_type_t *`) - Action type
- `a_tx` (`dap_chain_datum_tx_t **`) - Output transaction
- `a_tx_hash` (`dap_hash_fast_t *`) - Output transaction hash
**Returns:**
- `0` - Transaction found
- `non-zero` - Transaction not found or error
**Description:** Searches for a transaction in the wallet cache history.
#### `dap_chain_wallet_cache_tx_find_outs_with_val()`
Find transaction outputs with value in wallet cache.
```c
int dap_chain_wallet_cache_tx_find_outs_with_val(dap_chain_net_t *a_net,
const char *a_token_ticker,
const dap_chain_addr_t *a_addr,
uint256_t a_value,
dap_chain_datum_tx_t **a_tx,
dap_hash_fast_t *a_tx_hash);
```
**Parameters:**
- `a_net` (`dap_chain_net_t *`) - Network context
- `a_token_ticker` (`const char *`) - Token ticker
- `a_addr` (`const dap_chain_addr_t *`) - Address to search
- `a_value` (`uint256_t`) - Required value
- `a_tx` (`dap_chain_datum_tx_t **`) - Output transaction
- `a_tx_hash` (`dap_hash_fast_t *`) - Output transaction hash
**Returns:**
- `0` - Transaction found
- `non-zero` - Transaction not found or error
**Description:** Finds transaction outputs with specific value in wallet cache.
#### `dap_chain_wallet_cache_tx_find_outs()`
Find transaction outputs in wallet cache.
```c
int dap_chain_wallet_cache_tx_find_outs(dap_chain_net_t *a_net,
const char *a_token_ticker,
const dap_chain_addr_t *a_addr,
dap_chain_datum_tx_t **a_tx,
dap_hash_fast_t *a_tx_hash);
```
**Parameters:**
- `a_net` (`dap_chain_net_t *`) - Network context
- `a_token_ticker` (`const char *`) - Token ticker
- `a_addr` (`const dap_chain_addr_t *`) - Address to search
- `a_tx` (`dap_chain_datum_tx_t **`) - Output transaction
- `a_tx_hash` (`dap_hash_fast_t *`) - Output transaction hash
**Returns:**
- `0` - Transaction found
- `non-zero` - Transaction not found or error
**Description:** Finds transaction outputs in wallet cache.
#### `dap_chain_wallet_cache_iter_delete()`
Delete wallet cache iterator.
```c
void dap_chain_wallet_cache_iter_delete(dap_chain_wallet_cache_iter_t *a_iter);
```
**Parameters:**
- `a_iter` (`dap_chain_wallet_cache_iter_t *`) - Iterator to delete
**Description:** Frees memory associated with wallet cache iterator.
### Wallet Shared Functions
#### `dap_chain_wallet_shared_init()`
Initialize the shared wallet subsystem.
```c
int dap_chain_wallet_shared_init();
```
**Returns:**
- `0` - Success
- `negative value` - Error code
**Error Conditions:**
- Returns non-zero if shared wallet initialization fails
**Description:** Initializes the shared wallet functionality for multi-user wallet scenarios.
## Error Codes
### Wallet Core Error Codes
```c
#define DAP_WALLET_ERROR_SUCCESS 0 // Operation successful
#define DAP_WALLET_ERROR_INVALID_PARAMS -1 // Invalid parameters
#define DAP_WALLET_ERROR_FILE_NOT_FOUND -2 // Wallet file not found
#define DAP_WALLET_ERROR_PERMISSION_DENIED -3 // Permission denied
#define DAP_WALLET_ERROR_CORRUPTED_FILE -4 // Wallet file corrupted
#define DAP_WALLET_ERROR_WRONG_PASSWORD -5 // Wrong password
#define DAP_WALLET_ERROR_ALREADY_EXISTS -6 // Wallet already exists
```
### Wallet Cache Error Codes
```c
#define DAP_WALLET_CACHE_ERROR_SUCCESS 0 // Cache operation successful
#define DAP_WALLET_CACHE_ERROR_WRONG_ARGS -100 // Wrong arguments
#define DAP_WALLET_CACHE_ERROR_ADDR_NOT_FOUND -101 // Address not found in cache
#define DAP_WALLET_CACHE_ERROR_NO_TRANSACTIONS -102 // No transactions found
#define DAP_WALLET_CACHE_ERROR_ITERATION_END -103 // End of iteration reached
```
## Typical Examples
### Wallet Creation and Basic Operations Example
```c
#include <dap_chain_wallet.h>
void wallet_creation_example() {
log_it_info("=== Wallet Creation and Basic Operations ===");
// Step 1: Initialize wallet module
int result = dap_chain_wallet_init();
if (result != 0) {
log_it_error("✗ Failed to initialize wallet module: %d", result);
return;
}
log_it_info("✓ Wallet module initialized successfully");
// Step 2: Create new wallet
const char *wallet_name = "my_test_wallet";
const char *wallet_path = "/tmp/wallets";
const char *password = "secure_password123";
dap_chain_wallet_t *wallet = dap_chain_wallet_create(
wallet_name, wallet_path, SIG_TYPE_DILITHIUM, password);
if (!wallet) {
log_it_error("✗ Failed to create wallet '%s'", wallet_name);
return;
}
log_it_info("✓ Wallet '%s' created successfully", wallet->name);
// Step 3: Check wallet status
if (wallet->flags & DAP_WALLET$M_FL_PROTECTED) {
log_it_info("✓ Wallet is password protected");
}
if (wallet->flags & DAP_WALLET$M_FL_ACTIVE) {
log_it_info("✓ Wallet is active and ready for use");
}
// Step 4: Get wallet address
dap_chain_addr_t *addr = dap_chain_wallet_get_addr(wallet, 0x0001);
if (addr) {
log_it_info("✓ Wallet address obtained successfully");
} else {
log_it_error("✗ Failed to get wallet address");
}
// Step 5: Save wallet
result = dap_chain_wallet_save(wallet, password);
switch (result) {
case 0:
log_it_info("✓ Wallet saved successfully");
break;
case -1:
log_it_error("✗ Failed to save wallet: Invalid parameters");
break;
case -3:
log_it_error("✗ Failed to save wallet: Permission denied");
break;
default:
log_it_error("✗ Failed to save wallet: Unknown error %d", result);
break;
}
// Step 6: Close wallet
dap_chain_wallet_close(wallet);
log_it_info("✓ Wallet closed");
log_it_info("Wallet creation example completed");
}
```
### Wallet Transaction Cache Example
```c
#include <dap_chain_wallet_cache.h>
void wallet_cache_example() {
log_it_info("=== Wallet Transaction Cache Example ===");
// Step 1: Initialize wallet cache
int result = dap_chain_wallet_cache_init();
if (result != 0) {
log_it_error("✗ Failed to initialize wallet cache: %d", result);
return;
}
log_it_info("✓ Wallet cache initialized successfully");
// Step 2: Create cache iterator
dap_chain_addr_t test_addr = {0}; // Sample address
dap_chain_wallet_cache_iter_t *iter = dap_chain_wallet_cache_iter_create(test_addr);
if (!iter) {
log_it_error("✗ Failed to create cache iterator");
return;
}
log_it_info("✓ Cache iterator created successfully");
// Step 3: Iterate through transactions
dap_chain_datum_tx_t *tx = dap_chain_wallet_cache_iter_get(
iter, DAP_CHAIN_WALLET_CACHE_GET_FIRST);
int tx_count = 0;
while (tx) {
tx_count++;
log_it_info("✓ Found transaction #%d", tx_count);
// Get next transaction
tx = dap_chain_wallet_cache_iter_get(iter, DAP_CHAIN_WALLET_CACHE_GET_NEXT);
}
log_it_info("✓ Total transactions found: %d", tx_count);
// Step 4: Find specific transaction
dap_chain_datum_tx_t *found_tx = NULL;
dap_hash_fast_t tx_hash = {0};
int ret_code = 0;
result = dap_chain_wallet_cache_tx_find(
&test_addr, "CELL", &found_tx, &tx_hash, &ret_code);
switch (result) {
case 0:
log_it_info("✓ Transaction found successfully");
break;
case -100:
log_it_error("✗ Wrong arguments provided");
break;
case -101:
log_it_error("✗ Address not found in cache");
break;
default:
log_it_error("✗ Unknown error: %d", result);
break;
}
// Step 5: Cleanup
dap_chain_wallet_cache_iter_delete(iter);
log_it_info("✓ Cache iterator deleted");
log_it_info("Wallet cache example completed");
}
```
### Wallet Balance and Operations Example
```c
#include <dap_chain_wallet.h>
void wallet_balance_example() {
log_it_info("=== Wallet Balance and Operations Example ===");
// Step 1: Open existing wallet
const char *wallet_name = "my_test_wallet";
const char *wallet_path = "/tmp/wallets";
unsigned int status = 0;
dap_chain_wallet_t *wallet = dap_chain_wallet_open(wallet_name, wallet_path, &status);
if (!wallet) {
log_it_error("✗ Failed to open wallet '%s', status: %u", wallet_name, status);
return;
}
log_it_info("✓ Wallet '%s' opened successfully", wallet->name);
// Step 2: Check wallet balance
dap_chain_net_id_t net_id = {.uint64 = 0x0001};
const char *token_ticker = "CELL";
uint256_t balance = dap_chain_wallet_get_balance(wallet, net_id, token_ticker);
if (!IS_ZERO_256(balance)) {
char *balance_str = dap_chain_balance_to_coins(balance);
log_it_info("✓ Current %s balance: %s", token_ticker, balance_str);
DAP_DELETE(balance_str);
} else {
log_it_info("✓ No %s balance found or balance is zero", token_ticker);
}
// Step 3: Get wallet key information
size_t cert_count = dap_chain_wallet_get_certs_number(wallet);
log_it_info("✓ Wallet contains %zu certificates", cert_count);
// Step 4: Get wallet public key
dap_pkey_t *pkey = dap_chain_wallet_get_pkey(wallet, 0);
if (pkey) {
log_it_info("✓ Retrieved wallet public key successfully");
} else {
log_it_error("✗ Failed to retrieve wallet public key");
}
// Step 5: Get wallet address
dap_chain_addr_t *addr = dap_chain_wallet_get_addr(wallet, net_id);
if (addr) {
char *addr_str = dap_chain_addr_to_str(addr);
log_it_info("✓ Wallet address: %s", addr_str);
DAP_DELETE(addr_str);
} else {
log_it_error("✗ Failed to get wallet address");
}
// Step 6: Check wallet signature
const char *sign_check = dap_chain_wallet_check_sign(wallet);
if (sign_check) {
log_it_info("✓ Wallet signature check: %s", sign_check);
} else {
log_it_error("✗ Wallet signature check failed");
}
// Step 7: Close wallet
dap_chain_wallet_close(wallet);
log_it_info("✓ Wallet closed successfully");
log_it_info("Wallet balance example completed");
}
```
---
*See also: [[Modules/Module Chain|Module Chain]], [[ETC/Development Guide|Development Guide]], [[ETC/Troubleshooting|Troubleshooting]]*