Skip to content

Commit

Permalink
zcash_client_backend: Factor out a common type from `WalletSaplingOut…
Browse files Browse the repository at this point in the history
…put` and `WalletOrchardOutput`.
  • Loading branch information
nuttycom committed Feb 27, 2024
1 parent c3b7659 commit c30c362
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 78 deletions.
3 changes: 2 additions & 1 deletion zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ and this library adheres to Rust's notion of
- `Note`
- `ReceivedNote`
- `Recipient::{map_internal_account, internal_account_transpose_option}`
- `WalletSaplingOutput::recipient_key_scope`
- `WalletOutput`
- `WalletSaplingOutput::key_source`
- `WalletOrchardOutput` behind the `orchard` feature flag.
- `WalletSpend`
- `WalletTx::{orchard_spends, orchard_outputs}` behind the `orchard` feature flag.
Expand Down
94 changes: 17 additions & 77 deletions zcash_client_backend/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,29 +272,27 @@ impl KeySource {
}
}

/// A subset of an [`OutputDescription`] relevant to wallets and light clients.
///
/// [`OutputDescription`]: sapling::bundle::OutputDescription
pub struct WalletSaplingOutput {
/// An output that was successfully decrypted in the process of wallet scanning.
pub struct WalletOutput<Note, Nullifier> {
index: usize,
ephemeral_key: EphemeralKeyBytes,
note: sapling::Note,
note: Note,
is_change: bool,
note_commitment_tree_position: Position,
nf: Option<sapling::Nullifier>,
nf: Option<Nullifier>,
key_source: KeySource,
}

impl WalletSaplingOutput {
impl<Note, Nullifier> WalletOutput<Note, Nullifier> {
/// Constructs a new `WalletSaplingOutput` value from its constituent parts.
#[allow(clippy::too_many_arguments)]
pub fn from_parts(
index: usize,
ephemeral_key: EphemeralKeyBytes,
note: sapling::Note,
note: Note,
is_change: bool,
note_commitment_tree_position: Position,
nf: Option<sapling::Nullifier>,
nf: Option<Nullifier>,
key_source: KeySource,
) -> Self {
Self {
Expand All @@ -308,29 +306,29 @@ impl WalletSaplingOutput {
}
}

/// The index of the output in the transaction that created this output.
/// The index of the output or action in the transaction that created this output.
pub fn index(&self) -> usize {
self.index
}
/// The [`EphemeralKeyBytes`] of the transaction output.
/// The [`EphemeralKeyBytes`] value of the transaction output.
pub fn ephemeral_key(&self) -> &EphemeralKeyBytes {
&self.ephemeral_key
}
/// The note.
pub fn note(&self) -> &sapling::Note {
pub fn note(&self) -> &Note {
&self.note
}
/// A flag indicating whether the process of note decryption determined that this
/// output should be classified as change.
pub fn is_change(&self) -> bool {
self.is_change
}
/// The position of the note in the global Sapling note commitment tree.
/// The position of the note in the global note commitment tree.
pub fn note_commitment_tree_position(&self) -> Position {
self.note_commitment_tree_position
}
/// The nullifier for the note, if the key used to decrypt the note was able to compute it.
pub fn nf(&self) -> Option<&sapling::Nullifier> {
pub fn nf(&self) -> Option<&Nullifier> {
self.nf.as_ref()
}
/// Source metadata for the key that decrypted this note.
Expand All @@ -339,74 +337,16 @@ impl WalletSaplingOutput {
}
}

#[cfg(feature = "orchard")]
pub struct WalletOrchardOutput {
index: usize,
ephemeral_key: EphemeralKeyBytes,
note: orchard::note::Note,
is_change: bool,
note_commitment_tree_position: Position,
nf: Option<orchard::note::Nullifier>,
key_source: KeySource,
}
/// A subset of an [`OutputDescription`] relevant to wallets and light clients.
///
/// [`OutputDescription`]: sapling::bundle::OutputDescription
pub type WalletSaplingOutput = WalletOutput<sapling::Note, sapling::Nullifier>;

/// The output part of an Orchard [`Action`] that was decrypted in the process of scanning.
///
/// [`Action`]: orchard::Action
#[cfg(feature = "orchard")]
impl WalletOrchardOutput {
/// Constructs a new `WalletOrchardOutput` value from its constituent parts.
#[allow(clippy::too_many_arguments)]
pub fn from_parts(
index: usize,
ephemeral_key: EphemeralKeyBytes,
note: orchard::note::Note,
is_change: bool,
note_commitment_tree_position: Position,
nf: Option<orchard::note::Nullifier>,
key_source: KeySource,
) -> Self {
Self {
index,
ephemeral_key,
note,
is_change,
note_commitment_tree_position,
nf,
key_source,
}
}

/// The index of the Orchard action in the transaction that created this output.
pub fn index(&self) -> usize {
self.index
}
/// The [`EphemeralKeyBytes`] of the transaction output.
pub fn ephemeral_key(&self) -> &EphemeralKeyBytes {
&self.ephemeral_key
}
/// The note.
pub fn note(&self) -> &orchard::note::Note {
&self.note
}
/// A flag indicating whether the process of note decryption determined that this
/// output should be classified as change.
pub fn is_change(&self) -> bool {
self.is_change
}
/// The position of the note in the global Orchard note commitment tree.
pub fn note_commitment_tree_position(&self) -> Position {
self.note_commitment_tree_position
}
/// The nullifier for the note, if the key used to decrypt the note was able to compute it.
pub fn nf(&self) -> Option<&orchard::note::Nullifier> {
self.nf.as_ref()
}
/// Source metadata for the key that decrypted this note.
pub fn key_source(&self) -> KeySource {
self.key_source
}
}
pub type WalletOrchardOutput = WalletOutput<orchard::note::Note, orchard::note::Nullifier>;

/// An enumeration of supported shielded note types for use in [`ReceivedNote`]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down

0 comments on commit c30c362

Please sign in to comment.