Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #242 from cryptiumlabs/adrian/kms_upgrade
Browse files Browse the repository at this point in the history
Add Ledger init commands
  • Loading branch information
tarcieri committed Apr 22, 2019
2 parents 2aa623a + e1bf799 commit e75e47f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/commands/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! Subcommands of the `tmkms` command-line application

use crate::chain;
use crate::config::KmsConfig;
use abscissa::{Callable, GlobalConfig};
use std::process;
use tendermint::amino_types::vote::{SignVoteRequest, Vote};
use tendermint::amino_types::{SignableMsg, SignedMsgType};

#[derive(Debug, Options)]
pub enum LedgerCommand {
#[options(help = "initialise the height/round/step")]
Initialise(InitCommand),
}

impl_command!(LedgerCommand);

impl Callable for LedgerCommand {
fn call(&self) {
match self {
LedgerCommand::Initialise(init) => init.call(),
}
}
}

impl LedgerCommand {
pub(super) fn config_path(&self) -> Option<&String> {
match self {
LedgerCommand::Initialise(init) => init.config.as_ref(),
}
}
}

#[derive(Debug, Options)]
pub struct InitCommand {
#[options(short = "c", long = "config")]
pub config: Option<String>,

#[options(short = "h", long = "height")]
pub height: Option<i64>,

#[options(short = "r", long = "round")]
pub round: Option<i64>,
}

impl Callable for InitCommand {
fn call(&self) {
let config = KmsConfig::get_global();

chain::load_config(&config).unwrap_or_else(|e| {
status_err!("error loading configuration: {}", e);
process::exit(1);
});

let chain_id = config.validator[0].chain_id;
let registry = chain::REGISTRY.get();
let chain = registry.get_chain(&chain_id).unwrap();

let mut vote = Vote::default();
vote.height = self.height.unwrap();
vote.round = self.round.unwrap();
vote.vote_type = SignedMsgType::Proposal.to_u32();
println!("{:?}", vote);
let sign_vote_req = SignVoteRequest { vote: Some(vote) };
let mut to_sign = vec![];
sign_vote_req
.sign_bytes(config.validator[0].chain_id, &mut to_sign)
.unwrap();

let _sig = chain.keyring.sign_ed25519(None, &to_sign).unwrap();

println!(
"Successfully called the init command with height {}, and round {}",
self.height.unwrap(),
self.round.unwrap()
);
}
}
12 changes: 12 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mod version;
mod yubihsm;
#[cfg(feature = "yubihsm")]
pub use self::yubihsm::YubihsmCommand;
#[cfg(feature = "ledgertm")]
mod ledger;
#[cfg(feature = "ledgertm")]
pub use self::ledger::LedgerCommand;

pub use self::{
help::HelpCommand, keygen::KeygenCommand, start::StartCommand, version::VersionCommand,
Expand All @@ -34,6 +38,10 @@ pub enum KmsCommand {
#[cfg(feature = "yubihsm")]
#[options(help = "subcommands for YubiHSM2")]
Yubihsm(YubihsmCommand),

#[cfg(feature = "ledgertm")]
#[options(help = "subcommands for Ledger")]
Ledger(LedgerCommand),
}

// TODO: refactor abscissa internally so this is all part of the proc macro
Expand All @@ -59,6 +67,8 @@ impl LoadConfig<KmsConfig> for KmsCommand {
KmsCommand::Start(run) => run.config.as_ref(),
#[cfg(feature = "yubihsm")]
KmsCommand::Yubihsm(yubihsm) => yubihsm.config_path(),
#[cfg(feature = "ledgertm")]
KmsCommand::Ledger(ledger) => ledger.config_path(),
_ => return None,
};

Expand All @@ -84,6 +94,8 @@ impl Callable for KmsCommand {
KmsCommand::Version(version) => version.call(),
#[cfg(feature = "yubihsm")]
KmsCommand::Yubihsm(yubihsm) => yubihsm.call(),
#[cfg(feature = "ledgertm")]
KmsCommand::Ledger(ledger) => ledger.call(),
}
}
}

0 comments on commit e75e47f

Please sign in to comment.