Unverified Commit 619a3e70 authored by Martin Holst Swende's avatar Martin Holst Swende Committed by GitHub

signer/core: move EIP-712 types to package apitypes (#24029)

Fixes #23972
parent 93f196c4
...@@ -898,7 +898,7 @@ func testExternalUI(api *core.SignerAPI) { ...@@ -898,7 +898,7 @@ func testExternalUI(api *core.SignerAPI) {
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899") addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
data := `{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"test","type":"uint8"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"}]},"primaryType":"Mail","domain":{"name":"Ether Mail","version":"1","chainId":"1","verifyingContract":"0xCCCcccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"},"message":{"from":{"name":"Cow","test":"3","wallet":"0xcD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"},"to":{"name":"Bob","wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB","test":"2"},"contents":"Hello, Bob!"}}` data := `{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"test","type":"uint8"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"}]},"primaryType":"Mail","domain":{"name":"Ether Mail","version":"1","chainId":"1","verifyingContract":"0xCCCcccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"},"message":{"from":{"name":"Cow","test":"3","wallet":"0xcD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"},"to":{"name":"Bob","wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB","test":"2"},"contents":"Hello, Bob!"}}`
//_, err := api.SignData(ctx, accounts.MimetypeTypedData, *addr, hexutil.Encode([]byte(data))) //_, err := api.SignData(ctx, accounts.MimetypeTypedData, *addr, hexutil.Encode([]byte(data)))
var typedData core.TypedData var typedData apitypes.TypedData
json.Unmarshal([]byte(data), &typedData) json.Unmarshal([]byte(data), &typedData)
_, err := api.SignTypedData(ctx, *addr, typedData) _, err := api.SignTypedData(ctx, *addr, typedData)
expectApprove("sign 712 typed data", err) expectApprove("sign 712 typed data", err)
...@@ -1025,7 +1025,7 @@ func GenDoc(ctx *cli.Context) { ...@@ -1025,7 +1025,7 @@ func GenDoc(ctx *cli.Context) {
"of the work in canonicalizing and making sense of the data, and it's up to the UI to present" + "of the work in canonicalizing and making sense of the data, and it's up to the UI to present" +
"the user with the contents of the `message`" "the user with the contents of the `message`"
sighash, msg := accounts.TextAndHash([]byte("hello world")) sighash, msg := accounts.TextAndHash([]byte("hello world"))
messages := []*core.NameValueType{{Name: "message", Value: msg, Typ: accounts.MimetypeTextPlain}} messages := []*apitypes.NameValueType{{Name: "message", Value: msg, Typ: accounts.MimetypeTextPlain}}
add("SignDataRequest", desc, &core.SignDataRequest{ add("SignDataRequest", desc, &core.SignDataRequest{
Address: common.NewMixedcaseAddress(a), Address: common.NewMixedcaseAddress(a),
......
...@@ -57,7 +57,7 @@ type ExternalAPI interface { ...@@ -57,7 +57,7 @@ type ExternalAPI interface {
// SignData - request to sign the given data (plus prefix) // SignData - request to sign the given data (plus prefix)
SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error)
// SignTypedData - request to sign the given structured data (plus prefix) // SignTypedData - request to sign the given structured data (plus prefix)
SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data apitypes.TypedData) (hexutil.Bytes, error)
// EcRecover - recover public key from given message and signature // EcRecover - recover public key from given message and signature
EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error)
// Version info about the APIs // Version info about the APIs
...@@ -235,7 +235,7 @@ type ( ...@@ -235,7 +235,7 @@ type (
ContentType string `json:"content_type"` ContentType string `json:"content_type"`
Address common.MixedcaseAddress `json:"address"` Address common.MixedcaseAddress `json:"address"`
Rawdata []byte `json:"raw_data"` Rawdata []byte `json:"raw_data"`
Messages []*NameValueType `json:"messages"` Messages []*apitypes.NameValueType `json:"messages"`
Callinfo []apitypes.ValidationInfo `json:"call_info"` Callinfo []apitypes.ValidationInfo `json:"call_info"`
Hash hexutil.Bytes `json:"hash"` Hash hexutil.Bytes `json:"hash"`
Meta Metadata `json:"meta"` Meta Metadata `json:"meta"`
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core package apitypes
import ( import (
"bytes" "bytes"
......
This diff is collapsed.
...@@ -89,7 +89,7 @@ func (l *AuditLogger) SignGnosisSafeTx(ctx context.Context, addr common.Mixedcas ...@@ -89,7 +89,7 @@ func (l *AuditLogger) SignGnosisSafeTx(ctx context.Context, addr common.Mixedcas
return res, e return res, e
} }
func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) { func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data apitypes.TypedData) (hexutil.Bytes, error) {
l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(), l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
"addr", addr.String(), "data", data) "addr", addr.String(), "data", data)
b, e := l.api.SignTypedData(ctx, addr, data) b, e := l.api.SignTypedData(ctx, addr, data)
......
...@@ -34,15 +34,15 @@ type GnosisSafeTx struct { ...@@ -34,15 +34,15 @@ type GnosisSafeTx struct {
} }
// ToTypedData converts the tx to a EIP-712 Typed Data structure for signing // ToTypedData converts the tx to a EIP-712 Typed Data structure for signing
func (tx *GnosisSafeTx) ToTypedData() TypedData { func (tx *GnosisSafeTx) ToTypedData() apitypes.TypedData {
var data hexutil.Bytes var data hexutil.Bytes
if tx.Data != nil { if tx.Data != nil {
data = *tx.Data data = *tx.Data
} }
gnosisTypedData := TypedData{ gnosisTypedData := apitypes.TypedData{
Types: Types{ Types: apitypes.Types{
"EIP712Domain": []Type{{Name: "verifyingContract", Type: "address"}}, "EIP712Domain": []apitypes.Type{{Name: "verifyingContract", Type: "address"}},
"SafeTx": []Type{ "SafeTx": []apitypes.Type{
{Name: "to", Type: "address"}, {Name: "to", Type: "address"},
{Name: "value", Type: "uint256"}, {Name: "value", Type: "uint256"},
{Name: "data", Type: "bytes"}, {Name: "data", Type: "bytes"},
...@@ -55,11 +55,11 @@ func (tx *GnosisSafeTx) ToTypedData() TypedData { ...@@ -55,11 +55,11 @@ func (tx *GnosisSafeTx) ToTypedData() TypedData {
{Name: "nonce", Type: "uint256"}, {Name: "nonce", Type: "uint256"},
}, },
}, },
Domain: TypedDataDomain{ Domain: apitypes.TypedDataDomain{
VerifyingContract: tx.Safe.Address().Hex(), VerifyingContract: tx.Safe.Address().Hex(),
}, },
PrimaryType: "SafeTx", PrimaryType: "SafeTx",
Message: TypedDataMessage{ Message: apitypes.TypedDataMessage{
"to": tx.To.Address().Hex(), "to": tx.To.Address().Hex(),
"value": tx.Value.String(), "value": tx.Value.String(),
"data": data, "data": data,
......
This diff is collapsed.
...@@ -32,9 +32,10 @@ import ( ...@@ -32,9 +32,10 @@ import (
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/signer/core" "github.com/ethereum/go-ethereum/signer/core"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
) )
var typesStandard = core.Types{ var typesStandard = apitypes.Types{
"EIP712Domain": { "EIP712Domain": {
{ {
Name: "name", Name: "name",
...@@ -153,12 +154,12 @@ var jsonTypedData = ` ...@@ -153,12 +154,12 @@ var jsonTypedData = `
const primaryType = "Mail" const primaryType = "Mail"
var domainStandard = core.TypedDataDomain{ var domainStandard = apitypes.TypedDataDomain{
"Ether Mail", Name: "Ether Mail",
"1", Version: "1",
math.NewHexOrDecimal256(1), ChainId: math.NewHexOrDecimal256(1),
"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
"", Salt: "",
} }
var messageStandard = map[string]interface{}{ var messageStandard = map[string]interface{}{
...@@ -173,7 +174,7 @@ var messageStandard = map[string]interface{}{ ...@@ -173,7 +174,7 @@ var messageStandard = map[string]interface{}{
"contents": "Hello, Bob!", "contents": "Hello, Bob!",
} }
var typedData = core.TypedData{ var typedData = apitypes.TypedData{
Types: typesStandard, Types: typesStandard,
PrimaryType: primaryType, PrimaryType: primaryType,
Domain: domainStandard, Domain: domainStandard,
...@@ -194,7 +195,7 @@ func TestSignData(t *testing.T) { ...@@ -194,7 +195,7 @@ func TestSignData(t *testing.T) {
control.approveCh <- "Y" control.approveCh <- "Y"
control.inputCh <- "wrongpassword" control.inputCh <- "wrongpassword"
signature, err := api.SignData(context.Background(), core.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world"))) signature, err := api.SignData(context.Background(), apitypes.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world")))
if signature != nil { if signature != nil {
t.Errorf("Expected nil-data, got %x", signature) t.Errorf("Expected nil-data, got %x", signature)
} }
...@@ -202,7 +203,7 @@ func TestSignData(t *testing.T) { ...@@ -202,7 +203,7 @@ func TestSignData(t *testing.T) {
t.Errorf("Expected ErrLocked! '%v'", err) t.Errorf("Expected ErrLocked! '%v'", err)
} }
control.approveCh <- "No way" control.approveCh <- "No way"
signature, err = api.SignData(context.Background(), core.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world"))) signature, err = api.SignData(context.Background(), apitypes.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world")))
if signature != nil { if signature != nil {
t.Errorf("Expected nil-data, got %x", signature) t.Errorf("Expected nil-data, got %x", signature)
} }
...@@ -212,7 +213,7 @@ func TestSignData(t *testing.T) { ...@@ -212,7 +213,7 @@ func TestSignData(t *testing.T) {
// text/plain // text/plain
control.approveCh <- "Y" control.approveCh <- "Y"
control.inputCh <- "a_long_password" control.inputCh <- "a_long_password"
signature, err = api.SignData(context.Background(), core.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world"))) signature, err = api.SignData(context.Background(), apitypes.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world")))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -232,13 +233,13 @@ func TestSignData(t *testing.T) { ...@@ -232,13 +233,13 @@ func TestSignData(t *testing.T) {
} }
func TestDomainChainId(t *testing.T) { func TestDomainChainId(t *testing.T) {
withoutChainID := core.TypedData{ withoutChainID := apitypes.TypedData{
Types: core.Types{ Types: apitypes.Types{
"EIP712Domain": []core.Type{ "EIP712Domain": []apitypes.Type{
{Name: "name", Type: "string"}, {Name: "name", Type: "string"},
}, },
}, },
Domain: core.TypedDataDomain{ Domain: apitypes.TypedDataDomain{
Name: "test", Name: "test",
}, },
} }
...@@ -250,14 +251,14 @@ func TestDomainChainId(t *testing.T) { ...@@ -250,14 +251,14 @@ func TestDomainChainId(t *testing.T) {
if _, err := withoutChainID.HashStruct("EIP712Domain", withoutChainID.Domain.Map()); err != nil { if _, err := withoutChainID.HashStruct("EIP712Domain", withoutChainID.Domain.Map()); err != nil {
t.Errorf("Expected the typedData to encode the domain successfully, got %v", err) t.Errorf("Expected the typedData to encode the domain successfully, got %v", err)
} }
withChainID := core.TypedData{ withChainID := apitypes.TypedData{
Types: core.Types{ Types: apitypes.Types{
"EIP712Domain": []core.Type{ "EIP712Domain": []apitypes.Type{
{Name: "name", Type: "string"}, {Name: "name", Type: "string"},
{Name: "chainId", Type: "uint256"}, {Name: "chainId", Type: "uint256"},
}, },
}, },
Domain: core.TypedDataDomain{ Domain: apitypes.TypedDataDomain{
Name: "test", Name: "test",
ChainId: math.NewHexOrDecimal256(1), ChainId: math.NewHexOrDecimal256(1),
}, },
...@@ -323,7 +324,7 @@ func TestEncodeData(t *testing.T) { ...@@ -323,7 +324,7 @@ func TestEncodeData(t *testing.T) {
} }
func TestFormatter(t *testing.T) { func TestFormatter(t *testing.T) {
var d core.TypedData var d apitypes.TypedData
err := json.Unmarshal([]byte(jsonTypedData), &d) err := json.Unmarshal([]byte(jsonTypedData), &d)
if err != nil { if err != nil {
t.Fatalf("unmarshalling failed '%v'", err) t.Fatalf("unmarshalling failed '%v'", err)
...@@ -337,7 +338,7 @@ func TestFormatter(t *testing.T) { ...@@ -337,7 +338,7 @@ func TestFormatter(t *testing.T) {
t.Logf("'%v'\n", string(j)) t.Logf("'%v'\n", string(j))
} }
func sign(typedData core.TypedData) ([]byte, []byte, error) { func sign(typedData apitypes.TypedData) ([]byte, []byte, error) {
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map()) domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
...@@ -366,7 +367,7 @@ func TestJsonFiles(t *testing.T) { ...@@ -366,7 +367,7 @@ func TestJsonFiles(t *testing.T) {
t.Errorf("Failed to read file %v: %v", fInfo.Name(), err) t.Errorf("Failed to read file %v: %v", fInfo.Name(), err)
continue continue
} }
var typedData core.TypedData var typedData apitypes.TypedData
err = json.Unmarshal(data, &typedData) err = json.Unmarshal(data, &typedData)
if err != nil { if err != nil {
t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err) t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err)
...@@ -398,7 +399,7 @@ func TestFuzzerFiles(t *testing.T) { ...@@ -398,7 +399,7 @@ func TestFuzzerFiles(t *testing.T) {
t.Errorf("Failed to read file %v: %v", fInfo.Name(), err) t.Errorf("Failed to read file %v: %v", fInfo.Name(), err)
continue continue
} }
var typedData core.TypedData var typedData apitypes.TypedData
err = json.Unmarshal(data, &typedData) err = json.Unmarshal(data, &typedData)
if err != nil { if err != nil {
t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err) t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err)
...@@ -498,7 +499,7 @@ var gnosisTx = ` ...@@ -498,7 +499,7 @@ var gnosisTx = `
// TestGnosisTypedData tests the scenario where a user submits a full EIP-712 // TestGnosisTypedData tests the scenario where a user submits a full EIP-712
// struct without using the gnosis-specific endpoint // struct without using the gnosis-specific endpoint
func TestGnosisTypedData(t *testing.T) { func TestGnosisTypedData(t *testing.T) {
var td core.TypedData var td apitypes.TypedData
err := json.Unmarshal([]byte(gnosisTypedData), &td) err := json.Unmarshal([]byte(gnosisTypedData), &td)
if err != nil { if err != nil {
t.Fatalf("unmarshalling failed '%v'", err) t.Fatalf("unmarshalling failed '%v'", err)
......
...@@ -605,7 +605,7 @@ function ApproveSignData(r){ ...@@ -605,7 +605,7 @@ function ApproveSignData(r){
t.Logf("address %v %v\n", addr.String(), addr.Original()) t.Logf("address %v %v\n", addr.String(), addr.Original())
nvt := []*core.NameValueType{ nvt := []*apitypes.NameValueType{
{ {
Name: "message", Name: "message",
Typ: "text/plain", Typ: "text/plain",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment