From 35cb892b686caa31bd8199555553bac3f0d185e6 Mon Sep 17 00:00:00 2001 From: Paul Berg Date: Thu, 11 Oct 2018 13:05:54 -0700 Subject: [PATCH] Working on TypedData --- cmd/clef/audit.log | 11 -------- signer/core/api.go | 41 ++++++++++++++++++++++------- signer/core/{apiv2.go => apiv2.goz} | 2 +- signer/core/auditlog.go | 6 ++--- 4 files changed, 35 insertions(+), 25 deletions(-) delete mode 100644 cmd/clef/audit.log rename signer/core/{apiv2.go => apiv2.goz} (91%) diff --git a/cmd/clef/audit.log b/cmd/clef/audit.log deleted file mode 100644 index 66302cac97..0000000000 --- a/cmd/clef/audit.log +++ /dev/null @@ -1,11 +0,0 @@ -t=2018-10-10T14:45:26-0700 lvl=info msg=Configured api=signer audit log=audit.log -t=2018-10-10T14:45:37-0700 lvl=info msg=SignStructuredData api=signer type=request metadata="{\"remote\":\"127.0.0.1:59942\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826 [chksum ok]" data={PrimaryType:0x4d61696c} -t=2018-10-10T14:45:37-0700 lvl=info msg=SignStructuredData api=signer type=response data= error=nil -t=2018-10-10T14:45:55-0700 lvl=info msg=Configured api=signer audit log=audit.log -t=2018-10-10T14:45:55-0700 lvl=info msg=SignStructuredData api=signer type=request metadata="{\"remote\":\"127.0.0.1:59956\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826 [chksum ok]" data={PrimaryType:0x4d61696c} -t=2018-10-10T14:46:18-0700 lvl=info msg=Configured api=signer audit log=audit.log -t=2018-10-10T14:46:50-0700 lvl=info msg=SignStructuredData api=signer type=request metadata="{\"remote\":\"127.0.0.1:59967\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826 [chksum ok]" data={PrimaryType:0x4d61696c} -t=2018-10-10T14:51:32-0700 lvl=info msg=Configured api=signer audit log=audit.log -t=2018-10-10T14:51:59-0700 lvl=info msg=SignStructuredData api=signer type=request metadata="{\"remote\":\"127.0.0.1:60000\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826 [chksum ok]" data="{Hash:[226 140 80 200 149 243 254 73 125 193 200 45 83 189 176 154 4 204 167 86 220 130 208 11 170 238 116 129 150 85 62 148]}" -t=2018-10-10T14:52:51-0700 lvl=info msg=Configured api=signer audit log=audit.log -t=2018-10-10T14:52:56-0700 lvl=info msg=SignStructuredData api=signer type=request metadata="{\"remote\":\"127.0.0.1:60017\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826 [chksum ok]" data="{Hash:[226 140 80 200 149 243 254 73 125 193 200 45 83 189 176 154 4 204 167 86 220 130 208 11 170 238 116 129 150 85 62 148]}" diff --git a/signer/core/api.go b/signer/core/api.go index 6171331313..603fcc2e91 100644 --- a/signer/core/api.go +++ b/signer/core/api.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto/sha3" "io/ioutil" @@ -53,7 +54,7 @@ type ExternalAPI interface { // SignData - request to sign the given data (plus prefix) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data hexutil.Bytes) (hexutil.Bytes, error) // SignStructuredData - request to sign the given structured data (plus prefix) - SignStructuredData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) + SignStructuredData(ctx context.Context, data TypedData) (hexutil.Bytes, error) // EcRecover - recover public key from given message and signature EcRecover(ctx context.Context, contentType string, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) // Export - request to export an account @@ -608,11 +609,6 @@ func (api *SignerAPI) DetermineSignatureFormat(contentType string, data hexutil. sighash, msg := SignDataWithValidator(data) req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType} - case DataStructured.Mime: - // Typed data according to EIP712 - - sighash, msg := SignDataStructured(data) - req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType} case DataPlain.Mime: // Sign calculates an Ethereum ECDSA signature for: // keccack256("\x19${byte version}Ethereum Signed Message:\n" + len(message) + message)) @@ -672,12 +668,37 @@ func SignDataWithValidator(data []byte) ([]byte, string) { return crypto.Keccak256([]byte(msg)), msg } -// SignDataStructured signs the given message according to EIP712. +// TypedData represents a request to create a new filter. +type TypedData ethereum.TypedData + +// SignStructuredData signs the given message according to EIP712. // // https://github.com/ethereum/EIPs/issues/712 -func SignDataStructured(data []byte) ([]byte, string) { - msg := "TODO" - return crypto.Keccak256([]byte(msg)), msg +// +// If the format "\x19\x46" ‖ domainSeparator ‖ hashStruct(message)` is not respected, +// an error is returned +func (api *SignerAPI) SignStructuredData(ctx context.Context, data TypedData) (hexutil.Bytes, error) { + fmt.Println("data", data) + fmt.Println("data.Hash", data.Hash) + return common.Hex2Bytes("0xdeadbeef"), nil +} + +// UnmarshalJSON sets *args fields with given data. +func (args *TypedData) UnmarshalJSON(data []byte) error { + type input struct { + Hash *common.Hash `json:"hash"` + } + + var raw input + if err := json.Unmarshal(data, &raw); err != nil { + return err + } + + if raw.Hash != nil { + args.Hash = raw.Hash + } + + return nil } // SignDataPlain is a helper function that calculates a hash for the given message that can be diff --git a/signer/core/apiv2.go b/signer/core/apiv2.goz similarity index 91% rename from signer/core/apiv2.go rename to signer/core/apiv2.goz index 0ef25cb8d2..87716a8736 100644 --- a/signer/core/apiv2.go +++ b/signer/core/apiv2.goz @@ -32,7 +32,7 @@ type TypedData ethereum.TypedData // // If the format "\x19\x46" ‖ domainSeparator ‖ hashStruct(message)` is not respected, // an error is returned -func (api *SignerAPI) SignStructuredData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) { +func (api *SignerAPI) SignStructuredData(ctx context.Context, data TypedData) (hexutil.Bytes, error) { fmt.Println("data", data) fmt.Println("data.Hash", data.Hash) return common.Hex2Bytes("0xdeadbeef"), nil diff --git a/signer/core/auditlog.go b/signer/core/auditlog.go index ed14d5cc64..93b165568f 100644 --- a/signer/core/auditlog.go +++ b/signer/core/auditlog.go @@ -70,10 +70,10 @@ func (l *AuditLogger) SignData(ctx context.Context, contentType string, addr com return b, e } -func (l *AuditLogger) SignStructuredData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) { +func (l *AuditLogger) SignStructuredData(ctx context.Context, data TypedData) (hexutil.Bytes, error) { l.log.Info("SignStructuredData", "type", "request", "metadata", MetadataFromContext(ctx).String(), - "addr", addr.String(), "data", data) - b, e := l.api.SignStructuredData(ctx, addr, TypedData{}) + "addr", "data", data) + b, e := l.api.SignStructuredData(ctx, TypedData{}) l.log.Info("SignStructuredData", "type", "response", "data", common.Bytes2Hex(b), "error", e) return b, e }