Skip to content

Commit 9a42242

Browse files
committed
rest of the library adjusted
1 parent 64aad5f commit 9a42242

File tree

6 files changed

+75
-78
lines changed

6 files changed

+75
-78
lines changed

mithril-aggregator/src/multi_signer.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,19 @@ impl MultiSigner for MultiSignerImpl {
130130
self.aggregate_signature_type,
131131
) {
132132
Ok(multi_signature) => Ok(Some(multi_signature)),
133-
Err(ProtocolAggregationError::NotEnoughSignatures(actual, expected)) => {
134-
warn!(
135-
self.logger,
136-
"Could not compute multi-signature: Not enough signatures. Got only {actual} out of {expected}."
137-
);
138-
Ok(None)
139-
}
140-
Err(err) => Err(anyhow!(err).context(format!(
141-
"Multi Signer can not create multi-signature for entity type '{:?}'",
142-
open_message.signed_entity_type
143-
))),
133+
Err(err) => match err.downcast_ref::<ProtocolAggregationError>() {
134+
Some(ProtocolAggregationError::NotEnoughSignatures(actual, expected)) => {
135+
warn!(
136+
self.logger,
137+
"Could not compute multi-signature: Not enough signatures. Got only {actual} out of {expected}."
138+
);
139+
Ok(None)
140+
}
141+
_ => Err(anyhow!(err).context(format!(
142+
"Multi Signer can not create multi-signature for entity type '{:?}'",
143+
open_message.signed_entity_type
144+
))),
145+
},
144146
}
145147
}
146148
}

mithril-common/src/crypto_helper/cardano/key_certification.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
use std::{collections::HashMap, sync::Arc};
77

