Lib.rs compiles

This commit is contained in:
Francisco Gindre 2021-07-22 11:50:54 -03:00
parent 9fd20def37
commit 0f8c588a8c
2 changed files with 115 additions and 68 deletions

View File

@ -13,7 +13,7 @@ typedef struct {
uintptr_t len;
} FFIUVKBoxedSlice;
int32_t zcashlc_branch_id_for_height(int32_t height);
int32_t zcashlc_branch_id_for_height(int32_t height, uint32_t network_id);
/**
* Clears the record of the last error message.
@ -23,7 +23,8 @@ void zcashlc_clear_last_error(void);
int32_t zcashlc_clear_utxos(const uint8_t *db_data,
uintptr_t db_data_len,
const char *taddress,
int32_t above_height);
int32_t above_height,
uint32_t network_id);
/**
* Creates a transaction paying the specified address from the given account.
@ -45,18 +46,20 @@ int64_t zcashlc_create_to_address(const uint8_t *db_data,
const uint8_t *spend_params,
uintptr_t spend_params_len,
const uint8_t *output_params,
uintptr_t output_params_len);
uintptr_t output_params_len,
uint32_t network_id);
int32_t zcashlc_decrypt_and_store_transaction(const uint8_t *db_data,
uintptr_t db_data_len,
const uint8_t *tx,
uintptr_t tx_len);
uintptr_t tx_len,
uint32_t network_id);
/**
* derives a shielded address from the given extended full viewing key.
* call zcashlc_string_free with the returned pointer when done using it
*/
char *zcashlc_derive_extended_full_viewing_key(const char *extsk);
char *zcashlc_derive_extended_full_viewing_key(const char *extsk, uint32_t network_id);
/**
* Derives Extended Full Viewing Keys from the given seed into 'accounts' number of accounts.
@ -68,7 +71,8 @@ char *zcashlc_derive_extended_full_viewing_key(const char *extsk);
char **zcashlc_derive_extended_full_viewing_keys(const uint8_t *seed,
uintptr_t seed_len,
int32_t accounts,
uintptr_t *capacity_ret);
uintptr_t *capacity_ret,
uint32_t network_id);
/**
* Derives Extended Spending Keys from the given seed into 'accounts' number of accounts.
@ -80,7 +84,8 @@ char **zcashlc_derive_extended_full_viewing_keys(const uint8_t *seed,
char **zcashlc_derive_extended_spending_keys(const uint8_t *seed,
uintptr_t seed_len,
int32_t accounts,
uintptr_t *capacity_ret);
uintptr_t *capacity_ret,
uint32_t network_id);
/**
* derives a shielded address from the given seed.
@ -88,24 +93,25 @@ char **zcashlc_derive_extended_spending_keys(const uint8_t *seed,
*/
char *zcashlc_derive_shielded_address_from_seed(const uint8_t *seed,
uintptr_t seed_len,
int32_t account_index);
int32_t account_index,
uint32_t network_id);
/**
* derives a shielded address from the given viewing key.
* call zcashlc_string_free with the returned pointer when done using it
*/
char *zcashlc_derive_shielded_address_from_viewing_key(const char *extfvk);
char *zcashlc_derive_shielded_address_from_viewing_key(const char *extfvk, uint32_t network_id);
/**
* derives a shielded address from the given viewing key.
* call zcashlc_string_free with the returned pointer when done using it
*/
char *zcashlc_derive_transparent_address_from_public_key(const char *pubkey);
char *zcashlc_derive_transparent_address_from_public_key(const char *pubkey, uint32_t network_id);
/**
* Derives a transparent address from the given secret key enconded as a WIF string
*/
char *zcashlc_derive_transparent_address_from_secret_key(const char *tsk);
char *zcashlc_derive_transparent_address_from_secret_key(const char *tsk, uint32_t network_id);
/**
* Derives a transparent address from the given seed
@ -113,7 +119,8 @@ char *zcashlc_derive_transparent_address_from_secret_key(const char *tsk);
char *zcashlc_derive_transparent_address_from_seed(const uint8_t *seed,
uintptr_t seed_len,
int32_t account,
int32_t index);
int32_t index,
uint32_t network_id);
/**
* TEST TEST 123 TEST
@ -124,11 +131,13 @@ char *zcashlc_derive_transparent_address_from_seed(const uint8_t *seed,
char *zcashlc_derive_transparent_private_key_from_seed(const uint8_t *seed,
uintptr_t seed_len,
int32_t account,
int32_t index);
int32_t index,
uint32_t network_id);
FFIUVKBoxedSlice *zcashlc_derive_unified_viewing_keys_from_seed(const uint8_t *seed,
uintptr_t seed_len,
int32_t accounts);
int32_t accounts,
uint32_t network_id);
/**
* Copies the last error message into the provided allocated buffer.
@ -142,16 +151,23 @@ void zcashlc_free_uvk_array(FFIUVKBoxedSlice *uvks);
*
* Call `zcashlc_string_free` on the returned pointer when you are finished with it.
*/
char *zcashlc_get_address(const uint8_t *db_data, uintptr_t db_data_len, int32_t account);
char *zcashlc_get_address(const uint8_t *db_data,
uintptr_t db_data_len,
int32_t account,
uint32_t network_id);
/**
* Returns the balance for the account, including all unspent notes that we know about.
*/
int64_t zcashlc_get_balance(const uint8_t *db_data, uintptr_t db_data_len, int32_t account);
int64_t zcashlc_get_balance(const uint8_t *db_data,
uintptr_t db_data_len,
int32_t account,
uint32_t network_id);
int32_t zcashlc_get_nearest_rewind_height(const uint8_t *db_data,
uintptr_t db_data_len,
int32_t height);
int32_t height,
uint32_t network_id);
/**
* Returns the memo for a received note, if it is known and a valid UTF-8 string.
@ -163,7 +179,8 @@ int32_t zcashlc_get_nearest_rewind_height(const uint8_t *db_data,
*/
char *zcashlc_get_received_memo_as_utf8(const uint8_t *db_data,
uintptr_t db_data_len,
int64_t id_note);
int64_t id_note,
uint32_t network_id);
/**
* Returns the memo for a sent note, if it is known and a valid UTF-8 string.
@ -173,7 +190,10 @@ char *zcashlc_get_received_memo_as_utf8(const uint8_t *db_data,
*
* Call `zcashlc_string_free` on the returned pointer when you are finished with it.
*/
char *zcashlc_get_sent_memo_as_utf8(const uint8_t *db_data, uintptr_t db_data_len, int64_t id_note);
char *zcashlc_get_sent_memo_as_utf8(const uint8_t *db_data,
uintptr_t db_data_len,
int64_t id_note,
uint32_t network_id);
/**
* Returns the verified transparent balance for the address, which ignores utxos that have been
@ -181,7 +201,8 @@ char *zcashlc_get_sent_memo_as_utf8(const uint8_t *db_data, uintptr_t db_data_le
*/
int64_t zcashlc_get_total_transparent_balance(const uint8_t *db_data,
uintptr_t db_data_len,
const char *address);
const char *address,
uint32_t network_id);
/**
* Returns the verified balance for the account, which ignores notes that have been
@ -189,7 +210,8 @@ int64_t zcashlc_get_total_transparent_balance(const uint8_t *db_data,
*/
int64_t zcashlc_get_verified_balance(const uint8_t *db_data,
uintptr_t db_data_len,
int32_t account);
int32_t account,
uint32_t network_id);
/**
* Returns the verified transparent balance for the address, which ignores utxos that have been
@ -197,7 +219,8 @@ int64_t zcashlc_get_verified_balance(const uint8_t *db_data,
*/
int64_t zcashlc_get_verified_transparent_balance(const uint8_t *db_data,
uintptr_t db_data_len,
const char *address);
const char *address,
uint32_t network_id);
/**
* Initialises the data database with the given number of accounts using the given seed.
@ -212,7 +235,8 @@ char **zcashlc_init_accounts_table(const uint8_t *db_data,
const uint8_t *seed,
uintptr_t seed_len,
int32_t accounts,
uintptr_t *capacity_ret);
uintptr_t *capacity_ret,
uint32_t network_id);
/**
* Initialises the data database with the given extended full viewing keys
@ -220,7 +244,8 @@ char **zcashlc_init_accounts_table(const uint8_t *db_data,
*/
bool zcashlc_init_accounts_table_with_keys(const uint8_t *db_data,
uintptr_t db_data_len,
FFIUVKBoxedSlice *uvks);
FFIUVKBoxedSlice *uvks,
uint32_t network_id);
/**
* Initialises the data database with the given block.
@ -233,30 +258,33 @@ int32_t zcashlc_init_blocks_table(const uint8_t *db_data,
int32_t height,
const char *hash_hex,
uint32_t time,
const char *sapling_tree_hex);
const char *sapling_tree_hex,
uint32_t network_id);
/**
* Sets up the internal structure of the data database.
*/
int32_t zcashlc_init_data_database(const uint8_t *db_data, uintptr_t db_data_len);
int32_t zcashlc_init_data_database(const uint8_t *db_data,
uintptr_t db_data_len,
uint32_t network_id);
/**
* Returns true when the address is valid and shielded.
* Returns false in any other case
* Errors when the provided address belongs to another network
*/
bool zcashlc_is_valid_shielded_address(const char *address);
bool zcashlc_is_valid_shielded_address(const char *address, uint32_t network_id);
/**
* Returns true when the address is valid and transparent.
* Returns false in any other case
*/
bool zcashlc_is_valid_transparent_address(const char *address);
bool zcashlc_is_valid_transparent_address(const char *address, uint32_t network_id);
/**
* returns whether the given viewing key is valid or not
*/
bool zcashlc_is_valid_viewing_key(const char *key);
bool zcashlc_is_valid_viewing_key(const char *key, uint32_t network_id);
/**
* Returns the length of the last error message to be logged.
@ -272,7 +300,8 @@ bool zcashlc_put_utxo(const uint8_t *db_data,
const uint8_t *script_bytes,
uintptr_t script_bytes_len,
int64_t value,
int32_t height);
int32_t height,
uint32_t network_id);
/**
* Rewinds the data database to the given height.
@ -280,7 +309,10 @@ bool zcashlc_put_utxo(const uint8_t *db_data,
* If the requested height is greater than or equal to the height of the last scanned
* block, this function does nothing.
*/
bool zcashlc_rewind_to_height(const uint8_t *db_data, uintptr_t db_data_len, int32_t height);
bool zcashlc_rewind_to_height(const uint8_t *db_data,
uintptr_t db_data_len,
int32_t height,
uint32_t network_id);
/**
* Scans new blocks added to the cache for any transactions received by the tracked
@ -302,7 +334,8 @@ int32_t zcashlc_scan_blocks(const uint8_t *db_cache,
uintptr_t db_cache_len,
const uint8_t *db_data,
uintptr_t db_data_len,
uint32_t scan_limit);
uint32_t scan_limit,
uint32_t network_id);
int64_t zcashlc_shield_funds(const uint8_t *db_data,
uintptr_t db_data_len,
@ -313,7 +346,8 @@ int64_t zcashlc_shield_funds(const uint8_t *db_data,
const uint8_t *spend_params,
uintptr_t spend_params_len,
const uint8_t *output_params,
uintptr_t output_params_len);
uintptr_t output_params_len,
uint32_t network_id);
/**
* Frees strings returned by other zcashlc functions.
@ -341,7 +375,8 @@ void zcashlc_string_free(char *s);
int32_t zcashlc_validate_combined_chain(const uint8_t *db_cache,
uintptr_t db_cache_len,
const uint8_t *db_data,
uintptr_t db_data_len);
uintptr_t db_data_len,
uint32_t network_id);
/**
* Frees vectors of strings returned by other zcashlc functions.

View File

@ -366,7 +366,7 @@ pub unsafe extern "C" fn zcashlc_derive_unified_viewing_keys_from_seed(
.map(|account| {
let extfvk = ExtendedFullViewingKey::from(&spending_key(&seed, network.coin_type(), AccountId(account)));
let extpub = derive_public_key_from_seed(&network, &seed, AccountId(account), 0).unwrap();
unified_viewing_key_new(&extfvk, &extpub)
unified_viewing_key_new(&extfvk, &extpub, network)
})
.collect();
Ok(uvk_vec_to_ffi(uvks))
@ -629,7 +629,7 @@ pub unsafe extern "C" fn zcashlc_is_valid_shielded_address(address: *const c_cha
fn is_valid_shielded_address(address: &str,
network: &Network) -> bool {
match RecipientAddress::decode(&network, &address) {
match RecipientAddress::decode(network, &address) {
Some(addr) => match addr {
RecipientAddress::Shielded(_) => true,
RecipientAddress::Transparent(_) => false,
@ -671,7 +671,7 @@ pub unsafe extern "C" fn zcashlc_is_valid_viewing_key(key: *const c_char,
fn is_valid_transparent_address(address: &str,
network: &Network) -> bool {
match RecipientAddress::decode(&network, &address) {
match RecipientAddress::decode(network, &address) {
Some(addr) => match addr {
RecipientAddress::Shielded(_) => false,
RecipientAddress::Transparent(_) => true,
@ -906,7 +906,7 @@ pub extern "C" fn zcashlc_validate_combined_chain(
db_cache_len: usize,
db_data: *const u8,
db_data_len: usize,
network_id, u32,
network_id: u32,
) -> i32 {
let res = catch_panic(|| {
let network = parse_network(network_id)?;
@ -1337,36 +1337,6 @@ pub unsafe extern "C" fn zcashlc_derive_transparent_address_from_secret_key(
});
unwrap_exc_or_null(res)
}
//
// Helper code from: https://github.com/adityapk00/zecwallet-light-cli/blob/master/lib/src/lightwallet.rs
//
/// A trait for converting a [u8] to base58 encoded string.
pub trait ToBase58Check {
/// Converts a value of `self` to a base58 value, returning the owned string.
/// The version is a coin-specific prefix that is added.
/// The suffix is any bytes that we want to add at the end (like the "iscompressed" flag for
/// Secret key encoding)
fn to_base58check(&self, version: &[u8], suffix: &[u8]) -> String;
}
impl ToBase58Check for [u8] {
fn to_base58check(&self, version: &[u8], suffix: &[u8]) -> String {
let mut payload: Vec<u8> = Vec::new();
payload.extend_from_slice(version);
payload.extend_from_slice(self);
payload.extend_from_slice(suffix);
let checksum = double_sha256(&payload);
payload.append(&mut checksum[..4].to_vec());
payload.to_base58()
}
}
pub fn double_sha256(payload: &[u8]) -> Vec<u8> {
let h1 = Sha256::digest(&payload);
let h2 = Sha256::digest(&h1);
h2.to_vec()
}
#[no_mangle]
pub extern "C" fn zcashlc_shield_funds(
@ -1434,3 +1404,45 @@ pub extern "C" fn zcashlc_shield_funds(
});
unwrap_exc_or(res, -1)
}
//
// Utility functions
//
fn parse_network(value: u32) -> Result<Network, failure::Error> {
match value {
0 => Ok(TestNetwork),
1 => Ok(MainNetwork),
_ => Err(format_err!("Invalid network type: {}. Expected either 0 or 1 for Testnet or Mainnet, respectively.", value))
}
}
//
// Helper code from: https://github.com/adityapk00/zecwallet-light-cli/blob/master/lib/src/lightwallet.rs
//
/// A trait for converting a [u8] to base58 encoded string.
pub trait ToBase58Check {
/// Converts a value of `self` to a base58 value, returning the owned string.
/// The version is a coin-specific prefix that is added.
/// The suffix is any bytes that we want to add at the end (like the "iscompressed" flag for
/// Secret key encoding)
fn to_base58check(&self, version: &[u8], suffix: &[u8]) -> String;
}
impl ToBase58Check for [u8] {
fn to_base58check(&self, version: &[u8], suffix: &[u8]) -> String {
let mut payload: Vec<u8> = Vec::new();
payload.extend_from_slice(version);
payload.extend_from_slice(self);
payload.extend_from_slice(suffix);
let checksum = double_sha256(&payload);
payload.append(&mut checksum[..4].to_vec());
payload.to_base58()
}
}
pub fn double_sha256(payload: &[u8]) -> Vec<u8> {
let h1 = Sha256::digest(&payload);
let h2 = Sha256::digest(&h1);
h2.to_vec()
}