Skip to content

Commit

Permalink
cleanup plugin api from legacy conversion
Browse files Browse the repository at this point in the history
These functions are not really used anymore, and while the cost of
maintaining them is not that high, it's still better to have less things
to care about when updating, or when new users aks questions.

This commit focuses mostly on the bindings, mostly because that's where
most of the 'complexity' (duplication) is found, the underlying code is
not really that problematic, but it may be good to remove it too.

In any case, code can be retrieved from the history if necessary.

- walletRestore
- walletRetrieveFunds
- walletConvert
- conversionTransactionsSize
- conversionTransactionsGet
- conversionGetIgnored
- walletPendingTransactions
- pendingTransactionsSize
- pendingTransactionsGet
  • Loading branch information
ecioppettini committed Mar 14, 2022
1 parent c90d112 commit ca6b16d
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 1,775 deletions.
257 changes: 4 additions & 253 deletions bindings/wallet-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ use wallet_core::c::{
fragment::{fragment_delete, fragment_from_raw, fragment_id},
symmetric_cipher_decrypt,
time::BlockDate,
vote, wallet_convert, wallet_convert_ignored, wallet_convert_transactions_get,
wallet_convert_transactions_size, wallet_delete_conversion, wallet_delete_error,
wallet_delete_proposal, wallet_delete_settings, wallet_delete_wallet, wallet_id,
wallet_import_keys, wallet_recover, wallet_retrieve_funds, wallet_set_state,
wallet_spending_counter, wallet_total_value, wallet_vote_cast,
vote, wallet_delete_error, wallet_delete_proposal, wallet_delete_settings,
wallet_delete_wallet, wallet_id, wallet_import_keys, wallet_set_state, wallet_spending_counter,
wallet_total_value, wallet_vote_cast,
};
use wallet_core::{
Conversion as ConversionRust, Error as ErrorRust, Fragment as FragmentRust,
Proposal as ProposalRust, Wallet as WalletRust,
Error as ErrorRust, Fragment as FragmentRust, Proposal as ProposalRust, Wallet as WalletRust,
};

#[repr(C)]
pub struct Wallet {}
#[repr(C)]
pub struct Settings {}
#[repr(C)]
pub struct Conversion {}
#[repr(C)]
pub struct Proposal {}
#[repr(C)]
pub struct Fragment;
Expand All @@ -38,68 +33,11 @@ pub struct EncryptingVoteKey {}

pub type WalletPtr = *mut Wallet;
pub type SettingsPtr = *mut Settings;
pub type ConversionPtr = *mut Conversion;
pub type ProposalPtr = *mut Proposal;
pub type FragmentPtr = *mut Fragment;
pub type ErrorPtr = *mut Error;
pub type EncryptingVoteKeyPtr = *mut EncryptingVoteKey;

/// retrieve a wallet from the given mnemonics, password and protocol magic
///
/// this function will work for all yoroi, daedalus and other wallets
/// as it will try every kind of wallet anyway
///
/// You can also use this function to recover a wallet even after you have
/// transferred all the funds to the new format (see the _convert_ function)
///
/// The recovered wallet will be returned in `wallet_out`.
///
/// # parameters
///
/// * mnemonics: a null terminated utf8 string (already normalized NFKD) in english;
/// * password: pointer to the password (in bytes, can be UTF8 string or a bytes of anything);
/// this value is optional and passing a null pointer will result in no password;
/// * password_length: the length of the password;
/// * wallet_out: a pointer to a pointer. The recovered wallet will be allocated on this pointer;
///
/// # errors
///
/// The function may fail if:
///
/// * the mnemonics are not valid (invalid length or checksum);
/// * the `wallet_out` is null pointer
///
/// On error the function returns a `ErrorPtr`. On success `NULL` is returned.
/// The `ErrorPtr` can then be observed to gathered details of the error.
/// Don't forget to call `iohk_jormungandr_wallet_delete_error` to free
/// the `ErrorPtr` from memory and avoid memory leaks.
///
/// # Safety
///
/// This function dereference raw pointers. Even though
/// the function checks if the pointers are null. Mind not to put random values
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub unsafe extern "C" fn iohk_jormungandr_wallet_recover(
mnemonics: *const c_char,
password: *const u8,
password_length: usize,
wallet_out: *mut WalletPtr,
) -> ErrorPtr {
let mnemonics = CStr::from_ptr(mnemonics);

let mnemonics = mnemonics.to_string_lossy();
let r = wallet_recover(
&mnemonics,
password,
password_length,
wallet_out as *mut *mut WalletRust,
);

r.into_c_api() as ErrorPtr
}

/// recover a wallet from an account and a list of utxo keys
///
/// You can also use this function to recover a wallet even after you have
Expand Down Expand Up @@ -182,180 +120,6 @@ pub unsafe extern "C" fn iohk_jormungandr_wallet_id(
wallet_id(wallet as *mut WalletRust, id_out).into_c_api() as ErrorPtr
}

