Skip to content

Commit

Permalink
feat: improve handling of payment key argument and add documentation …
Browse files Browse the repository at this point in the history
…for some other fields (#359)
  • Loading branch information
LGLO authored Jan 10, 2025
1 parent 3632997 commit 964b8ec
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
9 changes: 4 additions & 5 deletions toolkit/cli/smart-contracts-commands/src/d_parameter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::PaymentFilePath;
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::d_param::upsert_d_param;
use sidechain_domain::DParameter;
use sidechain_domain::UtxoId;

use crate::read_private_key_from_file;

#[derive(Clone, Debug, clap::Parser)]
pub struct UpsertDParameterCmd {
#[clap(flatten)]
Expand All @@ -13,15 +12,15 @@ pub struct UpsertDParameterCmd {
permissioned_candidates_count: u16,
#[arg(long)]
registered_candidates_count: u16,
#[arg(long, short('k'))]
payment_key_file: String,
#[clap(flatten)]
payment_key_file: PaymentFilePath,
#[arg(long, short('c'))]
genesis_utxo: UtxoId,
}

impl UpsertDParameterCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = read_private_key_from_file(&self.payment_key_file)?;
let payment_key = self.payment_key_file.read_key()?;
let d_param = DParameter {
num_permissioned_candidates: self.permissioned_candidates_count,
num_registered_candidates: self.registered_candidates_count,
Expand Down
16 changes: 7 additions & 9 deletions toolkit/cli/smart-contracts-commands/src/governance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::read_private_key_from_file;
use crate::PaymentFilePath;
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::{
await_tx::FixedDelayRetries, init_governance::run_init_governance,
Expand Down Expand Up @@ -31,17 +31,16 @@ pub struct InitGovernanceCmd {
/// Governance authority hash to be set.
#[arg(long, short = 'g')]
governance_authority: MainchainAddressHash,
/// Path to the Cardano Payment Key file.
#[arg(long, short = 'k')]
payment_key_file: String,
#[clap(flatten)]
payment_key_file: PaymentFilePath,
/// Genesis UTXO of the new chain, it will be spent durning initialization. If not set, then one will be selected from UTXOs of the payment key.
#[arg(long, short = 'c')]
genesis_utxo: Option<UtxoId>,
}

impl InitGovernanceCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = read_private_key_from_file(&self.payment_key_file)?;
let payment_key = self.payment_key_file.read_key()?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;

run_init_governance(
Expand All @@ -63,17 +62,16 @@ pub struct UpdateGovernanceCmd {
/// Governance authority hash to be set.
#[arg(long, short = 'g')]
new_governance_authority: MainchainAddressHash,
/// Path to the Cardano Payment Key file.
#[arg(long, short = 'k')]
payment_key_file: String,
#[clap(flatten)]
payment_key_file: PaymentFilePath,
/// Genesis UTXO of the chain
#[arg(long, short = 'c')]
genesis_utxo: UtxoId,
}

impl UpdateGovernanceCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = read_private_key_from_file(&self.payment_key_file)?;
let payment_key = self.payment_key_file.read_key()?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;

run_update_governance(
Expand Down
24 changes: 20 additions & 4 deletions toolkit/cli/smart-contracts-commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,38 @@ impl SmartContractsCmd {
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CardanoKeyFileContent {
r#type: String,
cbor_hex: String,
}

pub(crate) fn read_private_key_from_file(path: &str) -> CmdResult<MainchainPrivateKey> {
#[derive(Clone, Debug, clap::Parser)]
pub(crate) struct PaymentFilePath {
/// Path to the Cardano Signing Key file used sign transaction(s) and pay for them
#[arg(long, short = 'k')]
payment_key_file: String,
}

impl PaymentFilePath {
pub(crate) fn read_key(&self) -> CmdResult<MainchainPrivateKey> {
let key = Self::read_and_parse(&self.payment_key_file)
.map_err(|e| format!("Could not read private key file. Reason: {}", e))?;
Ok(key)
}
fn read_and_parse(path: &str) -> CmdResult<MainchainPrivateKey> {
let file_content_str = String::from_utf8(std::fs::read(path)?)?;
let file_content = serde_json::from_str::<CardanoKeyFileContent>(&file_content_str)?;
let key_type = file_content.r#type;
if !key_type.contains("SigningKey") {
return Err(
format!("Unsupported key type: {}. Expected a signing key", key_type).into()
);
}
let key_hex = (file_content.cbor_hex.strip_prefix("5820"))
.ok_or("CBOR prefix missing in payment key".to_string())?;
let key_bytes = (hex::decode(key_hex)?.try_into())
.map_err(|_| format!("{} is not the valid lengh of 32", key_hex))?;
Ok(MainchainPrivateKey(key_bytes))
}
let key = read_and_parse(path)
.map_err(|e| format!("Could not read private key file. Reason: {}", e))?;
Ok(key)
}

// Parses public keys in formatted as SIDECHAIN_KEY:AURA_KEY:GRANDPA_KEY
Expand Down
22 changes: 15 additions & 7 deletions toolkit/cli/smart-contracts-commands/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{parse_partnerchain_public_keys, read_private_key_from_file};
use crate::{parse_partnerchain_public_keys, PaymentFilePath};
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::csl::MainchainPrivateKeyExt;
use partner_chains_cardano_offchain::{
Expand All @@ -14,30 +14,36 @@ use sidechain_domain::{
pub struct RegisterCmd {
#[clap(flatten)]
common_arguments: crate::CommonArguments,
/// Genesis UTXO of the partner-chain
#[arg(long)]
genesis_utxo: UtxoId,
/// UTXO that will be spend when executing registration transaction, part of the registration message
#[arg(long)]
registration_utxo: UtxoId,
#[arg(long)]
payment_key_file: String,
#[clap(flatten)]
payment_key_file: PaymentFilePath,
#[arg(
long,
value_name = "PARTNERCHAIN_KEY:AURA_KEY:GRANDPA_KEY",
alias = "sidechain-public-keys",
value_parser = parse_partnerchain_public_keys
)]
/// Colon separated hex strings representing bytes of the Sidechain, Aura and Grandpa public keys
partner_chain_public_keys: PermissionedCandidateData,
/// Hex string of bytes of the registration message signature by partner-chain key, obtained by 'registration-signatures' command
#[arg(long, alias = "sidechain-signature")]
partner_chain_signature: SidechainSignature,
/// Hex string representing bytes of the Stake Pool Verification Key
#[arg(long)]
spo_public_key: MainchainPublicKey,
/// Hex string of bytes of the registration message signature by main chain key, obtained by 'registration-signatures' command
#[arg(long)]
spo_signature: MainchainSignature,
}

impl RegisterCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = read_private_key_from_file(&self.payment_key_file)?;
let payment_key = self.payment_key_file.read_key()?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;
let candidate_registration = CandidateRegistration {
stake_ownership: AdaBasedStaking {
Expand Down Expand Up @@ -69,17 +75,19 @@ impl RegisterCmd {
pub struct DeregisterCmd {
#[clap(flatten)]
common_arguments: crate::CommonArguments,
/// Genesis UTXO of the partner-chain
#[arg(long)]
genesis_utxo: UtxoId,
#[arg(long)]
payment_key_file: String,
#[clap(flatten)]
payment_key_file: PaymentFilePath,
/// Hex string representing bytes of the Stake Pool Verification Key
#[arg(long)]
spo_public_key: MainchainPublicKey,
}

impl DeregisterCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_signing_key = read_private_key_from_file(&self.payment_key_file)?;
let payment_signing_key = self.payment_key_file.read_key()?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;

run_deregister(
Expand Down
15 changes: 7 additions & 8 deletions toolkit/cli/smart-contracts-commands/src/reserve.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::PaymentFilePath;
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::{
await_tx::FixedDelayRetries,
Expand Down Expand Up @@ -29,17 +30,16 @@ impl ReserveCmd {
pub struct InitReserveCmd {
#[clap(flatten)]
common_arguments: crate::CommonArguments,
/// Path to the Cardano Payment Key file.
#[arg(long, short('k'))]
payment_key_file: String,
#[clap(flatten)]
payment_key_file: PaymentFilePath,
/// Genesis UTXO of the partner-chain.
#[arg(long, short('c'))]
genesis_utxo: UtxoId,
}

impl InitReserveCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = crate::read_private_key_from_file(&self.payment_key_file)?;
let payment_key = self.payment_key_file.read_key()?;
let ogmios_client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;
let _ = init_reserve_management(
self.genesis_utxo,
Expand All @@ -56,9 +56,8 @@ impl InitReserveCmd {
pub struct CreateReserveCmd {
#[clap(flatten)]
common_arguments: crate::CommonArguments,
/// Path to the Cardano Payment Key file.
#[arg(long, short('k'))]
payment_key_file: String,
#[clap(flatten)]
payment_key_file: PaymentFilePath,
/// Genesis UTXO of the partner-chain.
#[arg(long, short('c'))]
genesis_utxo: UtxoId,
Expand All @@ -81,7 +80,7 @@ pub struct CreateReserveCmd {

impl CreateReserveCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = crate::read_private_key_from_file(&self.payment_key_file)?;
let payment_key = self.payment_key_file.read_key()?;
let ogmios_client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;
let _ = create_reserve_utxo(
ReserveParameters {
Expand Down

0 comments on commit 964b8ec

Please sign in to comment.