use derec_cryptography::vss;
use derec_proto::{DeRecShare, de_rec_share};
use crate::Error;
pub(crate) trait DeRecShareExt {
fn validate(&self, secret_id: u64, version: u32) -> Result<(), Error>;
}
impl DeRecShareExt for DeRecShare {
fn validate(&self, secret_id: u64, version: u32) -> Result<(), Error> {
use crate::primitives::recovery::RecoveryError;
if self.secret_id != secret_id {
#[cfg(feature = "logging")]
tracing::warn!("secret_id mismatch between request and stored share");
return Err(RecoveryError::SecretIdMismatch.into());
}
if self.version != version {
#[cfg(feature = "logging")]
tracing::warn!(
expected = version,
got = self.version,
"version mismatch between request and stored share"
);
return Err(RecoveryError::VersionMismatch {
expected: version,
got: self.version,
}
.into());
}
Ok(())
}
}
pub(crate) fn into_vss_subshares(subshares: Vec<de_rec_share::Subshare>) -> Vec<vss::Subshare> {
subshares
.into_iter()
.map(|p| vss::Subshare { x: p.x, y: p.y })
.collect()
}
pub(crate) fn to_wire_subshares(subshares: &[vss::Subshare]) -> Vec<de_rec_share::Subshare> {
subshares
.iter()
.map(|p| de_rec_share::Subshare {
x: p.x.clone(),
y: p.y.clone(),
})
.collect()
}