-
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 and added connect.go. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
- Loading branch information
Showing
6 changed files
with
182 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,7 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("Hello World!") | ||
} |
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,127 @@ | ||
/* | ||
Copyright 2024 IBM All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"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 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,17 @@ | ||
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/miekg/pkcs11 v1.1.1 // 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 | ||
) |
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,26 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/hyperledger/fabric-gateway v1.7.0 h1:bd1quU8qYPYqYO69m1tPIDSjB+D+u/rBJfE1eWFcpjY= | ||
github.com/hyperledger/fabric-gateway v1.7.0/go.mod h1:TItDGnq71eJcgz5TW+m5Sq3kWGp0AEI1HPCNxj0Eu7k= | ||
github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU= | ||
github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= | ||
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= | ||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= | ||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= | ||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs= | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= | ||
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= | ||
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= | ||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= | ||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |