backend.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package pluggable
  2. import (
  3. "reflect"
  4. "time"
  5. "github.com/ethereum/go-ethereum/accounts"
  6. "github.com/ethereum/go-ethereum/event"
  7. plugin "github.com/ethereum/go-ethereum/plugin/account"
  8. )
  9. var BackendType = reflect.TypeOf(&Backend{})
  10. type Backend struct {
  11. wallets []accounts.Wallet
  12. }
  13. func NewBackend() *Backend {
  14. return &Backend{
  15. wallets: []accounts.Wallet{
  16. &wallet{
  17. url: accounts.URL{
  18. Scheme: "plugin",
  19. Path: "account",
  20. },
  21. },
  22. },
  23. }
  24. }
  25. func (b *Backend) Wallets() []accounts.Wallet {
  26. cpy := make([]accounts.Wallet, len(b.wallets))
  27. copy(cpy, b.wallets)
  28. return cpy
  29. }
  30. // Subscribe implements accounts.Backend, creating a new subscription that is a no-op and simply exits when the Unsubscribe is called
  31. func (b *Backend) Subscribe(_ chan<- accounts.WalletEvent) event.Subscription {
  32. return event.NewSubscription(func(quit <-chan struct{}) error {
  33. <-quit
  34. return nil
  35. })
  36. }
  37. func (b *Backend) SetPluginService(s plugin.Service) error {
  38. return b.wallet().setPluginService(s)
  39. }
  40. func (b *Backend) TimedUnlock(account accounts.Account, password string, duration time.Duration) error {
  41. return b.wallet().timedUnlock(account, password, duration)
  42. }
  43. func (b *Backend) Lock(account accounts.Account) error {
  44. return b.wallet().lock(account)
  45. }
  46. // AccountCreator is the interface that wraps the plugin account creation methods.
  47. // This interface is used to simplify the pluggable.Backend API available to the account plugin CLI and enables easier testing.
  48. type AccountCreator interface {
  49. NewAccount(newAccountConfig interface{}) (accounts.Account, error)
  50. ImportRawKey(rawKey string, newAccountConfig interface{}) (accounts.Account, error)
  51. }
  52. func (b *Backend) NewAccount(newAccountConfig interface{}) (accounts.Account, error) {
  53. return b.wallet().newAccount(newAccountConfig)
  54. }
  55. func (b *Backend) ImportRawKey(rawKey string, newAccountConfig interface{}) (accounts.Account, error) {
  56. return b.wallet().importRawKey(rawKey, newAccountConfig)
  57. }
  58. func (b *Backend) wallet() *wallet {
  59. return b.wallets[0].(*wallet)
  60. }