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_manual_required_without_mapping.go
92 lines (80 loc) · 2.88 KB
/
cc_enc_manual_required_without_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
84
85
86
87
88
89
90
91
92
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"
)
// NewEncryptPaymentCC chaincode with required encrypting (encrypting key must be provided in transient map)
// WITHOUT mapping
func NewEncryptPaymentCC() *router.Chaincode {
r := router.New(`encrypted`).
Pre(encryption.ArgsDecrypt). // encrypted args required - key must be provided in transient map
Init(router.EmptyContextHandler)
r.Use(m.MapStates(StateMappings)) // use state mapping
debug.AddHandlers(r, `debug`)
// same as NewEncryptOnDemandPaymentCC
r.Group(`payment`).
Invoke(`Create`, invokePaymentCreateManualEncryptWithoutMapping, p.String(`type`), p.String(`id`), p.Int(`amount`)).
Query(`List`, queryPaymentsWithoutMapping, p.String(`type`)).
Query(`Get`, queryPaymentWithoutMapping, 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 invokePaymentCreateManualEncryptWithoutMapping(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)
}
// manually set key
return returnVal, s.Put(
[]string{`PaymentManual`, paymentType, paymentId},
&schema.Payment{
Type: paymentType,
Id: paymentId,
Amount: int32(paymentAmount)},
)
}
func queryPaymentsWithoutMapping(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
}
// manually set key
return s.List(`Payment`)
}
// Payment query chaincode function handler - can be used in encryption and no encryption mode
func queryPaymentWithoutMapping(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
}
// manually set key
return s.Get([]string{`Payment`, paymentType, paymentId})
}