github.com/koko1123/flow-go-1@v0.29.6/utils/grpcutils/grpc_client.go (about) 1 package grpcutils 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 7 "google.golang.org/grpc" 8 "google.golang.org/grpc/credentials" 9 10 "github.com/onflow/flow-go-sdk/crypto" 11 ) 12 13 // SecureGRPCDialOpt creates a secure GRPC dial option with TLS config 14 func SecureGRPCDialOpt(publicKeyHex string) (grpc.DialOption, error) { 15 bytes, err := hex.DecodeString(publicKeyHex) 16 if err != nil { 17 return nil, fmt.Errorf("failed to decode secured GRPC server public key hex %w", err) 18 } 19 20 publicFlowNetworkingKey, err := crypto.DecodePublicKey(crypto.ECDSA_P256, bytes) 21 if err != nil { 22 return nil, fmt.Errorf("failed to get public flow networking key could not decode public key bytes %w", err) 23 } 24 25 tlsConfig, err := DefaultClientTLSConfig(publicFlowNetworkingKey) 26 if err != nil { 27 return nil, fmt.Errorf("failed to get default TLS client config using public flow networking key %s %w", publicFlowNetworkingKey.String(), err) 28 } 29 30 return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), nil 31 }