Skip to content

Commit

Permalink
Conway cert builder support (#270)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rooooooooob authored Oct 31, 2023
1 parent 0b04eea commit 6102d5c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 43 deletions.
173 changes: 131 additions & 42 deletions chain/rust/src/builders/certificate_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
};
}

Expand Down Expand Up @@ -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(())
}
Expand Down
8 changes: 8 additions & 0 deletions chain/rust/src/builders/witness_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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());
// }
Expand Down
3 changes: 3 additions & 0 deletions chain/rust/src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,
Expand All @@ -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)?,
Expand Down
2 changes: 1 addition & 1 deletion chain/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ impl NetworkId {

pub fn testnet() -> Self {
Self {
network: 1,
network: 0,
encoding: None,
}
}
Expand Down

0 comments on commit 6102d5c

Please sign in to comment.