-
Notifications
You must be signed in to change notification settings - Fork 378
/
group.go
63 lines (48 loc) · 1.23 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
package bertytypes
import (
"encoding/hex"
"berty.tech/berty/v2/go/pkg/errcode"
"github.com/libp2p/go-libp2p-core/crypto"
"golang.org/x/crypto/ed25519"
)
func (m *Group) GetSigningPrivKey() (crypto.PrivKey, error) {
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) {
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, error) {
sharedSecret := [32]byte{}
copy(sharedSecret[:], m.Secret)
return &sharedSecret, nil
}