feat: add sign fn for sign_message

This commit is contained in:
ChrisCho-H 2024-10-12 12:37:04 +09:00
parent fe62d94ff7
commit c41a6e9b1b
1 changed files with 14 additions and 0 deletions

View File

@ -6,6 +6,7 @@
//! library is used with the `secp-recovery` feature. //! library is used with the `secp-recovery` feature.
use hashes::{sha256d, HashEngine}; use hashes::{sha256d, HashEngine};
use secp256k1::SecretKey;
use crate::consensus::encode::WriteExt; 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) 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;