github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/plugin/account/reloadableimpl.go (about) 1 package account 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/kisexp/xdchain/accounts" 8 "github.com/kisexp/xdchain/log" 9 ) 10 11 type DispenseFunc func() (Service, error) 12 13 type ReloadableService struct { 14 DispenseFunc DispenseFunc 15 } 16 17 func (am *ReloadableService) Status(ctx context.Context) (string, error) { 18 s, err := am.DispenseFunc() 19 if err != nil { 20 return "", err 21 } 22 return s.Status(ctx) 23 } 24 25 func (am *ReloadableService) Open(ctx context.Context, passphrase string) error { 26 s, err := am.DispenseFunc() 27 if err != nil { 28 return err 29 } 30 return s.Open(ctx, passphrase) 31 } 32 33 func (am *ReloadableService) Close(ctx context.Context) error { 34 s, err := am.DispenseFunc() 35 if err != nil { 36 return err 37 } 38 return s.Close(ctx) 39 } 40 41 func (am *ReloadableService) Accounts(ctx context.Context) []accounts.Account { 42 s, err := am.DispenseFunc() 43 if err != nil { 44 log.Error("unable to dispense account plugin", "err", err) 45 return []accounts.Account{} 46 } 47 return s.Accounts(ctx) 48 } 49 50 func (am *ReloadableService) Contains(ctx context.Context, account accounts.Account) bool { 51 s, err := am.DispenseFunc() 52 if err != nil { 53 log.Error("unable to dispense account plugin", "err", err) 54 return false 55 } 56 return s.Contains(ctx, account) 57 } 58 59 func (am *ReloadableService) Sign(ctx context.Context, account accounts.Account, toSign []byte) ([]byte, error) { 60 s, err := am.DispenseFunc() 61 if err != nil { 62 return nil, err 63 } 64 return s.Sign(ctx, account, toSign) 65 } 66 67 func (am *ReloadableService) UnlockAndSign(ctx context.Context, account accounts.Account, toSign []byte, passphrase string) ([]byte, error) { 68 s, err := am.DispenseFunc() 69 if err != nil { 70 return nil, err 71 } 72 return s.UnlockAndSign(ctx, account, toSign, passphrase) 73 } 74 75 func (am *ReloadableService) TimedUnlock(ctx context.Context, account accounts.Account, password string, duration time.Duration) error { 76 s, err := am.DispenseFunc() 77 if err != nil { 78 return err 79 } 80 return s.TimedUnlock(ctx, account, password, duration) 81 } 82 83 func (am *ReloadableService) Lock(ctx context.Context, account accounts.Account) error { 84 s, err := am.DispenseFunc() 85 if err != nil { 86 return err 87 } 88 return s.Lock(ctx, account) 89 } 90 91 func (am *ReloadableService) NewAccount(ctx context.Context, newAccountConfig interface{}) (accounts.Account, error) { 92 s, err := am.DispenseFunc() 93 if err != nil { 94 return accounts.Account{}, err 95 } 96 return s.NewAccount(ctx, newAccountConfig) 97 } 98 99 func (am *ReloadableService) ImportRawKey(ctx context.Context, rawKey string, newAccountConfig interface{}) (accounts.Account, error) { 100 s, err := am.DispenseFunc() 101 if err != nil { 102 return accounts.Account{}, err 103 } 104 return s.ImportRawKey(ctx, rawKey, newAccountConfig) 105 }