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

Commit ef4e068

Browse files
Rename getEIP712Hash to Bytes to clarity method intent (#1539)
* Rename getEIP712Hash to Bytes to clarity method intent * Update comments and revert unrelated code * Fix variable naming in tests * Fix to use raw data rather than hash from Go-Ethereum interface * Update eip712_test to detect EIP-712 hash vs bytes * Update TypedData conversion error message Co-authored-by: Freddy Caceres <facs95@gmail.com>
1 parent 4317929 commit ef4e068

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

app/ante/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ func (suite *AnteTestSuite) generateSingleSignature(signMode signing.SignMode, p
568568
msg = signDocBytes
569569

570570
if signType == "EIP-712" {
571-
msg, err = eip712.GetEIP712HashForMsg(signDocBytes)
571+
msg, err = eip712.GetEIP712BytesForMsg(signDocBytes)
572572
suite.Require().NoError(err)
573573
}
574574

crypto/ethsecp256k1/ethsecp256k1.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,15 @@ func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
213213
}
214214

215215
// Verifies the signature as an EIP-712 signature by first converting the message payload
216-
// to an EIP-712 hashed object, performing ECDSA verification on the hash. This is to support
216+
// to EIP-712 object bytes, then performing ECDSA verification on the hash. This is to support
217217
// signing a Cosmos payload using EIP-712.
218218
func (pubKey PubKey) verifySignatureAsEIP712(msg, sig []byte) bool {
219-
eip712Hash, err := eip712.GetEIP712HashForMsg(msg)
219+
eip712Bytes, err := eip712.GetEIP712BytesForMsg(msg)
220220
if err != nil {
221221
return false
222222
}
223223

224-
return pubKey.verifySignatureECDSA(eip712Hash, sig)
224+
return pubKey.verifySignatureECDSA(eip712Bytes, sig)
225225
}
226226

227227
// Perform standard ECDSA signature verification for the given raw bytes and signature.

ethereum/eip712/eip712_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,17 @@ func (suite *EIP712TestSuite) TestEIP712SignatureVerification() {
400400

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

411411
suite.Require().NoError(err)
412412

413-
sigHash := crypto.Keccak256Hash(eip712Hash)
414-
sig, err := privKey.Sign(sigHash.Bytes())
413+
sig, err := privKey.Sign(eip712Bytes)
415414
suite.Require().NoError(err)
416415

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

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

426425
// Verify against random bytes to ensure it does not pass unexpectedly (sanity check).

ethereum/eip712/encoding.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,21 @@ func SetEncodingConfig(cfg params.EncodingConfig) {
3636
protoCodec = codec.NewProtoCodec(cfg.InterfaceRegistry)
3737
}
3838

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

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

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

6256
// GetEIP712TypedDataForMsg returns the EIP-712 TypedData representation for either

0 commit comments

Comments
 (0)