2022-06-18 00:48:07 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-01-23 16:38:03 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'dart:async';
|
2021-01-26 21:00:26 +01:00
|
|
|
import 'package:gecko/globals.dart';
|
2021-11-14 19:21:20 +01:00
|
|
|
import 'package:gecko/models/wallet_data.dart';
|
2022-05-19 07:00:25 +02:00
|
|
|
import 'package:gecko/providers/substrate_sdk.dart';
|
2022-05-28 19:13:30 +02:00
|
|
|
import 'package:gecko/screens/common_elements.dart';
|
2022-05-19 07:00:25 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2021-01-23 16:38:03 +01:00
|
|
|
|
|
|
|
class MyWalletsProvider with ChangeNotifier {
|
2021-03-31 02:38:44 +02:00
|
|
|
List<WalletData> listWallets = [];
|
2022-05-31 18:23:56 +02:00
|
|
|
String pinCode = '';
|
2021-12-30 00:51:04 +01:00
|
|
|
late String mnemonic;
|
2021-12-23 12:36:09 +01:00
|
|
|
int? pinLenght;
|
2022-05-28 19:13:30 +02:00
|
|
|
bool isNewDerivationLoading = false;
|
2021-04-03 00:07:03 +02:00
|
|
|
|
2022-06-05 21:28:16 +02:00
|
|
|
int getCurrentChest() {
|
2021-11-07 04:23:05 +01:00
|
|
|
if (configBox.get('currentChest') == null) {
|
|
|
|
configBox.put('currentChest', 0);
|
2021-03-19 06:33:48 +01:00
|
|
|
}
|
2021-11-07 04:23:05 +01:00
|
|
|
|
|
|
|
return configBox.get('currentChest');
|
2021-03-19 06:33:48 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 16:38:03 +01:00
|
|
|
bool checkIfWalletExist() {
|
2021-11-14 04:33:59 +01:00
|
|
|
if (chestBox.isEmpty) {
|
2021-04-02 12:05:37 +02:00
|
|
|
log.i('No wallets detected');
|
2021-01-23 16:38:03 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 15:17:07 +02:00
|
|
|
List<WalletData> readAllWallets([int? _chest]) {
|
|
|
|
_chest = _chest ?? configBox.get('currentChest') ?? 0;
|
2021-11-07 06:50:09 +01:00
|
|
|
listWallets.clear();
|
2021-11-07 04:23:05 +01:00
|
|
|
walletBox.toMap().forEach((key, value) {
|
|
|
|
if (value.chest == _chest) {
|
|
|
|
listWallets.add(value);
|
|
|
|
}
|
2021-01-26 21:00:26 +01:00
|
|
|
});
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-01-26 21:00:26 +01:00
|
|
|
return listWallets;
|
|
|
|
}
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2022-05-29 04:30:03 +02:00
|
|
|
WalletData? getWalletDataById(List<int?> _id) {
|
2021-11-07 04:23:05 +01:00
|
|
|
if (_id.isEmpty) return WalletData();
|
2021-12-23 12:36:09 +01:00
|
|
|
int? _chest = _id[0];
|
|
|
|
int? _nbr = _id[1];
|
|
|
|
WalletData? _targetedWallet;
|
2021-04-04 15:35:38 +02:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
walletBox.toMap().forEach((key, value) {
|
|
|
|
if (value.chest == _chest && value.number == _nbr) {
|
2021-11-07 06:50:09 +01:00
|
|
|
_targetedWallet = value;
|
2021-11-14 19:21:20 +01:00
|
|
|
return;
|
2021-11-07 04:23:05 +01:00
|
|
|
}
|
|
|
|
});
|
2021-04-04 18:53:32 +02:00
|
|
|
|
2021-11-07 06:50:09 +01:00
|
|
|
return _targetedWallet;
|
2021-04-04 15:35:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 04:30:03 +02:00
|
|
|
WalletData? getWalletDataByAddress(String address) {
|
|
|
|
WalletData? _targetedWallet;
|
|
|
|
|
|
|
|
walletBox.toMap().forEach((key, value) {
|
|
|
|
if (value.address == address) {
|
|
|
|
_targetedWallet = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return _targetedWallet;
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:43:22 +02:00
|
|
|
WalletData getDefaultWallet([int? chest]) {
|
2021-11-14 03:12:07 +01:00
|
|
|
if (chestBox.isEmpty) {
|
|
|
|
return WalletData(chest: 0, number: 0);
|
|
|
|
} else {
|
2022-05-26 02:19:29 +02:00
|
|
|
chest ??= getCurrentChest();
|
2021-12-23 12:36:09 +01:00
|
|
|
int? defaultWalletNumber = chestBox.get(chest)!.defaultWallet;
|
2022-06-10 01:24:50 +02:00
|
|
|
return getWalletDataById([chest, defaultWalletNumber]) ??
|
|
|
|
WalletData(chest: chest, number: 0);
|
2021-04-04 15:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
Future<int> deleteAllWallet(context) async {
|
2022-05-20 15:15:29 +02:00
|
|
|
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
|
2022-05-31 18:23:56 +02:00
|
|
|
MyWalletsProvider _myWalletProvider =
|
|
|
|
Provider.of<MyWalletsProvider>(context, listen: false);
|
2021-01-28 15:10:09 +01:00
|
|
|
try {
|
2021-11-07 04:23:05 +01:00
|
|
|
log.w('DELETE ALL WALLETS ?');
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2022-05-29 01:38:27 +02:00
|
|
|
final bool? _answer = await (confirmPopup(
|
2022-06-18 00:48:07 +02:00
|
|
|
context, 'areYouSureForgetAllChests'.tr()));
|
2021-12-23 21:44:24 +01:00
|
|
|
if (_answer!) {
|
2021-11-07 04:23:05 +01:00
|
|
|
await walletBox.clear();
|
|
|
|
await chestBox.clear();
|
2021-11-07 06:50:09 +01:00
|
|
|
await configBox.delete('defaultWallet');
|
2022-05-20 15:15:29 +02:00
|
|
|
await _sub.deleteAllAccounts();
|
2022-05-31 18:23:56 +02:00
|
|
|
|
|
|
|
_myWalletProvider.pinCode = '';
|
|
|
|
|
2021-11-22 18:45:59 +01:00
|
|
|
await Navigator.of(context).pushNamedAndRemoveUntil(
|
|
|
|
'/',
|
|
|
|
ModalRoute.withName('/'),
|
|
|
|
);
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} catch (e) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 15:17:07 +02:00
|
|
|
Future<void> generateNewDerivation(context, String _name,
|
|
|
|
[int? number]) async {
|
|
|
|
isNewDerivationLoading = true;
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
final List idList = getNextWalletNumberAndDerivation();
|
|
|
|
int _newWalletNbr = idList[0];
|
|
|
|
int _newDerivationNbr = number ?? idList[1];
|
|
|
|
|
|
|
|
int? _chest = getCurrentChest();
|
|
|
|
|
|
|
|
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
|
|
|
|
|
2022-06-02 15:43:22 +02:00
|
|
|
WalletData defaultWallet = getDefaultWallet();
|
2022-06-01 15:17:07 +02:00
|
|
|
|
|
|
|
final address = await _sub.derive(
|
|
|
|
context, defaultWallet.address!, _newDerivationNbr, pinCode);
|
|
|
|
|
|
|
|
WalletData newWallet = WalletData(
|
|
|
|
version: dataVersion,
|
|
|
|
chest: _chest,
|
|
|
|
address: address,
|
|
|
|
number: _newWalletNbr,
|
|
|
|
name: _name,
|
|
|
|
derivation: _newDerivationNbr,
|
|
|
|
imageDefaultPath: '${_newWalletNbr % 4}.png');
|
|
|
|
|
|
|
|
await walletBox.add(newWallet);
|
|
|
|
|
|
|
|
isNewDerivationLoading = false;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> generateRootWallet(context, String _name) async {
|
2022-05-29 01:38:27 +02:00
|
|
|
MyWalletsProvider _myWalletProvider =
|
|
|
|
Provider.of<MyWalletsProvider>(context, listen: false);
|
|
|
|
|
2022-05-28 19:13:30 +02:00
|
|
|
isNewDerivationLoading = true;
|
|
|
|
notifyListeners();
|
2021-03-19 06:33:48 +01:00
|
|
|
int _newWalletNbr;
|
2021-12-23 12:36:09 +01:00
|
|
|
int? _chest = getCurrentChest();
|
2022-05-28 19:13:30 +02:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
List<WalletData> _walletConfig = readAllWallets(_chest);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
if (_walletConfig.isEmpty) {
|
2021-03-19 06:33:48 +01:00
|
|
|
_newWalletNbr = 0;
|
2021-02-15 03:59:07 +01:00
|
|
|
} else {
|
2021-12-23 12:36:09 +01:00
|
|
|
_newWalletNbr = _walletConfig.last.number! + 1;
|
2021-02-15 03:59:07 +01:00
|
|
|
}
|
2022-05-20 15:15:29 +02:00
|
|
|
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
|
2022-05-19 07:00:25 +02:00
|
|
|
|
2022-06-02 15:43:22 +02:00
|
|
|
WalletData defaultWallet = _myWalletProvider.getDefaultWallet();
|
2022-05-19 07:00:25 +02:00
|
|
|
|
2022-06-01 15:17:07 +02:00
|
|
|
final address =
|
|
|
|
await _sub.generateRootKeypair(defaultWallet.address!, pinCode);
|
2022-05-19 07:00:25 +02:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
WalletData newWallet = WalletData(
|
2022-05-24 16:51:40 +02:00
|
|
|
version: dataVersion,
|
2021-11-07 04:23:05 +01:00
|
|
|
chest: _chest,
|
2022-05-19 07:00:25 +02:00
|
|
|
address: address,
|
2021-11-07 04:23:05 +01:00
|
|
|
number: _newWalletNbr,
|
|
|
|
name: _name,
|
2022-06-01 15:17:07 +02:00
|
|
|
derivation: -1,
|
2022-05-28 19:13:30 +02:00
|
|
|
imageDefaultPath: '${_newWalletNbr % 4}.png');
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
await walletBox.add(newWallet);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2022-05-28 19:13:30 +02:00
|
|
|
isNewDerivationLoading = false;
|
2021-11-07 04:23:05 +01:00
|
|
|
notifyListeners();
|
2021-02-15 01:44:25 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 15:17:07 +02:00
|
|
|
List<int> getNextWalletNumberAndDerivation(
|
|
|
|
{int? chestNumber, bool isOneshoot = false}) {
|
|
|
|
int _newDerivationNbr = 0;
|
|
|
|
int _newWalletNbr = 0;
|
|
|
|
|
|
|
|
chestNumber ??= getCurrentChest();
|
|
|
|
|
|
|
|
List<WalletData> _walletConfig = readAllWallets(chestNumber);
|
|
|
|
|
|
|
|
if (_walletConfig.isEmpty) {
|
|
|
|
_newDerivationNbr = 2;
|
|
|
|
} else {
|
|
|
|
WalletData _lastWallet = _walletConfig.reduce(
|
|
|
|
(curr, next) => curr.derivation! > next.derivation! ? curr : next);
|
|
|
|
|
|
|
|
if (_lastWallet.derivation == -1) {
|
|
|
|
_newDerivationNbr = 2;
|
|
|
|
} else {
|
|
|
|
_newDerivationNbr = _lastWallet.derivation! + (isOneshoot ? 1 : 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
_newWalletNbr = _walletConfig.last.number! + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [_newWalletNbr, _newDerivationNbr];
|
|
|
|
}
|
|
|
|
|
2022-05-31 18:58:44 +02:00
|
|
|
int lockPin = 0;
|
2022-05-31 18:23:56 +02:00
|
|
|
Future resetPinCode([int minutes = 15]) async {
|
2022-05-31 18:58:44 +02:00
|
|
|
lockPin++;
|
|
|
|
final actualLock = lockPin;
|
2022-05-31 18:23:56 +02:00
|
|
|
await Future.delayed(
|
|
|
|
Duration(seconds: configBox.get('isCacheChecked') ? minutes * 60 : 1));
|
2022-05-31 18:58:44 +02:00
|
|
|
log.i('reset pin code, lock $actualLock ...');
|
|
|
|
if (actualLock == lockPin) pinCode = '';
|
2022-05-31 18:23:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
void rebuildWidget() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-01-23 16:38:03 +01:00
|
|
|
}
|