Remove alloc when hashing for Bitcoin message signing format
This commit is contained in:
parent
41e4471721
commit
25cb3d3539
|
@ -16,9 +16,9 @@
|
||||||
//!
|
//!
|
||||||
//! Various utility functions
|
//! Various utility functions
|
||||||
|
|
||||||
use hashes::{sha256d, Hash};
|
use hashes::{sha256d, Hash, HashEngine};
|
||||||
use blockdata::opcodes;
|
use blockdata::opcodes;
|
||||||
use consensus::encode;
|
use consensus::{encode, Encodable};
|
||||||
|
|
||||||
static MSG_SIGN_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
|
static MSG_SIGN_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
|
||||||
|
|
||||||
|
@ -59,14 +59,15 @@ pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
|
||||||
|
|
||||||
/// Hash message for signature using Bitcoin's message signing format
|
/// Hash message for signature using Bitcoin's message signing format
|
||||||
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
|
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
|
||||||
sha256d::Hash::hash(
|
let msg_len = encode::VarInt(msg.len() as u64);
|
||||||
&[
|
|
||||||
MSG_SIGN_PREFIX,
|
// TODO: Sanity assert that consensus_encode returned length matches the hashed length in the hasher.
|
||||||
&encode::serialize(&encode::VarInt(msg.len() as u64)),
|
let mut engine = sha256d::Hash::engine();
|
||||||
msg.as_bytes(),
|
engine.input(MSG_SIGN_PREFIX);
|
||||||
]
|
msg_len.consensus_encode(&mut engine).unwrap();
|
||||||
.concat(),
|
engine.input(msg.as_bytes());
|
||||||
)
|
|
||||||
|
sha256d::Hash::from_engine(engine)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue