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) {
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!"}}`
//_, err := api.SignData(ctx, accounts.MimetypeTypedData, *addr, hexutil.Encode([]byte(data)))
var typedData core.TypedData
var typedData apitypes.TypedData
json.Unmarshal([]byte(data), &typedData)
_, err := api.SignTypedData(ctx, *addr, typedData)
expectApprove("sign 712 typed data", err)
......@@ -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" +
"the user with the contents of the `message`"
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{
Address: common.NewMixedcaseAddress(a),
......
......@@ -57,7 +57,7 @@ type ExternalAPI interface {
// SignData - request to sign the given data (plus prefix)
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(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(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error)
// Version info about the APIs
......@@ -235,7 +235,7 @@ type (
ContentType string `json:"content_type"`
Address common.MixedcaseAddress `json:"address"`
Rawdata []byte `json:"raw_data"`
Messages []*NameValueType `json:"messages"`
Messages []*apitypes.NameValueType `json:"messages"`
Callinfo []apitypes.ValidationInfo `json:"call_info"`
Hash hexutil.Bytes `json:"hash"`
Meta Metadata `json:"meta"`
......
......@@ -14,7 +14,7 @@
// 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/>.
package core
package apitypes
import (
"bytes"
......
This diff is collapsed.
......@@ -89,7 +89,7 @@ func (l *AuditLogger) SignGnosisSafeTx(ctx context.Context, addr common.Mixedcas
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(),
"addr", addr.String(), "data", data)
b, e := l.api.SignTypedData(ctx, addr, data)
......
......@@ -34,15 +34,15 @@ type GnosisSafeTx struct {
}
// 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
if tx.Data != nil {
data = *tx.Data
}
gnosisTypedData := TypedData{
Types: Types{
"EIP712Domain": []Type{{Name: "verifyingContract", Type: "address"}},
"SafeTx": []Type{
gnosisTypedData := apitypes.TypedData{
Types: apitypes.Types{
"EIP712Domain": []apitypes.Type{{Name: "verifyingContract", Type: "address"}},
"SafeTx": []apitypes.Type{
{Name: "to", Type: "address"},
{Name: "value", Type: "uint256"},
{Name: "data", Type: "bytes"},
......@@ -55,11 +55,11 @@ func (tx *GnosisSafeTx) ToTypedData() TypedData {
{Name: "nonce", Type: "uint256"},
},
},
Domain: TypedDataDomain{
Domain: apitypes.TypedDataDomain{
VerifyingContract: tx.Safe.Address().Hex(),
},
PrimaryType: "SafeTx",
Message: TypedDataMessage{
Message: apitypes.TypedDataMessage{
"to": tx.To.Address().Hex(),
"value": tx.Value.String(),
"data": data,
......
This diff is collapsed.
......@@ -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)
......
......@@ -605,7 +605,7 @@ function ApproveSignData(r){
t.Logf("address %v %v\n", addr.String(), addr.Original())
nvt := []*core.NameValueType{
nvt := []*apitypes.NameValueType{
{
Name: "message",
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