Skip to content

Commit 572baae

Browse files
PaulRBergholiman
authored andcommitted
signer, clef: implement EIP191/712 (#17789)
* Named functions and defined a basic EIP191 content type list * Written basic content type functions * Added ecRecover method in the clef api * Updated the extapi changelog and addded indications in the README * Changed the version of the external API * Added tests for 0x45 * Implementing UnmarshalJSON() for TypedData * Working on TypedData * Solved the auditlog issue * Changed method to signTypedData * Changed mimes and implemented the 'encodeType' function for EIP-712 * Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible * Drafted recursive encodeData * Ran goimports and gofmt * Drafted first version of EIP-712, including tests * Temporarily switched to using common.Address in tests * Drafted text/validator and and rewritten []byte as hexutil.Bytes * Solved stringified address encoding issue * Changed the property type required by signData from bytes to interface{} * Fixed bugs in 'data/typed' signs * Brought legal warning back after temporarily disabling it for development * Added example RPC calls for account_signData and account_signTypedData * Named functions and defined a basic EIP191 content type list * Written basic content type functions * Added ecRecover method in the clef api * Updated the extapi changelog and addded indications in the README * Added tests for 0x45 * Implementing UnmarshalJSON() for TypedData * Working on TypedData * Solved the auditlog issue * Changed method to signTypedData * Changed mimes and implemented the 'encodeType' function for EIP-712 * Polished docstrings, ran goimports and swapped fmt.Errorf with errors.New where possible * Drafted recursive encodeData * Ran goimports and gofmt * Drafted first version of EIP-712, including tests * Temporarily switched to using common.Address in tests * Drafted text/validator and and rewritten []byte as hexutil.Bytes * Solved stringified address encoding issue * Changed the property type required by signData from bytes to interface{} * Fixed bugs in 'data/typed' signs * Brought legal warning back after temporarily disabling it for development * Added example RPC calls for account_signData and account_signTypedData * Polished and fixed PR * Polished and fixed PR * Solved malformed data panics and also wrote tests * Solved malformed data panics and also wrote tests * Added alphabetical sorting to type dependencies * Added alphabetical sorting to type dependencies * Added pretty print to data/typed UI * Added pretty print to data/typed UI * signer: more tests for typed data * signer: more tests for typed data * Fixed TestMalformedData4 errors and renamed IsValid to Validate * Fixed TestMalformedData4 errors and renamed IsValid to Validate * Fixed more new failing tests and deanonymised some functions * Fixed more new failing tests and deanonymised some functions * Added types to EIP712 output in cliui * Added types to EIP712 output in cliui * Fixed regexp issues * Fixed regexp issues * Added pseudo-failing test * Added pseudo-failing test * Fixed false positive test * Fixed false positive test * Added PrettyPrint method * Added PrettyPrint method * signer: refactor formatting and UI * signer: make ui use new message format for signing * Fixed breaking changes * Fixed rules_test failing test * Added extra regexp for reference types * signer: more hard types * Fixed failing test, formatted files * signer: use golang/x keccak * Fixed goimports error * clef, signer: address some review concerns * Implemented latest recommendations * Fixed comments and uintint256 issue * accounts, signer: fix mimetypes, add interface to sign data with passphrase * signer, accounts: remove duplicated code, pass hash preimages to signing * signer: prevent panic in type assertions, make cliui print rawdata as quotable-safe * signer: linter fixes, remove deprecated crypto dependency * accounts: fix goimport
1 parent 7c60d0a commit 572baae

19 files changed

Lines changed: 1969 additions & 169 deletions

File tree

‎accounts/accounts.go‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ type Account struct {
3535
URL URL `json:"url"` // Optional resource locator within a backend
3636
}
3737

38+
const (
39+
MimetypeTextWithValidator = "text/validator"
40+
MimetypeTypedData = "data/typed"
41+
MimetypeClique = "application/x-clique-header"
42+
MimetypeTextPlain = "text/plain"
43+
)
44+
3845
// Wallet represents a software or hardware wallet that might contain one or more
3946
// accounts (derived from the same seed).
4047
type Wallet interface {
@@ -101,6 +108,12 @@ type Wallet interface {
101108
// the account in a keystore).
102109
SignData(account Account, mimeType string, data []byte) ([]byte, error)
103110

111+
// SignDataWithPassphrase is identical to SignData, but also takes a password
112+
// NOTE: there's an chance that an erroneous call might mistake the two strings, and
113+
// supply password in the mimetype field, or vice versa. Thus, an implementation
114+
// should never echo the mimetype or return the mimetype in the error-response
115+
SignDataWithPassphrase(account Account, passphrase, mimeType string, data []byte) ([]byte, error)
116+
104117
// Signtext requests the wallet to sign the hash of a given piece of data, prefixed
105118
// by the Ethereum prefix scheme
106119
// It looks up the account specified either solely via its address contained within,
@@ -114,6 +127,9 @@ type Wallet interface {
114127
// the account in a keystore).
115128
SignText(account Account, text []byte) ([]byte, error)
116129

130+
// SignTextWithPassphrase is identical to Signtext, but also takes a password
131+
SignTextWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
132+
117133
// SignTx requests the wallet to sign the given transaction.
118134
//
119135
// It looks up the account specified either solely via its address contained within,
@@ -127,18 +143,7 @@ type Wallet interface {
127143
// the account in a keystore).
128144
SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
129145

130-
// SignTextWithPassphrase requests the wallet to sign the given text with the
131-
// given passphrase as extra authentication information.
132-
//
133-
// It looks up the account specified either solely via its address contained within,
134-
// or optionally with the aid of any location metadata from the embedded URL field.
135-
SignTextWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
136-
137-
// SignTxWithPassphrase requests the wallet to sign the given transaction, with the
138-
// given passphrase as extra authentication information.
139-
//
140-
// It looks up the account specified either solely via its address contained within,
141-
// or optionally with the aid of any location metadata from the embedded URL field.
146+
// SignTxWithPassphrase is identical to SignTx, but also takes a password
142147
SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
143148
}
144149

@@ -170,9 +175,22 @@ type Backend interface {
170175
//
171176
// This gives context to the signed message and prevents signing of transactions.
172177
func TextHash(data []byte) []byte {
173-
hash := sha3.NewLegacyKeccak256()
174-
fmt.Fprintf(hash, "\x19Ethereum Signed Message:\n%d%s", len(data), data)
175-
return hash.Sum(nil)
178+
hash, _ := TextAndHash(data)
179+
return hash
180+
}
181+
182+
// TextAndHash is a helper function that calculates a hash for the given message that can be
183+
// safely used to calculate a signature from.
184+
//
185+
// The hash is calulcated as
186+
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
187+
//
188+
// This gives context to the signed message and prevents signing of transactions.
189+
func TextAndHash(data []byte) ([]byte, string) {
190+
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), string(data))
191+
hasher := sha3.NewLegacyKeccak256()
192+
hasher.Write([]byte(msg))
193+
return hasher.Sum(nil), msg
176194
}
177195

178196
// WalletEventType represents the different event types that can be fired by

‎accounts/external/backend.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,14 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
184184
}
185185

186186
func (api *ExternalSigner) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
187-
return []byte{}, fmt.Errorf("operation not supported on external signers")
187+
return []byte{}, fmt.Errorf("passphrase-operations not supported on external signers")
188188
}
189189

190190
func (api *ExternalSigner) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
191-
return nil, fmt.Errorf("operation not supported on external signers")
191+
return nil, fmt.Errorf("passphrase-operations not supported on external signers")
192+
}
193+
func (api *ExternalSigner) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
194+
return nil, fmt.Errorf("passphrase-operations not supported on external signers")
192195
}
193196

