diff --git a/src/keys.rs b/src/keys.rs index 5f71bedc..571b0dc6 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -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 { NonZeroPallasBase::from_bytes(bytes[32..].try_into().unwrap()).map(|ivk| { diff --git a/src/spec.rs b/src/spec.rs index 1fae12b5..31cf1c95 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -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 { CtOption::new(NonZeroPallasBase(b), !b.ct_is_zero()) }