Implement IncomingViewingKey::to_bytes

This commit is contained in:
Kris Nuttycombe 2021-07-20 13:37:07 -06:00
parent b4a82211ce
commit 77cf4c9831
2 changed files with 22 additions and 0 deletions

View File

@ -305,6 +305,16 @@ impl DiversifierKey {
.unwrap();
Diversifier(enc.to_bytes_le().try_into().unwrap())
}
/// Return the raw bytes of the diversifier key
pub fn to_bytes(&self) -> &[u8; 32] {
&self.0
}
/// Construct a diversifier key from bytes
pub fn from_bytes(bytes: [u8; 32]) -> Self {
DiversifierKey(bytes)
}
}
/// A diversifier that can be used to derive a specific [`Address`] from a
@ -398,6 +408,14 @@ impl From<&FullViewingKey> for IncomingViewingKey {
}
impl IncomingViewingKey {
/// Serializes an Orchard incoming viewing key to its raw encoding
pub fn to_bytes(&self) -> [u8; 64] {
let mut result = [0u8; 64];
result.copy_from_slice(self.dk.to_bytes());
result[32..].copy_from_slice(&self.ivk.0.to_bytes());
result
}
/// Parses an Orchard incoming viewing key from its raw encoding.
pub fn from_bytes(bytes: &[u8; 64]) -> CtOption<Self> {
NonZeroPallasBase::from_bytes(bytes[32..].try_into().unwrap()).map(|ivk| {

View File

@ -73,6 +73,10 @@ impl NonZeroPallasBase {
pallas::Base::from_bytes(bytes).and_then(NonZeroPallasBase::from_base)
}
pub(crate) fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}
pub(crate) fn from_base(b: pallas::Base) -> CtOption<Self> {
CtOption::new(NonZeroPallasBase(b), !b.ct_is_zero())
}