Merge pull request #461 from nuttycom/sapling_fvk_address

Factors out sapling address generation from SaplingExtendedFullViewingKey
This commit is contained in:
Kris Nuttycombe 2021-12-02 14:10:23 -07:00 committed by GitHub
commit 5bb77e9149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 8 deletions

View File

@ -181,6 +181,42 @@ impl DiversifierKey {
} }
} }
/// Attempt to produce a payment address given the specified diversifier
/// index, and return None if the specified index does not produce a valid
/// diversifier.
pub fn sapling_address(
fvk: &FullViewingKey,
dk: &DiversifierKey,
j: DiversifierIndex,
) -> Option<PaymentAddress> {
dk.diversifier(j)
.and_then(|d_j| fvk.vk.to_payment_address(d_j))
}
/// Search the diversifier space starting at diversifier index `j` for
/// one which will produce a valid diversifier, and return the payment address
/// constructed using that diversifier along with the index at which the
/// valid diversifier was found.
pub fn sapling_find_address(
fvk: &FullViewingKey,
dk: &DiversifierKey,
j: DiversifierIndex,
) -> Option<(DiversifierIndex, PaymentAddress)> {
let (j, d_j) = dk.find_diversifier(j)?;
fvk.vk.to_payment_address(d_j).map(|addr| (j, addr))
}
/// Returns the payment address corresponding to the smallest valid diversifier
/// index, along with that index.
pub fn sapling_default_address(
fvk: &FullViewingKey,
dk: &DiversifierKey,
) -> (DiversifierIndex, PaymentAddress) {
// This unwrap is safe, if you have to search the 2^88 space of
// diversifiers it'll never return anyway.
sapling_find_address(fvk, dk, DiversifierIndex::new()).unwrap()
}
/// A Sapling extended spending key /// A Sapling extended spending key
#[derive(Clone)] #[derive(Clone)]
pub struct ExtendedSpendingKey { pub struct ExtendedSpendingKey {
@ -445,9 +481,7 @@ impl ExtendedFullViewingKey {
/// index, and return None if the specified index does not produce a valid /// index, and return None if the specified index does not produce a valid
/// diversifier. /// diversifier.
pub fn address(&self, j: DiversifierIndex) -> Option<PaymentAddress> { pub fn address(&self, j: DiversifierIndex) -> Option<PaymentAddress> {
self.dk sapling_address(&self.fvk, &self.dk, j)
.diversifier(j)
.and_then(|d_j| self.fvk.vk.to_payment_address(d_j))
} }
/// Search the diversifier space starting at diversifier index `j` for /// Search the diversifier space starting at diversifier index `j` for
@ -455,16 +489,13 @@ impl ExtendedFullViewingKey {
/// constructed using that diversifier along with the index at which the /// constructed using that diversifier along with the index at which the
/// valid diversifier was found. /// valid diversifier was found.
pub fn find_address(&self, j: DiversifierIndex) -> Option<(DiversifierIndex, PaymentAddress)> { pub fn find_address(&self, j: DiversifierIndex) -> Option<(DiversifierIndex, PaymentAddress)> {
let (j, d_j) = self.dk.find_diversifier(j)?; sapling_find_address(&self.fvk, &self.dk, j)
self.fvk.vk.to_payment_address(d_j).map(|addr| (j, addr))
} }
/// Returns the payment address corresponding to the smallest valid diversifier /// Returns the payment address corresponding to the smallest valid diversifier
/// index, along with that index. /// index, along with that index.
pub fn default_address(&self) -> (DiversifierIndex, PaymentAddress) { pub fn default_address(&self) -> (DiversifierIndex, PaymentAddress) {
// This unwrap is safe, if you have to search the 2^88 space of sapling_default_address(&self.fvk, &self.dk)
// diversifiers it'll never return anyway.
self.find_address(DiversifierIndex::new()).unwrap()
} }
} }