Skip to content

Commit 3a95a4d

Browse files
committed
fix(cardano-blockchain-types): check dup keys
Signed-off-by: bkioshn <[email protected]>
1 parent 65c781e commit 3a95a4d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,58 @@ impl Decode<'_, ()> for Cip36KeyRegistration {
8282

8383
let mut cip36_key_registration = Cip36KeyRegistration::default();
8484

85+
// Record of founded keys. Check for duplicate keys in the map
86+
let mut found_keys: HashSet<u16> = HashSet::new();
87+
8588
for _ in 0..map_len {
8689
let key: u16 = decode_helper(d, "key in CIP36 Key Registration", ctx)?;
8790

8891
if let Some(key) = Cip36KeyRegistrationKeys::from_repr(key) {
8992
match key {
9093
Cip36KeyRegistrationKeys::VotingKey => {
94+
if !found_keys.insert(key) {
95+
return Err(decode::Error::message(
96+
"Duplicate key in CIP36 Key Registration voting key",
97+
));
98+
}
9199
let (is_cip36, voting_keys) = decode_voting_key(d)?;
92100
cip36_key_registration.is_cip36 = Some(is_cip36);
93101
cip36_key_registration.voting_pks = voting_keys;
94102
},
95103
Cip36KeyRegistrationKeys::StakePk => {
104+
if !found_keys.insert(key) {
105+
return Err(decode::Error::message(
106+
"Duplicate key in CIP36 Key Registration stake public key",
107+
));
108+
}
96109
let stake_pk = decode_stake_pk(d)?;
97110
cip36_key_registration.stake_pk = stake_pk;
98111
},
99112
Cip36KeyRegistrationKeys::PaymentAddr => {
113+
if !found_keys.insert(key) {
114+
return Err(decode::Error::message(
115+
"Duplicate key in CIP36 Key Registration payment address",
116+
));
117+
}
100118
let shelley_addr = decode_payment_addr(d)?;
101119
cip36_key_registration.payment_addr = Some(shelley_addr.clone());
102120
cip36_key_registration.is_payable = !shelley_addr.payment().is_script();
103121
},
104122
Cip36KeyRegistrationKeys::Nonce => {
123+
if !found_keys.insert(key) {
124+
return Err(decode::Error::message(
125+
"Duplicate key in CIP36 Key Registration nonce",
126+
));
127+
}
105128
let raw_nonce = decode_nonce(d)?;
106129
cip36_key_registration.raw_nonce = raw_nonce;
107130
},
108131
Cip36KeyRegistrationKeys::Purpose => {
132+
if !found_keys.insert(key) {
133+
return Err(decode::Error::message(
134+
"Duplicate key in CIP36 Key Registration purpose",
135+
));
136+
}
109137
let purpose = decode_purpose(d)?;
110138
cip36_key_registration.purpose = purpose;
111139
},

0 commit comments

Comments
 (0)