diff --git a/Cargo.lock b/Cargo.lock index 899d3b64f..495f380f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9949,6 +9949,7 @@ version = "1.4.0" dependencies = [ "blake2b_simd", "byte-string-derive", + "cardano-serialization-lib", "derive_more", "figment", "hex", diff --git a/toolkit/cli/smart-contracts-commands/src/lib.rs b/toolkit/cli/smart-contracts-commands/src/lib.rs index 7d4ae3fe9..632b936b8 100644 --- a/toolkit/cli/smart-contracts-commands/src/lib.rs +++ b/toolkit/cli/smart-contracts-commands/src/lib.rs @@ -79,18 +79,6 @@ pub(crate) fn parse_partnerchain_public_keys( } } -fn payment_signing_key_to_mainchain_address_hash( - payment_signing_key: MainchainPrivateKey, -) -> CmdResult { - Ok(cardano_serialization_lib::PrivateKey::from_normal_bytes(&payment_signing_key.0)? - .to_public() - .hash() - .to_bytes() - .as_slice() - .try_into() - .map(MainchainAddressHash)?) -} - #[cfg(test)] mod test { use crate::parse_partnerchain_public_keys; diff --git a/toolkit/cli/smart-contracts-commands/src/register.rs b/toolkit/cli/smart-contracts-commands/src/register.rs index 0b0ad4e03..7c77e0c75 100644 --- a/toolkit/cli/smart-contracts-commands/src/register.rs +++ b/toolkit/cli/smart-contracts-commands/src/register.rs @@ -46,7 +46,7 @@ impl RegisterCmd { }, 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())?, + own_pkh: payment_key.to_pub_key_hash(), 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, @@ -81,13 +81,10 @@ impl DeregisterCmd { pub async fn execute(self) -> crate::CmdResult<()> { let payment_signing_key = read_private_key_from_file(&self.payment_key_file)?; let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?; - let own_pkh = - crate::payment_signing_key_to_mainchain_address_hash(payment_signing_key.clone())?; run_deregister( self.genesis_utxo, payment_signing_key, - own_pkh, self.spo_public_key, &client, FixedDelayRetries::two_minutes(), diff --git a/toolkit/offchain/src/register.rs b/toolkit/offchain/src/register.rs index 1d8ffffa6..05f994698 100644 --- a/toolkit/offchain/src/register.rs +++ b/toolkit/offchain/src/register.rs @@ -141,7 +141,6 @@ pub trait Deregister { &self, genesis_utxo: UtxoId, payment_signing_key: MainchainPrivateKey, - own_pkh: MainchainAddressHash, stake_ownership_pub_key: MainchainPublicKey, ) -> Result, OffchainError>; } @@ -154,13 +153,11 @@ where &self, genesis_utxo: UtxoId, payment_signing_key: MainchainPrivateKey, - own_pkh: MainchainAddressHash, stake_ownership_pub_key: MainchainPublicKey, ) -> Result, OffchainError> { run_deregister( genesis_utxo, payment_signing_key, - own_pkh, stake_ownership_pub_key, self, FixedDelayRetries::two_minutes(), @@ -176,7 +173,6 @@ pub async fn run_deregister< >( genesis_utxo: UtxoId, payment_signing_key: MainchainPrivateKey, - own_pkh: MainchainAddressHash, stake_ownership_pub_key: MainchainPublicKey, ogmios_client: &C, await_tx: A, @@ -186,7 +182,7 @@ pub async fn run_deregister< let validator_address = validator.address_bech32(ctx.network)?; let all_registration_utxos = ogmios_client.query_utxos(&[validator_address]).await?; let own_registrations = - get_own_registrations(own_pkh, stake_ownership_pub_key.clone(), &all_registration_utxos); + get_own_registrations(payment_signing_key.to_pub_key_hash(), stake_ownership_pub_key.clone(), &all_registration_utxos); if own_registrations.is_empty() { log::info!("✅ Candidate is not registered."); @@ -200,7 +196,7 @@ pub async fn run_deregister< let evaluate_response = ogmios_client.evaluate_transaction(&tx.to_bytes()).await.map_err(|e| { anyhow!( - "Evaluate candidate unregistration transaction request failed: {}, bytes: {}", + "Evaluate candidate deregistration transaction request failed: {}, bytes: {}", e, hex::encode(tx.to_bytes()) ) @@ -210,7 +206,7 @@ pub async fn run_deregister< let signed_tx = ctx.sign(&tx).to_bytes(); let result = ogmios_client.submit_transaction(&signed_tx).await.map_err(|e| { anyhow!( - "Submit candidate unregistration transaction request failed: {}, bytes: {}", + "Submit candidate deregistration transaction request failed: {}, bytes: {}", e, hex::encode(tx.to_bytes()) ) diff --git a/toolkit/offchain/tests/integration_tests.rs b/toolkit/offchain/tests/integration_tests.rs index b13532a31..73abed7ef 100644 --- a/toolkit/offchain/tests/integration_tests.rs +++ b/toolkit/offchain/tests/integration_tests.rs @@ -183,7 +183,7 @@ async fn run_register Option { +) -> Option { let eve_utxos = client.query_utxos(&[EVE_ADDRESS.to_string()]).await.unwrap(); let registration_utxo = eve_utxos.first().unwrap().utxo_id(); client diff --git a/toolkit/partner-chains-cli/src/cardano_key.rs b/toolkit/partner-chains-cli/src/cardano_key.rs index c5d02b770..456d784af 100644 --- a/toolkit/partner-chains-cli/src/cardano_key.rs +++ b/toolkit/partner-chains-cli/src/cardano_key.rs @@ -1,6 +1,6 @@ use crate::IOContext; use anyhow::anyhow; -use sidechain_domain::{MainchainAddressHash, MainchainPrivateKey}; +use sidechain_domain::MainchainPrivateKey; #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] @@ -45,15 +45,3 @@ pub(crate) fn get_mc_pkey_from_file( ) -> anyhow::Result { Ok(MainchainPrivateKey(get_key_bytes_from_file(path, context)?)) } - -pub(crate) fn get_mc_address_hash_from_pkey(pkey: &MainchainPrivateKey) -> MainchainAddressHash { - let csl_private_key = cardano_serialization_lib::PrivateKey::from_normal_bytes(&pkey.0) - .expect("Conversion is infallible"); - let csl_public_key_hash = csl_private_key - .to_public() - .hash() - .to_bytes() - .try_into() - .expect("Bytes represent correct public key hash"); - MainchainAddressHash(csl_public_key_hash) -} diff --git a/toolkit/partner-chains-cli/src/prepare_configuration/prepare_main_chain_config.rs b/toolkit/partner-chains-cli/src/prepare_configuration/prepare_main_chain_config.rs index e6ce79984..b8d87f7b9 100644 --- a/toolkit/partner-chains-cli/src/prepare_configuration/prepare_main_chain_config.rs +++ b/toolkit/partner-chains-cli/src/prepare_configuration/prepare_main_chain_config.rs @@ -42,7 +42,7 @@ fn get_private_key_and_key_hash( let cardano_signig_key_file = config_fields::CARDANO_PAYMENT_SIGNING_KEY_FILE .prompt_with_default_from_file_and_save(context); let pkey = cardano_key::get_mc_pkey_from_file(&cardano_signig_key_file, context)?; - let addr_hash = cardano_key::get_mc_address_hash_from_pkey(&pkey); + let addr_hash = pkey.to_pub_key_hash(); Ok((pkey, addr_hash)) } diff --git a/toolkit/primitives/domain/Cargo.toml b/toolkit/primitives/domain/Cargo.toml index 7b5e80744..ae00d8b58 100644 --- a/toolkit/primitives/domain/Cargo.toml +++ b/toolkit/primitives/domain/Cargo.toml @@ -24,6 +24,7 @@ lazy_static = { workspace = true } blake2b_simd = { workspace = true } figment = { workspace = true, optional = true } thiserror = { workspace = true, optional = true } +cardano-serialization-lib = { workspace = true, optional = true } [dev-dependencies] serde_json = { workspace = true } @@ -38,6 +39,7 @@ std = [ "num-bigint/std", "figment", "thiserror", + "cardano-serialization-lib", ] serde = [ "dep:serde", diff --git a/toolkit/primitives/domain/src/lib.rs b/toolkit/primitives/domain/src/lib.rs index 9370a314e..9833c30c2 100644 --- a/toolkit/primitives/domain/src/lib.rs +++ b/toolkit/primitives/domain/src/lib.rs @@ -216,6 +216,21 @@ impl core::fmt::Debug for MainchainPrivateKey { } } +impl MainchainPrivateKey { + #[cfg(feature = "std")] + pub fn to_pub_key_hash(&self) -> MainchainAddressHash { + cardano_serialization_lib::PrivateKey::from_normal_bytes(&self.0) + .expect("impossible") + .to_public() + .hash() + .to_bytes() + .as_slice() + .try_into() + .map(MainchainAddressHash) + .expect("impossible") + } +} + impl TryFrom> for MainchainPublicKey { type Error = &'static str;