mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 23:45:10 +00:00
For some signing implementations, such as ed25519, a non-default hash implementation must be specified when creating the Gateway connection in client applications. Rather than relying on the default hash algorithm, it is probably good practice in general to specify an algorithm that is compatible with your signing implementation. This change explicitly specifies the hash algorithm to raise visibility of the option to select the hash algorithm. Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
108 lines
2.8 KiB
Go
108 lines
2.8 KiB
Go
package web
|
|
|
|
import (
|
|
"crypto/x509"
|
|
"fmt"
|
|
"log"
|
|
"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"
|
|
)
|
|
|
|
// Initialize the setup for the organization.
|
|
func Initialize(setup OrgSetup) (*OrgSetup, error) {
|
|
log.Printf("Initializing connection for %s...\n", setup.OrgName)
|
|
clientConnection := setup.newGrpcConnection()
|
|
id := setup.newIdentity()
|
|
sign := setup.newSign()
|
|
|
|
gateway, err := client.Connect(
|
|
id,
|
|
client.WithSign(sign),
|
|
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),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
setup.Gateway = *gateway
|
|
log.Println("Initialization complete")
|
|
return &setup, nil
|
|
}
|
|
|
|
// newGrpcConnection creates a gRPC connection to the Gateway server.
|
|
func (setup OrgSetup) newGrpcConnection() *grpc.ClientConn {
|
|
certificate, err := loadCertificate(setup.TLSCertPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
certPool := x509.NewCertPool()
|
|
certPool.AddCert(certificate)
|
|
transportCredentials := credentials.NewClientTLSFromCert(certPool, setup.GatewayPeer)
|
|
|
|
connection, err := grpc.NewClient(setup.PeerEndpoint, grpc.WithTransportCredentials(transportCredentials))
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to create gRPC connection: %w", err))
|
|
}
|
|
|
|
return connection
|
|
}
|
|
|
|
// newIdentity creates a client identity for this Gateway connection using an X.509 certificate.
|
|
func (setup OrgSetup) newIdentity() *identity.X509Identity {
|
|
certificate, err := loadCertificate(setup.CertPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
id, err := identity.NewX509Identity(setup.MSPID, certificate)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return id
|
|
}
|
|
|
|
// newSign creates a function that generates a digital signature from a message digest using a private key.
|
|
func (setup OrgSetup) newSign() identity.Sign {
|
|
files, err := os.ReadDir(setup.KeyPath)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to read private key directory: %w", err))
|
|
}
|
|
privateKeyPEM, err := os.ReadFile(path.Join(setup.KeyPath, files[0].Name()))
|
|
|
|
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 loadCertificate(filename string) (*x509.Certificate, error) {
|
|
certificatePEM, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read certificate file: %w", err)
|
|
}
|
|
return identity.CertificateFromPEM(certificatePEM)
|
|
}
|