Skip to content

Commit 4050ab9

Browse files
committed
fix: move struct to its own file and private the inner regis
Signed-off-by: bkioshn <[email protected]>
1 parent 7aa1754 commit 4050ab9

File tree

4 files changed

+225
-183
lines changed

4 files changed

+225
-183
lines changed

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

Lines changed: 86 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
//! Chain of Cardano registration data
22
3+
pub mod payment_history;
4+
pub mod point_tx_idx;
5+
pub mod role_data;
6+
37
use std::{collections::HashMap, sync::Arc};
48

59
use anyhow::bail;
610
use c509_certificate::c509::C509;
711
use pallas::{
8-
codec::utils::Bytes,
9-
crypto::hash::Hash,
10-
ledger::{primitives::conway::Value, traverse::MultiEraTx},
12+
codec::utils::Bytes, crypto::hash::Hash, ledger::traverse::MultiEraTx,
1113
network::miniprotocols::Point,
1214
};
15+
use payment_history::PaymentHistory;
16+
use point_tx_idx::PointTxIdx;
17+
use role_data::RoleData;
1318
use tracing::error;
1419

1520
use crate::{
@@ -18,7 +23,6 @@ use crate::{
1823
rbac::{
1924
certs::{C509Cert, X509DerCert},
2025
pub_key::{Ed25519PublicKey, SimplePublicKeyType},
21-
role_data::KeyLocalRef,
2226
CertKeyHash,
2327
},
2428
Cip509, UuidV4,
@@ -78,139 +82,86 @@ impl RegistrationChain {
7882
})
7983
}
8084

