Skip to content

Commit

Permalink
fix: move struct to its own file and private the inner regis
Browse files Browse the repository at this point in the history
Signed-off-by: bkioshn <[email protected]>
  • Loading branch information
bkioshn committed Nov 29, 2024
1 parent 7aa1754 commit 4050ab9
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -18,7 +23,6 @@ use crate::{
rbac::{
certs::{C509Cert, X509DerCert},
pub_key::{Ed25519PublicKey, SimplePublicKeyType},
role_data::KeyLocalRef,
CertKeyHash,
},
Cip509, UuidV4,
Expand Down Expand Up @@ -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<UuidV4>,

// RBAC
/// Map of index in array to point, transaction index, and x509 certificate.
x509_certs: HashMap<usize, (PointTxIdx, Vec<u8>)>,
/// Map of index in array to point, transaction index, and c509 certificate.
c509_certs: HashMap<usize, (PointTxIdx, C509)>,
/// Map of index in array to point, transaction index, and public key.
simple_keys: HashMap<usize, (PointTxIdx, Ed25519PublicKey)>,
/// 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<u8, (PointTxIdx, RoleData)>,
/// List of payment keys to track.
tracking_payment_keys: Arc<Vec<Ed25519PublicKey>>,
/// Map of payment key to its history.
payment_history: HashMap<Ed25519PublicKey, Vec<PaymentHistory>>,
}

/// 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<usize, (PointTxIdx, Vec<u8>)> {
&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<usize, (PointTxIdx, C509)> {
&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<usize, (PointTxIdx, Ed25519PublicKey)> {
&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<u8, (PointTxIdx, RoleData)> {
&self.inner.role_data
}
}

/// Role data
#[derive(Clone)]
pub struct RoleData {
/// A signing keys to the data within registration.
signing_key_ref: Option<KeyLocalRef>,
/// An encryption keys to the data within registration.
encryption_ref: Option<KeyLocalRef>,
/// 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<u8, Vec<u8>>,
}

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<KeyLocalRef> {
&self.signing_key_ref
pub fn tracking_payment_keys(&self) -> &Vec<Ed25519PublicKey> {
&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<KeyLocalRef> {
&self.encryption_ref
pub fn payment_history(&self) -> &HashMap<Ed25519PublicKey, Vec<PaymentHistory>> {
&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<UuidV4>,

/// Get the role extended data.
#[must_use]
pub fn role_extended_data(&self) -> &HashMap<u8, Vec<u8>> {
&self.role_extended_data
}
// RBAC
/// Map of index in array to point, transaction index, and x509 certificate.
x509_certs: HashMap<usize, (PointTxIdx, Vec<u8>)>,
/// Map of index in array to point, transaction index, and c509 certificate.
c509_certs: HashMap<usize, (PointTxIdx, C509)>,
/// Map of index in array to point, transaction index, and public key.
simple_keys: HashMap<usize, (PointTxIdx, Ed25519PublicKey)>,
/// 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<u8, (PointTxIdx, RoleData)>,
/// List of payment keys to track.
tracking_payment_keys: Arc<Vec<Ed25519PublicKey>>,
/// Map of payment key to its history.
payment_history: HashMap<Ed25519PublicKey, Vec<PaymentHistory>>,
}

impl RegistrationChainInner {
Expand Down Expand Up @@ -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<Self> {
let mut new_inner = self.clone();
Expand Down Expand Up @@ -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<usize, (PointTxIdx, Vec<u8>)> {
&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<usize, (PointTxIdx, C509)> {
&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<usize, (PointTxIdx, Ed25519PublicKey)> {
&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<u8, (PointTxIdx, RoleData)> {
&self.role_data
}

/// Get the list of payment keys to track.
#[must_use]
pub fn tracking_payment_keys(&self) -> &Vec<Ed25519PublicKey> {
&self.tracking_payment_keys
}

/// Get the map of payment key to its history.
#[must_use]
pub fn payment_history(&self) -> &HashMap<Ed25519PublicKey, Vec<PaymentHistory>> {
&self.payment_history
}
}

/// Process x509 certificate for chain root.
Expand Down Expand Up @@ -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(),
),
),
);
}
}
Expand All @@ -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,
}
},
Expand All @@ -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,
}
},
Expand All @@ -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(),
),
),
);
}
}
Expand Down Expand Up @@ -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(_) => {
Expand Down
Loading

0 comments on commit 4050ab9

Please sign in to comment.