-
Notifications
You must be signed in to change notification settings - Fork 11
/
pubkey.go
55 lines (47 loc) · 1.26 KB
/
pubkey.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
package record
import (
"bytes"
"errors"
"fmt"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
mh "github.com/multiformats/go-multihash"
)
// PublicKeyValidator is a Validator that validates public keys.
type PublicKeyValidator struct{}
// Validate conforms to the Validator interface.
//
// It verifies that the passed in record value is the PublicKey that matches the
// passed in key.
func (pkv PublicKeyValidator) Validate(key string, value []byte) error {
ns, key, err := SplitKey(key)
if err != nil {
return err
}
if ns != "pk" {
return errors.New("namespace not 'pk'")
}
keyhash := []byte(key)
if _, err := mh.Cast(keyhash); err != nil {
return fmt.Errorf("key did not contain valid multihash: %s", err)
}
pk, err := crypto.UnmarshalPublicKey(value)
if err != nil {
return err
}
id, err := peer.IDFromPublicKey(pk)
if err != nil {
return err
}
if !bytes.Equal(keyhash, []byte(id)) {
return errors.New("public key does not match storage key")
}
return nil
}
// Select conforms to the Validator interface.
//
// It always returns 0 as all public keys are equivalently valid.
func (pkv PublicKeyValidator) Select(k string, vals [][]byte) (int, error) {
return 0, nil
}
var _ Validator = PublicKeyValidator{}