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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// This file is part of Substrate.

// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Implementation of the `insert` subcommand

use crate::{
	utils, with_crypto_scheme, CryptoSchemeFlag, Error, KeystoreParams, SharedParams, SubstrateCli,
};
use sc_keystore::LocalKeystore;
use sc_service::config::{BasePath, KeystoreConfig};
use sp_core::crypto::{KeyTypeId, SecretString};
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use std::{convert::TryFrom, sync::Arc};
use structopt::StructOpt;

/// The `insert` command
#[derive(Debug, StructOpt, Clone)]
#[structopt(name = "insert", about = "Insert a key to the keystore of a node.")]
pub struct InsertKeyCmd {
	/// The secret key URI.
	/// If the value is a file, the file content is used as URI.
	/// If not given, you will be prompted for the URI.
	#[structopt(long)]
	suri: Option<String>,

	/// Key type, examples: "gran", or "imon"
	#[structopt(long)]
	key_type: String,

	#[allow(missing_docs)]
	#[structopt(flatten)]
	pub shared_params: SharedParams,

	#[allow(missing_docs)]
	#[structopt(flatten)]
	pub keystore_params: KeystoreParams,

	#[allow(missing_docs)]
	#[structopt(flatten)]
	pub crypto_scheme: CryptoSchemeFlag,
}

impl InsertKeyCmd {
	/// Run the command
	pub fn run<C: SubstrateCli>(&self, cli: &C) -> Result<(), Error> {
		let suri = utils::read_uri(self.suri.as_ref())?;
		let base_path = self
			.shared_params
			.base_path()
			.unwrap_or_else(|| BasePath::from_project("", "", &C::executable_name()));
		let chain_id = self.shared_params.chain_id(self.shared_params.is_dev());
		let chain_spec = cli.load_spec(&chain_id)?;
		let config_dir = base_path.config_dir(chain_spec.id());

		let (keystore, public) = match self.keystore_params.keystore_config(&config_dir)? {
			(_, KeystoreConfig::Path { path, password }) => {
				let public = with_crypto_scheme!(
					self.crypto_scheme.scheme,
					to_vec(&suri, password.clone())
				)?;
				let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::open(path, password)?);
				(keystore, public)
			},
			_ => unreachable!("keystore_config always returns path and password; qed"),
		};

		let key_type =
			KeyTypeId::try_from(self.key_type.as_str()).map_err(|_| Error::KeyTypeInvalid)?;

		SyncCryptoStore::insert_unknown(&*keystore, key_type, &suri, &public[..])
			.map_err(|_| Error::KeyStoreOperation)?;

		Ok(())
	}
}

fn to_vec<P: sp_core::Pair>(uri: &str, pass: Option<SecretString>) -> Result<Vec<u8>, Error> {
	let p = utils::pair_from_suri::<P>(uri, pass)?;
	Ok(p.public().as_ref().to_vec())
}

#[cfg(test)]
mod tests {
	use super::*;
	use sc_service::{ChainSpec, ChainType, GenericChainSpec, NoExtension};
	use sp_core::{sr25519::Pair, Pair as _, Public};
	use structopt::StructOpt;
	use tempfile::TempDir;

	struct Cli;

	impl SubstrateCli for Cli {
		fn impl_name() -> String {
			"test".into()
		}

		fn impl_version() -> String {
			"2.0".into()
		}

		fn description() -> String {
			"test".into()
		}

		fn support_url() -> String {
			"test.test".into()
		}

		fn copyright_start_year() -> i32 {
			2021
		}

		fn author() -> String {
			"test".into()
		}

		fn native_runtime_version(_: &Box<dyn ChainSpec>) -> &'static sp_version::RuntimeVersion {
			unimplemented!("Not required in tests")
		}

		fn load_spec(&self, _: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
			Ok(Box::new(GenericChainSpec::from_genesis(
				"test",
				"test_id",
				ChainType::Development,
				|| unimplemented!("Not required in tests"),
				Vec::new(),
				None,
				None,
				None,
				NoExtension::None,
			)))
		}
	}

	#[test]
	fn insert_with_custom_base_path() {
		let path = TempDir::new().unwrap();
		let path_str = format!("{}", path.path().display());
		let (key, uri, _) = Pair::generate_with_phrase(None);

		let inspect = InsertKeyCmd::from_iter(&[
			"insert-key",
			"-d",
			&path_str,
			"--key-type",
			"test",
			"--suri",
			&uri,
		]);
		assert!(inspect.run(&Cli).is_ok());

		let keystore =
			LocalKeystore::open(path.path().join("chains").join("test_id").join("keystore"), None)
				.unwrap();
		assert!(keystore.has_keys(&[(key.public().to_raw_vec(), KeyTypeId(*b"test"))]));
	}
}