Merge rust-bitcoin/rust-bitcoin#3456: feat: add sign fn for sign_message

c41a6e9b1b feat: add sign fn for sign_message (ChrisCho-H)

Pull request description:

  While it's not hard to create the signature using `secp256k1` modules with `signed_msg_hash`, it's much more convenient and safe to provide one-way function to generate signed signature(even without the understanding about the semantics of bitcoin message signing).

ACKs for top commit:
  apoelstra:
    ACK c41a6e9b1b successfully ran local tests
  tcharding:
    ACK c41a6e9b1b

Tree-SHA512: 84caea275059381040c71100badb54556dd105722f79ac43d2df7eb0e5428cf8e7acc2d7f262625dd008837099a928c3c8be858f9ab11838c2eec1786e9f1844
This commit is contained in:
merge-script 2024-10-13 21:13:39 +00:00
commit 48b2975870
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 14 additions and 0 deletions

View File

@ -6,6 +6,7 @@
//! library is used with the `secp-recovery` feature.
use hashes::{sha256d, HashEngine};
use secp256k1::SecretKey;
use crate::consensus::encode::WriteExt;
@ -214,6 +215,19 @@ pub fn signed_msg_hash(msg: impl AsRef<[u8]>) -> sha256d::Hash {
sha256d::Hash::from_engine(engine)
}
/// Sign message using Bitcoin's message signing format.
#[cfg(feature = "secp-recovery")]
pub fn sign<C: secp256k1::Signing>(
secp_ctx: &secp256k1::Secp256k1<C>,
msg: impl AsRef<[u8]>,
privkey: SecretKey,
) -> MessageSignature {
let msg_hash = signed_msg_hash(msg);
let msg_to_sign = secp256k1::Message::from_digest(msg_hash.to_byte_array());
let secp_sig = secp_ctx.sign_ecdsa_recoverable(&msg_to_sign, &privkey);
MessageSignature { signature: secp_sig, compressed: true }
}
#[cfg(test)]
mod tests {
use super::*;