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

Fixes #23972
This commit is contained in:
Martin Holst Swende
2021-12-05 14:31:41 +01:00
committed by GitHub
parent 93f196c4b0
commit 619a3e7085
9 changed files with 776 additions and 771 deletions

View File

@ -32,9 +32,10 @@ import (
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/signer/core"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
)
var typesStandard = core.Types{
var typesStandard = apitypes.Types{
"EIP712Domain": {
{
Name: "name",
@ -153,12 +154,12 @@ var jsonTypedData = `
const primaryType = "Mail"
var domainStandard = core.TypedDataDomain{
"Ether Mail",
"1",
math.NewHexOrDecimal256(1),
"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
"",
var domainStandard = apitypes.TypedDataDomain{
Name: "Ether Mail",
Version: "1",
ChainId: math.NewHexOrDecimal256(1),
VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
Salt: "",
}
var messageStandard = map[string]interface{}{
@ -173,7 +174,7 @@ var messageStandard = map[string]interface{}{
"contents": "Hello, Bob!",
}
var typedData = core.TypedData{
var typedData = apitypes.TypedData{
Types: typesStandard,
PrimaryType: primaryType,
Domain: domainStandard,
@ -194,7 +195,7 @@ func TestSignData(t *testing.T) {
control.approveCh <- "Y"
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 {
t.Errorf("Expected nil-data, got %x", signature)
}
@ -202,7 +203,7 @@ func TestSignData(t *testing.T) {
t.Errorf("Expected ErrLocked! '%v'", err)
}
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 {
t.Errorf("Expected nil-data, got %x", signature)
}
@ -212,7 +213,7 @@ func TestSignData(t *testing.T) {
// text/plain
control.approveCh <- "Y"
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 {
t.Fatal(err)
}
@ -232,13 +233,13 @@ func TestSignData(t *testing.T) {
}
func TestDomainChainId(t *testing.T) {
withoutChainID := core.TypedData{
Types: core.Types{
"EIP712Domain": []core.Type{
withoutChainID := apitypes.TypedData{
Types: apitypes.Types{
"EIP712Domain": []apitypes.Type{
{Name: "name", Type: "string"},
},
},
Domain: core.TypedDataDomain{
Domain: apitypes.TypedDataDomain{
Name: "test",
},
}
@ -250,14 +251,14 @@ func TestDomainChainId(t *testing.T) {
if _, err := withoutChainID.HashStruct("EIP712Domain", withoutChainID.Domain.Map()); err != nil {
t.Errorf("Expected the typedData to encode the domain successfully, got %v", err)
}
withChainID := core.TypedData{
Types: core.Types{
"EIP712Domain": []core.Type{
withChainID := apitypes.TypedData{
Types: apitypes.Types{
"EIP712Domain": []apitypes.Type{
{Name: "name", Type: "string"},
{Name: "chainId", Type: "uint256"},
},
},
Domain: core.TypedDataDomain{
Domain: apitypes.TypedDataDomain{
Name: "test",
ChainId: math.NewHexOrDecimal256(1),
},
@ -323,7 +324,7 @@ func TestEncodeData(t *testing.T) {
}
func TestFormatter(t *testing.T) {
var d core.TypedData
var d apitypes.TypedData
err := json.Unmarshal([]byte(jsonTypedData), &d)
if err != nil {
t.Fatalf("unmarshalling failed '%v'", err)
@ -337,7 +338,7 @@ func TestFormatter(t *testing.T) {
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())
if err != nil {
return nil, nil, err
@ -366,7 +367,7 @@ func TestJsonFiles(t *testing.T) {
t.Errorf("Failed to read file %v: %v", fInfo.Name(), err)
continue
}
var typedData core.TypedData
var typedData apitypes.TypedData
err = json.Unmarshal(data, &typedData)
if err != nil {
t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err)
@ -398,7 +399,7 @@ func TestFuzzerFiles(t *testing.T) {
t.Errorf("Failed to read file %v: %v", fInfo.Name(), err)
continue
}
var typedData core.TypedData
var typedData apitypes.TypedData
err = json.Unmarshal(data, &typedData)
if err != nil {
t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err)
@ -498,7 +499,7 @@ var gnosisTx = `
// TestGnosisTypedData tests the scenario where a user submits a full EIP-712
// struct without using the gnosis-specific endpoint
func TestGnosisTypedData(t *testing.T) {
var td core.TypedData
var td apitypes.TypedData
err := json.Unmarshal([]byte(gnosisTypedData), &td)
if err != nil {
t.Fatalf("unmarshalling failed '%v'", err)