personal.go 3.0 KB

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