From 1c3350c92a52735153e9d4af4ec2b10897d1060d Mon Sep 17 00:00:00 2001 From: Kenji Phang Date: Sun, 3 Oct 2021 16:53:28 +0100 Subject: [PATCH] Sample CLI (#3) --- .gitignore | 2 ++ Cargo.toml | 5 ++++- src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 088ba6b..e6e0130 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk + +.vscode/ \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index b92bbbc..c1e333d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,12 @@ tiny-bip39 = "0.8.0" async-trait = "0.1.42" thiserror = "1.0.23" sp-core = "3.0.0" +async-std = { version = "1.10.0", features = ["attributes"] } +clap = "2.33" [dev-dependencies] -async-std = { version = "1.9.0", features = ["attributes"] } +async-std = { version = "1.10.0", features = ["attributes"] } [features] +default = ["std"] std = [] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2aa3bfe --- /dev/null +++ b/src/main.rs @@ -0,0 +1,55 @@ +extern crate clap; +use clap::{App, Arg}; +use libwallet::{ + self, + sr25519::{Pair, Public}, + Pair as _, SimpleVault, Wallet, +}; +use sp_core::crypto::Ss58Codec; + +#[async_std::main] +async fn main() { + let matches = App::new("Wallet Generator") + .version("0.1.0") + .author("Virto Team ") + .about("Generates Wallet Account") + .arg(Arg::with_name("seed") + .short("s") + .long("from-seed") + .value_name("MNEMONIC") + .help("Generates a wallet address from mnemonic.")) + .arg(Arg::with_name("network") + .short("n") + .long("network") + .value_name("NETWORK") + .help("Formats the address to specified network.")) + .get_matches(); + + let pub_address = get_pub_address(matches.value_of("seed")).await; + let network: &str = matches.value_of("network").unwrap_or("substrate"); + + let address: String = pub_address + .to_ss58check_with_version(network.parse().unwrap_or_else(|_| Default::default())); + println!("Public key (SS58): {}", address); +} + +async fn get_pub_address(seed: Option<&str>) -> Public { + let vault = match seed { + Some(mnemonic) => { + println!("Secret Key: \"{}\"", mnemonic); + let vault = SimpleVault::::from(mnemonic); + vault + } + None => { + let mnemonic: String = Pair::generate_with_phrase(None).1; + println!("Secret Key: \"{}\"", mnemonic); + let vault = SimpleVault::::from(mnemonic.as_str()); + vault + } + }; + + let mut wallet = Wallet::from(vault); + wallet.unlock("").await.unwrap(); + let public_add = wallet.root_account().unwrap().public(); + public_add +}