personal.go 3.5 KB

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