-
Notifications
You must be signed in to change notification settings - Fork 3
/
base_grpc.go
54 lines (43 loc) · 1.31 KB
/
base_grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package internal
import (
"context"
"crypto/tls"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
type BaseGrpc struct {
}
func NewBaseGrpc() *BaseGrpc {
return &BaseGrpc{}
}
func (p *BaseGrpc) Connection(
ctx context.Context,
nodeUrl string,
) (*grpc.ClientConn, error) {
authCredentials := grpc.WithTransportCredentials(insecure.NewCredentials())
callOptions := grpc.WithDefaultCallOptions()
if strings.Contains(nodeUrl, ":443") {
authCredentials = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
}
if strings.Contains(nodeUrl, "migaloo") {
callOptions = grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))
}
return grpc.DialContext(
ctx,
nodeUrl,
authCredentials,
callOptions,
)
}
func (p *BaseGrpc) GetCallOption() grpc.CallOption {
interfaceRegistry := codecTypes.NewInterfaceRegistry()
govtypes.RegisterInterfaces(interfaceRegistry)
govtypesv1.RegisterInterfaces(interfaceRegistry)
return grpc.ForceCodec(codec.NewProtoCodec(interfaceRegistry).GRPCCodec())
}