Skip to content

Commit 76b20ca

Browse files
committed
fix: use Hash from pallas for tx hash
Signed-off-by: bkioshn <[email protected]>
1 parent afcaa09 commit 76b20ca

File tree

1 file changed

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

1 file changed

+15
-34
lines changed

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

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use minicbor::{
1313
decode::{self},
1414
Decode, Decoder,
1515
};
16-
use pallas::ledger::traverse::MultiEraTx;
16+
use pallas::{crypto::hash::Hash, ledger::traverse::MultiEraTx};
1717
use strum_macros::FromRepr;
1818
use validation::{
1919
validate_aux, validate_payment_key, validate_role_singing_key, validate_stake_public_key,
@@ -39,7 +39,7 @@ pub struct Cip509 {
3939
/// Transaction inputs hash.
4040
pub txn_inputs_hash: TxInputHash, // bytes .size 16
4141
/// Optional previous transaction ID.
42-
pub prv_tx_id: Option<TxHash>, // bytes .size 32
42+
pub prv_tx_id: Option<Hash<32>>, // bytes .size 32
4343
/// x509 chunks.
4444
pub x509_chunks: X509Chunks, // chunk_type => [ + x509_chunk ]
4545
/// Validation signature.
@@ -70,30 +70,6 @@ impl TryFrom<Vec<u8>> for UuidV4 {
7070
}
7171
}
7272

73-
/// Transaction hash representing in 32 bytes.
74-
#[derive(Debug, PartialEq, Clone, Default)]
75-
pub struct TxHash([u8; 32]);
76-
77-
impl From<[u8; 32]> for TxHash {
78-
fn from(bytes: [u8; 32]) -> Self {
79-
TxHash(bytes)
80-
}
81-
}
82-
83-
impl TryFrom<Vec<u8>> for TxHash {
84-
type Error = &'static str;
85-
86-
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
87-
if vec.len() == 32 {
88-
let mut array = [0u8; 32];
89-
array.copy_from_slice(&vec);
90-
Ok(TxHash(array))
91-
} else {
92-
Err("Input Vec must be exactly 32 bytes")
93-
}
94-
}
95-
}
96-
9773
/// Transaction input hash representing in 16 bytes.
9874
#[derive(Debug, PartialEq, Clone, Default)]
9975
pub struct TxInputHash([u8; 16]);
@@ -158,11 +134,12 @@ impl Decode<'_, ()> for Cip509 {
158134
})?;
159135
},
160136
Cip509IntIdentifier::PreviousTxId => {
161-
cip509_metadatum.prv_tx_id = Some(
162-
TxHash::try_from(decode_bytes(d, "CIP509 previous tx ID")?).map_err(
163-
|_| decode::Error::message("Invalid data size of PreviousTxId"),
164-
)?,
165-
);
137+
let prv_tx_hash: [u8; 32] = decode_bytes(d, "CIP509 previous tx ID")?
138+
.try_into()
139+
.map_err(|_| {
140+
decode::Error::message("Invalid data size of PreviousTxId")
141+
})?;
142+
cip509_metadatum.prv_tx_id = Some(Hash::from(prv_tx_hash));
166143
},
167144
Cip509IntIdentifier::ValidationSignature => {
168145
let validation_signature = decode_bytes(d, "CIP509 validation signature")?;
@@ -201,7 +178,7 @@ impl Cip509 {
201178
/// transaction: only check whether the index exist within the transaction
202179
/// inputs.
203180
/// * Role signing key validation for role 0 where the signing keys should only be the certificates
204-
///
181+
///
205182
/// See:
206183
/// * <https://github.com/input-output-hk/catalyst-CIPs/tree/x509-envelope-metadata/CIP-XXXX>
207184
/// * <https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/x509-envelope.cddl>
@@ -236,7 +213,11 @@ impl Cip509 {
236213
}
237214
}
238215
}
239-
tx_input_validate && aux_validate && stake_key_validate && payment_key_validate && signing_key
216+
tx_input_validate
217+
&& aux_validate
218+
&& stake_key_validate
219+
&& payment_key_validate
220+
&& signing_key
240221
}
241222
}
242223

@@ -270,7 +251,7 @@ mod tests {
270251

271252
assert_eq!(decoded_cip509.purpose, UuidV4(purpose));
272253
assert_eq!(decoded_cip509.txn_inputs_hash, TxInputHash(txn_inputs_hash));
273-
assert_eq!(decoded_cip509.prv_tx_id, Some(TxHash(prv_tx_id)));
254+
assert_eq!(decoded_cip509.prv_tx_id, Some(prv_tx_id.into()));
274255
assert_eq!(decoded_cip509.validation_signature, validation_signature);
275256
}
276257
}

0 commit comments

Comments
 (0)