From 2e678437f56140593fa8213351e0d48f67afb017 Mon Sep 17 00:00:00 2001 From: mocodesmo Date: Tue, 28 May 2024 21:31:46 +0530 Subject: [PATCH] storage - migration cleanup --- lib/_pkg/storage/migration.dart | 75 ++++--- lib/_pkg/storage/migration0_1to0_2.dart | 252 ++++++++++++------------ 2 files changed, 161 insertions(+), 166 deletions(-) diff --git a/lib/_pkg/storage/migration.dart b/lib/_pkg/storage/migration.dart index 1e0a5b70..00ed912e 100644 --- a/lib/_pkg/storage/migration.dart +++ b/lib/_pkg/storage/migration.dart @@ -1,46 +1,43 @@ -import 'package:bb_mobile/_model/network.dart'; import 'package:bb_mobile/_pkg/storage/hive.dart'; import 'package:bb_mobile/_pkg/storage/migration0_1to0_2.dart'; import 'package:bb_mobile/_pkg/storage/secure_storage.dart'; -import 'package:bdk_flutter/bdk_flutter.dart' as bdk; -import 'package:flutter_bloc/flutter_bloc.dart'; -class MigrationCubit extends Cubit { - MigrationCubit() : super(false); - void toggle(bool value) => emit(value); -} +// class MigrationCubit extends Cubit { +// MigrationCubit() : super(false); +// void toggle(bool value) => emit(value); +// } -final migrationCubit = MigrationCubit(); +// final migrationCubit = MigrationCubit(); -bdk.Blockchain? mainBlockchain; -bdk.Blockchain? testBlockchain; +// bdk.Blockchain? mainBlockchain; +// bdk.Blockchain? testBlockchain; Future prepareMigration() async { - const ElectrumNetwork electrumNetwork = ElectrumNetwork.bullbitcoin(); + // const ElectrumNetwork electrumNetwork = ElectrumNetwork.bullbitcoin(); - mainBlockchain = await bdk.Blockchain.create( - config: bdk.BlockchainConfig.electrum( - config: bdk.ElectrumConfig( - url: electrumNetwork.mainnet, - retry: electrumNetwork.retry, - timeout: electrumNetwork.timeout, - stopGap: electrumNetwork.stopGap, - validateDomain: electrumNetwork.validateDomain, - ), - ), - ); + // mainBlockchain = await bdk.Blockchain.create( + // config: bdk.BlockchainConfig.electrum( + // config: bdk.ElectrumConfig( + // url: electrumNetwork.mainnet, + // retry: electrumNetwork.retry, + // timeout: electrumNetwork.timeout, + // stopGap: electrumNetwork.stopGap, + // validateDomain: electrumNetwork.validateDomain, + // ), + // ), + // ); - testBlockchain = await bdk.Blockchain.create( - config: bdk.BlockchainConfig.electrum( - config: bdk.ElectrumConfig( - url: electrumNetwork.testnet, - retry: electrumNetwork.retry, - timeout: electrumNetwork.timeout, - stopGap: electrumNetwork.stopGap, - validateDomain: electrumNetwork.validateDomain, - ), - ), - ); + // testBlockchain = await bdk.Blockchain.create( + // config: bdk.BlockchainConfig.electrum( + // config: bdk.ElectrumConfig( + // url: electrumNetwork.testnet, + // retry: electrumNetwork.retry, + // timeout: electrumNetwork.timeout, + // stopGap: electrumNetwork.stopGap, + // validateDomain: electrumNetwork.validateDomain, + // ), + // ), + // ); } Future doMigration( @@ -62,16 +59,16 @@ Future doMigration( await doMigration0_1to0_2( secureStorage, hiveStorage, - mainBlockchain!, - testBlockchain!, + // mainBlockchain!, + // testBlockchain!, ); } else if (toVersion.startsWith('0.3')) { if (fromVersion.startsWith('0.1')) { await doMigration0_1to0_2( secureStorage, hiveStorage, - mainBlockchain!, - testBlockchain!, + // mainBlockchain!, + // testBlockchain!, ); await doMigration02to03(secureStorage, hiveStorage); } else if (fromVersion.startsWith('0.2')) { @@ -82,8 +79,8 @@ Future doMigration( await doMigration0_1to0_2( secureStorage, hiveStorage, - mainBlockchain!, - testBlockchain!, + // mainBlockchain!, + // testBlockchain!, ); await doMigration02to03(secureStorage, hiveStorage); await doMigration03to04(secureStorage, hiveStorage); diff --git a/lib/_pkg/storage/migration0_1to0_2.dart b/lib/_pkg/storage/migration0_1to0_2.dart index 42224ecc..296dc11d 100644 --- a/lib/_pkg/storage/migration0_1to0_2.dart +++ b/lib/_pkg/storage/migration0_1to0_2.dart @@ -5,7 +5,6 @@ // Change 5: create a new Liquid wallet, based on the Bitcoin wallet import 'dart:convert'; -import 'package:bb_mobile/_model/address.dart'; import 'package:bb_mobile/_model/seed.dart'; import 'package:bb_mobile/_model/wallet.dart'; import 'package:bb_mobile/_pkg/storage/hive.dart'; @@ -19,7 +18,6 @@ import 'package:bb_mobile/_pkg/wallet/lwk/sensitive_create.dart'; import 'package:bb_mobile/_pkg/wallet/repository/sensitive_storage.dart'; import 'package:bb_mobile/_pkg/wallet/repository/storage.dart'; import 'package:bb_mobile/_pkg/wallet/repository/wallets.dart'; -import 'package:bdk_flutter/bdk_flutter.dart' as bdk; int mainWalletIndex = 0; int testWalletIndex = 0; @@ -27,8 +25,8 @@ int testWalletIndex = 0; Future doMigration0_1to0_2( SecureStorage secureStorage, HiveStorage hiveStorage, - bdk.Blockchain mainBlockchain, - bdk.Blockchain testBlockchain, + // bdk.Blockchain mainBlockchain, + // bdk.Blockchain testBlockchain, ) async { print('Migration: 0.1 to 0.2'); @@ -213,129 +211,129 @@ Future< // Skipping some addresses. This funciton will fix that // TODO: Change address list count or max change address index. // --> Doing max change address will be complex. So need to finalize if its necessary. -Future updateAddressNullIssue( - Map walletObj, - bdk.Blockchain mainBlockchain, - bdk.Blockchain testBlockchain, -) async { - final Wallet w = Wallet.fromJson(walletObj); - final WalletsRepository walletRepo = WalletsRepository(); - final bdkCreate = BDKCreate(walletsRepository: walletRepo); - final (bdkWallet, _) = await bdkCreate.loadPublicBdkWallet(w); - - print('syncing wallet ${w.id}. Pls wait...'); - final network = await bdkWallet!.network(); - await bdkWallet.sync( - blockchain: - network == bdk.Network.bitcoin ? mainBlockchain : testBlockchain, - ); - print('sync done'); - - final myAddressBook = [...w.myAddressBook].toList(); - - final bdk.AddressInfo lastDepositAddr = await bdkWallet.getAddress( - addressIndex: const bdk.AddressIndex.lastUnused(), - ); - final int depositAddressCount = lastDepositAddr.index; - - final bdk.AddressInfo lastChangeAddr = await bdkWallet.getInternalAddress( - addressIndex: const bdk.AddressIndex.lastUnused(), - ); - final int changeAddressCount = lastChangeAddr.index; - //int ivar = 0; - //w.myAddressBook.map((addr) { - // if (addr.kind == AddressKind.change) { - // changeAddressCount++; - // } else if (addr.kind == AddressKind.deposit) { - // depositAddressCount++; - // } - // print( - // 'myAddressbook[$ivar] : ${addr.index} ${addr.kind} : (${addr.address})', - // ); - // ivar++; - // return addr; - //}).toList(); - - final List
toAdd = []; - for (int i = 0; i < depositAddressCount; i++) { - bdk.AddressInfo nativeAddr; - String nativeAddrStr; - - nativeAddr = await bdkWallet.getAddress( - addressIndex: bdk.AddressIndex.peek(index: i), - ); - nativeAddrStr = await nativeAddr.address.asString(); - - final matchIndex = - myAddressBook.indexWhere((a) => a.address == nativeAddrStr); - // print('matchIndex $matchIndex $i $nativeAddrStr'); - if (matchIndex != -1) { - // print( - // 'myAddressbook.deposit index $i : ${nativeAddr.index} (${myAddressBook[matchIndex].address}, $nativeAddrStr)', - // ); - final newAddr = - myAddressBook[matchIndex].copyWith(index: nativeAddr.index); - myAddressBook[matchIndex] = newAddr; - } else { - toAdd.add( - Address( - address: nativeAddrStr, - kind: AddressKind.deposit, - state: AddressStatus.unused, - index: nativeAddr.index, - // balance: 0, // TODO: Balance and other fields? - ), - ); - } - } - myAddressBook.addAll(toAdd); - - toAdd.clear(); - for (int i = 0; i < changeAddressCount; i++) { - bdk.AddressInfo nativeAddr; - String nativeAddrStr; - - nativeAddr = await bdkWallet.getInternalAddress( - addressIndex: bdk.AddressIndex.peek(index: i), - ); - nativeAddrStr = await nativeAddr.address.asString(); - - final matchIndex = - myAddressBook.indexWhere((a) => a.address == nativeAddrStr); - // print('matchIndex $matchIndex $i'); - if (matchIndex != -1) { - // print( - // 'myAddressbook.change index $i : ${nativeAddr.index} (${myAddressBook[matchIndex].address}, $nativeAddrStr)', - // ); - - final newAddr = - myAddressBook[matchIndex].copyWith(index: nativeAddr.index); - myAddressBook[matchIndex] = newAddr; - } else { - toAdd.add( - Address( - address: nativeAddrStr, - kind: AddressKind.change, - state: AddressStatus.unused, - index: nativeAddr.index, - // balance: 0, // TODO: Balance and other fields? - ), - ); - } - } - myAddressBook.addAll(toAdd); - - // print('After patch:'); - // for (int i = 0; i < myAddressBook.length; i++) { - // print( - // 'myAddressbook[$i] : ${myAddressBook[i].index} ${myAddressBook[i].kind} : (${myAddressBook[i].address})', - // ); - // } - - return w.copyWith( - myAddressBook: myAddressBook, - ); -} +// Future updateAddressNullIssue( +// Map walletObj, +// // bdk.Blockchain mainBlockchain, +// // bdk.Blockchain testBlockchain, +// ) async { +// final Wallet w = Wallet.fromJson(walletObj); +// final WalletsRepository walletRepo = WalletsRepository(); +// final bdkCreate = BDKCreate(walletsRepository: walletRepo); +// final (bdkWallet, _) = await bdkCreate.loadPublicBdkWallet(w); + +// print('syncing wallet ${w.id}. Pls wait...'); +// final network = await bdkWallet!.network(); +// // await bdkWallet.sync( +// // blockchain: +// // network == bdk.Network.bitcoin ? mainBlockchain : testBlockchain, +// // ); +// print('sync done'); + +// final myAddressBook = [...w.myAddressBook].toList(); + +// final bdk.AddressInfo lastDepositAddr = await bdkWallet.getAddress( +// addressIndex: const bdk.AddressIndex.lastUnused(), +// ); +// final int depositAddressCount = lastDepositAddr.index; + +// final bdk.AddressInfo lastChangeAddr = await bdkWallet.getInternalAddress( +// addressIndex: const bdk.AddressIndex.lastUnused(), +// ); +// final int changeAddressCount = lastChangeAddr.index; +// //int ivar = 0; +// //w.myAddressBook.map((addr) { +// // if (addr.kind == AddressKind.change) { +// // changeAddressCount++; +// // } else if (addr.kind == AddressKind.deposit) { +// // depositAddressCount++; +// // } +// // print( +// // 'myAddressbook[$ivar] : ${addr.index} ${addr.kind} : (${addr.address})', +// // ); +// // ivar++; +// // return addr; +// //}).toList(); + +// final List
toAdd = []; +// for (int i = 0; i < depositAddressCount; i++) { +// bdk.AddressInfo nativeAddr; +// String nativeAddrStr; + +// nativeAddr = await bdkWallet.getAddress( +// addressIndex: bdk.AddressIndex.peek(index: i), +// ); +// nativeAddrStr = await nativeAddr.address.asString(); + +// final matchIndex = +// myAddressBook.indexWhere((a) => a.address == nativeAddrStr); +// // print('matchIndex $matchIndex $i $nativeAddrStr'); +// if (matchIndex != -1) { +// // print( +// // 'myAddressbook.deposit index $i : ${nativeAddr.index} (${myAddressBook[matchIndex].address}, $nativeAddrStr)', +// // ); +// final newAddr = +// myAddressBook[matchIndex].copyWith(index: nativeAddr.index); +// myAddressBook[matchIndex] = newAddr; +// } else { +// toAdd.add( +// Address( +// address: nativeAddrStr, +// kind: AddressKind.deposit, +// state: AddressStatus.unused, +// index: nativeAddr.index, +// // balance: 0, // TODO: Balance and other fields? +// ), +// ); +// } +// } +// myAddressBook.addAll(toAdd); + +// toAdd.clear(); +// for (int i = 0; i < changeAddressCount; i++) { +// bdk.AddressInfo nativeAddr; +// String nativeAddrStr; + +// nativeAddr = await bdkWallet.getInternalAddress( +// addressIndex: bdk.AddressIndex.peek(index: i), +// ); +// nativeAddrStr = await nativeAddr.address.asString(); + +// final matchIndex = +// myAddressBook.indexWhere((a) => a.address == nativeAddrStr); +// // print('matchIndex $matchIndex $i'); +// if (matchIndex != -1) { +// // print( +// // 'myAddressbook.change index $i : ${nativeAddr.index} (${myAddressBook[matchIndex].address}, $nativeAddrStr)', +// // ); + +// final newAddr = +// myAddressBook[matchIndex].copyWith(index: nativeAddr.index); +// myAddressBook[matchIndex] = newAddr; +// } else { +// toAdd.add( +// Address( +// address: nativeAddrStr, +// kind: AddressKind.change, +// state: AddressStatus.unused, +// index: nativeAddr.index, +// // balance: 0, // TODO: Balance and other fields? +// ), +// ); +// } +// } +// myAddressBook.addAll(toAdd); + +// // print('After patch:'); +// // for (int i = 0; i < myAddressBook.length; i++) { +// // print( +// // 'myAddressbook[$i] : ${myAddressBook[i].index} ${myAddressBook[i].kind} : (${myAddressBook[i].address})', +// // ); +// // } + +// return w.copyWith( +// myAddressBook: myAddressBook, +// ); +// } Future> addIsLiquid( Map walletObj,