forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.go
36 lines (30 loc) · 1.04 KB
/
invoke.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
package encryption
import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/pkg/errors"
"github.com/s7techlab/cckit/convert"
"github.com/s7techlab/cckit/state"
)
// InvokeChaincode decrypts received payload
func InvokeChaincode(
stub shim.ChaincodeStubInterface, encKey []byte, chaincodeName string,
args []interface{}, channel string, target interface{}) (interface{}, error) {
// args are not encrypted cause we cannot pass encryption key in transient map while invoking cc from cc
// thus target cc cannot decrypt args
aa, err := convert.ArgsToBytes(args...)
if err != nil {
return nil, errors.Wrap(err, `encrypt args`)
}
response := stub.InvokeChaincode(chaincodeName, aa, channel)
if response.Status != shim.OK {
return nil, errors.New(response.Message)
}
if len(response.Payload) == 0 {
return nil, state.ErrEmptyChaincodeResponsePayload
}
decrypted, err := Decrypt(encKey, response.Payload)
if err != nil {
return nil, errors.Wrap(err, `decrypt payload`)
}
return convert.FromBytes(decrypted, target)
}