/
balances.go
392 lines (343 loc) · 9.82 KB
/
balances.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
fct "github.com/FactomProject/factoid"
"github.com/FactomProject/factoid/wallet"
"github.com/FactomProject/fctwallet2/Wallet/Utility"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
)
/***********************************************
* General Support Functions
***********************************************/
var badChar, _ = regexp.Compile("[^A-Za-z0-9_-]")
var badHexChar, _ = regexp.Compile("[^A-Fa-f0-9]")
func ValidName(name string) error {
if len(name) > 32 {
return fmt.Errorf("Name of address is too long.")
}
if badChar.FindStringIndex(name) != nil {
return fmt.Errorf("Invalid name. Names must be alphanumeric, underscores, or hyphens.")
}
return nil
}
func GenAddress(state IState, adrType string, key string) error {
validErr := ValidName(key)
if validErr != nil {
return validErr
}
switch strings.ToLower(adrType) {
case "ec":
adr, err := state.GetFS().GetWallet().GenerateECAddress([]byte(key))
if err != nil {
return err
}
fmt.Println(key, "=", fct.ConvertECAddressToUserStr(adr))
case "fct":
adr, err := state.GetFS().GetWallet().GenerateFctAddress([]byte(key), 1, 1)
if err != nil {
return err
}
fmt.Println(key, "=", fct.ConvertFctAddressToUserStr(adr))
default:
return fmt.Errorf("Invalid Parameters")
}
return nil
}
// Get the Factoshis per Entry Credit Rate
func GetRate(state IState) (int64, error) {
str := fmt.Sprintf("http://%s/v1/factoid-get-fee/", state.GetServer())
resp, err := http.Get(str)
if err != nil {
return 0, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
resp.Body.Close()
type x struct {
Response struct {
Fee int64
}
Success bool
}
b := new(x)
if err = json.Unmarshal(body, b); err != nil {
return 0, err
}
return b.Response.Fee, err
}
func LookupAddress(state IState, adrType string, adr string) (string, error) {
if Utility.IsValidAddress(adr) && strings.HasPrefix(adr, adrType) {
baddr := fct.ConvertUserStrToAddress(adr)
adr = hex.EncodeToString(baddr)
} else if Utility.IsValidHexAddress(adr) {
// the address is good enough.
} else if Utility.IsValidNickname(adr) {
we := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(adr))
if we != nil {
we2 := we.(wallet.IWalletEntry)
addr, _ := we2.GetAddress()
adr = hex.EncodeToString(addr.Bytes())
} else {
return "", fmt.Errorf("Name %s is undefined.", adr)
}
} else {
return "", fmt.Errorf("Invalid Name. Check that you have entered the name correctly.")
}
return adr, nil
}
func FctBalance(state IState, adr string) (int64, error) {
adr, err := LookupAddress(state, "FA", adr)
if err != nil {
return 0, err
}
str := fmt.Sprintf("http://%s/v1/factoid-balance/%s", state.GetServer(), adr)
resp, err := http.Get(str)
if err != nil {
return 0, fmt.Errorf("Communication Error with Factom Client")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("Read Error with Factom Client")
}
resp.Body.Close()
type Balance struct {
Response string
Success bool
}
b := new(Balance)
if err := json.Unmarshal(body, b); err != nil {
return 0, fmt.Errorf("Parsing Error on data returned by Factom Client")
}
if !b.Success {
return 0, fmt.Errorf(err.Error())
}
v, err := strconv.ParseInt(b.Response, 10, 64)
if err != nil {
return 0, fmt.Errorf("Invalid balance returned by factomd")
}
return v, nil
}
func ECBalance(state IState, adr string) (int64, error) {
adr, err := LookupAddress(state, "EC", adr)
if err != nil {
return 0, err
}
str := fmt.Sprintf("http://%s/v1/entry-credit-balance/%s", state.GetServer(), adr)
resp, err := http.Get(str)
if err != nil {
return 0, fmt.Errorf("Communication Error with Factom Client")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("Read Error with Factom Client")
}
resp.Body.Close()
type Balance struct {
Response string
Success bool
}
b := new(Balance)
if err := json.Unmarshal(body, b); err != nil {
return 0, fmt.Errorf("Parsing Error on data returned by Factom Client")
}
if !b.Success {
return 0, fmt.Errorf(err.Error())
}
v, err := strconv.ParseInt(b.Response, 10, 64)
if err != nil {
return 0, fmt.Errorf("Invalid balance returned by factomd")
}
return v, nil
}
func GetBalances(state IState) []byte {
keys, values := state.GetFS().GetDB().GetKeysValues([]byte(fct.W_NAME))
ecKeys := make([]string, 0, len(keys))
fctKeys := make([]string, 0, len(keys))
ecBalances := make([]string, 0, len(keys))
fctBalances := make([]string, 0, len(keys))
fctAddresses := make([]string, 0, len(keys))
ecAddresses := make([]string, 0, len(keys))
var maxlen int
var connect = true
for i, k := range keys {
if len(k) > maxlen {
maxlen = len(k)
}
we, ok := values[i].(wallet.IWalletEntry)
if !ok {
panic("Get Addresses finds the database corrupt. Shouldn't happen")
}
var adr string
if we.GetType() == "ec" {
address, err := we.GetAddress()
if err != nil {
continue
}
adr = fct.ConvertECAddressToUserStr(address)
ecAddresses = append(ecAddresses, adr)
ecKeys = append(ecKeys, string(k))
bal, err := ECBalance(state, adr)
if err != nil {
connect = false
}
if connect {
ecBalances = append(ecBalances, strconv.FormatInt(bal, 10))
} else {
ecBalances = append(ecBalances, "-")
}
} else {
address, err := we.GetAddress()
if err != nil {
continue
}
adr = fct.ConvertFctAddressToUserStr(address)
fctAddresses = append(fctAddresses, adr)
fctKeys = append(fctKeys, string(k))
bal, err := FctBalance(state, adr)
if err != nil {
connect = false
}
sbal := fct.ConvertDecimal(uint64(bal))
if connect {
fctBalances = append(fctBalances, sbal)
} else {
fctBalances = append(fctBalances, "-")
}
}
}
var out bytes.Buffer
if len(fctKeys) > 0 {
out.WriteString("\n Factoid Addresses\n\n")
}
fstr := fmt.Sprintf("%s%vs %s38s %s14s\n", "%", maxlen+4, "%", "%")
for i, key := range fctKeys {
str := fmt.Sprintf(fstr, key, fctAddresses[i], fctBalances[i])
out.WriteString(str)
}
if len(ecKeys) > 0 {
out.WriteString("\n Entry Credit Addresses\n\n")
}
for i, key := range ecKeys {
str := fmt.Sprintf(fstr, key, ecAddresses[i], ecBalances[i])
out.WriteString(str)
}
if !connect {
out.WriteString("Balances are unavailable; Wallet is offline\n")
}
return out.Bytes()
}
/*************************************************************
* Balance
*************************************************************/
type Balance struct {
ICommand
}
func (Balance) Execute(state IState, args []string) (err error) {
if len(args) != 3 {
return fmt.Errorf("Wrong number of parameters")
}
var bal int64
switch strings.ToLower(args[1]) {
case "ec":
bal, err = ECBalance(state, args[2])
case "fct":
bal, err = FctBalance(state, args[2])
default:
return fmt.Errorf("Invalid parameters")
}
if err != nil {
return err
}
if args[1] == "fct" {
fmt.Println(args[2], "=", fct.ConvertDecimal(uint64(bal)))
} else {
fmt.Println(args[2], "=", bal)
}
return nil
}
func (Balance) Name() string {
return "Balance"
}
func (Balance) ShortHelp() string {
return "Balance <ec|fct> <name|address> -- Returns the Entry Credits or Factoids at the\n" +
" specified name or address"
}
func (Balance) LongHelp() string {
return `
Balance <ec|fct> <name|address> ec -- an Entry Credit address balance
fct -- a Factoid address balance
name -- Look up address by its name
address -- specify the address directly
`
}
/*************************************************************
* New Address
*************************************************************/
type NewAddress struct {
ICommand
}
func (NewAddress) Execute(state IState, args []string) (err error) {
if len(args) != 3 {
return fmt.Errorf("Incorrect Number of Arguments")
}
if err := ValidName(args[2]); err != nil {
return err
}
return GenAddress(state, args[1], args[2])
}
func (NewAddress) Name() string {
return "NewAddress"
}
func (NewAddress) ShortHelp() string {
return "NewAddress <ec|fct> <name> -- Returns a new Entry Credit or Factoid address"
}
func (NewAddress) LongHelp() string {
return `
NewAddress <ec|fct> <name> <ec> Generates a new Entry Credit Address and
saves it with the given <name>
<fct> Generates a new Factom Address and saves it
with the given <name>
<name> Names must be made up of alphanumeric
characters or underscores. They cannot be
more than 32 characters in length.
`
}
/*************************************************************
* Balances
*************************************************************/
type Balances struct {
ICommand
}
func (Balances) Execute(state IState, args []string) (err error) {
if len(args) != 1 {
return fmt.Errorf("Balances takes no arguments")
}
fmt.Println(string(GetBalances(state)))
return nil
}
func (Balances) Name() string {
return "Balances"
}
func (Balances) ShortHelp() string {
return "Balances -- Returns the balances of the Factoids and Entry Credits" +
" in this wallet, or tracked by this wallet."
}
func (Balances) LongHelp() string {
return `
Balances Returns the Factoid and Entry Credit names
and balances for the address in or tracked by
this wallet.
`
}