Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Rename getEIP712Hash to Bytes to clarity method intent #1539

Merged
merged 7 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func (suite *AnteTestSuite) generateSingleSignature(signMode signing.SignMode, p
msg = signDocBytes

if signType == "EIP-712" {
msg, err = eip712.GetEIP712HashForMsg(signDocBytes)
msg, err = eip712.GetEIP712BytesForMsg(signDocBytes)
suite.Require().NoError(err)
}

Expand Down
6 changes: 3 additions & 3 deletions crypto/ethsecp256k1/ethsecp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
}

// 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
// to EIP-712 object bytes, then 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)
eip712Bytes, err := eip712.GetEIP712BytesForMsg(msg)
if err != nil {
return false
}

return pubKey.verifySignatureECDSA(eip712Hash, sig)
return pubKey.verifySignatureECDSA(eip712Bytes, sig)
}

// Perform standard ECDSA signature verification for the given raw bytes and signature.
Expand Down
11 changes: 5 additions & 6 deletions ethereum/eip712/eip712_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,17 @@ func (suite *EIP712TestSuite) TestEIP712SignatureVerification() {

// Verify that the payload passes signature verification if signed as its EIP-712 representation.
func (suite *EIP712TestSuite) verifyEIP712SignatureVerification(expectedSuccess bool, privKey ethsecp256k1.PrivKey, pubKey ethsecp256k1.PubKey, signBytes []byte) {
// Convert to EIP712 hash and sign
eip712Hash, err := eip712.GetEIP712HashForMsg(signBytes)
// Convert to EIP712 bytes and sign
eip712Bytes, err := eip712.GetEIP712BytesForMsg(signBytes)
if !expectedSuccess {
// Expect failure generating EIP-712 hash
// Expect failure generating EIP-712 bytes
suite.Require().Error(err)
return
}

suite.Require().NoError(err)

sigHash := crypto.Keccak256Hash(eip712Hash)
sig, err := privKey.Sign(sigHash.Bytes())
sig, err := privKey.Sign(eip712Bytes)
suite.Require().NoError(err)

// Verify against original payload bytes. This should pass, even though it is not
Expand All @@ -420,7 +419,7 @@ func (suite *EIP712TestSuite) verifyEIP712SignatureVerification(expectedSuccess
suite.Require().True(res)

// Verify against the signed EIP-712 bytes. This should pass, since it is the message signed.
res = pubKey.VerifySignature(eip712Hash, sig)
res = pubKey.VerifySignature(eip712Bytes, sig)
suite.Require().True(res)

// Verify against random bytes to ensure it does not pass unexpectedly (sanity check).
Expand Down
16 changes: 5 additions & 11 deletions ethereum/eip712/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,21 @@ func SetEncodingConfig(cfg params.EncodingConfig) {
protoCodec = codec.NewProtoCodec(cfg.InterfaceRegistry)
}

// Get the EIP-712 object hash for the given SignDoc bytes by first decoding the bytes into
// Get the EIP-712 object bytes for the given SignDoc bytes by first decoding the bytes into
// an EIP-712 object, then hashing the EIP-712 object to create the bytes to be signed.
// See https://eips.ethereum.org/EIPS/eip-712 for more.
func GetEIP712HashForMsg(signDocBytes []byte) ([]byte, error) {
func GetEIP712BytesForMsg(signDocBytes []byte) ([]byte, error) {
typedData, err := GetEIP712TypedDataForMsg(signDocBytes)
if err != nil {
return nil, err
}

domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
_, rawData, err := apitypes.TypedDataAndHash(typedData)
if err != nil {
return nil, fmt.Errorf("could not hash EIP-712 domain: %w", err)
return nil, fmt.Errorf("could not get EIP-712 object bytes: %w", err)
}

typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
if err != nil {
return nil, fmt.Errorf("could not hash EIP-712 primary type: %w", err)
}
rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))

return rawData, nil
return []byte(rawData), nil
}

// GetEIP712TypedDataForMsg returns the EIP-712 TypedData representation for either
Expand Down