Add a verify function to PublicKey

Expose signature verification functionality for ECDSA signatures on the
`PublicKey` type.

We should have an identical function on `XOnlyPublicKey` but this will
have to be done in `secp2561`.
This commit is contained in:
Tobin C. Harding 2023-06-20 05:08:07 +10:00
parent 12e014e288
commit e04ac1e743
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 11 additions and 0 deletions

View File

@ -16,6 +16,7 @@ use internals::write_err;
pub use secp256k1::rand; pub use secp256k1::rand;
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey}; pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey};
use crate::crypto::ecdsa;
use crate::hash_types::{PubkeyHash, WPubkeyHash}; use crate::hash_types::{PubkeyHash, WPubkeyHash};
use crate::network::constants::Network; use crate::network::constants::Network;
use crate::prelude::*; use crate::prelude::*;
@ -251,6 +252,16 @@ impl PublicKey {
) -> PublicKey { ) -> PublicKey {
sk.public_key(secp) sk.public_key(secp)
} }
/// Checks that `sig` is a valid ECDSA signature for `msg` using this public key.
pub fn verify<C: secp256k1::Verification>(
&self,
secp: &Secp256k1<C>,
msg: &secp256k1::Message,
sig: &ecdsa::Signature,
) -> Result<(), Error> {
Ok(secp.verify_ecdsa(msg, &sig.sig, &self.inner)?)
}
} }
impl From<PublicKey> for XOnlyPublicKey { impl From<PublicKey> for XOnlyPublicKey {