forked from 5haman/maltego-btc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwallet.go
More file actions
166 lines (135 loc) · 4.37 KB
/
wallet.go
File metadata and controls
166 lines (135 loc) · 4.37 KB
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
package main
import (
"encoding/json"
"github.com/patrickmn/go-cache"
"log"
"strconv"
"time"
)
type WalletTxOutput struct {
WalletId string `json:"wallet_id"`
Label string `json:"label"`
Amount float64 `json:"amount"`
}
type WalletTx struct {
TxId string `json:"txid"`
WalletId string `json:"wallet_id"`
Label string `json:"label"`
Time uint64 `json:"time"`
Amount float64 `json:"amount"`
Balance float64 `json:"balance"`
Type string `json:"type"`
Fee float64 `json:"fee"`
WalletTxOutputs []WalletTxOutput `json:"outputs"`
}
type Wallet struct {
Label string `json:"label"`
WalletId string `json:"wallet_id"`
TxCount int `json:"txs_count"`
WalletTxs []WalletTx `json:"txs"`
Histogram []float64 `json:"histogram"`
Addresses []string `json:"addresses"`
Cached uint64 `json:"-"`
}
type WalletAddress struct {
Address string `json:"address"`
Balance float64 `json:"balance"`
IncomingTxs int `json:"incoming_txs"`
}
type WalletAddresses struct {
Label string `json:"label"`
WalletId string `json:"wallet_id"`
AddressesCount int `json:"addresses_count"`
Addresses []WalletAddress `json:"addresses"`
Cached uint64 `json:"-"`
}
func (w Wallet) GetId() string {
return w.WalletId
}
func (w WalletAddresses) GetId() string {
return w.WalletId
}
func (w Wallet) GetCacheTime() uint64 {
return w.Cached
}
func (w WalletAddresses) GetCacheTime() uint64 {
return w.Cached
}
func GetWallet(query string) (wallet Wallet) {
obj, found := WalletModel.Get(query)
if found {
wallet = obj.(Wallet)
log.Println("cache hit:", query)
} else {
wallet = RequestWallet(query, 0)
if wallet.TxCount > step && wallet.TxCount < config.TxsThreshold {
for from := step; from <= wallet.TxCount; from += step {
time.Sleep(2000 * time.Millisecond)
wallet2 := RequestWallet(query, from)
for _, tx := range wallet2.WalletTxs {
wallet.WalletTxs = append(wallet.WalletTxs, tx)
}
}
}
x := TimeRange{}
for _, t := range wallet.WalletTxs {
h := t.Time % 24
x = append(x, float64(h))
}
wallet.Histogram = HourHistogram(x)
wallet.Cached = uint64(time.Now().Unix())
// save to file cache
WalletModel.Set(wallet.WalletId, wallet, cache.NoExpiration)
}
//log.Println("time histogram:", wallet.WalletId, wallet.Histogram)
return
}
func RequestWallet(query string, from int) (wallet Wallet) {
requestMap[query] = true
log.Println("http: get wallet ", query)
url := ApiUrl + "/wallet?wallet=" + query + "&from=" + strconv.Itoa(int(from)) + "&count=100&caller=" + ApiAgent
bytes := HttpRequest(url)
_ = json.Unmarshal(bytes, &wallet)
return
}
func GetWalletAddresses(query string) (addresses WalletAddresses) {
obj, found := WalletAddressesModel.Get(query)
if found {
addresses = obj.(WalletAddresses)
log.Println("cache hit:", query)
} else {
addresses = RequestWalletAddresses(query, 0)
if addresses.AddressesCount > step && addresses.AddressesCount < config.TxsThreshold {
for from := step; from <= addresses.AddressesCount; from += step {
addresses2 := RequestWalletAddresses(query, from)
for _, addr := range addresses2.Addresses {
addresses.Addresses = append(addresses.Addresses, addr)
}
}
}
addresses.Cached = uint64(time.Now().Unix())
// save to redis cache
WalletAddressesModel.Set(addresses.WalletId, addresses, cache.NoExpiration)
}
return
}
func RequestWalletAddresses(query string, from int) (addresses WalletAddresses) {
requestMap[query] = true
log.Println("http: get wallet addresses ", query)
url := ApiUrl + "/wallet-addresses?wallet=" + query + "&from=" + strconv.Itoa(int(from)) + "&count=100&caller=" + ApiAgent
bytes := HttpRequest(url)
_ = json.Unmarshal(bytes, &addresses)
return
}
func GetAddressWallet(query string) (wallet Wallet) {
//No cache for this yet, placeholder function
wallet = Address2Wallet(query, 0)
return
}
func Address2Wallet(query string, from int) (wallet Wallet) {
log.Println("http: get address from wallet ", query)
url := ApiUrl + "/address-lookup?address=" + query + "&from=" + strconv.Itoa(int(from)) + "&count=100&caller=" + ApiAgent
bytes := HttpRequest(url)
_ = json.Unmarshal(bytes, &wallet)
return
}