-
Notifications
You must be signed in to change notification settings - Fork 26
/
contact.go
75 lines (57 loc) · 1.61 KB
/
contact.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
package protocoltypes
import (
"fmt"
"github.com/libp2p/go-libp2p/core/crypto"
"berty.tech/weshnet/pkg/errcode"
)
const RendezvousSeedLength = 32
type ShareableContactOptions uint64
const (
shareableContactOptionsUndefined = iota
ShareableContactOptionsAllowMissingRDVSeed
ShareableContactOptionsAllowMissingPK
)
var _ = shareableContactOptionsUndefined
func (m *ShareableContact) CheckFormat(options ...ShareableContactOptions) error {
var (
optionMissingPKAllowed = false
optionMissingRDVSeedAllowed = false
)
for _, o := range options {
if o == ShareableContactOptionsAllowMissingPK {
optionMissingPKAllowed = true
}
if o == ShareableContactOptionsAllowMissingRDVSeed {
optionMissingRDVSeedAllowed = true
}
}
if l := len(m.PublicRendezvousSeed); l != RendezvousSeedLength {
if !(l == 0 && optionMissingRDVSeedAllowed) {
return errcode.ErrInvalidInput.Wrap(fmt.Errorf("rendezvous seed length should not be %d", l))
}
}
if l := len(m.PK); l == 0 && !optionMissingPKAllowed {
return errcode.ErrInvalidInput.Wrap(fmt.Errorf("contact public key is missing"))
}
if l := len(m.PK); l != 0 {
_, err := crypto.UnmarshalEd25519PublicKey(m.PK)
if err != nil {
return errcode.ErrDeserialization.Wrap(err)
}
}
return nil
}
func (m *ShareableContact) IsSamePK(otherPK crypto.PubKey) bool {
pk, err := m.GetPubKey()
if err != nil {
return false
}
return otherPK.Equals(pk)
}
func (m *ShareableContact) GetPubKey() (crypto.PubKey, error) {
pk, err := crypto.UnmarshalEd25519PublicKey(m.PK)
if err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
return pk, nil
}