-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathmain_db.dart
More file actions
655 lines (577 loc) · 19.3 KB
/
main_db.dart
File metadata and controls
655 lines (577 loc) · 19.3 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'package:decimal/decimal.dart';
import 'package:isar/isar.dart';
import 'package:tuple/tuple.dart';
import '../../exceptions/main_db/main_db_exception.dart';
import '../../models/isar/models/block_explorer.dart';
import '../../models/isar/models/blockchain_data/v2/transaction_v2.dart';
import '../../models/isar/models/contact_entry.dart';
import '../../models/isar/models/isar_models.dart';
import '../../models/isar/ordinal.dart';
import '../../models/isar/stack_theme.dart';
import '../../models/isar/models/silent_payments/silent_payment_config.dart';
import '../../models/isar/models/silent_payments/silent_payment_metadata.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/extensions/extensions.dart';
import '../../utilities/stack_file_system.dart';
import '../../wallets/crypto_currency/crypto_currency.dart';
import '../../wallets/isar/models/frost_wallet_info.dart';
import '../../wallets/isar/models/spark_coin.dart';
import '../../wallets/isar/models/token_wallet_info.dart';
import '../../wallets/isar/models/wallet_info.dart';
import '../../wallets/isar/models/wallet_info_meta.dart';
part '../queries/queries.dart';
/*
* This file includes the functions that are used to interact with the main database.
* To add a new function, add it in the class MainDB.
*/
class MainDB {
MainDB._();
static MainDB? _instance;
static MainDB get instance => _instance ??= MainDB._();
Isar? _isar;
Isar get isar => _isar!;
Future<bool> initMainDB({Isar? mock}) async {
if (mock != null) {
_isar = mock;
return true;
}
if (_isar != null && isar.isOpen) return false;
_isar = await Isar.open(
[
TransactionSchema,
TransactionNoteSchema,
UTXOSchema,
AddressSchema,
AddressLabelSchema,
EthContractSchema,
TransactionBlockExplorerSchema,
StackThemeSchema,
ContactEntrySchema,
OrdinalSchema,
LelantusCoinSchema,
WalletInfoSchema,
TransactionV2Schema,
SparkCoinSchema,
WalletInfoMetaSchema,
TokenWalletInfoSchema,
FrostWalletInfoSchema,
SilentPaymentConfigSchema,
SilentPaymentMetadataSchema,
],
directory: (await StackFileSystem.applicationIsarDirectory()).path,
// inspector: kDebugMode,
inspector: false,
name: "wallet_data",
maxSizeMiB: 512,
);
return true;
}
Future<void> putWalletInfo(WalletInfo walletInfo) async {
try {
await isar.writeTxn(() async {
await isar.walletInfo.put(walletInfo);
});
} catch (e) {
throw MainDBException("failed putWalletInfo()", e);
}
}
Future<void> updateWalletInfo(WalletInfo walletInfo) async {
try {
await isar.writeTxn(() async {
final info = await isar.walletInfo
.where()
.walletIdEqualTo(walletInfo.walletId)
.findFirst();
if (info == null) {
throw Exception("updateWalletInfo() called with new WalletInfo."
" Use putWalletInfo()");
}
await isar.walletInfo.deleteByWalletId(walletInfo.walletId);
await isar.walletInfo.put(walletInfo);
});
} catch (e) {
throw MainDBException("failed updateWalletInfo()", e);
}
}
// contact entries
List<ContactEntry> getContactEntries() {
return isar.contactEntrys.where().sortByName().findAllSync();
}
Future<bool> deleteContactEntry({required String id}) {
try {
return isar.writeTxn(() async {
await isar.contactEntrys.deleteByCustomId(id);
return true;
});
} catch (e) {
throw MainDBException("failed deleteContactEntry: $id", e);
}
}
Future<bool> isContactEntryExists({required String id}) async {
return isar.contactEntrys
.where()
.customIdEqualTo(id)
.count()
.then((value) => value > 0);
}
ContactEntry? getContactEntry({required String id}) {
return isar.contactEntrys.where().customIdEqualTo(id).findFirstSync();
}
Future<bool> putContactEntry({required ContactEntry contactEntry}) async {
try {
return await isar.writeTxn(() async {
await isar.contactEntrys.put(contactEntry);
return true;
});
} catch (e) {
throw MainDBException("failed putContactEntry: $contactEntry", e);
}
}
// tx block explorers
TransactionBlockExplorer? getTransactionBlockExplorer({
required CryptoCurrency cryptoCurrency,
}) {
return isar.transactionBlockExplorers
.where()
.tickerEqualTo(cryptoCurrency.ticker)
.findFirstSync();
}
Future<int> putTransactionBlockExplorer(
TransactionBlockExplorer explorer,
) async {
try {
return await isar.writeTxn(() async {
return await isar.transactionBlockExplorers.put(explorer);
});
} catch (e) {
throw MainDBException("failed putTransactionBlockExplorer: $explorer", e);
}
}
// addresses
QueryBuilder<Address, Address, QAfterWhereClause> getAddresses(
String walletId,
) =>
isar.addresses.where().walletIdEqualTo(walletId);
Future<int> putAddress(Address address) async {
try {
return await isar.writeTxn(() async {
return await isar.addresses.put(address);
});
} catch (e) {
throw MainDBException("failed putAddress: $address", e);
}
}
Future<List<int>> putAddresses(List<Address> addresses) async {
try {
return await isar.writeTxn(() async {
return await isar.addresses.putAll(addresses);
});
} catch (e) {
throw MainDBException("failed putAddresses: $addresses", e);
}
}
Future<List<int>> updateOrPutAddresses(List<Address> addresses) async {
try {
final List<int> ids = [];
await isar.writeTxn(() async {
for (final address in addresses) {
final storedAddress = await isar.addresses
.getByValueWalletId(address.value, address.walletId);
int id;
if (storedAddress == null) {
id = await isar.addresses.put(address);
} else {
address.id = storedAddress.id;
await storedAddress.transactions.load();
final txns = storedAddress.transactions.toList();
await isar.addresses.delete(storedAddress.id);
id = await isar.addresses.put(address);
address.transactions.addAll(txns);
await address.transactions.save();
}
ids.add(id);
}
});
return ids;
} catch (e) {
throw MainDBException("failed updateOrPutAddresses: $addresses", e);
}
}
Future<Address?> getAddress(String walletId, String address) async {
return isar.addresses.getByValueWalletId(address, walletId);
}
Future<int> updateAddress(Address oldAddress, Address newAddress) async {
try {
return await isar.writeTxn(() async {
newAddress.id = oldAddress.id;
await oldAddress.transactions.load();
final txns = oldAddress.transactions.toList();
await isar.addresses.delete(oldAddress.id);
final id = await isar.addresses.put(newAddress);
newAddress.transactions.addAll(txns);
await newAddress.transactions.save();
return id;
});
} catch (e) {
throw MainDBException(
"failed updateAddress: from=$oldAddress to=$newAddress",
e,
);
}
}
// transactions
QueryBuilder<Transaction, Transaction, QAfterWhereClause> getTransactions(
String walletId,
) =>
isar.transactions.where().walletIdEqualTo(walletId);
Future<int> putTransaction(Transaction transaction) async {
try {
return await isar.writeTxn(() async {
return await isar.transactions.put(transaction);
});
} catch (e) {
throw MainDBException("failed putTransaction: $transaction", e);
}
}
Future<List<int>> putTransactions(List<Transaction> transactions) async {
try {
return await isar.writeTxn(() async {
return await isar.transactions.putAll(transactions);
});
} catch (e) {
throw MainDBException("failed putTransactions: $transactions", e);
}
}
Future<Transaction?> getTransaction(String walletId, String txid) async {
return isar.transactions.getByTxidWalletId(txid, walletId);
}
Stream<Transaction?> watchTransaction({
required Id id,
bool fireImmediately = false,
}) {
return isar.transactions.watchObject(id, fireImmediately: fireImmediately);
}
// utxos
QueryBuilder<UTXO, UTXO, QAfterWhereClause> getUTXOs(String walletId) =>
isar.utxos.where().walletIdEqualTo(walletId);
QueryBuilder<UTXO, UTXO, QAfterFilterCondition> getUTXOsByAddress(
String walletId,
String address,
) =>
isar.utxos
.where()
.walletIdEqualTo(walletId)
.filter()
.addressEqualTo(address);
Future<void> putUTXO(UTXO utxo) => isar.writeTxn(() async {
await isar.utxos.put(utxo);
});
Future<void> putUTXOs(List<UTXO> utxos) => isar.writeTxn(() async {
await isar.utxos.putAll(utxos);
});
Future<bool> updateUTXOs(String walletId, List<UTXO> utxos) async {
bool newUTXO = false;
await isar.writeTxn(() async {
final set = utxos.toSet();
for (final utxo in utxos) {
// check if utxo exists in db and update accordingly
final storedUtxo = await isar.utxos
.where()
.txidWalletIdVoutEqualTo(utxo.txid, utxo.walletId, utxo.vout)
.findFirst();
if (storedUtxo != null) {
// update
set.remove(utxo);
set.add(
storedUtxo.copyWith(
value: utxo.value,
address: utxo.address,
blockTime: utxo.blockTime,
blockHeight: utxo.blockHeight,
blockHash: utxo.blockHash,
),
);
} else {
newUTXO = true;
}
}
await isar.utxos.where().walletIdEqualTo(walletId).deleteAll();
await isar.utxos.putAll(set.toList());
});
return newUTXO;
}
Stream<UTXO?> watchUTXO({
required Id id,
bool fireImmediately = false,
}) {
return isar.utxos.watchObject(id, fireImmediately: fireImmediately);
}
// transaction notes
QueryBuilder<TransactionNote, TransactionNote, QAfterWhereClause>
getTransactionNotes(String walletId) =>
isar.transactionNotes.where().walletIdEqualTo(walletId);
Future<void> putTransactionNote(TransactionNote transactionNote) =>
isar.writeTxn(() async {
await isar.transactionNotes.put(transactionNote);
});
Future<void> putTransactionNotes(List<TransactionNote> transactionNotes) =>
isar.writeTxn(() async {
await isar.transactionNotes.putAll(transactionNotes);
});
Future<TransactionNote?> getTransactionNote(
String walletId,
String txid,
) async {
return isar.transactionNotes.getByTxidWalletId(
txid,
walletId,
);
}
Stream<TransactionNote?> watchTransactionNote({
required Id id,
bool fireImmediately = false,
}) {
return isar.transactionNotes
.watchObject(id, fireImmediately: fireImmediately);
}
// address labels
QueryBuilder<AddressLabel, AddressLabel, QAfterWhereClause> getAddressLabels(
String walletId,
) =>
isar.addressLabels.where().walletIdEqualTo(walletId);
Future<int> putAddressLabel(AddressLabel addressLabel) =>
isar.writeTxn(() async {
return await isar.addressLabels.put(addressLabel);
});
int putAddressLabelSync(AddressLabel addressLabel) => isar.writeTxnSync(() {
return isar.addressLabels.putSync(addressLabel);
});
Future<void> putAddressLabels(List<AddressLabel> addressLabels) =>
isar.writeTxn(() async {
await isar.addressLabels.putAll(addressLabels);
});
Future<AddressLabel?> getAddressLabel(
String walletId,
String addressString,
) async {
return isar.addressLabels.getByAddressStringWalletId(
addressString,
walletId,
);
}
AddressLabel? getAddressLabelSync(String walletId, String addressString) {
return isar.addressLabels.getByAddressStringWalletIdSync(
addressString,
walletId,
);
}
Stream<AddressLabel?> watchAddressLabel({
required Id id,
bool fireImmediately = false,
}) {
return isar.addressLabels.watchObject(id, fireImmediately: fireImmediately);
}
Future<int> updateAddressLabel(AddressLabel addressLabel) async {
try {
return await isar.writeTxn(() async {
return await isar.addressLabels.put(addressLabel);
});
} catch (e) {
throw MainDBException("failed updateAddressLabel", e);
}
}
//
Future<void> deleteWalletBlockchainData(String walletId) async {
final transactionCount = await getTransactions(walletId).count();
final transactionCountV2 =
await isar.transactionV2s.where().walletIdEqualTo(walletId).count();
final addressCount = await getAddresses(walletId).count();
final utxoCount = await getUTXOs(walletId).count();
// final lelantusCoinCount =
// await isar.lelantusCoins.where().walletIdEqualTo(walletId).count();
await isar.writeTxn(() async {
const paginateLimit = 50;
// transactions
for (int i = 0; i < transactionCount; i += paginateLimit) {
final txnIds = await getTransactions(walletId)
.offset(i)
.limit(paginateLimit)
.idProperty()
.findAll();
await isar.transactions.deleteAll(txnIds);
}
// transactions V2
for (int i = 0; i < transactionCountV2; i += paginateLimit) {
final txnIds = await isar.transactionV2s
.where()
.walletIdEqualTo(walletId)
.offset(i)
.limit(paginateLimit)
.idProperty()
.findAll();
await isar.transactionV2s.deleteAll(txnIds);
}
// addresses
for (int i = 0; i < addressCount; i += paginateLimit) {
final addressIds = await getAddresses(walletId)
.offset(i)
.limit(paginateLimit)
.idProperty()
.findAll();
await isar.addresses.deleteAll(addressIds);
}
// utxos
for (int i = 0; i < utxoCount; i += paginateLimit) {
final utxoIds = await getUTXOs(walletId)
.offset(i)
.limit(paginateLimit)
.idProperty()
.findAll();
await isar.utxos.deleteAll(utxoIds);
}
// lelantusCoins
await isar.lelantusCoins.where().walletIdEqualTo(walletId).deleteAll();
// for (int i = 0; i < lelantusCoinCount; i += paginateLimit) {
// final lelantusCoinIds = await isar.lelantusCoins
// .where()
// .walletIdEqualTo(walletId)
// .offset(i)
// .limit(paginateLimit)
// .idProperty()
// .findAll();
// await isar.lelantusCoins.deleteAll(lelantusCoinIds);
// }
// spark coins
await isar.sparkCoins
.where()
.walletIdEqualToAnyLTagHash(walletId)
.deleteAll();
});
}
Future<void> deleteAddressLabels(String walletId) async {
final addressLabelCount = await getAddressLabels(walletId).count();
await isar.writeTxn(() async {
const paginateLimit = 50;
for (int i = 0; i < addressLabelCount; i += paginateLimit) {
final labelIds = await getAddressLabels(walletId)
.offset(i)
.limit(paginateLimit)
.idProperty()
.findAll();
await isar.addressLabels.deleteAll(labelIds);
}
});
}
Future<void> deleteTransactionNotes(String walletId) async {
final noteCount = await getTransactionNotes(walletId).count();
await isar.writeTxn(() async {
const paginateLimit = 50;
for (int i = 0; i < noteCount; i += paginateLimit) {
final labelIds = await getTransactionNotes(walletId)
.offset(i)
.limit(paginateLimit)
.idProperty()
.findAll();
await isar.transactionNotes.deleteAll(labelIds);
}
});
}
Future<void> addNewTransactionData(
List<Tuple2<Transaction, Address?>> transactionsData,
String walletId,
) async {
try {
await isar.writeTxn(() async {
for (final data in transactionsData) {
final tx = data.item1;
final potentiallyUnconfirmedTx = await getTransaction(
walletId,
tx.txid,
);
if (potentiallyUnconfirmedTx != null) {
// update use id to replace tx
tx.id = potentiallyUnconfirmedTx.id;
await isar.transactions.delete(potentiallyUnconfirmedTx.id);
}
// save transaction
await isar.transactions.put(tx);
if (data.item2 != null) {
final address = await getAddress(walletId, data.item2!.value);
// check if address exists in db and add if it does not
if (address == null) {
await isar.addresses.put(data.item2!);
}
// link and save address
tx.address.value = address ?? data.item2!;
await tx.address.save();
}
}
});
} catch (e) {
throw MainDBException("failed addNewTransactionData", e);
}
}
Future<List<int>> updateOrPutTransactionV2s(
List<TransactionV2> transactions,
) async {
try {
final List<int> ids = [];
await isar.writeTxn(() async {
for (final tx in transactions) {
final storedTx = await isar.transactionV2s
.where()
.txidWalletIdEqualTo(tx.txid, tx.walletId)
.findFirst();
Id id;
if (storedTx == null) {
id = await isar.transactionV2s.put(tx);
} else {
tx.id = storedTx.id;
await isar.transactionV2s.delete(storedTx.id);
id = await isar.transactionV2s.put(tx);
}
ids.add(id);
}
});
return ids;
} catch (e) {
throw MainDBException(
"failed updateOrPutTransactionV2s: $transactions",
e,
);
}
}
// ========== Ethereum =======================================================
// eth contracts
QueryBuilder<EthContract, EthContract, QWhere> getEthContracts() =>
isar.ethContracts.where();
Future<EthContract?> getEthContract(String contractAddress) =>
isar.ethContracts.where().addressEqualTo(contractAddress).findFirst();
EthContract? getEthContractSync(String contractAddress) =>
isar.ethContracts.where().addressEqualTo(contractAddress).findFirstSync();
Future<int> putEthContract(EthContract contract) => isar.writeTxn(() async {
return await isar.ethContracts.put(contract);
});
Future<void> putEthContracts(List<EthContract> contracts) =>
isar.writeTxn(() async {
await isar.ethContracts.putAll(contracts);
});
// ========== Lelantus =======================================================
Future<int?> getHighestUsedMintIndex({required String walletId}) async {
return await isar.lelantusCoins
.where()
.walletIdEqualTo(walletId)
.sortByMintIndexDesc()
.mintIndexProperty()
.findFirst();
}
}