signed_data.go 32 KB

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