Changed method to signTypedData

This commit is contained in:
Paul Berg 2018-10-14 19:57:28 +01:00
parent ae5583a93c
commit 86dbe8aa3e
5 changed files with 98 additions and 89 deletions

View file

@ -1 +1,16 @@
t=2018-10-13T05:03:16-0700 lvl=info msg=Configured api=signer audit log=audit.log t=2018-10-13T05:03:16-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:04:21-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:04:35-0700 lvl=info msg=SignStructuredData api=signer type=request metadata="{\"remote\":\"127.0.0.1:61564\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr=data LOG15_ERROR= LOG15_ERROR="Normalized odd number of arguments by adding nil"
t=2018-10-13T05:04:35-0700 lvl=info msg=SignStructuredData api=signer type=response data= error=nil
t=2018-10-13T05:07:31-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:35:59-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:37:45-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:42:26-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:43:13-0700 lvl=info msg=SignTypedData api=signer type=request metadata="{\"remote\":\"127.0.0.1:61669\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0x66d76e6a80dc7d46d7ec1b79b15dfa34c3c6ef21 [chksum INVALID]" data="{Types:map[EIP712Domain:[map[name:name type:string] map[type:string name:version] map[name:chainId type:uint256] map[name:verifyingContract type:address]] Person:[map[name:name type:string] map[name:wallet type:address]] Mail:[map[name:from type:Person] map[name:to type:Person] map[name:contents type:string]]] PrimaryType:Mail Domain:{Name:Ether Mail Version:1 ChainId:{neg:false abs:[1]} VerifyingContract:[204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204] Salt:0x} Message:map[contents:Hello, Bob! from:map[name:Alice wallet:0x66d76e6a80dc7d46d7ec1b79b15dfa34c3c6ef21] to:map[name:Bob wallet:0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB]]}"
t=2018-10-13T05:43:13-0700 lvl=info msg=SignTypedData api=signer type=response data= error=nil
t=2018-10-13T05:43:50-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:44:06-0700 lvl=info msg=SignTypedData api=signer type=request metadata="{\"remote\":\"127.0.0.1:61680\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0x66d76e6a80dc7d46d7ec1b79b15dfa34c3c6ef21 [chksum INVALID]" data="{Types:map[Mail:[map[name:from type:Person] map[type:Person name:to] map[name:contents type:string]] EIP712Domain:[map[name:name type:string] map[name:version type:string] map[name:chainId type:uint256] map[name:verifyingContract type:address]] Person:[map[type:string name:name] map[name:wallet type:address]]] PrimaryType:Mail Domain:{Name:Ether Mail Version:1 ChainId:{neg:false abs:[1]} VerifyingContract:[204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204] Salt:0x} Message:map[from:map[wallet:0x66d76e6a80dc7d46d7ec1b79b15dfa34c3c6ef21 name:Alice] to:map[name:Bob wallet:0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB] contents:Hello, Bob!]}"
t=2018-10-13T05:44:06-0700 lvl=info msg=SignTypedData api=signer type=response data= error=nil
t=2018-10-13T05:46:53-0700 lvl=info msg=Configured api=signer audit log=audit.log
t=2018-10-13T05:46:58-0700 lvl=info msg=SignTypedData api=signer type=request metadata="{\"remote\":\"127.0.0.1:61694\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\",\"User-Agent\":\"PostmanRuntime/7.3.0\",\"Origin\":\"\"}" addr="0x66d76e6a80dc7d46d7ec1b79b15dfa34c3c6ef21 [chksum INVALID]" data="{Types:map[EIP712Domain:[map[name:name type:string] map[name:version type:string] map[name:chainId type:uint256] map[name:verifyingContract type:address]] Person:[map[name:name type:string] map[name:wallet type:address]] Mail:[map[name:from type:Person] map[type:Person name:to] map[name:contents type:string]]] PrimaryType:Mail Domain:{Name:Ether Mail Version:1 ChainId:{neg:false abs:[1]} VerifyingContract:[204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204] Salt:0x} Message:map[from:map[name:Alice wallet:0x66d76e6a80dc7d46d7ec1b79b15dfa34c3c6ef21] to:map[name:Bob wallet:0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB] contents:Hello, Bob!]}"
t=2018-10-13T05:46:58-0700 lvl=info msg=SignTypedData api=signer type=response data= error=nil

