signed_data_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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_test
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/json"
  21. "fmt"
  22. "os"
  23. "path"
  24. "strings"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/accounts/keystore"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/common/hexutil"
  29. "github.com/ethereum/go-ethereum/common/math"
  30. "github.com/ethereum/go-ethereum/crypto"
  31. "github.com/ethereum/go-ethereum/signer/core"
  32. "github.com/ethereum/go-ethereum/signer/core/apitypes"
  33. )
  34. var typesStandard = apitypes.Types{
  35. "EIP712Domain": {
  36. {
  37. Name: "name",
  38. Type: "string",
  39. },
  40. {
  41. Name: "version",
  42. Type: "string",
  43. },
  44. {
  45. Name: "chainId",
  46. Type: "uint256",
  47. },
  48. {
  49. Name: "verifyingContract",
  50. Type: "address",
  51. },
  52. },
  53. "Person": {
  54. {
  55. Name: "name",
  56. Type: "string",
  57. },
  58. {
  59. Name: "wallet",
  60. Type: "address",
  61. },
  62. },
  63. "Mail": {
  64. {
  65. Name: "from",
  66. Type: "Person",
  67. },
  68. {
  69. Name: "to",
  70. Type: "Person",
  71. },
  72. {
  73. Name: "contents",
  74. Type: "string",
  75. },
  76. },
  77. }
  78. var jsonTypedData = `
  79. {
  80. "types": {
  81. "EIP712Domain": [
  82. {
  83. "name": "name",
  84. "type": "string"
  85. },
  86. {
  87. "name": "version",
  88. "type": "string"
  89. },
  90. {
  91. "name": "chainId",
  92. "type": "uint256"
  93. },
  94. {
  95. "name": "verifyingContract",
  96. "type": "address"
  97. }
  98. ],
  99. "Person": [
  100. {
  101. "name": "name",
  102. "type": "string"
  103. },
  104. {
  105. "name": "test",
  106. "type": "uint8"
  107. },
  108. {
  109. "name": "wallet",
  110. "type": "address"
  111. }
  112. ],
  113. "Mail": [
  114. {
  115. "name": "from",
  116. "type": "Person"
  117. },
  118. {
  119. "name": "to",
  120. "type": "Person"
  121. },
  122. {
  123. "name": "contents",
  124. "type": "string"
  125. }
  126. ]
  127. },
  128. "primaryType": "Mail",
  129. "domain": {
  130. "name": "Ether Mail",
  131. "version": "1",
  132. "chainId": "1",
  133. "verifyingContract": "0xCCCcccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
  134. },
  135. "message": {
  136. "from": {
  137. "name": "Cow",
  138. "test": 3,
  139. "wallet": "0xcD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
  140. },
  141. "to": {
  142. "name": "Bob",
  143. "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
  144. },
  145. "contents": "Hello, Bob!"
  146. }
  147. }
  148. `
  149. const primaryType = "Mail"
  150. var domainStandard = apitypes.TypedDataDomain{
  151. Name: "Ether Mail",
  152. Version: "1",
  153. ChainId: math.NewHexOrDecimal256(1),
  154. VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
  155. Salt: "",
  156. }
  157. var messageStandard = map[string]interface{}{
  158. "from": map[string]interface{}{
  159. "name": "Cow",
  160. "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
  161. },
  162. "to": map[string]interface{}{
  163. "name": "Bob",
  164. "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
  165. },
  166. "contents": "Hello, Bob!",
  167. }
  168. var typedData = apitypes.TypedData{
  169. Types: typesStandard,
  170. PrimaryType: primaryType,
  171. Domain: domainStandard,
  172. Message: messageStandard,
  173. }
  174. func TestSignData(t *testing.T) {
  175. api, control := setup(t)
  176. //Create two accounts
  177. createAccount(control, api, t)
  178. createAccount(control, api, t)
  179. control.approveCh <- "1"
  180. list, err := api.List(context.Background())
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. a := common.NewMixedcaseAddress(list[0])
  185. control.approveCh <- "Y"
  186. control.inputCh <- "wrongpassword"
  187. signature, err := api.SignData(context.Background(), apitypes.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world")))
  188. if signature != nil {
  189. t.Errorf("Expected nil-data, got %x", signature)
  190. }
  191. if err != keystore.ErrDecrypt {
  192. t.Errorf("Expected ErrLocked! '%v'", err)
  193. }
  194. control.approveCh <- "No way"
  195. signature, err = api.SignData(context.Background(), apitypes.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world")))
  196. if signature != nil {
  197. t.Errorf("Expected nil-data, got %x", signature)
  198. }
  199. if err != core.ErrRequestDenied {
  200. t.Errorf("Expected ErrRequestDenied! '%v'", err)
  201. }
  202. // text/plain
  203. control.approveCh <- "Y"
  204. control.inputCh <- "a_long_password"
  205. signature, err = api.SignData(context.Background(), apitypes.TextPlain.Mime, a, hexutil.Encode([]byte("EHLO world")))
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. if signature == nil || len(signature) != 65 {
  210. t.Errorf("Expected 65 byte signature (got %d bytes)", len(signature))
  211. }
  212. // data/typed
  213. control.approveCh <- "Y"
  214. control.inputCh <- "a_long_password"
  215. signature, err = api.SignTypedData(context.Background(), a, typedData)
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. if signature == nil || len(signature) != 65 {
  220. t.Errorf("Expected 65 byte signature (got %d bytes)", len(signature))
  221. }
  222. }
  223. func TestDomainChainId(t *testing.T) {
  224. withoutChainID := apitypes.TypedData{
  225. Types: apitypes.Types{
  226. "EIP712Domain": []apitypes.Type{
  227. {Name: "name", Type: "string"},
  228. },
  229. },
  230. Domain: apitypes.TypedDataDomain{
  231. Name: "test",
  232. },
  233. }
  234. if _, ok := withoutChainID.Domain.Map()["chainId"]; ok {
  235. t.Errorf("Expected the chainId key to not be present in the domain map")
  236. }
  237. // should encode successfully
  238. if _, err := withoutChainID.HashStruct("EIP712Domain", withoutChainID.Domain.Map()); err != nil {
  239. t.Errorf("Expected the typedData to encode the domain successfully, got %v", err)
  240. }
  241. withChainID := apitypes.TypedData{
  242. Types: apitypes.Types{
  243. "EIP712Domain": []apitypes.Type{
  244. {Name: "name", Type: "string"},
  245. {Name: "chainId", Type: "uint256"},
  246. },
  247. },
  248. Domain: apitypes.TypedDataDomain{
  249. Name: "test",
  250. ChainId: math.NewHexOrDecimal256(1),
  251. },
  252. }
  253. if _, ok := withChainID.Domain.Map()["chainId"]; !ok {
  254. t.Errorf("Expected the chainId key be present in the domain map")
  255. }
  256. // should encode successfully
  257. if _, err := withChainID.HashStruct("EIP712Domain", withChainID.Domain.Map()); err != nil {
  258. t.Errorf("Expected the typedData to encode the domain successfully, got %v", err)
  259. }
  260. }
  261. func TestHashStruct(t *testing.T) {
  262. hash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. mainHash := fmt.Sprintf("0x%s", common.Bytes2Hex(hash))
  267. if mainHash != "0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e" {
  268. t.Errorf("Expected different hashStruct result (got %s)", mainHash)
  269. }
  270. hash, err = typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
  271. if err != nil {
  272. t.Error(err)
  273. }
  274. domainHash := fmt.Sprintf("0x%s", common.Bytes2Hex(hash))
  275. if domainHash != "0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f" {
  276. t.Errorf("Expected different domain hashStruct result (got %s)", domainHash)
  277. }
  278. }
  279. func TestEncodeType(t *testing.T) {
  280. domainTypeEncoding := string(typedData.EncodeType("EIP712Domain"))
  281. if domainTypeEncoding != "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" {
  282. t.Errorf("Expected different encodeType result (got %s)", domainTypeEncoding)
  283. }
  284. mailTypeEncoding := string(typedData.EncodeType(typedData.PrimaryType))
  285. if mailTypeEncoding != "Mail(Person from,Person to,string contents)Person(string name,address wallet)" {
  286. t.Errorf("Expected different encodeType result (got %s)", mailTypeEncoding)
  287. }
  288. }
  289. func TestTypeHash(t *testing.T) {
  290. mailTypeHash := fmt.Sprintf("0x%s", common.Bytes2Hex(typedData.TypeHash(typedData.PrimaryType)))
  291. if mailTypeHash != "0xa0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2" {
  292. t.Errorf("Expected different typeHash result (got %s)", mailTypeHash)
  293. }
  294. }
  295. func TestEncodeData(t *testing.T) {
  296. hash, err := typedData.EncodeData(typedData.PrimaryType, typedData.Message, 0)
  297. if err != nil {
  298. t.Fatal(err)
  299. }
  300. dataEncoding := fmt.Sprintf("0x%s", common.Bytes2Hex(hash))
  301. if dataEncoding != "0xa0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2fc71e5fa27ff56c350aa531bc129ebdf613b772b6604664f5d8dbe21b85eb0c8cd54f074a4af31b4411ff6a60c9719dbd559c221c8ac3492d9d872b041d703d1b5aadf3154a261abdd9086fc627b61efca26ae5702701d05cd2305f7c52a2fc8" {
  302. t.Errorf("Expected different encodeData result (got %s)", dataEncoding)
  303. }
  304. }
  305. func TestFormatter(t *testing.T) {
  306. var d apitypes.TypedData
  307. err := json.Unmarshal([]byte(jsonTypedData), &d)
  308. if err != nil {
  309. t.Fatalf("unmarshalling failed '%v'", err)
  310. }
  311. formatted, _ := d.Format()
  312. for _, item := range formatted {
  313. t.Logf("'%v'\n", item.Pprint(0))
  314. }
  315. j, _ := json.Marshal(formatted)
  316. t.Logf("'%v'\n", string(j))
  317. }
  318. func sign(typedData apitypes.TypedData) ([]byte, []byte, error) {
  319. domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
  320. if err != nil {
  321. return nil, nil, err
  322. }
  323. typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
  324. if err != nil {
  325. return nil, nil, err
  326. }
  327. rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
  328. sighash := crypto.Keccak256(rawData)
  329. return typedDataHash, sighash, nil
  330. }
  331. func TestJsonFiles(t *testing.T) {
  332. testfiles, err := os.ReadDir("testdata/")
  333. if err != nil {
  334. t.Fatalf("failed reading files: %v", err)
  335. }
  336. for i, fInfo := range testfiles {
  337. if !strings.HasSuffix(fInfo.Name(), "json") {
  338. continue
  339. }
  340. expectedFailure := strings.HasPrefix(fInfo.Name(), "expfail")
  341. data, err := os.ReadFile(path.Join("testdata", fInfo.Name()))
  342. if err != nil {
  343. t.Errorf("Failed to read file %v: %v", fInfo.Name(), err)
  344. continue
  345. }
  346. var typedData apitypes.TypedData
  347. err = json.Unmarshal(data, &typedData)
  348. if err != nil {
  349. t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err)
  350. continue
  351. }
  352. _, _, err = sign(typedData)
  353. t.Logf("Error %v\n", err)
  354. if err != nil && !expectedFailure {
  355. t.Errorf("Test %d failed, file %v: %v", i, fInfo.Name(), err)
  356. }
  357. if expectedFailure && err == nil {
  358. t.Errorf("Test %d succeeded (expected failure), file %v: %v", i, fInfo.Name(), err)
  359. }
  360. }
  361. }
  362. // TestFuzzerFiles tests some files that have been found by fuzzing to cause
  363. // crashes or hangs.
  364. func TestFuzzerFiles(t *testing.T) {
  365. corpusdir := path.Join("testdata", "fuzzing")
  366. testfiles, err := os.ReadDir(corpusdir)
  367. if err != nil {
  368. t.Fatalf("failed reading files: %v", err)
  369. }
  370. verbose := false
  371. for i, fInfo := range testfiles {
  372. data, err := os.ReadFile(path.Join(corpusdir, fInfo.Name()))
  373. if err != nil {
  374. t.Errorf("Failed to read file %v: %v", fInfo.Name(), err)
  375. continue
  376. }
  377. var typedData apitypes.TypedData
  378. err = json.Unmarshal(data, &typedData)
  379. if err != nil {
  380. t.Errorf("Test %d, file %v, json unmarshalling failed: %v", i, fInfo.Name(), err)
  381. continue
  382. }
  383. _, err = typedData.EncodeData("EIP712Domain", typedData.Domain.Map(), 1)
  384. if verbose && err != nil {
  385. t.Logf("%d, EncodeData[1] err: %v\n", i, err)
  386. }
  387. _, err = typedData.EncodeData(typedData.PrimaryType, typedData.Message, 1)
  388. if verbose && err != nil {
  389. t.Logf("%d, EncodeData[2] err: %v\n", i, err)
  390. }
  391. typedData.Format()
  392. }
  393. }
  394. var gnosisTypedData = `
  395. {
  396. "types": {
  397. "EIP712Domain": [
  398. { "type": "address", "name": "verifyingContract" }
  399. ],
  400. "SafeTx": [
  401. { "type": "address", "name": "to" },
  402. { "type": "uint256", "name": "value" },
  403. { "type": "bytes", "name": "data" },
  404. { "type": "uint8", "name": "operation" },
  405. { "type": "uint256", "name": "safeTxGas" },
  406. { "type": "uint256", "name": "baseGas" },
  407. { "type": "uint256", "name": "gasPrice" },
  408. { "type": "address", "name": "gasToken" },
  409. { "type": "address", "name": "refundReceiver" },
  410. { "type": "uint256", "name": "nonce" }
  411. ]
  412. },
  413. "domain": {
  414. "verifyingContract": "0x25a6c4BBd32B2424A9c99aEB0584Ad12045382B3"
  415. },
  416. "primaryType": "SafeTx",
  417. "message": {
  418. "to": "0x9eE457023bB3De16D51A003a247BaEaD7fce313D",
  419. "value": "20000000000000000",
  420. "data": "0x",
  421. "operation": 0,
  422. "safeTxGas": 27845,
  423. "baseGas": 0,
  424. "gasPrice": "0",
  425. "gasToken": "0x0000000000000000000000000000000000000000",
  426. "refundReceiver": "0x0000000000000000000000000000000000000000",
  427. "nonce": 3
  428. }
  429. }`
  430. var gnosisTx = `
  431. {
  432. "safe": "0x25a6c4BBd32B2424A9c99aEB0584Ad12045382B3",
  433. "to": "0x9eE457023bB3De16D51A003a247BaEaD7fce313D",
  434. "value": "20000000000000000",
  435. "data": null,
  436. "operation": 0,
  437. "gasToken": "0x0000000000000000000000000000000000000000",
  438. "safeTxGas": 27845,
  439. "baseGas": 0,
  440. "gasPrice": "0",
  441. "refundReceiver": "0x0000000000000000000000000000000000000000",
  442. "nonce": 3,
  443. "executionDate": null,
  444. "submissionDate": "2020-09-15T21:59:23.815748Z",
  445. "modified": "2020-09-15T21:59:23.815748Z",
  446. "blockNumber": null,
  447. "transactionHash": null,
  448. "safeTxHash": "0x28bae2bd58d894a1d9b69e5e9fde3570c4b98a6fc5499aefb54fb830137e831f",
  449. "executor": null,
  450. "isExecuted": false,
  451. "isSuccessful": null,
  452. "ethGasPrice": null,
  453. "gasUsed": null,
  454. "fee": null,
  455. "origin": null,
  456. "dataDecoded": null,
  457. "confirmationsRequired": null,
  458. "confirmations": [
  459. {
  460. "owner": "0xAd2e180019FCa9e55CADe76E4487F126Fd08DA34",
  461. "submissionDate": "2020-09-15T21:59:28.281243Z",
  462. "transactionHash": null,
  463. "confirmationType": "CONFIRMATION",
  464. "signature": "0x5e562065a0cb15d766dac0cd49eb6d196a41183af302c4ecad45f1a81958d7797753f04424a9b0aa1cb0448e4ec8e189540fbcdda7530ef9b9d95dfc2d36cb521b",
  465. "signatureType": "EOA"
  466. }
  467. ],
  468. "signatures": null
  469. }
  470. `
  471. // TestGnosisTypedData tests the scenario where a user submits a full EIP-712
  472. // struct without using the gnosis-specific endpoint
  473. func TestGnosisTypedData(t *testing.T) {
  474. var td apitypes.TypedData
  475. err := json.Unmarshal([]byte(gnosisTypedData), &td)
  476. if err != nil {
  477. t.Fatalf("unmarshalling failed '%v'", err)
  478. }
  479. _, sighash, err := sign(td)
  480. if err != nil {
  481. t.Fatal(err)
  482. }
  483. expSigHash := common.FromHex("0x28bae2bd58d894a1d9b69e5e9fde3570c4b98a6fc5499aefb54fb830137e831f")
  484. if !bytes.Equal(expSigHash, sighash) {
  485. t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
  486. }
  487. }
  488. // TestGnosisCustomData tests the scenario where a user submits only the gnosis-safe
  489. // specific data, and we fill the TypedData struct on our side
  490. func TestGnosisCustomData(t *testing.T) {
  491. var tx core.GnosisSafeTx
  492. err := json.Unmarshal([]byte(gnosisTx), &tx)
  493. if err != nil {
  494. t.Fatal(err)
  495. }
  496. var td = tx.ToTypedData()
  497. _, sighash, err := sign(td)
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. expSigHash := common.FromHex("0x28bae2bd58d894a1d9b69e5e9fde3570c4b98a6fc5499aefb54fb830137e831f")
  502. if !bytes.Equal(expSigHash, sighash) {
  503. t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
  504. }
  505. }
  506. var gnosisTypedDataWithChainId = `
  507. {
  508. "types": {
  509. "EIP712Domain": [
  510. { "type": "uint256", "name": "chainId" },
  511. { "type": "address", "name": "verifyingContract" }
  512. ],
  513. "SafeTx": [
  514. { "type": "address", "name": "to" },
  515. { "type": "uint256", "name": "value" },
  516. { "type": "bytes", "name": "data" },
  517. { "type": "uint8", "name": "operation" },
  518. { "type": "uint256", "name": "safeTxGas" },
  519. { "type": "uint256", "name": "baseGas" },
  520. { "type": "uint256", "name": "gasPrice" },
  521. { "type": "address", "name": "gasToken" },
  522. { "type": "address", "name": "refundReceiver" },
  523. { "type": "uint256", "name": "nonce" }
  524. ]
  525. },
  526. "domain": {
  527. "verifyingContract": "0x111dAE35D176A9607053e0c46e91F36AFbC1dc57",
  528. "chainId": "4"
  529. },
  530. "primaryType": "SafeTx",
  531. "message": {
  532. "to": "0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa",
  533. "value": "0",
  534. "data": "0xa9059cbb00000000000000000000000099d580d3a7fe7bd183b2464517b2cd7ce5a8f15a0000000000000000000000000000000000000000000000000de0b6b3a7640000",
  535. "operation": 0,
  536. "safeTxGas": 0,
  537. "baseGas": 0,
  538. "gasPrice": "0",
  539. "gasToken": "0x0000000000000000000000000000000000000000",
  540. "refundReceiver": "0x0000000000000000000000000000000000000000",
  541. "nonce": 15
  542. }
  543. }`
  544. var gnosisTxWithChainId = `
  545. {
  546. "safe": "0x111dAE35D176A9607053e0c46e91F36AFbC1dc57",
  547. "to": "0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa",
  548. "value": "0",
  549. "data": "0xa9059cbb00000000000000000000000099d580d3a7fe7bd183b2464517b2cd7ce5a8f15a0000000000000000000000000000000000000000000000000de0b6b3a7640000",
  550. "operation": 0,
  551. "gasToken": "0x0000000000000000000000000000000000000000",
  552. "safeTxGas": 0,
  553. "baseGas": 0,
  554. "gasPrice": "0",
  555. "refundReceiver": "0x0000000000000000000000000000000000000000",
  556. "nonce": 15,
  557. "executionDate": "2022-01-10T20:00:12Z",
  558. "submissionDate": "2022-01-10T19:59:59.689989Z",
  559. "modified": "2022-01-10T20:00:31.903635Z",
  560. "blockNumber": 9968802,
  561. "transactionHash": "0xc9fef30499ee8984974ab9dddd9d15c2a97c1a4393935dceed5efc3af9fc41a4",
  562. "safeTxHash": "0x6619dab5401503f2735256e12b898e69eb701d6a7e0d07abf1be4bb8aebfba29",
  563. "executor": "0xbc2BB26a6d821e69A38016f3858561a1D80d4182",
  564. "isExecuted": true,
  565. "isSuccessful": true,
  566. "ethGasPrice": "2500000009",
  567. "gasUsed": 82902,
  568. "fee": "207255000746118",
  569. "chainId": "4",
  570. "origin": null,
  571. "dataDecoded": {
  572. "method": "transfer",
  573. "parameters": [
  574. {
  575. "name": "to",
  576. "type": "address",
  577. "value": "0x99D580d3a7FE7BD183b2464517B2cD7ce5A8F15A"
  578. },
  579. {
  580. "name": "value",
  581. "type": "uint256",
  582. "value": "1000000000000000000"
  583. }
  584. ]
  585. },
  586. "confirmationsRequired": 1,
  587. "confirmations": [
  588. {
  589. "owner": "0xbc2BB26a6d821e69A38016f3858561a1D80d4182",
  590. "submissionDate": "2022-01-10T19:59:59.722500Z",
  591. "transactionHash": null,
  592. "signature": "0x5ca34641bcdee06e7b99143bfe34778195ca41022bd35837b96c204c7786be9d6dfa6dba43b53cd92da45ac728899e1561b232d28f38ba82df45f164caba38be1b",
  593. "signatureType": "EOA"
  594. }
  595. ],
  596. "signatures": "0x5ca34641bcdee06e7b99143bfe34778195ca41022bd35837b96c204c7786be9d6dfa6dba43b53cd92da45ac728899e1561b232d28f38ba82df45f164caba38be1b"
  597. }
  598. `
  599. func TestGnosisTypedDataWithChainId(t *testing.T) {
  600. var td apitypes.TypedData
  601. err := json.Unmarshal([]byte(gnosisTypedDataWithChainId), &td)
  602. if err != nil {
  603. t.Fatalf("unmarshalling failed '%v'", err)
  604. }
  605. _, sighash, err := sign(td)
  606. if err != nil {
  607. t.Fatal(err)
  608. }
  609. expSigHash := common.FromHex("0x6619dab5401503f2735256e12b898e69eb701d6a7e0d07abf1be4bb8aebfba29")
  610. if !bytes.Equal(expSigHash, sighash) {
  611. t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
  612. }
  613. }
  614. // TestGnosisCustomData tests the scenario where a user submits only the gnosis-safe
  615. // specific data, and we fill the TypedData struct on our side
  616. func TestGnosisCustomDataWithChainId(t *testing.T) {
  617. var tx core.GnosisSafeTx
  618. err := json.Unmarshal([]byte(gnosisTxWithChainId), &tx)
  619. if err != nil {
  620. t.Fatal(err)
  621. }
  622. var td = tx.ToTypedData()
  623. _, sighash, err := sign(td)
  624. if err != nil {
  625. t.Fatal(err)
  626. }
  627. expSigHash := common.FromHex("0x6619dab5401503f2735256e12b898e69eb701d6a7e0d07abf1be4bb8aebfba29")
  628. if !bytes.Equal(expSigHash, sighash) {
  629. t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
  630. }
  631. }
  632. var complexTypedData = `
  633. {
  634. "types": {
  635. "EIP712Domain": [
  636. {
  637. "name": "chainId",
  638. "type": "uint256"
  639. },
  640. {
  641. "name": "name",
  642. "type": "string"
  643. },
  644. {
  645. "name": "verifyingContract",
  646. "type": "address"
  647. },
  648. {
  649. "name": "version",
  650. "type": "string"
  651. }
  652. ],
  653. "Action": [
  654. {
  655. "name": "action",
  656. "type": "string"
  657. },
  658. {
  659. "name": "params",
  660. "type": "string"
  661. }
  662. ],
  663. "Cell": [
  664. {
  665. "name": "capacity",
  666. "type": "string"
  667. },
  668. {
  669. "name": "lock",
  670. "type": "string"
  671. },
  672. {
  673. "name": "type",
  674. "type": "string"
  675. },
  676. {
  677. "name": "data",
  678. "type": "string"
  679. },
  680. {
  681. "name": "extraData",
  682. "type": "string"
  683. }
  684. ],
  685. "Transaction": [
  686. {
  687. "name": "DAS_MESSAGE",
  688. "type": "string"
  689. },
  690. {
  691. "name": "inputsCapacity",
  692. "type": "string"
  693. },
  694. {
  695. "name": "outputsCapacity",
  696. "type": "string"
  697. },
  698. {
  699. "name": "fee",
  700. "type": "string"
  701. },
  702. {
  703. "name": "action",
  704. "type": "Action"
  705. },
  706. {
  707. "name": "inputs",
  708. "type": "Cell[]"
  709. },
  710. {
  711. "name": "outputs",
  712. "type": "Cell[]"
  713. },
  714. {
  715. "name": "digest",
  716. "type": "bytes32"
  717. }
  718. ]
  719. },
  720. "primaryType": "Transaction",
  721. "domain": {
  722. "chainId": "56",
  723. "name": "da.systems",
  724. "verifyingContract": "0x0000000000000000000000000000000020210722",
  725. "version": "1"
  726. },
  727. "message": {
  728. "DAS_MESSAGE": "SELL mobcion.bit FOR 100000 CKB",
  729. "inputsCapacity": "1216.9999 CKB",
  730. "outputsCapacity": "1216.9998 CKB",
  731. "fee": "0.0001 CKB",
  732. "digest": "0x53a6c0f19ec281604607f5d6817e442082ad1882bef0df64d84d3810dae561eb",
  733. "action": {
  734. "action": "start_account_sale",
  735. "params": "0x00"
  736. },
  737. "inputs": [
  738. {
  739. "capacity": "218 CKB",
  740. "lock": "das-lock,0x01,0x051c152f77f8efa9c7c6d181cc97ee67c165c506...",
  741. "type": "account-cell-type,0x01,0x",
  742. "data": "{ account: mobcion.bit, expired_at: 1670913958 }",
  743. "extraData": "{ status: 0, records_hash: 0x55478d76900611eb079b22088081124ed6c8bae21a05dd1a0d197efcc7c114ce }"
  744. }
  745. ],
  746. "outputs": [
  747. {
  748. "capacity": "218 CKB",
  749. "lock": "das-lock,0x01,0x051c152f77f8efa9c7c6d181cc97ee67c165c506...",
  750. "type": "account-cell-type,0x01,0x",
  751. "data": "{ account: mobcion.bit, expired_at: 1670913958 }",
  752. "extraData": "{ status: 1, records_hash: 0x55478d76900611eb079b22088081124ed6c8bae21a05dd1a0d197efcc7c114ce }"
  753. },
  754. {
  755. "capacity": "201 CKB",
  756. "lock": "das-lock,0x01,0x051c152f77f8efa9c7c6d181cc97ee67c165c506...",
  757. "type": "account-sale-cell-type,0x01,0x",
  758. "data": "0x1209460ef3cb5f1c68ed2c43a3e020eec2d9de6e...",
  759. "extraData": ""
  760. }
  761. ]
  762. }
  763. }
  764. `
  765. func TestComplexTypedData(t *testing.T) {
  766. var td apitypes.TypedData
  767. err := json.Unmarshal([]byte(complexTypedData), &td)
  768. if err != nil {
  769. t.Fatalf("unmarshalling failed '%v'", err)
  770. }
  771. _, sighash, err := sign(td)
  772. if err != nil {
  773. t.Fatal(err)
  774. }
  775. expSigHash := common.FromHex("0x42b1aca82bb6900ff75e90a136de550a58f1a220a071704088eabd5e6ce20446")
  776. if !bytes.Equal(expSigHash, sighash) {
  777. t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
  778. }
  779. }