From e306ea660c019d69ac3acd2fb55ac778c54d1479 Mon Sep 17 00:00:00 2001 From: bur Date: Wed, 21 Oct 2020 22:15:11 +0200 Subject: [PATCH] Update FPC GO SDK extension - CreateEnclave requires target peer endpoint - FPC invocations are invoked (queried) at a defined endpoint Signed-off-by: bur --- .../go/fpc/attestation/transformation.go | 2 +- client_sdk/go/fpc/contract.go | 42 +++++++++++++++++-- client_sdk/go/fpc/lifecycle.go | 19 ++++++--- client_sdk/go/test/main.go | 13 ++++-- go.mod | 2 + 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/client_sdk/go/fpc/attestation/transformation.go b/client_sdk/go/fpc/attestation/transformation.go index 83db4707e..f221ae9a1 100644 --- a/client_sdk/go/fpc/attestation/transformation.go +++ b/client_sdk/go/fpc/attestation/transformation.go @@ -14,7 +14,7 @@ func ToEvidence(credentials *protos.Credentials) (*protos.Credentials, error) { // TODO call brunos attestation_to_evidence.sh // call $FPC_PATH/common/crypto/attestation-api/conversion/attestation_to_evidence.sh - credentials.Evidence = credentials.Attestation + //credentials.Evidence = credentials.Attestation return credentials, nil } diff --git a/client_sdk/go/fpc/contract.go b/client_sdk/go/fpc/contract.go index d02336fef..54b366c7b 100644 --- a/client_sdk/go/fpc/contract.go +++ b/client_sdk/go/fpc/contract.go @@ -30,13 +30,14 @@ type ContractInterface interface { func GetContract(network *gateway.Network, chaincodeId string) ContractInterface { contract := network.GetContract(chaincodeId) ercc := network.GetContract("ercc") - return &Contract{contract, ercc, nil} + return &Contract{contract, ercc, nil, nil} } type Contract struct { contract *gateway.Contract ercc *gateway.Contract cachedChaincodeEncryptionKey []byte + enclavePeers []string } func (c *Contract) Name() string { @@ -54,6 +55,17 @@ func (c *Contract) getChaincodeEncryptionKey() ([]byte, error) { return c.cachedChaincodeEncryptionKey, nil } +func (c *Contract) getEnclavePeers() ([]string, error) { + if c.enclavePeers == nil { + ccKeyBytes, err := c.ercc.EvaluateTransaction("queryChaincodeEncryptionKey", c.Name()) + if err != nil { + return nil, err + } + c.cachedChaincodeEncryptionKey = ccKeyBytes + } + return c.enclavePeers, nil +} + func (c *Contract) prepareChaincodeInvocation(name string, args []string, resultEncryptionKey []byte) (string, error) { p := &utils.ChaincodeParams{ Function: name, @@ -94,8 +106,18 @@ func (c *Contract) EvaluateTransaction(name string, args ...string) ([]byte, err return nil, err } + // note that WithEndorsingPeers is only used with txn.Submit!!! + // GO SDK needs to be patched! We should create a PR for that! + txn, err := c.contract.CreateTransaction( + "__invoke", + gateway.WithEndorsingPeers(c.enclavePeers...), + ) + if err != nil { + return nil, err + } + log.Printf("calling __invoke!\n") - responseBytes, err := c.contract.EvaluateTransaction("__invoke", encryptedParamsBase64) + responseBytes, err := txn.Evaluate(encryptedParamsBase64) if err != nil { return nil, err } @@ -122,10 +144,20 @@ func (c *Contract) SubmitTransaction(name string, args ...string) ([]byte, error return nil, err } + txn, err := c.contract.CreateTransaction( + "__invoke", + gateway.WithEndorsingPeers(c.enclavePeers...), + ) + if err != nil { + return nil, err + } + log.Printf("calling __invoke!\n") + //responseBytes, err := c.contract.EvaluateTransaction("__invoke", encryptedParamsBase64) + responseBytes, err := txn.Evaluate(encryptedParamsBase64) // first invoke (query) fpc chaincode - responseBytes, err := c.contract.EvaluateTransaction("__invoke", encryptedParamsBase64) + //responseBytes, err := c.contract.EvaluateTransaction("__invoke", encryptedParamsBase64) if err != nil { return nil, errors.Wrap(err, "evaluation transaction failed") } @@ -145,6 +177,10 @@ func (c *Contract) SubmitTransaction(name string, args ...string) ([]byte, error return Decrypt(response.ResponseData, resultEncryptionKey) } +//func (c *Contract) CreateTransaction(name string, opts ...gateway.TransactionOption) (*gateway.Transaction, error) { +// return c.CreateTransaction(name, opts...) +//} + func (c *Contract) RegisterEvent(eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error) { return c.contract.RegisterEvent(eventFilter) } diff --git a/client_sdk/go/fpc/lifecycle.go b/client_sdk/go/fpc/lifecycle.go index fc1960cad..e25aefb71 100644 --- a/client_sdk/go/fpc/lifecycle.go +++ b/client_sdk/go/fpc/lifecycle.go @@ -6,31 +6,38 @@ import ( "fmt" "log" + "github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/fpc/attestation" "github.com/hyperledger-labs/fabric-private-chaincode/internal/utils" "github.com/golang/protobuf/proto" - "github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/fpc/attestation" "github.com/hyperledger-labs/fabric-private-chaincode/internal/protos" "github.com/hyperledger/fabric-sdk-go/pkg/gateway" "github.com/hyperledger/fabric/protoutil" ) type ManagementInterface interface { - CreateEnclave(attestationParams ...string) error + CreateEnclave(peer string, attestationParams ...string) error } type ManagementAPI struct { network *gateway.Network } -func (c *Contract) CreateEnclave(attestationParams ...string) error { +func (c *Contract) CreateEnclave(peer string, attestationParams ...string) error { p, err := json.Marshal(&utils.AttestationParams{Params: attestationParams}) attestationParamsBase64 := base64.StdEncoding.EncodeToString(p) - log.Printf("Prep attestation params: %s\n", attestationParamsBase64) - credentialsBytes, err := c.contract.EvaluateTransaction("__initEnclave", attestationParamsBase64) + txn, err := c.contract.CreateTransaction( + "__initEnclave", + gateway.WithEndorsingPeers(peer), + ) + if err != nil { + return err + } + + credentialsBytes, err := txn.Evaluate(attestationParamsBase64) if err != nil { return fmt.Errorf("evaluation error: %s", err) } @@ -57,5 +64,7 @@ func (c *Contract) CreateEnclave(attestationParams ...string) error { return err } + c.enclavePeers = append(c.enclavePeers, peer) + return nil } diff --git a/client_sdk/go/test/main.go b/client_sdk/go/test/main.go index 80199e9a7..fa60e5dc4 100644 --- a/client_sdk/go/test/main.go +++ b/client_sdk/go/test/main.go @@ -112,17 +112,24 @@ func main() { contract := fpc.GetContract(network, "ecc") // Setup Chaincode Enclave - // TODO How to specify where? log.Println("--> Create FPC chaincode enclave: ") attestationParams := []string{"some params"} - err = contract.CreateEnclave(attestationParams...) + err = contract.CreateEnclave("peer0.peer1.example.com:7051", attestationParams...) if err != nil { log.Fatalf("Failed to create enclave: %v", err) } + log.Println("--> QueryListEnclaveCredentials: ") + ercc := network.GetContract("ercc") + result, err := ercc.EvaluateTransaction("QueryListEnclaveCredentials", "ecc") + if err != nil { + log.Fatalf("Failed to Submit transaction: %v", err) + } + log.Printf("--> Result: %s\n", string(result)) + // Invoke FPC Chaincode log.Println("--> Invoke FPC chaincode: ") - result, err := contract.SubmitTransaction("myFunction", "arg1", "arg2", "arg3") + result, err = contract.SubmitTransaction("myFunction", "arg1", "arg2", "arg3") if err != nil { log.Fatalf("Failed to Submit transaction: %v", err) } diff --git a/go.mod b/go.mod index ae403869e..444e57822 100644 --- a/go.mod +++ b/go.mod @@ -37,3 +37,5 @@ require ( golang.org/x/tools v0.0.0-20200323164354-18ea2c8f7359 google.golang.org/protobuf v1.25.0 ) + +replace github.com/hyperledger/fabric-sdk-go => ../../hyperledger/fabric-sdk-go