194197
func (api *ExternalSigner) listAccounts() ([]common.Address, error) {
@@ -201,7 +204,7 @@ func (api *ExternalSigner) listAccounts() ([]common.Address, error) {
201204

202205
func (api *ExternalSigner) signCliqueBlock(a common.Address, rlpBlock hexutil.Bytes) (hexutil.Bytes, error) {
203206
var sig hexutil.Bytes
204-
if err := api.client.Call(&sig, "account_signData", "application/clique", a, rlpBlock); err != nil {
207+
if err := api.client.Call(&sig, "account_signData", core.ApplicationClique.Mime, a, rlpBlock); err != nil {
205208
return nil, err
206209
}
207210
if sig[64] != 27 && sig[64] != 28 {

‎accounts/keystore/wallet.go‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,18 @@ func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, dat
9797
return w.signHash(account, crypto.Keccak256(data))
9898
}
9999

100-
func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
101-
return w.signHash(account, accounts.TextHash(text))
102-
}
103-
104-
// SignTx implements accounts.Wallet, attempting to sign the given transaction
105-
// with the given account. If the wallet does not wrap this particular account,
106-
// an error is returned to avoid account leakage (even though in theory we may
107-
// be able to sign via our shared keystore backend).
108-
func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
100+
// SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed
101+
func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
109102
// Make sure the requested account is contained within
110103
if !w.Contains(account) {
111104
return nil, accounts.ErrUnknownAccount
112105
}
113106
// Account seems valid, request the keystore to sign
114-
return w.keystore.SignTx(account, tx, chainID)
107+
return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data))
108+
}
109+
110+
func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
111+
return w.signHash(account, accounts.TextHash(text))
115112
}
116113

