personal.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_deleteAccount": (*personalApi).DeleteAccount,
  35. "personal_unlockAccount": (*personalApi).UnlockAccount,
  36. }
  37. )
  38. // net callback handler
  39. type personalhandler func(*personalApi, *shared.Request) (interface{}, error)
  40. // net api provider
  41. type personalApi struct {
  42. xeth *xeth.XEth
  43. ethereum *eth.Ethereum
  44. methods map[string]personalhandler
  45. codec codec.ApiCoder
  46. }
  47. // create a new net api instance
  48. func NewPersonalApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *personalApi {
  49. return &personalApi{
  50. xeth: xeth,
  51. ethereum: eth,
  52. methods: personalMapping,
  53. codec: coder.New(nil),
  54. }
  55. }
  56. // collection with supported methods
  57. func (self *personalApi) Methods() []string {
  58. methods := make([]string, len(self.methods))
  59. i := 0
  60. for k := range self.methods {
  61. methods[i] = k
  62. i++
  63. }
  64. return methods
  65. }
  66. // Execute given request
  67. func (self *personalApi) Execute(req *shared.Request) (interface{}, error) {
  68. if callback, ok := self.methods[req.Method]; ok {
  69. return callback(self, req)
  70. }
  71. return nil, shared.NewNotImplementedError(req.Method)
  72. }
  73. func (self *personalApi) Name() string {
  74. return shared.PersonalApiName
  75. }
  76. func (self *personalApi) ApiVersion() string {
  77. return PersonalApiVersion
  78. }
  79. func (self *personalApi) ListAccounts(req *shared.Request) (interface{}, error) {
  80. return self.xeth.Accounts(), nil
  81. }
  82. func (self *personalApi) NewAccount(req *shared.Request) (interface{}, error) {
  83. args := new(NewAccountArgs)
  84. if err := self.codec.Decode(req.Params, &args); err != nil {
  85. return nil, shared.NewDecodeParamError(err.Error())
  86. }
  87. am := self.ethereum.AccountManager()
  88. acc, err := am.NewAccount(args.Passphrase)
  89. return acc.Address.Hex(), err
  90. }
  91. func (self *personalApi) DeleteAccount(req *shared.Request) (interface{}, error) {
  92. args := new(DeleteAccountArgs)
  93. if err := self.codec.Decode(req.Params, &args); err != nil {
  94. return nil, shared.NewDecodeParamError(err.Error())
  95. }
  96. addr := common.HexToAddress(args.Address)
  97. am := self.ethereum.AccountManager()
  98. if err := am.DeleteAccount(addr, args.Passphrase); err == nil {
  99. return true, nil
  100. } else {
  101. return false, err
  102. }
  103. }
  104. func (self *personalApi) UnlockAccount(req *shared.Request) (interface{}, error) {
  105. args := new(UnlockAccountArgs)
  106. if err := self.codec.Decode(req.Params, &args); err != nil {
  107. return nil, shared.NewDecodeParamError(err.Error())
  108. }
  109. if len(args.Passphrase) == 0 {
  110. fe := self.xeth.Frontend()
  111. if fe == nil {
  112. return false, fmt.Errorf("No password provided")
  113. }
  114. return fe.UnlockAccount(common.HexToAddress(args.Address).Bytes()), nil
  115. }
  116. am := self.ethereum.AccountManager()
  117. addr := common.HexToAddress(args.Address)
  118. err := am.TimedUnlock(addr, args.Passphrase, time.Duration(args.Duration)*time.Second)
  119. return err == nil, err
  120. }