forked from blocktree/tron-adapter
/
config.go
272 lines (243 loc) · 7.05 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright 2018 The openwallet Authors
* This file is part of the openwallet library.
*
* The openwallet library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The openwallet library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package tron
import (
"fmt"
"path/filepath"
"strings"
"time"
owcrypt "github.com/blocktree/go-owcrypt"
"github.com/blocktree/openwallet/v2/common/file"
"github.com/shopspring/decimal"
)
/*
工具可以读取各个币种钱包的默认的配置资料,
币种钱包的配置资料放在conf/{symbol}.conf,例如:ADA.conf, BTC.conf,ETH.conf。
执行wmd wallet -s <symbol> 命令会先检查是否存在该币种钱包的配置文件。
没有:执行ConfigFlow,配置文件初始化。
有:执行常规命令。
使用者还可以通过wmd config -s 进行修改配置文件。
或执行wmd config flow 重新进行一次配置初始化流程。
*/
const (
// Symbol 币种
Symbol = "TRX"
//MasterKey key for master
MasterKey = "Troncoin seed"
//CurveType to generate ChildKey by BIP32
CurveType = owcrypt.ECC_CURVE_SECP256K1
Decimals = 6
SUN int64 = 1 //最小单位
TRX int64 = SUN * 1000000 //1 TRX = 1000000 * sun
GasPrice = SUN * 140
CreateAccountCost = SUN * 1100000 //1.1 TRX = 1100000 * sun
)
//WalletConfig configs for Wallet
type WalletConfig struct {
//币种
Symbol string
MasterKey string
//RPCUser RPC认证账户名
RPCUser string
//RPCPassword RPC认证账户密码
RPCPassword string
AccessToken string
//证书目录
CertsDir string
//钥匙备份路径
keyDir string
//地址导出路径
addressDir string
//配置文件路径
configFilePath string
//配置文件名
configFileName string
//rpc证书
CertFileName string
//区块链数据文件
BlockchainFile string
//是否测试网络
IsTestNet bool
// 核心钱包是否只做监听
CoreWalletWatchOnly bool
//最大的输入数量
MaxTxInputs int
//本地数据库文件路径
dbPath string
//备份路径
backupDir string
//钱包服务API
ServerAPI string
//钱包安装的路径
NodeInstallPath string
//钱包数据文件目录
WalletDataPath string
//汇总阀值
Threshold decimal.Decimal
//汇总地址
SumAddress string
//汇总执行间隔时间
CycleSeconds time.Duration
//默认配置内容
DefaultConfig string
//曲线类型
CurveType uint32
//小数位长度
CoinDecimal decimal.Decimal
//后台数据源类型
RPCServerType int
//FeeLimit 智能合约最大能量消耗限制,1 Energy = 140 SUN
FeeLimit int64
//FeeMini 智能合约最小能量消耗起,1 Energy = 140 SUN
FeeMini int64
//数据目录
DataDir string
//Ignore the dust trade
IgnoreDustTRX decimal.Decimal
}
//NewConfig Create config instance
func NewConfig() *WalletConfig {
c := WalletConfig{}
//币种
c.Symbol = Symbol
c.MasterKey = MasterKey
c.CurveType = CurveType
//RPC认证账户名
c.RPCUser = ""
//RPC认证账户密码
c.RPCPassword = ""
//证书目录
c.CertsDir = filepath.Join("data", strings.ToLower(c.Symbol), "certs")
//钥匙备份路径
c.keyDir = filepath.Join("data", strings.ToLower(c.Symbol), "key")
//地址导出路径
c.addressDir = filepath.Join("data", strings.ToLower(c.Symbol), "address")
//区块链数据
//blockchainDir = filepath.Join("data", strings.ToLower(Symbol), "blockchain")
//配置文件路径
c.configFilePath = filepath.Join("conf")
//配置文件名
c.configFileName = c.Symbol + ".ini"
//rpc证书
c.CertFileName = "rpc.cert"
//区块链数据文件
c.BlockchainFile = "blockchain.db"
//是否测试网络
c.IsTestNet = true
// 核心钱包是否只做监听
c.CoreWalletWatchOnly = true
//最大的输入数量
c.MaxTxInputs = 50
//本地数据库文件路径
c.dbPath = filepath.Join("data", strings.ToLower(c.Symbol), "db")
//备份路径
c.backupDir = filepath.Join("data", strings.ToLower(c.Symbol), "backup")
//钱包服务API
c.ServerAPI = ""
//钱包安装的路径
c.NodeInstallPath = ""
//钱包数据文件目录
c.WalletDataPath = ""
//汇总阀值
c.Threshold = decimal.NewFromFloat(5)
//汇总地址
c.SumAddress = ""
//汇总执行间隔时间
c.CycleSeconds = time.Second * 10
//小数位长度
c.CoinDecimal = decimal.NewFromFloat(100000000)
//默认配置内容
c.DefaultConfig = `
# start node command
startNodeCMD = ""
# stop node command
stopNodeCMD = ""
# node install path
nodeInstallPath = ""
# mainnet data path
mainNetDataPath = ""
# testnet data path
testNetDataPath = ""
# RPC api url
serverAPI = ""
# RPC Authentication Username
rpcUser = ""
# RPC Authentication Password
rpcPassword = ""
# Is network test?
isTestNet = false
# the safe address that wallet send money to.
sumAddress = ""
# when wallet's balance is over this value, the wallet willl send money to [sumAddress]
threshold = ""
# summary task timer cycle time, sample: 1m , 30s, 3m20s etc
cycleSeconds = ""
# feeLimit, the maximum energy is 1000000000
feeLimit = 10000000
`
//创建目录
//file.MkdirAll(c.dbPath)
//file.MkdirAll(c.backupDir)
//file.MkdirAll(c.keyDir)
return &c
}
//PrintConfig Print config information
func (wc *WalletConfig) PrintConfig() error {
wc.InitConfig()
//读取配置
absFile := filepath.Join(wc.configFilePath, wc.configFileName)
//apiURL := c.String("apiURL")
//walletPath := c.String("walletPath")
//threshold := c.String("threshold")
//minSendAmount := c.String("minSendAmount")
//minFees := c.String("minFees")
//sumAddress := c.String("sumAddress")
//isTestNet, _ := c.Bool("isTestNet")
fmt.Printf("-----------------------------------------------------------\n")
//fmt.Printf("Wallet API URL: %s\n", apiURL)
//fmt.Printf("Wallet Data FilePath: %s\n", walletPath)
//fmt.Printf("Summary Address: %s\n", sumAddress)
//fmt.Printf("Summary Threshold: %s\n", threshold)
//fmt.Printf("Min Send Amount: %s\n", minSendAmount)
//fmt.Printf("Transfer Fees: %s\n", minFees)
//if isTestNet {
// fmt.Printf("Network: TestNet\n")
//} else {
// fmt.Printf("Network: MainNet\n")
//}
file.PrintFile(absFile)
fmt.Printf("-----------------------------------------------------------\n")
return nil
}
//InitConfig 初始化配置文件
func (wc *WalletConfig) InitConfig() {
//读取配置
absFile := filepath.Join(wc.configFilePath, wc.configFileName)
if !file.Exists(absFile) {
file.MkdirAll(wc.configFilePath)
file.WriteFile(absFile, []byte(wc.DefaultConfig), false)
}
}
//创建文件夹
func (wc *WalletConfig) makeDataDir() {
if len(wc.DataDir) == 0 {
//默认路径当前文件夹./data
wc.DataDir = "data"
}
//本地数据库文件路径
wc.dbPath = filepath.Join(wc.DataDir, strings.ToLower(wc.Symbol), "db")
//创建目录
file.MkdirAll(wc.dbPath)
}