From e04ac1e7437e87c21fba0eeb165b1f75333a880e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 20 Jun 2023 05:08:07 +1000 Subject: [PATCH] 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`. --- bitcoin/src/crypto/key.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 011bbae4..5108940e 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -16,6 +16,7 @@ use internals::write_err; pub use secp256k1::rand; pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey}; +use crate::crypto::ecdsa; use crate::hash_types::{PubkeyHash, WPubkeyHash}; use crate::network::constants::Network; use crate::prelude::*; @@ -251,6 +252,16 @@ impl PublicKey { ) -> PublicKey { sk.public_key(secp) } + + /// Checks that `sig` is a valid ECDSA signature for `msg` using this public key. + pub fn verify( + &self, + secp: &Secp256k1, + msg: &secp256k1::Message, + sig: &ecdsa::Signature, + ) -> Result<(), Error> { + Ok(secp.verify_ecdsa(msg, &sig.sig, &self.inner)?) + } } impl From for XOnlyPublicKey {