Implement util::misc::signed_msg_hash()

This commit is contained in:
Nadav Ivgi 2019-04-28 08:59:20 +03:00
parent 214345745b
commit 473b491409
No known key found for this signature in database
GPG Key ID: 81F6104CD0F150FC
1 changed files with 23 additions and 0 deletions

View File

@ -16,9 +16,12 @@
//! //!
//! Various utility functions //! Various utility functions
use bitcoin_hashes::{sha256d, Hash};
use blockdata::opcodes; use blockdata::opcodes;
use consensus::encode; use consensus::encode;
static MSG_SIGN_PREFIX: &'static [u8] = b"\x18Bitcoin Signed Message:\n";
/// Helper function to convert hex nibble characters to their respective value /// Helper function to convert hex nibble characters to their respective value
#[inline] #[inline]
fn hex_val(c: u8) -> Result<u8, encode::Error> { fn hex_val(c: u8) -> Result<u8, encode::Error> {
@ -107,6 +110,18 @@ pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
n_deleted n_deleted
} }
/// Hash message for signature using Bitcoin's message signing format
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
sha256d::Hash::hash(
&[
MSG_SIGN_PREFIX,
&encode::serialize(&encode::VarInt(msg.len() as u64)),
msg.as_bytes(),
]
.concat(),
)
}
#[cfg(all(test, feature="unstable"))] #[cfg(all(test, feature="unstable"))]
mod benches { mod benches {
use rand::{Rng, thread_rng}; use rand::{Rng, thread_rng};
@ -158,8 +173,10 @@ mod benches {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bitcoin_hashes::hex::ToHex;
use super::script_find_and_remove; use super::script_find_and_remove;
use super::hex_bytes; use super::hex_bytes;
use super::signed_msg_hash;
#[test] #[test]
fn test_script_find_and_remove() { fn test_script_find_and_remove() {
@ -207,5 +224,11 @@ mod tests {
assert!(hex_bytes("aBcDeF").is_ok()); assert!(hex_bytes("aBcDeF").is_ok());
assert!(hex_bytes("aBcD4eFL").is_err()); assert!(hex_bytes("aBcD4eFL").is_err());
} }
#[test]
fn test_signed_msg_hash() {
let hash = signed_msg_hash("test");
assert_eq!(hash.to_hex(), "a6f87fe6d58a032c320ff8d1541656f0282c2c7bfcc69d61af4c8e8ed528e49c");
}
} }