Skip to content

Commit

Permalink
Merge pull request #69 from yihuang/fix-vote-signing-byte
Browse files Browse the repository at this point in the history
Signing bytes need to encoded with encode_length_delimited
  • Loading branch information
liamsi authored Nov 19, 2019
2 parents 23042bb + 6ca48b5 commit ef7410c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tendermint/src/amino_types/message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use prost_amino::encoding::encoded_len_varint;
use std::convert::TryInto;

/// Extend the original prost::Message trait with a few helper functions in order to
/// reduce boiler-plate code (and without modifying the prost-amino dependency).
pub trait AminoMessage: prost_amino::Message {
Expand All @@ -15,6 +18,21 @@ pub trait AminoMessage: prost_amino::Message {
self.encode(&mut res).unwrap();
res
}

/// Encode prost-amino message as length delimited.
///
/// Warning: Only use this method, if you are in control what will be encoded.
/// If there is an encoding error, this method will panic.
fn bytes_vec_length_delimited(&self) -> Vec<u8>
where
Self: Sized,
{
let len = self.encoded_len();
let mut res =
Vec::with_capacity(len + encoded_len_varint(len.try_into().expect("length overflow")));
self.encode_length_delimited(&mut res).unwrap();
res
}
}
impl<M: prost_amino::Message> AminoMessage for M {
// blanket impl
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl lite::Vote for SignedVote {
}

fn sign_bytes(&self) -> Vec<u8> {
AminoMessage::bytes_vec(&self.vote.to_owned())
self.vote.bytes_vec_length_delimited()
}
fn signature(&self) -> &[u8] {
match &self.signature {
Expand Down

0 comments on commit ef7410c

Please sign in to comment.