bdk-ffi/cc/bdk_ffi_test.c
2021-09-28 17:03:25 -07:00

200 lines
8.3 KiB
C

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bdk_ffi.h"
int main (int argc, char const * const argv[])
{
// shared consts
char const *desc = "wpkh([bf988dd3/84'/1'/0']tpubDD7bHVspyCSvvU8qEycydF664NAX6EAPjJ77j9E614GU2zVdXgnZZo6JJjKbDT6fUn8owMN6TCP9rZMznsNEhJbpkEwp6fAyyoSqy3DH2Qj/0/*)";
char const *change = "wpkh([bf988dd3/84'/1'/0']tpubDD7bHVspyCSvvU8qEycydF664NAX6EAPjJ77j9E614GU2zVdXgnZZo6JJjKbDT6fUn8owMN6TCP9rZMznsNEhJbpkEwp6fAyyoSqy3DH2Qj/1/*)";
char const *net = "testnet";
char const *blocks = "ssl://electrum.blockstream.info:60002";
// test new wallet error
{
BlockchainConfig_t *bc_config = new_electrum_config(blocks, NULL, 5, 30, 100);
//DatabaseConfig_t *db_config = new_sled_config("/home/steve/.bdk", "test_wallet");
DatabaseConfig_t *db_config = new_memory_config();
// new wallet with bad descriptor
FfiResult_OpaqueWallet_ptr_t wallet_result = new_wallet_result("bad","bad",net,bc_config,db_config);
assert(wallet_result.err == FFI_ERROR_DESCRIPTOR);
assert(wallet_result.ok == NULL);
free_blockchain_config(bc_config);
free_database_config(db_config);
free_wallet_result(wallet_result);
}
// test new wallet
{
BlockchainConfig_t *bc_config = new_electrum_config(blocks, NULL, 5, 30, 100);
DatabaseConfig_t *db_config = new_memory_config();
// new wallet
FfiResult_OpaqueWallet_ptr_t wallet_result = new_wallet_result(desc,change,net,bc_config,db_config);
// printf("wallet_result.err = %d\n", wallet_result.err));
assert(wallet_result.err == FFI_ERROR_NONE);
assert(wallet_result.ok != NULL);
free_blockchain_config(bc_config);
free_database_config(db_config);
OpaqueWallet_t *wallet = wallet_result.ok;
// sync wallet
FfiResultVoid_t sync_result = sync_wallet(wallet);
assert(sync_result.err == FFI_ERROR_NONE);
free_void_result(sync_result);
// new address
FfiResult_char_ptr_t address1_result = new_address(wallet);
assert(address1_result.ok != NULL);
assert(address1_result.err == FFI_ERROR_NONE);
//printf("address1 = %s\n", address1_result.ok);
assert( 0 == strcmp(address1_result.ok,"tb1qh4ajvhz9nd76tqddnl99l89hx4dat33hrjauzw"));
free_string_result(address1_result);
FfiResult_char_ptr_t address2_result = new_address(wallet);
assert(address2_result.ok != NULL);
assert(address2_result.err == FFI_ERROR_NONE);
//printf("address2 = %s\n", address2_result.ok);
assert( 0 == strcmp(address2_result.ok,"tb1qr7pu0pech43hcjrc4pzxcen0qkslj7xk7s5w3m"));
free_string_result(address2_result);
// free_wallet
free_wallet_result(wallet_result);
// verify free_wallet after free_wallet fails (core dumped)
//// free_wallet_result(wallet_result);
// verify sync_wallet after free_wallet fails (core dumped)
//// FfiResultVoid_t sync_result2 = sync_wallet(wallet);
}
// test get unspent utxos
{
BlockchainConfig_t *bc_config = new_electrum_config(blocks, NULL, 5, 30, 100);
DatabaseConfig_t *db_config = new_memory_config();
// new wallet
FfiResult_OpaqueWallet_ptr_t wallet_result = new_wallet_result(desc,change,net,bc_config,db_config);
assert(wallet_result.err == FFI_ERROR_NONE);
assert(wallet_result.ok != NULL);
free_blockchain_config(bc_config);
free_database_config(db_config);
OpaqueWallet_t *wallet = wallet_result.ok;
// sync wallet
FfiResultVoid_t sync_result = sync_wallet(wallet);
assert(sync_result.err == FFI_ERROR_NONE);
free_void_result(sync_result);
// list unspent
FfiResult_Vec_LocalUtxo_t unspent_result = list_unspent(wallet);
assert(unspent_result.ok.len == 1);
assert(unspent_result.err == FFI_ERROR_NONE);
LocalUtxo_t * unspent_ptr = unspent_result.ok.ptr;
for (int i = 0; i < unspent_result.ok.len; i++) {
// printf("%d: outpoint.txid: %s\n", i, unspent_ptr[i].outpoint.txid);
assert(unspent_ptr[i].outpoint.txid != NULL);
// printf("%d: outpoint.vout: %d\n", i, unspent_ptr[i].outpoint.vout);
assert(unspent_ptr[i].outpoint.vout >= 0);
// printf("%d: txout.value: %ld\n", i, unspent_ptr[i].txout.value);
assert(unspent_ptr[i].txout.value > 0);
// printf("%d: txout.script_pubkey: %s\n", i, unspent_ptr[i].txout.script_pubkey);
assert(unspent_ptr[i].txout.script_pubkey != NULL);
// printf("%d: keychain: %d\n", i, unspent_ptr[i].keychain);
assert(unspent_ptr[i].keychain >= 0);
}
free_veclocalutxo_result(unspent_result);
free_wallet_result(wallet_result);
}
// test balance
{
BlockchainConfig_t *bc_config = new_electrum_config(blocks, NULL, 5, 30, 100);
DatabaseConfig_t *db_config = new_memory_config();
// new wallet
FfiResult_OpaqueWallet_ptr_t wallet_result = new_wallet_result(desc,change,net,bc_config,db_config);
assert(wallet_result.err == FFI_ERROR_NONE);
assert(wallet_result.ok != NULL);
free_blockchain_config(bc_config);
free_database_config(db_config);
OpaqueWallet_t *wallet = wallet_result.ok;
// sync wallet
FfiResultVoid_t sync_result = sync_wallet(wallet);
assert(sync_result.err == FFI_ERROR_NONE);
free_void_result(sync_result);
// get balance
FfiResult_uint64_t balance_result = balance(wallet);
//printf("balance.err = %d\n", (balance_result.err));
assert(balance_result.err == FFI_ERROR_NONE);
//printf("balance.ok = %ld\n", balance_result.ok);
assert(balance_result.ok > 0);
// free balance and wallet results
free_uint64_result(balance_result);
free_wallet_result(wallet_result);
}
// test get transaction details
{
BlockchainConfig_t *bc_config = new_electrum_config(blocks, NULL, 5, 30, 100);
DatabaseConfig_t *db_config = new_memory_config();
// new wallet
FfiResult_OpaqueWallet_ptr_t wallet_result = new_wallet_result(desc,change,net,bc_config,db_config);
assert(wallet_result.err == FFI_ERROR_NONE);
assert(wallet_result.ok != NULL);
free_blockchain_config(bc_config);
free_database_config(db_config);
OpaqueWallet_t *wallet = wallet_result.ok;
// sync wallet
FfiResultVoid_t sync_result = sync_wallet(wallet);
assert(sync_result.err == FFI_ERROR_NONE);
free_void_result(sync_result);
// list transactions
FfiResult_Vec_TransactionDetails_t txdetails_result = list_transactions(wallet);
assert(txdetails_result.ok.len > 0);
assert(txdetails_result.err == FFI_ERROR_NONE);
TransactionDetails_t * txdetails_ptr = txdetails_result.ok.ptr;
for (int i = 0; i < txdetails_result.ok.len; i++) {
//printf("%d: txid: %s\n", i, txdetails_ptr[i].txid);
assert(txdetails_ptr[i].txid != NULL);
//printf("%d: timestamp: %ld\n", i, txdetails_ptr[i].timestamp);
assert(txdetails_ptr[i].is_confirmed);
//printf("%d: received: %ld\n", i, txdetails_ptr[i].received);
//printf("%d: sent: %ld\n", i, txdetails_ptr[i].sent);
assert(txdetails_ptr[i].received > 0 || txdetails_ptr[i].sent > 0);
//printf("%d: fees: %ld\n", i, txdetails_ptr[i].fees);
assert(txdetails_ptr[i].fee > 0);
//printf("%d: height: %d\n", i, txdetails_ptr[i].height);
assert(txdetails_ptr[i].confirmation_time.height > 0);
}
free_vectxdetails_result(txdetails_result);
free_wallet_result(wallet_result);
}
return EXIT_SUCCESS;
}