Skip to content

Commit

Permalink
Add the PCZT Updater role
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Dec 5, 2024
1 parent d132b5b commit bcd08e1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/pczt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub use parse::ParseError;
mod io_finalizer;
pub use io_finalizer::IoFinalizerError;

mod updater;
pub use updater::{ActionUpdater, Updater, UpdaterError};

mod prover;
pub use prover::ProverError;

Expand Down
69 changes: 69 additions & 0 deletions src/pczt/updater.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use super::{Action, Bundle, Zip32Derivation};

impl Bundle {
/// Updates the bundle with information provided in the given closure.
pub fn update_with<F>(&mut self, f: F) -> Result<(), UpdaterError>
where
F: FnOnce(Updater<'_>) -> Result<(), UpdaterError>,
{
f(Updater(self))
}
}

/// An updater for an Orchard PCZT bundle.
#[derive(Debug)]
pub struct Updater<'a>(&'a mut Bundle);

impl<'a> Updater<'a> {
/// Provides read access to the bundle being updated.
pub fn bundle(&self) -> &Bundle {
&self.0
}

/// Updates the action at the given index with information provided in the given
/// closure.
pub fn update_action_with<F>(&mut self, index: usize, f: F) -> Result<(), UpdaterError>
where
F: FnOnce(ActionUpdater<'_>) -> Result<(), UpdaterError>,
{
f(ActionUpdater(
self.0
.actions
.get_mut(index)
.ok_or(UpdaterError::InvalidIndex)?,
))
}
}

/// An updater for an Orchard PCZT action.
#[derive(Debug)]
pub struct ActionUpdater<'a>(&'a mut Action);

impl<'a> ActionUpdater<'a> {
/// Sets the ZIP 32 derivation path for the spent note's signing key.
pub fn set_spend_zip32_derivation(&mut self, derivation: Zip32Derivation) {
self.0.spend.zip32_derivation = Some(derivation);
}

/// Stores the given spend-specific proprietary value at the given key.
pub fn set_spend_proprietary(&mut self, key: String, value: Vec<u8>) {
self.0.spend.proprietary.insert(key, value);
}

/// Sets the ZIP 32 derivation path for the new note's signing key.
pub fn set_output_zip32_derivation(&mut self, derivation: Zip32Derivation) {
self.0.output.zip32_derivation = Some(derivation);
}

/// Stores the given output-specific proprietary value at the given key.
pub fn set_output_proprietary(&mut self, key: String, value: Vec<u8>) {
self.0.output.proprietary.insert(key, value);
}
}

/// Errors that can occur while updating an Orchard bundle in a PCZT.
#[derive(Debug)]
pub enum UpdaterError {
/// An out-of-bounds index was provided when looking up an action.
InvalidIndex,
}

0 comments on commit bcd08e1

Please sign in to comment.