Skip to content

Commit

Permalink
Sample CLI (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenjiPcx authored Oct 3, 2021
1 parent 0d64daa commit 1c3350c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.vscode/
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
55 changes: 55 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>")
.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::<Pair>::from(mnemonic);
vault
}
None => {
let mnemonic: String = Pair::generate_with_phrase(None).1;
println!("Secret Key: \"{}\"", mnemonic);
let vault = SimpleVault::<Pair>::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
}

0 comments on commit 1c3350c

Please sign in to comment.