sc_cli/commands/
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//! Key related CLI utilities
19
20use super::{
21	generate::GenerateCmd, generate_node_key::GenerateNodeKeyCmd, insert_key::InsertKeyCmd,
22	inspect_key::InspectKeyCmd, inspect_node_key::InspectNodeKeyCmd,
23};
24use crate::{Error, SubstrateCli};
25
26/// Key utilities for the cli.
27#[derive(Debug, clap::Subcommand)]
28pub enum KeySubcommand {
29	/// Generate a random node key, write it to a file or stdout and write the
30	/// corresponding peer-id to stderr
31	GenerateNodeKey(GenerateNodeKeyCmd),
32
33	/// Generate a random account
34	Generate(GenerateCmd),
35
36	/// Gets a public key and a SS58 address from the provided Secret URI
37	Inspect(InspectKeyCmd),
38
39	/// Load a node key from a file or stdin and print the corresponding peer-id
40	InspectNodeKey(InspectNodeKeyCmd),
41
42	/// Insert a key to the keystore of a node.
43	Insert(InsertKeyCmd),
44}
45
46impl KeySubcommand {
47	/// run the key subcommands
48	pub fn run<C: SubstrateCli>(&self, cli: &C) -> Result<(), Error> {
49		match self {
50			KeySubcommand::GenerateNodeKey(cmd) => {
51				let chain_spec = cli.load_spec(cmd.chain.as_deref().unwrap_or(""))?;
52				cmd.run(chain_spec.id(), &C::executable_name())
53			},
54			KeySubcommand::Generate(cmd) => cmd.run(),
55			KeySubcommand::Inspect(cmd) => cmd.run(),
56			KeySubcommand::Insert(cmd) => cmd.run(cli),
57			KeySubcommand::InspectNodeKey(cmd) => cmd.run(),
58		}
59	}
60}