-
Notifications
You must be signed in to change notification settings - Fork 968
/
types.go
159 lines (136 loc) · 4.83 KB
/
types.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package keymanager
import (
"context"
"fmt"
"strings"
"github.com/prysmaticlabs/prysm/v5/async/event"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/crypto/bls"
validatorpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1/validator-client"
)
// IKeymanager defines a general keymanager interface for Prysm wallets.
type IKeymanager interface {
PublicKeysFetcher
Signer
KeyChangeSubscriber
KeyStoreExtractor
AccountLister
Deleter
}
// KeysFetcher for validating private and public keys.
type KeysFetcher interface {
FetchValidatingPrivateKeys(ctx context.Context) ([][32]byte, error)
PublicKeysFetcher
}
// PublicKeysFetcher for validating public keys.
type PublicKeysFetcher interface {
FetchValidatingPublicKeys(ctx context.Context) ([][fieldparams.BLSPubkeyLength]byte, error)
}
// Signer allows signing messages using a validator private key.
type Signer interface {
Sign(context.Context, *validatorpb.SignRequest) (bls.Signature, error)
}
// Importer can import new keystores into the keymanager.
type Importer interface {
ImportKeystores(
ctx context.Context, keystores []*Keystore, passwords []string,
) ([]*KeyStatus, error)
}
// Deleter can delete keystores from the keymanager.
type Deleter interface {
DeleteKeystores(ctx context.Context, publicKeys [][]byte) ([]*KeyStatus, error)
}
// KeyChangeSubscriber allows subscribing to changes made to the underlying keys.
type KeyChangeSubscriber interface {
SubscribeAccountChanges(pubKeysChan chan [][fieldparams.BLSPubkeyLength]byte) event.Subscription
}
// KeyStoreExtractor allows keys to be extracted from the keymanager.
type KeyStoreExtractor interface {
ExtractKeystores(ctx context.Context, publicKeys []bls.PublicKey, password string) ([]*Keystore, error)
}
// PublicKeyAdder allows adding public keys to the keymanager.
type PublicKeyAdder interface {
AddPublicKeys(publicKeys []string) []*KeyStatus
}
// KeyStatus is a json representation of the status fields for the keymanager apis
type KeyStatus struct {
Status KeyStatusType `json:"status"`
Message string `json:"message"`
}
// KeyStatusType is a category of key status
type KeyStatusType string
const (
StatusImported KeyStatusType = "IMPORTED"
StatusError KeyStatusType = "ERROR"
StatusDuplicate KeyStatusType = "DUPLICATE"
StatusUnknown KeyStatusType = "UNKNOWN"
StatusNotFound KeyStatusType = "NOT_FOUND"
StatusDeleted KeyStatusType = "DELETED"
StatusNotActive KeyStatusType = "NOT_ACTIVE"
)
// PublicKeyDeleter allows deleting public keys set in keymanager.
type PublicKeyDeleter interface {
DeletePublicKeys(publicKeys []string) []*KeyStatus
}
type ListKeymanagerAccountConfig struct {
ShowPrivateKeys bool
WalletAccountsDir string
KeymanagerConfigFileName string
}
type AccountLister interface {
ListKeymanagerAccounts(ctx context.Context, cfg ListKeymanagerAccountConfig) error
}
// Keystore json file representation as a Go struct.
type Keystore struct {
Crypto map[string]interface{} `json:"crypto"`
ID string `json:"uuid"`
Pubkey string `json:"pubkey"`
Version uint `json:"version"`
Description string `json:"description"`
Name string `json:"name,omitempty"` // field deprecated in favor of description, EIP2335
Path string `json:"path"`
}
// Kind defines an enum for either local, derived, or remote-signing
// keystores for Prysm wallets.
type Kind int
const (
// Local keymanager defines an on-disk, encrypted keystore-capable store.
Local Kind = iota
// Derived keymanager using a hierarchical-deterministic algorithm.
Derived
// Web3Signer keymanager capable of signing data using a remote signer called Web3Signer.
Web3Signer
)
// IncorrectPasswordErrMsg defines a common error string representing an EIP-2335
// keystore password was incorrect.
const IncorrectPasswordErrMsg = "invalid checksum"
// String marshals a keymanager kind to a string value.
func (k Kind) String() string {
switch k {
case Derived:
return "derived"
case Local:
// TODO(#10181) need a safe way to migrate away from using direct.
// function is used for directory creation, dangerous to change which may result in multiple directories.
// multiple directories will cause the isValid function to fail in wallet.go
// and may result in using a unintended wallet.
return "direct"
case Web3Signer:
return "web3signer"
default:
return fmt.Sprintf("%d", int(k))
}
}
// ParseKind from a raw string, returning a keymanager kind.
func ParseKind(k string) (Kind, error) {
switch strings.ToLower(k) {
case "derived":
return Derived, nil
case "direct", "imported", "local":
return Local, nil
case "web3signer":
return Web3Signer, nil
default:
return 0, fmt.Errorf("%s is not an allowed keymanager", k)
}
}