1- package encryption
1+ package testing
22
33import (
44 "fmt"
55
66 "github.com/hyperledger/fabric-protos-go/peer"
7+
8+ "github.com/s7techlab/cckit/extensions/encryption"
79 "github.com/s7techlab/cckit/response"
8- "github.com/s7techlab/cckit/testing"
10+ testcc "github.com/s7techlab/cckit/testing"
911)
1012
1113// MockInvoke helper for invoking MockStub with transient key and encrypted args
12- func MockInvoke (cc * testing .MockStub , encKey []byte , args ... interface {}) peer.Response {
13- encArgs , err := EncryptArgs (encKey , args ... )
14+ func MockInvoke (cc * testcc .MockStub , encKey []byte , args ... interface {}) peer.Response {
15+ encArgs , err := encryption . EncryptArgs (encKey , args ... )
1416 if err != nil {
1517 return response .Error (`unable to encrypt input args` )
1618 }
17- return cc .AddTransient (TransientMapWithKey (encKey )).InvokeBytes (encArgs ... )
19+ return cc .AddTransient (encryption . TransientMapWithKey (encKey )).InvokeBytes (encArgs ... )
1820}
1921
2022// MockQuery helper for querying MockStub with transient key and encrypted args
21- func MockQuery (cc * testing .MockStub , encKey []byte , args ... interface {}) peer.Response {
22- encArgs , err := EncryptArgs (encKey , args ... )
23+ func MockQuery (cc * testcc .MockStub , encKey []byte , args ... interface {}) peer.Response {
24+ encArgs , err := encryption . EncryptArgs (encKey , args ... )
2325 if err != nil {
2426 return response .Error (`unable to encrypt input args` )
2527 }
26- return cc .AddTransient (TransientMapWithKey (encKey )).QueryBytes (encArgs ... )
28+ return cc .AddTransient (encryption . TransientMapWithKey (encKey )).QueryBytes (encArgs ... )
2729}
2830
2931// MockStub wrapper for querying and invoking encrypted chaincode
3032type MockStub struct {
31- MockStub * testing .MockStub
33+ MockStub * testcc .MockStub
3234 //EncKey key for encrypt data before query/invoke
3335 EncKey []byte
3436
@@ -37,7 +39,7 @@ type MockStub struct {
3739}
3840
3941// NewMockStub creates wrapper for querying and invoking encrypted chaincode
40- func NewMockStub (mockStub * testing .MockStub , encKey []byte ) * MockStub {
42+ func NewMockStub (mockStub * testcc .MockStub , encKey []byte ) * MockStub {
4143 return & MockStub {MockStub : mockStub , EncKey : encKey }
4244}
4345
@@ -52,7 +54,7 @@ func (s *MockStub) Invoke(args ...interface{}) (response peer.Response) {
5254 //after receiving response we can decrypt received peer response
5355 // actual only for invoke, query responses are not encrypted
5456 if s .DecryptInvokeResponse && len (response .Payload ) > 0 && string (response .Payload ) != `null` {
55- if decrypted , err = Decrypt (s .EncKey , response .Payload ); err != nil {
57+ if decrypted , err = encryption . Decrypt (s .EncKey , response .Payload ); err != nil {
5658 panic (fmt .Sprintf (
5759 `decrypt mock invoke error with payload %s (%d): %s` ,
5860 string (response .Payload ), len (response .Payload ), err ))
@@ -68,11 +70,11 @@ func (s *MockStub) Query(args ...interface{}) peer.Response {
6870}
6971
7072func (s * MockStub ) Init (args ... interface {}) peer.Response {
71- encArgs , err := EncryptArgs (s .EncKey , args ... )
73+ encArgs , err := encryption . EncryptArgs (s .EncKey , args ... )
7274 if err != nil {
7375 return response .Error (`unable to encrypt input args` )
7476 }
75- return s .MockStub .AddTransient (TransientMapWithKey (s .EncKey )).InitBytes (encArgs ... )
77+ return s .MockStub .AddTransient (encryption . TransientMapWithKey (s .EncKey )).InitBytes (encArgs ... )
7678}
7779
7880func (s * MockStub ) From (args ... interface {}) * MockStub {
@@ -81,20 +83,5 @@ func (s *MockStub) From(args ...interface{}) *MockStub {
8183}
8284
8385func (s * MockStub ) LastEvent () * peer.ChaincodeEvent {
84- return MustDecryptEvent (s .EncKey , s .MockStub .ChaincodeEvent )
85- }
86-
87- // MustEncryptEvent helper for EncryptEvent. Panics in case of error.
88- func MustEncryptEvent (encKey []byte , event * peer.ChaincodeEvent ) * peer.ChaincodeEvent {
89- encrypted , err := EncryptEvent (encKey , event )
90- testing .PanicIfError (err )
91- return encrypted
92- }
93-
94- // MustDecryptEvent helper for DecryptEvent. Panics in case of error.
95- func MustDecryptEvent (encKey []byte , event * peer.ChaincodeEvent ) * peer.ChaincodeEvent {
96- decrypted , err := DecryptEvent (encKey , event )
97- testing .PanicIfError (err )
98-
99- return decrypted
86+ return encryption .MustDecryptEvent (s .EncKey , s .MockStub .ChaincodeEvent )
10087}
0 commit comments