Skip to main content

soil_cli/commands/
key.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Key related CLI utilities
8
9use super::{
10	generate::GenerateCmd, generate_node_key::GenerateNodeKeyCmd, insert_key::InsertKeyCmd,
11	inspect_key::InspectKeyCmd, inspect_node_key::InspectNodeKeyCmd,
12};
13use crate::{Error, SubstrateCli};
14
15/// Key utilities for the cli.
16#[derive(Debug, clap::Subcommand)]
17pub enum KeySubcommand {
18	/// Generate a random node key, write it to a file or stdout and write the
19	/// corresponding peer-id to stderr
20	GenerateNodeKey(GenerateNodeKeyCmd),
21
22	/// Generate a random account
23	Generate(GenerateCmd),
24
25	/// Gets a public key and a SS58 address from the provided Secret URI
26	Inspect(InspectKeyCmd),
27
28	/// Load a node key from a file or stdin and print the corresponding peer-id
29	InspectNodeKey(InspectNodeKeyCmd),
30
31	/// Insert a key to the keystore of a node.
32	Insert(InsertKeyCmd),
33}
34
35impl KeySubcommand {
36	/// run the key subcommands
37	pub fn run<C: SubstrateCli>(&self, cli: &C) -> Result<(), Error> {
38		match self {
39			KeySubcommand::GenerateNodeKey(cmd) => {
40				let chain_spec = cli.load_spec(cmd.chain.as_deref().unwrap_or(""))?;
41				cmd.run(chain_spec.id(), &C::executable_name())
42			},
43			KeySubcommand::Generate(cmd) => cmd.run(),
44			KeySubcommand::Inspect(cmd) => cmd.run(),
45			KeySubcommand::Insert(cmd) => cmd.run(cli),
46			KeySubcommand::InspectNodeKey(cmd) => cmd.run(),
47		}
48	}
49}