args.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. package rpc
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "math/big"
  6. "github.com/ethereum/go-ethereum/common"
  7. )
  8. func blockAge(raw interface{}, number *int64) (err error) {
  9. // Parse as integer
  10. num, ok := raw.(float64)
  11. if ok {
  12. *number = int64(num)
  13. return nil
  14. }
  15. // Parse as string/hexstring
  16. str, ok := raw.(string)
  17. if !ok {
  18. return NewDecodeParamError("BlockNumber is not a string")
  19. }
  20. switch str {
  21. case "latest":
  22. *number = -1
  23. case "pending":
  24. *number = 0
  25. default:
  26. *number = common.String2Big(str).Int64()
  27. }
  28. return nil
  29. }
  30. type GetBlockByHashArgs struct {
  31. BlockHash string
  32. Transactions bool
  33. }
  34. func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) {
  35. var obj []interface{}
  36. r := bytes.NewReader(b)
  37. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  38. return NewDecodeParamError(err.Error())
  39. }
  40. if len(obj) < 1 {
  41. return NewInsufficientParamsError(len(obj), 1)
  42. }
  43. argstr, ok := obj[0].(string)
  44. if !ok {
  45. return NewDecodeParamError("BlockHash not a string")
  46. }
  47. args.BlockHash = argstr
  48. if len(obj) > 1 {
  49. args.Transactions = obj[1].(bool)
  50. }
  51. return nil
  52. }
  53. type GetBlockByNumberArgs struct {
  54. BlockNumber int64
  55. Transactions bool
  56. }
  57. func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) {
  58. var obj []interface{}
  59. r := bytes.NewReader(b)
  60. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  61. return NewDecodeParamError(err.Error())
  62. }
  63. if len(obj) < 1 {
  64. return NewInsufficientParamsError(len(obj), 1)
  65. }
  66. if v, ok := obj[0].(float64); ok {
  67. args.BlockNumber = int64(v)
  68. } else {
  69. args.BlockNumber = common.Big(obj[0].(string)).Int64()
  70. }
  71. if len(obj) > 1 {
  72. args.Transactions = obj[1].(bool)
  73. }
  74. return nil
  75. }
  76. type NewTxArgs struct {
  77. From string
  78. To string
  79. Value *big.Int
  80. Gas *big.Int
  81. GasPrice *big.Int
  82. Data string
  83. BlockNumber int64
  84. }
  85. func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
  86. var obj []json.RawMessage
  87. var ext struct{ From, To, Value, Gas, GasPrice, Data string }
  88. // Decode byte slice to array of RawMessages
  89. if err := json.Unmarshal(b, &obj); err != nil {
  90. return NewDecodeParamError(err.Error())
  91. }
  92. // Check for sufficient params
  93. if len(obj) < 1 {
  94. return NewInsufficientParamsError(len(obj), 1)
  95. }
  96. // Decode 0th RawMessage to temporary struct
  97. if err := json.Unmarshal(obj[0], &ext); err != nil {
  98. return NewDecodeParamError(err.Error())
  99. }
  100. // var ok bool
  101. args.From = ext.From
  102. args.To = ext.To
  103. args.Value = common.String2Big(ext.Value)
  104. args.Gas = common.String2Big(ext.Gas)
  105. args.GasPrice = common.String2Big(ext.GasPrice)
  106. args.Data = ext.Data
  107. // Check for optional BlockNumber param
  108. if len(obj) > 1 {
  109. var raw interface{}
  110. if err = json.Unmarshal(obj[1], &raw); err != nil {
  111. return NewDecodeParamError(err.Error())
  112. }
  113. if err := blockAge(raw, &args.BlockNumber); err != nil {
  114. return err
  115. }
  116. }
  117. return nil
  118. }
  119. func (args *NewTxArgs) requirements() error {
  120. if len(args.From) == 0 {
  121. return NewValidationError("From", "Is required")
  122. }
  123. return nil
  124. }
  125. type GetStorageArgs struct {
  126. Address string
  127. BlockNumber int64
  128. }
  129. func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
  130. var obj []interface{}
  131. if err := json.Unmarshal(b, &obj); err != nil {
  132. return NewDecodeParamError(err.Error())
  133. }
  134. if len(obj) < 1 {
  135. return NewInsufficientParamsError(len(obj), 1)
  136. }
  137. addstr, ok := obj[0].(string)
  138. if !ok {
  139. return NewDecodeParamError("Address is not a string")
  140. }
  141. args.Address = addstr
  142. if len(obj) > 1 {
  143. if err := blockAge(obj[1], &args.BlockNumber); err != nil {
  144. return err
  145. }
  146. }
  147. return nil
  148. }
  149. func (args *GetStorageArgs) requirements() error {
  150. if len(args.Address) == 0 {
  151. return NewValidationError("Address", "cannot be blank")
  152. }
  153. return nil
  154. }
  155. type GetStorageAtArgs struct {
  156. Address string
  157. Key string
  158. BlockNumber int64
  159. }
  160. func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
  161. var obj []interface{}
  162. if err := json.Unmarshal(b, &obj); err != nil {
  163. return NewDecodeParamError(err.Error())
  164. }
  165. if len(obj) < 2 {
  166. return NewInsufficientParamsError(len(obj), 2)
  167. }
  168. addstr, ok := obj[0].(string)
  169. if !ok {
  170. return NewDecodeParamError("Address is not a string")
  171. }
  172. args.Address = addstr
  173. keystr, ok := obj[1].(string)
  174. if !ok {
  175. return NewDecodeParamError("Key is not a string")
  176. }
  177. args.Key = keystr
  178. if len(obj) > 2 {
  179. if err := blockAge(obj[2], &args.BlockNumber); err != nil {
  180. return err
  181. }
  182. }
  183. return nil
  184. }
  185. func (args *GetStorageAtArgs) requirements() error {
  186. if len(args.Address) == 0 {
  187. return NewValidationError("Address", "cannot be blank")
  188. }
  189. if len(args.Key) == 0 {
  190. return NewValidationError("Key", "cannot be blank")
  191. }
  192. return nil
  193. }
  194. type GetTxCountArgs struct {
  195. Address string
  196. BlockNumber int64
  197. }
  198. func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
  199. var obj []interface{}
  200. if err := json.Unmarshal(b, &obj); err != nil {
  201. return NewDecodeParamError(err.Error())
  202. }
  203. if len(obj) < 1 {
  204. return NewInsufficientParamsError(len(obj), 1)
  205. }
  206. addstr, ok := obj[0].(string)
  207. if !ok {
  208. return NewDecodeParamError("Address is not a string")
  209. }
  210. args.Address = addstr
  211. if len(obj) > 1 {
  212. if err := blockAge(obj[1], &args.BlockNumber); err != nil {
  213. return err
  214. }
  215. }
  216. return nil
  217. }
  218. func (args *GetTxCountArgs) requirements() error {
  219. if len(args.Address) == 0 {
  220. return NewValidationError("Address", "cannot be blank")
  221. }
  222. return nil
  223. }
  224. type GetBalanceArgs struct {
  225. Address string
  226. BlockNumber int64
  227. }
  228. func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
  229. var obj []interface{}
  230. if err := json.Unmarshal(b, &obj); err != nil {
  231. return NewDecodeParamError(err.Error())
  232. }
  233. if len(obj) < 1 {
  234. return NewInsufficientParamsError(len(obj), 1)
  235. }
  236. addstr, ok := obj[0].(string)
  237. if !ok {
  238. return NewDecodeParamError("Address is not a string")
  239. }
  240. args.Address = addstr
  241. if len(obj) > 1 {
  242. if err := blockAge(obj[1], &args.BlockNumber); err != nil {
  243. return err
  244. }
  245. }
  246. return nil
  247. }
  248. func (args *GetBalanceArgs) requirements() error {
  249. if len(args.Address) == 0 {
  250. return NewValidationError("Address", "cannot be blank")
  251. }
  252. return nil
  253. }
  254. type GetDataArgs struct {
  255. Address string
  256. BlockNumber int64
  257. }
  258. func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
  259. var obj []interface{}
  260. if err := json.Unmarshal(b, &obj); err != nil {
  261. return NewDecodeParamError(err.Error())
  262. }
  263. if len(obj) < 1 {
  264. return NewInsufficientParamsError(len(obj), 1)
  265. }
  266. addstr, ok := obj[0].(string)
  267. if !ok {
  268. return NewDecodeParamError("Address is not a string")
  269. }
  270. args.Address = addstr
  271. if len(obj) > 1 {
  272. if err := blockAge(obj[1], &args.BlockNumber); err != nil {
  273. return err
  274. }
  275. }
  276. return nil
  277. }
  278. func (args *GetDataArgs) requirements() error {
  279. if len(args.Address) == 0 {
  280. return NewValidationError("Address", "cannot be blank")
  281. }
  282. return nil
  283. }
  284. type BlockNumIndexArgs struct {
  285. BlockNumber int64
  286. Index int64
  287. }
  288. func (args *BlockNumIndexArgs) UnmarshalJSON(b []byte) (err error) {
  289. var obj []interface{}
  290. r := bytes.NewReader(b)
  291. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  292. return NewDecodeParamError(err.Error())
  293. }
  294. if len(obj) < 1 {
  295. return NewInsufficientParamsError(len(obj), 1)
  296. }
  297. arg0, ok := obj[0].(string)
  298. if !ok {
  299. return NewDecodeParamError("BlockNumber is not string")
  300. }
  301. args.BlockNumber = common.Big(arg0).Int64()
  302. if len(obj) > 1 {
  303. arg1, ok := obj[1].(string)
  304. if !ok {
  305. return NewDecodeParamError("Index not a string")
  306. }
  307. args.Index = common.Big(arg1).Int64()
  308. }
  309. return nil
  310. }
  311. type HashIndexArgs struct {
  312. Hash string
  313. Index int64
  314. }
  315. func (args *HashIndexArgs) UnmarshalJSON(b []byte) (err error) {
  316. var obj []interface{}
  317. r := bytes.NewReader(b)
  318. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  319. return NewDecodeParamError(err.Error())
  320. }
  321. if len(obj) < 1 {
  322. return NewInsufficientParamsError(len(obj), 1)
  323. }
  324. arg0, ok := obj[0].(string)
  325. if !ok {
  326. return NewDecodeParamError("Hash not a string")
  327. }
  328. args.Hash = arg0
  329. if len(obj) > 1 {
  330. arg1, ok := obj[1].(string)
  331. if !ok {
  332. return NewDecodeParamError("Index not a string")
  333. }
  334. args.Index = common.Big(arg1).Int64()
  335. }
  336. return nil
  337. }
  338. type Sha3Args struct {
  339. Data string
  340. }
  341. func (args *Sha3Args) UnmarshalJSON(b []byte) (err error) {
  342. var obj []interface{}
  343. r := bytes.NewReader(b)
  344. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  345. return NewDecodeParamError(err.Error())
  346. }
  347. if len(obj) < 1 {
  348. return NewInsufficientParamsError(len(obj), 1)
  349. }
  350. args.Data = obj[0].(string)
  351. return nil
  352. }
  353. type FilterOptions struct {
  354. Earliest int64
  355. Latest int64
  356. Address interface{}
  357. Topics []interface{}
  358. Skip int
  359. Max int
  360. }
  361. func (args *FilterOptions) UnmarshalJSON(b []byte) (err error) {
  362. var obj []struct {
  363. FromBlock interface{} `json:"fromBlock"`
  364. ToBlock interface{} `json:"toBlock"`
  365. Limit string `json:"limit"`
  366. Offset string `json:"offset"`
  367. Address string `json:"address"`
  368. Topics []interface{} `json:"topics"`
  369. }
  370. if err = json.Unmarshal(b, &obj); err != nil {
  371. return NewDecodeParamError(err.Error())
  372. }
  373. if len(obj) < 1 {
  374. return NewInsufficientParamsError(len(obj), 1)
  375. }
  376. fromstr, ok := obj[0].FromBlock.(string)
  377. if !ok {
  378. return NewDecodeParamError("FromBlock is not a string")
  379. }
  380. switch fromstr {
  381. case "latest":
  382. args.Earliest = 0
  383. default:
  384. args.Earliest = int64(common.Big(obj[0].FromBlock.(string)).Int64())
  385. }
  386. tostr, ok := obj[0].ToBlock.(string)
  387. if !ok {
  388. return NewDecodeParamError("ToBlock is not a string")
  389. }
  390. switch tostr {
  391. case "latest":
  392. args.Latest = 0
  393. case "pending":
  394. args.Latest = -1
  395. default:
  396. args.Latest = int64(common.Big(obj[0].ToBlock.(string)).Int64())
  397. }
  398. args.Max = int(common.Big(obj[0].Limit).Int64())
  399. args.Skip = int(common.Big(obj[0].Offset).Int64())
  400. args.Address = obj[0].Address
  401. args.Topics = obj[0].Topics
  402. return nil
  403. }
  404. type DbArgs struct {
  405. Database string
  406. Key string
  407. Value string
  408. }
  409. func (args *DbArgs) UnmarshalJSON(b []byte) (err error) {
  410. var obj []interface{}
  411. r := bytes.NewReader(b)
  412. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  413. return NewDecodeParamError(err.Error())
  414. }
  415. if len(obj) < 2 {
  416. return NewInsufficientParamsError(len(obj), 2)
  417. }
  418. args.Database = obj[0].(string)
  419. args.Key = obj[1].(string)
  420. if len(obj) > 2 {
  421. args.Value = obj[2].(string)
  422. }
  423. return nil
  424. }
  425. func (a *DbArgs) requirements() error {
  426. if len(a.Database) == 0 {
  427. return NewValidationError("Database", "cannot be blank")
  428. }
  429. if len(a.Key) == 0 {
  430. return NewValidationError("Key", "cannot be blank")
  431. }
  432. return nil
  433. }
  434. type WhisperMessageArgs struct {
  435. Payload string
  436. To string
  437. From string
  438. Topics []string
  439. Priority uint32
  440. Ttl uint32
  441. }
  442. func (args *WhisperMessageArgs) UnmarshalJSON(b []byte) (err error) {
  443. var obj []struct {
  444. Payload string
  445. To string
  446. From string
  447. Topics []string
  448. Priority string
  449. Ttl string
  450. }
  451. if err = json.Unmarshal(b, &obj); err != nil {
  452. return NewDecodeParamError(err.Error())
  453. }
  454. if len(obj) < 1 {
  455. return NewInsufficientParamsError(len(obj), 1)
  456. }
  457. args.Payload = obj[0].Payload
  458. args.To = obj[0].To
  459. args.From = obj[0].From
  460. args.Topics = obj[0].Topics
  461. args.Priority = uint32(common.Big(obj[0].Priority).Int64())
  462. args.Ttl = uint32(common.Big(obj[0].Ttl).Int64())
  463. return nil
  464. }
  465. type CompileArgs struct {
  466. Source string
  467. }
  468. func (args *CompileArgs) UnmarshalJSON(b []byte) (err error) {
  469. var obj []interface{}
  470. r := bytes.NewReader(b)
  471. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  472. return NewDecodeParamError(err.Error())
  473. }
  474. if len(obj) > 0 {
  475. args.Source = obj[0].(string)
  476. }
  477. return nil
  478. }
  479. type FilterStringArgs struct {
  480. Word string
  481. }
  482. func (args *FilterStringArgs) UnmarshalJSON(b []byte) (err error) {
  483. var obj []interface{}
  484. r := bytes.NewReader(b)
  485. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  486. return NewDecodeParamError(err.Error())
  487. }
  488. if len(obj) < 1 {
  489. return NewInsufficientParamsError(len(obj), 1)
  490. }
  491. var argstr string
  492. argstr, ok := obj[0].(string)
  493. if !ok {
  494. return NewDecodeParamError("Filter is not a string")
  495. }
  496. args.Word = argstr
  497. return nil
  498. }
  499. type FilterIdArgs struct {
  500. Id int
  501. }
  502. func (args *FilterIdArgs) UnmarshalJSON(b []byte) (err error) {
  503. var obj []string
  504. r := bytes.NewReader(b)
  505. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  506. return NewDecodeParamError(err.Error())
  507. }
  508. if len(obj) < 1 {
  509. return NewInsufficientParamsError(len(obj), 1)
  510. }
  511. args.Id = int(common.Big(obj[0]).Int64())
  512. return nil
  513. }
  514. type WhisperIdentityArgs struct {
  515. Identity string
  516. }
  517. func (args *WhisperIdentityArgs) UnmarshalJSON(b []byte) (err error) {
  518. var obj []string
  519. r := bytes.NewReader(b)
  520. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  521. return NewDecodeParamError(err.Error())
  522. }
  523. if len(obj) < 1 {
  524. return NewInsufficientParamsError(len(obj), 1)
  525. }
  526. args.Identity = obj[0]
  527. return nil
  528. }
  529. type WhisperFilterArgs struct {
  530. To string `json:"to"`
  531. From string
  532. Topics []string
  533. }
  534. func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
  535. var obj []struct {
  536. To string
  537. From string
  538. Topics []string
  539. }
  540. if err = json.Unmarshal(b, &obj); err != nil {
  541. return NewDecodeParamError(err.Error())
  542. }
  543. if len(obj) < 1 {
  544. return NewInsufficientParamsError(len(obj), 1)
  545. }
  546. args.To = obj[0].To
  547. args.From = obj[0].From
  548. args.Topics = obj[0].Topics
  549. return nil
  550. }