forked from cerc-io/laconicd-deprecated
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>
This commit is contained in:
parent
4317929b1e
commit
ef4e068606
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
@ -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).
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user