stack_wallet/lib/services/wallets_service.dart

112 lines
3.1 KiB
Dart
Raw Normal View History

/*
2023-05-26 21:21:16 +00:00
* This file is part of Stack Wallet.
*
2023-05-26 21:21:16 +00:00
* 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
*
*/
2022-08-26 08:11:35 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
2023-03-01 21:52:13 +00:00
import 'package:stackwallet/db/hive/db.dart';
2024-05-15 21:20:45 +00:00
import 'package:stackwallet/supported_coins.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/utilities/logger.dart';
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
class WalletInfo {
2024-05-15 21:20:45 +00:00
final String coinIdentifier;
2022-08-26 08:11:35 +00:00
final String walletId;
final String name;
@Deprecated("Legacy support only. Do not use.")
const WalletInfo({
2024-05-15 21:20:45 +00:00
required this.coinIdentifier,
required this.walletId,
required this.name,
});
2022-08-26 08:11:35 +00:00
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
factory WalletInfo.fromJson(Map<String, dynamic> jsonObject) {
return WalletInfo(
2024-05-15 21:20:45 +00:00
coinIdentifier: jsonObject["coin"] as String,
2022-08-26 08:11:35 +00:00
walletId: jsonObject["id"] as String,
name: jsonObject["name"] as String,
);
}
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Map<String, String> toMap() {
return {
"name": name,
"id": walletId,
2024-05-15 21:20:45 +00:00
"coin": coinIdentifier,
2022-08-26 08:11:35 +00:00
};
}
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
String toJsonString() {
return jsonEncode(toMap());
}
@override
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
String toString() {
return "WalletInfo: ${toJsonString()}";
}
}
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
class WalletsService extends ChangeNotifier {
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Future<Map<String, WalletInfo>>? _walletNames;
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Future<Map<String, WalletInfo>> get walletNames =>
_walletNames ??= _fetchWalletNames();
@Deprecated("Legacy support only. Do not use.")
WalletsService();
2022-08-26 08:11:35 +00:00
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Future<Map<String, WalletInfo>> _fetchWalletNames() async {
final names = DB.instance
.get<dynamic>(boxName: DB.boxNameAllWalletsData, key: 'names') as Map?;
if (names == null) {
Logging.instance.log(
2024-05-15 21:20:45 +00:00
"Fetched wallet 'names' returned null. Setting initializing 'names'",
level: LogLevel.Info,
);
2022-08-26 08:11:35 +00:00
await DB.instance.put<dynamic>(
2024-05-15 21:20:45 +00:00
boxName: DB.boxNameAllWalletsData,
key: 'names',
value: <String, dynamic>{},
);
2022-08-26 08:11:35 +00:00
return {};
}
2023-03-27 20:29:08 +00:00
Logging.instance.log("Fetched wallet names: $names", level: LogLevel.Info);
final mapped = Map<String, dynamic>.from(names);
mapped.removeWhere((name, dyn) {
final jsonObject = Map<String, dynamic>.from(dyn as Map);
try {
2024-05-21 18:10:09 +00:00
Coins.getCryptoCurrencyFor(jsonObject["coin"] as String);
2023-03-27 20:29:08 +00:00
return false;
} catch (e, s) {
2024-05-15 21:20:45 +00:00
Logging.instance.log(
"Error, ${jsonObject["coin"]} does not exist",
level: LogLevel.Error,
);
2023-03-27 20:29:08 +00:00
return true;
}
});
2024-05-15 21:20:45 +00:00
return mapped.map(
(name, dyn) => MapEntry(
name,
WalletInfo.fromJson(Map<String, dynamic>.from(dyn as Map)),
),
);
2023-03-27 20:29:08 +00:00
}
2022-08-26 08:11:35 +00:00
}