From 679e26d977796672f3264d7a46b9430504bdb73d Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Mon, 23 Oct 2023 12:09:10 -0700 Subject: [PATCH 1/3] Conway cert builder support support for adding conway certs to txs with support for deposit calculation also fixes NetworkId bug --- .../rust/src/builders/certificate_builder.rs | 79 +++++++++++-------- chain/rust/src/builders/witness_builder.rs | 8 ++ chain/rust/src/deposit.rs | 3 + chain/rust/src/utils.rs | 2 +- 4 files changed, 60 insertions(+), 32 deletions(-) diff --git a/chain/rust/src/builders/certificate_builder.rs b/chain/rust/src/builders/certificate_builder.rs index c6d1f52d..7a4cf959 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.clone()); - } - StakeCredential::PubKey { hash, .. } => { - required_witnesses.add_vkey_key_hash(hash.clone()); - } - }, - Certificate::StakeDelegation(cert) => match &cert.stake_credential { - StakeCredential::Script { hash, .. } => { - required_witnesses.add_script_hash(hash.clone()); - } - StakeCredential::PubKey { hash, .. } => { - required_witnesses.add_vkey_key_hash(hash.clone()); - } - }, + 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.clone()); @@ -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.clone()); } - 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,7 +116,9 @@ pub fn add_cert_vkeys( Certificate::PoolRetirement(cert) => { vkeys.insert(cert.ed25519_key_hash.clone()); } - Certificate::RegCert(_cert) => todo!(), + Certificate::RegCert(_cert) => { + // does not require a witness + } Certificate::UnregCert(_cert) => todo!(), Certificate::VoteDelegCert(_cert) => todo!(), Certificate::StakeVoteDelegCert(_cert) => todo!(), diff --git a/chain/rust/src/builders/witness_builder.rs b/chain/rust/src/builders/witness_builder.rs index 3d5de0bb..beac42ad 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, } } From e2674461549bdb94b9b7c87affeba0cff43c2198 Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Tue, 24 Oct 2023 09:11:27 -0700 Subject: [PATCH 2/3] add vkeys to add_cert_vkeys() --- .../rust/src/builders/certificate_builder.rs | 94 ++++++++++++++++--- 1 file changed, 83 insertions(+), 11 deletions(-) diff --git a/chain/rust/src/builders/certificate_builder.rs b/chain/rust/src/builders/certificate_builder.rs index 7a4cf959..c4c18ee8 100644 --- a/chain/rust/src/builders/certificate_builder.rs +++ b/chain/rust/src/builders/certificate_builder.rs @@ -119,17 +119,89 @@ pub fn add_cert_vkeys( Certificate::RegCert(_cert) => { // does not require a witness } - 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::UnregCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::VoteDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::StakeVoteDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::StakeRegDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::VoteRegDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::StakeVoteRegDelegCert(cert) => match &cert.stake_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::AuthCommitteeHotCert(cert) => match &cert.committee_cold_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::ResignCommitteeColdCert(cert) => match &cert.committee_cold_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::RegDrepCert(_cert) => { + // does not need a witness + } + Certificate::UnregDrepCert(cert) => match &cert.drep_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, + Certificate::UpdateDrepCert(cert) => match &cert.drep_credential { + StakeCredential::Script { hash, .. } => { + return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + } + StakeCredential::PubKey { hash, .. } => { + vkeys.insert(hash.clone()); + } + }, }; Ok(()) } From 93a67f98edd2a13e87853c0853211487e0295013 Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Tue, 24 Oct 2023 10:38:03 -0700 Subject: [PATCH 3/3] Fix clippy after merging #268 --- .../rust/src/builders/certificate_builder.rs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/chain/rust/src/builders/certificate_builder.rs b/chain/rust/src/builders/certificate_builder.rs index 5d5cacf2..7b015ea6 100644 --- a/chain/rust/src/builders/certificate_builder.rs +++ b/chain/rust/src/builders/certificate_builder.rs @@ -121,66 +121,66 @@ pub fn add_cert_vkeys( } Certificate::UnregCert(cert) => match &cert.stake_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::VoteDelegCert(cert) => match &cert.stake_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::StakeVoteDelegCert(cert) => match &cert.stake_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::StakeRegDelegCert(cert) => match &cert.stake_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::VoteRegDelegCert(cert) => match &cert.stake_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::StakeVoteRegDelegCert(cert) => match &cert.stake_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::AuthCommitteeHotCert(cert) => match &cert.committee_cold_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::ResignCommitteeColdCert(cert) => match &cert.committee_cold_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::RegDrepCert(_cert) => { @@ -188,18 +188,18 @@ pub fn add_cert_vkeys( } Certificate::UnregDrepCert(cert) => match &cert.drep_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, Certificate::UpdateDrepCert(cert) => match &cert.drep_credential { StakeCredential::Script { hash, .. } => { - return Err(CertBuilderError::ExpectedKeyHash(hash.clone())) + return Err(CertBuilderError::ExpectedKeyHash(*hash)) } StakeCredential::PubKey { hash, .. } => { - vkeys.insert(hash.clone()); + vkeys.insert(*hash); } }, };