mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-03-22 15:19:11 +00:00
transaction note isar migration
This commit is contained in:
parent
e20d16436d
commit
335c2b9993
3 changed files with 70 additions and 10 deletions
|
@ -1,9 +1,11 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:hive_flutter/hive_flutter.dart';
|
import 'package:hive_flutter/hive_flutter.dart';
|
||||||
|
import 'package:isar/isar.dart';
|
||||||
import 'package:stackwallet/db/hive/db.dart';
|
import 'package:stackwallet/db/hive/db.dart';
|
||||||
import 'package:stackwallet/db/isar/main_db.dart';
|
import 'package:stackwallet/db/isar/main_db.dart';
|
||||||
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
||||||
|
import 'package:stackwallet/models/isar/models/transaction_note.dart';
|
||||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||||
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
||||||
import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
|
import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
|
||||||
|
@ -46,6 +48,7 @@ Future<void> migrateWalletsToIsar({
|
||||||
(await Hive.openBox<String>(DB.boxNameFavoriteWallets)).values.toList();
|
(await Hive.openBox<String>(DB.boxNameFavoriteWallets)).values.toList();
|
||||||
|
|
||||||
final List<WalletInfo> newInfo = [];
|
final List<WalletInfo> newInfo = [];
|
||||||
|
final List<TransactionNote> migratedNotes = [];
|
||||||
|
|
||||||
//
|
//
|
||||||
// Convert each old info into the new Isar WalletInfo
|
// Convert each old info into the new Isar WalletInfo
|
||||||
|
@ -53,6 +56,33 @@ Future<void> migrateWalletsToIsar({
|
||||||
for (final old in oldInfo) {
|
for (final old in oldInfo) {
|
||||||
final walletBox = await Hive.openBox<dynamic>(old.walletId);
|
final walletBox = await Hive.openBox<dynamic>(old.walletId);
|
||||||
|
|
||||||
|
//
|
||||||
|
// First handle transaction notes
|
||||||
|
//
|
||||||
|
final newNoteCount = await MainDB.instance.isar.transactionNotes
|
||||||
|
.where()
|
||||||
|
.walletIdEqualTo(old.walletId)
|
||||||
|
.count();
|
||||||
|
if (newNoteCount == 0) {
|
||||||
|
final map = walletBox.get('notes') as Map?;
|
||||||
|
|
||||||
|
if (map != null) {
|
||||||
|
final notes = Map<String, String>.from(map);
|
||||||
|
|
||||||
|
for (final txid in notes.keys) {
|
||||||
|
final note = notes[txid];
|
||||||
|
if (note != null && note.isNotEmpty) {
|
||||||
|
final newNote = TransactionNote(
|
||||||
|
walletId: old.walletId,
|
||||||
|
txid: txid,
|
||||||
|
value: note,
|
||||||
|
);
|
||||||
|
migratedNotes.add(newNote);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Set other data values
|
// Set other data values
|
||||||
//
|
//
|
||||||
|
@ -110,6 +140,12 @@ Future<void> migrateWalletsToIsar({
|
||||||
newInfo.add(info);
|
newInfo.add(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (migratedNotes.isNotEmpty) {
|
||||||
|
await MainDB.instance.isar.writeTxn(() async {
|
||||||
|
await MainDB.instance.isar.transactionNotes.putAll(migratedNotes);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await MainDB.instance.isar.writeTxn(() async {
|
await MainDB.instance.isar.writeTxn(() async {
|
||||||
await MainDB.instance.isar.walletInfo.putAll(newInfo);
|
await MainDB.instance.isar.walletInfo.putAll(newInfo);
|
||||||
});
|
});
|
||||||
|
|
|
@ -25,7 +25,11 @@ class TransactionNote {
|
||||||
@Index()
|
@Index()
|
||||||
late String walletId;
|
late String walletId;
|
||||||
|
|
||||||
@Index(unique: true, composite: [CompositeIndex("walletId")])
|
@Index(
|
||||||
|
unique: true,
|
||||||
|
replace: true,
|
||||||
|
composite: [CompositeIndex("walletId")],
|
||||||
|
)
|
||||||
late String txid;
|
late String txid;
|
||||||
|
|
||||||
late String value;
|
late String value;
|
||||||
|
|
|
@ -20,13 +20,13 @@ import 'package:stackwallet/db/isar/main_db.dart';
|
||||||
import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart';
|
import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart';
|
||||||
import 'package:stackwallet/models/exchange/response_objects/trade.dart';
|
import 'package:stackwallet/models/exchange/response_objects/trade.dart';
|
||||||
import 'package:stackwallet/models/isar/models/contact_entry.dart';
|
import 'package:stackwallet/models/isar/models/contact_entry.dart';
|
||||||
|
import 'package:stackwallet/models/isar/models/transaction_note.dart';
|
||||||
import 'package:stackwallet/models/node_model.dart';
|
import 'package:stackwallet/models/node_model.dart';
|
||||||
import 'package:stackwallet/models/stack_restoring_ui_state.dart';
|
import 'package:stackwallet/models/stack_restoring_ui_state.dart';
|
||||||
import 'package:stackwallet/models/trade_wallet_lookup.dart';
|
import 'package:stackwallet/models/trade_wallet_lookup.dart';
|
||||||
import 'package:stackwallet/models/wallet_restore_state.dart';
|
import 'package:stackwallet/models/wallet_restore_state.dart';
|
||||||
import 'package:stackwallet/services/address_book_service.dart';
|
import 'package:stackwallet/services/address_book_service.dart';
|
||||||
import 'package:stackwallet/services/node_service.dart';
|
import 'package:stackwallet/services/node_service.dart';
|
||||||
import 'package:stackwallet/services/notes_service.dart';
|
|
||||||
import 'package:stackwallet/services/trade_notes_service.dart';
|
import 'package:stackwallet/services/trade_notes_service.dart';
|
||||||
import 'package:stackwallet/services/trade_sent_from_stack_service.dart';
|
import 'package:stackwallet/services/trade_sent_from_stack_service.dart';
|
||||||
import 'package:stackwallet/services/trade_service.dart';
|
import 'package:stackwallet/services/trade_service.dart';
|
||||||
|
@ -310,8 +310,15 @@ abstract class SWB {
|
||||||
|
|
||||||
backupWallet['restoreHeight'] = wallet.info.restoreHeight;
|
backupWallet['restoreHeight'] = wallet.info.restoreHeight;
|
||||||
|
|
||||||
NotesService notesService = NotesService(walletId: wallet.walletId);
|
final isarNotes = await MainDB.instance.isar.transactionNotes
|
||||||
var notes = await notesService.notes;
|
.where()
|
||||||
|
.walletIdEqualTo(wallet.walletId)
|
||||||
|
.findAll();
|
||||||
|
|
||||||
|
final notes = isarNotes
|
||||||
|
.asMap()
|
||||||
|
.map((key, value) => MapEntry(value.txid, value.value));
|
||||||
|
|
||||||
backupWallet['notes'] = notes;
|
backupWallet['notes'] = notes;
|
||||||
|
|
||||||
backupWallets.add(backupWallet);
|
backupWallets.add(backupWallet);
|
||||||
|
@ -423,15 +430,28 @@ abstract class SWB {
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore notes
|
// restore notes
|
||||||
NotesService notesService = NotesService(walletId: info.walletId);
|
final notesMap =
|
||||||
final notes = walletbackup["notes"] as Map?;
|
Map<String, String>.from(walletbackup["notes"] as Map? ?? {});
|
||||||
if (notes != null) {
|
final List<TransactionNote> notes = [];
|
||||||
for (final note in notes.entries) {
|
|
||||||
await notesService.editOrAddNote(
|
for (final key in notesMap.keys) {
|
||||||
txid: note.key as String, note: note.value as String);
|
if (notesMap[key] != null && notesMap[key]!.isNotEmpty) {
|
||||||
|
notes.add(
|
||||||
|
TransactionNote(
|
||||||
|
walletId: info.walletId,
|
||||||
|
txid: key,
|
||||||
|
value: notesMap[key]!,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (notes.isNotEmpty) {
|
||||||
|
await MainDB.instance.isar.writeTxn(() async {
|
||||||
|
await MainDB.instance.isar.transactionNotes.putAll(notes);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (_shouldCancelRestore) {
|
if (_shouldCancelRestore) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue