-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
179 lines (143 loc) · 3.42 KB
/
store.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
package memory
import (
"context"
"sort"
"sync"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/vault"
)
type store struct {
mu sync.Mutex
records []*vault.Record
last uint64
secret string
}
type ById []*vault.Record
func (a ById) Len() int { return len(a) }
func (a ById) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ById) Less(i, j int) bool { return a[i].Id < a[j].Id }
func New() vault.Store {
return &store{
records: make([]*vault.Record, 0),
last: 0,
secret: vault.GetSecret(),
}
}
func (s *store) reset() {
s.mu.Lock()
s.records = make([]*vault.Record, 0)
s.last = 0
s.mu.Unlock()
}
func (s *store) find(data *vault.Record) *vault.Record {
for _, item := range s.records {
if item.Id == data.Id {
return item
}
if item.PublicKey == data.PublicKey {
return item
}
}
return nil
}
func (s *store) findPublicKey(pubkey string) *vault.Record {
for _, item := range s.records {
if item.PublicKey == pubkey {
return item
}
}
return nil
}
func (s *store) findByState(state vault.State) []*vault.Record {
res := make([]*vault.Record, 0)
for _, item := range s.records {
if item.State == state {
res = append(res, item)
continue
}
}
return res
}
func (s *store) filter(items []*vault.Record, cursor query.Cursor, limit uint64, direction query.Ordering) []*vault.Record {
var start uint64
start = 0
if direction == query.Descending {
start = s.last + 1
}
if len(cursor) > 0 {
start = cursor.ToUint64()
}
var res []*vault.Record
for _, item := range items {
if item.Id > start && direction == query.Ascending {
res = append(res, item)
}
if item.Id < start && direction == query.Descending {
res = append(res, item)
}
}
if direction == query.Descending {
sort.Sort(sort.Reverse(ById(res)))
}
if len(res) >= int(limit) {
return res[:limit]
}
return res
}
func (s *store) Count(ctx context.Context) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
return uint64(len(s.records)), nil
}
func (s *store) CountByState(ctx context.Context, state vault.State) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
res := s.findByState(state)
return uint64(len(res)), nil
}
func (s *store) Save(ctx context.Context, data *vault.Record) error {
s.mu.Lock()
defer s.mu.Unlock()
s.last++
if item := s.find(data); item != nil {
item.State = data.State
} else {
if data.Id == 0 {
data.Id = s.last
}
c := data.Clone()
val, err := vault.Encrypt(c.PrivateKey, c.PublicKey)
if err != nil {
return err
}
c.PrivateKey = val
s.records = append(s.records, &c)
}
return nil
}
func (s *store) Get(ctx context.Context, sig string) (*vault.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
if item := s.findPublicKey(sig); item != nil {
val, err := vault.Decrypt(item.PrivateKey, item.PublicKey)
if err != nil {
return nil, err
}
cloned := item.Clone()
cloned.PrivateKey = val
return &cloned, nil
}
return nil, vault.ErrKeyNotFound
}
func (s *store) GetAllByState(ctx context.Context, state vault.State, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*vault.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
if items := s.findByState(state); len(items) > 0 {
res := s.filter(items, cursor, limit, direction)
if len(res) == 0 {
return nil, vault.ErrKeyNotFound
}
return res, nil
}
return nil, vault.ErrKeyNotFound
}