Skip to content

Commit fed440f

Browse files
feat: add comprehensive transaction building to wallet FFI
This commit adds production-ready transaction building functionality to the key-wallet-ffi layer, enabling C/Swift clients to create and sign transactions. New FFI Functions: 1. wallet_build_and_sign_transaction() - Comprehensive transaction builder that: - Selects UTXOs using Branch and Bound coin selection algorithm - Calculates fees based on fee_per_kb rate - Generates change addresses from internal address pool - Derives private keys from root extended key for signing - Returns fully signed, serialized transaction bytes 2. transaction_get_txid_from_bytes() - Utility function that: - Deserializes raw transaction bytes - Returns hex-encoded TXID as C string Implementation Details: - Integrates with ManagedWalletInfo for UTXO tracking and address management - Uses TransactionBuilder pattern with proper fee calculation - Maps addresses to derivation paths for key derivation - Comprehensive error handling with FFIError propagation - Proper memory management following FFI safety patterns - Updated wallet_build_transaction() to direct users to new combined function 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent bba5e0c commit fed440f

File tree

2 files changed

+415
-6
lines changed

2 files changed

+415
-6
lines changed

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)