signed_data.go 31 KB

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