This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathevent.go
105 lines (88 loc) · 2.76 KB
/
event.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
package encryption
import (
"encoding/base64"
"fmt"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/s7techlab/cckit/router"
"github.com/s7techlab/cckit/state"
)
// EventWithTransientKey creates encrypted event wrapper with provided key for symmetric encryption/decryption
func EventWithTransientKey(c router.Context) (state.Event, error) {
key, err := KeyFromTransient(c)
if err != nil {
return nil, err
}
return Event(c, key)
}
// EventWithTransientKeyIfProvided returns encrypted event wrapper if key for symmetric
// encryption/decryption is provided, otherwise return default event wrapper
func EventWithTransientKeyIfProvided(c router.Context) (state.Event, error) {
key, err := KeyFromTransient(c)
switch err {
case nil:
return Event(c, key)
case ErrKeyNotDefinedInTransientMap:
//default event wrapper without encryption
return c.Event(), nil
}
return nil, err
}
// Event encrypting the events before setEvent()
func Event(c router.Context, key []byte) (state.Event, error) {
//current state
s := c.Event()
s.UseSetTransformer(ToBytesEncryptor(key))
s.UseNameTransformer(StringEncryptor(key))
return s, nil
}
// StringEncryptor returns state.StringTransformer encrypting string with provided key
func StringEncryptor(key []byte) state.StringTransformer {
return func(s string) (encrypted string, err error) {
var (
enc []byte
)
if enc, err = Encrypt(key, []byte(s)); err != nil {
return ``, err
}
return base64.StdEncoding.EncodeToString(enc), nil
}
}
// EncryptEvent encrypts event payload and event name. Event name also base64 encoded.
// ChaincodeId and TxId remains unencrypted
func EncryptEvent(encKey []byte, event *peer.ChaincodeEvent) (encrypted *peer.ChaincodeEvent, err error) {
var (
encName, encPayload []byte
)
if encName, err = Encrypt(encKey, []byte(event.EventName)); err != nil {
return nil, err
}
if encPayload, err = Encrypt(encKey, event.Payload); err != nil {
return nil, err
}
return &peer.ChaincodeEvent{
ChaincodeId: event.ChaincodeId,
TxId: event.TxId,
EventName: base64.StdEncoding.EncodeToString(encName),
Payload: encPayload,
}, nil
}
func DecryptEvent(encKey []byte, event *peer.ChaincodeEvent) (decrypted *peer.ChaincodeEvent, err error) {
var (
encNameBytes, decName, decPayload []byte
)
if encNameBytes, err = base64.StdEncoding.DecodeString(event.EventName); err != nil {
return nil, fmt.Errorf(`event name base64 decoding: %w`, err)
}
if decName, err = Decrypt(encKey, encNameBytes); err != nil {
return nil, err
}
if decPayload, err = Decrypt(encKey, event.Payload); err != nil {
return nil, err
}
return &peer.ChaincodeEvent{
ChaincodeId: event.ChaincodeId,
TxId: event.TxId,
EventName: string(decName),
Payload: decPayload,
}, nil
}