forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_enc_context_with_mapping.go
82 lines (66 loc) · 2.94 KB
/
cc_enc_context_with_mapping.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
package payment
import (
"github.com/s7techlab/cckit/examples/payment/schema"
"github.com/s7techlab/cckit/extensions/debug"
"github.com/s7techlab/cckit/extensions/encryption"
"github.com/s7techlab/cckit/router"
p "github.com/s7techlab/cckit/router/param"
"github.com/s7techlab/cckit/state"
m "github.com/s7techlab/cckit/state/mapping"
)
// Chaincode WITH schema mapping
// and required encrypting
func NewEncryptedPaymentCCWithEncStateContext() *router.Chaincode {
r := router.New(`encrypted-with-custom-context`).
Pre(encryption.ArgsDecryptExcept(`debugStateGet`)). // encrypted args required, except method `stateGet`
Init(router.EmptyContextHandler)
r.Use(m.MapStates(StateMappings)) // use state mappings
r.Use(m.MapEvents(EventMappings)) // use event mappings
// default Context replaced with EncryptedStateContext only if key is provided in transient map
r.Use(encryption.EncStateContextIfKeyProvided)
debug.AddHandlers(r, `debug`)
r.Group(`payment`).
// use multiple separate params
// better way - to use single protobuf "payload" parameter
Invoke(`Create`, invokePaymentCreateWithDefaultContext, p.String(`type`), p.String(`id`), p.Int(`amount`)).
Query(`List`, queryPaymentsWithDefaultContext, p.String(`type`)).
Query(`Get`, queryPaymentWithDefaultContext, p.String(`type`), p.String(`id`))
return router.NewChaincode(r)
}
func invokePaymentCreateWithDefaultContext(c router.Context) (res interface{}, err error) {
// params comes unencrypted - "before" middleware decrypts its using key from transient map
var (
paymentType = c.ParamString(`type`)
paymentId = c.ParamString(`id`)
paymentAmount = c.ParamInt(`amount`)
payment = &schema.Payment{Type: paymentType, Id: paymentId, Amount: int32(paymentAmount)}
event = &schema.PaymentEvent{Type: paymentType, Id: paymentId, Amount: int32(paymentAmount)}
responsePayload []byte
)
if err = c.Event().Set(event); err != nil {
return
}
// State use encryption setting from context, state key sets manually
// returned value will be placed in ledger - so if we don't want to show in in ledger - we must encrypt it
if responsePayload, err = encryption.EncryptWithTransientKey(c, payment); err != nil {
return
}
return responsePayload, c.State().Insert(payment)
}
func queryPaymentsWithDefaultContext(c router.Context) (interface{}, error) {
//paymentType := c.ParamString(`type`)
//namespace, err := c.State().(m.MappedState).MappingNamespace(&schema.Payment{})
//if err != nil {
// return nil, err
//}
//return c.State().List(namespace.Append(state.Key { paymentType }), &schema.Payment{})
// some sugar to previous
return c.State().(m.MappedState).ListWith(&schema.Payment{}, state.Key{c.ParamString(`type`)})
}
func queryPaymentWithDefaultContext(c router.Context) (interface{}, error) {
var (
paymentType = c.ParamString(`type`)
paymentId = c.ParamString(`id`)
)
return c.State().Get(&schema.Payment{Type: paymentType, Id: paymentId})
}