@@ -30,26 +30,33 @@ use super::voting_pk::VotingPubKey;
3030#[ derive( Clone , Default , Debug ) ]
3131pub ( crate ) struct Cip36KeyRegistration {
3232 /// Is this CIP36 or CIP15 format.
33+ /// None if not either CIP36 or CIP15.
3334 pub is_cip36 : Option < bool > ,
3435 /// Voting public keys (called Delegations in the CIP-36 Spec).
3536 /// Field 1 in the CIP-36 61284 Spec.
3637 pub voting_pks : Vec < VotingPubKey > ,
3738 /// Stake public key to associate with the voting keys.
3839 /// Field 2 in the CIP-36 61284 Spec.
39- pub stake_pk : VerifyingKey ,
40+ /// None if it is not set.
41+ pub stake_pk : Option < VerifyingKey > ,
4042 /// Payment Address to associate with the voting keys.
4143 /// Field 3 in the CIP-36 61284 Spec.
44+ /// None if it is not set.
4245 pub payment_addr : Option < ShelleyAddress > ,
4346 /// Nonce (nonce that has been slot corrected).
4447 /// Field 4 in the CIP-36 61284 Spec.
45- pub nonce : u64 ,
48+ /// None if it is not set.
49+ pub nonce : Option < u64 > ,
4650 /// Registration Purpose (Always 0 for Catalyst).
4751 /// Field 5 in the CIP-36 61284 Spec.
48- pub purpose : u64 ,
52+ /// None if it is not set.
53+ pub purpose : Option < u64 > ,
4954 /// Raw nonce (nonce that has not had slot correction applied).
50- pub raw_nonce : u64 ,
55+ /// None if it is not set.
56+ pub raw_nonce : Option < u64 > ,
5157 /// Is payment address payable? (not a script)
52- pub is_payable : bool ,
58+ /// None if it is not set.
59+ pub is_payable : Option < bool > ,
5360}
5461
5562/// Enum of CIP36 registration (61284) with its associated unsigned integer key.
@@ -74,9 +81,6 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
7481
7582 let mut cip36_key_registration = Cip36KeyRegistration :: default ( ) ;
7683
77- // Record of founded keys. Check for duplicate keys in the map
78- let mut found_keys: Vec < Cip36KeyRegistrationKeys > = Vec :: new ( ) ;
79-
8084 // Record of errors found during decoding
8185 let mut err_report = Vec :: new ( ) ;
8286
@@ -86,11 +90,11 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
8690 if let Some ( key) = Cip36KeyRegistrationKeys :: from_repr ( key) {
8791 match key {
8892 Cip36KeyRegistrationKeys :: VotingKey => {
89- if found_keys . contains ( & key ) {
93+ if !cip36_key_registration . voting_pks . is_empty ( ) {
9094 err_report. push ( format ! (
9195 "Duplicate key in CIP36 Key Registration voting key at item {} in map" , index + 1 ) ,
9296 ) ;
93- continue ;
97+ continue ;
9498 }
9599 if let Some ( ( is_cip36, voting_keys) ) = decode_voting_key ( d, & mut err_report)
96100 {
@@ -99,77 +103,67 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
99103 }
100104 } ,
101105 Cip36KeyRegistrationKeys :: StakePk => {
102- if found_keys . contains ( & key ) {
106+ if cip36_key_registration . stake_pk . is_some ( ) {
103107 err_report. push ( format ! (
104108 "Duplicate key in CIP36 Key Registration stake public key at item {} in map" , index + 1 ) ,
105109 ) ;
106110 continue ;
107111 }
108112 if let Some ( stake_pk) = decode_stake_pk ( d, & mut err_report) {
109- cip36_key_registration. stake_pk = stake_pk;
113+ cip36_key_registration. stake_pk = Some ( stake_pk) ;
110114 }
111115 } ,
112116 Cip36KeyRegistrationKeys :: PaymentAddr => {
113- if found_keys . contains ( & key ) {
117+ if cip36_key_registration . payment_addr . is_some ( ) {
114118 err_report. push ( format ! (
115119 "Duplicate key in CIP36 Key Registration payment address at item {} in map" , index + 1 ) ,
116120 ) ;
117121 continue ;
118122 }
119123 if let Some ( shelley_addr) = decode_payment_addr ( d, & mut err_report) {
120124 cip36_key_registration. payment_addr = Some ( shelley_addr. clone ( ) ) ;
121- cip36_key_registration. is_payable = !shelley_addr. payment ( ) . is_script ( ) ;
125+ cip36_key_registration. is_payable = Some ( !shelley_addr. payment ( ) . is_script ( ) ) ;
122126 }
123127 } ,
124128 Cip36KeyRegistrationKeys :: Nonce => {
125- if found_keys . contains ( & key ) {
129+ if cip36_key_registration . raw_nonce . is_some ( ) {
126130 err_report. push ( format ! (
127- "Duplicate key in CIP36 Key Registration nonce at item {} in map" ,
128- index + 1
129- ) ) ;
131+ "Duplicate key in CIP36 Key Registration nonce at item {} in map" , index + 1 ) ,
132+ ) ;
130133 continue ;
131134 }
132135 if let Some ( nonce) = decode_nonce ( d, & mut err_report) {
133- cip36_key_registration. nonce = nonce;
136+ cip36_key_registration. raw_nonce = Some ( nonce) ;
134137 }
135138 } ,
136139 Cip36KeyRegistrationKeys :: Purpose => {
137- if found_keys . contains ( & key ) {
140+ if cip36_key_registration . purpose . is_some ( ) {
138141 err_report. push ( format ! (
139- "Duplicate key in CIP36 Key Registration purpose at item {} in map" ,
140- index + 1
141- ) ) ;
142+ "Duplicate key in CIP36 Key Registration purpose at item {} in map" , index + 1 ) ,
143+ ) ;
142144 continue ;
143145 }
144146 if let Some ( purpose) = decode_purpose ( d, & mut err_report) {
145- cip36_key_registration. purpose = purpose;
147+ cip36_key_registration. purpose = Some ( purpose) ;
146148 }
147149 } ,
148150 }
149-
150- // Update the founded keys.
151- found_keys. push ( key) ;
152151 }
153152 }
154153
155- if !found_keys. contains ( & Cip36KeyRegistrationKeys :: VotingKey ) {
156- err_report
157- . push ( "Missing required key in CIP36 Key Registration: Voting Key" . to_string ( ) ) ;
154+ if cip36_key_registration. voting_pks . is_empty ( ) {
155+ err_report. push ( "Missing required key in CIP36 Key Registration: Voting Key" . to_string ( ) ) ;
158156 }
159157
160- if !found_keys. contains ( & Cip36KeyRegistrationKeys :: StakePk ) {
161- err_report. push (
162- "Missing required key in CIP36 Key Registration: Stake Public Key" . to_string ( ) ,
163- ) ;
158+ if cip36_key_registration. stake_pk . is_none ( ) {
159+ err_report. push ( "Missing required key in CIP36 Key Registration: Stake Public Key" . to_string ( ) ) ;
164160 }
165161
166- if !found_keys. contains ( & Cip36KeyRegistrationKeys :: PaymentAddr ) {
167- err_report. push (
168- "Missing required key in CIP36 Key Registration: Payment Address" . to_string ( ) ,
169- ) ;
162+ if cip36_key_registration. payment_addr . is_none ( ) {
163+ err_report. push ( "Missing required key in CIP36 Key Registration: Payment Address" . to_string ( ) ) ;
170164 }
171165
172- if !found_keys . contains ( & Cip36KeyRegistrationKeys :: Nonce ) {
166+ if cip36_key_registration . raw_nonce . is_none ( ) {
173167 err_report. push ( "Missing required key in CIP36 Key Registration: Nonce" . to_string ( ) ) ;
174168 }
175169
@@ -459,4 +453,14 @@ mod tests {
459453 assert ! ( !is_cip36. unwrap( ) ) ;
460454 assert_eq ! ( voting_pk. len( ) , 1 ) ;
461455 }
456+
457+ #[ test]
458+ fn test_decode_nonce ( ) {
459+ let hex_data = hex:: decode ( "1A014905D1" ) . expect ( "cannot decode hex" ) ;
460+ let mut decoder = Decoder :: new ( & hex_data) ;
461+ let mut err_report = Vec :: new ( ) ;
462+ let nonce = decode_nonce ( & mut decoder, & mut err_report) ;
463+ assert ! ( err_report. is_empty( ) ) ;
464+ assert_eq ! ( nonce. unwrap( ) , 21562833 ) ;
465+ }
462466}
0 commit comments