-
Notifications
You must be signed in to change notification settings - Fork 378
/
orbitdb_signed_entry_keystore.go
73 lines (54 loc) · 1.45 KB
/
orbitdb_signed_entry_keystore.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
package bertyprotocol
import (
"encoding/hex"
"sync"
"github.com/libp2p/go-libp2p-core/crypto"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/go-ipfs-log/keystore"
)
type BertySignedKeyStore struct {
sync.Map
}
func (s *BertySignedKeyStore) SetKey(pk crypto.PrivKey) error {
pubKeyBytes, err := pk.GetPublic().Raw()
if err != nil {
return errcode.TODO.Wrap(err)
}
keyID := hex.EncodeToString(pubKeyBytes)
s.Store(keyID, pk)
return nil
}
func (s *BertySignedKeyStore) HasKey(id string) (bool, error) {
_, ok := s.Load(id)
return ok, nil
}
func (s *BertySignedKeyStore) CreateKey(id string) (crypto.PrivKey, error) {
return s.GetKey(id)
}
func (s *BertySignedKeyStore) GetKey(id string) (crypto.PrivKey, error) {
if privKey, ok := s.Load(id); ok {
if pk, ok := privKey.(crypto.PrivKey); ok {
return pk, nil
}
}
return nil, errcode.ErrGroupMemberUnknownGroupID
}
func (s *BertySignedKeyStore) Sign(privKey crypto.PrivKey, bytes []byte) ([]byte, error) {
return privKey.Sign(bytes)
}
func (s *BertySignedKeyStore) Verify(signature []byte, publicKey crypto.PubKey, data []byte) error {
ok, err := publicKey.Verify(data, signature)
if err != nil {
return err
}
if !ok {
return errcode.ErrGroupMemberLogEventSignature
}
return nil
}
func (s *BertySignedKeyStore) getIdentityProvider() *bertySignedIdentityProvider {
return &bertySignedIdentityProvider{
keyStore: s,
}
}
var _ keystore.Interface = (*BertySignedKeyStore)(nil)