This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
Migration013.go
301 lines (279 loc) · 9.95 KB
/
Migration013.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
package migrations
import (
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"time"
zaddr "github.com/OpenBazaar/multiwallet/zcash/address"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
"github.com/cpacia/bchutil"
)
type Migration013 struct{}
type Migration013_TransactionRecord_beforeMigration struct {
Txid string
Index uint32
Value int64
ScriptPubKey string
Spent bool
Timestamp time.Time
}
type Migration013_TransactionRecord_afterMigration struct {
Txid string
Index uint32
Value int64
Address string
Spent bool
Timestamp time.Time
}
type migration013_record struct {
orderID string
coin string
unmigratedTransactions []Migration013_TransactionRecord_beforeMigration
migratedTransactions []Migration013_TransactionRecord_afterMigration
}
func (Migration013) Up(repoPath string, dbPassword string, testnet bool) (err error) {
db, err := OpenDB(repoPath, dbPassword, testnet)
if err != nil {
return fmt.Errorf("opening db: %s", err.Error())
}
saleMigrationRecords, err := migration013_extractRecords(db, "select orderID, transactions, paymentCoin from sales;", false)
if err != nil {
return fmt.Errorf("get sales rows: %s", err.Error())
}
purchaseMigrationRecords, err := migration013_extractRecords(db, "select orderID, transactions, paymentCoin from purchases;", false)
if err != nil {
return fmt.Errorf("get purchase rows: %s", err.Error())
}
if err := withTransaction(db, func(tx *sql.Tx) error {
err := migration013_updateRecords(tx, saleMigrationRecords, "update sales set transactions = ? where orderID = ?", testnet, false)
if err != nil {
return fmt.Errorf("update sales: %s", err.Error())
}
err = migration013_updateRecords(tx, purchaseMigrationRecords, "update purchases set transactions = ? where orderID = ?", testnet, false)
if err != nil {
return fmt.Errorf("update purchases: %s", err.Error())
}
return nil
}); err != nil {
return fmt.Errorf("migrating up: %s", err.Error())
}
return writeRepoVer(repoPath, 14)
}
func (Migration013) Down(repoPath string, dbPassword string, testnet bool) (err error) {
db, err := OpenDB(repoPath, dbPassword, testnet)
if err != nil {
return fmt.Errorf("opening db: %s", err.Error())
}
saleMigrationRecords, err := migration013_extractRecords(db, "select orderID, transactions, paymentCoin from sales;", true)
if err != nil {
return fmt.Errorf("get sales rows: %s", err.Error())
}
purchaseMigrationRecords, err := migration013_extractRecords(db, "select orderID, transactions, paymentCoin from purchases;", true)
if err != nil {
return fmt.Errorf("get purchase rows: %s", err.Error())
}
if err := withTransaction(db, func(tx *sql.Tx) error {
err := migration013_updateRecords(tx, saleMigrationRecords, "update sales set transactions = ? where orderID = ?", testnet, true)
if err != nil {
return fmt.Errorf("update sales: %s", err.Error())
}
err = migration013_updateRecords(tx, purchaseMigrationRecords, "update purchases set transactions = ? where orderID = ?", testnet, true)
if err != nil {
return fmt.Errorf("update purchases: %s", err.Error())
}
return nil
}); err != nil {
return fmt.Errorf("migrating down: %s", err.Error())
}
return writeRepoVer(repoPath, 13)
}
func migration013_extractRecords(db *sql.DB, query string, migrateDown bool) ([]migration013_record, error) {
var (
results = make([]migration013_record, 0)
rows, err = db.Query(query)
)
if err != nil {
return nil, fmt.Errorf("selecting rows: %s", err.Error())
}
defer rows.Close()
for rows.Next() {
var (
serializedTransactions sql.NullString
r = migration013_record{}
)
if err := rows.Scan(&r.orderID, &serializedTransactions, &r.coin); err != nil {
return nil, fmt.Errorf("scanning rows: %s", err.Error())
}
if !serializedTransactions.Valid {
continue
}
if migrateDown {
if err := json.Unmarshal([]byte(serializedTransactions.String), &r.migratedTransactions); err != nil {
return nil, fmt.Errorf("unmarshal migrated transactions: %s", err.Error())
}
} else {
if err := json.Unmarshal([]byte(serializedTransactions.String), &r.unmigratedTransactions); err != nil {
return nil, fmt.Errorf("unmarshal unmigrated transactions: %s", err.Error())
}
}
results = append(results, r)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating rows: %s", err.Error())
}
return results, nil
}
func migration013_updateRecords(tx *sql.Tx, records []migration013_record, query string, testMode bool, migrateDown bool) error {
var update, err = tx.Prepare(query)
if err != nil {
return fmt.Errorf("prepare update statement: %s", err.Error())
}
defer update.Close()
for _, beforeRecord := range records {
if migrateDown {
var migratedTransactionRecords = make([]Migration013_TransactionRecord_beforeMigration, 0)
for _, beforeTx := range beforeRecord.migratedTransactions {
script, err := Migration013_AddressToScript(beforeRecord.coin, beforeTx.Address, testMode)
if err != nil {
return fmt.Errorf("address to script: %s", err.Error())
}
var migratedRecord = Migration013_TransactionRecord_beforeMigration{
Txid: beforeTx.Txid,
Index: beforeTx.Index,
Value: beforeTx.Value,
Spent: beforeTx.Spent,
Timestamp: beforeTx.Timestamp,
ScriptPubKey: hex.EncodeToString(script),
}
migratedTransactionRecords = append(migratedTransactionRecords, migratedRecord)
}
serializedTransactionRecords, err := json.Marshal(migratedTransactionRecords)
if err != nil {
return fmt.Errorf("marhsal transactions: %s", err.Error())
}
if _, err := update.Exec(string(serializedTransactionRecords), beforeRecord.orderID); err != nil {
return fmt.Errorf("updating record: %s", err.Error())
}
} else {
var migratedTransactionRecords = make([]Migration013_TransactionRecord_afterMigration, 0)
for _, beforeTx := range beforeRecord.unmigratedTransactions {
script, err := hex.DecodeString(beforeTx.ScriptPubKey)
if err != nil {
return fmt.Errorf("decode script: %s", err.Error())
}
addr, err := Migration013_ScriptToAddress(beforeRecord.coin, script, testMode)
if err != nil {
return fmt.Errorf("script to address: %s", err.Error())
}
var migratedRecord = Migration013_TransactionRecord_afterMigration{
Txid: beforeTx.Txid,
Index: beforeTx.Index,
Value: beforeTx.Value,
Spent: beforeTx.Spent,
Timestamp: beforeTx.Timestamp,
Address: addr,
}
migratedTransactionRecords = append(migratedTransactionRecords, migratedRecord)
}
serializedTransactionRecords, err := json.Marshal(migratedTransactionRecords)
if err != nil {
return fmt.Errorf("marhsal transactions: %s", err.Error())
}
if _, err := update.Exec(string(serializedTransactionRecords), beforeRecord.orderID); err != nil {
return fmt.Errorf("updating record: %s", err.Error())
}
}
}
return nil
}
func Migration013_ChainConfigParams(testnet bool) *chaincfg.Params {
if testnet {
return &chaincfg.TestNet3Params
}
return &chaincfg.MainNetParams
}
func Migration013_ScriptToAddress(coinType string, script []byte, testmodeEnanabled bool) (string, error) {
var params = Migration013_ChainConfigParams(testmodeEnanabled)
switch strings.ToLower(coinType) {
case "btc", "tbtc":
_, addrs, _, err := txscript.ExtractPkScriptAddrs(script, params)
if err != nil {
return "", fmt.Errorf("converting %s script to address: %s", coinType, err.Error())
}
if len(addrs) == 0 {
return "", fmt.Errorf("unable to convert %s address to script", coinType)
}
return addrs[0].String(), nil
case "bch", "tbch":
addr, err := bchutil.ExtractPkScriptAddrs(script, params)
if err != nil {
return "", fmt.Errorf("converting %s script to address: %s", coinType, err.Error())
}
return addr.String(), nil
case "zec", "tzec":
addr, err := zaddr.ExtractPkScriptAddrs(script, params)
if err != nil {
return "", fmt.Errorf("converting %s script to address: %s", coinType, err.Error())
}
return addr.String(), nil
}
return "", fmt.Errorf("unable to migrate coinType %s", coinType)
}
func migration013_DecodeBCHAddress(addr string, params *chaincfg.Params) (*btcutil.Address, error) {
// Legacy
decoded, err := btcutil.DecodeAddress(addr, params)
if err == nil {
return &decoded, nil
}
// Cashaddr
decoded, err = bchutil.DecodeAddress(addr, params)
if err == nil {
return &decoded, nil
}
// Bitpay
decoded, err = bchutil.DecodeBitpay(addr, params)
if err == nil {
return &decoded, nil
}
return nil, fmt.Errorf("unable to decode BCH address")
}
func Migration013_AddressToScript(coinType string, addr string, testmodeEnanabled bool) ([]byte, error) {
var params = Migration013_ChainConfigParams(testmodeEnanabled)
switch strings.ToLower(coinType) {
case "btc", "tbtc":
addr, err := btcutil.DecodeAddress(addr, params)
if err != nil {
return nil, fmt.Errorf("decoding %s address: %s", coinType, err.Error())
}
script, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, fmt.Errorf("converting %s address to script: %s", coinType, err.Error())
}
return script, nil
case "bch", "tbch":
addr, err := migration013_DecodeBCHAddress(addr, params)
if err != nil {
return nil, fmt.Errorf("decoding %s address: %s", coinType, err.Error())
}
script, err := bchutil.PayToAddrScript(*addr)
if err != nil {
return nil, fmt.Errorf("converting %s address to script: %s", coinType, err.Error())
}
return script, nil
case "zec", "tzec":
addr, err := zaddr.DecodeAddress(addr, params)
if err != nil {
return nil, fmt.Errorf("decoding %s address: %s", coinType, err.Error())
}
script, err := zaddr.PayToAddrScript(addr)
if err != nil {
return nil, fmt.Errorf("converting %s address to script: %s", coinType, err.Error())
}
return script, nil
}
return nil, fmt.Errorf("unable to migrate coinType %s", coinType)
}