-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwallet.go
295 lines (242 loc) · 8.71 KB
/
cwallet.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package coinharness
import (
"fmt"
"github.com/jfixby/coin"
"github.com/jfixby/pin"
"github.com/jfixby/pin/commandline"
"net"
"path/filepath"
"strconv"
)
type NewConsoleWalletArgs struct {
ClientFac RPCClientFactory
ConsoleCommandCook ConsoleCommandWalletCook
WalletExecutablePathProvider commandline.ExecutablePathProvider
AppDir string
ActiveNet Network
NodeRPCHost string
NodeRPCPort int
NodeUser string
NodePass string
WalletRPCHost string
WalletRPCPort int
WalletUser string
WalletPass string
}
func NewConsoleWallet(args *NewConsoleWalletArgs) *ConsoleWallet {
pin.AssertNotNil("WalletExecutablePathProvider", args.WalletExecutablePathProvider)
pin.AssertNotNil("ActiveNet", args.ActiveNet)
pin.AssertNotNil("ClientFac", args.ClientFac)
pin.AssertNotNil("args.NodeRPCHost", args.NodeRPCHost)
pin.AssertNotEmpty("args.NodeRPCHost", args.NodeRPCHost)
pin.AssertNotNil("args.WalletRPCHost", args.WalletRPCHost)
pin.AssertNotEmpty("args.WalletRPCHost", args.WalletRPCHost)
pin.AssertTrue("args.NodeRPCPort", args.NodeRPCPort != 0)
pin.AssertTrue("args.WalletRPCPort", args.WalletRPCPort != 0)
Wallet := &ConsoleWallet{
nodeRPCListener: net.JoinHostPort(args.NodeRPCHost, strconv.Itoa(args.NodeRPCPort)),
walletRpcListener: net.JoinHostPort(args.WalletRPCHost, strconv.Itoa(args.WalletRPCPort)),
WalletRpcUser: args.WalletUser,
WalletRpcPass: args.WalletPass,
NodeRpcUser: args.NodeUser,
NodeRpcPass: args.NodePass,
appDir: args.AppDir,
endpoint: "ws",
rPCClient: &RPCConnection{MaxConnRetries: 20, RPCClientFactory: args.ClientFac},
WalletExecutablePathProvider: args.WalletExecutablePathProvider,
network: args.ActiveNet,
ConsoleCommandCook: args.ConsoleCommandCook,
}
return Wallet
}
// ConsoleWallet launches a new node instance using command-line call.
// Implements harness.Testwallet.
type ConsoleWallet struct {
// WalletExecutablePathProvider returns path to the node executable
WalletExecutablePathProvider commandline.ExecutablePathProvider
NodeRpcUser string
NodeRpcPass string
WalletRpcUser string
WalletRpcPass string
nodeRPCListener string
walletRpcListener string
appDir string
debugLevel string
endpoint string
externalProcess commandline.ExternalProcess
rPCClient *RPCConnection
network Network
ConsoleCommandCook ConsoleCommandWalletCook
}
type ConsoleCommandWalletParams struct {
ExtraArguments map[string]interface{}
NodeRpcUser string
NodeRpcPass string
WalletRpcUser string
WalletRpcPass string
RpcConnect string
RpcListen string
AppDir string
DebugLevel string
CertFile string
NodeCertFile string
KeyFile string
Network Network
}
// RPCConnectionConfig produces a new connection config instance for RPC client
func (wallet *ConsoleWallet) RPCConnectionConfig() RPCConnectionConfig {
return RPCConnectionConfig{
Host: wallet.walletRpcListener,
Endpoint: wallet.endpoint,
User: wallet.WalletRpcUser,
Pass: wallet.WalletRpcPass,
CertificateFile: wallet.CertFile(),
}
}
// FullConsoleCommand returns the full console command used to
// launch external process of the Wallet
func (wallet *ConsoleWallet) FullConsoleCommand() string {
return wallet.externalProcess.FullConsoleCommand()
}
// RPCClient returns Wallet RPCConnection
func (wallet *ConsoleWallet) RPCClient() *RPCConnection {
return wallet.rPCClient
}
// CertFile returns file path of the .cert-file of the Wallet
func (wallet *ConsoleWallet) CertFile() string {
return filepath.Join(wallet.appDir, "rpc.cert")
}
// KeyFile returns file path of the .key-file of the Wallet
func (wallet *ConsoleWallet) KeyFile() string {
return filepath.Join(wallet.appDir, "rpc.key")
}
// Network returns current network of the Wallet
func (wallet *ConsoleWallet) Network() Network {
return wallet.network
}
// IsRunning returns true if ConsoleWallet is running external node process
func (wallet *ConsoleWallet) IsRunning() bool {
return wallet.externalProcess.IsRunning()
}
// Start Wallet process. Deploys working dir, launches node using command-line,
// connects RPC client to the wallet.
func (wallet *ConsoleWallet) Start(args *TestWalletStartArgs) error {
if wallet.IsRunning() {
pin.ReportTestSetupMalfunction(fmt.Errorf("ConsoleWallet is already running"))
}
fmt.Println("Start Wallet process...")
pin.MakeDirs(wallet.appDir)
exec := wallet.WalletExecutablePathProvider.Executable()
wallet.externalProcess.CommandName = exec
consoleCommandParams := &ConsoleCommandWalletParams{
ExtraArguments: args.ExtraArguments,
NodeRpcUser: wallet.NodeRpcUser,
NodeRpcPass: wallet.NodeRpcPass,
WalletRpcUser: wallet.WalletRpcUser,
WalletRpcPass: wallet.WalletRpcPass,
RpcConnect: wallet.nodeRPCListener,
RpcListen: wallet.walletRpcListener,
AppDir: wallet.appDir,
DebugLevel: wallet.debugLevel,
CertFile: wallet.CertFile(),
NodeCertFile: args.NodeRPCCertFile,
KeyFile: wallet.KeyFile(),
Network: wallet.network,
}
wallet.externalProcess.Arguments = commandline.ArgumentsToStringArray(
wallet.ConsoleCommandCook.CookArguments(consoleCommandParams),
)
wallet.externalProcess.Launch(args.DebugOutput)
// Wallet RPC instance will create a cert file when it is ready for incoming calls
pin.WaitForFile(wallet.CertFile(), 15)
fmt.Println("Connect to Wallet RPC...")
cfg := wallet.RPCConnectionConfig()
wallet.rPCClient.Connect(cfg, nil)
fmt.Println("Wallet RPC client connected.")
return nil
}
type ConsoleCommandWalletCook interface {
CookArguments(par *ConsoleCommandWalletParams) map[string]interface{}
}
// Stop interrupts the running Wallet process.
// Disconnects RPC client from the Wallet, removes cert-files produced by the node,
// stops node process.
func (wallet *ConsoleWallet) Stop() {
if !wallet.IsRunning() {
pin.ReportTestSetupMalfunction(fmt.Errorf("Wallet is not running"))
}
if wallet.rPCClient.IsConnected() {
fmt.Println("Disconnect from Wallet RPC...")
wallet.rPCClient.Disconnect()
}
fmt.Println("Stop Wallet process...")
err := wallet.externalProcess.Stop()
pin.CheckTestSetupMalfunction(err)
// Delete files, RPC servers will recreate them on the next launch sequence
pin.DeleteFile(wallet.CertFile())
pin.DeleteFile(wallet.KeyFile())
}
// Dispose simply stops the Wallet process if running
func (wallet *ConsoleWallet) Dispose() error {
if wallet.IsRunning() {
wallet.Stop()
}
return nil
}
func (wallet *ConsoleWallet) NewAddress(accountName string) (Address, error) {
return wallet. //
RPCClient(). //
Connection(). //
GetNewAddress(accountName)
}
func (wallet *ConsoleWallet) Sync(desiredHeight int64) int64 {
attempt := 0
maxAttempt := 10
for wallet.SyncedHeight() < desiredHeight {
pin.Sleep(1000)
count := wallet.SyncedHeight()
fmt.Println(" sync to: " + strconv.FormatInt(count, 10))
attempt++
if maxAttempt <= attempt {
}
}
return wallet.SyncedHeight()
}
func (wallet *ConsoleWallet) SyncedHeight() int64 {
rpcClient := wallet.rPCClient.Connection()
//h, err := rpcClient.GetBlockCount()
_, h, err := rpcClient.GetBestBlock()
pin.CheckTestSetupMalfunction(err)
return h
}
func (wallet *ConsoleWallet) CreateNewAccount(accountName string) error {
return wallet.rPCClient.Connection().CreateNewAccount(accountName)
}
func (wallet *ConsoleWallet) GetNewAddress(accountName string) (Address, error) {
return wallet.rPCClient.Connection().GetNewAddress(accountName)
}
func (wallet *ConsoleWallet) ValidateAddress(address Address) (*ValidateAddressResult, error) {
return wallet.rPCClient.Connection().ValidateAddress(address)
}
func (wallet *ConsoleWallet) GetBalance() (*GetBalanceResult, error) {
return wallet.rPCClient.Connection().GetBalance()
}
func (wallet *ConsoleWallet) SendFrom(account string, address Address, amount coin.Amount) error {
panic("")
//return wallet.rPCClient.Connection().SendRawTransaction()
}
func (wallet *ConsoleWallet) WalletUnlock(walletPassphrase string, timeout int64) error {
return wallet.rPCClient.Connection().WalletUnlock(walletPassphrase, timeout)
}
func (wallet *ConsoleWallet) WalletLock() error {
return wallet.rPCClient.Connection().WalletLock()
}
func (wallet *ConsoleWallet) ListUnspent() ([]*Unspent, error) {
return wallet.rPCClient.Connection().ListUnspent()
}
func (wallet *ConsoleWallet) WalletInfo() (*WalletInfoResult, error) {
return wallet.rPCClient.Connection().WalletInfo()
}
func (wallet *ConsoleWallet) ListAccounts() (map[string]coin.Amount, error) {
return wallet.rPCClient.Connection().ListAccounts()
}