gecko/lib/providers/my_wallets.dart

188 lines
5.2 KiB
Dart
Raw Normal View History

import 'dart:typed_data';
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';
import 'package:gecko/models/chest_data.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/models/wallet_data.dart';
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;
late String mnemonic;
late Uint8List cesiumSeed;
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() {
if (configBox.get('currentChest') == null) {
configBox.put('currentChest', 0);
}
return configBox.get('currentChest');
}
2021-01-23 16:38:03 +01:00
bool checkIfWalletExist() {
// configBox.delete('endpoint');
if (!configBox.containsKey('endpoint') || configBox.get('endpoint') == '') {
log.d('No endpoint, configure...');
2022-05-21 06:47:26 +02:00
configBox.put('endpoint', 'ws://127.0.0.1:9944');
}
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) {
listWallets.clear();
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-12-23 12:36:09 +01:00
WalletData? getWalletData(List<int?> _id) {
if (_id.isEmpty) return WalletData();
2021-12-23 12:36:09 +01:00
int? _chest = _id[0];
int? _nbr = _id[1];
WalletData? _targetedWallet;
walletBox.toMap().forEach((key, value) {
if (value.chest == _chest && value.number == _nbr) {
_targetedWallet = value;
2021-11-14 19:21:20 +01:00
return;
}
});
return _targetedWallet;
}
2021-12-23 12:36:09 +01:00
WalletData? getDefaultWallet(int? chest) {
if (chestBox.isEmpty) {
return WalletData(chest: 0, number: 0);
} else {
2021-12-23 12:36:09 +01:00
int? defaultWalletNumber = chestBox.get(chest)!.defaultWallet;
return getWalletData([chest, defaultWalletNumber]);
}
}
Future<int> deleteAllWallet(context) async {
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
try {
log.w('DELETE ALL WALLETS ?');
2021-12-23 21:44:24 +01:00
final bool? _answer = await (_confirmDeletingAllWallets(context));
if (_answer!) {
await walletBox.clear();
await chestBox.clear();
await configBox.delete('defaultWallet');
await _sub.deleteAllAccounts();
await Navigator.of(context).pushNamedAndRemoveUntil(
'/',
ModalRoute.withName('/'),
);
}
return 0;
} catch (e) {
return 1;
}
}
2021-12-23 12:36:09 +01:00
Future<bool?> _confirmDeletingAllWallets(context) async {
return showDialog<bool>(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
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),
),
actions: <Widget>[
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)
],
)
],
);
},
);
}
Future<void> generateNewDerivation(context, String _name) async {
int _newDerivationNbr;
int _newWalletNbr;
2021-12-23 12:36:09 +01:00
int? _chest = getCurrentChest();
List<WalletData> _walletConfig = readAllWallets(_chest);
2021-02-15 01:44:25 +01:00
if (_walletConfig.isEmpty) {
_newDerivationNbr = 2;
_newWalletNbr = 0;
} else {
_newDerivationNbr = _walletConfig.last.derivation! + 2;
2021-12-23 12:36:09 +01:00
_newWalletNbr = _walletConfig.last.number! + 1;
}
2021-02-15 01:44:25 +01:00
MyWalletsProvider myWalletProvider =
Provider.of<MyWalletsProvider>(context, listen: false);
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
final int? _currentChestNumber = myWalletProvider.getCurrentChest();
final ChestData _currentChest = chestBox.get(_currentChestNumber)!;
final address = await _sub.derive(
context, _currentChest.address!, _newDerivationNbr, pinCode);
WalletData newWallet = WalletData(
chest: _chest,
address: address,
number: _newWalletNbr,
name: _name,
derivation: _newDerivationNbr,
imageName: '${_newWalletNbr % 4}.png');
2021-02-15 01:44:25 +01:00
await walletBox.add(newWallet);
2021-02-15 01:44:25 +01:00
notifyListeners();
2021-02-15 01:44:25 +01:00
}
void rebuildWidget() {
notifyListeners();
}
2021-01-23 16:38:03 +01:00
}