Skip to content

Commit

Permalink
Add hasher as a param to Signed
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Jul 5, 2023
1 parent 044cfb0 commit c99acbb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 14 additions & 2 deletions core/src/proto/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use sha2::{Digest, Sha256};
use thiserror::Error;

use super::generated::types;
use crate::ledger::storage::{KeccakHasher, Sha256Hasher, StorageHasher};
#[cfg(any(feature = "tendermint", feature = "tendermint-abcipp"))]
use crate::tendermint_proto::abci::ResponseDeliverTx;
use crate::types::address::Address;
Expand Down Expand Up @@ -83,6 +84,10 @@ pub trait Signable<T> {
/// A byte vector containing the serialized data.
type Output: key::SignableBytes;

/// The hashing algorithm to use to sign serialized
/// data with.
type Hasher: 'static + StorageHasher;

/// Encodes `data` as a byte vector, with some arbitrary serialization
/// method.
///
Expand All @@ -103,6 +108,7 @@ pub struct SerializeWithBorsh;
pub struct SignableEthMessage;

impl<T: BorshSerialize> Signable<T> for SerializeWithBorsh {
type Hasher = Sha256Hasher;
type Output = Vec<u8>;

fn as_signable(data: &T) -> Vec<u8> {
Expand All @@ -112,6 +118,7 @@ impl<T: BorshSerialize> Signable<T> for SerializeWithBorsh {
}

impl Signable<KeccakHash> for SignableEthMessage {
type Hasher = KeccakHasher;
type Output = KeccakHash;

fn as_signable(hash: &KeccakHash) -> KeccakHash {
Expand Down Expand Up @@ -195,7 +202,8 @@ impl<T, S: Signable<T>> Signed<T, S> {
/// Initialize a new [`Signed`] instance.
pub fn new(keypair: &common::SecretKey, data: T) -> Self {
let to_sign = S::as_signable(&data);
let sig = common::SigScheme::sign(keypair, to_sign);
let sig =
common::SigScheme::sign_with_hasher::<S::Hasher>(keypair, to_sign);
Self::new_from(data, sig)
}

Expand All @@ -206,7 +214,11 @@ impl<T, S: Signable<T>> Signed<T, S> {
pk: &common::PublicKey,
) -> std::result::Result<(), VerifySigError> {
let signed_bytes = S::as_signable(&self.data);
common::SigScheme::verify_signature(pk, &signed_bytes, &self.sig)
common::SigScheme::verify_signature_with_hasher::<S::Hasher>(
pk,
&signed_bytes,
&self.sig,
)
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/types/vote_extensions/validator_set_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ mod tag {
use super::{
epoch_to_token, Vext, VotingPowersMapExt, GOVERNANCE_CONTRACT_VERSION,
};
use crate::ledger::storage::KeccakHasher;
use crate::proto::Signable;
use crate::types::eth_abi::{AbiEncode, Encode, Token};
use crate::types::keccak::KeccakHash;
Expand All @@ -372,6 +373,7 @@ mod tag {
pub struct SerializeWithAbiEncode;

impl Signable<Vext> for SerializeWithAbiEncode {
type Hasher = KeccakHasher;
type Output = KeccakHash;

fn as_signable(ext: &Vext) -> Self::Output {
Expand Down

0 comments on commit c99acbb

Please sign in to comment.