github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/accounts/pluggable/backend.go (about)

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