-
Notifications
You must be signed in to change notification settings - Fork 28
/
orbitdb_signed_entry_identity_provider.go
109 lines (86 loc) · 2.59 KB
/
orbitdb_signed_entry_identity_provider.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package weshnet
import (
"context"
"github.com/libp2p/go-libp2p/core/crypto"
"berty.tech/go-ipfs-log/identityprovider"
"berty.tech/weshnet/v2/pkg/errcode"
)
const (
identityGroupIDKey = "group_id"
storeTypeKey = "store_type"
identityType = "betry_group_entry"
)
type bertySignedIdentityProvider struct {
keyStore *BertySignedKeyStore
}
func (b *bertySignedIdentityProvider) UnmarshalPublicKey(data []byte) (crypto.PubKey, error) {
return crypto.UnmarshalPublicKey(data)
}
func (b *bertySignedIdentityProvider) GetID(_ context.Context, opts *identityprovider.CreateIdentityOptions) (string, error) {
return opts.ID, nil
}
//nolint:revive
func (b *bertySignedIdentityProvider) SignIdentity(ctx context.Context, data []byte, id string) ([]byte, error) {
return nil, nil
}
func (b *bertySignedIdentityProvider) GetType() string {
return identityType
}
func (b *bertySignedIdentityProvider) VerifyIdentity(*identityprovider.Identity) error {
return nil
}
func (b *bertySignedIdentityProvider) Sign(ctx context.Context, identity *identityprovider.Identity, bytes []byte) ([]byte, error) {
key, err := b.keyStore.GetKey(ctx, identity.ID)
if err != nil {
return nil, errcode.ErrCode_TODO.Wrap(err)
}
sig, err := key.Sign(bytes)
if err != nil {
return nil, errcode.ErrCode_TODO.Wrap(err)
}
return sig, nil
}
func (b *bertySignedIdentityProvider) signID(ctx context.Context, id string) (crypto.PubKey, []byte, error) {
privKey, err := b.keyStore.GetKey(ctx, id)
if err != nil {
return nil, nil, err
}
idSignature, err := b.keyStore.Sign(privKey, []byte(id))
if err != nil {
return nil, nil, err
}
return privKey.GetPublic(), idSignature, nil
}
func (b *bertySignedIdentityProvider) createIdentity(ctx context.Context, options *identityprovider.CreateIdentityOptions) (*identityprovider.Identity, error) {
id, err := b.GetID(ctx, options)
if err != nil {
return nil, err
}
publicKey, idSignature, err := b.signID(ctx, id)
if err != nil {
return nil, err
}
publicKeyRaw, err := publicKey.Raw()
if err != nil {
return nil, err
}
publicKeyBytes, err := crypto.MarshalPublicKey(publicKey)
if err != nil {
return nil, err
}
pubKeyIDSignature, err := b.SignIdentity(ctx, append(publicKeyRaw, idSignature...), options.ID)
if err != nil {
return nil, err
}
return &identityprovider.Identity{
ID: id,
PublicKey: publicKeyBytes,
Signatures: &identityprovider.IdentitySignature{
ID: idSignature,
PublicKey: pubKeyIDSignature,
},
Type: b.GetType(),
Provider: b,
}, nil
}
var _ identityprovider.Interface = (*bertySignedIdentityProvider)(nil)