From 6102d5cb03b0af84ac63097fe0b64dded93f07e0 Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Tue, 31 Oct 2023 14:42:10 -0300 Subject: [PATCH] Conway cert builder support (#270) * Conway cert builder support support for adding conway certs to txs with support for deposit calculation also fixes NetworkId bug * add vkeys to add_cert_vkeys() * Fix clippy after merging #268 --- .../rust/src/builders/certificate_builder.rs | 173 +++++++++++++----- chain/rust/src/builders/witness_builder.rs | 8 + chain/rust/src/deposit.rs | 3 + chain/rust/src/utils.rs | 2 +- 4 files changed, 143 insertions(+), 43 deletions(-) diff --git a/chain/rust/src/builders/certificate_builder.rs b/chain/rust/src/builders/certificate_builder.rs index ebe41fcf..7b015ea6 100644 --- a/chain/rust/src/builders/certificate_builder.rs +++ b/chain/rust/src/builders/certificate_builder.rs @@ -25,24 +25,15 @@ pub enum CertBuilderError { // comes from witsVKeyNeeded in the Ledger spec pub fn cert_required_wits(cert: &Certificate, required_witnesses: &mut RequiredWitnessSet) { match cert { - // stake key registrations do not require a witness - Certificate::StakeRegistration(_cert) => (), - Certificate::StakeDeregistration(cert) => match &cert.stake_credential { - StakeCredential::Script { hash, .. } => { - required_witnesses.add_script_hash(*hash); - } - StakeCredential::PubKey { hash, .. } => { - required_witnesses.add_vkey_key_hash(*hash); - } - }, - Certificate::StakeDelegation(cert) => match &cert.stake_credential { - StakeCredential::Script { hash, .. } => { - required_witnesses.add_script_hash(*hash); - } - StakeCredential::PubKey { hash, .. } => { - required_witnesses.add_vkey_key_hash(*hash); - } - }, + Certificate::StakeRegistration(_cert) => { + // stake key registrations do not require a witness + } + Certificate::StakeDeregistration(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::StakeDelegation(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } Certificate::PoolRegistration(cert) => { for owner in &cert.pool_params.pool_owners { required_witnesses.add_vkey_key_hash(*owner); @@ -52,18 +43,42 @@ pub fn cert_required_wits(cert: &Certificate, required_witnesses: &mut RequiredW Certificate::PoolRetirement(cert) => { required_witnesses.add_vkey_key_hash(cert.ed25519_key_hash); } - Certificate::RegCert(_cert) => todo!(), - Certificate::UnregCert(_cert) => todo!(), - Certificate::VoteDelegCert(_cert) => todo!(), - Certificate::StakeVoteDelegCert(_cert) => todo!(), - Certificate::StakeRegDelegCert(_cert) => todo!(), - Certificate::VoteRegDelegCert(_cert) => todo!(), - Certificate::StakeVoteRegDelegCert(_cert) => todo!(), - Certificate::AuthCommitteeHotCert(_cert) => todo!(), - Certificate::ResignCommitteeColdCert(_cert) => todo!(), - Certificate::RegDrepCert(_cert) => todo!(), - Certificate::UnregDrepCert(_cert) => todo!(), - Certificate::UpdateDrepCert(_cert) => todo!(), + Certificate::RegCert(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::UnregCert(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::VoteDelegCert(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::StakeVoteDelegCert(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::StakeRegDelegCert(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::VoteRegDelegCert(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::StakeVoteRegDelegCert(cert) => { + required_witnesses.add_from_credential(cert.stake_credential.clone()); + } + Certificate::AuthCommitteeHotCert(cert) => { + required_witnesses.add_from_credential(cert.committee_cold_credential.clone()); + } + Certificate::ResignCommitteeColdCert(cert) => { + required_witnesses.add_from_credential(cert.committee_cold_credential.clone()); + } + Certificate::RegDrepCert(_cert) => { + // does not need a witness + } + Certificate::UnregDrepCert(cert) => { + required_witnesses.add_from_credential(cert.drep_credential.clone()); + } + Certificate::UpdateDrepCert(cert) => { + required_witnesses.add_from_credential(cert.drep_credential.clone()); + } }; } @@ -101,18 +116,92 @@ pub fn add_cert_vkeys( Certificate::PoolRetirement(cert) => { vkeys.insert(cert.ed25519_key_hash); } - Certificate::RegCert(_cert) => todo!(), - Certificate::UnregCert(_cert) => todo!(), - Certificate::VoteDelegCert(_cert) => todo!(), - Certificate::StakeVoteDelegCert(_cert) => todo!(), - Certificate::StakeRegDelegCert(_cert) => todo!(), - Certificate::VoteRegDelegCert(_cert) => todo!(), - Certificate::StakeVoteRegDelegCert(_cert) => todo!(), - Certificate::AuthCommitteeHotCert(_cert) => todo!(), - Certificate::ResignCommitteeColdCert(_cert) => todo!(), - Certificate::RegDrepCert(_cert) => todo!(), - Certificate::UnregDrepCert(_cert) => todo!(), - Certificate::UpdateDrepCert(_cert) => todo!(), + Certificate::RegCert(_cert) => { + // does not require a witness + } + Certificate::UnregCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::VoteDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::StakeVoteDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::StakeRegDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::VoteRegDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::StakeVoteRegDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::AuthCommitteeHotCert(cert) => match &cert.committee_cold_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::ResignCommitteeColdCert(cert) => match &cert.committee_cold_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::RegDrepCert(_cert) => { + // does not need a witness + } + Certificate::UnregDrepCert(cert) => match &cert.drep_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, + Certificate::UpdateDrepCert(cert) => match &cert.drep_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(*hash)) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(*hash); + } + }, }; Ok(()) } diff --git a/chain/rust/src/builders/witness_builder.rs b/chain/rust/src/builders/witness_builder.rs index fdff14ad..1d85031f 100644 --- a/chain/rust/src/builders/witness_builder.rs +++ b/chain/rust/src/builders/witness_builder.rs @@ -6,6 +6,7 @@ use std::{ use crate::{ byron::ByronAddress, + certs::Credential, crypto::{hash::hash_plutus_data, BootstrapWitness, Vkey, Vkeywitness}, plutus::{PlutusData, PlutusScript, PlutusV1Script, PlutusV2Script, Redeemer}, transaction::{RequiredSigners, TransactionWitnessSet}, @@ -141,6 +142,13 @@ impl RequiredWitnessSet { } } + pub(crate) fn add_from_credential(&mut self, credential: Credential) { + match credential { + Credential::PubKey { hash, .. } => self.add_vkey_key_hash(hash), + Credential::Script { hash, .. } => self.add_script_hash(hash), + } + } + // pub fn add_plutus_script(&mut self, plutus_v1_script: &PlutusScript) { // self.add_script_hash(&plutus_v1_script.hash()); // } diff --git a/chain/rust/src/deposit.rs b/chain/rust/src/deposit.rs index 6fad75d4..c0ce313a 100644 --- a/chain/rust/src/deposit.rs +++ b/chain/rust/src/deposit.rs @@ -22,6 +22,7 @@ pub fn internal_get_implicit_input( .try_fold(0u64, |acc, cert| match cert { Certificate::PoolRetirement(_cert) => acc.checked_add(pool_deposit), Certificate::StakeDeregistration(_cert) => acc.checked_add(key_deposit), + Certificate::UnregCert(_cert) => acc.checked_add(key_deposit), _ => Some(acc), }) .ok_or(ArithmeticError::IntegerOverflow)?, @@ -45,6 +46,8 @@ pub fn internal_get_deposit( .try_fold(0u64, |acc, cert| match cert { Certificate::PoolRegistration(_cert) => acc.checked_add(pool_deposit), Certificate::StakeRegistration(_cert) => acc.checked_add(key_deposit), + Certificate::RegCert(_cert) => acc.checked_add(key_deposit), + Certificate::StakeRegDelegCert(_cert) => acc.checked_add(key_deposit), _ => Some(acc), }) .ok_or(ArithmeticError::IntegerOverflow)?, diff --git a/chain/rust/src/utils.rs b/chain/rust/src/utils.rs index b385c9a1..6e04e7ec 100644 --- a/chain/rust/src/utils.rs +++ b/chain/rust/src/utils.rs @@ -508,7 +508,7 @@ impl NetworkId { pub fn testnet() -> Self { Self { - network: 1, + network: 0, encoding: None, } }