Merge pull request #422 from rust-bitcoin/2020-04-remove-alloc
Remove some needless allocations
This commit is contained in:
commit
1c88be4df5
|
@ -33,7 +33,7 @@ use util::endian;
|
|||
use blockdata::constants::WITNESS_SCALE_FACTOR;
|
||||
#[cfg(feature="bitcoinconsensus")] use blockdata::script;
|
||||
use blockdata::script::Script;
|
||||
use consensus::{encode, serialize, Decodable, Encodable};
|
||||
use consensus::{encode, Decodable, Encodable};
|
||||
use hash_types::*;
|
||||
use VarInt;
|
||||
|
||||
|
@ -378,9 +378,11 @@ impl Transaction {
|
|||
_ => unreachable!()
|
||||
};
|
||||
// hash the result
|
||||
let mut raw_vec = serialize(&tx);
|
||||
raw_vec.extend_from_slice(&endian::u32_to_array_le(sighash_u32));
|
||||
SigHash::hash(&raw_vec)
|
||||
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)
|
||||
}
|
||||
|
||||
/// Gets the "weight" of this transaction, as defined by BIP141. For transactions with an empty
|
||||
|
@ -441,7 +443,7 @@ impl Transaction {
|
|||
/// The lambda spent should not return the same TxOut twice!
|
||||
pub fn verify<S>(&self, mut spent: S) -> Result<(), script::Error>
|
||||
where S: FnMut(&OutPoint) -> Option<TxOut> {
|
||||
let tx = serialize(&*self);
|
||||
let tx = encode::serialize(&*self);
|
||||
for (idx, input) in self.input.iter().enumerate() {
|
||||
if let Some(output) = spent(&input.previous_output) {
|
||||
output.script_pubkey.verify(idx, output.value, tx.as_slice())?;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
//!
|
||||
//! Various utility functions
|
||||
|
||||
use hashes::{sha256d, Hash};
|
||||
use hashes::{sha256d, Hash, HashEngine};
|
||||
use blockdata::opcodes;
|
||||
use consensus::encode;
|
||||
use consensus::{encode, Encodable};
|
||||
|
||||
static MSG_SIGN_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
|
||||
|
||||
|
@ -59,14 +59,14 @@ 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 {
|
||||
sha256d::Hash::hash(
|
||||
&[
|
||||
MSG_SIGN_PREFIX,
|
||||
&encode::serialize(&encode::VarInt(msg.len() as u64)),
|
||||
msg.as_bytes(),
|
||||
]
|
||||
.concat(),
|
||||
)
|
||||
|
||||
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());
|
||||
|
||||
sha256d::Hash::from_engine(engine)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in New Issue