get_checksum_bytes now checks input data for checksum

If `exclude_hash` is set, we split the input data, and if a checksum
already existed within the original data, we check the calculated
checksum against the original checksum.

Additionally, the implementation of `IntoWalletDescriptor` for `&str`
has been refactored for clarity.
This commit is contained in:
志宇
2022-09-29 13:06:03 +08:00
parent af0b3698c6
commit fd34956c29
3 changed files with 32 additions and 24 deletions

View File

@@ -40,6 +40,7 @@ pub mod policy;
pub mod template;
pub use self::checksum::get_checksum;
use self::checksum::get_checksum_bytes;
pub use self::derived::{AsDerived, DerivedDescriptorKey};
pub use self::error::Error as DescriptorError;
pub use self::policy::Policy;
@@ -84,19 +85,15 @@ impl IntoWalletDescriptor for &str {
secp: &SecpCtx,
network: Network,
) -> Result<(ExtendedDescriptor, KeyMap), DescriptorError> {
let descriptor = if self.contains('#') {
let parts: Vec<&str> = self.splitn(2, '#').collect();
if !get_checksum(parts[0])
.ok()
.map(|computed| computed == parts[1])
.unwrap_or(false)
{
return Err(DescriptorError::InvalidDescriptorChecksum);
let descriptor = match self.split_once('#') {
Some((desc, original_checksum)) => {
let checksum = get_checksum_bytes(desc, false)?;
if original_checksum.as_bytes() != &checksum {
return Err(DescriptorError::InvalidDescriptorChecksum);
}
desc
}
parts[0]
} else {
self
None => self,
};
ExtendedDescriptor::parse_descriptor(secp, descriptor)?