From c99acbb48159f059e2b5d83b5b1910e444120e54 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 5 Jul 2023 15:52:32 +0200 Subject: [PATCH] Add hasher as a param to `Signed` --- core/src/proto/types.rs | 16 ++++++++++++++-- .../vote_extensions/validator_set_update.rs | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/src/proto/types.rs b/core/src/proto/types.rs index ca9b5fddfa..d8c29742ec 100644 --- a/core/src/proto/types.rs +++ b/core/src/proto/types.rs @@ -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; @@ -83,6 +84,10 @@ pub trait Signable { /// 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. /// @@ -103,6 +108,7 @@ pub struct SerializeWithBorsh; pub struct SignableEthMessage; impl Signable for SerializeWithBorsh { + type Hasher = Sha256Hasher; type Output = Vec; fn as_signable(data: &T) -> Vec { @@ -112,6 +118,7 @@ impl Signable for SerializeWithBorsh { } impl Signable for SignableEthMessage { + type Hasher = KeccakHasher; type Output = KeccakHash; fn as_signable(hash: &KeccakHash) -> KeccakHash { @@ -195,7 +202,8 @@ impl> Signed { /// 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::(keypair, to_sign); Self::new_from(data, sig) } @@ -206,7 +214,11 @@ impl> Signed { 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::( + pk, + &signed_bytes, + &self.sig, + ) } } diff --git a/core/src/types/vote_extensions/validator_set_update.rs b/core/src/types/vote_extensions/validator_set_update.rs index 0e23393cd5..5a9324fec6 100644 --- a/core/src/types/vote_extensions/validator_set_update.rs +++ b/core/src/types/vote_extensions/validator_set_update.rs @@ -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; @@ -372,6 +373,7 @@ mod tag { pub struct SerializeWithAbiEncode; impl Signable for SerializeWithAbiEncode { + type Hasher = KeccakHasher; type Output = KeccakHash; fn as_signable(ext: &Vext) -> Self::Output {