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 pathcc_enc_context_with_mapping.go
83 lines (66 loc) · 2.93 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
83
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"
)
// NewEncryptedPaymentCCWithEncStateContext 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`
After(encryption.EncryptInvokeResponse()).
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)}
)
// State use encryption setting from context, state key sets manually
if err = c.State().Insert(payment); err != nil {
return nil, err
}
if err = c.Event().Set(event); err != nil {
return
}
// returned value will be placed in ledger - so if we don't want to show in ledger - we must encrypt it
return payment, nil
}
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})
}