-
Notifications
You must be signed in to change notification settings - Fork 23
/
config.go
179 lines (154 loc) · 4.59 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
package utils
import (
"encoding/base64"
"encoding/hex"
"io/fs"
"math"
"net"
"regexp"
"strings"
"time"
)
type ProposalStatus int
const (
ProposalStatusSynced ProposalStatus = iota
ProposalStatusNewProposal
ProposalStatusVoteStarted
ProposalStatusVoteFinished
)
type (
// AssetType is the capitalized version of an asset's symbol (e.g BTC, DCR,
// LTC) that serves as the asset's unique identity.
AssetType string
SyncStage int8
)
const (
LogFileName = "libwallet.log"
NilAsset AssetType = ""
BTCWalletAsset AssetType = "BTC"
DCRWalletAsset AssetType = "DCR"
LTCWalletAsset AssetType = "LTC"
fullDateformat = "2006-01-02 15:04:05"
dateOnlyFormat = "2006-01-02"
timeOnlyformat = "15:04:05"
shortTimeformat = "2006-01-02 15:04"
InvalidSyncStage SyncStage = -1
CFiltersFetchSyncStage SyncStage = 0
HeadersFetchSyncStage SyncStage = 1
AddressDiscoverySyncStage SyncStage = 2
HeadersRescanSyncStage SyncStage = 3
TxFilterAll int32 = 0
TxFilterSent int32 = 1
TxFilterReceived int32 = 2
TxFilterTransferred int32 = 3
TxFilterStaking int32 = 4
TxFilterCoinBase int32 = 5
TxFilterRegular int32 = 6
TxFilterMixed int32 = 7
TxFilterVoted int32 = 8
TxFilterRevoked int32 = 9
TxFilterImmature int32 = 10
TxFilterLive int32 = 11
TxFilterUnmined int32 = 12
TxFilterExpired int32 = 13
TxFilterTickets int32 = 14
TypeFilter = "Type"
DirectionFilter = "Direction"
HeightFilter = "BlockHeight"
TicketSpenderFilter = "TicketSpender"
LogLevelOff = "off"
LogLevelTrace = "trace"
LogLevelDebug = "debug"
LogLevelInfo = "info"
LogLevelWarn = "warn"
LogLevelError = "error"
LogLevelCritical = "critical"
DefaultLogLevel = LogLevelInfo
// UserFilePerm contains permissions for the user only. Attempting to modify
// more permissions require a super user permission that isn't readily available.
UserFilePerm = fs.FileMode(0o700)
)
// Stringer used in generating the directory path where the lowercase of the
// asset type is required. The uppercase defined by default is required to
// asset previously created using the uppercase.
func (str AssetType) ToStringLower() string {
return strings.ToLower(string(str))
}
// ToFull returns the full network name of the provided asset.
func (str AssetType) ToFull() string {
switch str {
case BTCWalletAsset:
return "Bitcoin"
case DCRWalletAsset:
return "Decred"
case LTCWalletAsset:
return "Litecoin"
default:
return "Unknown"
}
}
func (str AssetType) String() string {
return string(str)
}
// 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(dateOnlyFormat)
}
return utcTime.Format(timeOnlyformat)
}
func FormatUTCTime(timestamp int64) string {
return time.Unix(timestamp, 0).UTC().Format(fullDateformat)
}
func FormatUTCShortTime(timestamp int64) string {
return time.Unix(timestamp, 0).UTC().Format(shortTimeformat)
}
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) {
b, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return nil, err
}
return b, nil
}
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 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
}
// TrimNonAphanumeric removes all the characters that don't include the following:
// `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-`
func TrimNonAphaNumeric(text string) string {
reg, _ := regexp.Compile("[^a-zA-Z0-9-]+")
return reg.ReplaceAllString(text, "")
}