-
Notifications
You must be signed in to change notification settings - Fork 21
/
phrase.go
331 lines (272 loc) · 7.71 KB
/
phrase.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
package main
//TODO: add in checking for bip44 btc
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/hdkeychain"
bip32 "github.com/tyler-smith/go-bip32"
bip39 "github.com/tyler-smith/go-bip39"
)
// Phrase represents a phrase we are examining
type Phrase struct {
master string
xprv *hdkeychain.ExtendedKey
}
// Address represents a crypto address generated from a phrase, including details about how/if it has been used
type Address struct {
Address string
TxCount int
Balance float64
IsTest bool
Tokens []Token
}
// Token represents a balance of ERC20 tokens tied to an Ethereum address. Name is the ticker of the token, and
// Address is the contract address for the token.
type Token struct {
Name string
Ticker string
Address string
Balance float64
TxCount int
}
// NewPhrase returns a phrase object with the master key generated (and the eth address, since that is static)
func NewPhrase(phrase string) (p *Phrase, err error) {
p = new(Phrase)
// Populate our BIP39 seed
seed := bip39.NewSeed(phrase, "")
masterKey, _ := bip32.NewMasterKey(seed)
p.master = masterKey.String()
// Get our master xprv
// Path: m
p.xprv, err = hdkeychain.NewKeyFromString(p.master)
return
}
// getBitcoinAddress by specifying the chain number (0 for normal, 1 for change), child number (address number),
// and whether testnet or not. Count specifies how many addresses to return.
func (p Phrase) getBitcoinAddresses(purpose uint32, coin uint32, chainNo uint32, childNo uint32, count int, testnet bool) (addresses []*Address) {
if count < 1 {
count = 1
}
if count > 100 {
count = 100
}
for i := 0; i < count; i++ {
// Path: m/0H/[chain]/[child] (e.g. m/0H/0/0)
child, err := deriveHDKey(p.xprv, purpose, coin, 0, chainNo, childNo)
if err != nil {
fmt.Println("Uh-oh! HD derivation error:", err)
return
}
// Generate address based on testnet or mainnet
params := &chaincfg.MainNetParams
if testnet {
params = &chaincfg.TestNet3Params
}
pkh, err := child.Address(params)
if err != nil {
fmt.Println("Uh-oh! HD derivation error:", err)
return
}
addresses = append(addresses, &Address{Address: pkh.EncodeAddress()})
childNo++
}
return
}
// LookupBTC takes a slice of addresses and fills in the details
func (p Phrase) LookupBTC(addresses []*Address, isTestnet bool) (err error) {
domain := ""
if isTestnet {
domain = "testnet."
}
var addylist string
for i, v := range addresses {
if isTestnet {
addresses[i].IsTest = true
}
addylist += v.Address + ","
}
var BCi map[string]struct {
Balance int64 `json:"final_balance"`
TxCount int `json:"n_tx"`
Received int64 `json:"total_received"`
}
err = callAPI("https://"+domain+"blockchain.info/balance?active="+addylist, &BCi, btcRate)
if err != nil {
return
}
for i, v := range addresses {
addresses[i].TxCount = BCi[v.Address].TxCount
addresses[i].Balance = float64(float64(BCi[v.Address].Balance) / 100000000)
}
return
}
// LookupBCH takes a slice of addresses and fills in the details
func (p Phrase) LookupBCH(addresses []*Address) (err error) {
// NOTE: the API no longer supports looking up multiple addresses at once, so we will have to do them one at a time
for i, v := range addresses {
var BTCcom struct {
Error int `json:"err_no"`
Data struct {
Address string `json:"address"`
Balance int64 `json:"balance"`
TxCount int `json:"tx_count"`
Received int64 `json:"received"`
} `json:"data"`
}
url := "https://bch-chain.api.btc.com/v3/address/" + v.Address
err = callAPI(url, &BTCcom, btcRate)
if err != nil {
return
}
if BTCcom.Error != 0 {
err = errors.New("BTC.com error: " + strconv.Itoa(BTCcom.Error))
return
}
// We'll stop at the first unused address
if BTCcom.Data.TxCount == 0 {
return
}
addresses[i].TxCount = BTCcom.Data.TxCount
addresses[i].Balance = float64(float64(BTCcom.Data.Balance) / 100000000)
}
return
}
// LookupBTCBal follows the entire btc/bch chain and finds out the remaining balance for the entire wallet.
func (p Phrase) LookupBTCBal(coin string) (balance float64, isUsed bool, addresses []*Address, err error) {
batch := 50 // How many addresses to fetch at one time
skips := 0 // How many empty addresses in a row we've found
// chain 0 = main, chain 1 = change
for chain := uint32(0); chain < 2; chain++ {
child := uint32(0)
for skips < 10 { // Go until we find 10 in a row that are unused
var addr []*Address
switch coin {
case "btc32":
addr = p.getBitcoinAddresses(0, 0, chain, child, batch, false)
err = p.LookupBTC(addr, false)
case "btc44":
addr = p.getBitcoinAddresses(44, 0, chain, child, batch, false)
err = p.LookupBTC(addr, false)
case "tbt32":
addr = p.getBitcoinAddresses(0, 0, chain, child, batch, true)
err = p.LookupBTC(addr, true)
case "tbt44":
addr = p.getBitcoinAddresses(44, 1, chain, child, batch, true)
err = p.LookupBTC(addr, true)
case "bch32":
addr = p.getBitcoinAddresses(0, 0, chain, child, batch, false)
err = p.LookupBCH(addr)
case "bch440":
addr = p.getBitcoinAddresses(44, 0, chain, child, batch, false)
err = p.LookupBCH(addr)
case "bch44145":
addr = p.getBitcoinAddresses(44, 145, chain, child, batch, false)
err = p.LookupBCH(addr)
}
for _, v := range addr {
balance += v.Balance
if v.TxCount > 0 {
isUsed = true
skips = 0
} else {
skips++
}
}
addresses = append(addresses, addr...)
child += uint32(batch)
}
}
return
}
// Derive a key for a specific location in a BIP44 wallet
func deriveHDKey(xprv *hdkeychain.ExtendedKey, purpose uint32, coin uint32, account uint32, chain uint32, address uint32) (pubkey *hdkeychain.ExtendedKey, err error) {
// Path: m/44H
purp, err := xprv.Child(hdkeychain.HardenedKeyStart + purpose)
if err != nil {
return
}
if purpose == 0 {
// This is a bip32 path
// Path: m/0H/[chain]
var cha *hdkeychain.ExtendedKey
cha, err = purp.Child(chain)
if err != nil {
return
}
// Path: m/0H/[chain]/[child] (e.g. m/0H/0/0)
pubkey, err = cha.Child(address)
return
}
// Coin number
// Path: m/44H/60H
co, err := purp.Child(hdkeychain.HardenedKeyStart + coin)
if err != nil {
return
}
// Path: m/44H/60H/0H
acc, err := co.Child(hdkeychain.HardenedKeyStart + account)
if err != nil {
return
}
// Path: m/44H/60H/0H/0
cha, err := acc.Child(chain)
if err != nil {
fmt.Println(err)
return
}
// Path: m/44H/60H/0H/0/0
pubkey, err = cha.Child(address)
if err != nil {
fmt.Println(err)
return
}
return
}
func callAPI(url string, target interface{}, rate int64) (err error) {
prnt := fmt.Sprintf("Please wait: %s...%s", url[8:30], url[len(url)-10:])
fmt.Printf("%s\r", prnt)
//rate limit
time.Sleep(time.Until(lastcall.Add(time.Millisecond * time.Duration(rate))))
var res *http.Response
client := http.Client{
Timeout: 5 * time.Second,
}
res, err = client.Get(url)
if err != nil {
return
}
var data []byte
data, err = ioutil.ReadAll(res.Body)
if err != nil {
return
}
err = json.Unmarshal(data, target)
if err != nil {
return errors.New("Invalid server response: " + string(data))
}
lastcall = time.Now()
fmt.Printf("%s\r", strings.Repeat(" ", len(prnt)))
return
}
// Truncate any eth value to 8 decimals of precision (make large numbers easier)
func snipEth(input string, decimal int) (output float64, err error) {
tocut := decimal - 8
if tocut > 0 && len(input)-tocut > 0 {
start := len(input) - tocut
input = input[:start]
decimal = 8
}
output, err = strconv.ParseFloat(input, 64)
if decimal > 0 {
output = output / math.Pow(10, float64(decimal))
}
return
}