Skip to content

Commit 969df75

Browse files
committed
feat: add wrapper to registration chain inner
Signed-off-by: bkioshn <[email protected]>
1 parent bca6b40 commit 969df75

File tree

1 file changed

+136
-32
lines changed

1 file changed

+136
-32
lines changed

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

Lines changed: 136 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,44 @@ use crate::{
2626
utils::general::decremented_index,
2727
};
2828

29-
/// Registration chain type.
30-
pub type RegistrationChain = Arc<RegistrationChainInner>;
29+
/// Registration chains.
30+
pub struct RegistrationChain {
31+
inner: Arc<RegistrationChainInner>,
32+
}
33+
34+
impl RegistrationChain {
35+
/// Create a new instance of registration chain.
36+
pub fn new(
37+
&self, point: Point, tracking_payment_keys: Vec<Ed25519PublicKey>, tx_idx: usize,
38+
txn: &MultiEraTx, cip509: Cip509,
39+
) -> anyhow::Result<Self> {
40+
let inner = RegistrationChainInner::new(cip509, tracking_payment_keys, point, tx_idx, txn)?;
41+
42+
Ok(Self {
43+
inner: Arc::new(inner),
44+
})
45+
}
46+
47+
/// Update the registration chain.
48+
pub fn update(
49+
&self, point: Point, tx_idx: usize, txn: &MultiEraTx, cip509: Cip509,
50+
) -> anyhow::Result<Self> {
51+
let new_inner = self.inner.update(point, tx_idx, txn, cip509)?;
52+
53+
Ok(Self {
54+
inner: Arc::new(new_inner),
55+
})
56+
}
57+
58+
/// Get the registration chain inner.
59+
pub fn registration_chain(&self) -> &RegistrationChainInner {
60+
self.inner.as_ref()
61+
}
62+
}
3163

