clef, signer: address some review concerns

This commit is contained in:
Martin Holst Swende 2019-01-15 13:32:44 +01:00 committed by Paul Berg
parent 1e8f19db0a
commit b0e872cd21
5 changed files with 12 additions and 34 deletions

View file

@ -489,11 +489,8 @@ Response
### account_ecRecover ### account_ecRecover
#### Sign data #### Sign data
<<<<<<< HEAD
======= Derive the address from the account that was used to sign data with content type `text/plain` and the signature.
>>>>>>> c72099670... Added example RPC calls for account_signData and account_signTypedData
Derive the address from the account that was used to sign data with content type `text/plain` and the signature.
#### Arguments #### Arguments
- data [data]: data that was signed - data [data]: data that was signed

View file

@ -15,7 +15,6 @@ The addition of `contentType` makes it possible to use the method for different
* The external `account_Ecrecover`-method was removed. * The external `account_Ecrecover`-method was removed.
* The external `account_Import`-method was removed. * The external `account_Import`-method was removed.
#### 3.0.0 #### 3.0.0
* The external `account_List`-method was changed to not expose `url`, which contained info about the local filesystem. It now returns only a list of addresses. * The external `account_List`-method was changed to not expose `url`, which contained info about the local filesystem. It now returns only a list of addresses.

View file

