forked from textileio/go-threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keybook.go
196 lines (171 loc) · 3.95 KB
/
keybook.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package lstoremem
import (
"errors"
"sync"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
core "github.com/singyiu/go-threads/core/logstore"
"github.com/singyiu/go-threads/core/thread"
sym "github.com/singyiu/go-threads/crypto/symmetric"
)
type memoryKeyBook struct {
sync.RWMutex
pks map[thread.ID]map[peer.ID]crypto.PubKey
sks map[thread.ID]map[peer.ID]crypto.PrivKey
rks map[thread.ID][]byte
fks map[thread.ID][]byte
}
func (mkb *memoryKeyBook) getPubKey(t thread.ID, p peer.ID) (crypto.PubKey, bool) {
lmap, found := mkb.pks[t]
if lmap == nil {
return nil, found
}
hmap, found := lmap[p]
return hmap, found
}
func (mkb *memoryKeyBook) getPrivKey(t thread.ID, p peer.ID) (crypto.PrivKey, bool) {
lmap, found := mkb.sks[t]
if lmap == nil {
return nil, found
}
hmap, found := lmap[p]
return hmap, found
}
var _ core.KeyBook = (*memoryKeyBook)(nil)
func NewKeyBook() core.KeyBook {
return &memoryKeyBook{
pks: map[thread.ID]map[peer.ID]crypto.PubKey{},
sks: map[thread.ID]map[peer.ID]crypto.PrivKey{},
rks: map[thread.ID][]byte{},
fks: map[thread.ID][]byte{},
}
}
func (mkb *memoryKeyBook) PubKey(t thread.ID, p peer.ID) (crypto.PubKey, error) {
mkb.RLock()
pk, _ := mkb.getPubKey(t, p)
mkb.RUnlock()
return pk, nil
}
func (mkb *memoryKeyBook) AddPubKey(t thread.ID, p peer.ID, pk crypto.PubKey) error {
// check it's correct first
if !p.MatchesPublicKey(pk) {
return errors.New("ID does not match PublicKey")
}
mkb.Lock()
if mkb.pks[t] == nil {
mkb.pks[t] = make(map[peer.ID]crypto.PubKey, 1)
}
mkb.pks[t][p] = pk
mkb.Unlock()
return nil
}
func (mkb *memoryKeyBook) PrivKey(t thread.ID, p peer.ID) (crypto.PrivKey, error) {
mkb.RLock()
sk, _ := mkb.getPrivKey(t, p)
mkb.RUnlock()
return sk, nil
}
func (mkb *memoryKeyBook) AddPrivKey(t thread.ID, p peer.ID, sk crypto.PrivKey) error {
if sk == nil {
return errors.New("sk is nil (PrivKey)")
}
// check it's correct first
if !p.MatchesPrivateKey(sk) {
return errors.New("ID does not match PrivateKey")
}
mkb.Lock()
if mkb.sks[t] == nil {
mkb.sks[t] = make(map[peer.ID]crypto.PrivKey, 1)
}
mkb.sks[t][p] = sk
mkb.Unlock()
return nil
}
func (mkb *memoryKeyBook) ReadKey(t thread.ID) (key *sym.Key, err error) {
mkb.RLock()
b := mkb.rks[t]
if b != nil {
key, err = sym.FromBytes(b)
}
mkb.RUnlock()
return key, err
}
func (mkb *memoryKeyBook) AddReadKey(t thread.ID, key *sym.Key) error {
if key == nil {
return errors.New("key is nil (ReadKey)")
}
mkb.Lock()
mkb.rks[t] = key.Bytes()
mkb.Unlock()
return nil
}
func (mkb *memoryKeyBook) ServiceKey(t thread.ID) (key *sym.Key, err error) {
mkb.RLock()
b := mkb.fks[t]
if b != nil {
key, err = sym.FromBytes(b)
}
mkb.RUnlock()
return
}
func (mkb *memoryKeyBook) AddServiceKey(t thread.ID, key *sym.Key) error {
if key == nil {
return errors.New("key is nil (ServiceKey)")
}
mkb.Lock()
mkb.fks[t] = key.Bytes()
mkb.Unlock()
return nil
}
func (mkb *memoryKeyBook) ClearKeys(t thread.ID) error {
mkb.Lock()
delete(mkb.pks, t)
delete(mkb.sks, t)
delete(mkb.rks, t)
delete(mkb.fks, t)
mkb.Unlock()
return nil
}
func (mkb *memoryKeyBook) ClearLogKeys(t thread.ID, _ peer.ID) error {
mkb.Lock()
delete(mkb.pks, t)
delete(mkb.sks, t)
mkb.Unlock()
return nil
}
func (mkb *memoryKeyBook) LogsWithKeys(t thread.ID) (peer.IDSlice, error) {
mkb.RLock()
ps := make(map[peer.ID]struct{})
if mkb.pks[t] != nil {
for p := range mkb.pks[t] {
ps[p] = struct{}{}
}
}
if mkb.sks[t] != nil {
for p := range mkb.sks[t] {
ps[p] = struct{}{}
}
}
mkb.RUnlock()
var pids peer.IDSlice
for p := range ps {
pids = append(pids, p)
}
return pids, nil
}
func (mkb *memoryKeyBook) ThreadsFromKeys() (thread.IDSlice, error) {
mkb.RLock()
ts := make(map[thread.ID]struct{})
for t := range mkb.pks {
ts[t] = struct{}{}
}
for t := range mkb.sks {
ts[t] = struct{}{}
}
mkb.RUnlock()
var tids thread.IDSlice
for t := range ts {
tids = append(tids, t)
}
return tids, nil
}