-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add off-chain-data go client application
Created project structure, fixed typos. Implemented connect.go and getAllAssets.go. The latter uses an assetTransferBasic struct which provides a simple API for basic asset operations like create, transfer, etc. Added transact.go with some util functions. Using google uuid package to generate random UUIDs for the transactions. Implemented pretty printing of JSON results. Implemented app.go entry point with error handling. The existing commands are getAllAssets, transact and listen. They can be called from the command line via: "go run . <command> <command> ...". They will be executed in order and if a command is not known an the application panics and aborts before executing any of the commands. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
- Loading branch information
Showing
10 changed files
with
492 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
var allCommands = map[string]func(clientConnection *grpc.ClientConn){ | ||
"getAllAssets": getAllAssets, | ||
"transact": transact, | ||
} | ||
|
||
func main() { | ||
commands := os.Args[1:] | ||
if len(commands) == 0 { | ||
printUsage() | ||
panic(errors.New("missing command")) | ||
} | ||
|
||
for _, name := range commands { | ||
if _, exists := allCommands[name]; !exists { | ||
printUsage() | ||
panic(fmt.Errorf("unknown command: %s", name)) | ||
} | ||
fmt.Printf("command: %s\n", name) | ||
} | ||
|
||
client := newGrpcConnection() | ||
defer client.Close() | ||
|
||
for _, name := range commands { | ||
command := allCommands[name] | ||
command(client) | ||
} | ||
} | ||
|
||
func printUsage() { | ||
fmt.Println("Arguments: <command1> [<command2> ...]") | ||
fmt.Printf("Available commands: %v\n", availableCommands()) | ||
} | ||
|
||
func availableCommands() string { | ||
result := make([]string, len(allCommands)) | ||
i := 0 | ||
for command := range allCommands { | ||
result[i] = command | ||
i++ | ||
} | ||
|
||
return strings.Join(result, ", ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright 2024 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
"fmt" | ||
"os" | ||
"path" | ||
"time" | ||
|
||
"github.com/hyperledger/fabric-gateway/pkg/client" | ||
"github.com/hyperledger/fabric-gateway/pkg/hash" | ||
"github.com/hyperledger/fabric-gateway/pkg/identity" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
const peerName = "peer0.org1.example.com" | ||
|
||
var ( | ||
channelName = envOrDefault("CHANNEL_NAME", "mychannel") | ||
chaincodeName = envOrDefault("CHAINCODE_NAME", "basic") | ||
mspID = envOrDefault("MSP_ID", "Org1MSP") | ||
|
||
// Path to crypto materials. | ||
cryptoPath = envOrDefault("CRYPTO_PATH", "../../test-network/organizations/peerOrganizations/org1.example.com") | ||
|
||
// Path to user private key directory. | ||
keyDirectoryPath = envOrDefault("KEY_DIRECTORY_PATH", cryptoPath+"/users/User1@org1.example.com/msp/keystore") | ||
|
||
// Path to user certificate. | ||
certPath = envOrDefault("CERT_PATH", cryptoPath+"/users/User1@org1.example.com/msp/signcerts/cert.pem") | ||
|
||
// Path to peer tls certificate. | ||
tlsCertPath = envOrDefault("TLS_CERT_PATH", cryptoPath+"/peers/peer0.org1.example.com/tls/ca.crt") | ||
|
||
// Gateway peer endpoint. | ||
peerEndpoint = envOrDefault("PEER_ENDPOINT", "dns:///localhost:7051") | ||
|
||
// Gateway peer SSL host name override. | ||
peerHostAlias = envOrDefault("PEER_HOST_ALIAS", peerName) | ||
) | ||
|
||
func envOrDefault(key, defaultValue string) string { | ||
result := os.Getenv(key) | ||
if result == "" { | ||
return defaultValue | ||
} | ||
return result | ||
} | ||
|
||
func newGrpcConnection() *grpc.ClientConn { | ||
certificatePEM, err := os.ReadFile(tlsCertPath) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read TLS certificate file: %w", err)) | ||
} | ||
|
||
certificate, err := identity.CertificateFromPEM(certificatePEM) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
certPool := x509.NewCertPool() | ||
certPool.AddCert(certificate) | ||
transportCredentials := credentials.NewClientTLSFromCert(certPool, peerHostAlias) | ||
|
||
connection, err := grpc.NewClient(peerEndpoint, grpc.WithTransportCredentials(transportCredentials)) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to create gRPC connection: %w", err)) | ||
} | ||
|
||
return connection | ||
} | ||
|
||
func newConnectOptions(clientConnection *grpc.ClientConn) (identity.Identity, []client.ConnectOption) { | ||
return newIdentity(), []client.ConnectOption{ | ||
client.WithSign(newSign()), | ||
client.WithHash(hash.SHA256), | ||
client.WithClientConnection(clientConnection), | ||
client.WithEvaluateTimeout(5 * time.Second), | ||
client.WithEndorseTimeout(15 * time.Second), | ||
client.WithSubmitTimeout(5 * time.Second), | ||
client.WithCommitStatusTimeout(1 * time.Minute), | ||
} | ||
} | ||
|
||
func newIdentity() *identity.X509Identity { | ||
certificatePEM, err := os.ReadFile(certPath) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read certificate file: %w", err)) | ||
} | ||
|
||
certificate, err := identity.CertificateFromPEM(certificatePEM) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
id, err := identity.NewX509Identity(mspID, certificate) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return id | ||
} | ||
|
||
func newSign() identity.Sign { | ||
privateKeyPEM, err := readFirstFile(keyDirectoryPath) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read private key file: %w", err)) | ||
} | ||
|
||
privateKey, err := identity.PrivateKeyFromPEM(privateKeyPEM) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sign, err := identity.NewPrivateKeySign(privateKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return sign | ||
} | ||
|
||
func readFirstFile(dirPath string) ([]byte, error) { | ||
dir, err := os.Open(dirPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fileNames, err := dir.Readdirnames(1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return os.ReadFile(path.Join(dirPath, fileNames[0])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/hyperledger/fabric-gateway/pkg/client" | ||
) | ||
|
||
type asset struct { | ||
ID string | ||
Color string | ||
Size uint64 | ||
Owner string | ||
AppraisedValue uint64 | ||
} | ||
|
||
type assetTransferBasic struct { | ||
contract *client.Contract | ||
} | ||
|
||
func newAssetTransferBasic(contract *client.Contract) *assetTransferBasic { | ||
return &assetTransferBasic{contract} | ||
} | ||
|
||
func (atb *assetTransferBasic) createAsset(anAsset asset) { | ||
if _, err := atb.contract.Submit( | ||
"CreateAsset", | ||
client.WithArguments( | ||
anAsset.ID, | ||
anAsset.Color, | ||
strconv.FormatUint(anAsset.Size, 10), | ||
anAsset.Owner, | ||
strconv.FormatUint(anAsset.AppraisedValue, 10), | ||
)); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (atb *assetTransferBasic) transferAsset(id, newOwner string) string { | ||
result, err := atb.contract.Submit( | ||
"TransferAsset", | ||
client.WithArguments( | ||
id, | ||
newOwner, | ||
), | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return string(result) | ||
} | ||
|
||
func (atb *assetTransferBasic) deleteAsset(id string) { | ||
if _, err := atb.contract.Submit( | ||
"DeleteAsset", | ||
client.WithArguments( | ||
id, | ||
), | ||
); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (atb *assetTransferBasic) getAllAssets() []byte { | ||
result, err := atb.contract.Evaluate("GetAllAssets") | ||
if err != nil { | ||
panic(err) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2024 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/hyperledger/fabric-gateway/pkg/client" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func getAllAssets(clientConnection *grpc.ClientConn) { | ||
id, options := newConnectOptions(clientConnection) | ||
gateway, err := client.Connect(id, options...) | ||
if err != nil { | ||
panic((err)) | ||
} | ||
defer gateway.Close() | ||
|
||
contract := gateway.GetNetwork(channelName).GetContract(chaincodeName) | ||
smartContract := newAssetTransferBasic(contract) | ||
assets := smartContract.getAllAssets() | ||
|
||
fmt.Printf("%s\n", formatJSON(assets)) | ||
} | ||
|
||
func formatJSON(data []byte) string { | ||
var result bytes.Buffer | ||
if err := json.Indent(&result, data, "", " "); err != nil { | ||
panic(fmt.Errorf("failed to parse JSON: %w", err)) | ||
} | ||
return result.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module offChainData | ||
|
||
go 1.22.0 | ||
|
||
require ( | ||
github.com/hyperledger/fabric-gateway v1.7.0 | ||
google.golang.org/grpc v1.67.1 | ||
) | ||
|
||
require ( | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.4 // indirect | ||
github.com/miekg/pkcs11 v1.1.1 // indirect | ||
golang.org/x/crypto v0.28.0 // indirect | ||
golang.org/x/net v0.28.0 // indirect | ||
golang.org/x/sys v0.26.0 // indirect | ||
golang.org/x/text v0.19.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect | ||
google.golang.org/protobuf v1.35.1 // indirect | ||
) |
Oops, something went wrong.