Skip to content

Commit 3847005

Browse files
committed
fix(cardano-blockchain-types): visibility, cip36 constructor
Signed-off-by: bkioshn <[email protected]>
1 parent 57a4384 commit 3847005

File tree

6 files changed

+65
-34
lines changed

6 files changed

+65
-34
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ pub use auxdata::{
2121
scripts::{Script, ScriptArray, ScriptType, TransactionScripts},
2222
};
2323
pub use fork::Fork;
24-
pub use metadata::cip36::{
25-
key_registration::Cip36KeyRegistration, registration_witness::Cip36RegistrationWitness,
26-
voting_pk::VotingPubKey, Cip36, Cip36Validation,
27-
};
24+
pub use metadata::cip36::{voting_pk::VotingPubKey, Cip36};
2825
pub use multi_era_block_data::MultiEraBlock;
2926
pub use network::Network;
3027
pub use point::Point;

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use super::voting_pk::VotingPubKey;
2828
/// ```
2929
#[allow(clippy::module_name_repetitions)]
3030
#[derive(Clone, Default, Debug)]
31-
pub struct Cip36KeyRegistration {
31+
pub(crate) struct Cip36KeyRegistration {
3232
/// Is this CIP36 or CIP15 format.
3333
pub is_cip36: Option<bool>,
3434
/// Voting public keys (called Delegations in the CIP-36 Spec).
@@ -55,7 +55,7 @@ pub struct Cip36KeyRegistration {
5555
/// Enum of CIP36 registration (61284) with its associated unsigned integer key.
5656
#[derive(FromRepr, Debug, PartialEq)]
5757
#[repr(u16)]
58-
pub enum Cip36KeyRegistrationKeys {
58+
enum Cip36KeyRegistrationKeys {
5959
/// Voting key.
6060
VotingKey = 1,
6161
/// Stake public key.
@@ -224,10 +224,7 @@ fn decode_voting_key(
224224
};
225225

226226
// Since there is 1 voting key, all the weight goes to this key = 1.
227-
voting_keys.push(VotingPubKey {
228-
voting_pk: vk,
229-
weight: 1,
230-
});
227+
voting_keys.push(VotingPubKey::new(vk, 1));
231228
},
232229
// CIP36 type registration (multiple voting keys).
233230
// ```cddl
@@ -279,10 +276,7 @@ fn decode_voting_key(
279276
},
280277
};
281278

282-
voting_keys.push(VotingPubKey {
283-
voting_pk: vk,
284-
weight,
285-
});
279+
voting_keys.push(VotingPubKey::new(vk, weight));
286280
}
287281
},
288282

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct Cip36 {
3030
/// Validation value for CIP-36.
3131
#[allow(clippy::struct_excessive_bools, clippy::module_name_repetitions)]
3232
#[derive(Clone, Default, Debug)]
33-
pub struct Cip36Validation {
33+
pub(crate) struct Cip36Validation {
3434
/// Is the signature valid? (signature in 61285)
3535
pub is_valid_signature: bool,
3636
/// Is the payment address on the correct network?
@@ -58,7 +58,7 @@ impl Cip36 {
5858
/// or if the CIP-36 key registration or registration witness metadata cannot be
5959
/// decoded.
6060
pub fn new(
61-
aux_data: &TransactionAuxData, is_catalyst_strict: bool, slot: Slot,
61+
aux_data: &TransactionAuxData, is_catalyst_strict: bool, slot: Slot, network: Network,
6262
) -> anyhow::Result<Self> {
6363
let Some(k61284) = aux_data.metadata(MetadatumLabel::CIP036_REGISTRATION) else {
6464
bail!("CIP-36 key registration metadata not found")
@@ -94,11 +94,25 @@ impl Cip36 {
9494
},
9595
};
9696

97-
Ok(Self {
97+
let cip36 = Self {
9898
key_registration,
9999
registration_witness,
100100
is_catalyst_strict,
101-
})
101+
};
102+
103+
let mut validation_report = Vec::new();
104+
// If the code reach here, then the CIP36 decoding is successful.
105+
let validation = cip36.validate(network, k61284, &mut validation_report);
106+
107+
if validation.is_valid_signature
108+
&& validation.is_valid_payment_address_network
109+
&& validation.is_valid_voting_keys
110+
&& validation.is_valid_purpose
111+
{
112+
Ok(cip36)
113+
} else {
114+
bail!("CIP-36 validation failed: {validation:?}, Reports: {validation_report:?}")
115+
}
102116
}
103117

104118
/// Get the `is_cip36` flag from the registration.
@@ -144,6 +158,12 @@ impl Cip36 {
144158
self.key_registration.raw_nonce
145159
}
146160

161+
/// Is the payment address in the registration payable?
162+
#[must_use]
163+
pub fn is_payable(&self) -> bool {
164+
self.key_registration.is_payable
165+
}
166+
147167
/// Get the signature from the registration witness.
148168
#[must_use]
149169
pub fn signature(&self) -> Option<ed25519_dalek::Signature> {
@@ -172,7 +192,7 @@ impl Cip36 {
172192
/// * `network` - The blockchain network.
173193
/// * `metadata` - The metadata value to be validated.
174194
/// * `validation_report` - Validation report to store the validation result.
175-
pub fn validate(
195+
fn validate(
176196
&self, network: Network, metadata: &MetadatumValue, validation_report: &mut Vec<String>,
177197
) -> Cip36Validation {
178198
let is_valid_signature = validate_signature(self, metadata, validation_report);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use minicbor::{decode, Decode, Decoder};
1515
/// ```
1616
#[allow(clippy::module_name_repetitions)]
1717
#[derive(Clone, Default, Debug)]
18-
pub struct Cip36RegistrationWitness {
18+
pub(crate) struct Cip36RegistrationWitness {
1919
/// Signature of the registration data.
2020
pub signature: Option<ed25519_dalek::Signature>,
2121
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ mod tests {
149149
#[test]
150150
fn test_validate_voting_keys() {
151151
let mut cip36 = create_empty_cip36(true);
152-
cip36.key_registration.voting_pks.push(VotingPubKey {
153-
voting_pk: Some(VerifyingKey::default()),
154-
weight: 1,
155-
});
152+
cip36
153+
.key_registration
154+
.voting_pks
155+
.push(VotingPubKey::new(Some(VerifyingKey::default()), 1));
156156
let mut report = Vec::new();
157157

158158
let valid = validate_voting_keys(&cip36, &mut report);
@@ -164,14 +164,14 @@ mod tests {
164164
#[test]
165165
fn test_validate_invalid_voting_keys() {
166166
let mut cip36 = create_empty_cip36(true);
167-
cip36.key_registration.voting_pks.push(VotingPubKey {
168-
voting_pk: Some(VerifyingKey::default()),
169-
weight: 1,
170-
});
171-
cip36.key_registration.voting_pks.push(VotingPubKey {
172-
voting_pk: Some(VerifyingKey::default()),
173-
weight: 1,
174-
});
167+
cip36
168+
.key_registration
169+
.voting_pks
170+
.push(VotingPubKey::new(Some(VerifyingKey::default()), 1));
171+
cip36
172+
.key_registration
173+
.voting_pks
174+
.push(VotingPubKey::new(Some(VerifyingKey::default()), 1));
175175
let mut report = Vec::new();
176176

177177
let valid = validate_voting_keys(&cip36, &mut report);

rust/cardano-blockchain-types/src/metadata/cip36/voting_pk.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,27 @@ use ed25519_dalek::VerifyingKey;
66
#[derive(Clone, Debug)]
77
pub struct VotingPubKey {
88
/// Voting public key.
9-
pub voting_pk: Option<VerifyingKey>,
9+
voting_pk: Option<VerifyingKey>,
1010
/// Voting key associated weight.
11-
pub weight: u32,
11+
weight: u32,
12+
}
13+
14+
impl VotingPubKey {
15+
/// Create a new voting public key.
16+
#[must_use]
17+
pub fn new(voting_pk: Option<VerifyingKey>, weight: u32) -> Self {
18+
Self { voting_pk, weight }
19+
}
20+
21+
/// Get the voting public key.
22+
#[must_use]
23+
pub fn voting_pk(&self) -> Option<&VerifyingKey> {
24+
self.voting_pk.as_ref()
25+
}
26+
27+
/// Get the voting key weight.
28+
#[must_use]
29+
pub fn weight(&self) -> u32 {
30+
self.weight
31+
}
1232
}

0 commit comments

Comments
 (0)