Skip to content

Commit 4da9213

Browse files
committed
Allow converting CoreKeyShare into KeyInfo without validation
1 parent c0eed2d commit 4da9213

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

key-share/src/lib.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use generic_ec_zkp::polynomial::lagrange_coefficient;
2222
mod utils;
2323
mod valid;
2424

25-
pub use self::valid::{Valid, Validate, ValidateError, ValidateFromParts};
25+
pub use self::valid::{Valid, ValidProjection, Validate, ValidateError, ValidateFromParts};
2626

2727
/// Core key share
2828
///
@@ -339,6 +339,34 @@ impl<E: Curve> CoreKeyShare<E> {
339339
}
340340
}
341341

342+
impl<E: Curve> From<&DirtyCoreKeyShare<E>> for DirtyKeyInfo<E> {
343+
fn from(key_share: &DirtyCoreKeyShare<E>) -> Self {
344+
DirtyKeyInfo {
345+
curve: key_share.curve,
346+
shared_public_key: key_share.shared_public_key,
347+
public_shares: key_share.public_shares.clone(),
348+
vss_setup: key_share.vss_setup.clone(),
349+
#[cfg(feature = "hd-wallets")]
350+
chain_code: key_share.chain_code,
351+
}
352+
}
353+
}
354+
impl<E: Curve> From<DirtyCoreKeyShare<E>> for DirtyKeyInfo<E> {
355+
fn from(key_share: DirtyCoreKeyShare<E>) -> Self {
356+
DirtyKeyInfo {
357+
curve: key_share.curve,
358+
shared_public_key: key_share.shared_public_key,
359+
public_shares: key_share.public_shares,
360+
vss_setup: key_share.vss_setup,
361+
#[cfg(feature = "hd-wallets")]
362+
chain_code: key_share.chain_code,
363+
}
364+
}
365+
}
366+
367+
impl<E: Curve> ValidProjection<DirtyCoreKeyShare<E>> for DirtyKeyInfo<E> {}
368+
impl<E: Curve> ValidProjection<&DirtyCoreKeyShare<E>> for DirtyKeyInfo<E> {}
369+
342370
/// Error indicating that key share is not valid
343371
#[derive(Debug, thiserror::Error)]
344372
#[error(transparent)]

key-share/src/valid.rs

+24
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,27 @@ where
4646
Ok(Self(T::from_parts(parts)))
4747
}
4848
}
49+
50+
/// Constructs `Self` from its [`ValidProjection`]
51+
pub fn from<K>(value: Valid<K>) -> Self
52+
where
53+
T: ValidProjection<K>,
54+
K: Validate,
55+
{
56+
Self(value.0.into())
57+
}
4958
}
5059

5160
impl<T> Valid<T> {
5261
/// Returns wraped validated value
5362
pub fn into_inner(self) -> T {
5463
self.0
5564
}
65+
66+
/// Returns a wrapped reference to validated data
67+
pub fn as_ref(&self) -> Valid<&T> {
68+
Valid(&self.0)
69+
}
5670
}
5771

5872
impl<T> AsRef<T> for Valid<T> {
@@ -90,6 +104,13 @@ pub trait Validate {
90104
}
91105
}
92106

107+
impl<T: Validate> Validate for &T {
108+
type Error = <T as Validate>::Error;
109+
fn is_valid(&self) -> Result<(), Self::Error> {
110+
(*self).is_valid()
111+
}
112+
}
113+
93114
/// Represents a type that can be constructed and validated from `Parts`
94115
///
95116
/// That can be particularly useful when vaidating `Parts` is cheaper than validating `Self`.
@@ -153,6 +174,9 @@ pub trait ValidateFromParts<Parts>: Validate {
153174
fn from_parts(parts: Parts) -> Self;
154175
}
155176

177+
/// Marker trait stating that `Self` is projection of `T`: if `T` is valid, then `Self::from(T)` is valid as well
178+
pub trait ValidProjection<T: Validate>: Validate + From<T> {}
179+
156180
/// Validation error
157181
///
158182
/// Contains an error that explains why value was considered invalid, and the value itself. It can be used

0 commit comments

Comments
 (0)