personal.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package api
  2. import (
  3. "time"
  4. "github.com/ethereum/go-ethereum/common"
  5. "github.com/ethereum/go-ethereum/eth"
  6. "github.com/ethereum/go-ethereum/rpc/codec"
  7. "github.com/ethereum/go-ethereum/rpc/shared"
  8. "github.com/ethereum/go-ethereum/xeth"
  9. )
  10. const (
  11. PersonalApiVersion = "1.0"
  12. )
  13. var (
  14. // mapping between methods and handlers
  15. personalMapping = map[string]personalhandler{
  16. "personal_listAccounts": (*personalApi).ListAccounts,
  17. "personal_newAccount": (*personalApi).NewAccount,
  18. "personal_deleteAccount": (*personalApi).DeleteAccount,
  19. "personal_unlockAccount": (*personalApi).UnlockAccount,
  20. }
  21. )
  22. // net callback handler
  23. type personalhandler func(*personalApi, *shared.Request) (interface{}, error)
  24. // net api provider
  25. type personalApi struct {
  26. xeth *xeth.XEth
  27. ethereum *eth.Ethereum
  28. methods map[string]personalhandler
  29. codec codec.ApiCoder
  30. }
  31. // create a new net api instance
  32. func NewPersonalApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *personalApi {
  33. return &personalApi{
  34. xeth: xeth,
  35. ethereum: eth,
  36. methods: personalMapping,
  37. codec: coder.New(nil),
  38. }
  39. }
  40. // collection with supported methods
  41. func (self *personalApi) Methods() []string {
  42. methods := make([]string, len(self.methods))
  43. i := 0
  44. for k := range self.methods {
  45. methods[i] = k
  46. i++
  47. }
  48. return methods
  49. }
  50. // Execute given request
  51. func (self *personalApi) Execute(req *shared.Request) (interface{}, error) {
  52. if callback, ok := self.methods[req.Method]; ok {
  53. return callback(self, req)
  54. }
  55. return nil, shared.NewNotImplementedError(req.Method)
  56. }
  57. func (self *personalApi) Name() string {
  58. return PersonalApiName
  59. }
  60. func (self *personalApi) ApiVersion() string {
  61. return PersonalApiVersion
  62. }
  63. func (self *personalApi) ListAccounts(req *shared.Request) (interface{}, error) {
  64. return self.xeth.Accounts(), nil
  65. }
  66. func (self *personalApi) NewAccount(req *shared.Request) (interface{}, error) {
  67. args := new(NewAccountArgs)
  68. if err := self.codec.Decode(req.Params, &args); err != nil {
  69. return nil, shared.NewDecodeParamError(err.Error())
  70. }
  71. am := self.ethereum.AccountManager()
  72. acc, err := am.NewAccount(args.Passphrase)
  73. return acc.Address.Hex(), err
  74. }
  75. func (self *personalApi) DeleteAccount(req *shared.Request) (interface{}, error) {
  76. args := new(DeleteAccountArgs)
  77. if err := self.codec.Decode(req.Params, &args); err != nil {
  78. return nil, shared.NewDecodeParamError(err.Error())
  79. }
  80. addr := common.HexToAddress(args.Address)
  81. am := self.ethereum.AccountManager()
  82. if err := am.DeleteAccount(addr, args.Passphrase); err == nil {
  83. return true, nil
  84. } else {
  85. return false, err
  86. }
  87. }
  88. func (self *personalApi) UnlockAccount(req *shared.Request) (interface{}, error) {
  89. args := new(UnlockAccountArgs)
  90. if err := self.codec.Decode(req.Params, &args); err != nil {
  91. return nil, shared.NewDecodeParamError(err.Error())
  92. }
  93. var err error
  94. am := self.ethereum.AccountManager()
  95. addr := common.HexToAddress(args.Address)
  96. if args.Duration == -1 {
  97. err = am.Unlock(addr, args.Passphrase)
  98. } else {
  99. err = am.TimedUnlock(addr, args.Passphrase, time.Duration(args.Duration)*time.Second)
  100. }
  101. if err == nil {
  102. return true, nil
  103. }
  104. return false, err
  105. }