clean up walletInfo

This commit is contained in:
julian 2023-11-09 16:27:17 -06:00
parent d1a3f21396
commit f5bd1d22f6
4 changed files with 64 additions and 137 deletions

View file

@ -4,7 +4,6 @@ import 'package:hive_flutter/hive_flutter.dart';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/hive/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/transaction_note.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
@ -121,8 +120,7 @@ Future<void> migrateWalletsToIsar({
coinName: old.coin.name,
walletId: old.walletId,
name: old.name,
walletType: _walletTypeForCoin(old.coin),
mainAddressType: _addressTypeForCoin(old.coin),
mainAddressType: old.coin.primaryAddressType,
favouriteOrderIndex: favourites.indexOf(old.walletId),
isMnemonicVerified: allWalletsBox
.get("${old.walletId}_mnemonicHasBeenVerified") as bool? ??
@ -160,101 +158,3 @@ Future<void> _cleanupOnSuccess({required List<String> walletIds}) async {
await Hive.deleteBoxFromDisk(walletId);
}
}
WalletType _walletTypeForCoin(Coin coin) {
WalletType walletType;
switch (coin) {
case Coin.bitcoin:
case Coin.bitcoinTestNet:
case Coin.bitcoincash:
case Coin.bitcoincashTestnet:
case Coin.litecoin:
case Coin.dogecoin:
case Coin.firo:
case Coin.namecoin:
case Coin.particl:
case Coin.litecoinTestNet:
case Coin.firoTestNet:
case Coin.dogecoinTestNet:
case Coin.eCash:
walletType = WalletType.bip39HD;
break;
case Coin.monero:
case Coin.wownero:
walletType = WalletType.cryptonote;
break;
case Coin.epicCash:
case Coin.ethereum:
case Coin.tezos:
case Coin.nano:
case Coin.banano:
case Coin.stellar:
case Coin.stellarTestnet:
walletType = WalletType.bip39;
break;
}
return walletType;
}
AddressType _addressTypeForCoin(Coin coin) {
AddressType addressType;
switch (coin) {
case Coin.bitcoin:
case Coin.bitcoinTestNet:
case Coin.litecoin:
case Coin.litecoinTestNet:
addressType = AddressType.p2wpkh;
break;
case Coin.eCash:
case Coin.bitcoincash:
case Coin.bitcoincashTestnet:
case Coin.dogecoin:
case Coin.firo:
case Coin.firoTestNet:
case Coin.namecoin:
case Coin.particl:
case Coin.dogecoinTestNet:
addressType = AddressType.p2pkh;
break;
case Coin.monero:
case Coin.wownero:
addressType = AddressType.cryptonote;
break;
case Coin.epicCash:
addressType = AddressType.mimbleWimble;
break;
case Coin.ethereum:
addressType = AddressType.ethereum;
break;
case Coin.tezos:
// should not be unknown but since already used in prod changing
// this requires a migrate
addressType = AddressType.unknown;
break;
case Coin.nano:
addressType = AddressType.nano;
break;
case Coin.banano:
addressType = AddressType.banano;
break;
case Coin.stellar:
case Coin.stellarTestnet:
// should not be unknown but since already used in prod changing
// this requires a migrate
addressType = AddressType.unknown;
break;
}
return addressType;
}

View file

@ -8,6 +8,7 @@
*
*/
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
import 'package:stackwallet/utilities/constants.dart';
enum Coin {
@ -354,6 +355,55 @@ extension CoinExt on Coin {
}
int get decimals => Constants.decimalPlacesForCoin(this);
AddressType get primaryAddressType {
switch (this) {
case Coin.bitcoin:
case Coin.bitcoinTestNet:
case Coin.litecoin:
case Coin.litecoinTestNet:
return AddressType.p2wpkh;
break;
case Coin.eCash:
case Coin.bitcoincash:
case Coin.bitcoincashTestnet:
case Coin.dogecoin:
case Coin.firo:
case Coin.firoTestNet:
case Coin.namecoin:
case Coin.particl:
case Coin.dogecoinTestNet:
return AddressType.p2pkh;
case Coin.monero:
case Coin.wownero:
return AddressType.cryptonote;
case Coin.epicCash:
return AddressType.mimbleWimble;
case Coin.ethereum:
return AddressType.ethereum;
case Coin.tezos:
// should not be unknown but since already used in prod changing
// this requires a migrate
return AddressType.unknown;
case Coin.nano:
return AddressType.nano;
case Coin.banano:
return AddressType.banano;
case Coin.stellar:
case Coin.stellarTestnet:
// should not be unknown but since already used in prod changing
// this requires a migrate
return AddressType.unknown;
}
}
}
Coin coinFromPrettyName(String name) {

View file

@ -19,9 +19,6 @@ class WalletInfo implements IsarId {
final String name;
@enumerated
final WalletType walletType;
@enumerated
final AddressType mainAddressType;
@ -248,7 +245,6 @@ class WalletInfo implements IsarId {
required this.coinName,
required this.walletId,
required this.name,
required this.walletType,
required this.mainAddressType,
// cachedReceivingAddress should never actually be empty in practice as
@ -291,23 +287,10 @@ class WalletInfo implements IsarId {
throw UnimplementedError();
}
final WalletType walletType;
switch (coin) {
case Coin.bitcoin:
case Coin.bitcoinTestNet:
case Coin.bitcoincash:
case Coin.bitcoincashTestnet:
walletType = WalletType.bip39HD;
default:
throw UnimplementedError();
}
return WalletInfo(
coinName: coin.name,
walletId: walletIdOverride ?? const Uuid().v1(),
name: name,
walletType: walletType,
mainAddressType: mainAddressType,
restoreHeight: restoreHeight,
);
@ -328,7 +311,6 @@ class WalletInfo implements IsarId {
coinName: coinName ?? this.coinName,
walletId: walletId,
name: name ?? this.name,
walletType: walletType,
mainAddressType: mainAddressType,
favouriteOrderIndex: favouriteOrderIndex ?? this.favouriteOrderIndex,
cachedChainHeight: cachedChainHeight ?? this.cachedChainHeight,
@ -343,14 +325,15 @@ class WalletInfo implements IsarId {
}
@Deprecated("Legacy support")
factory WalletInfo.fromJson(Map<String, dynamic> jsonObject,
WalletType walletType, AddressType mainAddressType) {
factory WalletInfo.fromJson(
Map<String, dynamic> jsonObject,
AddressType mainAddressType,
) {
final coin = Coin.values.byName(jsonObject["coin"] as String);
return WalletInfo(
coinName: coin.name,
walletId: jsonObject["id"] as String,
name: jsonObject["name"] as String,
walletType: walletType,
mainAddressType: mainAddressType,
);
}
@ -380,12 +363,3 @@ abstract class WalletInfoKeys {
static const String cachedSecondaryBalance = "cachedSecondaryBalanceKey";
static const String epiccashData = "epiccashDataKey";
}
// Used in Isar db and stored there as int indexes so adding/removing values
// in this definition should be done extremely carefully in production
enum WalletType {
bip39,
bip39HD,
cryptonote,
privateKeyBased;
}

View file

@ -30,8 +30,12 @@ import 'package:stackwallet/wallets/wallet/impl/bitcoin_wallet.dart';
import 'package:stackwallet/wallets/wallet/impl/bitcoincash_wallet.dart';
import 'package:stackwallet/wallets/wallet/impl/epiccash_wallet.dart';
import 'package:stackwallet/wallets/wallet/impl/wownero_wallet.dart';
import 'package:stackwallet/wallets/wallet/intermediate/bip39_hd_wallet.dart';
import 'package:stackwallet/wallets/wallet/intermediate/bip39_wallet.dart';
import 'package:stackwallet/wallets/wallet/intermediate/cryptonote_wallet.dart';
import 'package:stackwallet/wallets/wallet/mixins/electrumx_mixin.dart';
import 'package:stackwallet/wallets/wallet/mixins/multi_address.dart';
import 'package:stackwallet/wallets/wallet/private_key_based_wallet.dart';
abstract class Wallet<T extends CryptoCurrency> {
// default to Transaction class. For TransactionV2 set to 2
@ -90,7 +94,6 @@ abstract class Wallet<T extends CryptoCurrency> {
// ========== Wallet Info Convenience Getters ================================
String get walletId => info.walletId;
WalletType get walletType => info.walletType;
/// Attempt to fetch the most recent chain height.
/// On failure return the last cached height.
@ -130,9 +133,9 @@ abstract class Wallet<T extends CryptoCurrency> {
prefs: prefs,
);
switch (walletInfo.walletType) {
case WalletType.bip39:
case WalletType.bip39HD:
switch (wallet.runtimeType) {
case Bip39Wallet:
case Bip39HDWallet:
await secureStorageInterface.write(
key: mnemonicKey(walletId: walletInfo.walletId),
value: mnemonic!,
@ -143,10 +146,10 @@ abstract class Wallet<T extends CryptoCurrency> {
);
break;
case WalletType.cryptonote:
case CryptonoteWallet:
break;
case WalletType.privateKeyBased:
case PrivateKeyBasedWallet:
await secureStorageInterface.write(
key: privateKeyKey(walletId: walletInfo.walletId),
value: privateKey!,