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