-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
143 lines (130 loc) · 3.34 KB
/
Copy pathconfig.go
File metadata and controls
143 lines (130 loc) · 3.34 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package go_ethutils
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/howeyc/gopass"
"io/ioutil"
"os"
"strconv"
"strings"
)
type ConfigInterface interface {
IsSet(key string) bool
GetInt(key string) int
GetString(key string) string
GetStringMapString(key string) map[string]string
}
type Config struct {
config ConfigInterface
}
func NewConfig(config ConfigInterface) *Config {
return &Config{
config: config,
}
}
func (c *Config) MustGetChainInfo() *Chain {
chainName := c.config.GetString("chain")
if chainName == "" {
fmt.Printf("chain not set")
os.Exit(1)
}
return c.GetChainInfo(c.config.GetString("chain"))
}
func (c *Config) GetChainInfo(chainName string) *Chain {
return ParserChainInfo(c.config.GetStringMapString("chains." + chainName))
}
func (c *Config) MustGetAccount() *Account {
accountName := c.config.GetString("account")
if accountName == "" {
fmt.Printf("account not set")
os.Exit(1)
}
account := c.GetAccount(accountName)
if account == nil {
fmt.Printf("account %s not set", accountName)
os.Exit(1)
}
return account
}
func getPassLoop(keystoreBytes []byte) string {
fmt.Println("please enter your keystore file password:")
for {
password, _ := gopass.GetPasswdMasked()
_, err := keystore.DecryptKey(keystoreBytes, string(password))
if err == nil {
fmt.Println("decrypt success")
return string(password)
}
fmt.Println(err.Error())
fmt.Println("please reenter it:")
}
}
func (c *Config) GetAccount(accountName string) *Account {
pStr := c.config.GetString("accounts." + accountName + ".key")
if pStr == "" {
return nil
}
var _account *Account
if IsMnemonic(pStr) {
_account = GetAccountFromMnemonic(pStr, c.config.GetInt("account_index"))
} else if strings.Contains(pStr, "/") {
keystoreBytes, err := ioutil.ReadFile(pStr)
if err != nil {
fmt.Printf("can not read keystore file content")
return nil
}
// keystore file
password := getPassLoop(keystoreBytes)
_account = GetAccountFromKS(pStr, password)
} else {
_account = GetAccountFromPStr(pStr)
}
if _account == nil {
return nil
}
_account.Client = c.MustGetChainInfo().Client
return _account
}
func (c *Config) MustGetToken(token string) *Token {
IsSet := c.config.IsSet("tokens." + token)
if !IsSet {
fmt.Printf("token %s not set", token)
os.Exit(1)
}
return c.GetToken(token)
}
func (c *Config) GetToken(token string) *Token {
tokenInfo := c.config.GetStringMapString("tokens." + strings.ToLower(token))
_decimal, _ := strconv.Atoi(tokenInfo["decimal"])
address := HexToAddress(tokenInfo["address"])
return &Token{
Address: &address,
Decimal: _decimal,
Symbol: token,
Name: token,
}
}
func (c *Config) MustGetHDAccountByIndex(_index int) *Account {
accountName := c.config.GetString("account")
if accountName == "" {
fmt.Printf("account not set")
os.Exit(1)
}
pStr := c.config.GetString("accounts." + accountName + ".key")
_account := GetAccountFromMnemonic(pStr, _index)
if _account == nil {
fmt.Printf("account %s parse error", accountName)
os.Exit(1)
}
return _account
}
func (c *Config) MustGetContractAddress(name string) *common.Address {
cStr := c.config.GetString("contracts." + name)
if cStr == "" {
fmt.Printf("contract %s not found", name)
os.Exit(1)
}
contract := HexToAddress(cStr)
return &contract
}