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
810use ed25519_dalek:: VerifyingKey ;
911use 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 ) ]
2831pub 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.
153156fn 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`.
249251fn 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}
0 commit comments