-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.go
484 lines (415 loc) · 13.3 KB
/
utils.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package dcrlibwallet
import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"decred.org/dcrwallet/v2/errors"
"decred.org/dcrwallet/v2/wallet"
"decred.org/dcrwallet/v2/wallet/txrules"
"decred.org/dcrwallet/v2/walletseed"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/hdkeychain/v3"
"github.com/decred/dcrd/wire"
"github.com/planetdecred/dcrlibwallet/internal/loader"
)
const (
walletDbName = "wallet.db"
// FetchPercentage is used to increase the initial estimate gotten during cfilters stage
FetchPercentage = 0.38
// Use 10% of estimated total headers fetch time to estimate rescan time
RescanPercentage = 0.1
// Use 80% of estimated total headers fetch time to estimate address discovery time
DiscoveryPercentage = 0.8
MaxAmountAtom = dcrutil.MaxAmount
MaxAmountDcr = dcrutil.MaxAmount / dcrutil.AtomsPerCoin
TestnetHDPath = "m / 44' / 1' / "
LegacyTestnetHDPath = "m / 44’ / 11’ / "
MainnetHDPath = "m / 44' / 42' / "
LegacyMainnetHDPath = "m / 44’ / 20’ / "
DefaultRequiredConfirmations = 2
LongAbbreviationFormat = "long"
ShortAbbreviationFormat = "short"
ShortestAbbreviationFormat = "shortest"
)
func (mw *MultiWallet) RequiredConfirmations() int32 {
spendUnconfirmed := mw.ReadBoolConfigValueForKey(SpendUnconfirmedConfigKey, false)
if spendUnconfirmed {
return 0
}
return DefaultRequiredConfirmations
}
func (wallet *Wallet) RequiredConfirmations() int32 {
var spendUnconfirmed bool
wallet.readUserConfigValue(true, SpendUnconfirmedConfigKey, &spendUnconfirmed)
if spendUnconfirmed {
return 0
}
return DefaultRequiredConfirmations
}
func (mw *MultiWallet) listenForShutdown() {
mw.cancelFuncs = make([]context.CancelFunc, 0)
mw.shuttingDown = make(chan bool)
go func() {
<-mw.shuttingDown
for _, cancel := range mw.cancelFuncs {
cancel()
}
}()
}
func (wallet *Wallet) shutdownContextWithCancel() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
wallet.cancelFuncs = append(wallet.cancelFuncs, cancel)
return ctx, cancel
}
func (wallet *Wallet) shutdownContext() (ctx context.Context) {
ctx, _ = wallet.shutdownContextWithCancel()
return
}
func (mw *MultiWallet) contextWithShutdownCancel() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
mw.cancelFuncs = append(mw.cancelFuncs, cancel)
return ctx, cancel
}
func (mw *MultiWallet) ValidateExtPubKey(extendedPubKey string) error {
_, err := hdkeychain.NewKeyFromString(extendedPubKey, mw.chainParams)
if err != nil {
if err == hdkeychain.ErrInvalidChild {
return errors.New(ErrUnusableSeed)
}
return errors.New(ErrInvalid)
}
return nil
}
func NormalizeAddress(addr string, defaultPort string) (string, error) {
// If the first SplitHostPort errors because of a missing port and not
// for an invalid host, add the port. If the second SplitHostPort
// fails, then a port is not missing and the original error should be
// returned.
host, port, origErr := net.SplitHostPort(addr)
if origErr == nil {
return net.JoinHostPort(host, port), nil
}
addr = net.JoinHostPort(addr, defaultPort)
_, _, err := net.SplitHostPort(addr)
if err != nil {
return "", origErr
}
return addr, nil
}
// For use with gomobile bind,
// doesn't support the alternative `GenerateSeed` function because it returns more than 2 types.
func GenerateSeed() (string, error) {
seed, err := hdkeychain.GenerateSeed(hdkeychain.RecommendedSeedLen)
if err != nil {
return "", err
}
return walletseed.EncodeMnemonic(seed), nil
}
func VerifySeed(seedMnemonic string) bool {
_, err := walletseed.DecodeUserInput(seedMnemonic)
return err == nil
}
// ExtractDateOrTime returns the date represented by the timestamp as a date string if the timestamp is over 24 hours ago.
// Otherwise, the time alone is returned as a string.
func ExtractDateOrTime(timestamp int64) string {
utcTime := time.Unix(timestamp, 0).UTC()
if time.Now().UTC().Sub(utcTime).Hours() > 24 {
return utcTime.Format("2006-01-02")
} else {
return utcTime.Format("15:04:05")
}
}
func FormatUTCTime(timestamp int64) string {
return time.Unix(timestamp, 0).UTC().Format("2006-01-02 15:04:05")
}
func AmountCoin(amount int64) float64 {
return dcrutil.Amount(amount).ToCoin()
}
func AmountAtom(f float64) int64 {
amount, err := dcrutil.NewAmount(f)
if err != nil {
log.Error(err)
return -1
}
return int64(amount)
}
func EncodeHex(hexBytes []byte) string {
return hex.EncodeToString(hexBytes)
}
func EncodeBase64(text []byte) string {
return base64.StdEncoding.EncodeToString(text)
}
func DecodeBase64(base64Text string) ([]byte, error) {
return base64.StdEncoding.DecodeString(base64Text)
}
func ShannonEntropy(text string) (entropy float64) {
if text == "" {
return 0
}
for i := 0; i < 256; i++ {
px := float64(strings.Count(text, string(byte(i)))) / float64(len(text))
if px > 0 {
entropy += -px * math.Log2(px)
}
}
return entropy
}
func TransactionDirectionName(direction int32) string {
switch direction {
case TxDirectionSent:
return "Sent"
case TxDirectionReceived:
return "Received"
case TxDirectionTransferred:
return "Yourself"
default:
return "invalid"
}
}
func CalculateTotalTimeRemaining(timeRemainingInSeconds int64) string {
minutes := timeRemainingInSeconds / 60
if minutes > 0 {
return fmt.Sprintf("%d min", minutes)
}
return fmt.Sprintf("%d sec", timeRemainingInSeconds)
}
func CalculateDaysBehind(lastHeaderTime int64) string {
diff := time.Since(time.Unix(lastHeaderTime, 0))
daysBehind := int(math.Round(diff.Hours() / 24))
if daysBehind == 0 {
return "<1 day"
} else if daysBehind == 1 {
return "1 day"
} else {
return fmt.Sprintf("%d days", daysBehind)
}
}
func roundUp(n float64) int32 {
return int32(math.Round(n))
}
func WalletUniqueConfigKey(walletID int, key string) string {
return fmt.Sprintf("%d%s", walletID, key)
}
func WalletExistsAt(directory string) bool {
walletDbFilePath := filepath.Join(directory, walletDbName)
exists, err := fileExists(walletDbFilePath)
if err != nil {
log.Errorf("wallet exists check error: %v", err)
}
return exists
}
func fileExists(filePath string) (bool, error) {
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func moveFile(sourcePath, destinationPath string) error {
if exists, _ := fileExists(sourcePath); exists {
return os.Rename(sourcePath, destinationPath)
}
return nil
}
// done returns whether the context's Done channel was closed due to
// cancellation or exceeded deadline.
func done(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
func backupFile(fileName string, suffix int) (newName string, err error) {
newName = fileName + ".bak" + strconv.Itoa(suffix)
exists, err := fileExists(newName)
if err != nil {
return "", err
} else if exists {
return backupFile(fileName, suffix+1)
}
err = moveFile(fileName, newName)
if err != nil {
return "", err
}
return newName, nil
}
func initWalletLoader(chainParams *chaincfg.Params, walletDataDir, walletDbDriver string) *loader.Loader {
// TODO: Allow users provide values to override these defaults.
cfg := &WalletConfig{
GapLimit: 20,
AllowHighFees: false,
RelayFee: txrules.DefaultRelayFeePerKb,
AccountGapLimit: wallet.DefaultAccountGapLimit,
DisableCoinTypeUpgrades: false,
ManualTickets: false,
MixSplitLimit: 10,
}
stakeOptions := &loader.StakeOptions{
VotingEnabled: false,
AddressReuse: false,
VotingAddress: nil,
}
walletLoader := loader.NewLoader(chainParams, walletDataDir, stakeOptions,
cfg.GapLimit, cfg.AllowHighFees, cfg.RelayFee, cfg.AccountGapLimit,
cfg.DisableCoinTypeUpgrades, cfg.ManualTickets, cfg.MixSplitLimit)
if walletDbDriver != "" {
walletLoader.SetDatabaseDriver(walletDbDriver)
}
return walletLoader
}
// makePlural is used with the TimeElapsed function. makePlural checks if the arguments passed is > 1,
// if true, it adds "s" after the given time to make it plural
func makePlural(x float64) string {
if int(x) == 1 {
return ""
}
return "s"
}
// TimeElapsed returns the formatted time diffrence between two times as a string.
// If the argument `fullTime` is set to true, then the full time available is returned e.g 3 hours, 2 minutes, 20 seconds ago,
// as opposed to 3 hours ago.
// If the argument `abbreviationFormat` is set to `long` the time format is e.g 2 minutes
// If the argument `abbreviationFormat` is set to `short` the time format is e.g 2 mins
// If the argument `abbreviationFormat` is set to `shortest` the time format is e.g 2 m
func TimeElapsed(now, then time.Time, abbreviationFormat string, fullTime bool) string {
var parts []string
var text string
year2, month2, day2 := now.Date()
hour2, minute2, second2 := now.Clock()
year1, month1, day1 := then.Date()
hour1, minute1, second1 := then.Clock()
year := math.Abs(float64(year2 - year1))
month := math.Abs(float64(month2 - month1))
day := math.Abs(float64(day2 - day1))
hour := math.Abs(float64(hour2 - hour1))
minute := math.Abs(float64(minute2 - minute1))
second := math.Abs(float64(second2 - second1))
week := math.Floor(day / 7)
if year > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(year))+" year"+makePlural(year))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(year))+" yr"+makePlural(year))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(year))+" y")
}
}
if month > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(month))+" month"+makePlural(month))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(month))+" mon"+makePlural(month))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(month))+" m")
}
}
if week > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(week))+" week"+makePlural(week))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(week))+" wk"+makePlural(week))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(week))+" w")
}
}
if day > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(day))+" day"+makePlural(day))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(day))+" dy"+makePlural(day))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(day))+" d")
}
}
if hour > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(hour))+" hour"+makePlural(hour))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(hour))+" hr"+makePlural(hour))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(hour))+" h")
}
}
if minute > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(minute))+" minute"+makePlural(minute))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(minute))+" min"+makePlural(minute))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(minute))+" mi")
}
}
if second > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(second))+" second"+makePlural(second))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(second))+" sec"+makePlural(second))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(second))+" s")
}
}
if now.After(then) {
text = " ago"
} else {
text = " after"
}
if len(parts) == 0 {
return "just now"
}
if fullTime {
return strings.Join(parts, ", ") + text
}
return parts[0] + text
}
// voteVersion was borrowed from upstream, and needs to always be in
// sync with the upstream method. This is the LOC to the upstream version:
// https://github.com/decred/dcrwallet/blob/master/wallet/wallet.go#L266
func voteVersion(params *chaincfg.Params) uint32 {
switch params.Net {
case wire.MainNet:
return 9
case 0x48e7a065: // TestNet2
return 6
case wire.TestNet3:
return 10
case wire.SimNet:
return 10
default:
return 1
}
}
// HttpGet helps to convert json(Byte data) into a struct object.
func HttpGet(url string, respObj interface{}) (*http.Response, []byte, error) {
rq := new(http.Client)
resp, err := rq.Get((url))
if err != nil {
return nil, nil, err
}
respBytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, nil, err
}
if resp.StatusCode != http.StatusOK {
return resp, respBytes, fmt.Errorf("%d response from server: %v", resp.StatusCode, string(respBytes))
}
err = json.Unmarshal(respBytes, respObj)
return resp, respBytes, err
}