-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfig.go
118 lines (101 loc) · 2.13 KB
/
config.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"io/ioutil"
"os"
"os/user"
"path"
"github.com/BurntSushi/toml"
"github.com/cosmos/cosmos-sdk/types/bech32"
)
var (
defaultBucketRegion = "ca-central-1"
defaultGas = 300000
)
// A chain we sign txs on
type Chain struct {
Name string // chain name
Binary string // binary to use for signing
Prefix string // bech32 address prefix
ID string // chain id for signing
Node string // node to broadcast signed txs to
Denom string // native denom
}
// A key we sign txs with
type Key struct {
Name string
Address string
LocalName string
}
// Credentials for AWS
type AWS struct {
Address string
Bucket string
BucketRegion string
Pub string
Priv string
}
// Config file
type Config struct {
User string
KeyringBackend string
DefaultGas int64
AWS AWS
Keys []Key
Chains []Chain
}
func (c *Config) GetChain(name string) (Chain, bool) {
for _, chain := range c.Chains {
if chain.Name == name {
return chain, true
}
}
return Chain{}, false
}
func (c *Config) GetKey(name string) (Key, bool) {
for _, key := range c.Keys {
if key.Name == name {
return key, true
}
}
return Key{}, false
}
// load toml config
func loadConfig(filename string) (*Config, error) {
if filename == "" {
if _, err := os.Stat(defaultLocalConfigFile); err == nil {
filename = defaultLocalConfigFile
} else {
usr, err := user.Current()
if err != nil {
return nil, err
}
filename = path.Join(usr.HomeDir, defaultGlobalConfigFile)
}
}
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c := &Config{}
err = toml.Unmarshal(b, c)
if err != nil {
return nil, err
}
if c.AWS.BucketRegion == "" {
c.AWS.BucketRegion = defaultBucketRegion
}
return c, nil
}
// convert the prefix on a bech32 address
func bech32ify(addrBech, prefix string) (string, error) {
hrp, addrBytes, err := bech32.DecodeAndConvert(addrBech)
if err != nil {
return "", err
}
_ = hrp
newAddrBech, err := bech32.ConvertAndEncode(prefix, addrBytes)
if err != nil {
return "", err
}
return newAddrBech, nil
}