Skip to content

Commit 1298283

Browse files
committed
Add a SignerMessagePart fallible conversion to Signer
1 parent 598a722 commit 1298283

File tree

1 file changed

+40
-29
lines changed
  • mithril-common/src/messages/message_parts

1 file changed

+40
-29
lines changed

mithril-common/src/messages/message_parts/signer.rs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
HexEncodedOpCert, HexEncodedVerificationKey, HexEncodedVerificationKeySignature, PartyId,
77
Signer, SignerWithStake, Stake,
88
},
9-
StdResult,
9+
StdError, StdResult,
1010
};
1111
use anyhow::Context;
1212
use serde::{Deserialize, Serialize};
@@ -180,34 +180,10 @@ pub struct SignerMessagePart {
180180
impl SignerMessagePart {
181181
/// Convert a set of signer message parts into a set of signers
182182
pub fn try_into_signers(messages: Vec<Self>) -> StdResult<Vec<Signer>> {
183-
let mut signers: Vec<Signer> = Vec::new();
184-
185-
for message in messages {
186-
let verification_key_signature: Option<ProtocolSignerVerificationKeySignature> = message.verification_key_signature
187-
.map(|f| f.try_into())
188-
.transpose()
189-
.with_context(|| format!("Error while parsing verification key signature message, party_id = '{}'", message.party_id))?;
190-
let operational_certificate: Option<ProtocolOpCert> = message
191-
.operational_certificate
192-
.map(|f| f.try_into())
193-
.transpose()
194-
.with_context(|| {
195-
format!(
196-
"Error while parsing operational certificate message, party_id = '{}'.",
197-
message.party_id
198-
)
199-
})?;
200-
let value = Signer {
201-
party_id: message.party_id,
202-
verification_key: message.verification_key.try_into()?,
203-
verification_key_signature,
204-
kes_period: message.kes_period,
205-
operational_certificate,
206-
};
207-
signers.push(value);
208-
}
209-
210-
Ok(signers)
183+
messages
184+
.into_iter()
185+
.map(|m| SignerMessagePart::try_into(m))
186+
.collect()
211187
}
212188

213189
/// Convert a set of signers into message parts
@@ -231,6 +207,41 @@ impl SignerMessagePart {
231207
}
232208
}
233209

210+
impl TryInto<Signer> for SignerMessagePart {
211+
type Error = StdError;
212+
213+
fn try_into(self) -> Result<Signer, Self::Error> {
214+
let verification_key_signature: Option<ProtocolSignerVerificationKeySignature> = self
215+
.verification_key_signature
216+
.map(|f| f.try_into())
217+
.transpose()
218+
.with_context(|| {
219+
format!(
220+
"Error while parsing verification key signature message, party_id = '{}'",
221+
self.party_id
222+
)
223+
})?;
224+
let operational_certificate: Option<ProtocolOpCert> = self
225+
.operational_certificate
226+
.map(|f| f.try_into())
227+
.transpose()
228+
.with_context(|| {
229+
format!(
230+
"Error while parsing operational certificate message, party_id = '{}'.",
231+
self.party_id
232+
)
233+
})?;
234+
let value = Signer {
235+
party_id: self.party_id,
236+
verification_key: self.verification_key.try_into()?,
237+
verification_key_signature,
238+
kes_period: self.kes_period,
239+
operational_certificate,
240+
};
241+
Ok(value)
242+
}
243+
}
244+
234245
impl From<Signer> for SignerMessagePart {
235246
fn from(value: Signer) -> Self {
236247
Self {

0 commit comments

Comments
 (0)