Skip to content

Commit d1251df

Browse files
Introduce Cip0134UriSet type
1 parent 433f3cb commit d1251df

File tree

12 files changed

+352
-228
lines changed

12 files changed

+352
-228
lines changed

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,43 @@ use validation::{
2525
use x509_chunks::X509Chunks;
2626

2727
use super::transaction::witness::TxWitness;
28-
use crate::utils::{
29-
decode_helper::{decode_bytes, decode_helper, decode_map_len},
30-
general::{decode_utf8, decremented_index},
31-
hashing::{blake2b_128, blake2b_256},
28+
use crate::{
29+
cardano::cip509::rbac::Cip509RbacMetadata,
30+
utils::{
31+
decode_helper::{decode_bytes, decode_helper, decode_map_len},
32+
general::{decode_utf8, decremented_index},
33+
hashing::{blake2b_128, blake2b_256},
34+
},
3235
};
3336

3437
/// CIP509 label.
3538
pub const LABEL: u64 = 509;
3639

37-
/// CIP509.
40+
/// A x509 metadata envelope.
41+
///
42+
/// The envelope is required to prevent replayability attacks. See [this document] for
43+
/// more details.
44+
///
45+
/// [this document]: https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/README.md
3846
#[derive(Debug, PartialEq, Clone, Default)]
3947
pub struct Cip509 {
40-
/// `UUIDv4` Purpose .
41-
pub purpose: Uuid, // (bytes .size 16)
48+
/// A registration purpose (UUIDv4).
49+
///
50+
/// The purpose is defined by the consuming dApp.
51+
pub purpose: Uuid,
4252
/// Transaction inputs hash.
43-
pub txn_inputs_hash: TxInputHash, // bytes .size 16
44-
/// Optional previous transaction ID.
45-
pub prv_tx_id: Option<Hash<32>>, // bytes .size 32
46-
/// x509 chunks.
47-
pub x509_chunks: X509Chunks, // chunk_type => [ + x509_chunk ]
53+
pub txn_inputs_hash: TxInputHash,
54+
/// An optional BLAKE2b hash of the previous transaction.
55+
///
56+
/// The hash must always be present except for the first registration transaction.
57+
pub prv_tx_id: Option<Hash<32>>,
58+
/// Metadata.
59+
///
60+
/// This field encoded in chunks. See [`X509Chunks`] for more details.
61+
pub metadata: Cip509RbacMetadata,
4862
/// Validation signature.
49-
pub validation_signature: Vec<u8>, // bytes size (1..64)
63+
// TODO: FIXME: This probably should be a separate type and not just Vec.
64+
pub validation_signature: Vec<u8>,
5065
}
5166

5267
/// Validation value for CIP509 metadatum.
@@ -132,9 +147,10 @@ impl Decode<'_, ()> for Cip509 {
132147
},
133148
}
134149
} else {
150+
// TODO: FIXME: Check that only one key (10, 11, 12 (+up to 17?)) is present?..
135151
// Handle the x509 chunks 10 11 12
136152
let x509_chunks = X509Chunks::decode(d, ctx)?;
137-
cip509_metadatum.x509_chunks = x509_chunks;
153+
cip509_metadatum.metadata = x509_chunks.into();
138154
}
139155
}
140156
Ok(cip509_metadatum)
@@ -179,7 +195,7 @@ impl Cip509 {
179195
let mut is_valid_stake_public_key = true;
180196
let mut is_valid_payment_key = true;
181197
let mut is_valid_signing_key = true;
182-
if let Some(role_set) = &self.x509_chunks.0.role_set {
198+
if let Some(role_set) = &self.metadata.role_set {
183199
// Validate only role 0
184200
for role in role_set {
185201
if role.role_number == 0 {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum X509DerCert {
1818
/// Deleted indicates the key is deleted.
1919
Deleted,
2020
/// X.509 certificate.
21+
// TODO: FIXME: Store `x509_cert::Certificate` instead of bytes?!
2122
X509Cert(Vec<u8>),
2223
}
2324

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

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,40 @@ use role_data::RoleData;
1616
use strum_macros::FromRepr;
1717

1818
use super::types::cert_key_hash::CertKeyHash;
19-
use crate::utils::decode_helper::{
20-
decode_any, decode_array_len, decode_bytes, decode_helper, decode_map_len,
19+
use crate::{
20+
cardano::cip509::utils::Cip0134UriSet,
21+
utils::decode_helper::{
22+
decode_any, decode_array_len, decode_bytes, decode_helper, decode_map_len,
23+
},
2124
};
2225

2326
/// Cip509 RBAC metadata.
27+
///
28+
/// See [this document] for more details.
29+
///
30+
/// [this document]: https://github.com/input-output-hk/catalyst-CIPs/tree/x509-role-registration-metadata/CIP-XXXX
2431
#[derive(Debug, PartialEq, Clone, Default)]
32+
// TODO: FIXME: Discuss if we need options everywhere (`Option<Vec> -> Vec`).
2533
pub struct Cip509RbacMetadata {
34+
// TODO: FIXME: Parse X509DerCert?..
35+
// TODO: FIXME: Store only C509Cert (transcode X509DerCert to C509Cert?).
36+
// TODO: FIXME: Check if we allow both lists to be present.
37+
// TODO: FIXME: Better documentation for the certificate field (or fields!).
2638
/// Optional list of x509 certificates.
2739
pub x509_certs: Option<Vec<X509DerCert>>,
2840
/// Optional list of c509 certificates.
2941
/// The value can be either the c509 certificate or c509 metadatum reference.
3042
pub c509_certs: Option<Vec<C509Cert>>,
31-
/// Optional list of Public keys.
43+
// TODO: FIXME: Better documentation for the URI set/list field.
44+
/// This field isn't present in the encoded format and is populated by processing both
45+
/// `x509_certs` and `c509_certs`.
46+
pub fixme: Cip0134UriSet,
47+
/// A list of public keys that can be used instead of storing full certificates.
48+
///
49+
/// Check [this section] to understand the how certificates and the public keys list
50+
/// are related.
51+
///
52+
/// [this section]: https://github.com/input-output-hk/catalyst-CIPs/tree/x509-role-registration-metadata/CIP-XXXX#storing-certificates-and-public-key
3253
pub pub_keys: Option<Vec<SimplePublicKeyType>>,
3354
/// Optional list of revocation list.
3455
pub revocation_list: Option<Vec<CertKeyHash>>,
@@ -60,74 +81,35 @@ pub enum Cip509RbacMetadataInt {
6081
RoleSet = 100,
6182
}
6283

63-
impl Cip509RbacMetadata {
64-
/// Create a new instance of `Cip509RbacMetadata`.
65-
pub(crate) fn new() -> Self {
66-
Self {
67-
x509_certs: None,
68-
c509_certs: None,
69-
pub_keys: None,
70-
revocation_list: None,
71-
role_set: None,
72-
purpose_key_data: HashMap::new(),
73-
}
74-
}
75-
76-
/// Set the x509 certificates.
77-
fn set_x509_certs(&mut self, x509_certs: Vec<X509DerCert>) {
78-
self.x509_certs = Some(x509_certs);
79-
}
80-
81-
/// Set the c509 certificates.
82-
fn set_c509_certs(&mut self, c509_certs: Vec<C509Cert>) {
83-
self.c509_certs = Some(c509_certs);
84-
}
85-
86-
/// Set the public keys.
87-
fn set_pub_keys(&mut self, pub_keys: Vec<SimplePublicKeyType>) {
88-
self.pub_keys = Some(pub_keys);
89-
}
90-
91-
/// Set the revocation list.
92-
fn set_revocation_list(&mut self, revocation_list: Vec<CertKeyHash>) {
93-
self.revocation_list = Some(revocation_list);
94-
}
95-
96-
/// Set the role data set.
97-
fn set_role_set(&mut self, role_set: Vec<RoleData>) {
98-
self.role_set = Some(role_set);
99-
}
100-
}
101-
10284
impl Decode<'_, ()> for Cip509RbacMetadata {
10385
fn decode(d: &mut Decoder, ctx: &mut ()) -> Result<Self, decode::Error> {
10486
let map_len = decode_map_len(d, "Cip509RbacMetadata")?;
10587

106-
let mut x509_rbac_metadata = Cip509RbacMetadata::new();
88+
let mut x509_rbac_metadata = Cip509RbacMetadata::default();
10789

10890
for _ in 0..map_len {
10991
let key: u16 = decode_helper(d, "key in Cip509RbacMetadata", ctx)?;
11092
if let Some(key) = Cip509RbacMetadataInt::from_repr(key) {
11193
match key {
11294
Cip509RbacMetadataInt::X509Certs => {
11395
let x509_certs = decode_array_rbac(d, "x509 certificate")?;
114-
x509_rbac_metadata.set_x509_certs(x509_certs);
96+
x509_rbac_metadata.x509_certs = Some(x509_certs);
11597
},
11698
Cip509RbacMetadataInt::C509Certs => {
11799
let c509_certs = decode_array_rbac(d, "c509 certificate")?;
118-
x509_rbac_metadata.set_c509_certs(c509_certs);
100+
x509_rbac_metadata.c509_certs = Some(c509_certs);
119101
},
120102
Cip509RbacMetadataInt::PubKeys => {
121103
let pub_keys = decode_array_rbac(d, "public keys")?;
122-
x509_rbac_metadata.set_pub_keys(pub_keys);
104+
x509_rbac_metadata.pub_keys = Some(pub_keys);
123105
},
124106
Cip509RbacMetadataInt::RevocationList => {
125107
let revocation_list = decode_revocation_list(d)?;
126-
x509_rbac_metadata.set_revocation_list(revocation_list);
108+
x509_rbac_metadata.revocation_list = Some(revocation_list);
127109
},
128110
Cip509RbacMetadataInt::RoleSet => {
129111
let role_set = decode_array_rbac(d, "role set")?;
130-
x509_rbac_metadata.set_role_set(role_set);
112+
x509_rbac_metadata.role_set = Some(role_set);
131113
},
132114
}
133115
} else {
@@ -139,6 +121,9 @@ impl Decode<'_, ()> for Cip509RbacMetadata {
139121
.insert(key, decode_any(d, "purpose key")?);
140122
}
141123
}
124+
125+
// TODO: FIXME:
126+
x509_rbac_metadata.fixme = Cip0134UriSet::new();
142127
Ok(x509_rbac_metadata)
143128
}
144129
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Transaction input hash type
22
3-
/// Transaction input hash representing in 16 bytes.
3+
/// A 16-byte hash of the transaction inputs field.
4+
///
5+
/// This type is described [here].
6+
///
7+
/// [here]: https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/README.md#key-1-txn-inputs-hash
48
#[derive(Debug, PartialEq, Clone, Default)]
59
pub struct TxInputHash([u8; 16]);
610

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Utilities for [CIP-134] (Cardano URIs - Address Representation).
2+
//!
3+
//! [CIP-134]: https://github.com/cardano-foundation/CIPs/tree/master/CIP-0134
4+
pub use self::{uri::Cip0134Uri, uri_set::Cip0134UriSet};
5+
6+
mod uri;
7+
mod uri_set;

rust/rbac-registration/src/cardano/cip509/utils/cip134.rs renamed to rust/rbac-registration/src/cardano/cip509/utils/cip134/uri.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Utility functions for CIP-0134 address.
1+
//! An URI in the CIP-0134 format.
22
33
// Ignore URIs that are used in tests and doc-examples.
44
// cSpell:ignoreRegExp web\+cardano:.+
@@ -13,7 +13,8 @@ use pallas::ledger::addresses::Address;
1313
/// See the [proposal] for more details.
1414
///
1515
/// [proposal]: https://github.com/cardano-foundation/CIPs/pull/888
16-
#[derive(Debug)]
16+
#[derive(Debug, Eq, PartialEq)]
17+
#[allow(clippy::module_name_repetitions)]
1718
pub struct Cip0134Uri {
1819
/// A URI string.
1920
uri: String,

0 commit comments

Comments
 (0)