Skip to content

Commit

Permalink
ETCM-8754: implement register tx submit (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpinter-iohk authored Dec 10, 2024
1 parent 0f1286f commit 95736ba
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ dev/local-environment/.env
dev/local-environment/docker-compose.yml
dev/local-environment/configurations/tests/e2e-tests
dev/local-environment/configurations/pc-contracts-cli/overrides

ogmios_client.log
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions toolkit/cli/smart-contracts-commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ serde = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
log4rs = { workspace = true }
cardano-serialization-lib = { workspace = true }
35 changes: 34 additions & 1 deletion toolkit/cli/smart-contracts-commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use log4rs::{
append::{console::ConsoleAppender, file::FileAppender},
config::Appender,
};
use sidechain_domain::MainchainPrivateKey;
use sidechain_domain::*;

pub mod get_scripts;
pub mod d_parameter;
pub mod init_governance;
pub mod register;

#[derive(Clone, Debug, clap::Subcommand)]
#[allow(clippy::large_enum_variant)]
Expand All @@ -17,6 +18,8 @@ pub enum SmartContractsCmd {
InitGovernance(init_governance::InitGovernanceCmd),
/// Upsert DParameter
UpsertDParameter(d_parameter::UpsertDParameterCmd),
/// Register candidate
Register(register::RegisterCmd),
}

#[derive(Clone, Debug, clap::Parser)]
Expand All @@ -34,6 +37,7 @@ impl SmartContractsCmd {
Self::InitGovernance(cmd) => cmd.execute().await,
Self::GetScripts(cmd) => cmd.execute().await,
Self::UpsertDParameter(cmd) => cmd.execute().await,
Self::Register(cmd) => cmd.execute().await,
}
}

Expand Down Expand Up @@ -79,3 +83,32 @@ pub fn setup_logging() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

Ok(())
}

// Parses public keys in formatted as SIDECHAIN_KEY:AURA_KEY:GRANDPA_KEY
pub(crate) fn parse_partnerchain_public_keys(
partner_chain_public_keys: &str,
) -> CmdResult<PermissionedCandidateData> {
if let [sidechain_pub_key, aura_pub_key, grandpa_pub_key] =
partner_chain_public_keys.split(":").collect::<Vec<_>>()[..]
{
Ok(PermissionedCandidateData {
sidechain_public_key: SidechainPublicKey(hex::decode(sidechain_pub_key)?),
aura_public_key: AuraPublicKey(hex::decode(aura_pub_key)?),
grandpa_public_key: GrandpaPublicKey(hex::decode(grandpa_pub_key)?),
})
} else {
Err("Failed to parse partner chain public keys.".into())
}
}

fn payment_signing_key_to_mainchain_address_hash(
payment_signing_key: MainchainPrivateKey,
) -> CmdResult<MainchainAddressHash> {
Ok(cardano_serialization_lib::PrivateKey::from_normal_bytes(&payment_signing_key.0)?
.to_public()
.hash()
.to_bytes()
.as_slice()
.try_into()
.map(MainchainAddressHash)?)
}
63 changes: 63 additions & 0 deletions toolkit/cli/smart-contracts-commands/src/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::{await_tx::FixedDelayRetries, register::run_register};
use sidechain_domain::{
AdaBasedStaking, CandidateRegistration, MainchainPublicKey, MainchainSignature,
PermissionedCandidateData, SidechainSignature, UtxoId,
};

use crate::{parse_partnerchain_public_keys, read_private_key_from_file};

#[derive(Clone, Debug, clap::Parser)]
pub struct RegisterCmd {
#[clap(flatten)]
common_arguments: crate::CommonArguments,
#[arg(long)]
genesis_utxo: UtxoId,
#[arg(long)]
registration_utxo: UtxoId,
#[arg(long)]
payment_key_file: String,
#[arg(
long,
value_name = "PARTNERCHAIN_KEY:AURA_KEY:GRANDPA_KEY",
alias = "sidechain-public-keys",
value_parser=parse_partnerchain_public_keys
)]
partnerchain_public_keys: PermissionedCandidateData,
#[arg(long, alias = "sidechain-signature")]
partnerchain_signature: SidechainSignature,
#[arg(long)]
spo_public_key: MainchainPublicKey,
#[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 client = HttpClient::builder().build(self.common_arguments.ogmios_host)?;
let candidate_registration = CandidateRegistration {
stake_ownership: AdaBasedStaking {
pub_key: self.spo_public_key,
signature: self.spo_signature,
},
partnerchain_pub_key: self.partnerchain_public_keys.sidechain_public_key,
partnerchain_signature: self.partnerchain_signature,
own_pkh: crate::payment_signing_key_to_mainchain_address_hash(payment_key.clone())?,
registration_utxo: self.registration_utxo,
aura_pub_key: self.partnerchain_public_keys.aura_public_key,
grandpa_pub_key: self.partnerchain_public_keys.grandpa_public_key,
};

run_register(
self.genesis_utxo,
&candidate_registration,
payment_key,
&client,
FixedDelayRetries::two_minutes(),
)
.await?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion toolkit/offchain/src/init_governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub async fn run_init_governance<

let result = client.submit_transaction(&signed_transaction.to_bytes()).await?;
let tx_id = result.transaction.id;
log::info!("✅ Transaction submited. ID: {}", hex::encode(result.transaction.id));
log::info!("✅ Transaction submitted. ID: {}", hex::encode(result.transaction.id));
await_tx
.await_tx_output(client, UtxoId { tx_hash: McTxHash(tx_id), index: UtxoIndex(0) })
.await?;
Expand Down
2 changes: 1 addition & 1 deletion toolkit/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod permissioned_candidates;
/// Utilities for handling Plutus script data
mod plutus_script;
/// Supports candidate registration
mod register;
pub mod register;
/// Provides synthetized scripts data
pub mod scripts_data;
#[cfg(test)]
Expand Down
Loading

0 comments on commit 95736ba

Please sign in to comment.