@@ -21,6 +21,7 @@ package util
2121import (
2222 "bytes"
2323 "context"
24+ "crypto/tls"
2425 "encoding/hex"
2526 "fmt"
2627 "net"
@@ -38,6 +39,7 @@ import (
3839 flowGo "github.com/onflow/flow-go/model/flow"
3940 flowaccess "github.com/onflow/flow/protobuf/go/flow/access"
4041 grpcOpts "google.golang.org/grpc"
42+ "google.golang.org/grpc/credentials"
4143 "google.golang.org/grpc/credentials/insecure"
4244
4345 emulatorUtils "github.com/onflow/flow-emulator/utils"
@@ -74,7 +76,7 @@ func IsAddressValidForNetwork(address flow.Address, networkName string) bool {
7476// by querying the access node to get the actual chain ID
7577func ValidateAddressForNetwork (address flow.Address , network * config.Network ) error {
7678 // Create a grpc client to query the network
77- client , err := grpc .NewBaseClient (network .Host , grpcOpts . WithTransportCredentials ( insecure . NewCredentials () ))
79+ client , err := grpc .NewBaseClient (network .Host , TransportCredentialForHost ( network . Host ))
7880 if err != nil {
7981 return fmt .Errorf ("failed to connect to access node: %w" , err )
8082 }
@@ -244,6 +246,22 @@ func AddFlowEntriesToCursorIgnore(targetDir string, loader flowkit.ReaderWriter)
244246 return addEntriesToIgnoreFile (cursorIgnorePath , flowEntries , loader )
245247}
246248
249+ // TransportCredentialForHost returns TLS credentials using system CA certificates
250+ // if the host uses port 443, or insecure credentials otherwise.
251+ func TransportCredentialForHost (host string ) grpcOpts.DialOption {
252+ _ , port , err := net .SplitHostPort (host )
253+ if err == nil && port == "443" {
254+ return grpcOpts .WithTransportCredentials (credentials .NewTLS (& tls.Config {MinVersion : tls .VersionTLS12 }))
255+ }
256+ return grpcOpts .WithTransportCredentials (insecure .NewCredentials ())
257+ }
258+
259+ // GRPCDialOptionForHost returns a grpcAccess.ClientOption that configures
260+ // TLS using system CA certificates for port 443 hosts, or insecure credentials otherwise.
261+ func GRPCDialOptionForHost (host string ) grpc.ClientOption {
262+ return grpc .WithGRPCDialOptions (TransportCredentialForHost (host ))
263+ }
264+
247265// GetAddressNetwork returns the chain ID for an address.
248266func GetAddressNetwork (address flow.Address ) (flow.ChainID , error ) {
249267 networks := []flow.ChainID {
@@ -282,7 +300,7 @@ func GetChainIDFromHost(host string) (flowGo.ChainID, error) {
282300
283301 conn , err := grpcOpts .NewClient (
284302 host ,
285- grpcOpts . WithTransportCredentials ( insecure . NewCredentials () ),
303+ TransportCredentialForHost ( host ),
286304 emulatorUtils .DefaultGRPCRetryInterceptor (),
287305 )
288306 if err != nil {
0 commit comments