Skip to content

Commit

Permalink
quick fix for the signing payloads in 0.34
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtau committed Nov 9, 2020
1 parent e0a7390 commit 2fa1678
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 25 deletions.
76 changes: 55 additions & 21 deletions src/amino_types/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ use crate::rpc;
use bytes::BufMut;
use ed25519_dalek as ed25519;
use once_cell::sync::Lazy;
use prost::Message as ProstMessage;
use prost_amino::{EncodeError, Message};
use prost_amino_derive::Message;
use std::convert::TryFrom;
use tendermint::{
block::{self, ParseId},
chain, consensus, error,
};
use tendermint_proto::types::{
CanonicalBlockId as ProtoCanonicalBlockId,
CanonicalPartSetHeader as ProtoCanonicalPartSetHeader,
CanonicalProposal as ProtoCanonicalProposal,
};

#[derive(Clone, PartialEq, Message)]
pub struct Proposal {
Expand Down Expand Up @@ -95,7 +101,12 @@ pub struct SignedProposalResponse {
}

impl SignableMsg for SignProposalRequest {
fn sign_bytes<B>(&self, chain_id: chain::Id, sign_bytes: &mut B) -> Result<bool, EncodeError>
fn sign_bytes<B>(
&self,
chain_id: chain::Id,
sign_bytes: &mut B,
is_protobuf: bool,
) -> Result<bool, EncodeError>
where
B: BufMut,
{
Expand All @@ -104,29 +115,52 @@ impl SignableMsg for SignProposalRequest {
pr.signature = vec![];
}
let proposal = spr.proposal.unwrap();
let cp = CanonicalProposal {
chain_id: chain_id.to_string(),
msg_type: SignedMsgType::Proposal.to_u32(),
height: proposal.height,
block_id: match proposal.block_id {
Some(bid) => Some(CanonicalBlockId {
hash: bid.hash,
parts_header: match bid.parts_header {
Some(psh) => Some(CanonicalPartSetHeader {
hash: psh.hash,
total: psh.total,
if !is_protobuf {
let cp = CanonicalProposal {
chain_id: chain_id.to_string(),
msg_type: SignedMsgType::Proposal.to_u32(),
height: proposal.height,
block_id: match proposal.block_id {
Some(bid) => Some(CanonicalBlockId {
hash: bid.hash,
parts_header: match bid.parts_header {
Some(psh) => Some(CanonicalPartSetHeader {
hash: psh.hash,
total: psh.total,
}),
None => None,
},
}),
None => None,
},
pol_round: proposal.pol_round,
round: proposal.round,
timestamp: proposal.timestamp,
};

cp.encode_length_delimited(sign_bytes)?;
} else {
let cp = ProtoCanonicalProposal {
chain_id: chain_id.to_string(),
r#type: SignedMsgType::Proposal.to_u32() as i32,
height: proposal.height,
block_id: proposal.block_id.as_ref().map(|x| ProtoCanonicalBlockId {
hash: x.hash.clone(),
part_set_header: x
.parts_header
.as_ref()
.map(|y| ProtoCanonicalPartSetHeader {
total: y.total as u32,
hash: y.hash.clone(),
}),
None => None,
},
}),
None => None,
},
pol_round: proposal.pol_round,
round: proposal.round,
timestamp: proposal.timestamp,
};
pol_round: proposal.pol_round,
round: proposal.round,
timestamp: proposal.timestamp.map(Into::into),
};

cp.encode_length_delimited(sign_bytes)?;
cp.encode_length_delimited(sign_bytes);
}
Ok(true)
}
fn set_signature(&mut self, sig: &ed25519::Signature) {
Expand Down
1 change: 1 addition & 0 deletions src/amino_types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub trait SignableMsg {
&self,
chain_id: chain::Id,
sign_bytes: &mut B,
is_protobuf: bool,
) -> Result<bool, EncodeError>;

/// Set the Ed25519 signature on the underlying message
Expand Down
37 changes: 34 additions & 3 deletions src/amino_types/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::rpc;
use bytes::BufMut;
use ed25519_dalek as ed25519;
use once_cell::sync::Lazy;
use prost::Message as ProstMessage;
use prost_amino::{error::EncodeError, Message};
use prost_amino_derive::Message;
use std::convert::TryFrom;
Expand All @@ -20,6 +21,10 @@ use tendermint::{
error::Error,
vote,
};
use tendermint_proto::types::{
CanonicalBlockId as ProtoCanonicalBlockId,
CanonicalPartSetHeader as ProtoCanonicalPartSetHeader, CanonicalVote as ProtoCanonicalVote,
};

const VALIDATOR_ADDR_SIZE: usize = 20;

Expand Down Expand Up @@ -176,7 +181,12 @@ impl CanonicalVote {
}

impl SignableMsg for SignVoteRequest {
fn sign_bytes<B>(&self, chain_id: chain::Id, sign_bytes: &mut B) -> Result<bool, EncodeError>
fn sign_bytes<B>(
&self,
chain_id: chain::Id,
sign_bytes: &mut B,
is_protobuf: bool,
) -> Result<bool, EncodeError>
where
B: BufMut,
{
Expand All @@ -185,9 +195,30 @@ impl SignableMsg for SignVoteRequest {
vo.signature = vec![];
}
let vote = svr.vote.unwrap();
let cv = CanonicalVote::new(vote, chain_id.as_str());
if !is_protobuf {
let cv = CanonicalVote::new(vote, chain_id.as_str());

cv.encode_length_delimited(sign_bytes)?;
cv.encode_length_delimited(sign_bytes)?;
} else {
let cv = ProtoCanonicalVote {
r#type: vote.vote_type as i32,
height: vote.height,
round: vote.round as i64,
block_id: vote.block_id.as_ref().map(|x| ProtoCanonicalBlockId {
hash: x.hash.clone(),
part_set_header: x
.parts_header
.as_ref()
.map(|y| ProtoCanonicalPartSetHeader {
total: y.total as u32,
hash: y.hash.clone(),
}),
}),
timestamp: vote.timestamp.map(Into::into),
chain_id: chain_id.to_string(),
};
cv.encode_length_delimited(sign_bytes);
}

Ok(true)
}
Expand Down
6 changes: 5 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ impl Session {
}

let mut to_sign = vec![];
request.sign_bytes(self.config.chain_id, &mut to_sign)?;
request.sign_bytes(
self.config.chain_id,
&mut to_sign,
self.config.protocol_version.is_protobuf(),
)?;

let started_at = Instant::now();

Expand Down

0 comments on commit 2fa1678

Please sign in to comment.