-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |