Skip to content

Commit

Permalink
[Wallet] - Ask user for Gateway address if wallet.conf does not exist (
Browse files Browse the repository at this point in the history
…MystenLabs#1713)

* ask user for Gateway address if wallet.conf does not exist

* add new address to new wallet conf
  • Loading branch information
patrickkuo authored May 2, 2022
1 parent 4e03821 commit d3ac68a
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions sui/src/bin/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use async_trait::async_trait;
use clap::*;
use colored::Colorize;
use std::{
io,
io::{stderr, stdout, Write},
ops::Deref,
path::PathBuf,
};

use async_trait::async_trait;
use clap::*;
use colored::Colorize;
use jsonrpsee::http_client::HttpClientBuilder;
use tracing::debug;

use sui::config::{Config, WalletConfig};
use sui::gateway_config::GatewayType;
use sui::keystore::KeystoreType;
use sui::{
shell::{
install_shell_plugins, AsyncHandler, CacheKey, CommandStructure, CompletionCache, Shell,
Expand All @@ -18,7 +26,6 @@ use sui::{
SUI_WALLET_CONFIG,
};
use sui_types::exit_main;
use tracing::debug;

const SUI: &str = " _____ _ _ __ ____ __
/ ___/__ __(_) | | / /___ _/ / /__ / /_
Expand Down Expand Up @@ -64,11 +71,41 @@ async fn try_main() -> Result<(), anyhow::Error> {
let mut app: Command = ClientOpt::command();
app = app.no_binary_name(false);
let options: ClientOpt = ClientOpt::from_arg_matches(&app.get_matches())?;

let wallet_conf_path = options
.config
.clone()
.unwrap_or(sui_config_dir()?.join(SUI_WALLET_CONFIG));

// Prompt user for connect to gateway if config not exists.
if !wallet_conf_path.exists() {
print!(
"Config file [{:?}] doesn't exist, do you want to connect to a Sui Gateway [yn]?",
wallet_conf_path
);
if matches!(read_line(), Ok(line) if line.to_lowercase() == "y") {
print!("Sui Gateway Url : ");
let url = read_line()?;

// Check url is valid
HttpClientBuilder::default().build(&url)?;
let keystore_path = wallet_conf_path
.parent()
.unwrap_or(&sui_config_dir()?)
.join("wallet.key");
let keystore = KeystoreType::File(keystore_path);
let new_address = keystore.init()?.add_random_key()?;
WalletConfig {
accounts: vec![new_address],
keystore,
gateway: GatewayType::RPC(url),
active_address: None,
}
.persisted(&wallet_conf_path)
.save()?;
}
}

let mut context = WalletContext::new(&wallet_conf_path)?;

// Sync all accounts on start up.
Expand Down Expand Up @@ -115,6 +152,13 @@ async fn try_main() -> Result<(), anyhow::Error> {
Ok(())
}

fn read_line() -> Result<String, anyhow::Error> {
let mut s = String::new();
let _ = stdout().flush();
io::stdin().read_line(&mut s)?;
Ok(s.trim_end().to_string())
}

#[tokio::main]
async fn main() {
exit_main!(try_main().await)
Expand Down

0 comments on commit d3ac68a

Please sign in to comment.