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
/
client_datastore.go
162 lines (150 loc) · 3.99 KB
/
client_datastore.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
package bux
import (
"encoding/json"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
const (
conditionAnd = "$and"
)
// processCustomFields will process all custom fields
func processCustomFields(conditions *map[string]interface{}) {
// Process the xpub_output_value
_, ok := (*conditions)["xpub_output_value"]
if ok {
processXpubOutputValueConditions(conditions)
}
// Process the xpub_output_value
_, ok = (*conditions)["xpub_metadata"]
if ok {
processXpubMetadataConditions(conditions)
}
}
// processXpubOutputValueConditions will process xpub_output_value
func processXpubOutputValueConditions(conditions *map[string]interface{}) {
m, _ := json.Marshal((*conditions)["xpub_output_value"]) //nolint:errchkjson // this check might break the current code
var r map[string]interface{}
_ = json.Unmarshal(m, &r)
xPubOutputValue := make([]map[string]interface{}, 0)
for xPub, value := range r {
outputKey := "xpub_output_value." + xPub
xPubOutputValue = append(xPubOutputValue, map[string]interface{}{
outputKey: value,
})
}
if len(xPubOutputValue) > 0 {
_, ok := (*conditions)[conditionAnd]
if ok {
and := (*conditions)[conditionAnd].([]map[string]interface{})
and = append(and, xPubOutputValue...)
(*conditions)[conditionAnd] = and
} else {
(*conditions)[conditionAnd] = xPubOutputValue
}
}
delete(*conditions, "xpub_output_value")
}
// processXpubMetadataConditions will process xpub_metadata
func processXpubMetadataConditions(conditions *map[string]interface{}) {
// marshal / unmarshal into standard map[string]interface{}
m, _ := json.Marshal((*conditions)["xpub_metadata"]) //nolint:errchkjson // this check might break the current code
var r map[string]interface{}
_ = json.Unmarshal(m, &r)
for xPub, xr := range r {
xPubMetadata := make([]map[string]interface{}, 0)
for key, value := range xr.(map[string]interface{}) {
xPubMetadata = append(xPubMetadata, map[string]interface{}{
"xpub_metadata.x": xPub,
"xpub_metadata.k": key,
"xpub_metadata.v": value,
})
}
if len(xPubMetadata) > 0 {
_, ok := (*conditions)[conditionAnd]
if ok {
and := (*conditions)[conditionAnd].([]map[string]interface{})
and = append(and, xPubMetadata...)
(*conditions)[conditionAnd] = and
} else {
(*conditions)[conditionAnd] = xPubMetadata
}
}
}
delete(*conditions, "xpub_metadata")
}
// getMongoIndexes will get indexes from mongo
func getMongoIndexes() map[string][]mongo.IndexModel {
return map[string][]mongo.IndexModel{
"block_headers": {
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "height",
Value: bsonx.Int32(1),
}}},
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "synced",
Value: bsonx.Int32(1),
}}},
},
"destinations": {
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "address",
Value: bsonx.Int32(1),
}}},
},
"draft_transactions": {
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "status",
Value: bsonx.Int32(1),
}}},
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "xpub_id",
Value: bsonx.Int32(1),
}, {
Key: "status",
Value: bsonx.Int32(1),
}}},
},
"transactions": {
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "xpub_metadata.x",
Value: bsonx.Int32(1),
}, {
Key: "xpub_metadata.k",
Value: bsonx.Int32(1),
}, {
Key: "xpub_metadata.v",
Value: bsonx.Int32(1),
}}},
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "xpub_in_ids",
Value: bsonx.Int32(1),
}}},
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "xpub_out_ids",
Value: bsonx.Int32(1),
}}},
},
"utxos": {
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "transaction_id",
Value: bsonx.Int32(1),
}, {
Key: "output_index",
Value: bsonx.Int32(1),
}}},
mongo.IndexModel{Keys: bsonx.Doc{{
Key: "xpub_id",
Value: bsonx.Int32(1),
}, {
Key: "type",
Value: bsonx.Int32(1),
}, {
Key: "draft_id",
Value: bsonx.Int32(1),
}, {
Key: "spending_tx_id",
Value: bsonx.Int32(1),
}}},
},
}
}