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
/
model_xpubs.go
314 lines (258 loc) · 8.61 KB
/
model_xpubs.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
package bux
import (
"context"
"errors"
"fmt"
"github.com/BuxOrg/bux/utils"
"github.com/mrz1836/go-datastore"
)
// Xpub is an object representing an HD-Key or extended public key (xPub for short)
//
// Gorm related models & indexes: https://gorm.io/docs/models.html - https://gorm.io/docs/indexes.html
type Xpub struct {
// Base model
Model `bson:",inline"`
// Model specific fields
ID string `json:"id" toml:"id" yaml:"id" gorm:"<-:create;type:char(64);primaryKey;comment:This is the sha256(xpub) hash" bson:"_id"`
CurrentBalance uint64 `json:"current_balance" toml:"current_balance" yaml:"current_balance" gorm:"<-;comment:The current balance of unspent satoshis" bson:"current_balance"`
NextInternalNum uint32 `json:"next_internal_num" toml:"next_internal_num" yaml:"next_internal_num" gorm:"<-;type:int;comment:The next index number for the internal xPub derivation" bson:"next_internal_num"`
NextExternalNum uint32 `json:"next_external_num" toml:"next_external_num" yaml:"next_external_num" gorm:"<-;type:int;comment:The next index number for the external xPub derivation" bson:"next_external_num"`
destinations []Destination `gorm:"-" bson:"-"` // json:"destinations,omitempty"
}
// newXpub will start a new xPub model
func newXpub(key string, opts ...ModelOps) *Xpub {
return &Xpub{
ID: utils.Hash(key),
Model: *NewBaseModel(ModelXPub, append(opts, WithXPub(key))...),
}
}
// newXpubUsingID will start a new xPub model using the xPubID
func newXpubUsingID(xPubID string, opts ...ModelOps) *Xpub {
return &Xpub{
ID: xPubID,
Model: *NewBaseModel(ModelXPub, opts...),
}
}
// getXpub will get the xPub with the given conditions
func getXpub(ctx context.Context, key string, opts ...ModelOps) (*Xpub, error) {
// Get the record
xPub := newXpub(key, opts...)
if err := Get(
ctx, xPub, nil, false, defaultDatabaseReadTimeout, true,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
return xPub, nil
}
// getXpubByID will get the xPub with the given conditions
func getXpubByID(ctx context.Context, xPubID string, opts ...ModelOps) (*Xpub, error) {
// Get the record
xPub := newXpubUsingID(xPubID, opts...)
if err := Get(
ctx, xPub, nil, false, defaultDatabaseReadTimeout, true,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
return xPub, nil
}
// getXpubWithCache will try to get from cache first, then datastore
//
// key is the raw xPub key or use xPubID
func getXpubWithCache(ctx context.Context, client ClientInterface,
key, xPubID string, opts ...ModelOps) (*Xpub, error) {
// Create the cache key
if len(key) > 0 {
xPubID = utils.Hash(key)
opts = append(opts, WithXPub(key)) // Add the xPub option which will set it on the model
} else if len(xPubID) == 0 {
return nil, ErrMissingFieldXpubID
}
cacheKey := fmt.Sprintf(cacheKeyXpubModel, xPubID)
// Attempt to get from cache
xPub := new(Xpub)
found, err := getModelFromCache(
ctx, client.Cachestore(), cacheKey, xPub,
)
if err != nil { // Found a real error
return nil, err
} else if found { // Return the cached model
xPub.enrich(ModelXPub, opts...) // Enrich the model with our parent options
return xPub, nil
}
client.Logger().Info(ctx, "xpub not found in cache")
// Get the xPub
if xPub, err = getXpubByID(
ctx, xPubID, opts...,
); err != nil {
return nil, err
} else if xPub == nil {
return nil, ErrMissingXpub
}
// Save to cache
// todo: run in a go routine
if err = saveToCache(
ctx, []string{cacheKey}, xPub, 0,
); err != nil {
return nil, err
}
// Return the model
return xPub, nil
}
// getXPubs will get all the xpubs matching the conditions
func getXPubs(ctx context.Context, usingMetadata *Metadata, conditions *map[string]interface{},
queryParams *datastore.QueryParams, opts ...ModelOps) ([]*Xpub, error) {
modelItems := make([]*Xpub, 0)
if err := getModelsByConditions(
ctx, ModelXPub, &modelItems, usingMetadata, conditions, queryParams, opts...,
); err != nil {
return nil, err
}
return modelItems, nil
}
// getXPubsCount will get a count of the xpubs matching the conditions
func getXPubsCount(ctx context.Context, usingMetadata *Metadata,
conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
return getModelCountByConditions(ctx, ModelXPub, Xpub{}, usingMetadata, conditions, opts...)
}
// GetModelName will get the name of the current model
func (m *Xpub) GetModelName() string {
return ModelXPub.String()
}
// GetModelTableName will get the db table name of the current model
func (m *Xpub) GetModelTableName() string {
return tableXPubs
}
// Save will save the model into the Datastore
func (m *Xpub) Save(ctx context.Context) error {
return Save(ctx, m)
}
// GetID will get the ID
func (m *Xpub) GetID() string {
return m.ID
}
// getNewDestination will get a new destination, adding to the xpub and incrementing num / address
func (m *Xpub) getNewDestination(ctx context.Context, chain uint32, destinationType string,
opts ...ModelOps) (*Destination, error) {
// Check the type
// todo: support more types of destinations
if destinationType != utils.ScriptTypePubKeyHash {
return nil, ErrUnsupportedDestinationType
}
// Increment the next num
num, err := m.incrementNextNum(ctx, chain)
if err != nil {
return nil, err
}
// Create the new address
var destination *Destination
if destination, err = newAddress(
m.rawXpubKey, chain, num, append(opts, New())...,
); err != nil {
return nil, err
}
// Add the destination to the xPub
m.destinations = append(m.destinations, *destination)
return destination, nil
}
// incrementBalance will atomically update the balance of the xPub
func (m *Xpub) incrementBalance(ctx context.Context, balanceIncrement int64) error {
// Increment the field
newBalance, err := incrementField(ctx, m, currentBalanceField, balanceIncrement)
if err != nil {
return err
}
// Update the field value
m.CurrentBalance = uint64(newBalance)
// Fire the after update
err = m.AfterUpdated(ctx)
return err
}
// incrementNextNum will atomically update the num of the given chain of the xPub and return it
func (m *Xpub) incrementNextNum(ctx context.Context, chain uint32) (uint32, error) {
var err error
var newNum int64
// Choose the field to update
fieldName := nextExternalNumField
if chain == utils.ChainInternal {
fieldName = nextInternalNumField
}
// Try to increment the field
if newNum, err = incrementField(
ctx, m, fieldName, 1,
); err != nil {
return 0, err
}
// Update the model
if chain == utils.ChainInternal {
m.NextInternalNum = uint32(newNum)
} else {
m.NextExternalNum = uint32(newNum)
}
if err = m.AfterUpdated(ctx); err != nil {
return 0, err
}
// return the previous number, which was next num
return uint32(newNum - 1), err
}
// ChildModels will get any related sub models
func (m *Xpub) ChildModels() (childModels []ModelInterface) {
for index := range m.destinations {
childModels = append(childModels, &m.destinations[index])
}
return
}
// BeforeCreating will fire before the model is being inserted into the Datastore
func (m *Xpub) BeforeCreating(_ context.Context) error {
m.DebugLog("starting: [" + m.name.String() + "] BeforeCreating hook...")
// Validate that the xPub key is correct
if _, err := utils.ValidateXPub(m.rawXpubKey); err != nil {
return err
}
// Make sure we have an ID
if len(m.ID) == 0 {
return ErrMissingFieldID
}
m.DebugLog("end: " + m.Name() + " BeforeCreating hook")
return nil
}
// AfterCreated will fire after the model is created in the Datastore
func (m *Xpub) AfterCreated(ctx context.Context) error {
m.DebugLog("starting: " + m.Name() + " AfterCreated hook...")
// todo: run these in go routines?
// Store in the cache
if err := saveToCache(
ctx, []string{fmt.Sprintf(cacheKeyXpubModel, m.GetID())}, m, 0,
); err != nil {
return err
}
m.DebugLog("end: " + m.Name() + " AfterCreated hook")
return nil
}
// AfterUpdated will fire after a successful update into the Datastore
func (m *Xpub) AfterUpdated(ctx context.Context) error {
m.DebugLog("starting: " + m.Name() + " AfterUpdated hook...")
// Store in the cache
if err := saveToCache(
ctx, []string{fmt.Sprintf(cacheKeyXpubModel, m.GetID())}, m, 0,
); err != nil {
return err
}
m.DebugLog("end: " + m.Name() + " AfterUpdated hook")
return nil
}
// Migrate model specific migration on startup
func (m *Xpub) Migrate(client datastore.ClientInterface) error {
return client.IndexMetadata(client.GetTableName(tableXPubs), metadataField)
}
// RemovePrivateData unset all fields that are sensitive
func (m *Xpub) RemovePrivateData() {
m.NextExternalNum = 0
m.NextInternalNum = 0
m.Metadata = nil
}