117114
// SignHashWithPassphrase implements accounts.Wallet, attempting to sign the
@@ -125,6 +122,19 @@ func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passph
125122
return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text))
126123
}
127124

125+
// SignTx implements accounts.Wallet, attempting to sign the given transaction
126+
// with the given account. If the wallet does not wrap this particular account,
127+
// an error is returned to avoid account leakage (even though in theory we may
128+
// be able to sign via our shared keystore backend).
129+
func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
130+
// Make sure the requested account is contained within
131+
if !w.Contains(account) {
132+
return nil, accounts.ErrUnknownAccount
133+
}
134+
// Account seems valid, request the keystore to sign
135+
return w.keystore.SignTx(account, tx, chainID)
136+
}
137+
128138
// SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
129139
// transaction with the given account using passphrase as extra authentication.
130140
func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {

‎accounts/usbwallet/wallet.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ func (w *wallet) SignData(account accounts.Account, mimeType string, data []byte
507507
return w.signHash(account, crypto.Keccak256(data))
508508
}
509509

510+
// SignDataWithPassphrase implements accounts.Wallet, attempting to sign the given
511+
// data with the given account using passphrase as extra authentication.
512+
// Since USB wallets don't rely on passphrases, these are silently ignored.
513+
func (w *wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
514+
return w.SignData(account, mimeType, data)
515+
}
516+
510517
func (w *wallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
511518
return w.signHash(account, accounts.TextHash(text))
512519
}

‎cmd/clef/README.md‎

Lines changed: 120 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ None
189189
"method": "account_new",
190190
"params": []
191191
}
192-
192+
```
193+
Response
194+
```
193195
{
194196
"id": 0,
195197
"jsonrpc": "2.0",
@@ -222,7 +224,9 @@ None
222224
"jsonrpc": "2.0",
223225
"method": "account_list"
224226
}
225-
227+
```
228+
Response
229+
```
226230
{
227231
"id": 1,
228232
"jsonrpc": "2.0",
@@ -285,8 +289,8 @@ Response
285289

286290
```json
287291
{
292+
"id": 2,
288293
"jsonrpc": "2.0",
289-
"id": 67,
290294
"error": {
291295
"code": -32000,
292296
"message": "Request denied"
@@ -298,6 +302,7 @@ Response
298302

299303
```json
300304
{
305+
"id": 67,
301306
"jsonrpc": "2.0",
302307
"method": "account_signTransaction",
303308
"params": [
@@ -311,8 +316,7 @@ Response
311316
"data": "0x4401a6e40000000000000000000000000000000000000000000000000000000000000012"
312317
},
313318
"safeSend(address)"
314-
],
315-
"id": 67
319+
]
316320
}
317321
```
318322
Response
@@ -346,15 +350,18 @@ Bash example:
346350
{"jsonrpc":"2.0","id":67,"result":{"raw":"0xf88380018203339407a565b7ed7d7a678680a4c162885bedbb695fe080a44401a6e4000000000000000000000000000000000000000000000000000000000000001226a0223a7c9bcf5531c99be5ea7082183816eb20cfe0bbc322e97cc5c7f71ab8b20ea02aadee6b34b45bb15bc42d9c09de4a6754e7000908da72d48cc7704971491663","tx":{"nonce":"0x0","gasPrice":"0x1","gas":"0x333","to":"0x07a565b7ed7d7a678680a4c162885bedbb695fe0","value":"0x0","input":"0x4401a6e40000000000000000000000000000000000000000000000000000000000000012","v":"0x26","r":"0x223a7c9bcf5531c99be5ea7082183816eb20cfe0bbc322e97cc5c7f71ab8b20e","s":"0x2aadee6b34b45bb15bc42d9c09de4a6754e7000908da72d48cc7704971491663","hash":"0xeba2df809e7a612a0a0d444ccfa5c839624bdc00dd29e3340d46df3870f8a30e"}}}
347351
```
348352

349-
350-
### account_sign
353+
### account_signData
351354

352355
#### Sign data
353356
Signs a chunk of data and returns the calculated signature.
354357

