diff --git a/rust/rbac-registration/src/registration/cardano.rs b/rust/rbac-registration/src/registration/cardano/mod.rs similarity index 83% rename from rust/rbac-registration/src/registration/cardano.rs rename to rust/rbac-registration/src/registration/cardano/mod.rs index d73518cfec..318772903a 100644 --- a/rust/rbac-registration/src/registration/cardano.rs +++ b/rust/rbac-registration/src/registration/cardano/mod.rs @@ -1,15 +1,20 @@ //! Chain of Cardano registration data +pub mod payment_history; +pub mod point_tx_idx; +pub mod role_data; + use std::{collections::HashMap, sync::Arc}; use anyhow::bail; use c509_certificate::c509::C509; use pallas::{ - codec::utils::Bytes, - crypto::hash::Hash, - ledger::{primitives::conway::Value, traverse::MultiEraTx}, + codec::utils::Bytes, crypto::hash::Hash, ledger::traverse::MultiEraTx, network::miniprotocols::Point, }; +use payment_history::PaymentHistory; +use point_tx_idx::PointTxIdx; +use role_data::RoleData; use tracing::error; use crate::{ @@ -18,7 +23,6 @@ use crate::{ rbac::{ certs::{C509Cert, X509DerCert}, pub_key::{Ed25519PublicKey, SimplePublicKeyType}, - role_data::KeyLocalRef, CertKeyHash, }, Cip509, UuidV4, @@ -78,139 +82,86 @@ impl RegistrationChain { }) } - /// Get the registration chain inner. + /// Get the current transaction ID hash. #[must_use] - pub fn registration_chain(&self) -> &RegistrationChainInner { - self.inner.as_ref() - } -} - -/// Inner structure of registration chain. -#[derive(Clone)] -pub struct RegistrationChainInner { - /// The current transaction ID hash (32 bytes) - current_tx_id_hash: Hash<32>, - /// List of purpose for this registration chain - purpose: Vec, - - // RBAC - /// Map of index in array to point, transaction index, and x509 certificate. - x509_certs: HashMap)>, - /// Map of index in array to point, transaction index, and c509 certificate. - c509_certs: HashMap, - /// Map of index in array to point, transaction index, and public key. - simple_keys: HashMap, - /// List of point, transaction index, and certificate key hash. - revocations: Vec<(PointTxIdx, CertKeyHash)>, - - // Role - /// Map of role number to point, transaction index, and role data. - role_data: HashMap, - /// List of payment keys to track. - tracking_payment_keys: Arc>, - /// Map of payment key to its history. - payment_history: HashMap>, -} - -/// Point (slot) and transaction index. -#[derive(Clone)] -pub struct PointTxIdx((Point, usize)); - -impl PointTxIdx { - /// Create an instance of point and transaction index. - pub(crate) fn new(point: Point, tx_idx: usize) -> Self { - PointTxIdx((point, tx_idx)) + pub fn current_tx_id_hash(&self) -> Hash<32> { + self.inner.current_tx_id_hash } - /// Get the point. + /// Get a list of purpose for this registration chain. #[must_use] - pub fn point(&self) -> &Point { - &self.0 .0 + pub fn purpose(&self) -> &[UuidV4] { + &self.inner.purpose } - /// Get the transaction index. + /// Get the map of index in array to point, transaction index, and x509 certificate. #[must_use] - pub fn tx_idx(&self) -> usize { - self.0 .1 + pub fn x509_certs(&self) -> &HashMap)> { + &self.inner.x509_certs } -} - -/// Payment history of the public key in tracking payment keys. -#[derive(Clone)] -pub struct PaymentHistory { - /// The point and transaction index. - point_tx_idx: PointTxIdx, - /// Transaction hash that this payment come from. - tx_hash: Hash<32>, - /// The transaction output index that this payment come from. - output_index: u16, - /// The value of the payment. - value: Value, -} -impl PaymentHistory { - /// Get the point and transaction index. + /// Get the map of index in array to point, transaction index, and c509 certificate. #[must_use] - pub fn point_tx_idx(&self) -> &PointTxIdx { - &self.point_tx_idx + pub fn c509_certs(&self) -> &HashMap { + &self.inner.c509_certs } - /// Get the transaction hash. + /// Get the map of index in array to point, transaction index, and public key. #[must_use] - pub fn tx_hash(&self) -> Hash<32> { - self.tx_hash + pub fn simple_keys(&self) -> &HashMap { + &self.inner.simple_keys } - /// Get the transaction output index. + /// Get a list of revocations. #[must_use] - pub fn output_index(&self) -> u16 { - self.output_index + pub fn revocations(&self) -> &[(PointTxIdx, CertKeyHash)] { + &self.inner.revocations } - /// Get the value of the payment. + /// Get the map of role number to point, transaction index, and role data. #[must_use] - pub fn value(&self) -> &Value { - &self.value + pub fn role_data(&self) -> &HashMap { + &self.inner.role_data } -} - -/// Role data -#[derive(Clone)] -pub struct RoleData { - /// A signing keys to the data within registration. - signing_key_ref: Option, - /// An encryption keys to the data within registration. - encryption_ref: Option, - /// A payment key where reward will be distributed to. - payment_key: Ed25519PublicKey, - /// Map of role extended data (10-99) to its data - role_extended_data: HashMap>, -} -impl RoleData { - /// Get the reference of signing keys. + /// Get the list of payment keys to track. #[must_use] - pub fn signing_key_ref(&self) -> &Option { - &self.signing_key_ref + pub fn tracking_payment_keys(&self) -> &Vec { + &self.inner.tracking_payment_keys } - /// Get the reference of encryption keys. + /// Get the map of payment key to its history. #[must_use] - pub fn encryption_ref(&self) -> &Option { - &self.encryption_ref + pub fn payment_history(&self) -> &HashMap> { + &self.inner.payment_history } +} - /// Get the payment key. - #[must_use] - pub fn payment_key(&self) -> &Ed25519PublicKey { - &self.payment_key - } +/// Inner structure of registration chain. +#[derive(Clone)] +struct RegistrationChainInner { + /// The current transaction ID hash (32 bytes) + current_tx_id_hash: Hash<32>, + /// List of purpose for this registration chain + purpose: Vec, - /// Get the role extended data. - #[must_use] - pub fn role_extended_data(&self) -> &HashMap> { - &self.role_extended_data - } + // RBAC + /// Map of index in array to point, transaction index, and x509 certificate. + x509_certs: HashMap)>, + /// Map of index in array to point, transaction index, and c509 certificate. + c509_certs: HashMap, + /// Map of index in array to point, transaction index, and public key. + simple_keys: HashMap, + /// List of point, transaction index, and certificate key hash. + revocations: Vec<(PointTxIdx, CertKeyHash)>, + + // Role + /// Map of role number to point, transaction index, and role data. + role_data: HashMap, + /// List of payment keys to track. + tracking_payment_keys: Arc>, + /// Map of payment key to its history. + payment_history: HashMap>, } impl RegistrationChainInner { @@ -287,7 +238,7 @@ impl RegistrationChainInner { /// # Errors /// /// Returns an error if data is invalid - pub fn update( + fn update( &self, point: Point, tx_idx: usize, txn: &MultiEraTx, cip509: Cip509, ) -> anyhow::Result { let mut new_inner = self.clone(); @@ -342,60 +293,6 @@ impl RegistrationChainInner { Ok(new_inner) } - - /// Get the current transaction ID hash. - #[must_use] - pub fn current_tx_id_hash(&self) -> Hash<32> { - self.current_tx_id_hash - } - - /// Get a list of purpose for this registration chain. - #[must_use] - pub fn purpose(&self) -> &[UuidV4] { - &self.purpose - } - - /// Get the map of index in array to point, transaction index, and x509 certificate. - #[must_use] - pub fn x509_certs(&self) -> &HashMap)> { - &self.x509_certs - } - - /// Get the map of index in array to point, transaction index, and c509 certificate. - #[must_use] - pub fn c509_certs(&self) -> &HashMap { - &self.c509_certs - } - - /// Get the map of index in array to point, transaction index, and public key. - #[must_use] - pub fn simple_keys(&self) -> &HashMap { - &self.simple_keys - } - - /// Get a list of revocations. - #[must_use] - pub fn revocations(&self) -> &[(PointTxIdx, CertKeyHash)] { - &self.revocations - } - - /// Get the map of role number to point, transaction index, and role data. - #[must_use] - pub fn role_data(&self) -> &HashMap { - &self.role_data - } - - /// Get the list of payment keys to track. - #[must_use] - pub fn tracking_payment_keys(&self) -> &Vec { - &self.tracking_payment_keys - } - - /// Get the map of payment key to its history. - #[must_use] - pub fn payment_history(&self) -> &HashMap> { - &self.payment_history - } } /// Process x509 certificate for chain root. @@ -556,12 +453,15 @@ fn chain_root_role_data( // Map of role number to point and role data role_data_map.insert( role_data.role_number, - (point_tx_idx.clone(), RoleData { - signing_key_ref: signing_key, - encryption_ref: encryption_key, - payment_key, - role_extended_data: role_data.role_extended_data_keys.clone(), - }), + ( + point_tx_idx.clone(), + RoleData::new( + signing_key, + encryption_key, + payment_key, + role_data.role_extended_data_keys.clone(), + ), + ), ); } } @@ -580,7 +480,7 @@ fn update_role_data( Some(key) => Some(key), None => { match inner.role_data.get(&role_data.role_number) { - Some((_, role_data)) => role_data.signing_key_ref.clone(), + Some((_, role_data)) => role_data.signing_key_ref().clone(), None => None, } }, @@ -591,7 +491,7 @@ fn update_role_data( Some(key) => Some(key), None => { match inner.role_data.get(&role_data.role_number) { - Some((_, role_data)) => role_data.encryption_ref.clone(), + Some((_, role_data)) => role_data.encryption_ref().clone(), None => None, } }, @@ -602,12 +502,15 @@ fn update_role_data( // Note that new role data will overwrite the old one inner.role_data.insert( role_data.role_number, - (point_tx_idx.clone(), RoleData { - signing_key_ref: signing_key, - encryption_ref: encryption_key, - payment_key, - role_extended_data: role_data.role_extended_data_keys.clone(), - }), + ( + point_tx_idx.clone(), + RoleData::new( + signing_key, + encryption_key, + payment_key, + role_data.role_extended_data_keys.clone(), + ), + ), ); } } @@ -668,12 +571,12 @@ fn update_payment_history( anyhow::anyhow!("Cannot convert usize to u16 in update payment history") })?; - payment_history.push(PaymentHistory { - point_tx_idx: point_tx_idx.clone(), - tx_hash: txn.hash(), + payment_history.push(PaymentHistory::new( + point_tx_idx.clone(), + txn.hash(), output_index, - value: o.value.clone(), - }); + o.value.clone(), + )); } }, pallas::ledger::primitives::conway::PseudoTransactionOutput::Legacy(_) => { diff --git a/rust/rbac-registration/src/registration/cardano/payment_history.rs b/rust/rbac-registration/src/registration/cardano/payment_history.rs new file mode 100644 index 0000000000..13e4aabc5d --- /dev/null +++ b/rust/rbac-registration/src/registration/cardano/payment_history.rs @@ -0,0 +1,56 @@ +//! Payment history of the public key in tracking payment keys. + +use pallas::{crypto::hash::Hash, ledger::primitives::conway::Value}; + +use super::point_tx_idx::PointTxIdx; + +/// Payment history of the public key in tracking payment keys. +#[derive(Clone)] +pub struct PaymentHistory { + /// The point and transaction index. + point_tx_idx: PointTxIdx, + /// Transaction hash that this payment come from. + tx_hash: Hash<32>, + /// The transaction output index that this payment come from. + output_index: u16, + /// The value of the payment. + value: Value, +} + +impl PaymentHistory { + /// Create an instance of payment history. + pub(crate) fn new( + point_tx_idx: PointTxIdx, tx_hash: Hash<32>, output_index: u16, value: Value, + ) -> Self { + PaymentHistory { + point_tx_idx, + tx_hash, + output_index, + value, + } + } + + /// Get the point and transaction index. + #[must_use] + pub fn point_tx_idx(&self) -> &PointTxIdx { + &self.point_tx_idx + } + + /// Get the transaction hash. + #[must_use] + pub fn tx_hash(&self) -> Hash<32> { + self.tx_hash + } + + /// Get the transaction output index. + #[must_use] + pub fn output_index(&self) -> u16 { + self.output_index + } + + /// Get the value of the payment. + #[must_use] + pub fn value(&self) -> &Value { + &self.value + } +} diff --git a/rust/rbac-registration/src/registration/cardano/point_tx_idx.rs b/rust/rbac-registration/src/registration/cardano/point_tx_idx.rs new file mode 100644 index 0000000000..27247dd5c2 --- /dev/null +++ b/rust/rbac-registration/src/registration/cardano/point_tx_idx.rs @@ -0,0 +1,26 @@ +//! Point or absolute slot and transaction index. + +use pallas::network::miniprotocols::Point; + +/// Point (slot) and transaction index. +#[derive(Clone)] +pub struct PointTxIdx((Point, usize)); + +impl PointTxIdx { + /// Create an instance of point and transaction index. + pub(crate) fn new(point: Point, tx_idx: usize) -> Self { + PointTxIdx((point, tx_idx)) + } + + /// Get the point. + #[must_use] + pub fn point(&self) -> &Point { + &self.0 .0 + } + + /// Get the transaction index. + #[must_use] + pub fn tx_idx(&self) -> usize { + self.0 .1 + } +} diff --git a/rust/rbac-registration/src/registration/cardano/role_data.rs b/rust/rbac-registration/src/registration/cardano/role_data.rs new file mode 100644 index 0000000000..684671415e --- /dev/null +++ b/rust/rbac-registration/src/registration/cardano/role_data.rs @@ -0,0 +1,57 @@ +//! RBAC role data + +use std::collections::HashMap; + +use crate::cardano::cip509::rbac::{pub_key::Ed25519PublicKey, role_data::KeyLocalRef}; + +/// Role data +#[derive(Clone)] +pub struct RoleData { + /// A signing keys to the data within registration. + signing_key_ref: Option, + /// An encryption keys to the data within registration. + encryption_ref: Option, + /// A payment key where reward will be distributed to. + payment_key: Ed25519PublicKey, + /// Map of role extended data (10-99) to its data + role_extended_data: HashMap>, +} + +impl RoleData { + /// Create an instance of role data. + pub(crate) fn new( + signing_key_ref: Option, encryption_ref: Option, + payment_key: Ed25519PublicKey, role_extended_data: HashMap>, + ) -> Self { + RoleData { + signing_key_ref, + encryption_ref, + payment_key, + role_extended_data, + } + } + + /// Get the reference of signing keys. + #[must_use] + pub fn signing_key_ref(&self) -> &Option { + &self.signing_key_ref + } + + /// Get the reference of encryption keys. + #[must_use] + pub fn encryption_ref(&self) -> &Option { + &self.encryption_ref + } + + /// Get the payment key. + #[must_use] + pub fn payment_key(&self) -> &Ed25519PublicKey { + &self.payment_key + } + + /// Get the role extended data. + #[must_use] + pub fn role_extended_data(&self) -> &HashMap> { + &self.role_extended_data + } +}