-
Notifications
You must be signed in to change notification settings - Fork 26
/
group.go
75 lines (58 loc) · 1.41 KB
/
group.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 (
"encoding/hex"
"github.com/libp2p/go-libp2p/core/crypto"
"golang.org/x/crypto/ed25519"
"berty.tech/weshnet/pkg/errcode"
)
func (m *Group) GetSigningPrivKey() (crypto.PrivKey, error) {
if len(m.Secret) == 0 {
return nil, errcode.ErrMissingInput
}
edSK := ed25519.NewKeyFromSeed(m.Secret)
sk, _, err := crypto.KeyPairFromStdKey(&edSK)
if err != nil {
return nil, err
}
return sk, nil
}
func (m *Group) GetPubKey() (crypto.PubKey, error) {
return crypto.UnmarshalEd25519PublicKey(m.PublicKey)
}
func (m *Group) GetSigningPubKey() (crypto.PubKey, error) {
if len(m.SignPub) != 0 {
return crypto.UnmarshalEd25519PublicKey(m.SignPub)
}
sk, err := m.GetSigningPrivKey()
if err != nil {
return nil, err
}
return sk.GetPublic(), nil
}
func (m *Group) IsValid() error {
pk, err := m.GetPubKey()
if err != nil {
return errcode.ErrDeserialization.Wrap(err)
}
ok, err := pk.Verify(m.Secret, m.SecretSig)
if err != nil {
return errcode.ErrCryptoSignatureVerification.Wrap(err)
}
if !ok {
return errcode.ErrCryptoSignatureVerification
}
return nil
}
// GroupIDAsString returns the group pub key as a string
func (m *Group) GroupIDAsString() string {
return hex.EncodeToString(m.PublicKey)
}
func (m *Group) Copy() *Group {
return &Group{
PublicKey: m.PublicKey,
Secret: m.Secret,
SecretSig: m.SecretSig,
GroupType: m.GroupType,
SignPub: m.SignPub,
}
}