util/address: avoid .expect/panic

This commit is contained in:
Marko Bencun 2021-11-14 23:32:33 +01:00
parent ad83f6ae00
commit f826316c25
No known key found for this signature in database
GPG Key ID: 804538928C37EAE8
1 changed files with 3 additions and 14 deletions

View File

@ -409,10 +409,7 @@ impl Payload {
/// Creates a pay to (compressed) public key hash payload from a public key /// Creates a pay to (compressed) public key hash payload from a public key
#[inline] #[inline]
pub fn p2pkh(pk: &ecdsa::PublicKey) -> Payload { pub fn p2pkh(pk: &ecdsa::PublicKey) -> Payload {
let mut hash_engine = PubkeyHash::engine(); Payload::PubkeyHash(PubkeyHash::hash(&pk.to_bytes()))
pk.write_into(&mut hash_engine)
.expect("engines don't error");
Payload::PubkeyHash(PubkeyHash::from_engine(hash_engine))
} }
/// Creates a pay to script hash P2SH payload from a script /// Creates a pay to script hash P2SH payload from a script
@ -430,13 +427,9 @@ impl Payload {
return Err(Error::UncompressedPubkey); return Err(Error::UncompressedPubkey);
} }
let mut hash_engine = WPubkeyHash::engine();
pk.write_into(&mut hash_engine)
.expect("engines don't error");
Ok(Payload::WitnessProgram { Ok(Payload::WitnessProgram {
version: WitnessVersion::V0, version: WitnessVersion::V0,
program: WPubkeyHash::from_engine(hash_engine)[..].to_vec(), program: WPubkeyHash::hash(&pk.to_bytes())[..].to_vec(),
}) })
} }
@ -446,13 +439,9 @@ impl Payload {
return Err(Error::UncompressedPubkey); return Err(Error::UncompressedPubkey);
} }
let mut hash_engine = WPubkeyHash::engine();
pk.write_into(&mut hash_engine)
.expect("engines don't error");
let builder = script::Builder::new() let builder = script::Builder::new()
.push_int(0) .push_int(0)
.push_slice(&WPubkeyHash::from_engine(hash_engine)[..]); .push_slice(&WPubkeyHash::hash(&pk.to_bytes())[..]);
Ok(Payload::ScriptHash(ScriptHash::hash( Ok(Payload::ScriptHash(ScriptHash::hash(
builder.into_script().as_bytes(), builder.into_script().as_bytes(),