Skip to content

Commit a723e32

Browse files
committed
fix(cardano-blockchain-types): fix linter
Signed-off-by: bkioshn <[email protected]>
1 parent 3a95a4d commit a723e32

File tree

7 files changed

+113
-51
lines changed

7 files changed

+113
-51
lines changed

rust/cardano-blockchain-types/src/cip36/key_registration.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//!
33
//! Catalyst registration data
44
//!
5-
//! https://cips.cardano.org/cip/CIP-36
6-
//! https://github.com/cardano-foundation/CIPs/blob/master/CIP-0036/schema.cddl
5+
//! <https://cips.cardano.org/cip/CIP-36>
6+
//! <https://github.com/cardano-foundation/CIPs/blob/master/CIP-0036/schema.cddl>
7+
8+
use std::collections::HashSet;
79

810
use ed25519_dalek::VerifyingKey;
911
use minicbor::{decode, Decode, Decoder};
@@ -24,6 +26,7 @@ use crate::utils::decode_helper::{decode_array_len, decode_bytes, decode_helper,
2426
/// ? 5 : $voting_purpose .default 0
2527
// }
2628
/// ```
29+
#[allow(clippy::module_name_repetitions)]
2730
#[derive(Clone, Default)]
2831
pub struct Cip36KeyRegistration {
2932
/// Is this CIP36 or CIP15 format.
@@ -91,7 +94,7 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
9194
if let Some(key) = Cip36KeyRegistrationKeys::from_repr(key) {
9295
match key {
9396
Cip36KeyRegistrationKeys::VotingKey => {
94-
if !found_keys.insert(key) {
97+
if !found_keys.insert(key as u16) {
9598
return Err(decode::Error::message(
9699
"Duplicate key in CIP36 Key Registration voting key",
97100
));
@@ -101,7 +104,7 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
101104
cip36_key_registration.voting_pks = voting_keys;
102105
},
103106
Cip36KeyRegistrationKeys::StakePk => {
104-
if !found_keys.insert(key) {
107+
if !found_keys.insert(key as u16) {
105108
return Err(decode::Error::message(
106109
"Duplicate key in CIP36 Key Registration stake public key",
107110
));
@@ -110,7 +113,7 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
110113
cip36_key_registration.stake_pk = stake_pk;
111114
},
112115
Cip36KeyRegistrationKeys::PaymentAddr => {
113-
if !found_keys.insert(key) {
116+
if !found_keys.insert(key as u16) {
114117
return Err(decode::Error::message(
115118
"Duplicate key in CIP36 Key Registration payment address",
116119
));
@@ -120,7 +123,7 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
120123
cip36_key_registration.is_payable = !shelley_addr.payment().is_script();
121124
},
122125
Cip36KeyRegistrationKeys::Nonce => {
123-
if !found_keys.insert(key) {
126+
if !found_keys.insert(key as u16) {
124127
return Err(decode::Error::message(
125128
"Duplicate key in CIP36 Key Registration nonce",
126129
));
@@ -129,7 +132,7 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
129132
cip36_key_registration.raw_nonce = raw_nonce;
130133
},
131134
Cip36KeyRegistrationKeys::Purpose => {
132-
if !found_keys.insert(key) {
135+
if !found_keys.insert(key as u16) {
133136
return Err(decode::Error::message(
134137
"Duplicate key in CIP36 Key Registration purpose",
135138
));
@@ -148,8 +151,8 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
148151
///
149152
/// # Returns
150153
///
151-
/// A tuple containing a boolean value, true if it is CIP36 format, false if it is CIP15 format
152-
/// and a vector of voting public keys.
154+
/// A tuple containing a boolean value, true if it is CIP36 format, false if it is CIP15
155+
/// format and a vector of voting public keys.
153156
fn decode_voting_key(d: &mut Decoder) -> Result<(bool, Vec<VotingPubKey>), decode::Error> {
154157
let mut voting_keys = Vec::new();
155158
let mut is_cip36 = false;
@@ -162,7 +165,7 @@ fn decode_voting_key(d: &mut Decoder) -> Result<(bool, Vec<VotingPubKey>), decod
162165
// ```
163166
minicbor::data::Type::Bytes => {
164167
let pub_key = decode_bytes(d, "CIP36 Key Registration voting key, single voting key")?;
165-
let vk = voting_pk_vec_to_verifying_key(pub_key).map_err(|e| {
168+
let vk = voting_pk_vec_to_verifying_key(&pub_key).map_err(|e| {
166169
decode::Error::message(format!(
167170
"CIP36 Key Registration voting key, singe voting key, {e}"
168171
))
@@ -203,7 +206,7 @@ fn decode_voting_key(d: &mut Decoder) -> Result<(bool, Vec<VotingPubKey>), decod
203206
&mut (),
204207
)?;
205208

206-
let vk = voting_pk_vec_to_verifying_key(pub_key).map_err(|e| {
209+
let vk = voting_pk_vec_to_verifying_key(&pub_key).map_err(|e| {
207210
decode::Error::message(format!(
208211
"CIP36 Key Registration voting key, multiple voting keys, {e}"
209212
))
@@ -216,19 +219,18 @@ fn decode_voting_key(d: &mut Decoder) -> Result<(bool, Vec<VotingPubKey>), decod
216219
}
217220
},
218221
_ => {
219-
return Err(decode::Error::message(format!(
220-
"Invalid datatype for CIP36 Key Registration voting key"
221-
)))
222+
return Err(decode::Error::message(
223+
"Invalid datatype for CIP36 Key Registration voting key",
224+
))
222225
},
223226
}
224227
Ok((is_cip36, voting_keys))
225228
}
226229

227-
/// Helper function for converting `Vec<u8>` to `VerifyingKey`.
228-
fn voting_pk_vec_to_verifying_key(pub_key: Vec<u8>) -> anyhow::Result<VerifyingKey> {
230+
/// Helper function for converting `&[u8]` to `VerifyingKey`.
231+
fn voting_pk_vec_to_verifying_key(pub_key: &[u8]) -> anyhow::Result<VerifyingKey> {
229232
VerifyingKey::from_bytes(
230233
pub_key
231-
.as_slice()
232234
.try_into()
233235
.map_err(|_| anyhow::anyhow!("Invalid verifying key length"))?,
234236
)
@@ -248,7 +250,7 @@ fn voting_pk_vec_to_verifying_key(pub_key: Vec<u8>) -> anyhow::Result<VerifyingK
248250
/// The stake public key as a `VerifyingKey`.
249251
fn decode_stake_pk(d: &mut Decoder) -> Result<VerifyingKey, decode::Error> {
250252
let pub_key = decode_bytes(d, "CIP36 Key Registration stake public key")?;
251-
voting_pk_vec_to_verifying_key(pub_key).map_err(|e| {
253+
voting_pk_vec_to_verifying_key(&pub_key).map_err(|e| {
252254
decode::Error::message(format!("CIP36 Key Registration stake public key, {e}"))
253255
})
254256
}

rust/cardano-blockchain-types/src/cip36/mod.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ pub struct Cip36 {
2323
pub is_catalyst_strict: bool,
2424
}
2525

26-
/// CIP36 Validation Report
26+
/// Validation value for CIP-36.
27+
#[allow(clippy::struct_excessive_bools, clippy::module_name_repetitions)]
2728
#[derive(Clone, Default)]
2829
pub struct Cip36Validation {
2930
/// Is the signature valid? (signature in 61285)
@@ -37,56 +38,90 @@ pub struct Cip36Validation {
3738
}
3839

3940
impl Cip36 {
41+
/// Get the `is_cip36` flag from the registration.
42+
/// True if it is CIP-36 format, false if CIP-15 format.
43+
#[must_use]
4044
pub fn is_cip36(&self) -> Option<bool> {
4145
self.key_registration.is_cip36
4246
}
4347

48+
/// Get the voting public keys from the registration.
49+
#[must_use]
4450
pub fn voting_pks(&self) -> Vec<VotingPubKey> {
4551
self.key_registration.voting_pks.clone()
4652
}
4753

54+
/// Get the stake public key from the registration.
55+
#[must_use]
4856
pub fn stake_pk(&self) -> VerifyingKey {
49-
self.key_registration.stake_pk.clone()
57+
self.key_registration.stake_pk
5058
}
5159

60+
/// Get the payment address from the registration.
61+
#[must_use]
5262
pub fn payment_address(&self) -> Option<ShelleyAddress> {
5363
self.key_registration.payment_addr.clone()
5464
}
5565

66+
/// Get the nonce from the registration.
67+
#[must_use]
5668
pub fn nonce(&self) -> u64 {
5769
self.key_registration.nonce
5870
}
5971

72+
/// Get the purpose from the registration.
73+
#[must_use]
6074
pub fn purpose(&self) -> u64 {
6175
self.key_registration.purpose
6276
}
6377

78+
/// Get the raw nonce from the registration.
79+
#[must_use]
6480
pub fn raw_nonce(&self) -> u64 {
6581
self.key_registration.raw_nonce
6682
}
6783

84+
/// Get the signature from the registration witness.
85+
#[must_use]
6886
pub fn signature(&self) -> Option<ed25519_dalek::Signature> {
69-
self.registration_witness.signature.clone()
87+
self.registration_witness.signature
7088
}
89+
90+
/// Get the Catalyst strict flag.
91+
#[must_use]
7192
pub fn is_strict_catalyst(&self) -> bool {
7293
self.is_catalyst_strict
7394
}
7495

96+
/// Validation for CIP-36
97+
/// The validation include the following:
98+
/// * Signature validation of the registration witness 61285 against the stake public
99+
/// key in key registration 61284.
100+
/// * Payment address network validation against the network. The given network should
101+
/// match the network tag within the payment address.
102+
/// * Purpose validation, the purpose should be 0 for Catalyst (when
103+
/// `is_strict_catalyst` is true).
104+
/// * Voting keys validation, Catalyst supports only a single voting key per
105+
/// registration when `is_strict_catalyst` is true.
106+
///
107+
/// # Parameters
108+
/// * `network` - The blockchain network.
109+
/// * `metadata` - The metadata value to be validated.
110+
/// * `validation_report` - Validation report to store the validation result.
75111
pub fn validate(
76112
&self, network: Network, metadata: &MetadatumValue, validation_report: &mut Vec<String>,
77113
) -> Cip36Validation {
78114
let is_valid_signature = validate_signature(self, metadata, validation_report);
79115
let is_valid_payment_address_network =
80-
validate_payment_address_network(&self, network, validation_report)
81-
.unwrap_or_default();
82-
let is_valid_purpose = validation::validate_purpose(self, validation_report);
116+
validate_payment_address_network(self, network, validation_report).unwrap_or_default();
83117
let is_valid_voting_keys = validate_voting_keys(self, validation_report);
118+
let is_valid_purpose = validation::validate_purpose(self, validation_report);
84119

85120
Cip36Validation {
86121
is_valid_signature,
87122
is_valid_payment_address_network,
88-
is_valid_purpose,
89123
is_valid_voting_keys,
124+
is_valid_purpose,
90125
}
91126
}
92127
}

rust/cardano-blockchain-types/src/cip36/registration_witness.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! CIP36 registration witness 61285
22
//!
3-
//! https://cips.cardano.org/cip/CIP-36
4-
//! https://github.com/cardano-foundation/CIPs/blob/master/CIP-0036/schema.cddl
3+
//! <https://cips.cardano.org/cip/CIP-36>
4+
//! <https://github.com/cardano-foundation/CIPs/blob/master/CIP-0036/schema.cddl>
55
66
use minicbor::{decode, Decode, Decoder};
77

@@ -14,6 +14,7 @@ use crate::utils::decode_helper::{decode_bytes, decode_helper, decode_map_len};
1414
/// 1 : $stake_witness
1515
/// }
1616
/// ```
17+
#[allow(clippy::module_name_repetitions)]
1718
#[derive(Clone, Default)]
1819
pub struct Cip36RegistrationWitness {
1920
/// Signature of the registration data.

rust/cardano-blockchain-types/src/cip36/validation.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Validation function for CIP-36
2+
13
use super::Cip36;
24
use crate::{MetadatumValue, Network};
35

@@ -22,20 +24,16 @@ pub(crate) fn validate_signature(
2224
.update(metadata.as_ref())
2325
.finalize();
2426

25-
let sig = match cip36.signature() {
26-
Some(sig) => sig,
27-
None => {
28-
validation_report.push(format!("Validate CIP36 Signature, signature is invalid"));
29-
return false;
30-
},
27+
let Some(sig) = cip36.signature() else {
28+
validation_report.push("Validate CIP36 Signature, signature is invalid".to_string());
29+
return false;
3130
};
3231

33-
match cip36.stake_pk().verify_strict(hash.as_bytes(), &sig) {
34-
Ok(_) => true,
35-
Err(_) => {
36-
validation_report.push(format!("Validate CIP36 Signature, cannot verify signature"));
37-
false
38-
},
32+
if let Ok(()) = cip36.stake_pk().verify_strict(hash.as_bytes(), &sig) {
33+
true
34+
} else {
35+
validation_report.push("Validate CIP36 Signature, cannot verify signature".to_string());
36+
false
3937
}
4038
}
4139

@@ -57,7 +55,7 @@ pub(crate) fn validate_payment_address_network(
5755

5856
Some(valid)
5957
} else {
60-
return None;
58+
None
6159
}
6260
}
6361

rust/cardano-blockchain-types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Catalyst Enhanced `MultiEraBlock` Structures
22
33
mod auxdata;
4+
mod cip36;
45
pub mod conversion;
56
mod fork;
67
pub mod hashes;
@@ -10,7 +11,6 @@ mod point;
1011
mod slot;
1112
mod txn_index;
1213
mod txn_witness;
13-
mod cip36;
1414
pub mod utils;
1515

1616
pub use auxdata::{

0 commit comments

Comments
 (0)