/// retrieve funds from daedalus or yoroi wallet in the given block0 (or
/// any other blocks).
///
/// Execute this function then you can check who much funds you have
/// retrieved from the given block.
///
/// this function may take sometimes so it is better to only call this
/// function if needed.
///
/// # Parameters
///
/// * wallet: the recovered wallet (see recover function);
/// * block0: the pointer to the bytes of the block0;
/// * block0_length: the length of the block0 byte string;
/// * settings_out: the settings that will be parsed from the given
/// block0;
///
/// # Errors
///
/// * this function may fail if the wallet pointer is null;
/// * the block is not valid (cannot be decoded)
///
/// On error the function returns a `ErrorPtr`. On success `NULL` is returned.
/// The `ErrorPtr` can then be observed to gathered details of the error.
/// Don't forget to call `iohk_jormungandr_wallet_delete_error` to free
/// the `ErrorPtr` from memory and avoid memory leaks.
///
/// # Safety
///
/// This function dereference raw pointers. Even though
/// the function checks if the pointers are null. Mind not to put random values
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub unsafe extern "C" fn iohk_jormungandr_wallet_retrieve_funds(
wallet: WalletPtr,
block0: *const u8,
block0_length: usize,
settings_out: *mut SettingsPtr,
) -> ErrorPtr {
let r = wallet_retrieve_funds(
wallet as *mut WalletRust,
block0,
block0_length,
settings_out as *mut *mut SettingsRust,
);

r.into_c_api() as ErrorPtr
}

/// once funds have been retrieved with `iohk_jormungandr_wallet_retrieve_funds`
/// it is possible to convert all existing funds to the new wallet.
///
/// The returned arrays are transactions to send to the network in order to do the
/// funds conversion.
///
/// Don't forget to call `iohk_jormungandr_wallet_delete_conversion` to
/// properly free the memory
///
/// # Errors
///
/// On error the function returns a `ErrorPtr`. On success `NULL` is returned.
/// The `ErrorPtr` can then be observed to gathered details of the error.
/// Don't forget to call `iohk_jormungandr_wallet_delete_error` to free
/// the `ErrorPtr` from memory and avoid memory leaks.
///
/// # Safety
///
/// This function dereference raw pointers. Even though
/// the function checks if the pointers are null. Mind not to put random values
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub unsafe extern "C" fn iohk_jormungandr_wallet_convert(
wallet: WalletPtr,
settings: SettingsPtr,
valid_until: BlockDate,
conversion_out: *mut ConversionPtr,
) -> ErrorPtr {
let r = wallet_convert(
wallet as *mut WalletRust,
settings as *mut SettingsRust,
valid_until,
conversion_out as *mut *mut ConversionRust,
);

r.into_c_api() as ErrorPtr
}

/// get the number of transactions built to convert the retrieved wallet
///
/// # Safety
///
/// This function dereference raw pointers. Even though
/// the function checks if the pointers are null. Mind not to put random values
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub unsafe extern "C" fn iohk_jormungandr_wallet_convert_transactions_size(
conversion: ConversionPtr,
) -> usize {
wallet_convert_transactions_size(conversion as *mut ConversionRust)
}

/// retrieve the index-nth transactions in the conversions starting from 0
/// and finishing at `size-1` where size is retrieved from
/// `iohk_jormungandr_wallet_convert_transactions_size`.
///
/// the memory allocated returned is not owned and should not be kept
/// for longer than potential call to `iohk_jormungandr_wallet_delete_conversion`
///
/// # Errors
///
/// On error the function returns a `ErrorPtr`. On success `NULL` is returned.
/// The `ErrorPtr` can then be observed to gathered details of the error.
/// Don't forget to call `iohk_jormungandr_wallet_delete_error` to free
/// the `ErrorPtr` from memory and avoid memory leaks.
///
/// # Safety
///
/// This function dereference raw pointers. Even though
/// the function checks if the pointers are null. Mind not to put random values
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub unsafe extern "C" fn iohk_jormungandr_wallet_convert_transactions_get(
conversion: ConversionPtr,
index: usize,
transaction_out: *mut *const u8,
transaction_size: *mut usize,
) -> ErrorPtr {
let r = wallet_convert_transactions_get(
conversion as *mut ConversionRust,
index,
transaction_out,
transaction_size,
);

r.into_c_api() as ErrorPtr
}

/// get the total value ignored in the conversion
///
/// value_out: will returns the total value lost into dust inputs
/// ignored_out: will returns the number of dust utxos
///
/// these returned values are informational only and this show that
/// there are UTxOs entries that are unusable because of the way they
/// are populated with dusts.
///
/// # Errors
///
/// On error the function returns a `ErrorPtr`. On success `NULL` is returned.
/// The `ErrorPtr` can then be observed to gathered details of the error.
/// Don't forget to call `iohk_jormungandr_wallet_delete_error` to free
/// the `ErrorPtr` from memory and avoid memory leaks.
///
/// # Safety
///
/// This function dereference raw pointers. Even though
/// the function checks if the pointers are null. Mind not to put random values
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub unsafe extern "C" fn iohk_jormungandr_wallet_convert_ignored(
conversion: ConversionPtr,
value_out: *mut u64,
ignored_out: *mut usize,
) -> ErrorPtr {
let r = wallet_convert_ignored(conversion as *mut ConversionRust, value_out, ignored_out);

r.into_c_api() as ErrorPtr
}

/// get the current spending counter for the (only) account in this wallet
///
///
Expand Down Expand Up @@ -789,19 +553,6 @@ pub extern "C" fn iohk_jormungandr_wallet_delete_wallet(wallet: WalletPtr) {
wallet_delete_wallet(wallet as *mut WalletRust)
}

/// delete the pointer
///
/// # Safety
///
/// This function dereference raw pointers. Even though
/// the function checks if the pointers are null. Mind not to put random values
/// in or you may see unexpected behaviors
///
#[no_mangle]
pub extern "C" fn iohk_jormungandr_wallet_delete_conversion(conversion: ConversionPtr) {
wallet_delete_conversion(conversion as *mut ConversionRust)
}

/// delete the pointer
///
/// # Safety
Expand Down
Loading

0 comments on commit ca6b16d

Please sign in to comment.