8+
use anyhow::anyhow;
89
use blake2::{
910
Blake2b, Digest,
1011
digest::{FixedOutput, consts::U32},
@@ -173,13 +174,8 @@ impl StmInitializerWrapper {
173174
/// * the current total stake (according to the registration service)
174175
/// # Error
175176
/// This function fails if the initializer is not registered.
176-
pub fn new_signer(
177-
self,
178-
closed_reg: ClosedKeyRegistration<D>,
179-
) -> Result<Signer<D>, ProtocolRegistrationErrorWrapper> {
180-
self.stm_initializer
181-
.create_signer(closed_reg)
182-
.map_err(ProtocolRegistrationErrorWrapper::CoreRegister)
177+
pub fn new_signer(self, closed_reg: ClosedKeyRegistration<D>) -> StdResult<Signer<D>> {
178+
self.stm_initializer.create_signer(closed_reg)
183179
}
184180

185181
/// Convert to bytes
@@ -199,7 +195,7 @@ impl StmInitializerWrapper {
199195
/// Convert a slice of bytes to an `StmInitializerWrapper`
200196
/// # Error
201197
/// The function fails if the given string of bytes is not of required size.
202-
pub fn from_bytes(bytes: &[u8]) -> Result<Self, RegisterError> {
198+
pub fn from_bytes(bytes: &[u8]) -> StdResult<Self> {
203199
let stm_initializer =
204200
Initializer::from_bytes(bytes.get(..256).ok_or(RegisterError::SerializationError)?)?;
205201
let bytes = bytes.get(256..).ok_or(RegisterError::SerializationError)?;
@@ -250,7 +246,7 @@ impl KeyRegWrapper {
250246
kes_sig: Option<ProtocolSignerVerificationKeySignature>, // Used for only for testing when SPO pool id is not certified
251247
kes_period: Option<KesPeriod>,
252248
pk: ProtocolSignerVerificationKey,
253-
) -> Result<ProtocolPartyId, ProtocolRegistrationErrorWrapper> {
249+
) -> StdResult<ProtocolPartyId> {
254250
let pool_id_bech32: ProtocolPartyId = if let Some(opcert) = opcert {
255251
let signature = kes_sig.ok_or(ProtocolRegistrationErrorWrapper::KesSignatureMissing)?;
256252
let kes_period =
@@ -264,9 +260,11 @@ impl KeyRegWrapper {
264260
.compute_protocol_party_id()
265261
.map_err(|_| ProtocolRegistrationErrorWrapper::PoolAddressEncoding)?
266262
} else {
267-
return Err(ProtocolRegistrationErrorWrapper::KesSignatureInvalid(
268-
kes_period,
269-
opcert.get_start_kes_period(),
263+
return Err(anyhow!(
264+
ProtocolRegistrationErrorWrapper::KesSignatureInvalid(
265+
kes_period,
266+
opcert.get_start_kes_period(),
267+
)
270268
));
271269
}
272270
} else {
@@ -277,12 +275,12 @@ impl KeyRegWrapper {
277275
};
278276

279277
if let Some(&stake) = self.stake_distribution.get(&pool_id_bech32) {
280-
self.stm_key_reg
281-
.register(stake, pk.into())
282-
.map_err(ProtocolRegistrationErrorWrapper::CoreRegister)?;
278+
self.stm_key_reg.register(stake, pk.into())?;
283279
return Ok(pool_id_bech32);
284280
}
285-
Err(ProtocolRegistrationErrorWrapper::PartyIdNonExisting)
281+
Err(anyhow!(
282+
ProtocolRegistrationErrorWrapper::PartyIdNonExisting
283+
))
286284
}
287285

288286
/// Finalize the key registration.

mithril-common/src/crypto_helper/codec/binary.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ mod binary_mithril_stm {
6161

6262
impl TryFromBytes for SingleSignature {
6363
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
64-
Self::from_bytes::<D>(bytes).map_err(|e| e.into())
64+
Self::from_bytes::<D>(bytes).map_err(|e| anyhow!(e))
6565
}
6666
}
6767

@@ -73,7 +73,7 @@ mod binary_mithril_stm {
7373

7474
impl TryFromBytes for SingleSignatureWithRegisteredParty {
7575
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
76-
Self::from_bytes::<D>(bytes).map_err(|e| e.into())
76+
Self::from_bytes::<D>(bytes).map_err(|e| anyhow!(e))
7777
}
7878
}
7979

@@ -97,7 +97,7 @@ mod binary_mithril_stm {
9797

9898
impl TryFromBytes for VerificationKey {
9999
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
100-
Self::from_bytes(bytes).map_err(|e| e.into())
100+
Self::from_bytes(bytes).map_err(|e| anyhow!(e))
101101
}
102102
}
103103

@@ -109,7 +109,7 @@ mod binary_mithril_stm {
109109

110110
impl TryFromBytes for VerificationKeyProofOfPossession {
111111
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
112-
Self::from_bytes(bytes).map_err(|e| e.into())
112+
Self::from_bytes(bytes).map_err(|e| anyhow!(e))
113113
}
114114
}
115115

@@ -139,7 +139,7 @@ mod binary_mithril_stm {
139139

140140
impl TryFromBytes for Initializer {
141141
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
142-
Self::from_bytes(bytes).map_err(|e| e.into())
142+
Self::from_bytes(bytes).map_err(|e| anyhow!(e))
143143
}
144144
}
145145
}

mithril-common/src/protocol/multi_signer.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use anyhow::{Context, anyhow};
1+
use anyhow::anyhow;
22
use mithril_stm::{AggregateSignatureType, Parameters};
33

44
use crate::{
55
StdResult,
6-
crypto_helper::{
7-
ProtocolAggregateVerificationKey, ProtocolAggregationError, ProtocolClerk,
8-
ProtocolMultiSignature,
9-
},
6+
crypto_helper::{ProtocolAggregateVerificationKey, ProtocolClerk, ProtocolMultiSignature},
107
entities::SingleSignature,
118
protocol::ToMessage,
129
};
@@ -31,7 +28,7 @@ impl MultiSigner {
3128
single_signatures: &[SingleSignature],
3229
message: &T,
3330
aggregate_signature_type: AggregateSignatureType,
34-
) -> Result<ProtocolMultiSignature, ProtocolAggregationError> {
31+
) -> StdResult<ProtocolMultiSignature> {
3532
let protocol_signatures: Vec<_> = single_signatures
3633
.iter()
3734
.map(|single_signature| single_signature.to_protocol_signature())
@@ -73,30 +70,30 @@ impl MultiSigner {
7370
))
7471
})?;
7572

76-
protocol_signature
77-
.verify(
78-
&self.protocol_parameters,
79-
&vk,
80-
&stake,
81-
&avk,
82-
message.to_message().as_bytes(),
83-
)
84-
.with_context(|| {
85-
format!(
86-
"Invalid signature for party: '{}'",
87-
single_signature.party_id
88-
)
89-
})?;
73+
protocol_signature.verify(
74+
&self.protocol_parameters,
75+
&vk,
76+
&stake,
77+
&avk,
78+
message.to_message().as_bytes(),
79+
)?;
80+
// .with_context(|| {
81+
// format!(
82+
// "Invalid signature for party: '{}'",
83+
// single_signature.party_id
84+
// )
85+
// })?;
9086

9187
Ok(())
9288
}
9389
}
9490

9591
#[cfg(test)]
9692
mod test {
97-
use mithril_stm::StmSignatureError;
93+
use mithril_stm::MultiSignatureError;
9894

9995
use crate::{
96+
crypto_helper::ProtocolAggregationError,
10097
entities::{ProtocolMessage, ProtocolMessagePartKey, ProtocolParameters},
10198
protocol::SignerBuilder,
10299
test::{
@@ -129,7 +126,10 @@ mod test {
129126
);
130127

131128
assert!(
132-
matches!(error, ProtocolAggregationError::NotEnoughSignatures(_, _)),
129+
matches!(
130+
error.downcast_ref::<ProtocolAggregationError>(),
131+
Some(ProtocolAggregationError::NotEnoughSignatures(_, _))
132+
),
133133
"Expected ProtocolAggregationError::NotEnoughSignatures, got: {error:?}"
134134
)
135135
}
@@ -194,8 +194,8 @@ mod test {
194194
"Verify single signature should fail if the signer isn't in the registered parties",
195195
);
196196

197-
match error.downcast_ref::<StmSignatureError>() {
198-
Some(StmSignatureError::SignatureInvalid(_)) => (),
197+
match error.downcast_ref::<MultiSignatureError>() {
198+
Some(MultiSignatureError::SignatureInvalid(_)) => (),
199199
_ => panic!("Expected an SignatureInvalid error, got: {error:?}"),
200200
}
201201
}
@@ -220,8 +220,8 @@ mod test {
220220
.verify_single_signature(&ProtocolMessage::default(), &single_signature)
221221
.expect_err("Verify single signature should fail");
222222

223-
match error.downcast_ref::<StmSignatureError>() {
224-
Some(StmSignatureError::SignatureInvalid(_)) => (),
223+
match error.downcast_ref::<MultiSignatureError>() {
224+
Some(MultiSignatureError::SignatureInvalid(_)) => (),
225225
_ => panic!("Expected an SignatureInvalid error, got: {error:?}"),
226226
}
227227
}

mithril-common/src/protocol/signer_builder.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ impl SignerBuilder {
4949
let mut key_registration = ProtocolKeyRegistration::init(&stake_distribution);
5050

5151
for signer in registered_signers {
52-
key_registration
53-
.register(
54-
Some(signer.party_id.to_owned()),
55-
signer.operational_certificate.clone(),
56-
signer.verification_key_signature,
57-
signer.kes_period,
58-
signer.verification_key,
59-
)
60-
.with_context(|| {
61-
format!("Registration failed for signer: '{}'", signer.party_id)
62-
})?;
52+
key_registration.register(
53+
Some(signer.party_id.to_owned()),
54+
signer.operational_certificate.clone(),
55+
signer.verification_key_signature,
56+
signer.kes_period,
57+
signer.verification_key,
58+
)?;
59+
// .with_context(|| {
60+
// format!("Registration failed for signer: '{}'", signer.party_id)
61+
// })?;
6362
}
6463

6564
let closed_registration = key_registration.close();
@@ -177,7 +176,7 @@ mod test {
177176
use mithril_stm::RegisterError;
178177

179178
use crate::{
180-
crypto_helper::{KesSignerStandard, ProtocolRegistrationErrorWrapper},
179+
crypto_helper::KesSignerStandard,
181180
test::{builder::MithrilFixtureBuilder, double::fake_data},
182181
};
183182

@@ -220,8 +219,8 @@ mod test {
220219
"We should not be able to construct a signer builder if a signer registration fail",
221220
);
222221

223-
match error.downcast_ref::<ProtocolRegistrationErrorWrapper>() {
224-
Some(ProtocolRegistrationErrorWrapper::CoreRegister(_)) => (),
222+
match error.downcast_ref::<RegisterError>() {
223+
Some(RegisterError::KeyRegistered { .. }) => (),
225224
_ => panic!("Expected an CoreRegister error, got: {error:?}"),
226225
}
227226
}
@@ -264,10 +263,8 @@ mod test {
264263
"We should not be able to construct a single signer from a not registered party",
265264
);
266265

267-
match error.downcast_ref::<ProtocolRegistrationErrorWrapper>() {
268-
Some(ProtocolRegistrationErrorWrapper::CoreRegister(
269-
RegisterError::UnregisteredInitializer,
270-
)) => (),
266+
match error.downcast_ref::<RegisterError>() {
267+
Some(RegisterError::UnregisteredInitializer) => (),
271268
_ => panic!(
272269
"Expected an ProtocolRegistrationErrorWrapper::CoreRegister error, got: {error:?}"
273270
),

mithril-stm/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ pub use aggregate_signature::{
125125
AggregateSignature, AggregateSignatureType, AggregateVerificationKey, BasicVerifier, Clerk,
126126
};
127127
pub use error::{
128-
AggregationError, CoreVerifierError, RegisterError, StmAggregateSignatureError,
129-
StmSignatureError,
128+
AggregationError, CoreVerifierError, MultiSignatureError, RegisterError,
129+
StmAggregateSignatureError, StmSignatureError,
130130
};
131131
pub use key_registration::{ClosedKeyRegistration, KeyRegistration};
132132
pub use parameters::Parameters;

0 commit comments

Comments
 (0)