/
validate.go
70 lines (54 loc) · 1.16 KB
/
validate.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
package name
import (
"errors"
"github.com/libp2p/go-libp2p-core/peer"
record "github.com/libp2p/go-libp2p-record"
)
var _ record.Validator = (*Validator)(nil)
// Validator ensures records are valid.
type Validator struct{}
// Validate ensures that the signature matches the topic id.
func (v Validator) Validate(key string, value []byte) error {
ns, k, err := record.SplitKey(key)
if err != nil {
return err
}
if ns != Namespace {
return record.ErrInvalidRecordType
}
peerID, err := peer.Decode(k)
if err != nil {
return err
}
pub, err := peerID.ExtractPublicKey()
if err != nil {
return err
}
rec, err := RecordFromCBOR(value)
if err != nil {
return err
}
match, err := rec.Verify(pub)
if err != nil {
return err
}
if !match {
return errors.New("signature does not match")
}
return nil
}
// Select finds the best record by comparing sequence numbers.
func (v Validator) Select(key string, vals [][]byte) (int, error) {
var ind int
var max uint64
for i, v := range vals {
rec, err := RecordFromCBOR(v)
if err != nil {
return -1, err
}
if rec.Sequence >= max {
ind, max = i, rec.Sequence
}
}
return ind, nil
}