Skip to content

Commit

Permalink
Add insert key CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-tsurko committed Aug 16, 2024
1 parent 2602558 commit 4e9bd2c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 38 deletions.
4 changes: 2 additions & 2 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::service::EthConfiguration;
use self::validator::ValidateCmd;
use self::validator::ValidatorCmd;

mod validator;

Expand Down Expand Up @@ -73,5 +73,5 @@ pub enum Subcommand {
RuntimeVersion,

/// Validator related commands.
Validate(ValidateCmd),
Validator(ValidatorCmd),
}
95 changes: 77 additions & 18 deletions node/src/cli/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sc_keystore::LocalKeystore;
use sc_service::config::{BasePath, KeystoreConfig};
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_keystore::{KeystoreExt, KeystorePtr};
use sp_session::SessionKeys;

Expand All @@ -13,7 +14,7 @@ use crate::service::FullClient;

/// Validator related commands.
#[derive(Debug, clap::Parser)]
pub struct ValidateCmd {
pub struct ValidatorCmd {
#[allow(missing_docs)]
#[command(subcommand)]
subcommand: Option<ValidateSubcommands>,
Expand All @@ -23,7 +24,7 @@ pub struct ValidateCmd {
pub shared_params: SharedParams,
}

impl ValidateCmd {
impl ValidatorCmd {
pub fn run<Cli: SubstrateCli>(&self, cli: &Cli, client: &FullClient) -> Result<(), Error> {
match &self.subcommand {
Some(sc) => sc.run(cli, client),
Expand All @@ -32,7 +33,7 @@ impl ValidateCmd {
}
}

impl CliConfiguration for ValidateCmd {
impl CliConfiguration for ValidatorCmd {
fn shared_params(&self) -> &SharedParams {
&self.shared_params
}
Expand All @@ -46,9 +47,8 @@ pub enum ValidateSubcommands {
/// Decode session keys.
DecodeSessionKeys(DecodeSessionKeysCmd),

// /// Insert session keys into the keystore.
// InsertSessionKeys(InsertSessionKeysCmd),

/// Insert session keys into the keystore.
InsertKey(InsertKeyCmd),
// /// Set session keys.
// SetSessionKeys(SetSessionKeysCmd),

Expand All @@ -62,6 +62,7 @@ impl ValidateSubcommands {
match self {
Self::GenerateSessionKeys(cmd) => cmd.run(cli, client),
Self::DecodeSessionKeys(cmd) => cmd.run(),
Self::InsertKey(cmd) => cmd.run(cli),
}
}
}
Expand All @@ -81,18 +82,7 @@ pub struct GenerateSessionKeysCmd {
impl GenerateSessionKeysCmd {
/// Run the command
pub fn run<Cli: SubstrateCli>(&self, cli: &Cli, client: &FullClient) -> Result<(), Error> {
let base_path = self
.shared_params
.base_path()?
.unwrap_or_else(|| BasePath::from_project("", "", &Cli::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: KeystorePtr = match self.keystore_params.keystore_config(&config_dir)? {
KeystoreConfig::Path { path, password } => LocalKeystore::open(path, password)?.into(),
_ => unreachable!("keystore_config always returns path and password; qed"),
};

let keystore = init_keystore(cli, &self.shared_params, &self.keystore_params)?;
let best_block_hash = client.info().best_hash;
let mut runtime_api = client.runtime_api();

Expand All @@ -112,6 +102,75 @@ impl CliConfiguration for GenerateSessionKeysCmd {
fn shared_params(&self) -> &SharedParams {
&self.shared_params
}

fn keystore_params(&self) -> Option<&KeystoreParams> {
Some(&self.keystore_params)
}
}

/// `insert-key` subcommand.
#[derive(Debug, Clone, Parser)]
pub struct InsertKeyCmd {
#[allow(missing_docs)]
#[clap(flatten)]
pub shared_params: SharedParams,

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

/// Key type.
#[arg(long)]
pub key_type: String,

/// SURI.
#[arg(long, default_value_t)]
pub suri: String,

/// Public key.
#[arg(long)]
pub public: Bytes,
}

impl InsertKeyCmd {
/// Run the command
pub fn run<Cli: SubstrateCli>(&self, cli: &Cli) -> Result<(), Error> {
let keystore = init_keystore(cli, &self.shared_params, &self.keystore_params)?;
let key_type = self.key_type.as_str().try_into().map_err(|_| Error::KeyTypeInvalid)?;
keystore
.insert(key_type, &self.suri, &self.public[..])
.map_err(|_| Error::KeystoreOperation)?;

Ok(())
}
}

impl CliConfiguration for InsertKeyCmd {
fn shared_params(&self) -> &SharedParams {
&self.shared_params
}

fn keystore_params(&self) -> Option<&KeystoreParams> {
Some(&self.keystore_params)
}
}

fn init_keystore<Cli: SubstrateCli>(
cli: &Cli,
shared_params: &SharedParams,
keystore_params: &KeystoreParams,
) -> Result<KeystorePtr, Error> {
let base_path = shared_params
.base_path()?
.unwrap_or_else(|| BasePath::from_project("", "", &Cli::executable_name()));
let chain_id = shared_params.chain_id(shared_params.is_dev());
let chain_spec = cli.load_spec(&chain_id)?;
let config_dir = base_path.config_dir(chain_spec.id());

match keystore_params.keystore_config(&config_dir)? {
KeystoreConfig::Path { path, password } => Ok(LocalKeystore::open(path, password)?.into()),
_ => unreachable!("keystore_config always returns path and password; qed"),
}
}

/// `decode-session-keys` subcommand.
Expand Down
19 changes: 1 addition & 18 deletions node/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
// This file is part of Substrate.

// Copyright (C) 2017-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.

use futures::TryFutureExt;
// Substrate
use sc_cli::{ChainSpec, SubstrateCli};
Expand Down Expand Up @@ -275,7 +258,7 @@ pub fn run() -> sc_cli::Result<()> {

Ok(())
},
Some(Subcommand::Validate(cmd)) => {
Some(Subcommand::Validator(cmd)) => {
let runner = cli.create_runner(cmd)?;
log::set_max_level(log::LevelFilter::Off);
runner.sync_run(|mut config| {
Expand Down

0 comments on commit 4e9bd2c

Please sign in to comment.