81-
/// Get the registration chain inner.
85+
/// Get the current transaction ID hash.
8286
#[must_use]
83-
pub fn registration_chain(&self) -> &RegistrationChainInner {
84-
self.inner.as_ref()
85-
}
86-
}
87-
88-
/// Inner structure of registration chain.
89-
#[derive(Clone)]
90-
pub struct RegistrationChainInner {
91-
/// The current transaction ID hash (32 bytes)
92-
current_tx_id_hash: Hash<32>,
93-
/// List of purpose for this registration chain
94-
purpose: Vec<UuidV4>,
95-
96-
// RBAC
97-
/// Map of index in array to point, transaction index, and x509 certificate.
98-
x509_certs: HashMap<usize, (PointTxIdx, Vec<u8>)>,
99-
/// Map of index in array to point, transaction index, and c509 certificate.
100-
c509_certs: HashMap<usize, (PointTxIdx, C509)>,
101-
/// Map of index in array to point, transaction index, and public key.
102-
simple_keys: HashMap<usize, (PointTxIdx, Ed25519PublicKey)>,
103-
/// List of point, transaction index, and certificate key hash.
104-
revocations: Vec<(PointTxIdx, CertKeyHash)>,
105-
106-
// Role
107-
/// Map of role number to point, transaction index, and role data.
108-
role_data: HashMap<u8, (PointTxIdx, RoleData)>,
109-
/// List of payment keys to track.
110-
tracking_payment_keys: Arc<Vec<Ed25519PublicKey>>,
111-
/// Map of payment key to its history.
112-
payment_history: HashMap<Ed25519PublicKey, Vec<PaymentHistory>>,
113-
}
114-
115-
/// Point (slot) and transaction index.
116-
#[derive(Clone)]
117-
pub struct PointTxIdx((Point, usize));
118-
119-
impl PointTxIdx {
120-
/// Create an instance of point and transaction index.
121-
pub(crate) fn new(point: Point, tx_idx: usize) -> Self {
122-
PointTxIdx((point, tx_idx))
87+
pub fn current_tx_id_hash(&self) -> Hash<32> {
88+
self.inner.current_tx_id_hash
12389
}
12490

125-
/// Get the point.
91+
/// Get a list of purpose for this registration chain.
12692
#[must_use]
127-
pub fn point(&self) -> &Point {
128-
&self.0 .0
93+
pub fn purpose(&self) -> &[UuidV4] {
94+
&self.inner.purpose
12995
}
13096

131-
/// Get the transaction index.
97+
/// Get the map of index in array to point, transaction index, and x509 certificate.
13298
#[must_use]
133-
pub fn tx_idx(&self) -> usize {
134-
self.0 .1
99+
pub fn x509_certs(&self) -> &HashMap<usize, (PointTxIdx, Vec<u8>)> {
100+
&self.inner.x509_certs
135101
}
136-
}
137-
138-
/// Payment history of the public key in tracking payment keys.
139-
#[derive(Clone)]
140-
pub struct PaymentHistory {
141-
/// The point and transaction index.
142-
point_tx_idx: PointTxIdx,
143-
/// Transaction hash that this payment come from.
144-
tx_hash: Hash<32>,
145-
/// The transaction output index that this payment come from.
146-
output_index: u16,
147-
/// The value of the payment.
148-
value: Value,
149-
}
150102

151-
impl PaymentHistory {
152-
/// Get the point and transaction index.
103+
/// Get the map of index in array to point, transaction index, and c509 certificate.
153104
#[must_use]
154-
pub fn point_tx_idx(&self) -> &PointTxIdx {
155-
&self.point_tx_idx
105+
pub fn c509_certs(&self) -> &HashMap<usize, (PointTxIdx, C509)> {
106+
&self.inner.c509_certs
156107
}
157108

158-
/// Get the transaction hash.
109+
/// Get the map of index in array to point, transaction index, and public key.
159110
#[must_use]
160-
pub fn tx_hash(&self) -> Hash<32> {
161-
self.tx_hash
111+
pub fn simple_keys(&self) -> &HashMap<usize, (PointTxIdx, Ed25519PublicKey)> {
112+
&self.inner.simple_keys
162113
}
163114

164-
/// Get the transaction output index.
115+
/// Get a list of revocations.
165116
#[must_use]
166-
pub fn output_index(&self) -> u16 {
167-
self.output_index
117+
pub fn revocations(&self) -> &[(PointTxIdx, CertKeyHash)] {
118+
&self.inner.revocations
168119
}
169120

170-
/// Get the value of the payment.
121+
/// Get the map of role number to point, transaction index, and role data.
171122
#[must_use]
172-
pub fn value(&self) -> &Value {
173-
&self.value
123+
pub fn role_data(&self) -> &HashMap<u8, (PointTxIdx, RoleData)> {
124+
&self.inner.role_data
174125
}
175-
}
176-
177-
/// Role data
178-
#[derive(Clone)]
179-
pub struct RoleData {
180-
/// A signing keys to the data within registration.
181-
signing_key_ref: Option<KeyLocalRef>,
182-
/// An encryption keys to the data within registration.
183-
encryption_ref: Option<KeyLocalRef>,
184-
/// A payment key where reward will be distributed to.
185-
payment_key: Ed25519PublicKey,
186-
/// Map of role extended data (10-99) to its data
187-
role_extended_data: HashMap<u8, Vec<u8>>,
188-
}
189126

190-
impl RoleData {
191-
/// Get the reference of signing keys.
127+
/// Get the list of payment keys to track.
192128
#[must_use]
193-
pub fn signing_key_ref(&self) -> &Option<KeyLocalRef> {
194-
&self.signing_key_ref
129+
pub fn tracking_payment_keys(&self) -> &Vec<Ed25519PublicKey> {
130+
&self.inner.tracking_payment_keys
195131
}
196132

197-
/// Get the reference of encryption keys.
133+
/// Get the map of payment key to its history.
198134
#[must_use]
199-
pub fn encryption_ref(&self) -> &Option<KeyLocalRef> {
200-
&self.encryption_ref
135+
pub fn payment_history(&self) -> &HashMap<Ed25519PublicKey, Vec<PaymentHistory>> {
136+
&self.inner.payment_history
201137
}
138+
}
202139

203-
/// Get the payment key.
204-
#[must_use]
205-
pub fn payment_key(&self) -> &Ed25519PublicKey {
206-
&self.payment_key
207-
}
140+
/// Inner structure of registration chain.
141+
#[derive(Clone)]
142+
struct RegistrationChainInner {
143+
/// The current transaction ID hash (32 bytes)
144+
current_tx_id_hash: Hash<32>,
145+
/// List of purpose for this registration chain
146+
purpose: Vec<UuidV4>,
208147

209-
/// Get the role extended data.
210-
#[must_use]
211-
pub fn role_extended_data(&self) -> &HashMap<u8, Vec<u8>> {
212-
&self.role_extended_data
213-
}
148+
// RBAC
149+
/// Map of index in array to point, transaction index, and x509 certificate.
150+
x509_certs: HashMap<usize, (PointTxIdx, Vec<u8>)>,
151+
/// Map of index in array to point, transaction index, and c509 certificate.
152+
c509_certs: HashMap<usize, (PointTxIdx, C509)>,
153+
/// Map of index in array to point, transaction index, and public key.
154+
simple_keys: HashMap<usize, (PointTxIdx, Ed25519PublicKey)>,
155+
/// List of point, transaction index, and certificate key hash.
156+
revocations: Vec<(PointTxIdx, CertKeyHash)>,
157+
158+
// Role
159+
/// Map of role number to point, transaction index, and role data.
160+
role_data: HashMap<u8, (PointTxIdx, RoleData)>,
161+
/// List of payment keys to track.
162+
tracking_payment_keys: Arc<Vec<Ed25519PublicKey>>,
163+
/// Map of payment key to its history.
164+
payment_history: HashMap<Ed25519PublicKey, Vec<PaymentHistory>>,
214165
}
215166

216167
impl RegistrationChainInner {
@@ -287,7 +238,7 @@ impl RegistrationChainInner {
287238
/// # Errors
288239
///
289240
/// Returns an error if data is invalid
290-
pub fn update(
241+
fn update(
291242
&self, point: Point, tx_idx: usize, txn: &MultiEraTx, cip509: Cip509,
292243
) -> anyhow::Result<Self> {
293244
let mut new_inner = self.clone();
@@ -342,60 +293,6 @@ impl RegistrationChainInner {
342293

343294
Ok(new_inner)
344295
}
345-
346-
/// Get the current transaction ID hash.
347-
#[must_use]
348-
pub fn current_tx_id_hash(&self) -> Hash<32> {
349-
self.current_tx_id_hash
350-
}
351-
352-
/// Get a list of purpose for this registration chain.
353-
#[must_use]
354-
pub fn purpose(&self) -> &[UuidV4] {
355-
&self.purpose
356-
}
357-
358-
/// Get the map of index in array to point, transaction index, and x509 certificate.
359-
#[must_use]
360-
pub fn x509_certs(&self) -> &HashMap<usize, (PointTxIdx, Vec<u8>)> {
361-
&self.x509_certs
362-
}
363-
364-
/// Get the map of index in array to point, transaction index, and c509 certificate.
365-
#[must_use]
366-
pub fn c509_certs(&self) -> &HashMap<usize, (PointTxIdx, C509)> {
367-
&self.c509_certs
368-
}
369-
370-
/// Get the map of index in array to point, transaction index, and public key.
371-
#[must_use]
372-
pub fn simple_keys(&self) -> &HashMap<usize, (PointTxIdx, Ed25519PublicKey)> {
373-
&self.simple_keys
374-
}
375-
376-
/// Get a list of revocations.
377-
#[must_use]
378-
pub fn revocations(&self) -> &[(PointTxIdx, CertKeyHash)] {
379-
&self.revocations
380-
}
381-
382-
/// Get the map of role number to point, transaction index, and role data.
383-
#[must_use]
384-
pub fn role_data(&self) -> &HashMap<u8, (PointTxIdx, RoleData)> {
385-
&self.role_data
386-
}
387-
388-
/// Get the list of payment keys to track.
389-
#[must_use]
390-
pub fn tracking_payment_keys(&self) -> &Vec<Ed25519PublicKey> {
391-
&self.tracking_payment_keys
392-
}
393-
394-
/// Get the map of payment key to its history.
395-
#[must_use]
396-
pub fn payment_history(&self) -> &HashMap<Ed25519PublicKey, Vec<PaymentHistory>> {
397-
&self.payment_history
398-
}
399296
}
400297

401298
/// Process x509 certificate for chain root.
@@ -556,12 +453,15 @@ fn chain_root_role_data(
556453
// Map of role number to point and role data
557454
role_data_map.insert(
558455
role_data.role_number,
559-
(point_tx_idx.clone(), RoleData {
560-
signing_key_ref: signing_key,
561-
encryption_ref: encryption_key,
562-
payment_key,
563-
role_extended_data: role_data.role_extended_data_keys.clone(),
564-
}),
456+
(
457+
point_tx_idx.clone(),
458+
RoleData::new(
459+
signing_key,
460+
encryption_key,
461+
payment_key,
462+
role_data.role_extended_data_keys.clone(),
463+
),
464+
),
565465
);
566466
}
567467
}
@@ -580,7 +480,7 @@ fn update_role_data(
580480
Some(key) => Some(key),
581481
None => {
582482
match inner.role_data.get(&role_data.role_number) {
583-
Some((_, role_data)) => role_data.signing_key_ref.clone(),
483+
Some((_, role_data)) => role_data.signing_key_ref().clone(),
584484
None => None,
585485
}
586486
},
@@ -591,7 +491,7 @@ fn update_role_data(
591491
Some(key) => Some(key),
592492
None => {
593493
match inner.role_data.get(&role_data.role_number) {
594-
Some((_, role_data)) => role_data.encryption_ref.clone(),
494+
Some((_, role_data)) => role_data.encryption_ref().clone(),
595495
None => None,
596496
}
597497
},
@@ -602,12 +502,15 @@ fn update_role_data(
602502
// Note that new role data will overwrite the old one
603503
inner.role_data.insert(
604504
role_data.role_number,
605-
(point_tx_idx.clone(), RoleData {
606-
signing_key_ref: signing_key,
607-
encryption_ref: encryption_key,
608-
payment_key,
609-
role_extended_data: role_data.role_extended_data_keys.clone(),
610-
}),
505+
(
506+
point_tx_idx.clone(),
507+
RoleData::new(
508+
signing_key,
509+
encryption_key,
510+
payment_key,
511+
role_data.role_extended_data_keys.clone(),
512+
),
513+
),
611514
);
612515
}
613516
}
@@ -668,12 +571,12 @@ fn update_payment_history(
668571
anyhow::anyhow!("Cannot convert usize to u16 in update payment history")
669572
})?;
670573

671-
payment_history.push(PaymentHistory {
672-
point_tx_idx: point_tx_idx.clone(),
673-
tx_hash: txn.hash(),
574+
payment_history.push(PaymentHistory::new(
575+
point_tx_idx.clone(),
576+
txn.hash(),
674577
output_index,
675-
value: o.value.clone(),
676-
});
578+
o.value.clone(),
579+
));
677580
}
678581
},
679582
pallas::ledger::primitives::conway::PseudoTransactionOutput::Legacy(_) => {

0 commit comments

Comments
 (0)