-
Notifications
You must be signed in to change notification settings - Fork 2
/
dht_namespace_pk_validator.go
61 lines (48 loc) · 1.52 KB
/
dht_namespace_pk_validator.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 crabfs
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
libp2pRecord "github.com/libp2p/go-libp2p-record"
crabfsCrypto "github.com/runletapp/crabfs/crypto"
)
// DHTNamespacePKValidatorNew creates a new validator that validates for all versions
func DHTNamespacePKValidatorNew() libp2pRecord.Validator {
return DHTNamespacePKValidatorV1{}
}
// DHTNamespacePKValidatorV1 validates the /crabfs keys on the dht datastore
type DHTNamespacePKValidatorV1 struct {
}
// Validate validates the given record, returning an error if it's
// invalid (e.g., expired, signed by the wrong key, etc.).
func (validator DHTNamespacePKValidatorV1) Validate(key string, value []byte) error {
parts := strings.Split(key, "/")
if len(parts) != 3 {
return fmt.Errorf("Invalid key. Expexted format: /crabfs_pk/<hash> Got: %v", key)
}
if _, err := crabfsCrypto.UnmarshalPublicKey(value); err != nil {
return err
}
hash := sha256.New()
_, err := hash.Write(value)
if err != nil {
return err
}
pskHash := hash.Sum(nil)
expectedKey := fmt.Sprintf("/crabfs_pk/%s", hex.EncodeToString(pskHash))
if !strings.HasPrefix(key, expectedKey) {
return fmt.Errorf("Invalid key. Expexted: %s Got: %v", expectedKey, key)
}
return nil
}
// Select selects the best record from the set of records (e.g., the
// newest).
//
// Decisions made by select should be stable.
func (validator DHTNamespacePKValidatorV1) Select(key string, values [][]byte) (int, error) {
if len(values) == 0 {
return 0, fmt.Errorf("No values")
}
return 0, nil
}