// Copyright 2018 The go-ethereum Authors // This file is part of go-ethereum. // // go-ethereum is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // go-ethereum is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with go-ethereum. If not, see . // package core import ( "context" "encoding/json" "fmt" "math/big" "testing" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ) var typesStandard = EIP712Types{ "EIP712Domain": { { "name": "name", "type": "string", }, { "name": "version", "type": "string", }, { "name": "chainId", "type": "uint256", }, { "name": "verifyingContract", "type": "address", }, }, "Person": { { "name": "name", "type": "string", }, { "name": "wallet", "type": "address", }, }, "Mail": { { "name": "from", "type": "Person", }, { "name": "to", "type": "Person", }, { "name": "contents", "type": "string", }, }, } const primaryType = "Mail" var domainStandard = EIP712Domain{ "Ether Mail", "1", big.NewInt(1), "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", "", } var messageStandard = map[string]interface{}{ "from": map[string]interface{}{ "name": "Cow", "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", }, "to": map[string]interface{}{ "name": "Bob", "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", }, "contents": "Hello, Bob!", } var typedData = TypedData{ Types: typesStandard, PrimaryType: primaryType, Domain: domainStandard, Message: messageStandard, } func TestSignData(t *testing.T) { api, control := setup(t) //Create two accounts createAccount(control, api, t) createAccount(control, api, t) control <- "1" list, err := api.List(context.Background()) if err != nil { t.Fatal(err) } a := common.NewMixedcaseAddress(list[0]) control <- "Y" control <- "wrongpassword" signature, err := api.SignData(context.Background(), TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world"))) if signature != nil { t.Errorf("Expected nil-data, got %x", signature) } if err != keystore.ErrDecrypt { t.Errorf("Expected ErrLocked! %v", err) } control <- "No way" signature, err = api.SignData(context.Background(), TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world"))) if signature != nil { t.Errorf("Expected nil-data, got %x", signature) } if err != ErrRequestDenied { t.Errorf("Expected ErrRequestDenied! %v", err) } // text/plain control <- "Y" control <- "a_long_password" signature, err = api.SignData(context.Background(), TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world"))) if err != nil { t.Fatal(err) } if signature == nil || len(signature) != 65 { t.Errorf("Expected 65 byte signature (got %d bytes)", len(signature)) } // data/typed control <- "Y" control <- "a_long_password" signature, err = api.SignTypedData(context.Background(), a, typedData) if err != nil { t.Fatal(err) } if signature == nil || len(signature) != 65 { t.Errorf("Expected 65 byte signature (got %d bytes)", len(signature)) } } func TestHashStruct(t *testing.T) { hash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message) if err != nil { t.Fatal(err) } mainHash := fmt.Sprintf("0x%s", common.Bytes2Hex(hash)) if mainHash != "0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e" { t.Errorf("Expected different hashStruct result (got %s)", mainHash) } hash, err = typedData.HashStruct("EIP712Domain", typedData.Domain.Map()) if err != nil { t.Error(err) } domainHash := fmt.Sprintf("0x%s", common.Bytes2Hex(hash)) if domainHash != "0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f" { t.Errorf("Expected different hashStruct result (got %s)", domainHash) } } func TestEncodeType(t *testing.T) { domainTypeEncoding := string(typedData.EncodeType("EIP712Domain")) if domainTypeEncoding != "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" { t.Errorf("Expected different encodeType result (got %s)", domainTypeEncoding) } mailTypeEncoding := string(typedData.EncodeType(typedData.PrimaryType)) if mailTypeEncoding != "Mail(Person from,Person to,string contents)Person(string name,address wallet)" { t.Errorf("Expected different encodeType result (got %s)", mailTypeEncoding) } } func TestTypeHash(t *testing.T) { mailTypeHash := fmt.Sprintf("0x%s", common.Bytes2Hex(typedData.TypeHash(typedData.PrimaryType))) if mailTypeHash != "0xa0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2" { t.Errorf("Expected different typeHash result (got %s)", mailTypeHash) } } func TestEncodeData(t *testing.T) { hash, err := typedData.EncodeData(typedData.PrimaryType, typedData.Message) if err != nil { t.Fatal(err) } dataEncoding := fmt.Sprintf("0x%s", common.Bytes2Hex(hash)) if dataEncoding != "0xa0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2fc71e5fa27ff56c350aa531bc129ebdf613b772b6604664f5d8dbe21b85eb0c8cd54f074a4af31b4411ff6a60c9719dbd559c221c8ac3492d9d872b041d703d1b5aadf3154a261abdd9086fc627b61efca26ae5702701d05cd2305f7c52a2fc8" { t.Errorf("Expected different encodeData result (got %s)", dataEncoding) } } func TestMalformedData1(t *testing.T) { var 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": "wallet", "type": "address" } ], "Mail": [ { "name": "from", "type": "Person" }, { "name": "to", "type": "Person" }, { "name": "contents", "type": "Person" } ] }, "primaryType": "Mail", "domain": { "name": "Ether Mail", "version": "1", "chainId": 1, "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" }, "message": { "from": { "name": "Cow", "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" }, "to": { "name": "Bob", "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" }, "contents": "Hello, Bob!" } } ` var typedData TypedData err := json.Unmarshal([]byte(data), &typedData) if err != nil { t.Fatalf("unmarshalling failed %v", err) } err = typedData.Validate() if err != nil { t.Fatalf("Expected no error, got %v", err) } _, err = typedData.HashStruct(typedData.PrimaryType, typedData.Message) if err.Error() != "provided data 'Hello, Bob!' doesn't match type 'Person'" { t.Errorf("Expected `provided data 'Hello, Bob!' doesn't match type 'Person'`, got %v", err) } } func TestMalformedDomainData(t *testing.T) { var 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": "wallet", "type": "address" } ], "Mail": [ { "name": "from", "type": "Person" }, { "name": "to", "type": "Person" }, { "name": "contents", "type": "Blahonga" } ] }, "primaryType": "Mail", "domain": { "name": "Ether Mail", "version": "1", "chainId": 1, "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" }, "message": { "from": { "name": "Cow", "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" }, "to": { "name": "Bob", "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" }, "contents": "Hello, Bob!" } }` var typedData TypedData err := json.Unmarshal([]byte(data), &typedData) if err != nil { t.Fatalf("unmarshalling failed %v", err) } err = typedData.Validate() if err == nil { t.Fatalf("Expected `referenced type 'Blahonga' is undefined`, got %v", err) } _, err = typedData.HashStruct(typedData.PrimaryType, typedData.Message) if err.Error() != "unrecognized interface type " { t.Errorf("Expected `unrecognized interface type `, got %v", err) } } func TestMalformedData3(t *testing.T) { var 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": "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, "vxerifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" }, "message": { "from": { "name": "Cow", "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" }, "to": { "name": "Bob", "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" }, "contents": "Hello, Bob!" } } ` var typedData TypedData err := json.Unmarshal([]byte(data), &typedData) if err != nil { t.Fatalf("unmarshalling failed %v", err) } err = typedData.Validate() if err != nil { t.Fatalf("Expected no error, got %v", err) } _, err = typedData.HashStruct("EIP712Domain", typedData.Domain.Map()) if err.Error() != "provided data '' doesn't match type 'address'" { t.Errorf("Expected `provided data '' doesn't match type 'address'`, got %v", err) } } func TestMalformedData4(t *testing.T) { var data = ` { "types": { "EIP712Domain": [ { "name": "name", "type": "string" }, { "name": "version", "type": "string" }, { "name": "chainId", "type": "uint256 ... and now for something completely different" }, { "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": { "Signed by": "Bill Gates -- this text won't affect the hash'", "we can": "stuff anything here, really", "name": "Ether Mail", "version": "65536", "chainId": 1, "verifyingContract": "0xCCCcccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" }, "message": { "from": { "name": "Cow", "wallet": "0xcD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" }, "to": { "name": "Bob", "test": 65536, "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" }, "blahonga": "zonk bonk", "contents": "åäzö \r\n test, Bob!" } } ` // The struct above contains several quirks // 1. Using dynamic types and only validating the prefix: //{ // "name": "chainId", // "type": "uint256 ... and now for something completely different" //}, // 2. Using dynamic types, but not verifying that the data fits into it // "test": 65536, <-- test defined as uint8 // 3a. Extra data in message // "blahonga": "zonk bonk", // 3b ... and in domain // "Signed by": "Bill Gates", var typedData TypedData err := json.Unmarshal([]byte(data), &typedData) if err != nil { t.Fatalf("unmarshalling failed %v", err) } err = typedData.Validate() if err.Error() != "unknown atomic type 'uint256 ... and now for something completely different'" { t.Fatalf("Expected `unknown atomic type 'uint256 ... and now for something completely different'`, got %v", err) } }