From cc005e15daa1300c07e1481035da998d184f584c Mon Sep 17 00:00:00 2001 From: Flamewind97 Date: Thu, 13 Apr 2023 15:19:02 +0200 Subject: [PATCH 01/10] add singleKVS wrapper Signed-off-by: chenchanglew --- ecc_go/chaincode/singleKVS.go | 45 ++++++++++++++++++++++ samples/chaincode/secret-keeper-go/main.go | 5 ++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 ecc_go/chaincode/singleKVS.go diff --git a/ecc_go/chaincode/singleKVS.go b/ecc_go/chaincode/singleKVS.go new file mode 100644 index 000000000..9ea6ab01c --- /dev/null +++ b/ecc_go/chaincode/singleKVS.go @@ -0,0 +1,45 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package chaincode + +import ( + "fmt" + + "github.com/hyperledger/fabric-chaincode-go/shim" + "github.com/hyperledger/fabric-private-chaincode/ecc/chaincode" +) + +var SingleKey = "SingleKey" + +type SKVSWrapper struct { + *chaincode.EnclaveChaincode +} + +type skvsStub struct { + shim.ChaincodeStubInterface +} + +func (s *skvsStub) GetState(key string) ([]byte, error) { + fmt.Printf("Inside SKVS solution, GetState, key=%s\n", key) + return s.ChaincodeStubInterface.GetState(SingleKey) + // return s.ChaincodeStubInterface.GetState(key) +} + +func (s *skvsStub) PutState(key string, value []byte) error { + fmt.Printf("Inside SKVS solution, PutState, key=%s, value=%x\n", key, value) + return s.ChaincodeStubInterface.PutState(SingleKey, value) + // return s.ChaincodeStubInterface.PutState(key, value) +} + +func (s *SKVSWrapper) GetStub() shim.ChaincodeStubInterface { + // get the original stub + stub := s.GetStub() + fmt.Println("Inside SKVS solution, GetStub") + // create a new stub with the overridden GetState() function + skvsStub := &skvsStub{stub} + return skvsStub +} diff --git a/samples/chaincode/secret-keeper-go/main.go b/samples/chaincode/secret-keeper-go/main.go index a8f707f1b..b3b1515a0 100644 --- a/samples/chaincode/secret-keeper-go/main.go +++ b/samples/chaincode/secret-keeper-go/main.go @@ -25,11 +25,14 @@ func main() { secretChaincode, _ := contractapi.NewChaincode(&chaincode.SecretKeeper{}) chaincode := fpc.NewPrivateChaincode(secretChaincode) + // single KVS + skvsChaincode := &fpc.SingleKVSWrapper{chaincode} + // start chaincode as a service server := &shim.ChaincodeServer{ CCID: ccid, Address: addr, - CC: chaincode, + CC: skvsChaincode, TLSProps: shim.TLSProperties{ Disabled: true, // just for testing good enough }, From 1c2e39cb9fbc5a1b65d012dc2ffee5ad55706b5e Mon Sep 17 00:00:00 2001 From: Flamewind97 Date: Fri, 14 Apr 2023 12:08:45 +0200 Subject: [PATCH 02/10] able to save into single key Signed-off-by: chenchanglew --- ecc_go/chaincode/enclave_go/skvs_stub.go | 110 ++++++++++++ .../enclave_go/skvs_stub_Interface.go | 108 ++++++++++++ ecc_go/chaincode/singleKVS.go | 163 +++++++++++++++--- samples/chaincode/secret-keeper-go/main.go | 7 +- 4 files changed, 359 insertions(+), 29 deletions(-) create mode 100644 ecc_go/chaincode/enclave_go/skvs_stub.go create mode 100644 ecc_go/chaincode/enclave_go/skvs_stub_Interface.go diff --git a/ecc_go/chaincode/enclave_go/skvs_stub.go b/ecc_go/chaincode/enclave_go/skvs_stub.go new file mode 100644 index 000000000..3d68f116d --- /dev/null +++ b/ecc_go/chaincode/enclave_go/skvs_stub.go @@ -0,0 +1,110 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package enclave_go + +import ( + "crypto/sha256" + + "github.com/hyperledger/fabric-chaincode-go/shim" + "github.com/hyperledger/fabric-private-chaincode/internal/protos" + "github.com/hyperledger/fabric/protoutil" + "github.com/pkg/errors" + "google.golang.org/protobuf/proto" +) + +type skvsStub struct { + *EnclaveStub +} + +func NewSkvsStub(cc shim.Chaincode) *skvsStub { + enclaveStub := NewEnclaveStub(cc) + return &skvsStub{enclaveStub} +} + +func (e *skvsStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) ([]byte, error) { + logger.Error("==== SKVS ChaincodeInvoke ====") + + signedProposal, err := stub.GetSignedProposal() + if err != nil { + shim.Error(err.Error()) + } + + if err := e.verifySignedProposal(stub, chaincodeRequestMessageBytes); err != nil { + return nil, errors.Wrap(err, "signed proposal verification failed") + } + + // unmarshal chaincodeRequest + chaincodeRequestMessage := &protos.ChaincodeRequestMessage{} + err = proto.Unmarshal(chaincodeRequestMessageBytes, chaincodeRequestMessage) + if err != nil { + return nil, err + } + + // get key transport message including the encryption keys for request and response + keyTransportMessage, err := e.extractKeyTransportMessage(chaincodeRequestMessage) + if err != nil { + return nil, errors.Wrap(err, "cannot extract keyTransportMessage") + } + + // decrypt request + cleartextChaincodeRequest, err := e.extractCleartextChaincodeRequest(chaincodeRequestMessage, keyTransportMessage) + if err != nil { + return nil, errors.Wrap(err, "cannot decrypt chaincode request") + } + + // create a new instance of a FPC RWSet that we pass to the stub and later return with the response + rwset := NewReadWriteSet() + + // Invoke chaincode + // we wrap the stub with our FpcStubInterface + // ** Implement our own FpcStubInterface + skvsStub := NewSkvsStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) + ccResponse := e.ccRef.Invoke(skvsStub) + // ** + // fpcStub := NewFpcStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) + // ccResponse := e.ccRef.Invoke(fpcStub) + + // marshal chaincode response + ccResponseBytes, err := protoutil.Marshal(&ccResponse) + if err != nil { + return nil, err + } + + //encrypt response + encryptedResponse, err := e.csp.EncryptMessage(keyTransportMessage.GetResponseEncryptionKey(), ccResponseBytes) + if err != nil { + return nil, err + } + + chaincodeRequestMessageHash := sha256.Sum256(chaincodeRequestMessageBytes) + + response := &protos.ChaincodeResponseMessage{ + EncryptedResponse: encryptedResponse, + FpcRwSet: rwset.ToFPCKVSet(), + EnclaveId: e.identity.GetEnclaveId(), + Proposal: signedProposal, + ChaincodeRequestMessageHash: chaincodeRequestMessageHash[:], + } + + responseBytes, err := proto.Marshal(response) + if err != nil { + return nil, err + } + + // create signature + sig, err := e.identity.Sign(responseBytes) + if err != nil { + return nil, err + } + + signedResponse := &protos.SignedChaincodeResponseMessage{ + ChaincodeResponseMessage: responseBytes, + Signature: sig, + } + + return proto.Marshal(signedResponse) +} diff --git a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go new file mode 100644 index 000000000..49b80f42b --- /dev/null +++ b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go @@ -0,0 +1,108 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package enclave_go + +import ( + "encoding/hex" + "encoding/json" + + "github.com/hyperledger/fabric-chaincode-go/shim" + pb "github.com/hyperledger/fabric-protos-go/peer" +) + +var SingleKey = "SingleKey" + +type SkvsStubInterface struct { + *FpcStubInterface +} + +func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { + fpcStub := NewFpcStubInterface(stub, input, rwset, sep) + return &SkvsStubInterface{fpcStub} +} + +func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { + logger.Warningf("Calling Get State (Start), key = %s", key) + encValue, err := s.GetPublicState(SingleKey) + if err != nil { + return nil, err + } + + // in case the key does not exist, return early + if len(encValue) == 0 { + logger.Warningf("Calling Get State (End), data empty return.") + return nil, nil + } + + value, err := s.sep.DecryptState(encValue) + if err != nil { + return nil, err + } + + // Create an interface{} value to hold the unmarshalled data + var allData map[string]string + + // Unmarshal the JSON data into the interface{} value + err = json.Unmarshal(value, &allData) + if err != nil { + logger.Errorf("SKVS Json unmarshal error: %s", err) + return nil, err + } + logger.Warningf("Calling Get State (Mid), key = %s, decrypted done alldata = %s", key, allData) + + targetHex := allData[key] + targetBytes, err := hex.DecodeString(targetHex) + logger.Warningf("Calling Get State (End), Target: %s, TargetBytes: %x, err: %s", targetHex, targetBytes, err) + return targetBytes, err + // return s.sep.DecryptState(encValue) +} + +func (s *SkvsStubInterface) PutState(key string, value []byte) error { + logger.Warningf("Calling Put State (Start), key = %s, value = %x", key, value) + // grab all data from the state. + encAllValue, err := s.GetPublicState(SingleKey) + if err != nil { + return err + } + var allData map[string]string + + if len(encAllValue) == 0 { + // current world state is empty + allData = map[string]string{} + } else { + allValue, err := s.sep.DecryptState(encAllValue) + if err != nil { + return err + } + // Unmarshal the JSON data into the interface{} value + err = json.Unmarshal(allValue, &allData) + if err != nil { + logger.Errorf("SKVS Json unmarshal error: %s", err) + return err + } + } + logger.Warningf("Calling Put State (Mid-1), decrypt succeed, allData = %s", allData) + + valueHex := hex.EncodeToString(value) + + allData[key] = valueHex + logger.Warningf("Calling Put State (Mid-2), add need data key = %s, valueHex = %s, allData = %s", key, valueHex, allData) + + byteAllData, err := json.Marshal(allData) + if err != nil { + return err + } + logger.Warningf("Calling Put State (Mid-3), successfull marshal allData, byteAlldata = %x", byteAllData) + + encValue, err := s.sep.EncryptState(byteAllData) + if err != nil { + return err + } + logger.Warningf("Calling Put State (End), put encValue %x", encValue) + + return s.PutPublicState(SingleKey, encValue) +} diff --git a/ecc_go/chaincode/singleKVS.go b/ecc_go/chaincode/singleKVS.go index 9ea6ab01c..e756b8cbd 100644 --- a/ecc_go/chaincode/singleKVS.go +++ b/ecc_go/chaincode/singleKVS.go @@ -7,39 +7,150 @@ SPDX-License-Identifier: Apache-2.0 package chaincode import ( - "fmt" - "github.com/hyperledger/fabric-chaincode-go/shim" "github.com/hyperledger/fabric-private-chaincode/ecc/chaincode" + "github.com/hyperledger/fabric-private-chaincode/ecc/chaincode/ercc" + "github.com/hyperledger/fabric-private-chaincode/ecc_go/chaincode/enclave_go" + "github.com/hyperledger/fabric-private-chaincode/internal/endorsement" + "github.com/hyperledger/fabric/common/flogging" ) -var SingleKey = "SingleKey" +var logger = flogging.MustGetLogger("enclave_go") -type SKVSWrapper struct { - *chaincode.EnclaveChaincode +func NewSkvsChaincode(cc shim.Chaincode) *chaincode.EnclaveChaincode { + ecc := &chaincode.EnclaveChaincode{ + Enclave: enclave_go.NewSkvsStub(cc), + Validator: endorsement.NewValidator(), + Extractor: &chaincode.ExtractorImpl{}, + Ercc: &ercc.StubImpl{}, + } + return ecc } -type skvsStub struct { - shim.ChaincodeStubInterface -} +// type skvsStub struct { +// *enclave_go.EnclaveStub +// } -func (s *skvsStub) GetState(key string) ([]byte, error) { - fmt.Printf("Inside SKVS solution, GetState, key=%s\n", key) - return s.ChaincodeStubInterface.GetState(SingleKey) - // return s.ChaincodeStubInterface.GetState(key) -} +// func NewSkvsStub(cc shim.Chaincode) *skvsStub { +// enclaveStub := enclave_go.NewEnclaveStub(cc) +// return &skvsStub{enclaveStub} +// } -func (s *skvsStub) PutState(key string, value []byte) error { - fmt.Printf("Inside SKVS solution, PutState, key=%s, value=%x\n", key, value) - return s.ChaincodeStubInterface.PutState(SingleKey, value) - // return s.ChaincodeStubInterface.PutState(key, value) -} +// func (e *skvsStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) ([]byte, error) { +// logger.Error("==== SKVS ChaincodeInvoke ====") -func (s *SKVSWrapper) GetStub() shim.ChaincodeStubInterface { - // get the original stub - stub := s.GetStub() - fmt.Println("Inside SKVS solution, GetStub") - // create a new stub with the overridden GetState() function - skvsStub := &skvsStub{stub} - return skvsStub -} +// signedProposal, err := stub.GetSignedProposal() +// if err != nil { +// shim.Error(err.Error()) +// } + +// if err := e.verifySignedProposal(stub, chaincodeRequestMessageBytes); err != nil { +// return nil, errors.Wrap(err, "signed proposal verification failed") +// } + +// // unmarshal chaincodeRequest +// chaincodeRequestMessage := &protos.ChaincodeRequestMessage{} +// err = proto.Unmarshal(chaincodeRequestMessageBytes, chaincodeRequestMessage) +// if err != nil { +// return nil, err +// } + +// // get key transport message including the encryption keys for request and response +// keyTransportMessage, err := e.extractKeyTransportMessage(chaincodeRequestMessage) +// if err != nil { +// return nil, errors.Wrap(err, "cannot extract keyTransportMessage") +// } + +// // decrypt request +// cleartextChaincodeRequest, err := e.extractCleartextChaincodeRequest(chaincodeRequestMessage, keyTransportMessage) +// if err != nil { +// return nil, errors.Wrap(err, "cannot decrypt chaincode request") +// } + +// // create a new instance of a FPC RWSet that we pass to the stub and later return with the response +// rwset := NewReadWriteSet() + +// // Invoke chaincode +// // we wrap the stub with our FpcStubInterface +// // ** Implement our own FpcStubInterface +// skvsStub := NewSkvsStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) +// ccResponse := e.ccRef.Invoke(skvsStub) +// // ** +// // fpcStub := NewFpcStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) +// // ccResponse := e.ccRef.Invoke(fpcStub) + +// // marshal chaincode response +// ccResponseBytes, err := protoutil.Marshal(&ccResponse) +// if err != nil { +// return nil, err +// } + +// //encrypt response +// encryptedResponse, err := e.csp.EncryptMessage(keyTransportMessage.GetResponseEncryptionKey(), ccResponseBytes) +// if err != nil { +// return nil, err +// } + +// chaincodeRequestMessageHash := sha256.Sum256(chaincodeRequestMessageBytes) + +// response := &protos.ChaincodeResponseMessage{ +// EncryptedResponse: encryptedResponse, +// FpcRwSet: rwset.ToFPCKVSet(), +// EnclaveId: e.identity.GetEnclaveId(), +// Proposal: signedProposal, +// ChaincodeRequestMessageHash: chaincodeRequestMessageHash[:], +// } + +// responseBytes, err := proto.Marshal(response) +// if err != nil { +// return nil, err +// } + +// // create signature +// sig, err := e.identity.Sign(responseBytes) +// if err != nil { +// return nil, err +// } + +// signedResponse := &protos.SignedChaincodeResponseMessage{ +// ChaincodeResponseMessage: responseBytes, +// Signature: sig, +// } + +// return proto.Marshal(signedResponse) +// } + +// func (e *skvsStub) verifySignedProposal(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) error { +// return e.EnclaveStub.verifySignedProposal(stub, chaincodeRequestMessageBytes) +// } + +// type SkvsStubInterface struct { +// *enclave_go.FpcStubInterface +// } + +// func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { +// fpcStub := enclave_go.NewFpcStubInterface(stub, input, rwset, sep) +// return &SkvsStubInterface{fpcStub} +// } + +// func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { +// encValue, err := s.GetPublicState(SingleKey) +// if err != nil { +// return nil, err +// } + +// // in case the key does not exist, return early +// if len(encValue) == 0 { +// return nil, nil +// } + +// return s.sep.DecryptState(encValue) +// } + +// func (s *SkvsStubInterface) PutState(key string, value []byte) error { +// encValue, err := s.sep.EncryptState(SingleKey) +// if err != nil { +// return err +// } +// return s.PutPublicState(key, encValue) +// } diff --git a/samples/chaincode/secret-keeper-go/main.go b/samples/chaincode/secret-keeper-go/main.go index b3b1515a0..7ab8b9b07 100644 --- a/samples/chaincode/secret-keeper-go/main.go +++ b/samples/chaincode/secret-keeper-go/main.go @@ -23,10 +23,11 @@ func main() { // create chaincode secretChaincode, _ := contractapi.NewChaincode(&chaincode.SecretKeeper{}) - chaincode := fpc.NewPrivateChaincode(secretChaincode) + // chaincode := fpc.NewPrivateChaincode(secretChaincode) + skvsChaincode := fpc.NewSkvsChaincode(secretChaincode) - // single KVS - skvsChaincode := &fpc.SingleKVSWrapper{chaincode} + // // single KVS + // skvsChaincode := &fpc.SKVSWrapper{chaincode} // start chaincode as a service server := &shim.ChaincodeServer{ From ec65187523f14b5f5a0297a1d3c350e8eb245b3d Mon Sep 17 00:00:00 2001 From: Flamewind97 Date: Fri, 14 Apr 2023 15:36:24 +0200 Subject: [PATCH 03/10] change to only getState Once Signed-off-by: chenchanglew --- .../enclave_go/skvs_stub_Interface.go | 101 ++++++++---------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go index 49b80f42b..86e1b2425 100644 --- a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go +++ b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go @@ -12,91 +12,80 @@ import ( "github.com/hyperledger/fabric-chaincode-go/shim" pb "github.com/hyperledger/fabric-protos-go/peer" + "github.com/pkg/errors" ) -var SingleKey = "SingleKey" - type SkvsStubInterface struct { *FpcStubInterface + allDataOld map[string]string + allDataNew map[string]string + key string } func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { + logger.Warning("==== Get New Skvs Interface =====") fpcStub := NewFpcStubInterface(stub, input, rwset, sep) - return &SkvsStubInterface{fpcStub} -} - -func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { - logger.Warningf("Calling Get State (Start), key = %s", key) - encValue, err := s.GetPublicState(SingleKey) + skvsStub := SkvsStubInterface{fpcStub, map[string]string{}, map[string]string{}, "SKVS"} + err := skvsStub.InitSKVS() if err != nil { - return nil, err + logger.Warningf("Error!! Initializing SKVS failed") } - - // in case the key does not exist, return early - if len(encValue) == 0 { - logger.Warningf("Calling Get State (End), data empty return.") - return nil, nil - } - - value, err := s.sep.DecryptState(encValue) - if err != nil { - return nil, err - } - - // Create an interface{} value to hold the unmarshalled data - var allData map[string]string - - // Unmarshal the JSON data into the interface{} value - err = json.Unmarshal(value, &allData) - if err != nil { - logger.Errorf("SKVS Json unmarshal error: %s", err) - return nil, err - } - logger.Warningf("Calling Get State (Mid), key = %s, decrypted done alldata = %s", key, allData) - - targetHex := allData[key] - targetBytes, err := hex.DecodeString(targetHex) - logger.Warningf("Calling Get State (End), Target: %s, TargetBytes: %x, err: %s", targetHex, targetBytes, err) - return targetBytes, err - // return s.sep.DecryptState(encValue) + return &skvsStub } -func (s *SkvsStubInterface) PutState(key string, value []byte) error { - logger.Warningf("Calling Put State (Start), key = %s, value = %x", key, value) - // grab all data from the state. - encAllValue, err := s.GetPublicState(SingleKey) +func (s *SkvsStubInterface) InitSKVS() error { + logger.Warningf(" === Initializing SKVS === ") + + // get current state, this will only operate once + encValue, err := s.GetPublicState(s.key) if err != nil { - return err + return nil } - var allData map[string]string - if len(encAllValue) == 0 { - // current world state is empty - allData = map[string]string{} + if len(encValue) == 0 { + logger.Warningf("SKVS is empty, Initiating.") } else { - allValue, err := s.sep.DecryptState(encAllValue) + value, err := s.sep.DecryptState(encValue) if err != nil { return err } - // Unmarshal the JSON data into the interface{} value - err = json.Unmarshal(allValue, &allData) + logger.Warningf("SKVS has default value, loading current value.") + + err = json.Unmarshal(value, &s.allDataOld) + err = json.Unmarshal(value, &s.allDataNew) if err != nil { logger.Errorf("SKVS Json unmarshal error: %s", err) return err } } - logger.Warningf("Calling Put State (Mid-1), decrypt succeed, allData = %s", allData) - valueHex := hex.EncodeToString(value) + logger.Warningf("SKVS Init finish, allDataOld = %s, allDataNew = %s", s.allDataOld, s.allDataNew) + return nil +} + +func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { + logger.Warningf("Calling Get State (Start), key = %s, alldataOld = %s", key, s.allDataOld) + targetHex, found := s.allDataOld[key] + if found != true { + return nil, errors.New("skvs allDataOld key not found") + } + targetBytes, err := hex.DecodeString(targetHex) + logger.Warningf("Calling Get State (End), TargetHex: %s, TargetBytes: %x, err: %s", targetHex, targetBytes, err) + return targetBytes, err +} + +func (s *SkvsStubInterface) PutState(key string, value []byte) error { + logger.Warningf("Calling Put State (Start), key = %s, value = %x, alldata = %s", key, value, s.allDataNew) - allData[key] = valueHex - logger.Warningf("Calling Put State (Mid-2), add need data key = %s, valueHex = %s, allData = %s", key, valueHex, allData) + valueHex := hex.EncodeToString(value) + s.allDataNew[key] = valueHex + logger.Warningf("Calling Put State (Mid-1), add need data key = %s, valueHex = %s, allData = %s", key, valueHex, s.allDataNew) - byteAllData, err := json.Marshal(allData) + byteAllData, err := json.Marshal(s.allDataNew) if err != nil { return err } - logger.Warningf("Calling Put State (Mid-3), successfull marshal allData, byteAlldata = %x", byteAllData) + logger.Warningf("Calling Put State (Mid-2), successfull marshal allData, byteAlldata = %x", byteAllData) encValue, err := s.sep.EncryptState(byteAllData) if err != nil { @@ -104,5 +93,5 @@ func (s *SkvsStubInterface) PutState(key string, value []byte) error { } logger.Warningf("Calling Put State (End), put encValue %x", encValue) - return s.PutPublicState(SingleKey, encValue) + return s.PutPublicState(s.key, encValue) } From eafbf169ae6d19e785d61a7a81d51b25aedfcae4 Mon Sep 17 00:00:00 2001 From: Flamewind97 Date: Fri, 14 Apr 2023 16:21:14 +0200 Subject: [PATCH 04/10] change storing type and add del state Signed-off-by: chenchanglew --- .../enclave_go/skvs_stub_Interface.go | 50 ++++++++++++------- samples/chaincode/secret-keeper-go/main.go | 3 -- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go index 86e1b2425..581248e0b 100644 --- a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go +++ b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go @@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0 package enclave_go import ( - "encoding/hex" "encoding/json" "github.com/hyperledger/fabric-chaincode-go/shim" @@ -17,15 +16,15 @@ import ( type SkvsStubInterface struct { *FpcStubInterface - allDataOld map[string]string - allDataNew map[string]string + allDataOld map[string][]byte + allDataNew map[string][]byte key string } func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { logger.Warning("==== Get New Skvs Interface =====") fpcStub := NewFpcStubInterface(stub, input, rwset, sep) - skvsStub := SkvsStubInterface{fpcStub, map[string]string{}, map[string]string{}, "SKVS"} + skvsStub := SkvsStubInterface{fpcStub, map[string][]byte{}, map[string][]byte{}, "SKVS"} err := skvsStub.InitSKVS() if err != nil { logger.Warningf("Error!! Initializing SKVS failed") @@ -59,39 +58,54 @@ func (s *SkvsStubInterface) InitSKVS() error { } } - logger.Warningf("SKVS Init finish, allDataOld = %s, allDataNew = %s", s.allDataOld, s.allDataNew) + logger.Warningf("SKVS Init finish, allDataOld: %s, allDataNew: %s", s.allDataOld, s.allDataNew) return nil } func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { - logger.Warningf("Calling Get State (Start), key = %s, alldataOld = %s", key, s.allDataOld) - targetHex, found := s.allDataOld[key] + logger.Warningf("Calling Get State (Start), key: %s, alldataOld: %s", key, s.allDataOld) + value, found := s.allDataOld[key] if found != true { return nil, errors.New("skvs allDataOld key not found") } - targetBytes, err := hex.DecodeString(targetHex) - logger.Warningf("Calling Get State (End), TargetHex: %s, TargetBytes: %x, err: %s", targetHex, targetBytes, err) - return targetBytes, err + logger.Warningf("Calling Get State (End), key: %s, value: %x", key, value) + return value, nil } func (s *SkvsStubInterface) PutState(key string, value []byte) error { - logger.Warningf("Calling Put State (Start), key = %s, value = %x, alldata = %s", key, value, s.allDataNew) - - valueHex := hex.EncodeToString(value) - s.allDataNew[key] = valueHex - logger.Warningf("Calling Put State (Mid-1), add need data key = %s, valueHex = %s, allData = %s", key, valueHex, s.allDataNew) + logger.Warningf("Calling Put State (Start), key: %s, value: %x, alldata: %s", key, value, s.allDataNew) + s.allDataNew[key] = value byteAllData, err := json.Marshal(s.allDataNew) if err != nil { return err } - logger.Warningf("Calling Put State (Mid-2), successfull marshal allData, byteAlldata = %x", byteAllData) - encValue, err := s.sep.EncryptState(byteAllData) if err != nil { return err } - logger.Warningf("Calling Put State (End), put encValue %x", encValue) + logger.Warningf("Calling Put State (End), put encValue: %x", encValue) return s.PutPublicState(s.key, encValue) } + +func (s *SkvsStubInterface) DelState(key string) error { + delete(s.allDataNew, key) + byteAllData, err := json.Marshal(s.allDataNew) + if err != nil { + return err + } + encValue, err := s.sep.EncryptState(byteAllData) + if err != nil { + return err + } + return s.PutPublicState(s.key, encValue) +} + +func (s *SkvsStubInterface) GetStateByRange(startKey string, endKey string) (shim.StateQueryIteratorInterface, error) { + panic("not implemented") // TODO: Implement +} + +func (s *SkvsStubInterface) GetStateByRangeWithPagination(startKey string, endKey string, pageSize int32, bookmark string) (shim.StateQueryIteratorInterface, *pb.QueryResponseMetadata, error) { + panic("not implemented") // TODO: Implement +} diff --git a/samples/chaincode/secret-keeper-go/main.go b/samples/chaincode/secret-keeper-go/main.go index 7ab8b9b07..1dc1af38a 100644 --- a/samples/chaincode/secret-keeper-go/main.go +++ b/samples/chaincode/secret-keeper-go/main.go @@ -26,9 +26,6 @@ func main() { // chaincode := fpc.NewPrivateChaincode(secretChaincode) skvsChaincode := fpc.NewSkvsChaincode(secretChaincode) - // // single KVS - // skvsChaincode := &fpc.SKVSWrapper{chaincode} - // start chaincode as a service server := &shim.ChaincodeServer{ CCID: ccid, From 8fb8b84cd1a83de40d9ef331434a5669f1607275 Mon Sep 17 00:00:00 2001 From: chenchanglew Date: Tue, 11 Jun 2024 15:02:37 +0200 Subject: [PATCH 05/10] refactor: clean up code Signed-off-by: chenchanglew --- ecc_go/chaincode/enclave_go/skvs_stub.go | 2 +- ecc_go/chaincode/singleKVS.go | 128 ----------------------- 2 files changed, 1 insertion(+), 129 deletions(-) diff --git a/ecc_go/chaincode/enclave_go/skvs_stub.go b/ecc_go/chaincode/enclave_go/skvs_stub.go index 3d68f116d..03152d312 100644 --- a/ecc_go/chaincode/enclave_go/skvs_stub.go +++ b/ecc_go/chaincode/enclave_go/skvs_stub.go @@ -26,7 +26,7 @@ func NewSkvsStub(cc shim.Chaincode) *skvsStub { } func (e *skvsStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) ([]byte, error) { - logger.Error("==== SKVS ChaincodeInvoke ====") + logger.Warning("==== SKVS ChaincodeInvoke ====") signedProposal, err := stub.GetSignedProposal() if err != nil { diff --git a/ecc_go/chaincode/singleKVS.go b/ecc_go/chaincode/singleKVS.go index e756b8cbd..b34e0137a 100644 --- a/ecc_go/chaincode/singleKVS.go +++ b/ecc_go/chaincode/singleKVS.go @@ -26,131 +26,3 @@ func NewSkvsChaincode(cc shim.Chaincode) *chaincode.EnclaveChaincode { } return ecc } - -// type skvsStub struct { -// *enclave_go.EnclaveStub -// } - -// func NewSkvsStub(cc shim.Chaincode) *skvsStub { -// enclaveStub := enclave_go.NewEnclaveStub(cc) -// return &skvsStub{enclaveStub} -// } - -// func (e *skvsStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) ([]byte, error) { -// logger.Error("==== SKVS ChaincodeInvoke ====") - -// signedProposal, err := stub.GetSignedProposal() -// if err != nil { -// shim.Error(err.Error()) -// } - -// if err := e.verifySignedProposal(stub, chaincodeRequestMessageBytes); err != nil { -// return nil, errors.Wrap(err, "signed proposal verification failed") -// } - -// // unmarshal chaincodeRequest -// chaincodeRequestMessage := &protos.ChaincodeRequestMessage{} -// err = proto.Unmarshal(chaincodeRequestMessageBytes, chaincodeRequestMessage) -// if err != nil { -// return nil, err -// } - -// // get key transport message including the encryption keys for request and response -// keyTransportMessage, err := e.extractKeyTransportMessage(chaincodeRequestMessage) -// if err != nil { -// return nil, errors.Wrap(err, "cannot extract keyTransportMessage") -// } - -// // decrypt request -// cleartextChaincodeRequest, err := e.extractCleartextChaincodeRequest(chaincodeRequestMessage, keyTransportMessage) -// if err != nil { -// return nil, errors.Wrap(err, "cannot decrypt chaincode request") -// } - -// // create a new instance of a FPC RWSet that we pass to the stub and later return with the response -// rwset := NewReadWriteSet() - -// // Invoke chaincode -// // we wrap the stub with our FpcStubInterface -// // ** Implement our own FpcStubInterface -// skvsStub := NewSkvsStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) -// ccResponse := e.ccRef.Invoke(skvsStub) -// // ** -// // fpcStub := NewFpcStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) -// // ccResponse := e.ccRef.Invoke(fpcStub) - -// // marshal chaincode response -// ccResponseBytes, err := protoutil.Marshal(&ccResponse) -// if err != nil { -// return nil, err -// } - -// //encrypt response -// encryptedResponse, err := e.csp.EncryptMessage(keyTransportMessage.GetResponseEncryptionKey(), ccResponseBytes) -// if err != nil { -// return nil, err -// } - -// chaincodeRequestMessageHash := sha256.Sum256(chaincodeRequestMessageBytes) - -// response := &protos.ChaincodeResponseMessage{ -// EncryptedResponse: encryptedResponse, -// FpcRwSet: rwset.ToFPCKVSet(), -// EnclaveId: e.identity.GetEnclaveId(), -// Proposal: signedProposal, -// ChaincodeRequestMessageHash: chaincodeRequestMessageHash[:], -// } - -// responseBytes, err := proto.Marshal(response) -// if err != nil { -// return nil, err -// } - -// // create signature -// sig, err := e.identity.Sign(responseBytes) -// if err != nil { -// return nil, err -// } - -// signedResponse := &protos.SignedChaincodeResponseMessage{ -// ChaincodeResponseMessage: responseBytes, -// Signature: sig, -// } - -// return proto.Marshal(signedResponse) -// } - -// func (e *skvsStub) verifySignedProposal(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) error { -// return e.EnclaveStub.verifySignedProposal(stub, chaincodeRequestMessageBytes) -// } - -// type SkvsStubInterface struct { -// *enclave_go.FpcStubInterface -// } - -// func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { -// fpcStub := enclave_go.NewFpcStubInterface(stub, input, rwset, sep) -// return &SkvsStubInterface{fpcStub} -// } - -// func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { -// encValue, err := s.GetPublicState(SingleKey) -// if err != nil { -// return nil, err -// } - -// // in case the key does not exist, return early -// if len(encValue) == 0 { -// return nil, nil -// } - -// return s.sep.DecryptState(encValue) -// } - -// func (s *SkvsStubInterface) PutState(key string, value []byte) error { -// encValue, err := s.sep.EncryptState(SingleKey) -// if err != nil { -// return err -// } -// return s.PutPublicState(key, encValue) -// } From 6583c84626917e93c776340cc360483f407c7f61 Mon Sep 17 00:00:00 2001 From: chenchanglew Date: Wed, 12 Jun 2024 13:45:41 +0200 Subject: [PATCH 06/10] lint: make checks Signed-off-by: chenchanglew --- ecc_go/chaincode/enclave_go/skvs_stub.go | 3 ++- ecc_go/chaincode/enclave_go/skvs_stub_Interface.go | 6 +++++- ecc_go/chaincode/singleKVS.go | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ecc_go/chaincode/enclave_go/skvs_stub.go b/ecc_go/chaincode/enclave_go/skvs_stub.go index 03152d312..27b9d94ce 100644 --- a/ecc_go/chaincode/enclave_go/skvs_stub.go +++ b/ecc_go/chaincode/enclave_go/skvs_stub.go @@ -8,6 +8,7 @@ package enclave_go import ( "crypto/sha256" + "fmt" "github.com/hyperledger/fabric-chaincode-go/shim" "github.com/hyperledger/fabric-private-chaincode/internal/protos" @@ -30,7 +31,7 @@ func (e *skvsStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRe signedProposal, err := stub.GetSignedProposal() if err != nil { - shim.Error(err.Error()) + return nil, fmt.Errorf("cannot get signed proposal: %s", err.Error()) } if err := e.verifySignedProposal(stub, chaincodeRequestMessageBytes); err != nil { diff --git a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go index 581248e0b..64aa92ce3 100644 --- a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go +++ b/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go @@ -51,6 +51,10 @@ func (s *SkvsStubInterface) InitSKVS() error { logger.Warningf("SKVS has default value, loading current value.") err = json.Unmarshal(value, &s.allDataOld) + if err != nil { + logger.Errorf("SKVS Json unmarshal error: %s", err) + return err + } err = json.Unmarshal(value, &s.allDataNew) if err != nil { logger.Errorf("SKVS Json unmarshal error: %s", err) @@ -65,7 +69,7 @@ func (s *SkvsStubInterface) InitSKVS() error { func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { logger.Warningf("Calling Get State (Start), key: %s, alldataOld: %s", key, s.allDataOld) value, found := s.allDataOld[key] - if found != true { + if !found { return nil, errors.New("skvs allDataOld key not found") } logger.Warningf("Calling Get State (End), key: %s, value: %x", key, value) diff --git a/ecc_go/chaincode/singleKVS.go b/ecc_go/chaincode/singleKVS.go index b34e0137a..b78c54185 100644 --- a/ecc_go/chaincode/singleKVS.go +++ b/ecc_go/chaincode/singleKVS.go @@ -18,6 +18,7 @@ import ( var logger = flogging.MustGetLogger("enclave_go") func NewSkvsChaincode(cc shim.Chaincode) *chaincode.EnclaveChaincode { + logger.Info("Creating new SKVS Chaincode") ecc := &chaincode.EnclaveChaincode{ Enclave: enclave_go.NewSkvsStub(cc), Validator: endorsement.NewValidator(), From b14f71f4c8afb9f02374cfe3507669f8d061e11b Mon Sep 17 00:00:00 2001 From: chenchanglew Date: Tue, 20 Aug 2024 16:46:56 +0200 Subject: [PATCH 07/10] chore: change filename to small letters Signed-off-by: chenchanglew --- .../enclave_go/{skvs_stub_Interface.go => skvs_stub_interface.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ecc_go/chaincode/enclave_go/{skvs_stub_Interface.go => skvs_stub_interface.go} (100%) diff --git a/ecc_go/chaincode/enclave_go/skvs_stub_Interface.go b/ecc_go/chaincode/enclave_go/skvs_stub_interface.go similarity index 100% rename from ecc_go/chaincode/enclave_go/skvs_stub_Interface.go rename to ecc_go/chaincode/enclave_go/skvs_stub_interface.go From 3e086dd231c54ad99ad31cff25f29d60f3758f9f Mon Sep 17 00:00:00 2001 From: chenchanglew Date: Tue, 26 Nov 2024 18:01:17 +0100 Subject: [PATCH 08/10] chore: refactor SkvsStubInterface Signed-off-by: chenchanglew --- ecc_go/chaincode/enclave_go/enclave.go | 7 +- ecc_go/chaincode/enclave_go/skvs_stub.go | 102 ++--------------------- 2 files changed, 12 insertions(+), 97 deletions(-) diff --git a/ecc_go/chaincode/enclave_go/enclave.go b/ecc_go/chaincode/enclave_go/enclave.go index 04545d894..3b25a8729 100644 --- a/ecc_go/chaincode/enclave_go/enclave.go +++ b/ecc_go/chaincode/enclave_go/enclave.go @@ -18,6 +18,7 @@ import ( "github.com/hyperledger/fabric-private-chaincode/ecc_go/chaincode/enclave_go/attestation" "github.com/hyperledger/fabric-private-chaincode/internal/crypto" "github.com/hyperledger/fabric-private-chaincode/internal/protos" + pb "github.com/hyperledger/fabric-protos-go/peer" "github.com/hyperledger/fabric/bccsp" "github.com/hyperledger/fabric/bccsp/factory" "github.com/hyperledger/fabric/common/flogging" @@ -37,6 +38,7 @@ type EnclaveStub struct { hostParams *protos.HostParameters chaincodeParams *protos.CCParameters fabricCryptoProvider bccsp.BCCSP + stubProvider func(shim.ChaincodeStubInterface, *pb.ChaincodeInput, *readWriteSet, StateEncryptionFunctions) shim.ChaincodeStubInterface } func NewEnclaveStub(cc shim.Chaincode) *EnclaveStub { @@ -49,6 +51,9 @@ func NewEnclaveStub(cc shim.Chaincode) *EnclaveStub { csp: crypto.GetDefaultCSP(), ccRef: cc, fabricCryptoProvider: cryptoProvider, + stubProvider: func(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) shim.ChaincodeStubInterface { + return NewFpcStubInterface(stub, input, rwset, sep) + }, } } @@ -161,7 +166,7 @@ func (e *EnclaveStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincod // Invoke chaincode // we wrap the stub with our FpcStubInterface - fpcStub := NewFpcStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) + fpcStub := e.stubProvider(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) ccResponse := e.ccRef.Invoke(fpcStub) // marshal chaincode response diff --git a/ecc_go/chaincode/enclave_go/skvs_stub.go b/ecc_go/chaincode/enclave_go/skvs_stub.go index 27b9d94ce..5162fcf0a 100644 --- a/ecc_go/chaincode/enclave_go/skvs_stub.go +++ b/ecc_go/chaincode/enclave_go/skvs_stub.go @@ -7,105 +7,15 @@ SPDX-License-Identifier: Apache-2.0 package enclave_go import ( - "crypto/sha256" - "fmt" - "github.com/hyperledger/fabric-chaincode-go/shim" - "github.com/hyperledger/fabric-private-chaincode/internal/protos" - "github.com/hyperledger/fabric/protoutil" - "github.com/pkg/errors" - "google.golang.org/protobuf/proto" + pb "github.com/hyperledger/fabric-protos-go/peer" ) -type skvsStub struct { - *EnclaveStub -} - -func NewSkvsStub(cc shim.Chaincode) *skvsStub { +func NewSkvsStub(cc shim.Chaincode) *EnclaveStub { + logger.Warning("==== SKVS NewSkvsStub ====") enclaveStub := NewEnclaveStub(cc) - return &skvsStub{enclaveStub} -} - -func (e *skvsStub) ChaincodeInvoke(stub shim.ChaincodeStubInterface, chaincodeRequestMessageBytes []byte) ([]byte, error) { - logger.Warning("==== SKVS ChaincodeInvoke ====") - - signedProposal, err := stub.GetSignedProposal() - if err != nil { - return nil, fmt.Errorf("cannot get signed proposal: %s", err.Error()) - } - - if err := e.verifySignedProposal(stub, chaincodeRequestMessageBytes); err != nil { - return nil, errors.Wrap(err, "signed proposal verification failed") - } - - // unmarshal chaincodeRequest - chaincodeRequestMessage := &protos.ChaincodeRequestMessage{} - err = proto.Unmarshal(chaincodeRequestMessageBytes, chaincodeRequestMessage) - if err != nil { - return nil, err - } - - // get key transport message including the encryption keys for request and response - keyTransportMessage, err := e.extractKeyTransportMessage(chaincodeRequestMessage) - if err != nil { - return nil, errors.Wrap(err, "cannot extract keyTransportMessage") - } - - // decrypt request - cleartextChaincodeRequest, err := e.extractCleartextChaincodeRequest(chaincodeRequestMessage, keyTransportMessage) - if err != nil { - return nil, errors.Wrap(err, "cannot decrypt chaincode request") - } - - // create a new instance of a FPC RWSet that we pass to the stub and later return with the response - rwset := NewReadWriteSet() - - // Invoke chaincode - // we wrap the stub with our FpcStubInterface - // ** Implement our own FpcStubInterface - skvsStub := NewSkvsStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) - ccResponse := e.ccRef.Invoke(skvsStub) - // ** - // fpcStub := NewFpcStubInterface(stub, cleartextChaincodeRequest.GetInput(), rwset, e.ccKeys) - // ccResponse := e.ccRef.Invoke(fpcStub) - - // marshal chaincode response - ccResponseBytes, err := protoutil.Marshal(&ccResponse) - if err != nil { - return nil, err - } - - //encrypt response - encryptedResponse, err := e.csp.EncryptMessage(keyTransportMessage.GetResponseEncryptionKey(), ccResponseBytes) - if err != nil { - return nil, err - } - - chaincodeRequestMessageHash := sha256.Sum256(chaincodeRequestMessageBytes) - - response := &protos.ChaincodeResponseMessage{ - EncryptedResponse: encryptedResponse, - FpcRwSet: rwset.ToFPCKVSet(), - EnclaveId: e.identity.GetEnclaveId(), - Proposal: signedProposal, - ChaincodeRequestMessageHash: chaincodeRequestMessageHash[:], + enclaveStub.stubProvider = func(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) shim.ChaincodeStubInterface { + return NewSkvsStubInterface(stub, input, rwset, sep) } - - responseBytes, err := proto.Marshal(response) - if err != nil { - return nil, err - } - - // create signature - sig, err := e.identity.Sign(responseBytes) - if err != nil { - return nil, err - } - - signedResponse := &protos.SignedChaincodeResponseMessage{ - ChaincodeResponseMessage: responseBytes, - Signature: sig, - } - - return proto.Marshal(signedResponse) + return enclaveStub } From b29bccac204e388bd8a724783923eabb7840b326 Mon Sep 17 00:00:00 2001 From: chenchanglew Date: Mon, 7 Oct 2024 09:46:01 +0200 Subject: [PATCH 09/10] feat: change secret-keeper-go makefile Signed-off-by: chenchanglew --- ecc_go/build.mk | 5 ++- samples/chaincode/secret-keeper-go/Makefile | 12 ++++++ .../secret-keeper-go/cmd/naive/main.go | 41 ++++++++++++++++++ .../secret-keeper-go/cmd/skvs/main.go | 42 +++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 samples/chaincode/secret-keeper-go/cmd/naive/main.go create mode 100644 samples/chaincode/secret-keeper-go/cmd/skvs/main.go diff --git a/ecc_go/build.mk b/ecc_go/build.mk index 70d7aee2b..0d6f7f363 100644 --- a/ecc_go/build.mk +++ b/ecc_go/build.mk @@ -16,11 +16,12 @@ DOCKER_FILE ?= $(FPC_PATH)/ecc_go/Dockerfile EGO_CONFIG_FILE ?= $(FPC_PATH)/ecc_go/enclave.json ECC_BINARY ?= ecc ECC_BUNDLE ?= $(ECC_BINARY)-bundle +MAIN_GO_PATH ?= main.go build: ecc docker env ecc: ecc_dependencies - ego-go build $(GOTAGS) -o $(ECC_BINARY) main.go + ego-go build $(GOTAGS) -o $(ECC_BINARY) $(MAIN_GO_PATH) cp $(EGO_CONFIG_FILE) . ego sign ego uniqueid $(ECC_BINARY) > mrenclave @@ -28,7 +29,7 @@ ecc: ecc_dependencies .PHONY: with_go with_go: ecc_dependencies - $(GO) build $(GOTAGS) -o $(ECC_BUNDLE) main.go + $(GO) build $(GOTAGS) -o $(ECC_BUNDLE) $(MAIN_GO_PATH) echo "fake_mrenclave" > mrenclave ecc_dependencies: diff --git a/samples/chaincode/secret-keeper-go/Makefile b/samples/chaincode/secret-keeper-go/Makefile index 8f80af00d..1aaba8224 100644 --- a/samples/chaincode/secret-keeper-go/Makefile +++ b/samples/chaincode/secret-keeper-go/Makefile @@ -7,3 +7,15 @@ TOP = ../../.. include $(TOP)/ecc_go/build.mk CC_NAME ?= fpc-secret-keeper-go + +# Define paths for cmd subdirectories +NAIVE_PATH = cmd/naive/main.go +SKVS_PATH = cmd/skvs/main.go + +# Target to build naive version +naive: + $(MAKE) build MAIN_GO_PATH=$(NAIVE_PATH) + +# Target to build skvs version +skvs: + $(MAKE) build MAIN_GO_PATH=$(SKVS_PATH) diff --git a/samples/chaincode/secret-keeper-go/cmd/naive/main.go b/samples/chaincode/secret-keeper-go/cmd/naive/main.go new file mode 100644 index 000000000..a8f707f1b --- /dev/null +++ b/samples/chaincode/secret-keeper-go/cmd/naive/main.go @@ -0,0 +1,41 @@ +/* +Copyright IBM Corp. All Rights Reserved. +Copyright 2020 Intel Corporation + +SPDX-License-Identifier: Apache-2.0 +*/ + +package main + +import ( + "os" + + "github.com/hyperledger/fabric-chaincode-go/shim" + "github.com/hyperledger/fabric-contract-api-go/contractapi" + fpc "github.com/hyperledger/fabric-private-chaincode/ecc_go/chaincode" + "github.com/hyperledger/fabric-private-chaincode/samples/chaincode/secret-keeper-go/chaincode" +) + +func main() { + + ccid := os.Getenv("CHAINCODE_PKG_ID") + addr := os.Getenv("CHAINCODE_SERVER_ADDRESS") + + // create chaincode + secretChaincode, _ := contractapi.NewChaincode(&chaincode.SecretKeeper{}) + chaincode := fpc.NewPrivateChaincode(secretChaincode) + + // start chaincode as a service + server := &shim.ChaincodeServer{ + CCID: ccid, + Address: addr, + CC: chaincode, + TLSProps: shim.TLSProperties{ + Disabled: true, // just for testing good enough + }, + } + + if err := server.Start(); err != nil { + panic(err) + } +} diff --git a/samples/chaincode/secret-keeper-go/cmd/skvs/main.go b/samples/chaincode/secret-keeper-go/cmd/skvs/main.go new file mode 100644 index 000000000..1dc1af38a --- /dev/null +++ b/samples/chaincode/secret-keeper-go/cmd/skvs/main.go @@ -0,0 +1,42 @@ +/* +Copyright IBM Corp. All Rights Reserved. +Copyright 2020 Intel Corporation + +SPDX-License-Identifier: Apache-2.0 +*/ + +package main + +import ( + "os" + + "github.com/hyperledger/fabric-chaincode-go/shim" + "github.com/hyperledger/fabric-contract-api-go/contractapi" + fpc "github.com/hyperledger/fabric-private-chaincode/ecc_go/chaincode" + "github.com/hyperledger/fabric-private-chaincode/samples/chaincode/secret-keeper-go/chaincode" +) + +func main() { + + ccid := os.Getenv("CHAINCODE_PKG_ID") + addr := os.Getenv("CHAINCODE_SERVER_ADDRESS") + + // create chaincode + secretChaincode, _ := contractapi.NewChaincode(&chaincode.SecretKeeper{}) + // chaincode := fpc.NewPrivateChaincode(secretChaincode) + skvsChaincode := fpc.NewSkvsChaincode(secretChaincode) + + // start chaincode as a service + server := &shim.ChaincodeServer{ + CCID: ccid, + Address: addr, + CC: skvsChaincode, + TLSProps: shim.TLSProperties{ + Disabled: true, // just for testing good enough + }, + } + + if err := server.Start(); err != nil { + panic(err) + } +} From 754f02d86bb9a6d8420d02c38ab0da541dcdf10f Mon Sep 17 00:00:00 2001 From: chenchanglew Date: Mon, 14 Oct 2024 20:45:08 +0200 Subject: [PATCH 10/10] fix: edit makefile Signed-off-by: chenchanglew --- samples/chaincode/secret-keeper-go/Makefile | 11 ++---- samples/chaincode/secret-keeper-go/main.go | 42 --------------------- 2 files changed, 3 insertions(+), 50 deletions(-) delete mode 100644 samples/chaincode/secret-keeper-go/main.go diff --git a/samples/chaincode/secret-keeper-go/Makefile b/samples/chaincode/secret-keeper-go/Makefile index 1aaba8224..ab6d5f6b1 100644 --- a/samples/chaincode/secret-keeper-go/Makefile +++ b/samples/chaincode/secret-keeper-go/Makefile @@ -4,18 +4,13 @@ # SPDX-License-Identifier: Apache-2.0 TOP = ../../.. -include $(TOP)/ecc_go/build.mk CC_NAME ?= fpc-secret-keeper-go # Define paths for cmd subdirectories -NAIVE_PATH = cmd/naive/main.go +DEFAULT= cmd/naive/main.go SKVS_PATH = cmd/skvs/main.go -# Target to build naive version -naive: - $(MAKE) build MAIN_GO_PATH=$(NAIVE_PATH) +MAIN_GO_PATH ?=$(DEFAULT) -# Target to build skvs version -skvs: - $(MAKE) build MAIN_GO_PATH=$(SKVS_PATH) +include $(TOP)/ecc_go/build.mk diff --git a/samples/chaincode/secret-keeper-go/main.go b/samples/chaincode/secret-keeper-go/main.go deleted file mode 100644 index 1dc1af38a..000000000 --- a/samples/chaincode/secret-keeper-go/main.go +++ /dev/null @@ -1,42 +0,0 @@ -/* -Copyright IBM Corp. All Rights Reserved. -Copyright 2020 Intel Corporation - -SPDX-License-Identifier: Apache-2.0 -*/ - -package main - -import ( - "os" - - "github.com/hyperledger/fabric-chaincode-go/shim" - "github.com/hyperledger/fabric-contract-api-go/contractapi" - fpc "github.com/hyperledger/fabric-private-chaincode/ecc_go/chaincode" - "github.com/hyperledger/fabric-private-chaincode/samples/chaincode/secret-keeper-go/chaincode" -) - -func main() { - - ccid := os.Getenv("CHAINCODE_PKG_ID") - addr := os.Getenv("CHAINCODE_SERVER_ADDRESS") - - // create chaincode - secretChaincode, _ := contractapi.NewChaincode(&chaincode.SecretKeeper{}) - // chaincode := fpc.NewPrivateChaincode(secretChaincode) - skvsChaincode := fpc.NewSkvsChaincode(secretChaincode) - - // start chaincode as a service - server := &shim.ChaincodeServer{ - CCID: ccid, - Address: addr, - CC: skvsChaincode, - TLSProps: shim.TLSProperties{ - Disabled: true, // just for testing good enough - }, - } - - if err := server.Start(); err != nil { - panic(err) - } -}