sc_cli/commands/
insert_key.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Implementation of the `insert` subcommand
19
20use crate::{
21	utils, with_crypto_scheme, CryptoScheme, Error, KeystoreParams, SharedParams, SubstrateCli,
22};
23use clap::Parser;
24use sc_keystore::LocalKeystore;
25use sc_service::config::{BasePath, KeystoreConfig};
26use sp_core::crypto::{KeyTypeId, SecretString};
27use sp_keystore::KeystorePtr;
28
29/// The `insert` command
30#[derive(Debug, Clone, Parser)]
31#[command(name = "insert", about = "Insert a key to the keystore of a node.")]
32pub struct InsertKeyCmd {
33	/// The secret key URI.
34	/// If the value is a file, the file content is used as URI.
35	/// If not given, you will be prompted for the URI.
36	#[arg(long)]
37	suri: Option<String>,
38
39	/// Key type, examples: "gran", or "imon".
40	#[arg(long)]
41	key_type: String,
42
43	#[allow(missing_docs)]
44	#[clap(flatten)]
45	pub shared_params: SharedParams,
46
47	#[allow(missing_docs)]
48	#[clap(flatten)]
49	pub keystore_params: KeystoreParams,
50
51	/// The cryptography scheme that should be used to generate the key out of the given URI.
52	#[arg(long, value_name = "SCHEME", value_enum, ignore_case = true)]
53	pub scheme: CryptoScheme,
54}
55
56impl InsertKeyCmd {
57	/// Run the command
58	pub fn run<C: SubstrateCli>(&self, cli: &C) -> Result<(), Error> {
59		let suri = utils::read_uri(self.suri.as_ref())?;
60		let base_path = self
61			.shared_params
62			.base_path()?
63			.unwrap_or_else(|| BasePath::from_project("", "", &C::executable_name()));
64		let chain_id = self.shared_params.chain_id(self.shared_params.is_dev());
65		let chain_spec = cli.load_spec(&chain_id)?;
66		let config_dir = base_path.config_dir(chain_spec.id());
67
68		let (keystore, public) = match self.keystore_params.keystore_config(&config_dir)? {
69			KeystoreConfig::Path { path, password } => {
70				let public = with_crypto_scheme!(self.scheme, to_vec(&suri, password.clone()))?;
71				let keystore: KeystorePtr = LocalKeystore::open(path, password)?.into();
72				(keystore, public)
73			},
74			_ => unreachable!("keystore_config always returns path and password; qed"),
75		};
76
77		let key_type =
78			KeyTypeId::try_from(self.key_type.as_str()).map_err(|_| Error::KeyTypeInvalid)?;
79
80		keystore
81			.insert(key_type, &suri, &public[..])
82			.map_err(|_| Error::KeystoreOperation)?;
83
84		Ok(())
85	}
86}
87
88fn to_vec<P: sp_core::Pair>(uri: &str, pass: Option<SecretString>) -> Result<Vec<u8>, Error> {
89	let p = utils::pair_from_suri::<P>(uri, pass)?;
90	Ok(p.public().as_ref().to_vec())
91}
92
93#[cfg(test)]
94mod tests {
95	use super::*;
96	use sc_service::{ChainSpec, ChainType, GenericChainSpec, NoExtension};
97	use sp_core::{sr25519::Pair, ByteArray, Pair as _};
98	use sp_keystore::Keystore;
99	use tempfile::TempDir;
100
101	struct Cli;
102
103	impl SubstrateCli for Cli {
104		fn impl_name() -> String {
105			"test".into()
106		}
107
108		fn impl_version() -> String {
109			"2.0".into()
110		}
111
112		fn description() -> String {
113			"test".into()
114		}
115
116		fn support_url() -> String {
117			"test.test".into()
118		}
119
120		fn copyright_start_year() -> i32 {
121			2021
122		}
123
124		fn author() -> String {
125			"test".into()
126		}
127
128		fn load_spec(&self, _: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
129			let builder =
130				GenericChainSpec::<NoExtension, ()>::builder(Default::default(), NoExtension::None);
131			Ok(Box::new(
132				builder
133					.with_name("test")
134					.with_id("test_id")
135					.with_chain_type(ChainType::Development)
136					.with_genesis_config_patch(Default::default())
137					.build(),
138			))
139		}
140	}
141
142	#[test]
143	fn insert_with_custom_base_path() {
144		let path = TempDir::new().unwrap();
145		let path_str = format!("{}", path.path().display());
146		let (key, uri, _) = Pair::generate_with_phrase(None);
147
148		let inspect = InsertKeyCmd::parse_from(&[
149			"insert-key",
150			"-d",
151			&path_str,
152			"--key-type",
153			"test",
154			"--suri",
155			&uri,
156			"--scheme=sr25519",
157		]);
158		assert!(inspect.run(&Cli).is_ok());
159
160		let keystore =
161			LocalKeystore::open(path.path().join("chains").join("test_id").join("keystore"), None)
162				.unwrap();
163		assert!(keystore.has_keys(&[(key.public().to_raw_vec(), KeyTypeId(*b"test"))]));
164	}
165}