Skip to content

Commit 85320da

Browse files
committed
fix: use ShelleyPaymentPart
Signed-off-by: bkioshn <[email protected]>
1 parent 26e76dd commit 85320da

File tree

3 files changed

+43
-86
lines changed

3 files changed

+43
-86
lines changed

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

Lines changed: 0 additions & 54 deletions
This file was deleted.

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

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Chain of Cardano registration data
22
3-
pub mod cip19_shelley_addr;
43
pub mod payment_history;
54
pub mod point_tx_idx;
65
pub mod role_data;
@@ -9,9 +8,12 @@ use std::{collections::HashMap, sync::Arc};
98

109
use anyhow::bail;
1110
use c509_certificate::c509::C509;
12-
use cip19_shelley_addr::Cip19ShelleyAddrs;
1311
use pallas::{
14-
codec::utils::Bytes, crypto::hash::Hash, ledger::traverse::MultiEraTx,
12+
crypto::hash::Hash,
13+
ledger::{
14+
addresses::{Address, ShelleyPaymentPart},
15+
traverse::MultiEraTx,
16+
},
1517
network::miniprotocols::Point,
1618
};
1719
use payment_history::PaymentHistory;
@@ -44,7 +46,7 @@ impl RegistrationChain {
4446
///
4547
/// # Arguments
4648
/// - `cip509` - The CIP509.
47-
/// - `tracking_payment_keys` - The list of Shelley keys to track.
49+
/// - `tracking_payment_keys` - The list of payment keys to track.
4850
/// - `point` - The point (slot) of the transaction.
4951
/// - `tx_idx` - The transaction index.
5052
/// - `txn` - The transaction.
@@ -53,7 +55,7 @@ impl RegistrationChain {
5355
///
5456
/// Returns an error if data is invalid
5557
pub fn new(
56-
point: Point, tracking_payment_keys: Vec<Cip19ShelleyAddrs>, tx_idx: usize,
58+
point: Point, tracking_payment_keys: Vec<ShelleyPaymentPart>, tx_idx: usize,
5759
txn: &MultiEraTx, cip509: Cip509,
5860
) -> anyhow::Result<Self> {
5961
let inner = RegistrationChainInner::new(cip509, tracking_payment_keys, point, tx_idx, txn)?;
@@ -126,15 +128,15 @@ impl RegistrationChain {
126128
&self.inner.role_data
127129
}
128130

129-
/// Get the list of Shelley keys to track.
131+
/// Get the list of payment keys to track.
130132
#[must_use]
131-
pub fn tracking_payment_keys(&self) -> &Vec<Cip19ShelleyAddrs> {
133+
pub fn tracking_payment_keys(&self) -> &Vec<ShelleyPaymentPart> {
132134
&self.inner.tracking_payment_keys
133135
}
134136

135-
/// Get the map of tracked Shelley keys to its history.
137+
/// Get the map of tracked payment keys to its history.
136138
#[must_use]
137-
pub fn payment_history(&self) -> &HashMap<Cip19ShelleyAddrs, Vec<PaymentHistory>> {
139+
pub fn payment_history(&self) -> &HashMap<ShelleyPaymentPart, Vec<PaymentHistory>> {
138140
&self.inner.payment_history
139141
}
140142
}
@@ -161,9 +163,9 @@ struct RegistrationChainInner {
161163
/// Map of role number to point, transaction index, and role data.
162164
role_data: HashMap<u8, (PointTxIdx, RoleData)>,
163165
/// List of payment keys to track.
164-
tracking_payment_keys: Arc<Vec<Cip19ShelleyAddrs>>,
166+
tracking_payment_keys: Arc<Vec<ShelleyPaymentPart>>,
165167
/// Map of payment key to its history.
166-
payment_history: HashMap<Cip19ShelleyAddrs, Vec<PaymentHistory>>,
168+
payment_history: HashMap<ShelleyPaymentPart, Vec<PaymentHistory>>,
167169
}
168170

169171
impl RegistrationChainInner {
@@ -172,7 +174,7 @@ impl RegistrationChainInner {
172174
///
173175
/// # Arguments
174176
/// - `cip509` - The CIP509.
175-
/// - `tracking_payment_keys` - The list of Shelley keys to track.
177+
/// - `tracking_payment_keys` - The list of payment keys to track.
176178
/// - `point` - The point (slot) of the transaction.
177179
/// - `tx_idx` - The transaction index.
178180
/// - `txn` - The transaction.
@@ -181,8 +183,8 @@ impl RegistrationChainInner {
181183
///
182184
/// Returns an error if data is invalid
183185
fn new(
184-
cip509: Cip509, tracking_payment_keys: Vec<Cip19ShelleyAddrs>, point: Point, tx_idx: usize,
185-
txn: &MultiEraTx,
186+
cip509: Cip509, tracking_payment_keys: Vec<ShelleyPaymentPart>, point: Point,
187+
tx_idx: usize, txn: &MultiEraTx,
186188
) -> anyhow::Result<Self> {
187189
// Should be chain root, return immediately if not
188190
if cip509.prv_tx_id.is_some() {
@@ -450,7 +452,7 @@ fn chain_root_role_data(
450452
let encryption_key = role_data.role_encryption_key.clone();
451453

452454
// Get the payment key
453-
let payment_key = get_shelley_addr_from_tx(txn, role_data.payment_key)?;
455+
let payment_key = get_payment_addr_from_tx(txn, role_data.payment_key)?;
454456

455457
// Map of role number to point and role data
456458
role_data_map.insert(
@@ -498,7 +500,7 @@ fn update_role_data(
498500
}
499501
},
500502
};
501-
let payment_key = get_shelley_addr_from_tx(txn, role_data.payment_key)?;
503+
let payment_key = get_payment_addr_from_tx(txn, role_data.payment_key)?;
502504

503505
// Map of role number to point and role data
504506
// Note that new role data will overwrite the old one
@@ -520,9 +522,9 @@ fn update_role_data(
520522
}
521523

522524
/// Helper function for retrieving the Shelley address from the transaction.
523-
fn get_shelley_addr_from_tx(
525+
fn get_payment_addr_from_tx(
524526
txn: &MultiEraTx, payment_key_ref: Option<i16>,
525-
) -> anyhow::Result<Cip19ShelleyAddrs> {
527+
) -> anyhow::Result<Option<ShelleyPaymentPart>> {
526528
// The index should exist since it pass the basic validation
527529
if let Some(key_ref) = payment_key_ref {
528530
if let MultiEraTx::Conway(tx) = txn {
@@ -535,11 +537,13 @@ fn get_shelley_addr_from_tx(
535537
pallas::ledger::primitives::conway::PseudoTransactionOutput::PostAlonzo(
536538
o,
537539
) => {
538-
let shelley_addr: Cip19ShelleyAddrs =
539-
o.address.clone().try_into().map_err(|_| {
540-
anyhow::anyhow!("Failed to convert Vec<u8> to Cip19ShelleyAddrs in payment key reference")
541-
})?;
542-
return Ok(shelley_addr);
540+
let address =
541+
Address::from_bytes(&o.address).map_err(|e| anyhow::anyhow!(e))?;
542+
543+
if let Address::Shelley(addr) = address {
544+
return Ok(Some(addr.payment().clone()));
545+
}
546+
bail!("Unsupported address type in payment key reference");
543547
},
544548
// Not support legacy form of transaction output
545549
pallas::ledger::primitives::conway::PseudoTransactionOutput::Legacy(_) => {
@@ -554,21 +558,27 @@ fn get_shelley_addr_from_tx(
554558
bail!("Unsupported payment key reference to transaction input");
555559
}
556560
}
557-
Ok(Cip19ShelleyAddrs::default())
561+
Ok(None)
558562
}
559563

560564
/// Update the payment history given the tracking payment keys.
561565
fn update_payment_history(
562-
tracking_key: &Cip19ShelleyAddrs, txn: &MultiEraTx, point_tx_idx: &PointTxIdx,
566+
tracking_key: &ShelleyPaymentPart, txn: &MultiEraTx, point_tx_idx: &PointTxIdx,
563567
) -> anyhow::Result<Vec<PaymentHistory>> {
564568
let mut payment_history = Vec::new();
565569
if let MultiEraTx::Conway(tx) = txn {
566570
// Conway era -> Post alonzo tx output
567571
for (index, output) in tx.transaction_body.outputs.iter().enumerate() {
568572
match output {
569573
pallas::ledger::primitives::conway::PseudoTransactionOutput::PostAlonzo(o) => {
570-
let address_bytes: Bytes = tracking_key.clone().into();
571-
if address_bytes == o.address {
574+
let address =
575+
Address::from_bytes(&o.address).map_err(|e| anyhow::anyhow!(e))?;
576+
let shelley_payment = if let Address::Shelley(addr) = address {
577+
addr.payment().clone()
578+
} else {
579+
bail!("Unsupported address type in update payment history");
580+
};
581+
if tracking_key == &shelley_payment {
572582
let output_index: u16 = index.try_into().map_err(|_| {
573583
anyhow::anyhow!("Cannot convert usize to u16 in update payment history")
574584
})?;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use std::collections::HashMap;
44

5-
use super::cip19_shelley_addr::Cip19ShelleyAddrs;
5+
use pallas::ledger::addresses::ShelleyPaymentPart;
6+
67
use crate::cardano::cip509::rbac::role_data::KeyLocalRef;
78

89
/// Role data
@@ -12,8 +13,8 @@ pub struct RoleData {
1213
signing_key_ref: Option<KeyLocalRef>,
1314
/// An encryption keys to the data within registration.
1415
encryption_ref: Option<KeyLocalRef>,
15-
/// A payment key (Shelley address) where reward will be distributed to.
16-
payment_key: Cip19ShelleyAddrs,
16+
/// A payment key where reward will be distributed to.
17+
payment_key: Option<ShelleyPaymentPart>,
1718
/// Map of role extended data (10-99) to its data
1819
role_extended_data: HashMap<u8, Vec<u8>>,
1920
}
@@ -22,7 +23,7 @@ impl RoleData {
2223
/// Create an instance of role data.
2324
pub(crate) fn new(
2425
signing_key_ref: Option<KeyLocalRef>, encryption_ref: Option<KeyLocalRef>,
25-
payment_key: Cip19ShelleyAddrs, role_extended_data: HashMap<u8, Vec<u8>>,
26+
payment_key: Option<ShelleyPaymentPart>, role_extended_data: HashMap<u8, Vec<u8>>,
2627
) -> Self {
2728
RoleData {
2829
signing_key_ref,
@@ -46,7 +47,7 @@ impl RoleData {
4647

4748
/// Get the payment key.
4849
#[must_use]
49-
pub fn payment_key(&self) -> &Cip19ShelleyAddrs {
50+
pub fn payment_key(&self) -> &Option<ShelleyPaymentPart> {
5051
&self.payment_key
5152
}
5253

0 commit comments

Comments
 (0)