Skip to content

Commit

Permalink
Update FPC GO SDK extension
Browse files Browse the repository at this point in the history
- CreateEnclave requires target peer endpoint
- FPC invocations are invoked (queried) at a defined endpoint

Signed-off-by: bur <[email protected]>
  • Loading branch information
mbrandenburger committed Oct 27, 2020
1 parent e2dd849 commit e306ea6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion client_sdk/go/fpc/attestation/transformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
42 changes: 39 additions & 3 deletions client_sdk/go/fpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
}
Expand All @@ -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)
}
Expand Down
19 changes: 14 additions & 5 deletions client_sdk/go/fpc/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -57,5 +64,7 @@ func (c *Contract) CreateEnclave(attestationParams ...string) error {
return err
}

c.enclavePeers = append(c.enclavePeers, peer)

return nil
}
13 changes: 10 additions & 3 deletions client_sdk/go/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e306ea6

Please sign in to comment.