355358
#### Arguments
359+
- content type [string]: type of signed data
360+
- `text/validator`: hex data with custom validator defined in a contract
361+
- `application/clique`: [clique](https://github.com/ethereum/EIPs/issues/225) headers
362+
- `text/plain`: simple hex data validated by `account_ecRecover`
356363
- account [address]: account to sign with
357-
- data [data]: data to sign
364+
- data [object]: data to sign
358365

359366
#### Result
360367
- calculated signature [data]
@@ -364,8 +371,9 @@ Bash example:
364371
{
365372
"id": 3,
366373
"jsonrpc": "2.0",
367-
"method": "account_sign",
374+
"method": "account_signData",
368375
"params": [
376+
"data/plain",
369377
"0x1923f626bb8dc025849e00f99c25fe2b2f7fb0db",
370378
"0xaabbccdd"
371379
]
@@ -381,11 +389,109 @@ Response
381389
}
382390
```
383391

392+
### account_signTypedData
393+
394+
#### Sign data
395+
Signs a chunk of structured data conformant to [EIP712]([EIP-712](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md)) and returns the calculated signature.
396+
397+
#### Arguments
398+
- account [address]: account to sign with
399+
- data [object]: data to sign
400+
401+
#### Result
402+
- calculated signature [data]
403+
404+
#### Sample call
405+
```json
406+
{
407+
"id": 68,
408+
"jsonrpc": "2.0",
409+
"method": "account_signTypedData",
410+
"params": [
411+
"0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826",
412+
{
413+
"types": {
414+
"EIP712Domain": [
415+
{
416+
"name": "name",
417+
"type": "string"
418+
},
419+
{
420+
"name": "version",
421+
"type": "string"
422+
},
423+
{
424+
"name": "chainId",
425+
"type": "uint256"
426+
},
427+
{
428+
"name": "verifyingContract",
429+
"type": "address"
430+
}
431+
],
432+
"Person": [
433+
{
434+
"name": "name",
435+
"type": "string"
436+
},
437+
{
438+
"name": "wallet",
439+
"type": "address"
440+
}
441+
],
442+
"Mail": [
443+
{
444+
"name": "from",
445+
"type": "Person"
446+
},
447+
{
448+
"name": "to",
449+
"type": "Person"
450+
},
451+
{
452+
"name": "contents",
453+
"type": "string"
454+
}
455+
]
456+
},
457+
"primaryType": "Mail",
458+
"domain": {
459+
"name": "Ether Mail",
460+
"version": "1",
461+
"chainId": 1,
462+
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
463+
},
464+
"message": {
465+
"from": {
466+
"name": "Cow",
467+
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
468+
},
469+
"to": {
470+
"name": "Bob",
471+
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
472+
},
473+
"contents": "Hello, Bob!"
474+
}
475+
}
476+
]
477+
}
478+
```
479+
Response
480+
481+
```json
482+
{
483+
"id": 1,
484+
"jsonrpc": "2.0",
485+
"result": "0x4355c47d63924e8a72e509b65029052eb6c299d53a04e167c5775fd466751c9d07299936d304c153f6443dfa05f40ff007d72911b6f72307f996231605b915621c"
486+
}
487+
```
488+
384489
### account_ecRecover
385490

386-
#### Recover address
387-
Derive the address from the account that was used to sign data from the data and signature.
388-
491+
#### Sign data
492+
493+
Derive the address from the account that was used to sign data with content type `text/plain` and the signature.
494+
389495
#### Arguments
390496
- data [data]: data that was signed
391497
- signature [data]: the signature to verify
@@ -400,6 +506,7 @@ Response
400506
"jsonrpc": "2.0",
401507
"method": "account_ecRecover",
402508
"params": [
509+
"data/plain",
403510
"0xaabbccdd",
404511
"0x5b6693f153b48ec1c706ba4169960386dbaa6903e249cc79a8e6ddc434451d417e1e57327872c7f538beeb323c300afa9999a3d4a5de6caf3be0d5ef832b67ef1c"
405512
]
@@ -413,7 +520,6 @@ Response
413520
"jsonrpc": "2.0",
414521
"result": "0x1923f626bb8dc025849e00f99c25fe2b2f7fb0db"
415522
}
416-
417523
```
418524

419525
### account_import
@@ -458,7 +564,7 @@ Response
458564
},
459565
"id": "09bccb61-b8d3-4e93-bf4f-205a8194f0b9",
460566
"version": 3
461-
},
567+
}
462568
]
463569
}
464570
```

0 commit comments

Comments
 (0)