Remove alloc when hashing for Bitcoin message signing format

This commit is contained in:
Elichai Turkel 2020-04-08 15:01:47 +03:00
parent 41e4471721
commit 25cb3d3539
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
1 changed files with 11 additions and 10 deletions

View File

@ -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)]