Skip to content

Commit

Permalink
zcash_keys: Add decode_extfvk_with_network
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Aug 30, 2024
1 parent 59582a7 commit 897018a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
3 changes: 3 additions & 0 deletions zcash_keys/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Added
- `zcash_keys::encoding::decode_extfvk_with_network`

## [0.3.0] - 2024-08-19
### Notable changes
- `zcash_keys`:
Expand Down
40 changes: 36 additions & 4 deletions zcash_keys/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::address::UnifiedAddress;
use bs58::{self, decode::Error as Bs58Error};
use std::fmt;
use zcash_primitives::consensus::NetworkConstants;
use zcash_primitives::consensus::{NetworkConstants, NetworkType};

use zcash_address::unified::{self, Encoding};
use zcash_primitives::{consensus, legacy::TransparentAddress};
Expand Down Expand Up @@ -245,9 +245,8 @@ pub fn encode_extended_full_viewing_key(hrp: &str, extfvk: &ExtendedFullViewingK
bech32_encode(hrp, |w| extfvk.write(w))
}

/// Decodes an [`ExtendedFullViewingKey`] from a Bech32-encoded string.
///
/// [`ExtendedFullViewingKey`]: sapling::zip32::ExtendedFullViewingKey
/// Decodes an [`ExtendedFullViewingKey`] from a Bech32-encoded string, verifying that it matches
/// the provided human-readable prefix.
#[cfg(feature = "sapling")]
pub fn decode_extended_full_viewing_key(
hrp: &str,
Expand All @@ -256,6 +255,39 @@ pub fn decode_extended_full_viewing_key(
bech32_decode(hrp, s, |data| ExtendedFullViewingKey::read(&data[..]).ok())
}

/// Decodes an [`ExtendedFullViewingKey`] and the [`NetworkType`] that it is intended for use with
/// from a Bech32-encoded string.
#[cfg(feature = "sapling")]
pub fn decode_extfvk_with_network(
s: &str,
) -> Result<(NetworkType, ExtendedFullViewingKey), Bech32DecodeError> {
use zcash_protocol::constants::{mainnet, regtest, testnet};

let (decoded_hrp, data, variant) = bech32::decode(s)?;
if variant != Variant::Bech32 {
Err(Bech32DecodeError::IncorrectVariant(variant))
} else {
let network = match &decoded_hrp[..] {
mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => Ok(NetworkType::Main),
testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => Ok(NetworkType::Test),
regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY => Ok(NetworkType::Regtest),
other => Err(Bech32DecodeError::HrpMismatch {
expected: format!(
"One of {}, {}, or {}",
mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
),
actual: other.to_string(),
}),
}?;
let fvk = ExtendedFullViewingKey::read(&Vec::<u8>::from_base32(&data)?[..])
.map_err(|_| Bech32DecodeError::ReadError)?;

Ok((network, fvk))
}
}

/// Writes a [`PaymentAddress`] as a Bech32-encoded string.
///
/// # Examples
Expand Down

0 comments on commit 897018a

Please sign in to comment.