forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_enc_manual_ondemand_with_mapping.go
82 lines (70 loc) · 2.72 KB
/
cc_enc_manual_ondemand_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 encrypting data on demand (if encrypting key is provided in transient map)
func NewEncryptOnDemandPaymentCC() *router.Chaincode {
r := router.New(`encrypted-on-demand`).
Pre(encryption.ArgsDecryptIfKeyProvided). // encrypted args optional - key can be provided in transient map
Init(router.EmptyContextHandler)
debug.AddHandlers(r, `debug`)
r.Use(m.MapStates(StateMappings)) // use state mappings
r.Group(`payment`).
Invoke(`Create`, invokePaymentCreateManualEncryptWithMapping, p.String(`type`), p.String(`id`), p.Int(`amount`)).
Query(`List`, queryPaymentsWithMapping, p.String(`type`)).
Query(`Get`, queryPaymentWithMapping, p.String(`type`), p.String(`id`))
return router.NewChaincode(r)
}
// Payment creation chaincode function handler - can be used in encryption and no encryption mode
func invokePaymentCreateManualEncryptWithMapping(c router.Context) (interface{}, error) {
var (
paymentType = c.ParamString(`type`)
paymentId = c.ParamString(`id`)
paymentAmount = c.ParamInt(`amount`)
s state.State
err error
returnVal []byte
)
// Explicit encrypted state wrapper with key from transient map if key provided
if s, err = encryption.StateWithTransientKeyIfProvided(c); err != nil {
return nil, err
}
returnVal = []byte(paymentId)
// return encrypted val if key provided
if key, _ := encryption.KeyFromTransient(c); key != nil {
returnVal, err = encryption.Encrypt(key, paymentId)
}
return returnVal, s.Put(&schema.Payment{Type: paymentType, Id: paymentId, Amount: int32(paymentAmount)})
}
func queryPaymentsWithMapping(c router.Context) (interface{}, error) {
var (
//paymentType = c.ParamString(`type`)
s state.State
err error
)
// Explicit encrypted state wrapper with key from transient map if key provided
if s, err = encryption.StateWithTransientKeyIfProvided(c); err != nil {
return nil, err
}
return s.List(&schema.Payment{})
}
// Payment query chaincode function handler - can be used in encryption and no encryption mode
func queryPaymentWithMapping(c router.Context) (interface{}, error) {
var (
paymentType = c.ParamString(`type`)
paymentId = c.ParamString(`id`)
s state.State
err error
)
if s, err = encryption.StateWithTransientKeyIfProvided(c); err != nil {
return nil, err
}
return s.Get(&schema.Payment{Type: paymentType, Id: paymentId})
}