|
| 1 | +/* |
| 2 | + * Copyright 2024 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/x509" |
| 11 | + "fmt" |
| 12 | + "os" |
| 13 | + "path" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/hyperledger/fabric-gateway/pkg/client" |
| 17 | + "github.com/hyperledger/fabric-gateway/pkg/hash" |
| 18 | + "github.com/hyperledger/fabric-gateway/pkg/identity" |
| 19 | + "google.golang.org/grpc" |
| 20 | + "google.golang.org/grpc/credentials" |
| 21 | +) |
| 22 | + |
| 23 | +const peerName = "peer0.org1.example.com" |
| 24 | + |
| 25 | +var ( |
| 26 | + channelName = envOrDefault("CHANNEL_NAME", "mychannel") |
| 27 | + chaincodeName = envOrDefault("CHAINCODE_NAME", "basic") |
| 28 | + mspID = envOrDefault("MSP_ID", "Org1MSP") |
| 29 | + |
| 30 | + // Path to crypto materials. |
| 31 | + cryptoPath = envOrDefault("CRYPTO_PATH", "../../test-network/organizations/peerOrganizations/org1.example.com") |
| 32 | + |
| 33 | + // Path to user private key directory. |
| 34 | + keyDirectoryPath = envOrDefault("KEY_DIRECTORY_PATH", cryptoPath+"/users/User1@org1.example.com/msp/keystore") |
| 35 | + |
| 36 | + // Path to user certificate. |
| 37 | + certPath = envOrDefault("CERT_PATH", cryptoPath+"/users/User1@org1.example.com/msp/signcerts/cert.pem") |
| 38 | + |
| 39 | + // Path to peer tls certificate. |
| 40 | + tlsCertPath = envOrDefault("TLS_CERT_PATH", cryptoPath+"/peers/peer0.org1.example.com/tls/ca.crt") |
| 41 | + |
| 42 | + // Gateway peer endpoint. |
| 43 | + peerEndpoint = envOrDefault("PEER_ENDPOINT", "dns:///localhost:7051") |
| 44 | + |
| 45 | + // Gateway peer SSL host name override. |
| 46 | + peerHostAlias = envOrDefault("PEER_HOST_ALIAS", peerName) |
| 47 | +) |
| 48 | + |
| 49 | +func envOrDefault(key, defaultValue string) string { |
| 50 | + result := os.Getenv(key) |
| 51 | + if result == "" { |
| 52 | + return defaultValue |
| 53 | + } |
| 54 | + return result |
| 55 | +} |
| 56 | + |
| 57 | +func newGrpcConnection() *grpc.ClientConn { |
| 58 | + certificatePEM, err := os.ReadFile(tlsCertPath) |
| 59 | + if err != nil { |
| 60 | + panic(fmt.Errorf("failed to read TLS certificate file: %w", err)) |
| 61 | + } |
| 62 | + |
| 63 | + certificate, err := identity.CertificateFromPEM(certificatePEM) |
| 64 | + if err != nil { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + |
| 68 | + certPool := x509.NewCertPool() |
| 69 | + certPool.AddCert(certificate) |
| 70 | + transportCredentials := credentials.NewClientTLSFromCert(certPool, peerHostAlias) |
| 71 | + |
| 72 | + connection, err := grpc.NewClient(peerEndpoint, grpc.WithTransportCredentials(transportCredentials)) |
| 73 | + if err != nil { |
| 74 | + panic(fmt.Errorf("failed to create gRPC connection: %w", err)) |
| 75 | + } |
| 76 | + |
| 77 | + return connection |
| 78 | +} |
| 79 | + |
| 80 | +func newConnectOptions(clientConnection *grpc.ClientConn) (identity.Identity, []client.ConnectOption) { |
| 81 | + return newIdentity(), []client.ConnectOption{ |
| 82 | + client.WithSign(newSign()), |
| 83 | + client.WithHash(hash.SHA256), |
| 84 | + client.WithClientConnection(clientConnection), |
| 85 | + client.WithEvaluateTimeout(5 * time.Second), |
| 86 | + client.WithEndorseTimeout(15 * time.Second), |
| 87 | + client.WithSubmitTimeout(5 * time.Second), |
| 88 | + client.WithCommitStatusTimeout(1 * time.Minute), |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func newIdentity() *identity.X509Identity { |
| 93 | + certificatePEM, err := os.ReadFile(certPath) |
| 94 | + if err != nil { |
| 95 | + panic(fmt.Errorf("failed to read certificate file: %w", err)) |
| 96 | + } |
| 97 | + |
| 98 | + certificate, err := identity.CertificateFromPEM(certificatePEM) |
| 99 | + if err != nil { |
| 100 | + panic(err) |
| 101 | + } |
| 102 | + |
| 103 | + id, err := identity.NewX509Identity(mspID, certificate) |
| 104 | + if err != nil { |
| 105 | + panic(err) |
| 106 | + } |
| 107 | + |
| 108 | + return id |
| 109 | +} |
| 110 | + |
| 111 | +func newSign() identity.Sign { |
| 112 | + privateKeyPEM, err := readFirstFile(keyDirectoryPath) |
| 113 | + if err != nil { |
| 114 | + panic(fmt.Errorf("failed to read private key file: %w", err)) |
| 115 | + } |
| 116 | + |
| 117 | + privateKey, err := identity.PrivateKeyFromPEM(privateKeyPEM) |
| 118 | + if err != nil { |
| 119 | + panic(err) |
| 120 | + } |
| 121 | + |
| 122 | + sign, err := identity.NewPrivateKeySign(privateKey) |
| 123 | + if err != nil { |
| 124 | + panic(err) |
| 125 | + } |
| 126 | + |
| 127 | + return sign |
| 128 | +} |
| 129 | + |
| 130 | +func readFirstFile(dirPath string) ([]byte, error) { |
| 131 | + dir, err := os.Open(dirPath) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + |
| 136 | + fileNames, err := dir.Readdirnames(1) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + return os.ReadFile(path.Join(dirPath, fileNames[0])) |
| 142 | +} |
0 commit comments