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 pathopt.go
123 lines (106 loc) · 2.89 KB
/
opt.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package gateway
import (
"context"
"fmt"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/msp"
"github.com/s7techlab/cckit/extensions/encryption"
"github.com/s7techlab/cckit/state/mapping"
)
type (
Opts struct {
Context []ContextOpt
Input []InputOpt
Output []OutputOpt
Event []EventOpt
}
InstanceOpts struct {
Locator *ChaincodeLocator
Opts Opts
}
Opt func(*Opts)
ContextOpt func(ctx context.Context) context.Context
InputOpt func(input *ChaincodeInput) error
OutputOpt func(action InvocationType, response *peer.Response) error
EventOpt func(event *ChaincodeEvent) error
)
func WithDefaultSigner(defaultSigner msp.SigningIdentity) Opt {
return func(opts *Opts) {
opts.Context = append(opts.Context, func(ctx context.Context) context.Context {
return ContextWithDefaultSigner(ctx, defaultSigner)
})
}
}
func WithDefaultTransientMapValue(key string, value []byte) Opt {
return func(o *Opts) {
o.Input = append(o.Input, func(input *ChaincodeInput) error {
if input.Transient == nil {
input.Transient = make(map[string][]byte)
}
if _, exists := input.Transient[key]; !exists {
input.Transient[key] = value
}
return nil
})
}
}
func WithEncryption(encKey []byte) Opt {
return func(o *Opts) {
WithDefaultTransientMapValue(encryption.TransientMapKey, encKey)(o)
WithArgsEncryption(encKey)(o)
WithInvokePayloadDecryption(encKey)(o)
WithEventDecryption(encKey)(o)
}
}
func WithArgsEncryption(encKey []byte) Opt {
return func(o *Opts) {
o.Input = append(o.Input, func(ccInput *ChaincodeInput) (err error) {
ccInput.Args, err = encryption.EncryptArgsBytes(encKey, ccInput.Args)
return err
})
}
}
func WithInvokePayloadDecryption(encKey []byte) Opt {
return func(o *Opts) {
o.Output = append(o.Output, func(action InvocationType, r *peer.Response) (err error) {
if action != InvocationType_INVOCATION_TYPE_INVOKE {
return nil
}
r.Payload, err = encryption.Decrypt(encKey, r.Payload)
if err != nil {
return fmt.Errorf(`decrypt invoke payload: %w`, err)
}
return nil
})
}
}
func WithEventDecryption(encKey []byte) Opt {
return func(o *Opts) {
o.Event = append(o.Event, func(e *ChaincodeEvent) error {
de, err := encryption.DecryptEvent(encKey, e.Event)
if err != nil {
return err
}
e.Event = de
return nil
})
}
}
func WithEventResolver(resolver mapping.EventResolver) Opt {
return func(o *Opts) {
o.Event = append(o.Event, func(e *ChaincodeEvent) error {
eventPayload, err := resolver.Resolve(e.Event.EventName, e.Event.Payload)
if err != nil {
return err
}
bb, err := (&jsonpb.Marshaler{EmitDefaults: true, OrigName: true}).MarshalToString(eventPayload.(proto.Message))
if err != nil {
return err
}
e.Payload = &RawJson{Value: []byte(bb)}
return nil
})
}
}