-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmem.go
61 lines (52 loc) · 1.38 KB
/
mem.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
package valkeystore
import (
"errors"
"github.com/ethereum/go-ethereum/crypto"
"github.com/Fantom-foundation/go-opera/inter/validatorpk"
"github.com/Fantom-foundation/go-opera/valkeystore/encryption"
)
type MemKeystore struct {
mem map[string]*encryption.PrivateKey
auth map[string]string
}
func NewMemKeystore() *MemKeystore {
return &MemKeystore{
mem: make(map[string]*encryption.PrivateKey),
auth: make(map[string]string),
}
}
func (m *MemKeystore) Has(pubkey validatorpk.PubKey) bool {
_, ok := m.mem[m.idxOf(pubkey)]
return ok
}
func (m *MemKeystore) Add(pubkey validatorpk.PubKey, key []byte, auth string) error {
if m.Has(pubkey) {
return ErrAlreadyExists
}
if pubkey.Type != validatorpk.Types.Secp256k1 {
return encryption.ErrNotSupportedType
}
decoded, err := crypto.ToECDSA(key)
if err != nil {
return err
}
m.mem[m.idxOf(pubkey)] = &encryption.PrivateKey{
Type: pubkey.Type,
Bytes: key,
Decoded: decoded,
}
m.auth[m.idxOf(pubkey)] = auth
return nil
}
func (m *MemKeystore) Get(pubkey validatorpk.PubKey, auth string) (*encryption.PrivateKey, error) {
if !m.Has(pubkey) {
return nil, ErrNotFound
}
if m.auth[m.idxOf(pubkey)] != auth {
return nil, errors.New("could not decrypt key with given password")
}
return m.mem[m.idxOf(pubkey)], nil
}
func (m *MemKeystore) idxOf(pubkey validatorpk.PubKey) string {
return string(pubkey.Bytes())
}