/
common.go
97 lines (82 loc) · 2.74 KB
/
common.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"os"
"strings"
"github.com/jwrookie/jwsolana-go/rpc"
"github.com/jwrookie/jwsolana-go/vault"
)
func getClient() *rpc.Client {
httpHeaders := viper.GetStringSlice("global-http-header")
for i := 0; i < 25; i++ {
if val := os.Getenv(fmt.Sprintf("SLNC_GLOBAL_HTTP_HEADER_%d", i)); val != "" {
httpHeaders = append(httpHeaders, val)
}
}
headers := make(map[string]string)
for _, header := range httpHeaders {
headerArray := strings.SplitN(header, ": ", 2)
if len(headerArray) != 2 || strings.Contains(headerArray[0], " ") {
errorCheck("validating http headers", fmt.Errorf("invalid HTTP Header format"))
}
headers[headerArray[0]] = headerArray[1]
}
api := rpc.NewWithHeaders(sanitizeAPIURL(viper.GetString("global-rpc-url")), headers)
return api
}
func sanitizeAPIURL(input string) string {
switch input {
case "devnet":
return "https://api.devnet.solana.com"
case "testnet":
return "https://api.testnet.solana.com"
case "mainnet":
return "https://api.mainnet-beta.solana.com"
}
return strings.TrimRight(input, "/")
}
func errorCheck(prefix string, err error) {
if err != nil {
fmt.Printf("ERROR: %s: %s\n", prefix, err)
if strings.HasSuffix(err.Error(), "connection refused") && strings.Contains(err.Error(), defaultRPCURL) {
fmt.Println("Have you selected a valid Solana JSON-RPC endpoint ? You can use the --rpc-url flag or SLNC_GLOBAL_RPC_URL environment variable.")
}
os.Exit(1)
}
}
func mustGetWallet() *vault.Vault {
vault, err := setupWallet()
errorCheck("wallet setup", err)
return vault
}
func setupWallet() (*vault.Vault, error) {
walletFile := viper.GetString("global-vault-file")
if _, err := os.Stat(walletFile); err != nil {
return nil, fmt.Errorf("wallet file %q missing: %w", walletFile, err)
}
v, err := vault.NewVaultFromWalletFile(walletFile)
if err != nil {
return nil, fmt.Errorf("loading vault: %w", err)
}
boxer, err := vault.SecretBoxerForType(v.SecretBoxWrap, viper.GetString("global-kms-gcp-keypath"))
if err != nil {
return nil, fmt.Errorf("secret boxer: %w", err)
}
if err := v.Open(boxer); err != nil {
return nil, fmt.Errorf("opening: %w", err)
}
return v, nil
}