|
1 | 1 | package stateless |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
4 | 8 | "google.golang.org/grpc" |
| 9 | + "google.golang.org/grpc/credentials" |
5 | 10 | "google.golang.org/grpc/credentials/insecure" |
6 | 11 |
|
| 12 | + "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" |
7 | 13 | cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc" |
| 14 | + "github.com/oasisprotocol/oasis-core/go/common/identity" |
8 | 15 | consensusAPI "github.com/oasisprotocol/oasis-core/go/consensus/api" |
9 | 16 | ) |
10 | 17 |
|
11 | 18 | // NewProvider creates a new consensus provider for the stateless client. |
12 | | -func NewProvider(address string) (*consensusAPI.Client, error) { |
13 | | - conn, err := cmnGrpc.Dial( |
14 | | - address, |
15 | | - grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 19 | +func NewProvider(address string, cert *tls.Certificate) (*consensusAPI.Client, error) { |
| 20 | + target, creds, err := createCredentials(address, cert) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + |
| 25 | + opts := []grpc.DialOption{ |
| 26 | + grpc.WithTransportCredentials(creds), |
16 | 27 | grpc.WithDefaultCallOptions(grpc.WaitForReady(true)), |
17 | | - ) |
| 28 | + } |
| 29 | + |
| 30 | + conn, err := cmnGrpc.Dial(target, opts...) |
18 | 31 | if err != nil { |
19 | 32 | return nil, err |
20 | 33 | } |
21 | 34 |
|
22 | 35 | return consensusAPI.NewClient(conn), nil |
23 | 36 | } |
| 37 | + |
| 38 | +func createCredentials(address string, cert *tls.Certificate) (string, credentials.TransportCredentials, error) { |
| 39 | + switch { |
| 40 | + case cmnGrpc.IsSocketAddress(address): |
| 41 | + return address, insecure.NewCredentials(), nil |
| 42 | + case !containsPublicKey(address): |
| 43 | + return address, credentials.NewTLS(&tls.Config{}), nil |
| 44 | + default: |
| 45 | + return createClientCredentials(address, cert) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func createClientCredentials(address string, cert *tls.Certificate) (string, credentials.TransportCredentials, error) { |
| 50 | + key, target, _ := strings.Cut(address, "@") |
| 51 | + |
| 52 | + var pk signature.PublicKey |
| 53 | + if err := pk.UnmarshalText([]byte(key)); err != nil { |
| 54 | + return "", nil, fmt.Errorf("malformed address: %s", address) |
| 55 | + } |
| 56 | + |
| 57 | + opts := &cmnGrpc.ClientOptions{ |
| 58 | + CommonName: identity.CommonName, |
| 59 | + ServerPubKeys: map[signature.PublicKey]bool{ |
| 60 | + pk: true, |
| 61 | + }, |
| 62 | + Certificates: []tls.Certificate{ |
| 63 | + *cert, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + creds, err := cmnGrpc.NewClientCreds(opts) |
| 68 | + if err != nil { |
| 69 | + return "", nil, err |
| 70 | + } |
| 71 | + |
| 72 | + return target, creds, nil |
| 73 | +} |
| 74 | + |
| 75 | +func containsPublicKey(address string) bool { |
| 76 | + return strings.Contains(address, "@") |
| 77 | +} |
0 commit comments