Skip to content

Commit b51687f

Browse files
Update the payment key logic
1 parent 5934fd4 commit b51687f

File tree

4 files changed

+13
-89
lines changed

4 files changed

+13
-89
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct CborRoleData {
2323
/// Optional role encryption key.
2424
pub encryption_key: Option<KeyLocalRef>,
2525
/// Optional payment key.
26-
pub payment_key: Option<i16>,
26+
pub payment_key: Option<u16>,
2727
/// Optional role extended data keys.
2828
/// Empty map if no role extended data keys.
2929
pub extended_data: HashMap<u8, Vec<u8>>,

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

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ use pallas::ledger::{
1010
traverse::MultiEraTx,
1111
};
1212

13-
use crate::{
14-
cardano::cip509::{
15-
rbac::role_data::CborRoleData,
16-
utils::cip19::{compare_key_hash, extract_key_hash},
17-
KeyLocalRef, RoleNumber,
18-
},
19-
utils::general::decremented_index,
13+
use crate::cardano::cip509::{
14+
rbac::role_data::CborRoleData,
15+
utils::cip19::{compare_key_hash, extract_key_hash},
16+
KeyLocalRef, RoleNumber,
2017
};
2118

2219
/// A role data.
@@ -94,59 +91,10 @@ impl RoleData {
9491

9592
/// Converts the payment key from the form encoded in CBOR role data to `ShelleyAddress`.
9693
fn convert_payment_key(
97-
key: Option<i16>, txn: &conway::MintedTx, context: &str, report: &ProblemReport,
94+
index: Option<u16>, txn: &conway::MintedTx, context: &str, report: &ProblemReport,
9895
) -> Option<ShelleyAddress> {
99-
let key = key?;
96+
let index: usize = index?.into();
10097

101-
if key == 0 {
102-
report.invalid_value(
103-
"payment key",
104-
"0",
105-
"Payment reference key must not be 0",
106-
context,
107-
);
108-
return None;
109-
}
110-
111-
let index = match decremented_index(key.abs()) {
112-
Ok(value) => value,
113-
Err(e) => {
114-
report.other(
115-
&format!("Invalid index ({key:?}) of the payment key: {e:?}"),
116-
context,
117-
);
118-
return None;
119-
},
120-
};
121-
122-
// Negative indicates reference to transaction output.
123-
if key < 0 {
124-
convert_transaction_output(index, txn, context, report)
125-
} else {
126-
// Positive indicates reference to tx input.
127-
let inputs = &txn.transaction_body.inputs;
128-
// Check whether the index exists in transaction inputs.
129-
if inputs.get(index).is_none() {
130-
report.other(
131-
&format!(
132-
"Role payment key reference index ({index}) is not found in transaction inputs"
133-
),
134-
context,
135-
);
136-
}
137-
138-
report.other(
139-
&format!("Payment key reference ({key:?}) to transaction input is unsupported"),
140-
context,
141-
);
142-
None
143-
}
144-
}
145-
146-
/// Converts payment key transaction output reference to `ShelleyAddress`.
147-
fn convert_transaction_output(
148-
index: usize, txn: &conway::MintedTx, context: &str, report: &ProblemReport,
149-
) -> Option<ShelleyAddress> {
15098
let outputs = &txn.transaction_body.outputs;
15199
let txn = MultiEraTx::Conway(Box::new(Cow::Borrowed(txn)));
152100
let witness = match TxnWitness::new(&[txn]) {
@@ -161,7 +109,9 @@ fn convert_transaction_output(
161109
Some(conway::PseudoTransactionOutput::PostAlonzo(o)) => &o.address,
162110
Some(conway::PseudoTransactionOutput::Legacy(_)) => {
163111
report.other(
164-
&format!("Unsupported legacy transaction output type in payment key reference (index = {index})"),
112+
&format!(
113+
"Unsupported legacy transaction output type in payment key index ({index})"
114+
),
165115
context,
166116
);
167117
return None;
@@ -177,20 +127,19 @@ fn convert_transaction_output(
177127
},
178128
};
179129
validate_payment_output(address, &witness, context, report);
130+
180131
match Address::from_bytes(address) {
181132
Ok(Address::Shelley(a)) => Some(a),
182133
Ok(a) => {
183134
report.other(
184-
&format!(
185-
"Unsupported address type ({a:?}) in payment key reference (index = {index})"
186-
),
135+
&format!("Unsupported address type ({a:?}) in payment key index ({index})"),
187136
context,
188137
);
189138
None
190139
},
191140
Err(e) => {
192141
report.other(
193-
&format!("Invalid address in payment key reference (index = {index}): {e:?}"),
142+
&format!("Invalid address in payment key index ({index}): {e:?}"),
194143
context,
195144
);
196145
None

rust/rbac-registration/src/utils/general.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Utility functions for the RBAC registration module.
22
33
pub mod decode_helper;
4-
// TODO: FIXME: Remove module or pub crate?
5-
pub(crate) mod general;
64

75
#[cfg(test)]
86
pub mod test;

0 commit comments

Comments
 (0)