use crate::error::AttributesError;
use crate::Error;
use crate::{AttrId, AttrView, Multikey, ThresholdAttrView, ThresholdKeyView};
pub(crate) struct View<'a> {
mk: &'a Multikey,
}
impl<'a> TryFrom<&'a Multikey> for View<'a> {
type Error = Error;
fn try_from(mk: &'a Multikey) -> Result<Self, Self::Error> {
Ok(Self { mk })
}
}
impl<'a> AttrView for View<'a> {
fn is_encrypted(&self) -> bool {
false
}
fn is_public_key(&self) -> bool {
false
}
fn is_secret_key(&self) -> bool {
false
}
fn is_secret_key_share(&self) -> bool {
true
}
}
impl<'a> ThresholdAttrView for View<'a> {
fn threshold(&self) -> Result<usize, Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgThreshold)
.ok_or(AttributesError::MissingThreshold)?;
if bytes.len() < 2 {
return Err(AttributesError::MissingThreshold.into());
}
let v = u16::from_le_bytes([bytes[0], bytes[1]]);
Ok(v as usize)
}
fn limit(&self) -> Result<usize, Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgLimit)
.ok_or(AttributesError::MissingLimit)?;
if bytes.len() < 2 {
return Err(AttributesError::MissingLimit.into());
}
let v = u16::from_le_bytes([bytes[0], bytes[1]]);
Ok(v as usize)
}
fn identifier(&self) -> Result<&[u8], Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgIdentifier)
.ok_or(AttributesError::MissingShareIdentifier)?;
Ok(bytes.as_slice())
}
fn threshold_data(&self) -> Result<&[u8], Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgGroupPublicKey)
.ok_or(AttributesError::MissingThresholdData)?;
Ok(bytes.as_slice())
}
}
impl<'a> ThresholdKeyView for View<'a> {
fn group_pubkey(&self) -> Result<Vec<u8>, Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgGroupPublicKey)
.ok_or(AttributesError::MissingThresholdData)?;
Ok(bytes.to_vec())
}
fn is_threshold_key(&self) -> bool {
true
}
fn participant_count(&self) -> Result<u16, Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgLimit)
.ok_or(AttributesError::MissingLimit)?;
if bytes.len() < 2 {
return Err(AttributesError::MissingLimit.into());
}
Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
}
fn threshold(&self) -> Result<u16, Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgThreshold)
.ok_or(AttributesError::MissingThreshold)?;
if bytes.len() < 2 {
return Err(AttributesError::MissingThreshold.into());
}
Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
}
fn owner_vlad(&self) -> Result<Vec<u8>, Error> {
let bytes = self
.mk
.attributes
.get(&AttrId::DkgOwnerId)
.ok_or(AttributesError::MissingShareIdentifier)?;
Ok(bytes.to_vec())
}
}