use std::env::current_dir;
use std::fs::create_dir_all;
use std::fs::read;
#[allow(unused_imports)]
use nextgraph::local_broker::{
app_request, app_request_stream, init_local_broker, session_start, session_stop, user_connect,
user_disconnect, wallet_close, wallet_create_v0, wallet_get, wallet_get_file, wallet_import,
wallet_open_with_pazzle_words, wallet_read_file, wallet_was_opened, LocalBrokerConfig,
SessionConfig,
};
use nextgraph::net::types::BootstrapContentV0;
use nextgraph::repo::types::PubKey;
use nextgraph::wallet::types::CreateWalletV0;
use nextgraph::wallet::{display_mnemonic, emojis::display_pazzle};
#[async_std::main]
async fn main() -> std::io::Result<()> {
let mut current_path = current_dir()?;
current_path.push(".ng");
current_path.push("example");
create_dir_all(current_path.clone())?;
init_local_broker(Box::new(move || {
LocalBrokerConfig::BasePath(current_path.clone())
}))
.await;
let security_img = read("nextgraph/examples/wallet-security-image-demo.png")?;
let peer_id_of_server_broker = PubKey::nil();
println!("Creating the wallet. this will take some time...");
let wallet_result = wallet_create_v0(CreateWalletV0 {
security_img,
security_txt: "know yourself".to_string(),
pin: [1, 2, 1, 2],
pazzle_length: 9,
send_bootstrap: false,
send_wallet: false,
result_with_wallet_file: true,
local_save: true,
core_bootstrap: BootstrapContentV0::new_localhost(peer_id_of_server_broker),
core_registration: None,
additional_bootstrap: None,
pdf: false,
device_name: "test".to_string(),
})
.await?;
println!("Your wallet name is : {}", wallet_result.wallet_name);
let pazzle = display_pazzle(&wallet_result.pazzle);
let mut pazzle_words = vec![];
println!("Your pazzle is: {:?}", wallet_result.pazzle);
for emoji in pazzle {
println!(
"\t{}:\t{}{}",
emoji.0,
if emoji.0.len() > 12 { "" } else { "\t" },
emoji.1
);
pazzle_words.push(emoji.1.to_string());
}
println!("Your mnemonic is:");
display_mnemonic(&wallet_result.mnemonic)
.iter()
.for_each(|word| print!("{} ", word.as_str()));
println!("");
let user_id = wallet_result.personal_identity();
let status = user_connect(&user_id).await?;
let error_reason = status[0].3.as_ref().unwrap();
assert!(error_reason == "NoiseHandshakeFailed" || error_reason == "ConnectionError");
user_disconnect(&user_id).await?;
let wallet_file = wallet_get_file(&wallet_result.wallet_name).await?;
assert_eq!(wallet_file, wallet_result.wallet_file);
session_stop(&user_id).await?;
wallet_close(&wallet_result.wallet_name).await?;
let _wallet = wallet_get(&wallet_result.wallet_name).await?;
let opened_wallet =
wallet_open_with_pazzle_words(&wallet_result.wallet, &pazzle_words, [1, 2, 1, 2])?;
let _client = wallet_was_opened(opened_wallet).await?;
let _session = session_start(SessionConfig::new_save(
&user_id,
&wallet_result.wallet_name,
))
.await?;
let status = user_connect(&user_id).await?;
let error_reason = status[0].3.as_ref().unwrap();
assert!(error_reason == "NoiseHandshakeFailed" || error_reason == "ConnectionError");
user_disconnect(&user_id).await?;
session_stop(&user_id).await?;
wallet_close(&wallet_result.wallet_name).await?;
Ok(())
}