Skip to content

Commit b3d1b17

Browse files
committed
descriptor: eliminate several instances of Unexpected
When we are parsing keys using from_str, use the appropriate variant of ParseError. Also there was one place where we were taking an Error, converting it to a string, then wrapping in Error::Unexpected. I suspect this was a rebase mistake or something like that. Just get rid of the conversion. There are now 2 instances of Error::Unexpected in the codebase and one instance of errstr.
1 parent 1467453 commit b3d1b17

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/descriptor/mod.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::miniscript::{satisfy, Legacy, Miniscript, Segwitv0};
2727
use crate::plan::{AssetProvider, Plan};
2828
use crate::prelude::*;
2929
use crate::{
30-
expression, hash256, BareCtx, Error, ForEachKey, FromStrKey, MiniscriptKey, Satisfier,
31-
ToPublicKey, TranslateErr, Translator,
30+
expression, hash256, BareCtx, Error, ForEachKey, FromStrKey, MiniscriptKey, ParseError,
31+
Satisfier, ToPublicKey, TranslateErr, Translator,
3232
};
3333

3434
mod bare;
@@ -715,8 +715,9 @@ impl Descriptor<DescriptorPublicKey> {
715715
Some(sk),
716716
),
717717
Err(_) => (
718-
DescriptorPublicKey::from_str(s)
719-
.map_err(|e| Error::Unexpected(e.to_string()))?,
718+
// try to parse as a public key if parsing as a secret key failed
719+
s.parse()
720+
.map_err(|e| Error::Parse(ParseError::box_from_str(e)))?,
720721
None,
721722
),
722723
};
@@ -741,37 +742,34 @@ impl Descriptor<DescriptorPublicKey> {
741742
}
742743

743744
fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, Error> {
744-
let hash =
745-
sha256::Hash::from_str(sha256).map_err(|e| Error::Unexpected(e.to_string()))?;
746-
Ok(hash)
745+
sha256
746+
.parse()
747+
.map_err(|e| Error::Parse(ParseError::box_from_str(e)))
747748
}
748749

749750
fn hash256(&mut self, hash256: &String) -> Result<hash256::Hash, Error> {
750-
let hash = hash256::Hash::from_str(hash256)
751-
.map_err(|e| Error::Unexpected(e.to_string()))?;
752-
Ok(hash)
751+
hash256
752+
.parse()
753+
.map_err(|e| Error::Parse(ParseError::box_from_str(e)))
753754
}
754755

755756
fn ripemd160(&mut self, ripemd160: &String) -> Result<ripemd160::Hash, Error> {
756-
let hash = ripemd160::Hash::from_str(ripemd160)
757-
.map_err(|e| Error::Unexpected(e.to_string()))?;
758-
Ok(hash)
757+
ripemd160
758+
.parse()
759+
.map_err(|e| Error::Parse(ParseError::box_from_str(e)))
759760
}
760761

761762
fn hash160(&mut self, hash160: &String) -> Result<hash160::Hash, Error> {
762-
let hash = hash160::Hash::from_str(hash160)
763-
.map_err(|e| Error::Unexpected(e.to_string()))?;
764-
Ok(hash)
763+
hash160
764+
.parse()
765+
.map_err(|e| Error::Parse(ParseError::box_from_str(e)))
765766
}
766767
}
767768

768769
let descriptor = Descriptor::<String>::from_str(s)?;
769-
let descriptor = descriptor.translate_pk(&mut keymap_pk).map_err(|e| {
770-
Error::Unexpected(
771-
e.expect_translator_err("No Outer context errors")
772-
.to_string(),
773-
)
774-
})?;
770+
let descriptor = descriptor
771+
.translate_pk(&mut keymap_pk)
772+
.map_err(|e| e.expect_translator_err("No Outer context errors"))?;
775773

776774
Ok((descriptor, keymap_pk.0))
777775
}

0 commit comments

Comments
 (0)