Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choosing identity dynamically to call transactions #49

Merged
merged 13 commits into from
May 10, 2024
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.21

- name: Build
run: go build -v github.com/hyperledger-labs/cc-tools-demo/chaincode
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

## Directory Structure

- `/fabric`: Fabric network v2.2 used as a test environment
- `/fabric`: Fabric network v2.5 used as a test environment
- `/chaincode`: chaincode-related files
- `/ccapi`: chaincode REST API in Golang project

## Development

The `cc-tools` library has been tested in Fabric v1.4, v2.2 and v2.4 networks.
The `cc-tools` library has been tested in Fabric v2.2, v2.4 and v2.5 networks.

Dependencies for chaincode and chaincode API:

- Go 1.14 or higher
- Go 1.21 or higher

Dependencies for test environment:

Expand Down
2 changes: 1 addition & 1 deletion ccapi/chaincode/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func HandleEvent(channelName, ccName string, event EventHandler) {

func RegisterForEvents() {
// Get registered events on the chaincode
res, _, err := Invoke(os.Getenv("CHANNEL"), os.Getenv("CCNAME"), "getEvents", nil, nil)
res, _, err := Invoke(os.Getenv("CHANNEL"), os.Getenv("CCNAME"), "getEvents", os.Getenv("USER"), nil, nil)
if err != nil {
fmt.Println("error registering for events: ", err)
return
Expand Down
4 changes: 2 additions & 2 deletions ccapi/chaincode/eventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (event EventHandler) Execute(ccEvent *fab.CCEvent) {
cc = event.Chaincode
}

res, _, err := Invoke(ch, cc, event.Transaction, [][]byte{ccEvent.Payload}, nil)
res, _, err := Invoke(ch, cc, event.Transaction, os.Getenv("USER"), [][]byte{ccEvent.Payload}, nil)
if err != nil {
fmt.Println("error invoking transaction: ", err)
return
Expand Down Expand Up @@ -86,7 +86,7 @@ func (event EventHandler) Execute(ccEvent *fab.CCEvent) {
txName = "runEvent"
}

_, _, err := Invoke(os.Getenv("CHANNEL"), os.Getenv("CCNAME"), txName, [][]byte{args}, nil)
_, _, err := Invoke(os.Getenv("CHANNEL"), os.Getenv("CCNAME"), txName, os.Getenv("USER"), [][]byte{args}, nil)
if err != nil {
fmt.Println("error invoking transaction: ", err)
return
Expand Down
4 changes: 2 additions & 2 deletions ccapi/chaincode/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
)

func Invoke(channelName, ccName, txName string, txArgs [][]byte, transientRequest []byte) (*channel.Response, int, error) {
func Invoke(channelName, ccName, txName, user string, txArgs [][]byte, transientRequest []byte) (*channel.Response, int, error) {
// create channel manager
fabMngr, err := common.NewFabricChClient(channelName, os.Getenv("USER"), os.Getenv("ORG"))
fabMngr, err := common.NewFabricChClient(channelName, user, os.Getenv("ORG"))
if err != nil {
return nil, http.StatusInternalServerError, err
}
Expand Down
4 changes: 2 additions & 2 deletions ccapi/chaincode/invokeGateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/pkg/errors"
)

func InvokeGateway(channelName, chaincodeName, txName, args string, transientArgs []byte, endorsingOrgs []string) ([]byte, error) {
func InvokeGateway(channelName, chaincodeName, txName, args, user string, transientArgs []byte, endorsingOrgs []string) ([]byte, error) {
// Gateway endpoint
endpoint := os.Getenv("FABRIC_GATEWAY_ENDPOINT")

Expand All @@ -20,7 +20,7 @@ func InvokeGateway(channelName, chaincodeName, txName, args string, transientArg
defer grpcConn.Close()

// Create gateway connection
gw, err := common.CreateGatewayConnection(grpcConn)
gw, err := common.CreateGatewayConnection(grpcConn, user)
if err != nil {
return nil, errors.Wrap(err, "failed to create gateway connection")
}
Expand Down
4 changes: 2 additions & 2 deletions ccapi/chaincode/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
)

func Query(channelName, ccName, txName string, txArgs [][]byte) (*channel.Response, int, error) {
func Query(channelName, ccName, txName, user string, txArgs [][]byte) (*channel.Response, int, error) {
// create channel manager
fabMngr, err := common.NewFabricChClient(channelName, os.Getenv("USER"), os.Getenv("ORG"))
fabMngr, err := common.NewFabricChClient(channelName, user, os.Getenv("ORG"))
if err != nil {
return nil, http.StatusInternalServerError, err
}
Expand Down
4 changes: 2 additions & 2 deletions ccapi/chaincode/queryGateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
)

func QueryGateway(channelName, chaincodeName, txName, args string) ([]byte, error) {
func QueryGateway(channelName, chaincodeName, txName, args, user string) ([]byte, error) {
// Gateway endpoint
endpoint := os.Getenv("FABRIC_GATEWAY_ENDPOINT")

Expand All @@ -19,7 +19,7 @@ func QueryGateway(channelName, chaincodeName, txName, args string) ([]byte, erro
defer grpcConn.Close()

// Create gateway connection
gw, err := common.CreateGatewayConnection(grpcConn)
gw, err := common.CreateGatewayConnection(grpcConn, user)
if err != nil {
return nil, errors.Wrap(err, "failed to create gateway connection")
}
Expand Down
30 changes: 12 additions & 18 deletions ccapi/common/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
)

var (
gatewayId *identity.X509Identity
gatewaySign identity.Sign
gatewayTLSCredentials *credentials.TransportCredentials
)

Expand All @@ -43,26 +41,22 @@ func CreateGrpcConnection(endpoint string) (*grpc.ClientConn, error) {
return grpc.Dial(endpoint, grpc.WithTransportCredentials(*gatewayTLSCredentials))
}

func CreateGatewayConnection(grpcConn *grpc.ClientConn) (*client.Gateway, error) {
// Check identity was created
if gatewayId == nil {
id, err := newIdentity(getSignCert(os.Getenv("USER")), GetMSPID())
if err != nil {
return nil, errors.Wrap(err, "failed to create new identity")
}
gatewayId = id
func CreateGatewayConnection(grpcConn *grpc.ClientConn, user string) (*client.Gateway, error) {
// Create identity
id, err := newIdentity(getSignCert(user), GetMSPID())
if err != nil {
return nil, errors.Wrap(err, "failed to create new identity")
}
gatewayId := id

// Check sign function was created
if gatewaySign == nil {
sign, err := newSign(getSignKey(os.Getenv("USER")))
if err != nil {
return nil, errors.Wrap(err, "failed to create new sign function")
}

gatewaySign = sign
// Create sign function
sign, err := newSign(getSignKey(user))
if err != nil {
return nil, errors.Wrap(err, "failed to create new sign function")
}

gatewaySign := sign

// Create a Gateway connection for a specific client identity.
return client.Connect(
gatewayId,
Expand Down
7 changes: 6 additions & 1 deletion ccapi/handlers/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ func Invoke(c *gin.Context) {
argList = append(argList, args)
}

res, status, err := chaincode.Invoke(channelName, chaincodeName, txName, argList, transientMapByte)
user := c.GetHeader("User")
if user == "" {
user = "Admin"
}

res, status, err := chaincode.Invoke(channelName, chaincodeName, txName, user, argList, transientMapByte)
if err != nil {
common.Abort(c, status, err)
return
Expand Down
7 changes: 6 additions & 1 deletion ccapi/handlers/invokeGateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ func invokeGateway(c *gin.Context, channelName, chaincodeName string) {
}

// Invoke
result, err := chaincode.InvokeGateway(channelName, chaincodeName, txName, string(reqBytes), transientBytes, endorsers)
user := c.GetHeader("User")
if user == "" {
user = "Admin"
}

result, err := chaincode.InvokeGateway(channelName, chaincodeName, txName, string(reqBytes), user, transientBytes, endorsers)
if err != nil {
err, status := common.ParseError(err)
common.Abort(c, status, err)
Expand Down
7 changes: 6 additions & 1 deletion ccapi/handlers/invokeV1.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ func InvokeV1(c *gin.Context) {
argList = append(argList, args)
}

res, status, err := chaincode.Invoke(channelName, chaincodeName, txName, argList, transientMapByte)
user := c.GetHeader("User")
if user == "" {
user = "Admin"
}

res, status, err := chaincode.Invoke(channelName, chaincodeName, txName, user, argList, transientMapByte)
if err != nil {
common.Abort(c, status, err)
return
Expand Down
7 changes: 6 additions & 1 deletion ccapi/handlers/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func Query(c *gin.Context) {
argList = append(argList, args)
}

res, status, err := chaincode.Query(channelName, chaincodeName, txName, argList)
user := c.GetHeader("User")
if user == "" {
user = "Admin"
}

res, status, err := chaincode.Query(channelName, chaincodeName, txName, user, argList)
if err != nil {
common.Abort(c, status, err)
return
Expand Down
7 changes: 6 additions & 1 deletion ccapi/handlers/queryGateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func queryGateway(c *gin.Context, channelName, chaincodeName string) {
txName := c.Param("txname")

// Query
result, err := chaincode.QueryGateway(channelName, chaincodeName, txName, string(args))
user := c.GetHeader("User")
if user == "" {
user = "Admin"
}

result, err := chaincode.QueryGateway(channelName, chaincodeName, txName, string(args), user)
if err != nil {
err, status := common.ParseError(err)
common.Abort(c, status, err)
Expand Down
7 changes: 6 additions & 1 deletion ccapi/handlers/queryV1.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func QueryV1(c *gin.Context) {
argList = append(argList, args)
}

res, status, err := chaincode.Query(channelName, chaincodeName, txName, argList)
user := c.GetHeader("User")
if user == "" {
user = "Admin"
}

res, status, err := chaincode.Query(channelName, chaincodeName, txName, user, argList)
if err != nil {
common.Abort(c, status, err)
return
Expand Down
35 changes: 28 additions & 7 deletions chaincode/go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
module github.com/hyperledger-labs/cc-tools-demo/chaincode

go 1.13
go 1.21

// replace github.com/hyperledger-labs/cc-tools => ../../cc-tools

require (
github.com/cucumber/godog v0.12.5
github.com/hyperledger-labs/cc-tools v0.0.0-20230925111202-1b51d7cd3cc5
github.com/hyperledger-labs/cc-tools v0.0.0-20240509203838-10894c139aba
github.com/hyperledger/fabric-chaincode-go v0.0.0-20210603161043-af0e3898842a
github.com/hyperledger/fabric-protos-go v0.0.0-20210528200356-82833ecdac31
)

require (
github.com/cucumber/gherkin-go/v19 v19.0.3 // indirect
github.com/cucumber/messages-go/v16 v16.0.1 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
github.com/hashicorp/go-memdb v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hyperledger/fabric v2.1.1+incompatible // indirect
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/mitchellh/mapstructure v1.3.2 // indirect
github.com/pelletier/go-toml v1.8.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.3.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20210920023735-84f357641f63 // indirect
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
github.com/sykesm/zap-logfmt v0.0.4 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
go.uber.org/zap v1.16.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
Loading
Loading