From 743fe5e7529d994cd430a194a1617a531318b96f Mon Sep 17 00:00:00 2001 From: Freddy Caceres Date: Wed, 23 Feb 2022 17:59:44 -0500 Subject: [PATCH 1/6] add signTypedData api --- rpc/ethereum/namespaces/eth/api.go | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/rpc/ethereum/namespaces/eth/api.go b/rpc/ethereum/namespaces/eth/api.go index a9f70e8690..756e04095a 100644 --- a/rpc/ethereum/namespaces/eth/api.go +++ b/rpc/ethereum/namespaces/eth/api.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/ethereum/go-ethereum/signer/core/apitypes" "math" "math/big" @@ -433,6 +434,40 @@ func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.By return signature, nil } +// SignTypedData signs EIP-712 conformant typed data +func (e *PublicAPI) SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error) { + e.logger.Debug("eth_signTypedData", "address", address.Hex(), "data", typedData) + from := sdk.AccAddress(address.Bytes()) + + _, err := e.clientCtx.Keyring.KeyByAddress(from) + if err != nil { + e.logger.Error("failed to find key in keyring", "address", address.String()) + return nil, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error()) + } + + domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map()) + if err != nil { + return nil, err + } + + typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message) + if err != nil { + return nil, err + } + + rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash))) + sighash := crypto.Keccak256(rawData) + // Sign the requested hash with the wallet + signature, _, err := e.clientCtx.Keyring.SignByAddress(from, sighash) + if err != nil { + e.logger.Error("keyring.SignByAddress failed", "address", address.Hex()) + return nil, err + } + + signature[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper + return signature, nil +} + // SendTransaction sends an Ethereum transaction. func (e *PublicAPI) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error) { e.logger.Debug("eth_sendTransaction", "args", args.String()) From b344e87e0b13005c3abde575568524d8ca923774 Mon Sep 17 00:00:00 2001 From: Freddy Caceres Date: Wed, 23 Feb 2022 18:00:59 -0500 Subject: [PATCH 2/6] fix typo --- rpc/ethereum/namespaces/eth/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/ethereum/namespaces/eth/api.go b/rpc/ethereum/namespaces/eth/api.go index 756e04095a..637efb1fa3 100644 --- a/rpc/ethereum/namespaces/eth/api.go +++ b/rpc/ethereum/namespaces/eth/api.go @@ -456,9 +456,9 @@ func (e *PublicAPI) SignTypedData(address common.Address, typedData apitypes.Typ } rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash))) - sighash := crypto.Keccak256(rawData) + sigHash := crypto.Keccak256(rawData) // Sign the requested hash with the wallet - signature, _, err := e.clientCtx.Keyring.SignByAddress(from, sighash) + signature, _, err := e.clientCtx.Keyring.SignByAddress(from, sigHash) if err != nil { e.logger.Error("keyring.SignByAddress failed", "address", address.Hex()) return nil, err From fc2444bb2828d12f06c45e6473390d382e6b05fc Mon Sep 17 00:00:00 2001 From: Freddy Caceres Date: Thu, 24 Feb 2022 08:19:33 -0500 Subject: [PATCH 3/6] fix lint issues --- rpc/ethereum/namespaces/eth/api.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rpc/ethereum/namespaces/eth/api.go b/rpc/ethereum/namespaces/eth/api.go index 637efb1fa3..c9b3cfe92c 100644 --- a/rpc/ethereum/namespaces/eth/api.go +++ b/rpc/ethereum/namespaces/eth/api.go @@ -4,10 +4,11 @@ import ( "context" "encoding/json" "fmt" - "github.com/ethereum/go-ethereum/signer/core/apitypes" "math" "math/big" + "github.com/ethereum/go-ethereum/signer/core/apitypes" + "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" From 2de9de0cd890536403210c76606597d82c69aaf7 Mon Sep 17 00:00:00 2001 From: Freddy Caceres Date: Thu, 24 Feb 2022 09:29:24 -0500 Subject: [PATCH 4/6] add crypto recovery offset --- rpc/ethereum/namespaces/eth/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/ethereum/namespaces/eth/api.go b/rpc/ethereum/namespaces/eth/api.go index c9b3cfe92c..66b7a9c9ca 100644 --- a/rpc/ethereum/namespaces/eth/api.go +++ b/rpc/ethereum/namespaces/eth/api.go @@ -431,7 +431,7 @@ func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.By return nil, err } - signature[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper + signature[crypto.RecoveryIDOffset] -= 27 // Transform V from 0/1 to 27/28 according to the yellow paper return signature, nil } @@ -465,7 +465,7 @@ func (e *PublicAPI) SignTypedData(address common.Address, typedData apitypes.Typ return nil, err } - signature[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper + signature[crypto.RecoveryIDOffset] -= 27 // Transform V from 0/1 to 27/28 according to the yellow paper return signature, nil } From e9a11e6412fc1dbc1d97f241e5c30e31b98ee0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Fri, 25 Feb 2022 09:32:09 -0300 Subject: [PATCH 5/6] Apply suggestions from code review --- rpc/ethereum/namespaces/eth/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/ethereum/namespaces/eth/api.go b/rpc/ethereum/namespaces/eth/api.go index 66b7a9c9ca..0e5a3d03ba 100644 --- a/rpc/ethereum/namespaces/eth/api.go +++ b/rpc/ethereum/namespaces/eth/api.go @@ -431,7 +431,7 @@ func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.By return nil, err } - signature[crypto.RecoveryIDOffset] -= 27 // Transform V from 0/1 to 27/28 according to the yellow paper + signature[crypto.RecoveryIDOffset] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper return signature, nil } @@ -465,7 +465,7 @@ func (e *PublicAPI) SignTypedData(address common.Address, typedData apitypes.Typ return nil, err } - signature[crypto.RecoveryIDOffset] -= 27 // Transform V from 0/1 to 27/28 according to the yellow paper + signature[crypto.RecoveryIDOffset] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper return signature, nil } From 821273ffd73368b68dc35fadf01b4b4911cc66db Mon Sep 17 00:00:00 2001 From: Freddy Caceres Date: Fri, 25 Feb 2022 07:53:58 -0500 Subject: [PATCH 6/6] add changelog --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abeec1c2e5..9aea2047fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +