9f848472e4 Refactor use map_err (wim-web)

Pull request description:

  issue: https://github.com/rust-bitcoin/rust-bitcoin/issues/793

  change to using map_err

ACKs for top commit:
  Kixunil:
    ACK 9f848472e4
  apoelstra:
    ACK 9f848472e4

Tree-SHA512: 93dac16463bf84825f764f3ef81833c27722a52f56737d30f14160d070959ad13bbfdf5f3c4871b961ce05fa9f75ed36acbacaa40ff6ba3bbf449b9c9173c0c7
This commit is contained in:
Andrew Poelstra 2022-01-18 20:54:23 +00:00
commit 64451a2144
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 27 additions and 20 deletions

View File

@ -109,16 +109,21 @@ impl Deserialize for EcdsaSig {
// also has a field sighash_u32 (See BIP141). For example, when signing with non-standard
// 0x05, the sighash message would have the last field as 0x05u32 while, the verification
// would use check the signature assuming sighash_u32 as `0x01`.
match EcdsaSig::from_slice(&bytes) {
Ok(sig) => Ok(sig),
Err(EcdsaSigError::EmptySignature) =>
Err(encode::Error::ParseFailed("Empty partial signature data")),
Err(EcdsaSigError::NonStandardSigHashType(flag)) =>
Err(encode::Error::from(psbt::Error::NonStandardSigHashType(flag))),
Err(EcdsaSigError::Secp256k1(..)) =>
Err(encode::Error::ParseFailed("Invalid Ecdsa signature")),
Err(EcdsaSigError::HexEncoding(..)) => unreachable!("Decoding from slice, not hex")
}
EcdsaSig::from_slice(&bytes)
.map_err(|e| match e {
EcdsaSigError::EmptySignature => {
encode::Error::ParseFailed("Empty partial signature data")
}
EcdsaSigError::NonStandardSigHashType(flag) => {
encode::Error::from(psbt::Error::NonStandardSigHashType(flag))
}
EcdsaSigError::Secp256k1(..) => {
encode::Error::ParseFailed("Invalid Ecdsa signature")
}
EcdsaSigError::HexEncoding(..) => {
unreachable!("Decoding from slice, not hex")
}
})
}
}
@ -205,16 +210,18 @@ impl Serialize for schnorr::SchnorrSig {
impl Deserialize for schnorr::SchnorrSig {
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
match schnorr::SchnorrSig::from_slice(&bytes) {
Ok(sig) => Ok(sig),
Err(schnorr::SchnorrSigError::InvalidSighashType(flag)) => {
Err(encode::Error::from(psbt::Error::NonStandardSigHashType(flag as u32)))
}
Err(schnorr::SchnorrSigError::InvalidSchnorrSigSize(_)) =>
Err(encode::Error::ParseFailed("Invalid Schnorr signature length")),
Err(schnorr::SchnorrSigError::Secp256k1(..)) =>
Err(encode::Error::ParseFailed("Invalid Schnorr signature")),
}
schnorr::SchnorrSig::from_slice(&bytes)
.map_err(|e| match e {
schnorr::SchnorrSigError::InvalidSighashType(flag) => {
encode::Error::from(psbt::Error::NonStandardSigHashType(flag as u32))
}
schnorr::SchnorrSigError::InvalidSchnorrSigSize(_) => {
encode::Error::ParseFailed("Invalid Schnorr signature length")
}
schnorr::SchnorrSigError::Secp256k1(..) => {
encode::Error::ParseFailed("Invalid Schnorr signature")
}
})
}
}