Skip to content

Commit 9cb49d8

Browse files
committed
chore: refactor SkvsStubInterface
1 parent 43869b2 commit 9cb49d8

File tree

2 files changed

+12
-97
lines changed

2 files changed

+12
-97
lines changed

ecc_go/chaincode/enclave_go/enclave.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hyperledger/fabric-private-chaincode/ecc_go/chaincode/enclave_go/attestation"
1919
"github.com/hyperledger/fabric-private-chaincode/internal/crypto"
2020
"github.com/hyperledger/fabric-private-chaincode/internal/protos"
21+
pb "github.com/hyperledger/fabric-protos-go/peer"
2122
"github.com/hyperledger/fabric/bccsp"
2223
"github.com/hyperledger/fabric/bccsp/factory"
2324
"github.com/hyperledger/fabric/common/flogging"
@@ -37,6 +38,7 @@ type EnclaveStub struct {
3738
hostParams *protos.HostParameters
3839
chaincodeParams *protos.CCParameters
3940
fabricCryptoProvider bccsp.BCCSP
41+
stubProvider func(shim.ChaincodeStubInterface, *pb.ChaincodeInput, *readWriteSet, StateEncryptionFunctions) shim.ChaincodeStubInterface
4042
}
4143

4244
func NewEnclaveStub(cc shim.Chaincode) *EnclaveStub {
@@ -49,6 +51,9 @@ func NewEnclaveStub(cc shim.Chaincode) *EnclaveStub {
4951
csp: crypto.GetDefaultCSP(),
5052
ccRef: cc,
5153
fabricCryptoProvider: cryptoProvider,
54+
stubProvider: func(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) shim.ChaincodeStubInterface {
55+
return NewFpcStubInterface(stub, input, rwset, sep)
56+
},
5257
}
5358
}
5459

@@ -161,7 +166,7 @@ func (e *EnclaveStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincod
161166

162167
// Invoke chaincode
163168
// we wrap the stub with our FpcStubInterface
164-
fpcStub := NewFpcStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys)
169+
fpcStub := e.stubProvider(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys)
165170
ccResponse := e.ccRef.Invoke(fpcStub)
166171

167172
// marshal chaincode response

ecc_go/chaincode/enclave_go/skvs_stub.go

Lines changed: 6 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,105 +7,15 @@ SPDX-License-Identifier: Apache-2.0
77
package enclave_go
88

99
import (
10-
"crypto/sha256"
11-
"fmt"
12-
1310
"github.com/hyperledger/fabric-chaincode-go/shim"
14-
"github.com/hyperledger/fabric-private-chaincode/internal/protos"
15-
"github.com/hyperledger/fabric/protoutil"
16-
"github.com/pkg/errors"
17-
"google.golang.org/protobuf/proto"
11+
pb "github.com/hyperledger/fabric-protos-go/peer"
1812
)
1913

20-
type skvsStub struct {
21-
*EnclaveStub
22-
}
23-
24-
func NewSkvsStub(cc shim.Chaincode) *skvsStub {
14+
func NewSkvsStub(cc shim.Chaincode) *EnclaveStub {
15+
logger.Warning("==== SKVS NewSkvsStub ====")
2516
enclaveStub := NewEnclaveStub(cc)
26-
return &skvsStub{enclaveStub}
27-
}
28-
29-
func (e *skvsStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) ([]byte, error) {
30-
logger.Warning("==== SKVS ChaincodeInvoke ====")
31-
32-
signedProposal, err := stub.GetSignedProposal()
33-
if err != nil {
34-
return nil, fmt.Errorf("cannot get signed proposal: %s", err.Error())
35-
}
36-
37-
if err := e.verifySignedProposal(stub, chaincodeRequestMessageBytes); err != nil {
38-
return nil, errors.Wrap(err, "signed proposal verification failed")
39-
}
40-
41-
// unmarshal chaincodeRequest
42-
chaincodeRequestMessage := &protos.ChaincodeRequestMessage{}
43-
err = proto.Unmarshal(chaincodeRequestMessageBytes, chaincodeRequestMessage)
44-
if err != nil {
45-
return nil, err
46-
}
47-
48-
// get key transport message including the encryption keys for request and response
49-
keyTransportMessage, err := e.extractKeyTransportMessage(chaincodeRequestMessage)
50-
if err != nil {
51-
return nil, errors.Wrap(err, "cannot extract keyTransportMessage")
52-
}
53-
54-
// decrypt request
55-
cleartextChaincodeRequest, err := e.extractCleartextChaincodeRequest(chaincodeRequestMessage, keyTransportMessage)
56-
if err != nil {
57-
return nil, errors.Wrap(err, "cannot decrypt chaincode request")
58-
}
59-
60-
// create a new instance of a FPC RWSet that we pass to the stub and later return with the response
61-
rwset := NewReadWriteSet()
62-
63-
// Invoke chaincode
64-
// we wrap the stub with our FpcStubInterface
65-
// ** Implement our own FpcStubInterface
66-
skvsStub := NewSkvsStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys)
67-
ccResponse := e.ccRef.Invoke(skvsStub)
68-
// **
69-
// fpcStub := NewFpcStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys)
70-
// ccResponse := e.ccRef.Invoke(fpcStub)
71-
72-
// marshal chaincode response
73-
ccResponseBytes, err := protoutil.Marshal(&ccResponse)
74-
if err != nil {
75-
return nil, err
76-
}
77-
78-
//encrypt response
79-
encryptedResponse, err := e.csp.EncryptMessage(keyTransportMessage.GetResponseEncryptionKey(), ccResponseBytes)
80-
if err != nil {
81-
return nil, err
82-
}
83-
84-
chaincodeRequestMessageHash := sha256.Sum256(chaincodeRequestMessageBytes)
85-
86-
response := &protos.ChaincodeResponseMessage{
87-
EncryptedResponse: encryptedResponse,
88-
FpcRwSet: rwset.ToFPCKVSet(),
89-
EnclaveId: e.identity.GetEnclaveId(),
90-
Proposal: signedProposal,
91-
ChaincodeRequestMessageHash: chaincodeRequestMessageHash[:],
17+
enclaveStub.stubProvider = func(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) shim.ChaincodeStubInterface {
18+
return NewSkvsStubInterface(stub, input, rwset, sep)
9219
}
93-
94-
responseBytes, err := proto.Marshal(response)
95-
if err != nil {
96-
return nil, err
97-
}
98-
99-
// create signature
100-
sig, err := e.identity.Sign(responseBytes)
101-
if err != nil {
102-
return nil, err
103-
}
104-
105-
signedResponse := &protos.SignedChaincodeResponseMessage{
106-
ChaincodeResponseMessage: responseBytes,
107-
Signature: sig,
108-
}
109-
110-
return proto.Marshal(signedResponse)
20+
return enclaveStub
11121
}

0 commit comments

Comments
 (0)