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: ACKc41a6e9b1b
successfully ran local tests tcharding: ACKc41a6e9b1b
Tree-SHA512: 84caea275059381040c71100badb54556dd105722f79ac43d2df7eb0e5428cf8e7acc2d7f262625dd008837099a928c3c8be858f9ab11838c2eec1786e9f1844
This commit is contained in:
commit
48b2975870
|
@ -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::*;
|
||||||
|
|
Loading…
Reference in New Issue