personal_args.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 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 api
  17. import (
  18. "encoding/json"
  19. "github.com/ethereum/go-ethereum/rpc/shared"
  20. )
  21. type NewAccountArgs struct {
  22. Passphrase string
  23. }
  24. func (args *NewAccountArgs) UnmarshalJSON(b []byte) (err error) {
  25. var obj []interface{}
  26. if err := json.Unmarshal(b, &obj); err != nil {
  27. return shared.NewDecodeParamError(err.Error())
  28. }
  29. if len(obj) < 1 {
  30. return shared.NewInsufficientParamsError(len(obj), 1)
  31. }
  32. if passhrase, ok := obj[0].(string); ok {
  33. args.Passphrase = passhrase
  34. return nil
  35. }
  36. return shared.NewInvalidTypeError("passhrase", "not a string")
  37. }
  38. type DeleteAccountArgs struct {
  39. Address string
  40. Passphrase string
  41. }
  42. func (args *DeleteAccountArgs) UnmarshalJSON(b []byte) (err error) {
  43. var obj []interface{}
  44. if err := json.Unmarshal(b, &obj); err != nil {
  45. return shared.NewDecodeParamError(err.Error())
  46. }
  47. if len(obj) < 2 {
  48. return shared.NewInsufficientParamsError(len(obj), 2)
  49. }
  50. if addr, ok := obj[0].(string); ok {
  51. args.Address = addr
  52. } else {
  53. return shared.NewInvalidTypeError("address", "not a string")
  54. }
  55. if passhrase, ok := obj[1].(string); ok {
  56. args.Passphrase = passhrase
  57. } else {
  58. return shared.NewInvalidTypeError("passhrase", "not a string")
  59. }
  60. return nil
  61. }
  62. type UnlockAccountArgs struct {
  63. Address string
  64. Passphrase string
  65. Duration int
  66. }
  67. func (args *UnlockAccountArgs) UnmarshalJSON(b []byte) (err error) {
  68. var obj []interface{}
  69. if err := json.Unmarshal(b, &obj); err != nil {
  70. return shared.NewDecodeParamError(err.Error())
  71. }
  72. args.Duration = 0
  73. if len(obj) < 1 {
  74. return shared.NewInsufficientParamsError(len(obj), 1)
  75. }
  76. if addrstr, ok := obj[0].(string); ok {
  77. args.Address = addrstr
  78. } else {
  79. return shared.NewInvalidTypeError("address", "not a string")
  80. }
  81. if len(obj) >= 2 && obj[1] != nil {
  82. if passphrasestr, ok := obj[1].(string); ok {
  83. args.Passphrase = passphrasestr
  84. } else {
  85. return shared.NewInvalidTypeError("passphrase", "not a string")
  86. }
  87. }
  88. if len(obj) >= 3 && obj[2] != nil {
  89. if duration, ok := obj[2].(float64); ok {
  90. args.Duration = int(duration)
  91. }
  92. }
  93. return nil
  94. }