Remove the cursor overhead, write is implemented on vec these days

This commit is contained in:
Elichai Turkel 2020-04-12 19:00:55 +03:00
parent 25cb3d3539
commit af31017eb1
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
3 changed files with 5 additions and 7 deletions

View File

@ -378,10 +378,9 @@ impl Transaction {
_ => unreachable!()
};
// hash the result
// TODO: Sanity assert that consensus_encode returned length matches the hashed length in the hasher.
let sighash_arr = endian::u32_to_array_le(sighash_u32);
let mut engine = SigHash::engine();
tx.consensus_encode(&mut engine).unwrap();
let sighash_arr = endian::u32_to_array_le(sighash_u32);
sighash_arr.consensus_encode(&mut engine).unwrap();
SigHash::from_engine(engine)
}

View File

@ -149,10 +149,10 @@ impl From<psbt::Error> for Error {
/// Encode an object into a vector
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
let mut encoder = Cursor::new(vec![]);
let mut encoder = Vec::new();
let len = data.consensus_encode(&mut encoder).unwrap();
assert_eq!(len, encoder.get_ref().len());
encoder.into_inner()
assert_eq!(len, encoder.len());
encoder
}
/// Encode an object into a hex-encoded string

View File

@ -59,11 +59,10 @@ pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
/// Hash message for signature using Bitcoin's message signing format
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
let msg_len = encode::VarInt(msg.len() as u64);
// TODO: Sanity assert that consensus_encode returned length matches the hashed length in the hasher.
let mut engine = sha256d::Hash::engine();
engine.input(MSG_SIGN_PREFIX);
let msg_len = encode::VarInt(msg.len() as u64);
msg_len.consensus_encode(&mut engine).unwrap();
engine.input(msg.as_bytes());