diff --git a/src/address.rs b/src/address.rs index 560e3942..a6712051 100644 --- a/src/address.rs +++ b/src/address.rs @@ -32,7 +32,7 @@ impl Address { Address { d, pk_d } } - pub(crate) fn diversifer(&self) -> Diversifier { + pub(crate) fn diversifier(&self) -> Diversifier { self.d } diff --git a/src/keys.rs b/src/keys.rs index 636d0332..eace73b0 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -406,12 +406,6 @@ impl FullViewingKey { #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct DiversifierKey([u8; 32]); -impl From<&FullViewingKey> for DiversifierKey { - fn from(fvk: &FullViewingKey) -> Self { - fvk.derive_dk_ovk().0 - } -} - /// The index for a particular diversifier. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct DiversifierIndex([u8; 11]); @@ -437,6 +431,13 @@ impl From<[u8; 11]> for DiversifierIndex { } } +impl DiversifierIndex { + /// Returns the raw bytes of the diversifier index. + pub fn to_bytes(&self) -> &[u8; 11] { + &self.0 + } +} + impl DiversifierKey { /// Returns the diversifier at index 0. pub fn default_diversifier(&self) -> Diversifier { @@ -556,7 +557,7 @@ pub struct IncomingViewingKey { impl From<&FullViewingKey> for IncomingViewingKey { fn from(fvk: &FullViewingKey) -> Self { IncomingViewingKey { - dk: fvk.into(), + dk: fvk.derive_dk_ovk().0, ivk: fvk.into(), } } @@ -583,6 +584,18 @@ impl IncomingViewingKey { }) } + /// Checks whether the given address was derived from this incoming viewing + /// key, and returns the diversifier index used to derive the address if + /// so. Returns `None` if the address was not derived from this key. + pub fn diversifier_index(&self, addr: &Address) -> Option { + let j = self.dk.diversifier_index(&addr.diversifier()); + if &self.address_at(j) == addr { + Some(j) + } else { + None + } + } + /// Returns the default payment address for this key. pub fn default_address(&self) -> Address { self.address(self.dk.default_diversifier()) diff --git a/src/note_encryption.rs b/src/note_encryption.rs index c0aabfd3..c307625e 100644 --- a/src/note_encryption.rs +++ b/src/note_encryption.rs @@ -149,7 +149,7 @@ impl Domain for OrchardDomain { ) -> NotePlaintextBytes { let mut np = [0; NOTE_PLAINTEXT_SIZE]; np[0] = 0x02; - np[1..12].copy_from_slice(note.recipient().diversifer().as_array()); + np[1..12].copy_from_slice(note.recipient().diversifier().as_array()); np[12..20].copy_from_slice(¬e.value().to_bytes()); np[20..52].copy_from_slice(note.rseed().to_bytes()); np[52..].copy_from_slice(memo);