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
/
action_xpub.go
284 lines (237 loc) · 7.47 KB
/
action_xpub.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
package bux
import (
"context"
"sort"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/utils"
"github.com/libsv/go-bt"
"github.com/mrz1836/go-whatsonchain"
)
// NewXpub will parse the xPub and save it into the Datastore
//
// xPubKey is the raw public xPub
// opts are options and can include "metadata"
func (c *Client) NewXpub(ctx context.Context, xPubKey string, opts ...ModelOps) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "new_xpub")
// Create the model & set the default options (gives options from client->model)
xPub := newXpub(
xPubKey, c.DefaultModelOptions(append(opts, New())...)...,
)
// Save the model
if err := xPub.Save(ctx); err != nil {
return nil, err
}
// Return the created model
return xPub, nil
}
// GetXpub will get an existing xPub from the Datastore
//
// xPubKey is the raw public xPub
func (c *Client) GetXpub(ctx context.Context, xPubKey string) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_xpub")
// Attempt to get from cache or datastore
xPub, err := getXpubWithCache(ctx, c, xPubKey, "", c.DefaultModelOptions()...)
if err != nil {
return nil, err
}
// Return the model
return xPub, nil
}
// GetXpubByID will get an existing xPub from the Datastore
//
// xPubID is the hash of the xPub
func (c *Client) GetXpubByID(ctx context.Context, xPubID string) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_xpub_by_id")
// Attempt to get from cache or datastore
xPub, err := getXpubWithCache(ctx, c, "", xPubID, c.DefaultModelOptions()...)
if err != nil {
return nil, err
}
// Return the model
return xPub, nil
}
// UpdateXpubMetadata will update the metadata in an existing xPub
//
// xPubID is the hash of the xP
func (c *Client) UpdateXpubMetadata(ctx context.Context, xPubID string, metadata Metadata) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "update_xpub_by_id")
// Get the xPub
xPub, err := c.GetXpubByID(ctx, xPubID)
if err != nil {
return nil, err
}
// Update the metadata
xPub.UpdateMetadata(metadata)
// Save the model
if err = xPub.Save(ctx); err != nil {
return nil, err
}
// Return the model
return xPub, nil
}
// ImportXpub will import a given xPub and all related destinations and transactions
//
// xPubKey is the raw public xPub
func (c *Client) ImportXpub(ctx context.Context, xPubKey string, opts ...ModelOps) (*ImportResults, error) {
// Validate the xPub
xPub, err := utils.ValidateXPub(xPubKey)
if err != nil {
return nil, err
}
// Start an accumulator
results := &ImportResults{Key: xPub.String()}
// Set the WOC client
woc := c.Chainstate().WhatsOnChain()
var allTransactions []*whatsonchain.HistoryRecord
// Assume gap of 10 addresses, if no txs are found for 10
maxGapLimit := 10
// internal/external derivations, assume we've found everything
gapHit := false
for !gapHit {
// Derive internal addresses until depth
c.Logger().Info(ctx, "Deriving internal addresses...")
addressList := whatsonchain.AddressList{}
var addresses []string
if addresses, err = c.deriveAddresses(
ctx, xPub.String(), utils.ChainInternal, maxGapLimit,
); err != nil {
return nil, err
}
results.InternalAddresses += maxGapLimit
addressList.Addresses = append(addressList.Addresses, addresses...)
// Derive external addresses until gap limit
c.Logger().Info(ctx, "Deriving external addresses...")
if addresses, err = c.deriveAddresses(
ctx, xPub.String(), utils.ChainExternal, maxGapLimit,
); err != nil {
return nil, err
}
results.ExternalAddresses += maxGapLimit
addressList.Addresses = append(addressList.Addresses, addresses...)
// Get all transactions for those addresses
/*transactions, addressesWithTransactions, err := getAllTransactionsFromAddresses(
ctx, woc, addressList,
)*/
var transactions []*whatsonchain.HistoryRecord
transactions, err = getTransactionsFromAddressesViaBitbus(addressList.Addresses)
if err != nil {
return nil, err
}
if len(transactions) == 0 {
gapHit = true
}
// results.AddressesWithTransactions = append(results.AddressesWithTransactions, addressesWithTransactions...)
allTransactions = append(allTransactions, transactions...)
}
// Remove any duplicate transactions from all historical txs
allTransactions = removeDuplicates(allTransactions)
// Set all the hashes
txHashes := whatsonchain.TxHashes{}
for _, t := range allTransactions {
txHashes.TxIDs = append(txHashes.TxIDs, t.TxHash)
results.TransactionsFound++
}
// Run the bulk transaction processor
var rawTxs []string
var txInfos whatsonchain.TxList
if txInfos, err = woc.BulkRawTransactionDataProcessor(
ctx, &txHashes,
); err != nil {
return nil, err
}
// Loop and build from the inputs
var tx *bt.Tx
for i := 0; i < len(txInfos); i++ {
if tx, err = bt.NewTxFromString(
txInfos[i].Hex,
); err != nil {
return nil, err
}
var inputs []whatsonchain.VinInfo
for _, in := range tx.Inputs {
// todo: upgrade and use go-bt v2
vin := whatsonchain.VinInfo{
TxID: in.PreviousTxID,
}
inputs = append(inputs, vin)
}
txInfos[i].Vin = inputs
rawTxs = append(rawTxs, txInfos[i].Hex)
}
// Sort all transactions by block height
c.Logger().Info(ctx, "Sorting transactions to be recorded...")
sort.Slice(txInfos, func(i, j int) bool {
return txInfos[i].BlockHeight < txInfos[j].BlockHeight
})
// Sort transactions that are in the same block by previous tx
for i := 0; i < len(txInfos); i++ {
info := txInfos[i]
bh := info.BlockHeight
var sameBlockTxs []*whatsonchain.TxInfo
sameBlockTxs = append(sameBlockTxs, info)
// Loop through all remaining txs until block height is not the same
for j := i + 1; j < len(txInfos); j++ {
if txInfos[j].BlockHeight == bh {
sameBlockTxs = append(sameBlockTxs, txInfos[j])
} else {
break
}
}
if len(sameBlockTxs) == 1 {
continue
}
// Sort transactions by whether previous txs are referenced in the inputs
sort.Slice(sameBlockTxs, func(i, j int) bool {
for _, in := range sameBlockTxs[i].Vin {
if in.TxID == sameBlockTxs[j].Hash {
return false
}
}
return true
})
copy(txInfos[i:i+len(sameBlockTxs)], sameBlockTxs)
i += len(sameBlockTxs) - 1
}
// Record transactions in bux
for _, rawTx := range rawTxs {
if _, err = c.RecordTransaction(
ctx, xPubKey, rawTx, "", opts...,
); err != nil {
return nil, err
}
results.TransactionsImported++
}
return results, nil
}
// GetXPubs gets all xpubs matching the conditions
func (c *Client) GetXPubs(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_destinations")
// Get the count
xPubs, err := getXPubs(
ctx, metadataConditions, conditions, queryParams, c.DefaultModelOptions(opts...)...,
)
if err != nil {
return nil, err
}
return xPubs, nil
}
// GetXPubsCount gets a count of all xpubs matching the conditions
func (c *Client) GetXPubsCount(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_destinations")
// Get the count
count, err := getXPubsCount(
ctx, metadataConditions, conditions, c.DefaultModelOptions(opts...)...,
)
if err != nil {
return 0, err
}
return count, nil
}