signed_data.go 33 KB

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