Skip to content

Commit 618213d

Browse files
Merge pull request #175 from dashpay/feat/wallet-ffi-transaction-builder
feat: add comprehensive transaction building to wallet FFI
2 parents 5eb9bbd + 65b66e8 commit 618213d

File tree

3 files changed

+453
-11
lines changed

3 files changed

+453
-11
lines changed

key-wallet-ffi/FFI_API.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This document provides a comprehensive reference for all FFI (Foreign Function I
44

55
**Auto-generated**: This documentation is automatically generated from the source code. Do not edit manually.
66

7-
**Total Functions**: 236
7+
**Total Functions**: 238
88

99
## Table of Contents
1010

@@ -68,7 +68,7 @@ Functions: 19
6868

6969
### Wallet Operations
7070

71-
Functions: 57
71+
Functions: 58
7272

7373
| Function | Description | Module |
7474
|----------|-------------|--------|
@@ -100,7 +100,8 @@ Functions: 57
100100
| `wallet_add_account` | Add an account to the wallet without xpub # Safety This function dereferenc... | wallet |
101101
| `wallet_add_account_with_string_xpub` | Add an account to the wallet with xpub as string # Safety This function der... | wallet |
102102
| `wallet_add_account_with_xpub_bytes` | Add an account to the wallet with xpub as byte array # Safety This function... | wallet |
103-
| `wallet_build_transaction` | Build a transaction # Safety - `wallet` must be a valid pointer to an FFIWa... | transaction |
103+
| `wallet_build_and_sign_transaction` | Build and sign a transaction using the wallet's managed info This is the rec... | transaction |
104+
| `wallet_build_transaction` | Build a transaction (unsigned) This creates an unsigned transaction | transaction |
104105
| `wallet_check_transaction` | Check if a transaction belongs to the wallet using ManagedWalletInfo # Safet... | transaction |
105106
| `wallet_create_from_mnemonic` | Create a new wallet from mnemonic (backward compatibility - single network) ... | wallet |
106107
| `wallet_create_from_mnemonic_with_options` | Create a new wallet from mnemonic with options # Safety - `mnemonic` must b... | wallet |
@@ -250,7 +251,7 @@ Functions: 10
250251

251252
### Transaction Management
252253

253-
Functions: 13
254+
Functions: 14
254255

255256
| Function | Description | Module |
256257
|----------|-------------|--------|
@@ -263,6 +264,7 @@ Functions: 13
263264
| `transaction_deserialize` | Deserialize a transaction # Safety - `data` must be a valid pointer to seria... | transaction |
264265
| `transaction_destroy` | Destroy a transaction # Safety - `tx` must be a valid pointer to an FFITrans... | transaction |
265266
| `transaction_get_txid` | Get the transaction ID # Safety - `tx` must be a valid pointer to an FFITran... | transaction |
267+
| `transaction_get_txid_from_bytes` | Get transaction ID from raw transaction bytes # Safety - `tx_bytes` must be ... | transaction |
266268
| `transaction_serialize` | Serialize a transaction # Safety - `tx` must be a valid pointer to an FFITra... | transaction |
267269
| `transaction_sighash` | Calculate signature hash for an input # Safety - `tx` must be a valid pointe... | transaction |
268270
| `transaction_sign_input` | Sign a transaction input # Safety - `tx` must be a valid pointer to an FFITr... | transaction |
@@ -1130,14 +1132,30 @@ This function dereferences raw pointers. The caller must ensure that: - The wall
11301132

11311133
---
11321134

1135+
#### `wallet_build_and_sign_transaction`
1136+
1137+
```c
1138+
wallet_build_and_sign_transaction(managed_wallet: *mut FFIManagedWalletInfo, wallet: *const FFIWallet, network: FFINetwork, account_index: c_uint, outputs: *const FFITxOutput, outputs_count: usize, fee_per_kb: u64, current_height: u32, tx_bytes_out: *mut *mut u8, tx_len_out: *mut usize, error: *mut FFIError,) -> bool
1139+
```
1140+
1141+
**Description:**
1142+
Build and sign a transaction using the wallet's managed info This is the recommended way to build transactions. It handles: - UTXO selection using coin selection algorithms - Fee calculation - Change address generation - Transaction signing # Safety - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `wallet` must be a valid pointer to an FFIWallet - `network` must be a valid FFINetwork - `outputs` must be a valid pointer to an array of FFITxOutput with at least `outputs_count` elements - `tx_bytes_out` must be a valid pointer to store the transaction bytes pointer - `tx_len_out` must be a valid pointer to store the transaction length - `error` must be a valid pointer to an FFIError - The returned transaction bytes must be freed with `transaction_bytes_free`
1143+
1144+
**Safety:**
1145+
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `wallet` must be a valid pointer to an FFIWallet - `network` must be a valid FFINetwork - `outputs` must be a valid pointer to an array of FFITxOutput with at least `outputs_count` elements - `tx_bytes_out` must be a valid pointer to store the transaction bytes pointer - `tx_len_out` must be a valid pointer to store the transaction length - `error` must be a valid pointer to an FFIError - The returned transaction bytes must be freed with `transaction_bytes_free`
1146+
1147+
**Module:** `transaction`
1148+
1149+
---
1150+
11331151
#### `wallet_build_transaction`
11341152

11351153
```c
11361154
wallet_build_transaction(wallet: *mut FFIWallet, _network: FFINetworks, account_index: c_uint, outputs: *const FFITxOutput, outputs_count: usize, fee_per_kb: u64, tx_bytes_out: *mut *mut u8, tx_len_out: *mut usize, error: *mut FFIError,) -> bool
11371155
```
11381156

11391157
**Description:**
1140-
Build a transaction # Safety - `wallet` must be a valid pointer to an FFIWallet - `outputs` must be a valid pointer to an array of FFITxOutput with at least `outputs_count` elements - `tx_bytes_out` must be a valid pointer to store the transaction bytes pointer - `tx_len_out` must be a valid pointer to store the transaction length - `error` must be a valid pointer to an FFIError - The returned transaction bytes must be freed with `transaction_bytes_free`
1158+
Build a transaction (unsigned) This creates an unsigned transaction. Use wallet_sign_transaction to sign it afterward. For a combined build+sign operation, use wallet_build_and_sign_transaction. # Safety - `wallet` must be a valid pointer to an FFIWallet - `outputs` must be a valid pointer to an array of FFITxOutput with at least `outputs_count` elements - `tx_bytes_out` must be a valid pointer to store the transaction bytes pointer - `tx_len_out` must be a valid pointer to store the transaction length - `error` must be a valid pointer to an FFIError - The returned transaction bytes must be freed with `transaction_bytes_free`
11411159

11421160
**Safety:**
11431161
- `wallet` must be a valid pointer to an FFIWallet - `outputs` must be a valid pointer to an array of FFITxOutput with at least `outputs_count` elements - `tx_bytes_out` must be a valid pointer to store the transaction bytes pointer - `tx_len_out` must be a valid pointer to store the transaction length - `error` must be a valid pointer to an FFIError - The returned transaction bytes must be freed with `transaction_bytes_free`
@@ -3318,6 +3336,22 @@ Get the transaction ID # Safety - `tx` must be a valid pointer to an FFITransac
33183336

33193337
---
33203338

3339+
#### `transaction_get_txid_from_bytes`
3340+
3341+
```c
3342+
transaction_get_txid_from_bytes(tx_bytes: *const u8, tx_len: usize, error: *mut FFIError,) -> *mut c_char
3343+
```
3344+
3345+
**Description:**
3346+
Get transaction ID from raw transaction bytes # Safety - `tx_bytes` must be a valid pointer to transaction bytes - `tx_len` must be the correct length of the transaction - `error` must be a valid pointer to an FFIError # Returns - Pointer to null-terminated hex string of TXID (must be freed with string_free) - NULL on error
3347+
3348+
**Safety:**
3349+
- `tx_bytes` must be a valid pointer to transaction bytes - `tx_len` must be the correct length of the transaction - `error` must be a valid pointer to an FFIError
3350+
3351+
**Module:** `transaction`
3352+
3353+
---
3354+
33213355
#### `transaction_serialize`
33223356

33233357
```c

key-wallet-ffi/include/key_wallet_ffi.h

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3182,7 +3182,10 @@ bool mnemonic_to_seed(const char *mnemonic,
31823182
void mnemonic_free(char *mnemonic) ;
31833183

31843184
/*
3185-
Build a transaction
3185+
Build a transaction (unsigned)
3186+
3187+
This creates an unsigned transaction. Use wallet_sign_transaction to sign it afterward.
3188+
For a combined build+sign operation, use wallet_build_and_sign_transaction.
31863189
31873190
# Safety
31883191
@@ -3227,6 +3230,40 @@ bool wallet_sign_transaction(const FFIWallet *wallet,
32273230
FFIError *error)
32283231
;
32293232

3233+
/*
3234+
Build and sign a transaction using the wallet's managed info
3235+
3236+
This is the recommended way to build transactions. It handles:
3237+
- UTXO selection using coin selection algorithms
3238+
- Fee calculation
3239+
- Change address generation
3240+
- Transaction signing
3241+
3242+
# Safety
3243+
3244+
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo
3245+
- `wallet` must be a valid pointer to an FFIWallet
3246+
- `network` must be a valid FFINetwork
3247+
- `outputs` must be a valid pointer to an array of FFITxOutput with at least `outputs_count` elements
3248+
- `tx_bytes_out` must be a valid pointer to store the transaction bytes pointer
3249+
- `tx_len_out` must be a valid pointer to store the transaction length
3250+
- `error` must be a valid pointer to an FFIError
3251+
- The returned transaction bytes must be freed with `transaction_bytes_free`
3252+
*/
3253+
3254+
bool wallet_build_and_sign_transaction(FFIManagedWalletInfo *managed_wallet,
3255+
const FFIWallet *wallet,
3256+
FFINetwork network,
3257+
unsigned int account_index,
3258+
const FFITxOutput *outputs,
3259+
size_t outputs_count,
3260+
uint64_t fee_per_kb,
3261+
uint32_t current_height,
3262+
uint8_t **tx_bytes_out,
3263+
size_t *tx_len_out,
3264+
FFIError *error)
3265+
;
3266+
32303267
/*
32313268
Check if a transaction belongs to the wallet using ManagedWalletInfo
32323269
@@ -3313,6 +3350,20 @@ bool wallet_check_transaction(FFIWallet *wallet,
33133350
*/
33143351
int32_t transaction_get_txid(const FFITransaction *tx, uint8_t *txid_out) ;
33153352

3353+
/*
3354+
Get transaction ID from raw transaction bytes
3355+
3356+
# Safety
3357+
- `tx_bytes` must be a valid pointer to transaction bytes
3358+
- `tx_len` must be the correct length of the transaction
3359+
- `error` must be a valid pointer to an FFIError
3360+
3361+
# Returns
3362+
- Pointer to null-terminated hex string of TXID (must be freed with string_free)
3363+
- NULL on error
3364+
*/
3365+
char *transaction_get_txid_from_bytes(const uint8_t *tx_bytes, size_t tx_len, FFIError *error) ;
3366+
33163367
/*
33173368
Serialize a transaction
33183369

0 commit comments

Comments
 (0)