Skip to content

Commit

Permalink
CLI to sign data using authority wdkg key (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj authored Oct 6, 2023
1 parent d12e316 commit 00126b9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.4.5"
version = "0.4.8"
authors = ["Webb Technologies Inc."]
edition = "2021"
license = "Unlicense"
Expand Down
3 changes: 3 additions & 0 deletions standalone/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ pub enum Subcommand {

/// Db meta columns information.
FrontierDb(fc_cli::FrontierDbCmd),

/// DKG signer
DKGSigner(crate::signer_cli::DKGSignerCmd),
}
1 change: 1 addition & 0 deletions standalone/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub fn run() -> sc_cli::Result<()> {

match &cli.subcommand {
Some(Subcommand::Key(cmd)) => cmd.run(&cli),
Some(Subcommand::DKGSigner(cmd)) => cmd.run(&cli),
Some(Subcommand::DKGKey(cmd)) => cmd.run(&cli),
Some(Subcommand::BuildSpec(cmd)) => {
let runner = cli.create_runner(cmd)?;
Expand Down
1 change: 1 addition & 0 deletions standalone/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub mod distributions;
pub mod eth;
pub mod rpc;
pub mod service;
pub mod signer_cli;
pub mod testnet_fixtures;
pub mod utils;
1 change: 1 addition & 0 deletions standalone/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod command;
mod distributions;
mod eth;
mod rpc;
mod signer_cli;
mod testnet_fixtures;
mod utils;

Expand Down
76 changes: 76 additions & 0 deletions standalone/node/src/signer_cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#![allow(clippy::all)]
// Copyright 2023 Webb Technologies Inc.
//
// 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.

//! Signer module to sign data with wdkg keytype in keystore
use parity_scale_codec::{Decode, Encode};
use sc_cli::{
utils, with_crypto_scheme, CryptoScheme, Error, KeystoreParams, Result as CliResult,
SharedParams, SubstrateCli,
};
use sc_keystore::LocalKeystore;
use sc_service::{config::KeystoreConfig, BasePath};
use sp_keystore::KeystorePtr;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::fmt::Debug;

/// The `chain-info` subcommand used to output db meta columns information.
#[derive(Debug, Clone, clap::Parser)]
pub struct DKGSignerCmd {
/// Key type, examples: "gran", or "imon".
#[arg(long)]
data: String,

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

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

impl DKGSignerCmd {
/// Run the command
pub fn run<C: SubstrateCli>(&self, cli: &C) -> Result<(), Error> {
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 = match self.keystore_params.keystore_config(&config_dir)? {
KeystoreConfig::Path { path, password } => {
let keystore: KeystorePtr = LocalKeystore::open(path, password)?.into();
keystore
},
_ => unreachable!("keystore_config always returns path and password; qed"),
};

let key_type = dkg_runtime_primitives::KEY_TYPE;

let maybe_public = keystore.ecdsa_public_keys(key_type);
let public = maybe_public.first().unwrap();

let signature = keystore
.ecdsa_sign(key_type, &public, &self.data.as_bytes())
.map_err(|_| Error::KeystoreOperation)?;

println!("Signature : {:?}", signature);

Ok(())
}
}

0 comments on commit 00126b9

Please sign in to comment.