This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
import.go
165 lines (153 loc) · 4.67 KB
/
import.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
package bux
import (
"bufio"
"bytes"
"context"
"errors"
"io"
"net/http"
"os"
"strconv"
"github.com/BuxOrg/bux/utils"
"github.com/mrz1836/go-whatsonchain"
"github.com/tidwall/gjson"
)
// ImportResults are the results from the import
type ImportResults struct {
ExternalAddresses int `json:"external_addresses"`
InternalAddresses int `json:"internal_addresses"`
AddressesWithTransactions []string `json:"addresses_with_transactions"`
Key string `json:"key"`
TransactionsFound int `json:"transactions_found"`
TransactionsImported int `json:"transactions_imported"`
}
/*
// getUnspentTransactionsFromAddresses will get all unspent transactions related to address
func getUnspentTransactionsFromAddresses(ctx context.Context, client whatsonchain.ClientInterface, addressList whatsonchain.AddressList) ([]*whatsonchain.HistoryRecord, error) {
histories, err := client.BulkUnspentTransactionsProcessor(
ctx, &addressList,
)
if err != nil {
return nil, err
}
var txs []*whatsonchain.HistoryRecord
for _, h := range histories {
txs = append(txs, h.Utxos...)
}
return txs, nil
}
*/
/*
// getAllTransactionsFromAddresses will get all transactions related to addresses
func getAllTransactionsFromAddresses(ctx context.Context, client whatsonchain.ClientInterface,
addressList whatsonchain.AddressList) ([]*whatsonchain.HistoryRecord, []string, error) {
var addressesWithTransactions []string
var txs []*whatsonchain.HistoryRecord
for _, address := range addressList.Addresses {
history, err := client.AddressHistory(ctx, address)
if err != nil {
return nil, addressesWithTransactions, err
}
if len(history) > 0 {
addressesWithTransactions = append(addressesWithTransactions, address)
}
txs = append(txs, history...)
}
return txs, addressesWithTransactions, nil
}
*/
// deriveAddresses will derive a new set of addresses for a xpub
func (c *Client) deriveAddresses(ctx context.Context, xpub string, chain uint32, amount int) ([]string, error) {
var addressList []string
for i := 0; i < amount; i++ {
destination, err := c.NewDestination(ctx, xpub, chain, utils.ScriptTypePubKeyHash, false, c.DefaultModelOptions()...)
if err != nil {
return []string{}, err
}
addressList = append(addressList, destination.Address)
}
return addressList, nil
}
// removeDuplicates will remove duplicate transactions
func removeDuplicates(transactions []*whatsonchain.HistoryRecord) []*whatsonchain.HistoryRecord {
keys := make(map[string]bool)
var list []*whatsonchain.HistoryRecord
for _, tx := range transactions {
if _, value := keys[tx.TxHash]; !value {
keys[tx.TxHash] = true
list = append(list, tx)
}
}
return list
}
func getTransactionsFromAddressesViaBitbus(addresses []string) ([]*whatsonchain.HistoryRecord, error) {
var transactions []*whatsonchain.HistoryRecord
parentQuery := []byte(`
{
"q": {
"find": { "$or": [ { "out.e.a": "ADDRESS" }, {"in.e.a": "ADDRESS"} ] },
"sort": { "blk.i": 1 },
"project": { "blk": 1, "tx.h": 1, "out.f3": 1 },
"limit": LIMIT,
"skip": OFFSET
}
}`)
for _, address := range addresses {
doneWithPagination := false
offset := 0
limit := 100
for !doneWithPagination {
query := bytes.Replace(parentQuery, []byte("ADDRESS"), []byte(address), 2)
query = bytes.Replace(query, []byte("OFFSET"), []byte(strconv.Itoa(offset)), 1)
query = bytes.Replace(query, []byte("LIMIT"), []byte(strconv.Itoa(limit)), 1)
txs, err := bitbusRequest(query)
if err != nil {
return transactions, err
}
transactions = append(transactions, txs...)
if len(txs) < 100 {
doneWithPagination = true
}
offset += 100
}
}
return transactions, nil
}
func bitbusRequest(query []byte) ([]*whatsonchain.HistoryRecord, error) {
planariaToken := os.Getenv("PLANARIA_TOKEN")
client := http.Client{}
var transactions []*whatsonchain.HistoryRecord
req, err := http.NewRequestWithContext(
context.Background(), http.MethodPost, "https://txo.bitbus.network/block", bytes.NewBuffer(query),
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("token", planariaToken)
var resp *http.Response
if resp, err = client.Do(req); err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
reader := bufio.NewReader(resp.Body)
for {
var line []byte
line, err = reader.ReadBytes('\n')
if errors.Is(err, io.EOF) {
break
}
json := gjson.ParseBytes(line)
txID := gjson.Get(json.String(), "tx.h")
if txID.Str == "" {
break
}
record := &whatsonchain.HistoryRecord{
TxHash: txID.Str,
}
transactions = append(transactions, record)
}
return transactions, nil
}