forked from cerc-io/laconicd-deprecated
Refactor EIP-712 signature verification (#1397)
* [WIP] EIP-712 Signature Refactor * Debug and add ante tests * Add tests for failure cases * Add changelog entry * Code cleanup * Add tests for MsgDelegate and MsgWithdrawDelegationReward * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Code cleanup * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Minor codefix * Update ethereum/eip712/encoding.go * Minor code revision updates * Refactor EIP712 unit tests to use test suite * Address import cycle and implement minor refactors * Fix lint issues * Add EIP712 unit suite test function * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update ethereum/eip712/encoding.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Add minor refactors; increase test coverage * Correct ante_test for change in payload * Add single-signer util and tests * Update ethereum/eip712/encoding.go * Update ethereum/eip712/encoding.go * fix build Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Freddy Caceres <facs95@gmail.com>
This commit is contained in:
co-authored by
Federico Kunze Küllmer
Freddy Caceres
parent
41425fc6d4
commit
cb632c6bef
@@ -10,7 +10,7 @@ import (
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
|
||||
"github.com/evmos/ethermint/ethereum/eip712"
|
||||
tmcrypto "github.com/tendermint/tendermint/crypto"
|
||||
)
|
||||
|
||||
@@ -203,10 +203,28 @@ func (pubKey *PubKey) UnmarshalAminoJSON(bz []byte) error {
|
||||
|
||||
// VerifySignature verifies that the ECDSA public key created a given signature over
|
||||
// the provided message. It will calculate the Keccak256 hash of the message
|
||||
// prior to verification.
|
||||
// prior to verification and approve verification if the signature can be verified
|
||||
// from either the original message or its EIP-712 representation.
|
||||
//
|
||||
// CONTRACT: The signature should be in [R || S] format.
|
||||
func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
|
||||
return pubKey.verifySignatureECDSA(msg, sig) || pubKey.verifySignatureAsEIP712(msg, sig)
|
||||
}
|
||||
|
||||
// Verifies the signature as an EIP-712 signature by first converting the message payload
|
||||
// to an EIP-712 hashed object, performing ECDSA verification on the hash. This is to support
|
||||
// signing a Cosmos payload using EIP-712.
|
||||
func (pubKey PubKey) verifySignatureAsEIP712(msg, sig []byte) bool {
|
||||
eip712Hash, err := eip712.GetEIP712HashForMsg(msg)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return pubKey.verifySignatureECDSA(eip712Hash, sig)
|
||||
}
|
||||
|
||||
// Perform standard ECDSA signature verification for the given raw bytes and signature.
|
||||
func (pubKey PubKey) verifySignatureECDSA(msg, sig []byte) bool {
|
||||
if len(sig) == crypto.SignatureLength {
|
||||
// remove recovery ID (V) if contained in the signature
|
||||
sig = sig[:len(sig)-1]
|
||||
|
||||
Reference in New Issue
Block a user