/
badger.go
544 lines (474 loc) · 13 KB
/
badger.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package db
import (
"bytes"
"fmt"
"strings"
"github.com/regnull/easyecc"
"github.com/btcsuite/btcutil/base58"
"github.com/dgraph-io/badger/v3"
"github.com/regnull/ubikom/pb"
"github.com/regnull/ubikom/protoio"
"github.com/regnull/ubikom/util"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"
)
const (
keyPrefix = "pkey_"
namePrefix = "name_"
maxParentKeys = 1 // For now, only a single parent is allowed.
addressPrefix = "address_"
)
type BadgerDB struct {
db *badger.DB
}
func NewBadgerDB(db *badger.DB) *BadgerDB {
return &BadgerDB{db: db}
}
func (b *BadgerDB) RegisterKey(publicKey *easyecc.PublicKey) error {
publicKeyBase58 := base58.Encode(publicKey.SerializeCompressed())
log.Info().Str("key", publicKeyBase58).Msg("registering public key")
dbKey := keyPrefix + publicKeyBase58
err := b.db.Update(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(dbKey))
if item != nil {
return ErrRecordExists
}
// Register the key.
rec := &pb.KeyRecord{
RegistrationTimestamp: util.NowMs()}
recBytes, err := proto.Marshal(rec)
if err != nil {
return fmt.Errorf("error marshaling key record: %w", err)
}
err = txn.Set([]byte(dbKey), recBytes)
if err != nil {
return err
}
// Register key address as name.
nameKey := namePrefix + publicKey.Address()
err = txn.Set([]byte(nameKey), publicKey.SerializeCompressed())
if err != nil {
return err
}
log.Info().Str("name", publicKey.Address()).Msg("name registered")
return nil
})
return err
}
func (b *BadgerDB) RegisterKeyParent(childKey *easyecc.PublicKey, parentKey *easyecc.PublicKey) error {
childBase58 := base58.Encode(childKey.SerializeCompressed())
childDbKey := keyPrefix + childBase58
err := b.db.Update(func(txn *badger.Txn) error {
// Retrieve the key.
item, err := txn.Get([]byte(childDbKey))
if err != nil {
return fmt.Errorf("error getting key record: %w", err)
}
if item == nil {
return ErrNotFound
}
keyRec := &pb.KeyRecord{}
err = item.Value(func(val []byte) error {
err := proto.Unmarshal(val, keyRec)
if err != nil {
return fmt.Errorf("failed to unmarshal key record: %w", err)
}
return nil
})
// Add parent to the key record.
for _, p := range keyRec.GetParentKey() {
if bytes.Compare(parentKey.SerializeCompressed(), p) == 0 {
// This key is already registered as a parent.
return ErrRecordExists
}
}
keyRec.ParentKey = append(keyRec.ParentKey, parentKey.SerializeCompressed())
if len(keyRec.GetParentKey()) > maxParentKeys {
return fmt.Errorf("too many parent keys")
}
// Update key registration.
recBytes, err := proto.Marshal(keyRec)
if err != nil {
return fmt.Errorf("error marshaling key record: %w", err)
}
err = txn.Set([]byte(childDbKey), recBytes)
if err != nil {
return err
}
return nil
})
return err
}
func (b *BadgerDB) RegisterName(originator, target *easyecc.PublicKey, name string) error {
name = strings.ToLower(name)
dbKey := namePrefix + name
err := b.db.Update(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(dbKey))
var prev *easyecc.PublicKey
if item != nil {
// If the name is already registered, we can change the registration,
// as long as we are authorized to do so.
var previousKeyBytes [33]byte
err = item.Value(func(val []byte) error {
if copy(previousKeyBytes[:], val) != 33 {
return fmt.Errorf("invalid name registration record")
}
return nil
})
if err != nil {
return err
}
prev, err = easyecc.NewPublicFromSerializedCompressed(previousKeyBytes[:])
if err != nil {
return err
}
}
authorized, err := CheckRegisterNameAuthorization(txn, originator, target, prev)
if err != nil {
return err
}
if !authorized {
return ErrNotAuthorized
}
err = txn.Set([]byte(dbKey), target.SerializeCompressed())
return err
})
return err
}
func (b *BadgerDB) RegisterAddress(originator, target *easyecc.PublicKey, name string, protocol pb.Protocol, address string) error {
name = strings.ToLower(name)
err := b.db.Update(func(txn *badger.Txn) error {
// If the target has a parent, it must be the parent who sends the request.
targetParent, err := GetParent(txn, target)
if err != nil {
if err != badger.ErrKeyNotFound {
return err
}
}
if targetParent != nil && !originator.Equal(targetParent) {
return ErrNotAuthorized
}
// Make sure the requested name is registered to this key.
nameKey := namePrefix + name
item, err := txn.Get([]byte(nameKey))
if err != nil {
return fmt.Errorf("error getting name, %w", err)
}
if item == nil {
return fmt.Errorf("name %s is not registered", name)
}
// This is the key associated with the name. It must match the target key.
var registeredKeyBytes []byte
err = item.Value(func(val []byte) error {
registeredKeyBytes = append([]byte{}, val...)
return nil
})
if err != nil {
return fmt.Errorf("error getting name, %w", err)
}
registeredKey, err := easyecc.NewPublicFromSerializedCompressed(registeredKeyBytes)
if err != nil {
return err
}
if !registeredKey.Equal(target) {
return ErrNotAuthorized
}
addressKey := addressPrefix + name + "_" + protocol.String()
err = txn.Set([]byte(addressKey), []byte(address))
return err
})
return err
}
func (b *BadgerDB) DisableKey(originator *easyecc.PublicKey, key *easyecc.PublicKey) error {
publicKeyBase58 := base58.Encode(key.SerializeCompressed())
dbKey := keyPrefix + publicKeyBase58
err := b.db.Update(func(txn *badger.Txn) error {
// If the target has a parent, it must be the parent who sends the request.
keyParent, err := GetParent(txn, key)
if err != nil {
if err != badger.ErrKeyNotFound {
return err
}
}
if keyParent != nil && !originator.Equal(keyParent) {
return ErrNotAuthorized
}
// Retrieve the key.
item, err := txn.Get([]byte(dbKey))
if err != nil {
return fmt.Errorf("error getting key record: %w", err)
}
if item == nil {
return ErrNotFound
}
keyRec := &pb.KeyRecord{}
err = item.Value(func(val []byte) error {
err := proto.Unmarshal(val, keyRec)
if err != nil {
return fmt.Errorf("failed to unmarshal key record: %w", err)
}
return nil
})
// Key if the originator key is authorized to make changes.
// For this operation to be authorized, the originator must be the key itself,
// or its parent.
authorized := false
if originator.Equal(key) {
authorized = true
} else {
for _, parentKey := range keyRec.GetParentKey() {
if originator.EqualSerializedCompressed(parentKey) {
authorized = true
break
}
}
}
if !authorized {
return ErrNotAuthorized
}
keyRec.Disabled = true
keyRec.DisabledTimestamp = util.NowMs()
keyRec.DisabledBy = originator.SerializeCompressed()
// Update key registration.
recBytes, err := proto.Marshal(keyRec)
if err != nil {
return fmt.Errorf("error marshaling key record: %w", err)
}
err = txn.Set([]byte(dbKey), recBytes)
if err != nil {
return err
}
return nil
})
return err
}
func (b *BadgerDB) GetKey(key *easyecc.PublicKey) (*pb.KeyRecord, error) {
dbKey := keyPrefix + base58.Encode(key.SerializeCompressed())
keyRec := &pb.KeyRecord{}
err := b.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(dbKey))
if err != nil {
if err == badger.ErrKeyNotFound {
return ErrNotFound
}
return fmt.Errorf("error getting key record: %w", err)
}
if item == nil {
return ErrNotFound
}
err = item.Value(func(val []byte) error {
err := proto.Unmarshal(val, keyRec)
if err != nil {
return fmt.Errorf("failed to unmarshal key record: %w", err)
}
return nil
})
return nil
})
if err != nil {
return nil, err
}
return keyRec, nil
}
func (b *BadgerDB) GetName(name string) (*easyecc.PublicKey, error) {
name = strings.ToLower(name)
var keyBytes []byte
err := b.db.View(func(txn *badger.Txn) error {
nameKey := namePrefix + name
item, err := txn.Get([]byte(nameKey))
if err != nil {
if err == badger.ErrKeyNotFound {
return ErrNotFound
}
return fmt.Errorf("error getting name, %w", err)
}
if item == nil {
return ErrNotFound
}
err = item.Value(func(val []byte) error {
keyBytes = append([]byte{}, val...)
return nil
})
return err
})
if err != nil {
return nil, err
}
key, err := easyecc.NewPublicFromSerializedCompressed(keyBytes)
if err != nil {
// Invalid key.
return nil, err
}
return key, nil
}
func (b *BadgerDB) GetAddress(name string, protocol pb.Protocol) (string, error) {
var addressBytes []byte
err := b.db.View(func(txn *badger.Txn) error {
addressKey := addressPrefix + name + "_" + protocol.String()
item, err := txn.Get([]byte(addressKey))
if err != nil {
return fmt.Errorf("error getting name, %w", err)
}
if item == nil {
return ErrNotFound
}
err = item.Value(func(val []byte) error {
addressBytes = append([]byte{}, val...)
return nil
})
return err
})
if err != nil {
return "", err
}
return string(addressBytes), nil
}
func CheckSelfOrParent(txn *badger.Txn, originator, target *easyecc.PublicKey) (bool, error) {
if originator.Equal(target) {
return true, nil
}
targetBase58 := base58.Encode(target.SerializeCompressed())
key := keyPrefix + targetBase58
item, err := txn.Get([]byte(key))
if err != nil {
return false, fmt.Errorf("error getting key record: %w", err)
}
if item == nil {
return false, fmt.Errorf("target key not found")
}
keyRec := &pb.KeyRecord{}
err = item.Value(func(val []byte) error {
err := proto.Unmarshal(val, keyRec)
if err != nil {
return fmt.Errorf("failed to unmarshal key record: %w", err)
}
return nil
})
if err != nil {
return false, fmt.Errorf("invalid key record: %w", err)
}
for _, p := range keyRec.GetParentKey() {
if originator.EqualSerializedCompressed(p) {
return true, nil
}
}
return false, nil
}
func GetParent(txn *badger.Txn, key *easyecc.PublicKey) (*easyecc.PublicKey, error) {
targetBase58 := base58.Encode(key.SerializeCompressed())
dbKey := keyPrefix + targetBase58
item, err := txn.Get([]byte(dbKey))
if err != nil {
return nil, fmt.Errorf("error getting key record: %w", err)
}
if item == nil {
return nil, fmt.Errorf("target key not found")
}
keyRec := &pb.KeyRecord{}
err = item.Value(func(val []byte) error {
err := proto.Unmarshal(val, keyRec)
if err != nil {
return fmt.Errorf("failed to unmarshal key record: %w", err)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("invalid key record: %w", err)
}
if len(keyRec.GetParentKey()) == 0 {
// No parent key.
return nil, nil
}
if len(keyRec.GetParentKey()) > 1 {
return nil, fmt.Errorf("invalid key record")
}
parent, err := easyecc.NewPublicFromSerializedCompressed(keyRec.GetParentKey()[0])
if err != nil {
return nil, fmt.Errorf("invalid parent key: %w", err)
}
return parent, nil
}
func CheckRegisterNameAuthorization(txn *badger.Txn, originator, target, prev *easyecc.PublicKey) (bool, error) {
var prevParent *easyecc.PublicKey
if prev != nil {
var err error
prevParent, err = GetParent(txn, prev)
if err != nil {
return false, err
}
}
targetParent, err := GetParent(txn, target)
if err != nil {
return false, err
}
// 1. Check if the originator has the authority.
// If the target has a parent, only the parent can request the change.
if targetParent != nil && !originator.Equal(targetParent) {
return false, nil
}
// The request must be for the same key, or for the child key.
if prev != nil {
if !originator.Equal(prev) && !originator.Equal(prevParent) {
return false, nil
}
}
// 2. Check if the change can be done.
// Can assign to a new key.
if prev == nil {
return true, nil
}
// Can re-assign name to the same key (noop).
if prev.Equal(target) {
return true, nil
}
// Can assign from child to parent.
if prevParent != nil && prevParent.Equal(target) {
return true, nil
}
// Can assign from parent to child.
if targetParent != nil && targetParent.Equal(prev) {
return true, nil
}
// Can assign to another child.
if targetParent != nil && targetParent.Equal(prevParent) {
return true, nil
}
// Nothing else can be done.
return false, nil
}
func (b *BadgerDB) WriteKeys(w protoio.Writer, cutoffTime uint64) error {
err := b.db.View(func(txn *badger.Txn) error {
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := []byte(keyPrefix)
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
name := string(item.Key())
publicKeyBase58 := name[len(keyPrefix):]
keyRec := &pb.KeyRecord{}
err := item.Value(func(val []byte) error {
err := proto.Unmarshal(val, keyRec)
if err != nil {
return fmt.Errorf("failed to unmarshal key record: %w", err)
}
return nil
})
if err != nil {
return err
}
// if keyRec.RegistrationTimestamp > int64(cutoffTime) {
// continue
// }
rec := &pb.DBRecord{
Name: publicKeyBase58,
Payload: &pb.DBRecord_Key{
Key: keyRec}}
err = w.Write(rec)
if err != nil {
return err
}
}
return nil
})
return err
}