Skip to content

Commit a233ad6

Browse files
Implement role_0_stake_addresses for both registration chain and cip509 (#305)
1 parent 316bc89 commit a233ad6

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

rust/rbac-registration/src/cardano/cip509/cip509.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use std::{borrow::Cow, collections::HashMap};
66

77
use anyhow::{anyhow, Context};
8-
use cardano_blockchain_types::{MetadatumLabel, MultiEraBlock, TransactionId, TxnIndex};
8+
use cardano_blockchain_types::{
9+
MetadatumLabel, MultiEraBlock, StakeAddress, TransactionId, TxnIndex,
10+
};
911
use catalyst_types::{
1012
cbor_utils::{report_duplicated_key, report_missing_keys},
1113
hashes::{Blake2b256Hash, BLAKE_2B256_SIZE},
@@ -257,6 +259,15 @@ impl Cip509 {
257259
self.catalyst_id.as_ref()
258260
}
259261

262+
/// Returns a list of role 0 stake addresses.
263+
#[must_use]
264+
pub fn role_0_stake_addresses(&self) -> Vec<StakeAddress> {
265+
self.metadata
266+
.as_ref()
267+
.map(|m| m.certificate_uris.stake_addresses(0))
268+
.unwrap_or_default()
269+
}
270+
260271
/// Returns `Cip509` fields consuming the structure if it was successfully decoded and
261272
/// validated otherwise return the problem report that contains all the encountered
262273
/// issues.

rust/rbac-registration/src/cardano/cip509/utils/cip134_uri_set.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use c509_certificate::{
77
general_names::general_name::{GeneralNameTypeRegistry, GeneralNameValue},
88
C509ExtensionType,
99
};
10-
use cardano_blockchain_types::Cip0134Uri;
10+
use cardano_blockchain_types::{Cip0134Uri, StakeAddress};
1111
use catalyst_types::problem_report::ProblemReport;
1212
use der_parser::der::parse_der_sequence;
13+
use pallas::ledger::addresses::Address;
1314
use tracing::debug;
1415
use x509_cert::der::oid::db::rfc5912::ID_CE_SUBJECT_ALT_NAME;
1516

@@ -67,10 +68,25 @@ impl Cip0134UriSet {
6768
self.x_uris().is_empty() && self.c_uris().is_empty()
6869
}
6970

71+
/// Returns a list of stake addresses by the given index.
72+
#[must_use]
73+
pub fn stake_addresses(&self, index: usize) -> Vec<StakeAddress> {
74+
let mut result = Vec::new();
75+
76+
if let Some(uris) = self.x_uris().get(&index) {
77+
result.extend(convert_stake_addresses(uris));
78+
}
79+
if let Some(uris) = self.c_uris().get(&index) {
80+
result.extend(convert_stake_addresses(uris));
81+
}
82+
83+
result
84+
}
85+
7086
/// Return the updated URIs set.
7187
///
7288
/// The resulting set includes all the data from both the original and a new one. In
73-
/// the following example for brevity we only consider ony type of uris:
89+
/// the following example for brevity we only consider one type of uris:
7490
/// ```text
7591
/// // Original data:
7692
/// 0: [uri_1]
@@ -259,6 +275,18 @@ fn extract_c509_uris(certificates: &[C509Cert], report: &ProblemReport) -> UrisM
259275
result
260276
}
261277

278+
/// Converts a list of `Cip0134Uri` to a list of stake addresses.
279+
fn convert_stake_addresses(uris: &[Cip0134Uri]) -> Vec<StakeAddress> {
280+
uris.iter()
281+
.filter_map(|uri| {
282+
match uri.address() {
283+
Address::Stake(a) => Some(a.clone().into()),
284+
_ => None,
285+
}
286+
})
287+
.collect()
288+
}
289+
262290
#[cfg(test)]
263291
mod tests {
264292
use pallas::ledger::addresses::{Address, Network};

rust/rbac-registration/src/registration/cardano/mod.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use std::{collections::HashMap, sync::Arc};
66

77
use anyhow::bail;
88
use c509_certificate::c509::C509;
9-
use cardano_blockchain_types::{Cip0134Uri, StakeAddress, TransactionId};
9+
use cardano_blockchain_types::{StakeAddress, TransactionId};
1010
use catalyst_types::{
1111
id_uri::{key_rotation::KeyRotation, IdUri},
1212
uuid::UuidV4,
1313
};
1414
use ed25519_dalek::VerifyingKey;
15-
use pallas::ledger::addresses::Address;
1615
use tracing::error;
1716
use update_rbac::{
1817
revocations_list, update_c509_certs, update_public_keys, update_role_data, update_x509_certs,
@@ -212,16 +211,7 @@ impl RegistrationChain {
212211
/// Returns a list of role 0 stake addresses.
213212
#[must_use]
214213
pub fn role_0_stake_addresses(&self) -> Vec<StakeAddress> {
215-
let mut result = Vec::new();
216-
217-
if let Some(uris) = self.inner.certificate_uris.x_uris().get(&0) {
218-
result.extend(convert_stake_addresses(uris));
219-
}
220-
if let Some(uris) = self.inner.certificate_uris.c_uris().get(&0) {
221-
result.extend(convert_stake_addresses(uris));
222-
}
223-
224-
result
214+
self.inner.certificate_uris.stake_addresses(0)
225215
}
226216
}
227217

@@ -410,18 +400,6 @@ impl RegistrationChainInner {
410400
}
411401
}
412402

413-
/// Converts a list of `Cip0134Uri` to a list of stake addresses.
414-
fn convert_stake_addresses(uris: &[Cip0134Uri]) -> Vec<StakeAddress> {
415-
uris.iter()
416-
.filter_map(|uri| {
417-
match uri.address() {
418-
Address::Stake(a) => Some(a.clone().into()),
419-
_ => None,
420-
}
421-
})
422-
.collect()
423-
}
424-
425403
#[cfg(test)]
426404
mod test {
427405
use super::*;

0 commit comments

Comments
 (0)