Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
wallet new error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Mar 21, 2019
1 parent ca7f8a8 commit 6ae5c81
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
5 changes: 3 additions & 2 deletions cardano-c/cardano.h
Expand Up @@ -154,8 +154,9 @@ typedef struct cardano_account cardano_account;
* \param [in] password_size The size of the password string
* \returns pointer to the constructed wallet that must be freed with `cardano_wallet_delete`
*/
cardano_wallet *cardano_wallet_new(const uint8_t * const entropy_ptr, unsigned long entropy_size,
const char * const password_ptr, unsigned long password_size);
cardano_result cardano_wallet_new(const uint8_t * const entropy_ptr, unsigned long entropy_size,
const char * const password_ptr, unsigned long password_size,
cardano_wallet** wallet);
/*!
* Free the memory of a wallet allocated with `cardano_wallet_new`
*/
Expand Down
13 changes: 8 additions & 5 deletions cardano-c/src/wallet.rs
Expand Up @@ -9,7 +9,7 @@ use std::os::raw::c_char;
use std::{ffi, ptr, slice};

use address::ffi_address_to_base58;
use types::{AccountPtr, WalletPtr};
use types::{AccountPtr, CardanoResult, WalletPtr};

/* ******************************************************************************* *
* Wallet object *
Expand All @@ -25,27 +25,30 @@ use types::{AccountPtr, WalletPtr};
/// object. This function may fail if:
///
/// - panic: if there is no more memory to allocate the object to return
/// - panic or return 0 (nullptr or NULL) if the given seed_ptr is of invalid length
/// - return `failure` if the given seed_ptr is of invalid length
/// - return 'success' when not
///
#[no_mangle]
pub extern "C" fn cardano_wallet_new(
entropy_ptr: *const u8, /* expecting entropy ptr ... */
entropy_size: usize, /* entropy size */
password_ptr: *const u8, /* password ptr */
password_size: usize, /* password size */
) -> WalletPtr {
wallet_out: *mut WalletPtr,
) -> CardanoResult {
let entropy_slice = unsafe { slice::from_raw_parts(entropy_ptr, entropy_size) };
let password = unsafe { slice::from_raw_parts(password_ptr, password_size) };

let entropy = match bip::bip39::Entropy::from_slice(entropy_slice) {
Err(_) => return ptr::null_mut(),
Err(_) => return CardanoResult::failure(),
Ok(e) => e,
};

let wallet = bip44::Wallet::from_entropy(&entropy, &password, hdwallet::DerivationScheme::V2);

let wallet_box = Box::new(wallet);
Box::into_raw(wallet_box)
unsafe { ptr::write(wallet_out, Box::into_raw(wallet_box)) };
CardanoResult::success()
}

/// take ownership of the given pointer and free the associated data
Expand Down
52 changes: 42 additions & 10 deletions cardano-c/test/test.c
Expand Up @@ -5,32 +5,64 @@
#include "cardano.h"
#include "unity/unity.h"

static const uint8_t static_wallet_entropy[16] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
static const uint8_t static_wallet_entropy[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

void test_can_create_address(void) {
static const char* alias = "Test Wallet";
static char *address;
void test_can_create_address(void)
{
static const char *alias = "Test Wallet";
static char *address;

cardano_wallet *wallet = cardano_wallet_new(static_wallet_entropy, 16, "abc", 3);
cardano_wallet *wallet;
cardano_result wallet_rc = cardano_wallet_new(static_wallet_entropy, 16, "abc", 3, &wallet);

TEST_ASSERT_MESSAGE(wallet, "The wallet creation failed");
TEST_ASSERT_EQUAL_MESSAGE(0, wallet_rc, "The wallet creation failed");

cardano_account *account = cardano_account_create(wallet, alias, 0);

TEST_ASSERT_MESSAGE(account, "The account creation failed");

cardano_account_generate_addresses(account, 0, 0, 1, &address);
cardano_account_generate_addresses(account, 0, 0, 1, &address);

TEST_ASSERT_MESSAGE(!cardano_address_is_valid(address), "The generated address is invalid");

TEST_ASSERT_MESSAGE(!cardano_address_is_valid(address) , "The generated address is invalid");
cardano_account_delete(account);

cardano_account_delete(account);
cardano_wallet_delete(wallet);
}

void invalid_entropy_size_returns_failure()
{
char *password = "abc";
const uint8_t invalid_wallet_entropy[4] = {0};

cardano_wallet_delete(wallet);
cardano_wallet *wallet;
cardano_result wallet_rc = cardano_wallet_new(
invalid_wallet_entropy, sizeof(invalid_wallet_entropy), password, strlen(password), &wallet);

TEST_ASSERT_EQUAL(1, wallet_rc);
}

void valid_entropy_size_returns_success()
{
const size_t valid_sizes[] = {12, 16, 20, 24, 28, 32};
const char *password = "abc";
for (int i = 0; i < sizeof(valid_sizes) / sizeof(size_t); ++i)
{
size_t size = valid_sizes[i];
uint8_t *valid_wallet_entropy = malloc(size);
cardano_wallet *wallet;
cardano_result wallet_rc = cardano_wallet_new(
valid_wallet_entropy, size, password, strlen(password), &wallet);
TEST_ASSERT_EQUAL(0, wallet_rc);
free(valid_wallet_entropy);
}
}

int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_can_create_address);
RUN_TEST(invalid_entropy_size_returns_failure);
RUN_TEST(valid_entropy_size_returns_success);
return UNITY_END();
}

0 comments on commit 6ae5c81

Please sign in to comment.