@ -176,7 +176,6 @@ type (
ContentType string `json:"content_type"` ContentType string `json:"content_type"`
Address common.MixedcaseAddress `json:"address"` Address common.MixedcaseAddress `json:"address"`
Rawdata interface{} `json:"raw_data"` Rawdata interface{} `json:"raw_data"`
//Message string `json:"message"`
Message []*NameValueType `json:"message"` Message []*NameValueType `json:"message"`
Hash hexutil.Bytes `json:"hash"` Hash hexutil.Bytes `json:"hash"`
Meta Metadata `json:"meta"` Meta Metadata `json:"meta"`

View file

@ -29,17 +29,15 @@ import (
"strings" "strings"
"unicode" "unicode"
"golang.org/x/crypto/sha3"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"golang.org/x/crypto/sha3"
) )
type SigFormat struct { type SigFormat struct {
@ -118,7 +116,7 @@ type TypedDataDomain struct {
Salt string `json:"salt"` Salt string `json:"salt"`
} }
var typedDataRegexp = regexp.MustCompile(`^((address|bool|bytes|string)|((bytes)([1-9]|[1-2][0-9]|3[0-2]))|((int|uint)(8|16|32|64|128|256)))(\[])?$`) var typedDataRegexp = regexp.MustCompile(`^((address|bool|bytes|string)|((bytes)([1-9]|[1-2][0-9]|3[0-2]))|((int|uint)(8|16|32|64|128|256)))(\[\])?$`)
var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Z](\w*)(\[])?$`) var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Z](\w*)(\[])?$`)
// Sign receives a request and produces a signature // Sign receives a request and produces a signature
@ -222,7 +220,7 @@ func (api *SignerAPI) determineSignatureFormat(contentType string, addr common.M
}, },
} }
req = &SignDataRequest{ContentType: mediaType, Rawdata: cliqueData, Message: message, Hash: sighash} req = &SignDataRequest{ContentType: mediaType, Rawdata: cliqueData, Message: message, Hash: sighash}
case TextPlain.Mime: default: // also case TextPlain.Mime:
// Calculates an Ethereum ECDSA signature for: // Calculates an Ethereum ECDSA signature for:
// hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}") // hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}")
plainData, err := hexutil.Decode(data.(string)) plainData, err := hexutil.Decode(data.(string))
@ -239,8 +237,6 @@ func (api *SignerAPI) determineSignatureFormat(contentType string, addr common.M
} }
req = &SignDataRequest{ContentType: mediaType, Rawdata: plainData, Message: message, Hash: sighash} req = &SignDataRequest{ContentType: mediaType, Rawdata: plainData, Message: message, Hash: sighash}
default:
return nil, fmt.Errorf("content type '%s' not implemented for signing", contentType)
} }
return req, nil return req, nil
@ -315,7 +311,6 @@ func (api *SignerAPI) SignTypedData(ctx context.Context, addr common.MixedcaseAd
return nil, err return nil, err
} }
sighash := crypto.Keccak256([]byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))) sighash := crypto.Keccak256([]byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash))))
//output := typedData.PrettyPrint()
message := typedData.Format() message := typedData.Format()
req := &SignDataRequest{ContentType: DataTyped.Mime, Rawdata: typedData.Map(), Message: message, Hash: sighash} req := &SignDataRequest{ContentType: DataTyped.Mime, Rawdata: typedData.Map(), Message: message, Hash: sighash}
signature, err := api.Sign(ctx, addr, req) signature, err := api.Sign(ctx, addr, req)
@ -391,6 +386,7 @@ func (typedData *TypedData) EncodeType(primaryType string) hexutil.Bytes {
return buffer.Bytes() return buffer.Bytes()
} }
// TypeHash creates the keccak256 hash of the data
func (typedData *TypedData) TypeHash(primaryType string) hexutil.Bytes { func (typedData *TypedData) TypeHash(primaryType string) hexutil.Bytes {
return crypto.Keccak256(typedData.EncodeType(primaryType)) return crypto.Keccak256(typedData.EncodeType(primaryType))
} }
@ -628,7 +624,7 @@ func (typedData *TypedData) PrettyPrint() string {
return output.String() return output.String()
} }
// Format returns a representation of d, which can be easily displayed by a user-interface // Format returns a representation of typedData, which can be easily displayed by a user-interface
// without in-depth knowledge about 712 rules // without in-depth knowledge about 712 rules
func (typedData *TypedData) Format() []*NameValueType { func (typedData *TypedData) Format() []*NameValueType {
var nvts []*NameValueType var nvts []*NameValueType

View file

@ -275,7 +275,7 @@ func TestEncodeData(t *testing.T) {
} }
} }
func TestMalformedData1(t *testing.T) { func TestMalformedDomainkeys(t *testing.T) {
// Verifies that malformed domain keys are properly caught: // Verifies that malformed domain keys are properly caught:
//{ //{
// "name": "Ether Mail", // "name": "Ether Mail",
@ -365,7 +365,7 @@ func TestMalformedData1(t *testing.T) {
} }
} }
func TestMalformedData2(t *testing.T) { func TestTypeMismatch(t *testing.T) {
// Verifies that: // Verifies that:
// 1. Mismatches between the given type and data, i.e. `Person` and // 1. Mismatches between the given type and data, i.e. `Person` and
// the data item is a string, are properly caught: // the data item is a string, are properly caught:
@ -472,7 +472,7 @@ func TestMalformedData2(t *testing.T) {
} }
} }
func TestMalformedData3(t *testing.T) { func TestMalformedTypesAndExtradata(t *testing.T) {
// Verifies several quirks // Verifies several quirks
// 1. Using dynamic types and only validating the prefix: // 1. Using dynamic types and only validating the prefix:
//{ //{
@ -567,7 +567,7 @@ func TestMalformedData3(t *testing.T) {
} }
} }
func TestMalformedData4(t *testing.T) { func TestTypeMismatch(t *testing.T) {
// Verifies data that doesn't fit into it: // Verifies data that doesn't fit into it:
//{ //{
// "test": 65536 <-- test defined as uint8 // "test": 65536 <-- test defined as uint8
@ -606,19 +606,6 @@ func TestFormatter(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unmarshalling failed %v", err) t.Fatalf("unmarshalling failed %v", err)
} }
//x := PrettyPrintPrimitiveValue("address", "wallet", "0x123123123", 1)
//fmt.Printf(x)
//y := FormatPrimitiveValue("address", "wallet", "0x123123123")
//fmt.Printf("%v\n", y)
//fmt.Printf(d.PrettyPrintData(d.PrimaryType,d.Message, 1))
//formatted := d.FormatData(d.PrimaryType,d.Message)
//for _,item := range formatted{
// fmt.Printf("%v\n", item.Pprint(0))
//}
formatted := d.Format() formatted := d.Format()
for _, item := range formatted { for _, item := range formatted {
fmt.Printf("%v\n", item.Pprint(0)) fmt.Printf("%v\n", item.Pprint(0))
@ -629,7 +616,7 @@ func TestFormatter(t *testing.T) {
} }
func TestMalformedData5(t *testing.T) { func TestCustomTypeAsArray(t *testing.T) {
var jsonTypedData = ` var jsonTypedData = `
{ {
"types": { "types": {