/
group.go
89 lines (68 loc) · 1.71 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package protocoltypes
import (
"encoding/hex"
"github.com/libp2p/go-libp2p-core/crypto"
"golang.org/x/crypto/ed25519"
"berty.tech/berty/v2/go/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) GetSharedSecret() *[32]byte {
sharedSecret := [32]byte{}
copy(sharedSecret[:], m.Secret)
return &sharedSecret
}
func (m *Group) FilterForReplication() (*Group, error) {
groupSigPK, err := m.GetSigningPubKey()
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
groupSigPKBytes, err := groupSigPK.Raw()
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
return &Group{
PublicKey: m.PublicKey,
SignPub: groupSigPKBytes,
}, nil
}