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';
|
2022-05-19 07:00:25 +02:00
|
|
|
import 'package:gecko/models/chest_data.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';
|
|
|
|
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 = [];
|
2021-12-23 12:36:09 +01:00
|
|
|
late String pinCode;
|
2021-12-30 00:51:04 +01:00
|
|
|
late String mnemonic;
|
2021-12-23 12:36:09 +01:00
|
|
|
int? pinLenght;
|
2021-04-03 00:07:03 +02:00
|
|
|
|
2021-12-23 12:36:09 +01: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
List<WalletData> readAllWallets(int? _chest) {
|
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
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
WalletData? getWalletData(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-26 02:19:29 +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;
|
2021-11-14 03:12:07 +01:00
|
|
|
return getWalletData([chest, defaultWalletNumber]);
|
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);
|
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
|
|
|
|
2021-12-23 21:44:24 +01:00
|
|
|
final bool? _answer = await (_confirmDeletingAllWallets(context));
|
|
|
|
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();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
Future<bool?> _confirmDeletingAllWallets(context) async {
|
2021-01-28 15:10:09 +01:00
|
|
|
return showDialog<bool>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: true, // user must tap button!
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
2022-05-20 15:15:29 +02:00
|
|
|
backgroundColor: backgroundColor,
|
|
|
|
content: const Text(
|
|
|
|
'Êtes-vous sûr de vouloir oublier tous vos coffres ?',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
|
|
|
|
),
|
2021-01-28 15:10:09 +01:00
|
|
|
actions: <Widget>[
|
2022-05-20 15:15:29 +02:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
|
|
|
key: const Key('confirmDeletingAllWallets'),
|
|
|
|
child: const Text(
|
|
|
|
"Oui",
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 20,
|
|
|
|
color: Color(0xffD80000),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, true);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(width: 20),
|
|
|
|
TextButton(
|
|
|
|
child: const Text(
|
|
|
|
"Non",
|
|
|
|
style: TextStyle(fontSize: 20),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, false);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(height: 120)
|
|
|
|
],
|
|
|
|
)
|
2021-01-28 15:10:09 +01:00
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:08:45 +01:00
|
|
|
Future<void> generateNewDerivation(context, String _name) async {
|
2021-02-15 03:59:07 +01:00
|
|
|
int _newDerivationNbr;
|
2021-03-19 06:33:48 +01:00
|
|
|
int _newWalletNbr;
|
2021-12-23 12:36:09 +01:00
|
|
|
int? _chest = getCurrentChest();
|
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) {
|
2022-05-20 07:22:57 +02:00
|
|
|
_newDerivationNbr = 2;
|
2021-03-19 06:33:48 +01:00
|
|
|
_newWalletNbr = 0;
|
2021-02-15 03:59:07 +01:00
|
|
|
} else {
|
2022-05-20 07:22:57 +02:00
|
|
|
_newDerivationNbr = _walletConfig.last.derivation! + 2;
|
2021-12-23 12:36:09 +01:00
|
|
|
_newWalletNbr = _walletConfig.last.number! + 1;
|
2021-02-15 03:59:07 +01:00
|
|
|
}
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2022-05-19 07:00:25 +02:00
|
|
|
MyWalletsProvider myWalletProvider =
|
|
|
|
Provider.of<MyWalletsProvider>(context, listen: false);
|
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
|
2022-05-19 07:00:25 +02:00
|
|
|
|
|
|
|
final int? _currentChestNumber = myWalletProvider.getCurrentChest();
|
|
|
|
final ChestData _currentChest = chestBox.get(_currentChestNumber)!;
|
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
final address = await _sub.derive(
|
2022-05-20 07:22:57 +02:00
|
|
|
context, _currentChest.address!, _newDerivationNbr, 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,
|
2021-11-11 14:45:12 +01:00
|
|
|
derivation: _newDerivationNbr,
|
2021-11-15 00:39:40 +01:00
|
|
|
imageName: '${_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
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
notifyListeners();
|
2021-02-15 01:44:25 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
void rebuildWidget() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-01-23 16:38:03 +01:00
|
|
|
}
|