Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wallet ffi use dns #6152

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 93 additions & 22 deletions base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ use tari_common_types::{
};
use tari_comms::{
multiaddr::Multiaddr,
peer_manager::NodeIdentity,
peer_manager::{NodeIdentity, PeerQuery},
transports::MemoryTransport,
types::CommsPublicKey,
};
Expand Down Expand Up @@ -5259,6 +5259,7 @@ pub unsafe extern "C" fn wallet_create(
passphrase: *const c_char,
seed_words: *const TariSeedWords,
network_str: *const c_char,
peer_seed_str: *const c_char,

callback_received_transaction: unsafe extern "C" fn(*mut TariPendingInboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(*mut TariCompletedTransaction),
Expand Down Expand Up @@ -5321,24 +5322,16 @@ pub unsafe extern "C" fn wallet_create(
SafePassword::from(pf)
};

let network = if network_str.is_null() {
error = LibWalletError::from(InterfaceError::NullError("network".to_string())).code;
let peer_seed = if peer_seed_str.is_null() {
error = LibWalletError::from(InterfaceError::NullError("peer seed dns".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
} else {
let network = CStr::from_ptr(network_str)
let peer_seed = CStr::from_ptr(peer_seed_str)
.to_str()
.expect("A non-null network should be able to be converted to string");
info!(target: LOG_TARGET, "network set to {}", network);

match Network::from_str(network) {
Ok(n) => n,
Err(_) => {
error = LibWalletError::from(InterfaceError::InvalidArgument("network".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
},
}
.expect("A non-null peer seed should be able to be converted to string");
info!(target: LOG_TARGET, "peer seed dns {}", peer_seed);
peer_seed
};

let recovery_seed = if seed_words.is_null() {
Expand All @@ -5355,6 +5348,26 @@ pub unsafe extern "C" fn wallet_create(
}
};

let network = if network_str.is_null() {
error = LibWalletError::from(InterfaceError::NullError("network".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
} else {
let network = CStr::from_ptr(network_str)
.to_str()
.expect("A non-null network should be able to be converted to string");
info!(target: LOG_TARGET, "network set to {}", network);

match Network::from_str(network) {
Ok(n) => n,
Err(_) => {
error = LibWalletError::from(InterfaceError::InvalidArgument("network".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
},
}
};

let runtime = match Runtime::new() {
Ok(r) => r,
Err(e) => {
Expand Down Expand Up @@ -5473,6 +5486,7 @@ pub unsafe extern "C" fn wallet_create(
let peer_seeds = PeerSeedsConfig {
dns_seeds_name_server: DEFAULT_DNS_NAME_SERVER.parse().unwrap(),
dns_seeds_use_dnssec: true,
dns_seeds: StringList::from(vec![peer_seed.to_string()]),
..Default::default()
};

Expand Down Expand Up @@ -6365,6 +6379,45 @@ pub unsafe extern "C" fn wallet_set_base_node_peer(
}
true
}
/// Gets all seed peers known by the wallet
///
/// ## Arguments
/// `wallet` - The TariWallet pointer
/// `error_out` - Pointer to an int which will be modified to an error code should one occur, may not be null. Functions
/// as an out parameter.
///
/// ## Returns
/// `TariPublicKeys` - Returns a list of all known public keys
///
/// # Safety
/// None
#[no_mangle]
pub unsafe extern "C" fn wallet_get_seed_peers(wallet: *mut TariWallet, error_out: *mut c_int) -> *mut TariPublicKeys {
let mut error = 0;
ptr::swap(error_out, &mut error as *mut c_int);
if wallet.is_null() {
error = LibWalletError::from(InterfaceError::NullError("wallet".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
}
let peer_manager = (*wallet).wallet.comms.peer_manager();
let query = PeerQuery::new().select_where(|p| p.is_seed());
match (*wallet).runtime.block_on(async move {
let peers = peer_manager.perform_query(query).await?;
let mut public_keys = Vec::with_capacity(peers.len());
for peer in peers {
public_keys.push(peer.public_key);
}
Result::<_, WalletError>::Ok(public_keys)
}) {
Ok(public_keys) => Box::into_raw(Box::new(TariPublicKeys(public_keys))),
Err(e) => {
error = LibWalletError::from(e).code;
ptr::swap(error_out, &mut error as *mut c_int);
ptr::null_mut()
},
}
}

/// Upserts a TariContact to the TariWallet. If the contact does not exist it will be Inserted. If it does exist the
/// Alias will be updated.
Expand Down Expand Up @@ -9508,6 +9561,7 @@ mod test {
let passphrase: *const c_char =
CString::into_raw(CString::new("Hello from Alasca").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -9517,6 +9571,7 @@ mod test {
passphrase,
ptr::null(),
alice_network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -9561,6 +9616,7 @@ mod test {
passphrase,
ptr::null(),
alice_network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -9664,7 +9720,7 @@ mod test {

let passphrase: *const c_char =
CString::into_raw(CString::new("dolphis dancing in the coastal waters").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -9674,6 +9730,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -9888,7 +9945,7 @@ mod test {

let passphrase: *const c_char =
CString::into_raw(CString::new("a cat outside in Istanbul").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let wallet = wallet_create(
config,
ptr::null(),
Expand All @@ -9898,6 +9955,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -9952,6 +10010,7 @@ mod test {
let log_path: *const c_char =
CString::into_raw(CString::new(temp_dir.path().join("asdf").to_str().unwrap()).unwrap())
as *const c_char;
let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let recovered_wallet = wallet_create(
config,
log_path,
Expand All @@ -9961,6 +10020,7 @@ mod test {
passphrase,
seed_words,
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10028,7 +10088,7 @@ mod test {

let passphrase: *const c_char =
CString::into_raw(CString::new("Satoshi Nakamoto").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -10038,6 +10098,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10177,7 +10238,7 @@ mod test {

let passphrase: *const c_char =
CString::into_raw(CString::new("J-bay open corona").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -10187,6 +10248,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10310,7 +10372,7 @@ mod test {

let passphrase: *const c_char =
CString::into_raw(CString::new("The master and margarita").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -10320,6 +10382,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10523,7 +10586,7 @@ mod test {
);

let passphrase: *const c_char = CString::into_raw(CString::new("niao").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -10533,6 +10596,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10744,7 +10808,7 @@ mod test {
);

let passphrase: *const c_char = CString::into_raw(CString::new("niao").unwrap()) as *const c_char;

let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -10754,6 +10818,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10996,6 +11061,7 @@ mod test {
error_ptr,
);
let passphrase: *const c_char = CString::into_raw(CString::new("niao").unwrap()) as *const c_char;
let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let wallet_ptr = wallet_create(
config,
ptr::null(),
Expand All @@ -11005,6 +11071,7 @@ mod test {
passphrase,
ptr::null(),
network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -11234,6 +11301,7 @@ mod test {
error_ptr,
);
let passphrase: *const c_char = CString::into_raw(CString::new("niao").unwrap()) as *const c_char;
let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let alice_wallet_ptr = wallet_create(
alice_config,
ptr::null(),
Expand All @@ -11243,6 +11311,7 @@ mod test {
passphrase,
ptr::null(),
alice_network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -11294,6 +11363,7 @@ mod test {
error_ptr,
);
let passphrase: *const c_char = CString::into_raw(CString::new("niao").unwrap()) as *const c_char;
let dns_string: *const c_char = CString::into_raw(CString::new("").unwrap()) as *const c_char;
let bob_wallet_ptr = wallet_create(
bob_config,
ptr::null(),
Expand All @@ -11303,6 +11373,7 @@ mod test {
passphrase,
ptr::null(),
bob_network_str,
dns_string,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down
18 changes: 18 additions & 0 deletions base_layer/wallet_ffi/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,7 @@ struct TariWallet *wallet_create(TariCommsConfig *config,
const char *passphrase,
const struct TariSeedWords *seed_words,
const char *network_str,
const char *peer_seed_str,
void (*callback_received_transaction)(TariPendingInboundTransaction*),
void (*callback_received_transaction_reply)(TariCompletedTransaction*),
void (*callback_received_finalized_transaction)(TariCompletedTransaction*),
Expand Down Expand Up @@ -2999,6 +3000,23 @@ bool wallet_set_base_node_peer(struct TariWallet *wallet,
const char *address,
int *error_out);

/**
* Gets all seed peers known by the wallet
*
* ## Arguments
* `wallet` - The TariWallet pointer
* `error_out` - Pointer to an int which will be modified to an error code should one occur, may not be null. Functions
* as an out parameter.
*
* ## Returns
* `TariPublicKeys` - Returns a list of all known public keys
*
* # Safety
* None
*/
struct TariPublicKeys *wallet_get_seed_peers(struct TariWallet *wallet,
int *error_out);

/**
* Upserts a TariContact to the TariWallet. If the contact does not exist it will be Inserted. If it does exist the
* Alias will be updated.
Expand Down
1 change: 1 addition & 0 deletions integration_tests/src/ffi/ffi_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ extern "C" {
passphrase: *const c_char,
seed_words: *const TariSeedWords,
network_str: *const c_char,
peer_seed_str: *const c_char,
callback_received_transaction: unsafe extern "C" fn(*mut TariPendingInboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(*mut TariCompletedTransaction),
callback_received_finalized_transaction: unsafe extern "C" fn(*mut TariCompletedTransaction),
Expand Down
1 change: 1 addition & 0 deletions integration_tests/src/ffi/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl Wallet {
CString::new("kensentme").unwrap().into_raw(),
seed_words_ptr,
CString::new("localnet").unwrap().into_raw(),
CString::new("").unwrap().into_raw(),
callback_received_transaction,
callback_received_transaction_reply,
callback_received_finalized_transaction,
Expand Down
Loading