View file

@ -53,7 +53,7 @@ type ExternalAPI interface {
// SignData - request to sign the given data (plus prefix) // SignData - request to sign the given data (plus prefix)
SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data hexutil.Bytes) (hexutil.Bytes, error) 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 - request to sign the given structured data (plus prefix)
SignStructuredData(ctx context.Context, data TypedData) (hexutil.Bytes, error) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error)
// EcRecover - recover public key from given message and signature // EcRecover - recover public key from given message and signature
EcRecover(ctx context.Context, contentType string, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) EcRecover(ctx context.Context, contentType string, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error)
// Export - request to export an account // Export - request to export an account
@ -118,21 +118,21 @@ type SigFormat struct {
} }
var ( var (
ApplicationValidator = SigFormat{ TextPlain = SigFormat{
"application/validator", "text/plain",
0x00, 0x00,
} }
TextValidator = SigFormat{
"text/validator",
0x01,
}
DataTyped = SigFormat{
"data/typed",
0x45,
}
ApplicationClique = SigFormat{ ApplicationClique = SigFormat{
"application/clique", "application/clique",
0x01, 0x90,
}
DataPlain = SigFormat{
"data/plain",
0x45,
}
DataStructured = SigFormat{
"data/structured",
0x46,
} }
) )
@ -551,7 +551,7 @@ func (api *SignerAPI) SignTransaction(ctx context.Context, args SendTxArgs, meth
// where the V value will be 27 or 28 for legacy reasons. // where the V value will be 27 or 28 for legacy reasons.
func (api *SignerAPI) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data hexutil.Bytes) (hexutil.Bytes, error) { func (api *SignerAPI) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data hexutil.Bytes) (hexutil.Bytes, error) {
var req, err = api.DetermineSignatureFormat(contentType, data) var req, err = api.determineSignatureFormat(contentType, data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -584,40 +584,56 @@ func (api *SignerAPI) SignData(ctx context.Context, contentType string, addr com
} }
// Determines which signature method should be used based upon the mime type // Determines which signature method should be used based upon the mime type
func (api *SignerAPI) DetermineSignatureFormat(contentType string, data hexutil.Bytes) (*SignDataRequest, error) { func (api *SignerAPI) determineSignatureFormat(contentType string, data hexutil.Bytes) (*SignDataRequest, error) {
var req *SignDataRequest var req *SignDataRequest
mediaType, _, err := mime.ParseMediaType(contentType) mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil { if err != nil {
return nil, err return nil, err
} }
switch mediaType { switch mediaType {
case ApplicationClique.Mime: case TextValidator.Mime:
// Clique is the Ethereum PoA standard
header := &types.Header{}
if err := rlp.DecodeBytes(data, header); err != nil {
return nil, err
}
sighash, err := SignCliqueHeader(header)
if err != nil {
return nil, err
}
msg := fmt.Sprintf("Clique block %d [0x%x]", header.Number, header.Hash())
req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType}
case ApplicationValidator.Mime:
// Data with an intended validator // Data with an intended validator
sighash, msg := SignDataWithValidator(data) sighash, msg := signTextWithValidator(data)
req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType} req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType}
case DataPlain.Mime: case TextPlain.Mime:
// Sign calculates an Ethereum ECDSA signature for: // Sign calculates an Ethereum ECDSA signature for:
// keccack256("\x19${byte version}Ethereum Signed Message:\n" + len(message) + message)) // hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}")
// In the cases where it matters ensure that the charset is handled. The charset // In the cases where it matters ensure that the charset is handled. The charset
// resides in the 'params' returned as the second returnvalue from mime.ParseMediaType // resides in the 'params' returned as the second returnvalue from mime.ParseMediaType
// charset, ok := params["charset"] // charset, ok := params["charset"]
// As it is now, we accept any charset and just treat it as 'raw'. // As it is now, we accept any charset and just treat it as 'raw'.
sighash, msg := SignDataPlain(data) sighash, msg := signTextPlain(data)
req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType}
//case DataTyped.Mime:
// // Typed data according to EIP712:
// //
// // hash = keccak256("\x19${byteVersion}${domainSeparator}${hashStruct(message)}")
// fmt.Println("Did we get here, chief? #1")
// typedData := TypedData{}
// if err := rlp.DecodeBytes(data, typedData); err != nil {
// return nil, err
// }
// fmt.Println("Did we get here, chief? #2")
// sighash, err := signTypedData(context.Background(), typedData)
// if err != nil {
// return nil, err
// }
// msg := fmt.Sprintf("Typed data domain %s", typedData.Domain)
// req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType}
case ApplicationClique.Mime:
// Clique is the Ethereum PoA standard
header := &types.Header{}
if err := rlp.DecodeBytes(data, header); err != nil {
return nil, err
}
sighash, err := signCliqueHeader(header)
if err != nil {
return nil, err
}
msg := fmt.Sprintf("Clique block %d [0x%x]", header.Number, header.Hash())
req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType} req = &SignDataRequest{Rawdata: data, Message: msg, Hash: sighash, ContentType: mediaType}
default: default:
return nil, fmt.Errorf("content type '%s' not implemented for signing", contentType) return nil, fmt.Errorf("content type '%s' not implemented for signing", contentType)
@ -626,6 +642,25 @@ func (api *SignerAPI) DetermineSignatureFormat(contentType string, data hexutil.
} }
// signTextPlain is a helper function that calculates a hash for the given message that can be
// safely used to calculate a signature from.
//
// The hash is calculated as
// keccak256("\x19${byteVersion}Ethereum Signed Message:\n"${message length}${message}).
//
// This gives context to the signed message and prevents signing of transactions.
func signTextPlain(data []byte) ([]byte, string) {
msg := fmt.Sprintf("\x19\\x%xEthereum Signed Message:\n%d%s", TextPlain.ByteVersion, len(data), data)
return crypto.Keccak256([]byte(msg)), msg
}
// signTextWithValidator signs the given message which can be further recovered
// with the given validator.
func signTextWithValidator(data []byte) ([]byte, string) {
msg := "TODO"
return crypto.Keccak256([]byte(msg)), msg
}
// SignCliqueHeader returns the hash which is used as input for the proof-of-authority // SignCliqueHeader returns the hash which is used as input for the proof-of-authority
// signing. It is the hash of the entire header apart from the 65 byte signature // signing. It is the hash of the entire header apart from the 65 byte signature
// contained at the end of the extra data. // contained at the end of the extra data.
@ -633,7 +668,7 @@ func (api *SignerAPI) DetermineSignatureFormat(contentType string, data hexutil.
// The method requires the extra data to be at least 65 bytes -- the original implementation // The method requires the extra data to be at least 65 bytes -- the original implementation
// in clique.go panics if this is the case, thus it's been reimplemented here to avoid the panic // in clique.go panics if this is the case, thus it's been reimplemented here to avoid the panic
// and simply return an error instead // and simply return an error instead
func SignCliqueHeader(header *types.Header) (hexutil.Bytes, error) { func signCliqueHeader(header *types.Header) (hexutil.Bytes, error) {
hash := common.Hash{} hash := common.Hash{}
if len(header.Extra) < 65 { if len(header.Extra) < 65 {
return hash.Bytes(), fmt.Errorf("clique header extradata too short, %d < 65", len(header.Extra)) return hash.Bytes(), fmt.Errorf("clique header extradata too short, %d < 65", len(header.Extra))
@ -660,38 +695,19 @@ func SignCliqueHeader(header *types.Header) (hexutil.Bytes, error) {
return hash.Bytes(), nil return hash.Bytes(), nil
} }
// SignDataWithValidator signs the given message which can be further recovered
// with the given validator.
func SignDataWithValidator(data []byte) ([]byte, string) {
msg := "TODO"
return crypto.Keccak256([]byte(msg)), msg
}
// SignDataPlain is a helper function that calculates a hash for the given message that can be
// safely used to calculate a signature from.
//
// The hash is calculated as
// keccak256("\x19${byte version}Ethereum Signed Message:\n"${message length}${message}).
//
// This gives context to the signed message and prevents signing of transactions.
func SignDataPlain(data []byte) ([]byte, string) {
msg := fmt.Sprintf("\x19\\x%xEthereum Signed Message:\n%d%s", DataPlain.ByteVersion, len(data), data)
return crypto.Keccak256([]byte(msg)), msg
}
// Determines the content type and then recovers the address associated with the given sig // Determines the content type and then recovers the address associated with the given sig
func (api *SignerAPI) EcRecover(ctx context.Context, contentType string, data, sig hexutil.Bytes) (common.Address, error) { func (api *SignerAPI) EcRecover(ctx context.Context, contentType string, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) {
mediaType, _, err := mime.ParseMediaType(contentType) mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil { if err != nil {
return common.Address{}, err return common.Address{}, err
} }
switch mediaType { switch mediaType {
case DataPlain.Mime: case TextPlain.Mime:
// Returns the address for the Account that was used to create the signature. // Returns the address for the Account that was used to create the signature.
// //
// Note, this function is compatible with eth_sign and personal_sign. As such it recovers // Note, this function is compatible with eth_sign and personal_sign. As such it recovers
// the address of: // the address of:
// hash = keccak256("\x19${byte version}Ethereum Signed Message:\n"${message length}${message}) // hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}")
// addr = ecrecover(hash, signature) // addr = ecrecover(hash, signature)
// //
// Note, the signature must conform to the secp256k1 curve R, S and V values, where // Note, the signature must conform to the secp256k1 curve R, S and V values, where
@ -706,7 +722,7 @@ func (api *SignerAPI) EcRecover(ctx context.Context, contentType string, data, s
return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)") return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)")
} }
sig[64] -= 27 // Transform yellow paper V from 27/28 to 0/1 sig[64] -= 27 // Transform yellow paper V from 27/28 to 0/1
hash, _ := SignDataPlain(data) hash, _ := signTextPlain(data)
rpk, err := crypto.SigToPub(hash, sig) rpk, err := crypto.SigToPub(hash, sig)
if err != nil { if err != nil {
return common.Address{}, err return common.Address{}, err

View file

@ -1,7 +1,7 @@
# Specs # Specs
`encode(domainSeparator : 𝔹²⁵⁶, message : 𝕊) = "\x19\x01" ‖ domainSeparator ‖ hashStruct(message)` `encode(domainSeparator : 𝔹²⁵⁶, message : 𝕊) = "\x19\x45" ‖ domainSeparator ‖ hashStruct(message)`
- data adheres to 𝕊, a structure defined in the rigorous eip-712 - data adheres to 𝕊, a structure defined in the rigorous eip-712
- `\x01` is needed to comply with EIP-191 - `\x45` is needed to comply with EIP-191
- `domainSeparator` and `hashStruct` are defined below - `domainSeparator` and `hashStruct` are defined below
## A) domainSeparator ## A) domainSeparator

View file

@ -25,32 +25,10 @@ type EIP712Domain struct {
// Typed data according to EIP712 // Typed data according to EIP712
// //
// If the format "\x19\x46" ‖ domainSeparator ‖ hashStruct(message)` is not respected, // hash = keccak256("\x19${byteVersion}${domainSeparator}${hashStruct(message)}")
// an error is returned func (api *SignerAPI) SignTypedData(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("addr", addr)
fmt.Println("data", data) //fmt.Println("data", data)
fmt.Println("data.PrimaryType", data.PrimaryType) fmt.Println("data.Domain", data.Domain)
return common.Hex2Bytes("0xdeadbeef"), nil return common.Hex2Bytes("0xdeadbeef"), nil
} }
// TypedData represents a request to create a new filter.
// Same as ethereum.FilterQuery but with UnmarshalJSON() method.
//type TypedData ethereum.TypedData
// 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
//}

View file

@ -70,11 +70,11 @@ func (l *AuditLogger) SignData(ctx context.Context, contentType string, addr com
return b, e return b, e
} }
func (l *AuditLogger) SignStructuredData(ctx context.Context, data TypedData) (hexutil.Bytes, error) { func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) {
l.log.Info("SignStructuredData", "type", "request", "metadata", MetadataFromContext(ctx).String(), l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
"addr", "data", data) "addr", addr.String(), "data", data)
b, e := l.api.SignStructuredData(ctx, data) b, e := l.api.SignTypedData(ctx, addr, data)
l.log.Info("SignStructuredData", "type", "response", "data", common.Bytes2Hex(b), "error", e) l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
return b, e return b, e
} }