1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::error::Result;
use sc_service::config::KeystoreConfig;
use std::{fs, path::{PathBuf, Path}};
use structopt::StructOpt;
use crate::error;
use sp_core::crypto::SecretString;
const DEFAULT_KEYSTORE_CONFIG_PATH: &'static str = "keystore";
#[derive(Debug, StructOpt)]
pub struct KeystoreParams {
	
	#[structopt(long = "keystore-uri")]
	pub keystore_uri: Option<String>,
	
	#[structopt(long = "keystore-path", value_name = "PATH", parse(from_os_str))]
	pub keystore_path: Option<PathBuf>,
	
	#[structopt(
		long = "password-interactive",
		conflicts_with_all = &[ "password", "password-filename" ]
	)]
	pub password_interactive: bool,
	
	#[structopt(
		long = "password",
		parse(try_from_str = secret_string_from_str),
		conflicts_with_all = &[ "password-interactive", "password-filename" ]
	)]
	pub password: Option<SecretString>,
	
	#[structopt(
		long = "password-filename",
		value_name = "PATH",
		parse(from_os_str),
		conflicts_with_all = &[ "password-interactive", "password" ]
	)]
	pub password_filename: Option<PathBuf>,
}
pub fn secret_string_from_str(s: &str) -> std::result::Result<SecretString, String> {
	std::str::FromStr::from_str(s).map_err(|_| "Could not get SecretString".to_string())
}
impl KeystoreParams {
	
	
	
	pub fn keystore_config(&self, config_dir: &Path) -> Result<(Option<String>, KeystoreConfig)> {
		let password = if self.password_interactive {
			#[cfg(not(target_os = "unknown"))]
			{
				let password = input_keystore_password()?;
				Some(SecretString::new(password))
			}
			#[cfg(target_os = "unknown")]
			None
		} else if let Some(ref file) = self.password_filename {
			let password = fs::read_to_string(file)
				.map_err(|e| format!("{}", e))?;
			Some(SecretString::new(password))
		} else {
			self.password.clone()
		};
		let path = self
			.keystore_path
			.clone()
			.unwrap_or_else(|| config_dir.join(DEFAULT_KEYSTORE_CONFIG_PATH));
		Ok((self.keystore_uri.clone(), KeystoreConfig::Path { path, password }))
	}
	
	pub fn read_password(&self) -> error::Result<Option<SecretString>> {
		let (password_interactive, password) = (self.password_interactive, self.password.clone());
		let pass = if password_interactive {
			let password = rpassword::read_password_from_tty(Some("Key password: "))?;
			Some(SecretString::new(password))
		} else {
			password
		};
		Ok(pass)
	}
}
#[cfg(not(target_os = "unknown"))]
fn input_keystore_password() -> Result<String> {
	rpassword::read_password_from_tty(Some("Keystore password: "))
		.map_err(|e| format!("{:?}", e).into())
}