-
Notifications
You must be signed in to change notification settings - Fork 249
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
zcash_keys: Add decode_extfvk_with_network
#1523
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -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}; | ||
|
@@ -66,6 +66,16 @@ | |
} | ||
} | ||
|
||
#[cfg(feature = "sapling")] | ||
impl std::error::Error for Bech32DecodeError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
match &self { | ||
Bech32DecodeError::Bech32Error(e) => Some(e), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "sapling")] | ||
fn bech32_decode<T, F>(hrp: &str, s: &str, read: F) -> Result<T, Bech32DecodeError> | ||
where | ||
|
@@ -245,9 +255,8 @@ | |
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, | ||
|
@@ -256,6 +265,39 @@ | |
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 { | ||
Comment on lines
+276
to
+279
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
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 | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the other functions in this file don't explicitly say in their names that they're for Sapling, but that still seems odd. Non-blocking; I don't think we can do anything about it right now, but we should consider moving them to a
zcash_keys::sapling::encoding
module if they are never going to be more general.