Skip to content
Permalink
Browse files

Merge pull request #338 from input-output-hk/jcli_no_panic

Jcli no panic
  • Loading branch information...
NicolasDP committed May 16, 2019
2 parents cc59cd0 + c4c115b commit 0602c6fdebcc8dea362693be255996db117ef331
@@ -1,5 +1,5 @@
use bech32::{Bech32, ToBase32};
use chain_addr::{AddressReadable, Discrimination, Kind};
use chain_crypto::bech32::Bech32 as _;
use chain_crypto::{AsymmetricKey, Ed25519Extended, PublicKey};
use jcli_app::utils::key_parser::parse_pub_key;
use structopt::StructOpt;
@@ -52,10 +52,14 @@ pub struct AccountArgs {
testing: bool,
}

custom_error! {pub Error
MultisigAddressNotSupported = "multisig addresses are not supported",
}

impl Address {
pub fn exec(self) {
pub fn exec(self) -> Result<(), Error> {
match self {
Address::Info(info_args) => address_info(&info_args.address),
Address::Info(info_args) => address_info(&info_args.address)?,
Address::Single(single_args) => {
if let Some(delegation) = single_args.delegation {
mk_delegation(single_args.key, single_args.testing, delegation)
@@ -65,10 +69,11 @@ impl Address {
}
Address::Account(account_args) => mk_account(account_args.key, account_args.testing),
}
Ok(())
}
}

fn address_info(address: &AddressReadable) {
fn address_info(address: &AddressReadable) -> Result<(), Error> {
let chain_addr::Address(discrimination, kind) = address.to_address();
match discrimination {
Discrimination::Production => {
@@ -80,14 +85,15 @@ fn address_info(address: &AddressReadable) {
}

match kind {
Kind::Single(single) => println!("public key: {}", print_pub_key(single)),
Kind::Account(account) => println!("account: {}", print_pub_key(account)),
Kind::Multisig(_) => unimplemented!(),
Kind::Single(single) => println!("public key: {}", single.to_bech32_str()),
Kind::Account(account) => println!("account: {}", account.to_bech32_str()),
Kind::Multisig(_) => return Err(Error::MultisigAddressNotSupported),
Kind::Group(pubk, groupk) => {
println!("public key: {}", print_pub_key(pubk));
println!("group key: {}", print_pub_key(groupk));
println!("public key: {}", pubk.to_bech32_str());
println!("group key: {}", groupk.to_bech32_str());
}
}
Ok(())
}

fn mk_single(s: PublicKey<Ed25519Extended>, testing: bool) {
@@ -135,8 +141,3 @@ where
let kind = f(s, d);
mk_address(discrimination, kind);
}

fn print_pub_key<A: AsymmetricKey>(pk: PublicKey<A>) -> Bech32 {
let hrp = A::PUBLIC_BECH32_HRP.to_string();
Bech32::new(hrp, pk.to_base32()).unwrap()
}
@@ -1,17 +1,8 @@
use chain_impl_mockchain::certificate::CertificateContent;
use jcli_app::utils::io;
use jormungandr_utils::certificate as cert_utils;
use std::io::{Read, Write};
use jcli_app::certificate::{self, Error};
use std::path::PathBuf;
use structopt::StructOpt;

custom_error! {pub Error
Encoding { source: cert_utils::Error } = "Invalid certificate",
CryptoEncoding { source: chain_crypto::bech32::Error } = "Invalid private key",
Io { source: std::io::Error } = "I/O Error",
NotStakePoolRegistration = "Invalid certificate, expecting a stake pool registration",
}

#[derive(StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub struct GetStakePoolId {
@@ -23,17 +14,10 @@ pub struct GetStakePoolId {

impl GetStakePoolId {
pub fn exec(self) -> Result<(), Error> {
let mut input = io::open_file_read(&self.input).unwrap();
let mut input_str = String::new();
input.read_to_string(&mut input_str)?;
let cert = cert_utils::deserialize_from_bech32(&input_str.trim())?;

let cert = certificate::read_cert(self.input)?;
match cert.content {
CertificateContent::StakePoolRegistration(s) => {
let id = s.to_id();
let mut f = io::open_file_write(&self.output).unwrap();
writeln!(f, "{}", id)?;
Ok(())
CertificateContent::StakePoolRegistration(stake_pool_info) => {
certificate::write_output(self.output, stake_pool_info.to_id())
}
_ => Err(Error::NotStakePoolRegistration),
}
@@ -1,3 +1,8 @@
use chain_impl_mockchain::certificate::Certificate as MockchainCertificate;
use jcli_app::utils::io;
use jormungandr_utils::certificate;
use std::fmt::Display;
use std::io::{BufRead, BufReader, Write};
use std::path::PathBuf;
use structopt::StructOpt;

@@ -8,11 +13,14 @@ mod new_stake_pool_registration;
mod sign;

custom_error! {pub Error
CannotCreatePoolRegistration { source: new_stake_pool_registration::Error } = "Cannot create new stake pool registration certificate",
CannotCreateKeyRegistration { source: new_stake_key_registration::Error } = "Cannot create new stake key registration certificate",
CannotCreateDelegation { source: new_stake_delegation::Error } = "Cannot create new stake delegation certificate",
CannotSignCertificate { source: sign::Error } = "Cannot sign certificate",
CannotGetStakePoolId { source: get_stake_pool_id::Error } = "Cannot get stake pool id from the certificate",
CertInvalid { source: certificate::Error } = "invalid certificate",
PrivKeyInvaild { source: chain_crypto::bech32::Error } = "invalid private key",
Io { source: std::io::Error } = "I/O Error",
NotStakePoolRegistration = "invalid certificate, expecting a stake pool registration",
InputInvalid { source: std::io::Error, path: PathBuf }
= @{{ let _ = source; format_args!("invalid input file path '{}'", path.display()) }},
OutputInvalid { source: std::io::Error, path: PathBuf }
= @{{ let _ = source; format_args!("invalid output file path '{}'", path.display()) }},
}

#[derive(StructOpt)]
@@ -78,3 +86,33 @@ impl Certificate {
Ok(())
}
}

fn read_cert(input: Option<PathBuf>) -> Result<MockchainCertificate, Error> {
let cert_str = read_input(input)?;
let cert = certificate::deserialize_from_bech32(cert_str.trim())?;
Ok(cert)
}

fn read_input(input: Option<PathBuf>) -> Result<String, Error> {
let reader = io::open_file_read(&input).map_err(|source| Error::InputInvalid {
source,
path: input.unwrap_or_default(),
})?;
let mut input_str = String::new();
BufReader::new(reader).read_line(&mut input_str)?;
Ok(input_str)
}

fn write_cert(output: Option<PathBuf>, cert: MockchainCertificate) -> Result<(), Error> {
let bech32 = certificate::serialize_to_bech32(&cert)?;
write_output(output, bech32)
}

fn write_output(output: Option<PathBuf>, data: impl Display) -> Result<(), Error> {
let mut writer = io::open_file_write(&output).map_err(|source| Error::OutputInvalid {
source,
path: output.unwrap_or_default(),
})?;
writeln!(writer, "{}", data)?;
Ok(())
}
@@ -1,17 +1,12 @@
use chain_crypto::{Ed25519Extended, PublicKey};
use chain_impl_mockchain::certificate::{self, CertificateContent, StakeDelegation as Delegation};
use jcli_app::utils::io;
use chain_impl_mockchain::certificate::{
Certificate, CertificateContent, StakeDelegation as Delegation,
};
use jcli_app::certificate::{self, Error};
use jcli_app::utils::key_parser::parse_pub_key;
use jormungandr_utils::certificate as cert_utils;
use std::io::Write;
use std::path::PathBuf;
use structopt::StructOpt;

custom_error! {pub Error
Encoding { source: cert_utils::Error } = "Invalid certificate",
Io { source: std::io::Error } = "I/O error",
}

#[derive(StructOpt)]
pub struct StakeDelegation {
/// the stake pool id
@@ -31,15 +26,10 @@ impl StakeDelegation {
stake_key_id: self.stake_id.into(),
pool_id: self.pool_id.into(),
};

let cert = certificate::Certificate {
let cert = Certificate {
content: CertificateContent::StakeDelegation(content),
signatures: vec![],
};

let bech32 = cert_utils::serialize_to_bech32(&cert)?;
let mut file = io::open_file_write(&self.output).unwrap();
writeln!(file, "{}", bech32)?;
Ok(())
certificate::write_cert(self.output, cert)
}
}
@@ -1,19 +1,12 @@
use chain_crypto::{Ed25519Extended, PublicKey};
use chain_impl_mockchain::certificate::{
self, CertificateContent, StakeKeyRegistration as Registration,
Certificate, CertificateContent, StakeKeyRegistration as Registration,
};
use jcli_app::utils::io;
use jcli_app::certificate::{self, Error};
use jcli_app::utils::key_parser::parse_pub_key;
use jormungandr_utils::certificate as cert_utils;
use std::io::Write;
use std::path::PathBuf;
use structopt::StructOpt;

custom_error! {pub Error
Encoding { source: cert_utils::Error } = "Invalid certificate",
Io { source: std::io::Error } = "I/O error",
}

#[derive(StructOpt)]
pub struct StakeKeyRegistration {
/// the delegation key
@@ -29,15 +22,10 @@ impl StakeKeyRegistration {
let content = Registration {
stake_key_id: self.key.into(),
};

let cert = certificate::Certificate {
let cert = Certificate {
content: CertificateContent::StakeKeyRegistration(content),
signatures: vec![],
};

let bech32 = cert_utils::serialize_to_bech32(&cert)?;
let mut file = io::open_file_write(&self.output).unwrap();
writeln!(file, "{}", bech32)?;
Ok(())
certificate::write_cert(self.output, cert)
}
}
@@ -1,21 +1,14 @@
use chain_crypto::{Curve25519_2HashDH, Ed25519Extended, PublicKey, SumEd25519_12};
use chain_impl_mockchain::{
certificate::{self, CertificateContent},
certificate::{Certificate, CertificateContent},
leadership::genesis::GenesisPraosLeader,
stake::{StakeKeyId, StakePoolInfo},
};
use jcli_app::utils::io;
use jcli_app::certificate::{self, Error};
use jcli_app::utils::key_parser::parse_pub_key;
use jormungandr_utils::certificate as cert_utils;
use std::io::Write;
use std::path::PathBuf;
use structopt::StructOpt;

custom_error! {pub Error
Encoding { source: cert_utils::Error } = "Invalid certificate",
Io { source: std::io::Error } = "I/O error",
}

#[derive(Debug, StructOpt)]
pub struct StakePoolRegistration {
/// serial code for the stake pool certificate
@@ -61,15 +54,10 @@ impl StakePoolRegistration {
vrf_public_key: self.vrf_key,
},
};

let cert = certificate::Certificate {
let cert = Certificate {
content: CertificateContent::StakePoolRegistration(content),
signatures: vec![],
};

let bech32 = cert_utils::serialize_to_bech32(&cert)?;
let mut file = io::open_file_write(&self.output).unwrap();
writeln!(file, "{}", bech32)?;
Ok(())
certificate::write_cert(self.output, cert)
}
}
@@ -1,20 +1,9 @@
use chain_crypto::{bech32, Ed25519Extended, SecretKey};
use chain_crypto::{bech32::Bech32, Ed25519Extended, SecretKey};
use chain_impl_mockchain::certificate::CertificateContent;
use jcli_app::utils::io;
use jormungandr_utils::certificate as cert_utils;
use std::{
fs,
io::{Read, Write},
path::PathBuf,
};
use jcli_app::certificate::{self, Error};
use std::path::PathBuf;
use structopt::StructOpt;

custom_error! {pub Error
Encoding { source: cert_utils::Error } = "Invalid certificate",
CryptoEncoding { source: chain_crypto::bech32::Error } = "Invalid private key",
Io { source: std::io::Error } = "I/O Error",
}

#[derive(StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub struct Sign {
@@ -30,13 +19,10 @@ pub struct Sign {

impl Sign {
pub fn exec(self) -> Result<(), Error> {
let mut input = io::open_file_read(&self.input).unwrap();
let mut input_str = String::new();
input.read_to_string(&mut input_str)?;
let mut cert = cert_utils::deserialize_from_bech32(&input_str.trim())?;
let key_str = fs::read_to_string(self.signing_key)?;
let private_key =
<SecretKey<Ed25519Extended> as bech32::Bech32>::try_from_bech32_str(&key_str.trim())?;
let mut cert = certificate::read_cert(self.input)?;
let key_str = certificate::read_input(Some(self.signing_key))?;
let private_key = SecretKey::<Ed25519Extended>::try_from_bech32_str(key_str.trim())?;

let signature = match &cert.content {
CertificateContent::StakeKeyRegistration(s) => s.make_certificate(&private_key),
CertificateContent::StakeKeyDeregistration(s) => s.make_certificate(&private_key),
@@ -45,9 +31,6 @@ impl Sign {
CertificateContent::StakePoolRetirement(s) => s.make_certificate(&private_key),
};
cert.signatures.push(signature);
let bech32 = cert_utils::serialize_to_bech32(&cert)?;
let mut f = io::open_file_write(&self.output).unwrap();
writeln!(f, "{}", bech32)?;
Ok(())
certificate::write_cert(self.output, cert)
}
}
@@ -1,8 +1,9 @@
use cardano::util::hex;
use chain_core::property::Deserialize as _;
use chain_impl_mockchain::message::Message as MockMessage;
use jcli_app::utils;
use std::io::Read;
use jcli_app::debug::Error;
use jcli_app::utils::{error::CustomErrorFiller, io};
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use structopt::StructOpt;

@@ -14,14 +15,20 @@ pub struct Message {
}

impl Message {
pub fn exec(self) {
let mut hex = String::new();
utils::io::open_file_read(&self.input)
.unwrap()
.read_to_string(&mut hex)
.unwrap();
let bytes = hex::decode(&hex).unwrap();
let message = MockMessage::deserialize(bytes.as_ref()).unwrap();
pub fn exec(self) -> Result<(), Error> {
let reader = io::open_file_read(&self.input).map_err(|source| Error::InputInvalid {
source,
path: self.input.unwrap_or_default(),
})?;
let mut hex_str = String::new();
BufReader::new(reader).read_line(&mut hex_str)?;
let bytes = hex::decode(hex_str.trim())?;
let message =
MockMessage::deserialize(bytes.as_ref()).map_err(|source| Error::MessageMalformed {
source,
filler: CustomErrorFiller,
})?;
println!("{:#?}", message);
Ok(())
}
}
Oops, something went wrong.

0 comments on commit 0602c6f

Please sign in to comment.
You can’t perform that action at this time.