Skip to content

Commit 372b59b

Browse files
More error handling
1 parent 5247896 commit 372b59b

File tree

1 file changed

+33
-34
lines changed
  • rust/rbac-registration/src/cardano/cip509

1 file changed

+33
-34
lines changed

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

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,6 @@ impl Cip509 {
194194
result
195195
}
196196

197-
/// Creates an "empty" `Cip509` instance with all optional fields set to `None`.
198-
fn with_decode_context(context: &DecodeContext) -> Self {
199-
let txn_hash = MultiEraTx::Conway(Box::new(Cow::Borrowed(context.txn)))
200-
.hash()
201-
.into();
202-
203-
Self {
204-
purpose: None,
205-
txn_inputs_hash: None,
206-
prv_tx_id: None,
207-
metadata: None,
208-
validation_signature: None,
209-
payment_history: context.payment_history.clone(),
210-
txn_hash,
211-
origin: context.origin.clone(),
212-
report: context.report.clone(),
213-
}
214-
}
215-
216197
/// Returns all role numbers present in this `Cip509` instance.
217198
pub fn all_roles(&self) -> Vec<RoleNumber> {
218199
if let Some(metadata) = &self.metadata {
@@ -288,7 +269,11 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509 {
288269
// below we should try to recover as much data as possible and not to return early.
289270
let map_len = decode_map_len(d, context)?;
290271

291-
let mut result = Self::with_decode_context(decode_context);
272+
let mut purpose = None;
273+
let mut txn_inputs_hash = None;
274+
let mut prv_tx_id = None;
275+
let mut validation_signature = None;
276+
let mut metadata = None;
292277

293278
let mut found_keys = Vec::new();
294279
let mut is_metadata_found = false;
@@ -297,7 +282,7 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509 {
297282
// We don't want to consume key here because it can be a part of chunked metadata that
298283
// is decoded below.
299284
let Ok(key) = d.probe().u8() else {
300-
result.report.other(
285+
decode_context.report.other(
301286
&format!("Unable to decode map key ({index} index)"),
302287
context,
303288
);
@@ -307,25 +292,26 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509 {
307292
// Consume the key. This should never fail because we used `probe` above.
308293
let _: u8 = decode_helper(d, context, &mut ())?;
309294

310-
if report_duplicated_key(&found_keys, &key, index, context, &result.report) {
295+
if report_duplicated_key(&found_keys, &key, index, context, &decode_context.report)
296+
{
311297
continue;
312298
}
313299
found_keys.push(key);
314300

315301
match key {
316302
Cip509IntIdentifier::Purpose => {
317-
result.purpose = decode_purpose(d, context, &result.report);
303+
purpose = decode_purpose(d, context, &decode_context.report);
318304
},
319305
Cip509IntIdentifier::TxInputsHash => {
320-
result.txn_inputs_hash = decode_input_hash(d, context, &result.report);
306+
txn_inputs_hash = decode_input_hash(d, context, &decode_context.report);
321307
},
322308
Cip509IntIdentifier::PreviousTxId => {
323-
result.prv_tx_id =
324-
decode_previous_transaction_id(d, context, &result.report);
309+
prv_tx_id =
310+
decode_previous_transaction_id(d, context, &decode_context.report);
325311
},
326312
Cip509IntIdentifier::ValidationSignature => {
327-
result.validation_signature =
328-
decode_validation_signature(d, context, &result.report);
313+
validation_signature =
314+
decode_validation_signature(d, context, &decode_context.report);
329315
},
330316
}
331317
} else {
@@ -334,7 +320,7 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509 {
334320
// metadata, but it isn't allowed. See this link for more details:
335321
// https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/README.md#keys-10-11-or-12---x509-chunked-data
336322
if is_metadata_found {
337-
result.report.duplicate_field(
323+
decode_context.report.duplicate_field(
338324
"metadata",
339325
"Only one instance of the chunked metadata should be present",
340326
context,
@@ -344,9 +330,9 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509 {
344330
is_metadata_found = true;
345331

346332
match X509Chunks::decode(d, decode_context) {
347-
Ok(chunks) => result.metadata = chunks.into(),
333+
Ok(chunks) => metadata = chunks.into(),
348334
Err(e) => {
349-
result.report.other(
335+
decode_context.report.other(
350336
&format!("Unable to decode metadata from chunks: {e:?}"),
351337
context,
352338
);
@@ -360,14 +346,27 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509 {
360346
Cip509IntIdentifier::TxInputsHash,
361347
Cip509IntIdentifier::ValidationSignature,
362348
];
363-
report_missing_keys(&found_keys, &required_keys, context, &result.report);
349+
report_missing_keys(&found_keys, &required_keys, context, &decode_context.report);
364350
if !is_metadata_found {
365-
result
351+
decode_context
366352
.report
367353
.missing_field("metadata (10, 11 or 12 chunks)", context);
368354
}
369355

370-
Ok(result)
356+
let txn_hash = MultiEraTx::Conway(Box::new(Cow::Borrowed(decode_context.txn)))
357+
.hash()
358+
.into();
359+
Ok(Self {
360+
purpose,
361+
txn_inputs_hash,
362+
prv_tx_id,
363+
metadata,
364+
validation_signature,
365+
payment_history: Default::default(),
366+
txn_hash,
367+
origin: decode_context.origin.clone(),
368+
report: decode_context.report.clone(),
369+
})
371370
}
372371
}
373372

0 commit comments

Comments
 (0)