Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write_as_raw_encoding method to UnifiedAddress #711

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this library adheres to Rust's notion of
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `zcash_client_backend::address::UnifiedAddress::write_as_raw_encoding`

## [0.6.0] - 2022-11-12
### Added
Expand Down
40 changes: 36 additions & 4 deletions zcash_client_backend/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl UnifiedAddress {
self.transparent.as_ref()
}

fn to_address(&self, net: Network) -> ZcashAddress {
let ua = unified::Address::try_from_items(
fn to_unified(&self) -> unified::Address {
unified::Address::try_from_items(
self.unknown
.iter()
.map(|(typecode, data)| unified::Receiver::Unknown {
Expand All @@ -161,8 +161,40 @@ impl UnifiedAddress {
)
.collect(),
)
.expect("UnifiedAddress should only be constructed safely");
ZcashAddress::from_unified(net, ua)
.expect("UnifiedAddress should only be constructed safely")
}

fn to_address(&self, net: Network) -> ZcashAddress {
ZcashAddress::from_unified(net, self.to_unified())
}

/// Returns the raw encoding of this `UnifiedAddress` as specified in ZIP 316
pub fn write_as_raw_encoding<W: std::io::Write>(&self, mut writer: W) -> std::io::Result<()> {
for item in self.to_unified().items() {
match item {
unified::Receiver::Orchard(data) => {
writer.write_all(&[3])?;
writer.write_all(&data)?;
}
unified::Receiver::Sapling(data) => {
writer.write_all(&[2])?;
writer.write_all(&data)?;
}
unified::Receiver::P2sh(data) => {
writer.write_all(&[1])?;
writer.write_all(&data)?;
}
unified::Receiver::P2pkh(data) => {
writer.write_all(&[0])?;
writer.write_all(&data)?;
}
unified::Receiver::Unknown { typecode, data } => {
zcash_encoding::CompactSize::write(&mut writer, typecode as usize)?;
writer.write_all(&data)?;
}
}
}
Ok(())
}

/// Returns the string encoding of this `UnifiedAddress` for the given network.
Expand Down