Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions dash-spv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ 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),
Expand Down
2 changes: 2 additions & 0 deletions key-wallet-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["cdylib", "staticlib", "lib"]
[features]
default = []
bip38 = ["key-wallet/bip38"]
bincode = ["key-wallet-manager/bincode", "key-wallet/bincode"]

[dependencies]
key-wallet = { path = "../key-wallet", default-features = false, features = ["std"] }
Expand All @@ -31,5 +32,6 @@ hex = "0.4"
cbindgen = "0.29"

[dev-dependencies]
key-wallet-ffi = { path = ".", features = ["bincode"] }
tempfile = "3.0"
hex = "0.4"
1 change: 1 addition & 0 deletions key-wallet-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ item_types = ["functions", "enums", "structs", "typedefs", "opaque", "constants"
"FFIExtendedPublicKey" = ""
"FFIManagedWalletInfo" = ""
"FFIWalletManager" = ""
"ManagedWalletInfo" = ""

[export.rename]
# Rename types to match C conventions
Expand Down
209 changes: 95 additions & 114 deletions key-wallet-ffi/include/key_wallet_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,15 @@ typedef enum {
} FFILanguage;

/*
FFI Network type
FFI Network type (bit flags for multiple networks)
*/
typedef enum {
DASH = 0,
TESTNET = 1,
REGTEST = 2,
DEVNET = 3,
NO_NETWORKS = 0,
DASH = 1,
TESTNET = 2,
REGTEST = 4,
DEVNET = 8,
ALL_NETWORKS = 15,
} FFINetwork;

/*
Expand Down Expand Up @@ -351,16 +353,6 @@ typedef struct {
int32_t highest_used_index;
} FFIAddressPoolInfo;

/*
Balance structure for FFI
*/
typedef struct {
uint64_t confirmed;
uint64_t unconfirmed;
uint64_t immature;
uint64_t total;
} FFIBalance;

/*
Provider key info
*/
Expand Down Expand Up @@ -524,7 +516,7 @@ extern "C" {
FFIAccountResult wallet_get_account(const FFIWallet *wallet,
FFINetwork network,
unsigned int account_index,
unsigned int account_type)
FFIAccountType account_type)
;

/*
Expand Down Expand Up @@ -641,9 +633,8 @@ unsigned int wallet_get_account_count(const FFIWallet *wallet,

bool managed_wallet_get_address_pool_info(const FFIManagedWallet *managed_wallet,
FFINetwork network,
unsigned int account_type,
FFIAccountType account_type,
unsigned int account_index,
unsigned int registration_index,
FFIAddressPoolType pool_type,
FFIAddressPoolInfo *info_out,
FFIError *error)
Expand All @@ -663,9 +654,8 @@ bool managed_wallet_get_address_pool_info(const FFIManagedWallet *managed_wallet

bool managed_wallet_set_gap_limit(FFIManagedWallet *managed_wallet,
FFINetwork network,
unsigned int account_type,
FFIAccountType account_type,
unsigned int account_index,
unsigned int registration_index,
FFIAddressPoolType pool_type,
unsigned int gap_limit,
FFIError *error)
Expand All @@ -688,9 +678,8 @@ bool managed_wallet_set_gap_limit(FFIManagedWallet *managed_wallet,
bool managed_wallet_generate_addresses_to_index(FFIManagedWallet *managed_wallet,
const FFIWallet *wallet,
FFINetwork network,
unsigned int account_type,
FFIAccountType account_type,
unsigned int account_index,
unsigned int registration_index,
FFIAddressPoolType pool_type,
unsigned int target_index,
FFIError *error)
Expand All @@ -715,41 +704,6 @@ bool managed_wallet_mark_address_used(FFIManagedWallet *managed_wallet,
FFIError *error)
;

/*
Get wallet balance

# Safety

- `wallet` must be a valid pointer to an FFIWallet instance
- `balance_out` must be a valid pointer to an FFIBalance structure
- `error` must be a valid pointer to an FFIError structure or null
- The caller must ensure all pointers remain valid for the duration of this call
*/

bool wallet_get_balance(const FFIWallet *wallet,
FFINetwork network,
FFIBalance *balance_out,
FFIError *error)
;

/*
Get account balance

# Safety

- `wallet` must be a valid pointer to an FFIWallet instance
- `balance_out` must be a valid pointer to an FFIBalance structure
- `error` must be a valid pointer to an FFIError structure or null
- The caller must ensure all pointers remain valid for the duration of this call
*/

bool wallet_get_account_balance(const FFIWallet *wallet,
FFINetwork network,
unsigned int account_index,
FFIBalance *balance_out,
FFIError *error)
;

/*
Create a new master extended private key from seed

Expand Down Expand Up @@ -1502,26 +1456,6 @@ bool wallet_generate_provider_key(const FFIWallet *wallet,
*/
void provider_key_info_free(FFIProviderKeyInfo *info) ;

/*
Get the address for a provider key

This returns the P2PKH address corresponding to the provider key at
the specified index. This is useful for funding provider accounts.

# Safety

- `wallet` must be a valid pointer to an FFIWallet
- `error` must be a valid pointer to an FFIError or null
- The returned string must be freed by the caller
*/

char *wallet_get_provider_key_address(const FFIWallet *wallet,
FFINetwork network,
FFIProviderKeyType key_type,
unsigned int _key_index,
FFIError *error)
;

/*
Sign data with a provider key

Expand Down Expand Up @@ -1564,7 +1498,7 @@ bool wallet_sign_with_provider_key(const FFIWallet *wallet,
*/

bool wallet_build_transaction(FFIWallet *wallet,
FFINetwork network,
FFINetwork _network,
unsigned int account_index,
const FFITxOutput *outputs,
size_t outputs_count,
Expand All @@ -1588,7 +1522,7 @@ bool wallet_build_transaction(FFIWallet *wallet,
*/

bool wallet_sign_transaction(const FFIWallet *wallet,
FFINetwork network,
FFINetwork _network,
const uint8_t *tx_bytes,
size_t tx_len,
uint8_t **signed_tx_out,
Expand Down Expand Up @@ -1785,13 +1719,13 @@ bool wallet_get_utxos(const FFIWallet *_wallet,

FFIWallet *wallet_create_from_mnemonic_with_options(const char *mnemonic,
const char *passphrase,
FFINetwork network,
FFINetwork networks,
const FFIWalletAccountCreationOptions *account_options,
FFIError *error)
;

/*
Create a new wallet from mnemonic (backward compatibility)
Create a new wallet from mnemonic (backward compatibility - single network)

# Safety

Expand Down Expand Up @@ -1821,7 +1755,7 @@ FFIWallet *wallet_create_from_mnemonic(const char *mnemonic,

FFIWallet *wallet_create_from_seed_with_options(const uint8_t *seed,
size_t seed_len,
FFINetwork network,
FFINetwork networks,
const FFIWalletAccountCreationOptions *account_options,
FFIError *error)
;
Expand All @@ -1842,34 +1776,6 @@ FFIWallet *wallet_create_from_seed(const uint8_t *seed,
FFIError *error)
;

/*
Create a new wallet from seed bytes

# Safety

- `seed_bytes` must be a valid pointer to a byte array of `seed_len` length
- `error` must be a valid pointer to an FFIError structure or null
- The caller must ensure all pointers remain valid for the duration of this call
- The returned pointer must be freed with `wallet_free` when no longer needed
*/

FFIWallet *wallet_create_from_seed_bytes(const uint8_t *seed_bytes,
size_t seed_len,
FFINetwork network,
FFIError *error)
;

/*
Create a watch-only wallet from extended public key

# Safety

- `xpub` must be a valid pointer to a null-terminated C string
- `error` must be a valid pointer to an FFIError structure or null
- The caller must ensure all pointers remain valid for the duration of this call
*/
FFIWallet *wallet_create_from_xpub(const char *xpub, FFINetwork network, FFIError *error) ;

/*
Create a new random wallet with options

Expand All @@ -1880,7 +1786,7 @@ FFIWallet *wallet_create_from_seed_bytes(const uint8_t *seed_bytes,
- The caller must ensure all pointers remain valid for the duration of this call
*/

FFIWallet *wallet_create_random_with_options(FFINetwork network,
FFIWallet *wallet_create_random_with_options(FFINetwork networks,
const FFIWalletAccountCreationOptions *account_options,
FFIError *error)
;
Expand Down Expand Up @@ -1986,7 +1892,7 @@ char *wallet_get_xpub(const FFIWallet *wallet,

FFIAccountResult wallet_add_account(FFIWallet *wallet,
FFINetwork network,
unsigned int account_type,
FFIAccountType account_type,
unsigned int account_index)
;

Expand All @@ -2004,7 +1910,7 @@ FFIAccountResult wallet_add_account(FFIWallet *wallet,

FFIAccountResult wallet_add_account_with_xpub_bytes(FFIWallet *wallet,
FFINetwork network,
unsigned int account_type,
FFIAccountType account_type,
unsigned int account_index,
const uint8_t *xpub_bytes,
size_t xpub_len)
Expand All @@ -2024,7 +1930,7 @@ FFIAccountResult wallet_add_account_with_xpub_bytes(FFIWallet *wallet,

FFIAccountResult wallet_add_account_with_string_xpub(FFIWallet *wallet,
FFINetwork network,
unsigned int account_type,
FFIAccountType account_type,
unsigned int account_index,
const char *xpub_string)
;
Expand Down Expand Up @@ -2074,6 +1980,81 @@ bool wallet_manager_add_wallet_from_mnemonic(FFIWalletManager *manager,
FFIError *error)
;

/*
Add a wallet from mnemonic to the manager and return serialized bytes

Creates a wallet from a mnemonic phrase, adds it to the manager, optionally downgrading it
to a pubkey-only wallet (watch-only or externally signable), and returns the serialized wallet bytes.

# Safety

- `manager` must be a valid pointer to an FFIWalletManager instance
- `mnemonic` must be a valid pointer to a null-terminated C string
- `passphrase` must be a valid pointer to a null-terminated C string or null
- `birth_height` is optional, pass 0 for default
- `account_options` must be a valid pointer to FFIWalletAccountCreationOptions or null
- `downgrade_to_pubkey_wallet` if true, creates a watch-only or externally signable wallet
- `allow_external_signing` if true AND downgrade_to_pubkey_wallet is true, creates an externally signable wallet
- `wallet_bytes_out` must be a valid pointer to a pointer that will receive the serialized bytes
- `wallet_bytes_len_out` must be a valid pointer that will receive the byte length
- `wallet_id_out` must be a valid pointer to a 32-byte array that will receive the wallet ID
- `error` must be a valid pointer to an FFIError structure or null
- The caller must ensure all pointers remain valid for the duration of this call
- The caller must free the returned wallet_bytes using wallet_manager_free_wallet_bytes()
*/

bool wallet_manager_add_wallet_from_mnemonic_return_serialized_bytes(FFIWalletManager *manager,
const char *mnemonic,
const char *passphrase,
FFINetwork network,
unsigned int birth_height,
const FFIWalletAccountCreationOptions *account_options,
bool downgrade_to_pubkey_wallet,
bool allow_external_signing,
uint8_t **wallet_bytes_out,
size_t *wallet_bytes_len_out,
uint8_t *wallet_id_out,
FFIError *error)
;

/*
Free wallet bytes buffer

# Safety

- `wallet_bytes` must be a valid pointer to a buffer allocated by wallet_manager_add_wallet_from_mnemonic_return_serialized_bytes
- `bytes_len` must match the original allocation size
- The pointer must not be used after calling this function
- This function must only be called once per buffer
*/

void wallet_manager_free_wallet_bytes(uint8_t *wallet_bytes,
size_t bytes_len)
;

/*
Import a wallet from bincode-serialized bytes

Deserializes a wallet from bytes and adds it to the manager.
Returns a 32-byte wallet ID on success.

# Safety

- `manager` must be a valid pointer to an FFIWalletManager instance
- `wallet_bytes` must be a valid pointer to bincode-serialized wallet bytes
- `wallet_bytes_len` must be the exact length of the wallet bytes
- `wallet_id_out` must be a valid pointer to a 32-byte array that will receive the wallet ID
- `error` must be a valid pointer to an FFIError structure or null
- The caller must ensure all pointers remain valid for the duration of this call
*/

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)
;

/*
Get wallet IDs

Expand Down
Loading
Loading