signed_data.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package core
  18. import (
  19. "bytes"
  20. "context"
  21. "errors"
  22. "fmt"
  23. "math/big"
  24. "mime"
  25. "regexp"
  26. "sort"
  27. "strconv"
  28. "strings"
  29. "unicode"
  30. "github.com/ethereum/go-ethereum/accounts"
  31. "github.com/ethereum/go-ethereum/accounts/abi"
  32. "github.com/ethereum/go-ethereum/common"
  33. "github.com/ethereum/go-ethereum/common/hexutil"
  34. "github.com/ethereum/go-ethereum/common/math"
  35. "github.com/ethereum/go-ethereum/consensus/clique"
  36. "github.com/ethereum/go-ethereum/core/types"
  37. "github.com/ethereum/go-ethereum/crypto"
  38. "github.com/ethereum/go-ethereum/rlp"
  39. )
  40. type SigFormat struct {
  41. Mime string
  42. ByteVersion byte
  43. }
  44. var (
  45. TextValidator = SigFormat{
  46. accounts.MimetypeTextWithValidator,
  47. 0x00,
  48. }
  49. DataTyped = SigFormat{
  50. accounts.MimetypeTypedData,
  51. 0x01,
  52. }
  53. ApplicationClique = SigFormat{
  54. accounts.MimetypeClique,
  55. 0x02,
  56. }
  57. TextPlain = SigFormat{
  58. accounts.MimetypeTextPlain,
  59. 0x45,
  60. }
  61. )
  62. type ValidatorData struct {
  63. Address common.Address
  64. Message hexutil.Bytes
  65. }
  66. type TypedData struct {
  67. Types Types `json:"types"`
  68. PrimaryType string `json:"primaryType"`
  69. Domain TypedDataDomain `json:"domain"`
  70. Message TypedDataMessage `json:"message"`
  71. }
  72. type Type struct {
  73. Name string `json:"name"`
  74. Type string `json:"type"`
  75. }
  76. func (t *Type) isArray() bool {
  77. return strings.HasSuffix(t.Type, "[]")
  78. }
  79. // typeName returns the canonical name of the type. If the type is 'Person[]', then
  80. // this method returns 'Person'
  81. func (t *Type) typeName() string {
  82. if strings.HasSuffix(t.Type, "[]") {
  83. return strings.TrimSuffix(t.Type, "[]")
  84. }
  85. return t.Type
  86. }
  87. func (t *Type) isReferenceType() bool {
  88. // Reference types must have a leading uppercase characer
  89. return unicode.IsUpper([]rune(t.Type)[0])
  90. }
  91. type Types map[string][]Type
  92. type TypePriority struct {
  93. Type string
  94. Value uint
  95. }
  96. type TypedDataMessage = map[string]interface{}
  97. type TypedDataDomain struct {
  98. Name string `json:"name"`
  99. Version string `json:"version"`
  100. ChainId *big.Int `json:"chainId"`
  101. VerifyingContract string `json:"verifyingContract"`
  102. Salt string `json:"salt"`
  103. }
  104. var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Z](\w*)(\[\])?$`)
  105. // sign receives a request and produces a signature
  106. // Note, the produced signature conforms to the secp256k1 curve R, S and V values,
  107. // where the V value will be 27 or 28 for legacy reasons, if legacyV==true.
  108. func (api *SignerAPI) sign(addr common.MixedcaseAddress, req *SignDataRequest, legacyV bool) (hexutil.Bytes, error) {
  109. // We make the request prior to looking up if we actually have the account, to prevent
  110. // account-enumeration via the API
  111. res, err := api.UI.ApproveSignData(req)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if !res.Approved {
  116. return nil, ErrRequestDenied
  117. }
  118. // Look up the wallet containing the requested signer
  119. account := accounts.Account{Address: addr.Address()}
  120. wallet, err := api.am.Find(account)
  121. if err != nil {
  122. return nil, err
  123. }
  124. pw, err := api.lookupOrQueryPassword(account.Address,
  125. "Password for signing",
  126. fmt.Sprintf("Please enter password for signing data with account %s", account.Address.Hex()))
  127. if err != nil {
  128. return nil, err
  129. }
  130. // Sign the data with the wallet
  131. signature, err := wallet.SignDataWithPassphrase(account, pw, req.ContentType, req.Rawdata)
  132. if err != nil {
  133. return nil, err
  134. }
  135. if legacyV {
  136. signature[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
  137. }
  138. return signature, nil
  139. }
  140. // SignData signs the hash of the provided data, but does so differently
  141. // depending on the content-type specified.
  142. //
  143. // Different types of validation occur.
  144. func (api *SignerAPI) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) {
  145. var req, transformV, err = api.determineSignatureFormat(ctx, contentType, addr, data)
  146. if err != nil {
  147. return nil, err
  148. }
  149. signature, err := api.sign(addr, req, transformV)
  150. if err != nil {
  151. api.UI.ShowError(err.Error())
  152. return nil, err
  153. }
  154. return signature, nil
  155. }
  156. // determineSignatureFormat determines which signature method should be used based upon the mime type
  157. // In the cases where it matters ensure that the charset is handled. The charset
  158. // resides in the 'params' returned as the second returnvalue from mime.ParseMediaType
  159. // charset, ok := params["charset"]
  160. // As it is now, we accept any charset and just treat it as 'raw'.
  161. // This method returns the mimetype for signing along with the request
  162. func (api *SignerAPI) determineSignatureFormat(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (*SignDataRequest, bool, error) {
  163. var (
  164. req *SignDataRequest
  165. useEthereumV = true // Default to use V = 27 or 28, the legacy Ethereum format
  166. )
  167. mediaType, _, err := mime.ParseMediaType(contentType)
  168. if err != nil {
  169. return nil, useEthereumV, err
  170. }
  171. switch mediaType {
  172. case TextValidator.Mime:
  173. // Data with an intended validator
  174. validatorData, err := UnmarshalValidatorData(data)
  175. if err != nil {
  176. return nil, useEthereumV, err
  177. }
  178. sighash, msg := SignTextValidator(validatorData)
  179. message := []*NameValueType{
  180. {
  181. Name: "message",
  182. Typ: "text",
  183. Value: msg,
  184. },
  185. }
  186. req = &SignDataRequest{ContentType: mediaType, Rawdata: []byte(msg), Message: message, Hash: sighash}
  187. case ApplicationClique.Mime:
  188. // Clique is the Ethereum PoA standard
  189. stringData, ok := data.(string)
  190. if !ok {
  191. return nil, useEthereumV, fmt.Errorf("input for %v must be an hex-encoded string", ApplicationClique.Mime)
  192. }
  193. cliqueData, err := hexutil.Decode(stringData)
  194. if err != nil {
  195. return nil, useEthereumV, err
  196. }
  197. header := &types.Header{}
  198. if err := rlp.DecodeBytes(cliqueData, header); err != nil {
  199. return nil, useEthereumV, err
  200. }
  201. // The incoming clique header is already truncated, sent to us with a extradata already shortened
  202. if len(header.Extra) < 65 {
  203. // Need to add it back, to get a suitable length for hashing
  204. newExtra := make([]byte, len(header.Extra)+65)
  205. copy(newExtra, header.Extra)
  206. header.Extra = newExtra
  207. }
  208. // Get back the rlp data, encoded by us
  209. sighash, cliqueRlp, err := cliqueHeaderHashAndRlp(header)
  210. if err != nil {
  211. return nil, useEthereumV, err
  212. }
  213. message := []*NameValueType{
  214. {
  215. Name: "Clique header",
  216. Typ: "clique",
  217. Value: fmt.Sprintf("clique header %d [0x%x]", header.Number, header.Hash()),
  218. },
  219. }
  220. // Clique uses V on the form 0 or 1
  221. useEthereumV = false
  222. req = &SignDataRequest{ContentType: mediaType, Rawdata: cliqueRlp, Message: message, Hash: sighash}
  223. default: // also case TextPlain.Mime:
  224. // Calculates an Ethereum ECDSA signature for:
  225. // hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}")
  226. // We expect it to be a string
  227. if stringData, ok := data.(string); !ok {
  228. return nil, useEthereumV, fmt.Errorf("input for text/plain must be an hex-encoded string")
  229. } else {
  230. if textData, err := hexutil.Decode(stringData); err != nil {
  231. return nil, useEthereumV, err
  232. } else {
  233. sighash, msg := accounts.TextAndHash(textData)
  234. message := []*NameValueType{
  235. {
  236. Name: "message",
  237. Typ: accounts.MimetypeTextPlain,
  238. Value: msg,
  239. },
  240. }
  241. req = &SignDataRequest{ContentType: mediaType, Rawdata: []byte(msg), Message: message, Hash: sighash}
  242. }
  243. }
  244. }
  245. req.Address = addr
  246. req.Meta = MetadataFromContext(ctx)
  247. return req, useEthereumV, nil
  248. }
  249. // SignTextWithValidator signs the given message which can be further recovered
  250. // with the given validator.
  251. // hash = keccak256("\x19\x00"${address}${data}).
  252. func SignTextValidator(validatorData ValidatorData) (hexutil.Bytes, string) {
  253. msg := fmt.Sprintf("\x19\x00%s%s", string(validatorData.Address.Bytes()), string(validatorData.Message))
  254. fmt.Printf("SignTextValidator:%s\n", msg)
  255. return crypto.Keccak256([]byte(msg)), msg
  256. }
  257. // cliqueHeaderHashAndRlp returns the hash which is used as input for the proof-of-authority
  258. // signing. It is the hash of the entire header apart from the 65 byte signature
  259. // contained at the end of the extra data.
  260. //
  261. // The method requires the extra data to be at least 65 bytes -- the original implementation
  262. // in clique.go panics if this is the case, thus it's been reimplemented here to avoid the panic
  263. // and simply return an error instead
  264. func cliqueHeaderHashAndRlp(header *types.Header) (hash, rlp []byte, err error) {
  265. if len(header.Extra) < 65 {
  266. err = fmt.Errorf("clique header extradata too short, %d < 65", len(header.Extra))
  267. return
  268. }
  269. rlp = clique.CliqueRLP(header)
  270. hash = clique.SealHash(header).Bytes()
  271. return hash, rlp, err
  272. }
  273. // SignTypedData signs EIP-712 conformant typed data
  274. // hash = keccak256("\x19${byteVersion}${domainSeparator}${hashStruct(message)}")
  275. func (api *SignerAPI) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, typedData TypedData) (hexutil.Bytes, error) {
  276. domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
  277. if err != nil {
  278. return nil, err
  279. }
  280. typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
  281. if err != nil {
  282. return nil, err
  283. }
  284. rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
  285. sighash := crypto.Keccak256(rawData)
  286. message := typedData.Format()
  287. req := &SignDataRequest{ContentType: DataTyped.Mime, Rawdata: rawData, Message: message, Hash: sighash}
  288. signature, err := api.sign(addr, req, true)
  289. if err != nil {
  290. api.UI.ShowError(err.Error())
  291. return nil, err
  292. }
  293. return signature, nil
  294. }
  295. // HashStruct generates a keccak256 hash of the encoding of the provided data
  296. func (typedData *TypedData) HashStruct(primaryType string, data TypedDataMessage) (hexutil.Bytes, error) {
  297. encodedData, err := typedData.EncodeData(primaryType, data, 1)
  298. if err != nil {
  299. return nil, err
  300. }
  301. return crypto.Keccak256(encodedData), nil
  302. }
  303. // Dependencies returns an array of custom types ordered by their hierarchical reference tree
  304. func (typedData *TypedData) Dependencies(primaryType string, found []string) []string {
  305. includes := func(arr []string, str string) bool {
  306. for _, obj := range arr {
  307. if obj == str {
  308. return true
  309. }
  310. }
  311. return false
  312. }
  313. if includes(found, primaryType) {
  314. return found
  315. }
  316. if typedData.Types[primaryType] == nil {
  317. return found
  318. }
  319. found = append(found, primaryType)
  320. for _, field := range typedData.Types[primaryType] {
  321. for _, dep := range typedData.Dependencies(field.Type, found) {
  322. if !includes(found, dep) {
  323. found = append(found, dep)
  324. }
  325. }
  326. }
  327. return found
  328. }
  329. // EncodeType generates the following encoding:
  330. // `name ‖ "(" ‖ member₁ ‖ "," ‖ member₂ ‖ "," ‖ … ‖ memberₙ ")"`
  331. //
  332. // each member is written as `type ‖ " " ‖ name` encodings cascade down and are sorted by name
  333. func (typedData *TypedData) EncodeType(primaryType string) hexutil.Bytes {
  334. // Get dependencies primary first, then alphabetical
  335. deps := typedData.Dependencies(primaryType, []string{})
  336. slicedDeps := deps[1:]
  337. sort.Strings(slicedDeps)
  338. deps = append([]string{primaryType}, slicedDeps...)
  339. // Format as a string with fields
  340. var buffer bytes.Buffer
  341. for _, dep := range deps {
  342. buffer.WriteString(dep)
  343. buffer.WriteString("(")
  344. for _, obj := range typedData.Types[dep] {
  345. buffer.WriteString(obj.Type)
  346. buffer.WriteString(" ")
  347. buffer.WriteString(obj.Name)
  348. buffer.WriteString(",")
  349. }
  350. buffer.Truncate(buffer.Len() - 1)
  351. buffer.WriteString(")")
  352. }
  353. return buffer.Bytes()
  354. }
  355. // TypeHash creates the keccak256 hash of the data
  356. func (typedData *TypedData) TypeHash(primaryType string) hexutil.Bytes {
  357. return crypto.Keccak256(typedData.EncodeType(primaryType))
  358. }
  359. // EncodeData generates the following encoding:
  360. // `enc(value₁) ‖ enc(value₂) ‖ … ‖ enc(valueₙ)`
  361. //
  362. // each encoded member is 32-byte long
  363. func (typedData *TypedData) EncodeData(primaryType string, data map[string]interface{}, depth int) (hexutil.Bytes, error) {
  364. if err := typedData.validate(); err != nil {
  365. return nil, err
  366. }
  367. buffer := bytes.Buffer{}
  368. // Verify extra data
  369. if len(typedData.Types[primaryType]) < len(data) {
  370. return nil, errors.New("there is extra data provided in the message")
  371. }
  372. // Add typehash
  373. buffer.Write(typedData.TypeHash(primaryType))
  374. // Add field contents. Structs and arrays have special handlers.
  375. for _, field := range typedData.Types[primaryType] {
  376. encType := field.Type
  377. encValue := data[field.Name]
  378. if encType[len(encType)-1:] == "]" {
  379. arrayValue, ok := encValue.([]interface{})
  380. if !ok {
  381. return nil, dataMismatchError(encType, encValue)
  382. }
  383. arrayBuffer := bytes.Buffer{}
  384. parsedType := strings.Split(encType, "[")[0]
  385. for _, item := range arrayValue {
  386. if typedData.Types[parsedType] != nil {
  387. mapValue, ok := item.(map[string]interface{})
  388. if !ok {
  389. return nil, dataMismatchError(parsedType, item)
  390. }
  391. encodedData, err := typedData.EncodeData(parsedType, mapValue, depth+1)
  392. if err != nil {
  393. return nil, err
  394. }
  395. arrayBuffer.Write(encodedData)
  396. } else {
  397. bytesValue, err := typedData.EncodePrimitiveValue(parsedType, item, depth)
  398. if err != nil {
  399. return nil, err
  400. }
  401. arrayBuffer.Write(bytesValue)
  402. }
  403. }
  404. buffer.Write(crypto.Keccak256(arrayBuffer.Bytes()))
  405. } else if typedData.Types[field.Type] != nil {
  406. mapValue, ok := encValue.(map[string]interface{})
  407. if !ok {
  408. return nil, dataMismatchError(encType, encValue)
  409. }
  410. encodedData, err := typedData.EncodeData(field.Type, mapValue, depth+1)
  411. if err != nil {
  412. return nil, err
  413. }
  414. buffer.Write(crypto.Keccak256(encodedData))
  415. } else {
  416. byteValue, err := typedData.EncodePrimitiveValue(encType, encValue, depth)
  417. if err != nil {
  418. return nil, err
  419. }
  420. buffer.Write(byteValue)
  421. }
  422. }
  423. return buffer.Bytes(), nil
  424. }
  425. // EncodePrimitiveValue deals with the primitive values found
  426. // while searching through the typed data
  427. func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interface{}, depth int) ([]byte, error) {
  428. switch encType {
  429. case "address":
  430. stringValue, ok := encValue.(string)
  431. if !ok || !common.IsHexAddress(stringValue) {
  432. return nil, dataMismatchError(encType, encValue)
  433. }
  434. retval := make([]byte, 32)
  435. copy(retval[12:], common.HexToAddress(stringValue).Bytes())
  436. return retval, nil
  437. case "bool":
  438. boolValue, ok := encValue.(bool)
  439. if !ok {
  440. return nil, dataMismatchError(encType, encValue)
  441. }
  442. if boolValue {
  443. return math.PaddedBigBytes(common.Big1, 32), nil
  444. }
  445. return math.PaddedBigBytes(common.Big0, 32), nil
  446. case "string":
  447. strVal, ok := encValue.(string)
  448. if !ok {
  449. return nil, dataMismatchError(encType, encValue)
  450. }
  451. return crypto.Keccak256([]byte(strVal)), nil
  452. case "bytes":
  453. bytesValue, ok := encValue.([]byte)
  454. if !ok {
  455. return nil, dataMismatchError(encType, encValue)
  456. }
  457. return crypto.Keccak256(bytesValue), nil
  458. }
  459. if strings.HasPrefix(encType, "bytes") {
  460. lengthStr := strings.TrimPrefix(encType, "bytes")
  461. length, err := strconv.Atoi(lengthStr)
  462. if err != nil {
  463. return nil, fmt.Errorf("invalid size on bytes: %v", lengthStr)
  464. }
  465. if length < 0 || length > 32 {
  466. return nil, fmt.Errorf("invalid size on bytes: %d", length)
  467. }
  468. if byteValue, ok := encValue.(hexutil.Bytes); !ok {
  469. return nil, dataMismatchError(encType, encValue)
  470. } else {
  471. return math.PaddedBigBytes(new(big.Int).SetBytes(byteValue), 32), nil
  472. }
  473. }
  474. if strings.HasPrefix(encType, "int") || strings.HasPrefix(encType, "uint") {
  475. length := 0
  476. if encType == "int" || encType == "uint" {
  477. length = 256
  478. } else {
  479. lengthStr := ""
  480. if strings.HasPrefix(encType, "uint") {
  481. lengthStr = strings.TrimPrefix(encType, "uint")
  482. } else {
  483. lengthStr = strings.TrimPrefix(encType, "int")
  484. }
  485. atoiSize, err := strconv.Atoi(lengthStr)
  486. if err != nil {
  487. return nil, fmt.Errorf("invalid size on integer: %v", lengthStr)
  488. }
  489. length = atoiSize
  490. }
  491. bigIntValue, ok := encValue.(*big.Int)
  492. if bigIntValue.BitLen() > length {
  493. return nil, fmt.Errorf("integer larger than '%v'", encType)
  494. }
  495. if !ok {
  496. return nil, dataMismatchError(encType, encValue)
  497. }
  498. return abi.U256(bigIntValue), nil
  499. }
  500. return nil, fmt.Errorf("unrecognized type '%s'", encType)
  501. }
  502. // dataMismatchError generates an error for a mismatch between
  503. // the provided type and data
  504. func dataMismatchError(encType string, encValue interface{}) error {
  505. return fmt.Errorf("provided data '%v' doesn't match type '%s'", encValue, encType)
  506. }
  507. // EcRecover recovers the address associated with the given sig.
  508. // Only compatible with `text/plain`
  509. func (api *SignerAPI) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) {
  510. // Returns the address for the Account that was used to create the signature.
  511. //
  512. // Note, this function is compatible with eth_sign and personal_sign. As such it recovers
  513. // the address of:
  514. // hash = keccak256("\x19${byteVersion}Ethereum Signed Message:\n${message length}${message}")
  515. // addr = ecrecover(hash, signature)
  516. //
  517. // Note, the signature must conform to the secp256k1 curve R, S and V values, where
  518. // the V value must be be 27 or 28 for legacy reasons.
  519. //
  520. // https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_ecRecover
  521. if len(sig) != 65 {
  522. return common.Address{}, fmt.Errorf("signature must be 65 bytes long")
  523. }
  524. if sig[64] != 27 && sig[64] != 28 {
  525. return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)")
  526. }
  527. sig[64] -= 27 // Transform yellow paper V from 27/28 to 0/1
  528. hash := accounts.TextHash(data)
  529. rpk, err := crypto.SigToPub(hash, sig)
  530. if err != nil {
  531. return common.Address{}, err
  532. }
  533. return crypto.PubkeyToAddress(*rpk), nil
  534. }
  535. // UnmarshalValidatorData converts the bytes input to typed data
  536. func UnmarshalValidatorData(data interface{}) (ValidatorData, error) {
  537. raw, ok := data.(map[string]interface{})
  538. if !ok {
  539. return ValidatorData{}, errors.New("validator input is not a map[string]interface{}")
  540. }
  541. addr, ok := raw["address"].(string)
  542. if !ok {
  543. return ValidatorData{}, errors.New("validator address is not sent as a string")
  544. }
  545. addrBytes, err := hexutil.Decode(addr)
  546. if err != nil {
  547. return ValidatorData{}, err
  548. }
  549. if !ok || len(addrBytes) == 0 {
  550. return ValidatorData{}, errors.New("validator address is undefined")
  551. }
  552. message, ok := raw["message"].(string)
  553. if !ok {
  554. return ValidatorData{}, errors.New("message is not sent as a string")
  555. }
  556. messageBytes, err := hexutil.Decode(message)
  557. if err != nil {
  558. return ValidatorData{}, err
  559. }
  560. if !ok || len(messageBytes) == 0 {
  561. return ValidatorData{}, errors.New("message is undefined")
  562. }
  563. return ValidatorData{
  564. Address: common.BytesToAddress(addrBytes),
  565. Message: messageBytes,
  566. }, nil
  567. }
  568. // validate makes sure the types are sound
  569. func (typedData *TypedData) validate() error {
  570. if err := typedData.Types.validate(); err != nil {
  571. return err
  572. }
  573. if err := typedData.Domain.validate(); err != nil {
  574. return err
  575. }
  576. return nil
  577. }
  578. // Map generates a map version of the typed data
  579. func (typedData *TypedData) Map() map[string]interface{} {
  580. dataMap := map[string]interface{}{
  581. "types": typedData.Types,
  582. "domain": typedData.Domain.Map(),
  583. "primaryType": typedData.PrimaryType,
  584. "message": typedData.Message,
  585. }
  586. return dataMap
  587. }
  588. // PrettyPrint generates a nice output to help the users
  589. // of clef present data in their apps
  590. func (typedData *TypedData) PrettyPrint() string {
  591. output := bytes.Buffer{}
  592. formatted := typedData.Format()
  593. for _, item := range formatted {
  594. output.WriteString(fmt.Sprintf("%v\n", item.Pprint(0)))
  595. }
  596. return output.String()
  597. }
  598. // Format returns a representation of typedData, which can be easily displayed by a user-interface
  599. // without in-depth knowledge about 712 rules
  600. func (typedData *TypedData) Format() []*NameValueType {
  601. var nvts []*NameValueType
  602. nvts = append(nvts, &NameValueType{
  603. Name: "EIP712Domain",
  604. Value: typedData.formatData("EIP712Domain", typedData.Domain.Map()),
  605. Typ: "domain",
  606. })
  607. nvts = append(nvts, &NameValueType{
  608. Name: typedData.PrimaryType,
  609. Value: typedData.formatData(typedData.PrimaryType, typedData.Message),
  610. Typ: "primary type",
  611. })
  612. return nvts
  613. }
  614. func (typedData *TypedData) formatData(primaryType string, data map[string]interface{}) []*NameValueType {
  615. var output []*NameValueType
  616. // Add field contents. Structs and arrays have special handlers.
  617. for _, field := range typedData.Types[primaryType] {
  618. encName := field.Name
  619. encValue := data[encName]
  620. item := &NameValueType{
  621. Name: encName,
  622. Typ: field.Type,
  623. }
  624. if field.isArray() {
  625. arrayValue, _ := encValue.([]interface{})
  626. parsedType := field.typeName()
  627. for _, v := range arrayValue {
  628. if typedData.Types[parsedType] != nil {
  629. mapValue, _ := v.(map[string]interface{})
  630. mapOutput := typedData.formatData(parsedType, mapValue)
  631. item.Value = mapOutput
  632. } else {
  633. primitiveOutput := formatPrimitiveValue(field.Type, encValue)
  634. item.Value = primitiveOutput
  635. }
  636. }
  637. } else if typedData.Types[field.Type] != nil {
  638. mapValue, _ := encValue.(map[string]interface{})
  639. mapOutput := typedData.formatData(field.Type, mapValue)
  640. item.Value = mapOutput
  641. } else {
  642. primitiveOutput := formatPrimitiveValue(field.Type, encValue)
  643. item.Value = primitiveOutput
  644. }
  645. output = append(output, item)
  646. }
  647. return output
  648. }
  649. func formatPrimitiveValue(encType string, encValue interface{}) string {
  650. switch encType {
  651. case "address":
  652. stringValue, _ := encValue.(string)
  653. return common.HexToAddress(stringValue).String()
  654. case "bool":
  655. boolValue, _ := encValue.(bool)
  656. return fmt.Sprintf("%t", boolValue)
  657. case "bytes", "string":
  658. return fmt.Sprintf("%s", encValue)
  659. }
  660. if strings.HasPrefix(encType, "bytes") {
  661. return fmt.Sprintf("%s", encValue)
  662. } else if strings.HasPrefix(encType, "uint") || strings.HasPrefix(encType, "int") {
  663. bigIntValue, _ := encValue.(*big.Int)
  664. return fmt.Sprintf("%d (0x%x)", bigIntValue, bigIntValue)
  665. }
  666. return "NA"
  667. }
  668. // NameValueType is a very simple struct with Name, Value and Type. It's meant for simple
  669. // json structures used to communicate signing-info about typed data with the UI
  670. type NameValueType struct {
  671. Name string `json:"name"`
  672. Value interface{} `json:"value"`
  673. Typ string `json:"type"`
  674. }
  675. // Pprint returns a pretty-printed version of nvt
  676. func (nvt *NameValueType) Pprint(depth int) string {
  677. output := bytes.Buffer{}
  678. output.WriteString(strings.Repeat("\u00a0", depth*2))
  679. output.WriteString(fmt.Sprintf("%s [%s]: ", nvt.Name, nvt.Typ))
  680. if nvts, ok := nvt.Value.([]*NameValueType); ok {
  681. output.WriteString("\n")
  682. for _, next := range nvts {
  683. sublevel := next.Pprint(depth + 1)
  684. output.WriteString(sublevel)
  685. }
  686. } else {
  687. output.WriteString(fmt.Sprintf("%q\n", nvt.Value))
  688. }
  689. return output.String()
  690. }
  691. // Validate checks if the types object is conformant to the specs
  692. func (t Types) validate() error {
  693. for typeKey, typeArr := range t {
  694. for _, typeObj := range typeArr {
  695. if typeKey == typeObj.Type {
  696. return fmt.Errorf("type '%s' cannot reference itself", typeObj.Type)
  697. }
  698. if typeObj.isReferenceType() {
  699. if _, exist := t[typeObj.Type]; !exist {
  700. return fmt.Errorf("reference type '%s' is undefined", typeObj.Type)
  701. }
  702. if !typedDataReferenceTypeRegexp.MatchString(typeObj.Type) {
  703. return fmt.Errorf("unknown reference type '%s", typeObj.Type)
  704. }
  705. } else if !isPrimitiveTypeValid(typeObj.Type) {
  706. return fmt.Errorf("unknown type '%s'", typeObj.Type)
  707. }
  708. }
  709. }
  710. return nil
  711. }
  712. // Checks if the primitive value is valid
  713. func isPrimitiveTypeValid(primitiveType string) bool {
  714. if primitiveType == "address" ||
  715. primitiveType == "address[]" ||
  716. primitiveType == "bool" ||
  717. primitiveType == "bool[]" ||
  718. primitiveType == "string" ||
  719. primitiveType == "string[]" {
  720. return true
  721. }
  722. if primitiveType == "bytes" ||
  723. primitiveType == "bytes[]" ||
  724. primitiveType == "bytes1" ||
  725. primitiveType == "bytes1[]" ||
  726. primitiveType == "bytes2" ||
  727. primitiveType == "bytes2[]" ||
  728. primitiveType == "bytes3" ||
  729. primitiveType == "bytes3[]" ||
  730. primitiveType == "bytes4" ||
  731. primitiveType == "bytes4[]" ||
  732. primitiveType == "bytes5" ||
  733. primitiveType == "bytes5[]" ||
  734. primitiveType == "bytes6" ||
  735. primitiveType == "bytes6[]" ||
  736. primitiveType == "bytes7" ||
  737. primitiveType == "bytes7[]" ||
  738. primitiveType == "bytes8" ||
  739. primitiveType == "bytes8[]" ||
  740. primitiveType == "bytes9" ||
  741. primitiveType == "bytes9[]" ||
  742. primitiveType == "bytes10" ||
  743. primitiveType == "bytes10[]" ||
  744. primitiveType == "bytes11" ||
  745. primitiveType == "bytes11[]" ||
  746. primitiveType == "bytes12" ||
  747. primitiveType == "bytes12[]" ||
  748. primitiveType == "bytes13" ||
  749. primitiveType == "bytes13[]" ||
  750. primitiveType == "bytes14" ||
  751. primitiveType == "bytes14[]" ||
  752. primitiveType == "bytes15" ||
  753. primitiveType == "bytes15[]" ||
  754. primitiveType == "bytes16" ||
  755. primitiveType == "bytes16[]" ||
  756. primitiveType == "bytes17" ||
  757. primitiveType == "bytes17[]" ||
  758. primitiveType == "bytes18" ||
  759. primitiveType == "bytes18[]" ||
  760. primitiveType == "bytes19" ||
  761. primitiveType == "bytes19[]" ||
  762. primitiveType == "bytes20" ||
  763. primitiveType == "bytes20[]" ||
  764. primitiveType == "bytes21" ||
  765. primitiveType == "bytes21[]" ||
  766. primitiveType == "bytes22" ||
  767. primitiveType == "bytes22[]" ||
  768. primitiveType == "bytes23" ||
  769. primitiveType == "bytes23[]" ||
  770. primitiveType == "bytes24" ||
  771. primitiveType == "bytes24[]" ||
  772. primitiveType == "bytes25" ||
  773. primitiveType == "bytes25[]" ||
  774. primitiveType == "bytes26" ||
  775. primitiveType == "bytes26[]" ||
  776. primitiveType == "bytes27" ||
  777. primitiveType == "bytes27[]" ||
  778. primitiveType == "bytes28" ||
  779. primitiveType == "bytes28[]" ||
  780. primitiveType == "bytes29" ||
  781. primitiveType == "bytes29[]" ||
  782. primitiveType == "bytes30" ||
  783. primitiveType == "bytes30[]" ||
  784. primitiveType == "bytes31" ||
  785. primitiveType == "bytes31[]" {
  786. return true
  787. }
  788. if primitiveType == "int" ||
  789. primitiveType == "int[]" ||
  790. primitiveType == "int8" ||
  791. primitiveType == "int8[]" ||
  792. primitiveType == "int16" ||
  793. primitiveType == "int16[]" ||
  794. primitiveType == "int32" ||
  795. primitiveType == "int32[]" ||
  796. primitiveType == "int64" ||
  797. primitiveType == "int64[]" ||
  798. primitiveType == "int128" ||
  799. primitiveType == "int128[]" ||
  800. primitiveType == "int256" ||
  801. primitiveType == "int256[]" {
  802. return true
  803. }
  804. if primitiveType == "uint" ||
  805. primitiveType == "uint[]" ||
  806. primitiveType == "uint8" ||
  807. primitiveType == "uint8[]" ||
  808. primitiveType == "uint16" ||
  809. primitiveType == "uint16[]" ||
  810. primitiveType == "uint32" ||
  811. primitiveType == "uint32[]" ||
  812. primitiveType == "uint64" ||
  813. primitiveType == "uint64[]" ||
  814. primitiveType == "uint128" ||
  815. primitiveType == "uint128[]" ||
  816. primitiveType == "uint256" ||
  817. primitiveType == "uint256[]" {
  818. return true
  819. }
  820. return false
  821. }
  822. // validate checks if the given domain is valid, i.e. contains at least
  823. // the minimum viable keys and values
  824. func (domain *TypedDataDomain) validate() error {
  825. if domain.ChainId == big.NewInt(0) {
  826. return errors.New("chainId must be specified according to EIP-155")
  827. }
  828. if len(domain.Name) == 0 && len(domain.Version) == 0 && len(domain.VerifyingContract) == 0 && len(domain.Salt) == 0 {
  829. return errors.New("domain is undefined")
  830. }
  831. return nil
  832. }
  833. // Map is a helper function to generate a map version of the domain
  834. func (domain *TypedDataDomain) Map() map[string]interface{} {
  835. dataMap := map[string]interface{}{
  836. "chainId": domain.ChainId,
  837. }
  838. if len(domain.Name) > 0 {
  839. dataMap["name"] = domain.Name
  840. }
  841. if len(domain.Version) > 0 {
  842. dataMap["version"] = domain.Version
  843. }
  844. if len(domain.VerifyingContract) > 0 {
  845. dataMap["verifyingContract"] = domain.VerifyingContract
  846. }
  847. if len(domain.Salt) > 0 {
  848. dataMap["salt"] = domain.Salt
  849. }
  850. return dataMap
  851. }