3264
/// Inner structure of registration chain.
3365
#[derive(Clone)]
34-
pub struct RegistrationChainInner {
66+
struct RegistrationChainInner {
3567
/// The current transaction ID hash (32 bytes)
3668
current_tx_id_hash: Hash<32>,
3769
/// List of purpose for this registration chain
@@ -57,7 +89,6 @@ pub struct RegistrationChainInner {
5789
}
5890

5991
/// Point (slot) and transaction index.
60-
#[allow(dead_code)]
6192
#[derive(Clone)]
6293
pub(crate) struct PointTxIdx((Point, usize));
6394

@@ -69,9 +100,8 @@ impl PointTxIdx {
69100
}
70101

71102
/// Payment history of the public key in tracking payment keys.
72-
#[allow(dead_code)]
73103
#[derive(Clone)]
74-
pub struct PaymentHistory {
104+
struct PaymentHistory {
75105
/// The point and transaction index.
76106
point_tx_idx: PointTxIdx,
77107
/// Transaction hash that this payment come from.
@@ -82,10 +112,31 @@ pub struct PaymentHistory {
82112
value: Value,
83113
}
84114

115+
impl PaymentHistory {
116+
/// Get the point and transaction index.
117+
pub fn point_tx_idx(&self) -> &PointTxIdx {
118+
&self.point_tx_idx
119+
}
120+
121+
/// Get the transaction hash.
122+
pub fn tx_hash(&self) -> Hash<32> {
123+
self.tx_hash
124+
}
125+
126+
/// Get the transaction output index.
127+
pub fn output_index(&self) -> u16 {
128+
self.output_index
129+
}
130+
131+
/// Get the value of the payment.
132+
pub fn value(&self) -> &Value {
133+
&self.value
134+
}
135+
}
136+
85137
/// Role data
86-
#[allow(dead_code)]
87138
#[derive(Clone)]
88-
pub struct RoleData {
139+
struct RoleData {
89140
/// List of reference of signing keys to the data within registration.
90141
signing_key_ref: Vec<KeyLocalRef>,
91142
/// List of reference of encryption keys to the data within registration.
@@ -96,6 +147,28 @@ pub struct RoleData {
96147
role_extended_data: HashMap<u8, Vec<u8>>,
97148
}
98149

150+
impl RoleData {
151+
/// Get the reference of signing keys.
152+
pub fn signing_key_ref(&self) -> &[KeyLocalRef] {
153+
&self.signing_key_ref
154+
}
155+
156+
/// Get the reference of encryption keys.
157+
pub fn encryption_ref(&self) -> &[KeyLocalRef] {
158+
&self.encryption_ref
159+
}
160+
161+
/// Get the payment key.
162+
pub fn payment_key(&self) -> &Ed25519PublicKey {
163+
&self.payment_key
164+
}
165+
166+
/// Get the role extended data.
167+
pub fn role_extended_data(&self) -> &HashMap<u8, Vec<u8>> {
168+
&self.role_extended_data
169+
}
170+
}
171+
99172
impl RegistrationChainInner {
100173
/// Create a new instance of registration chain.
101174
/// The first new value should be the chain root.
@@ -110,8 +183,7 @@ impl RegistrationChainInner {
110183
/// # Errors
111184
///
112185
/// Returns an error if data is invalid
113-
#[allow(dead_code)]
114-
pub fn new(
186+
fn new(
115187
cip509: Cip509, tracking_payment_keys: Vec<Ed25519PublicKey>, point: Point, tx_idx: usize,
116188
txn: &MultiEraTx,
117189
) -> anyhow::Result<Self> {
@@ -171,7 +243,6 @@ impl RegistrationChainInner {
171243
/// # Errors
172244
///
173245
/// Returns an error if data is invalid
174-
#[allow(dead_code)]
175246
pub fn update(
176247
&self, point: Point, tx_idx: usize, txn: &MultiEraTx, cip509: Cip509,
177248
) -> anyhow::Result<Self> {
@@ -227,10 +298,45 @@ impl RegistrationChainInner {
227298

228299
Ok(new_inner)
229300
}
301+
302+
pub fn current_tx_id_hash(&self) -> Hash<32> {
303+
self.current_tx_id_hash
304+
}
305+
306+
pub fn purpose(&self) -> &[UuidV4] {
307+
&self.purpose
308+
}
309+
310+
pub fn x509_certs(&self) -> &HashMap<usize, (PointTxIdx, Vec<u8>)> {
311+
&self.x509_certs
312+
}
313+
314+
pub fn c509_certs(&self) -> &HashMap<usize, (PointTxIdx, C509)> {
315+
&self.c509_certs
316+
}
317+
318+
pub fn simple_keys(&self) -> &HashMap<usize, (PointTxIdx, Ed25519PublicKey)> {
319+
&self.simple_keys
320+
}
321+
322+
pub fn revocations(&self) -> &[(PointTxIdx, CertKeyHash)] {
323+
&self.revocations
324+
}
325+
326+
pub fn role_data(&self) -> &HashMap<u8, (PointTxIdx, RoleData)> {
327+
&self.role_data
328+
}
329+
330+
pub fn tracking_payment_keys(&self) -> &Vec<Ed25519PublicKey> {
331+
&self.tracking_payment_keys
332+
}
333+
334+
pub fn payment_history(&self) -> &HashMap<Ed25519PublicKey, Vec<PaymentHistory>> {
335+
&self.payment_history
336+
}
230337
}
231338

232339
/// Process x509 certificate for chain root.
233-
#[allow(dead_code)]
234340
fn chain_root_x509_certs(
235341
x509_certs: Option<Vec<X509DerCert>>, point_tx_idx: &PointTxIdx,
236342
) -> HashMap<usize, (PointTxIdx, Vec<u8>)> {
@@ -247,7 +353,6 @@ fn chain_root_x509_certs(
247353
}
248354

249355
/// Update x509 certificates in the registration chain.
250-
#[allow(dead_code)]
251356
fn update_x509_certs(
252357
new_inner: &mut RegistrationChainInner, x509_certs: Option<Vec<X509DerCert>>,
253358
point_tx_idx: &PointTxIdx,
@@ -273,7 +378,6 @@ fn update_x509_certs(
273378
}
274379

275380
/// Process c509 certificates for chain root.
276-
#[allow(dead_code)]
277381
fn chain_root_c509_certs(
278382
c509_certs: Option<Vec<C509Cert>>, point_tx_idx: &PointTxIdx,
279383
) -> HashMap<usize, (PointTxIdx, C509)> {
@@ -290,7 +394,6 @@ fn chain_root_c509_certs(
290394
}
291395

292396
/// Update c509 certificates in the registration chain.
293-
#[allow(dead_code)]
294397
fn update_c509_certs(
295398
new_inner: &mut RegistrationChainInner, c509_certs: Option<Vec<C509Cert>>,
296399
point_tx_idx: &PointTxIdx,
@@ -321,7 +424,6 @@ fn update_c509_certs(
321424
}
322425

323426
/// Process public keys for chain root.
324-
#[allow(dead_code)]
325427
fn chain_root_public_keys(
326428
pub_keys: Option<Vec<SimplePublicKeyType>>, point_tx_idx: &PointTxIdx,
327429
) -> HashMap<usize, (PointTxIdx, Ed25519PublicKey)> {
@@ -338,7 +440,6 @@ fn chain_root_public_keys(
338440
}
339441

340442
/// Update public keys in the registration chain.
341-
#[allow(dead_code)]
342443
fn update_public_keys(
343444
new_inner: &mut RegistrationChainInner, pub_keys: Option<Vec<SimplePublicKeyType>>,
344445
point_tx_idx: &PointTxIdx,
@@ -364,7 +465,6 @@ fn update_public_keys(
364465
}
365466

366467
/// Process the revocation list.
367-
#[allow(dead_code)]
368468
fn revocations_list(
369469
revocation_list: Option<Vec<CertKeyHash>>, point_tx_idx: &PointTxIdx,
370470
) -> Vec<(PointTxIdx, CertKeyHash)> {
@@ -378,7 +478,6 @@ fn revocations_list(
378478
}
379479

380480
/// Process the role data for chain root.
381-
#[allow(dead_code)]
382481
fn chain_root_role_data(
383482
role_set: Option<Vec<cip509::rbac::role_data::RoleData>>, txn: &MultiEraTx,
384483
point_tx_idx: &PointTxIdx,
@@ -395,20 +494,22 @@ fn chain_root_role_data(
395494
// Map of role number to point and role data
396495
role_data_map.insert(
397496
role_data.role_number,
398-
(point_tx_idx.clone(), RoleData {
399-
signing_key_ref: signing_key,
400-
encryption_ref: encryption_key,
401-
payment_key,
402-
role_extended_data: role_data.role_extended_data_keys.clone(),
403-
}),
497+
(
498+
point_tx_idx.clone(),
499+
RoleData {
500+
signing_key_ref: signing_key,
501+
encryption_ref: encryption_key,
502+
payment_key,
503+
role_extended_data: role_data.role_extended_data_keys.clone(),
504+
},
505+
),
404506
);
405507
}
406508
}
407509
Ok(role_data_map)
408510
}
409511

410512
/// Update the role data in the registration chain.
411-
#[allow(dead_code)]
412513
fn update_role_data(
413514
inner: &mut RegistrationChainInner, role_set: Option<Vec<cip509::rbac::role_data::RoleData>>,
414515
txn: &MultiEraTx, point_tx_idx: &PointTxIdx,
@@ -436,12 +537,15 @@ fn update_role_data(
436537
// Note that new role data will overwrite the old one
437538
inner.role_data.insert(
438539
role_data.role_number,
439-
(point_tx_idx.clone(), RoleData {
440-
signing_key_ref: signing_key,
441-
encryption_ref: encryption_key,
442-
payment_key,
443-
role_extended_data: role_data.role_extended_data_keys.clone(),
444-
}),
540+
(
541+
point_tx_idx.clone(),
542+
RoleData {
543+
signing_key_ref: signing_key,
544+
encryption_ref: encryption_key,
545+
payment_key,
546+
role_extended_data: role_data.role_extended_data_keys.clone(),
547+
},
548+
),
445549
);
446550
}
447551
}

0 commit comments

Comments
 (0)