2021-01-28 15:10:09 +01:00
|
|
|
import 'dart:io';
|
2021-03-19 06:33:48 +01:00
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
import 'package:fast_base58/fast_base58.dart';
|
2021-01-28 15:10:09 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:gecko/globals.dart';
|
2021-12-23 15:13:58 +01:00
|
|
|
import 'package:gecko/providers/my_wallets.dart';
|
2021-11-14 19:21:20 +01:00
|
|
|
import 'package:gecko/models/wallet_data.dart';
|
2022-05-21 06:47:26 +02:00
|
|
|
import 'package:gecko/providers/substrate_sdk.dart';
|
2021-03-21 23:04:11 +01:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
2022-05-21 06:47:26 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2021-03-19 06:33:48 +01:00
|
|
|
import 'package:truncate/truncate.dart';
|
2021-01-28 15:10:09 +01:00
|
|
|
|
|
|
|
class WalletOptionsProvider with ChangeNotifier {
|
2022-05-19 07:00:25 +02:00
|
|
|
TextEditingController address = TextEditingController();
|
2021-11-14 19:21:20 +01:00
|
|
|
final TextEditingController _newWalletName = TextEditingController();
|
2021-01-28 15:10:09 +01:00
|
|
|
bool isWalletUnlock = false;
|
|
|
|
bool ischangedPin = false;
|
2021-11-14 19:21:20 +01:00
|
|
|
TextEditingController newPin = TextEditingController();
|
2021-03-21 00:35:32 +01:00
|
|
|
bool isEditing = false;
|
2022-05-19 09:07:26 +02:00
|
|
|
bool isBalanceBlur = false;
|
2021-03-21 00:35:32 +01:00
|
|
|
FocusNode walletNameFocus = FocusNode();
|
|
|
|
TextEditingController nameController = TextEditingController();
|
2021-12-23 12:36:09 +01:00
|
|
|
late bool isDefaultWallet;
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
Future<NewWallet>? get badWallet => null;
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2021-02-14 23:17:03 +01:00
|
|
|
int getPinLenght(_walletNbr) {
|
2021-12-23 15:13:58 +01:00
|
|
|
return pinLength;
|
2021-02-09 22:11:57 +01:00
|
|
|
}
|
|
|
|
|
2022-05-21 06:47:26 +02:00
|
|
|
void _renameWallet(List<int?> _walletID, String _newName,
|
2021-12-23 15:13:58 +01:00
|
|
|
{required bool isCesium}) async {
|
2022-05-21 06:47:26 +02:00
|
|
|
MyWalletsProvider myWalletClass = MyWalletsProvider();
|
2021-11-07 04:23:05 +01:00
|
|
|
|
2022-05-21 06:47:26 +02:00
|
|
|
WalletData _walletTarget = myWalletClass.getWalletData(_walletID)!;
|
|
|
|
_walletTarget.name = _newName;
|
|
|
|
await walletBox.put(_walletTarget.key, _walletTarget);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
|
|
|
_newWalletName.text = '';
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
bool editWalletName(List<int?> _wID, {bool? isCesium}) {
|
2021-03-21 00:35:32 +01:00
|
|
|
bool nameState;
|
|
|
|
if (isEditing) {
|
|
|
|
if (!nameController.text.contains(':') &&
|
2021-03-21 01:42:29 +01:00
|
|
|
nameController.text.length <= 39) {
|
2021-12-23 12:36:09 +01:00
|
|
|
_renameWallet(_wID, nameController.text, isCesium: isCesium!);
|
2021-03-21 00:35:32 +01:00
|
|
|
nameState = true;
|
|
|
|
} else {
|
|
|
|
nameState = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nameState = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEditing ? isEditing = false : isEditing = true;
|
|
|
|
notifyListeners();
|
|
|
|
return nameState;
|
|
|
|
}
|
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
Future<int> deleteWallet(context, WalletData wallet) async {
|
2021-12-23 21:44:24 +01:00
|
|
|
final bool? _answer = await (_confirmDeletingWallet(context, wallet.name));
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2021-12-23 21:44:24 +01:00
|
|
|
if (_answer!) {
|
2022-05-20 15:15:29 +02:00
|
|
|
await walletBox.delete(wallet.key);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
// Navigator.popUntil(
|
|
|
|
// context,
|
|
|
|
// ModalRoute.withName('/mywallets'),
|
|
|
|
// );
|
|
|
|
Navigator.pop(context);
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
2021-02-15 01:44:25 +01:00
|
|
|
return 0;
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
Future<bool?> _confirmDeletingWallet(context, _walletName) 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(
|
|
|
|
title: Text(
|
|
|
|
'Êtes-vous sûr de vouloir supprimer le portefeuille "$_walletName" ?'),
|
|
|
|
content: SingleChildScrollView(
|
|
|
|
child: ListBody(
|
2021-11-14 19:21:20 +01:00
|
|
|
children: const <Widget>[
|
2021-02-15 01:44:25 +01:00
|
|
|
Text('Vous pourrez restaurer ce portefeuille plus tard.'),
|
2021-01-28 15:10:09 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
2021-11-14 19:21:20 +01:00
|
|
|
child: const Text("Non", key: Key('cancelDeleting')),
|
2021-01-28 15:10:09 +01:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, false);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
2021-11-14 19:21:20 +01:00
|
|
|
child: const Text("Oui", key: Key('confirmDeleting')),
|
2021-01-28 15:10:09 +01:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, true);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-19 06:33:48 +01:00
|
|
|
String getShortPubkey(String pubkey) {
|
|
|
|
List<int> pubkeyByte = Base58Decode(pubkey);
|
|
|
|
Digest pubkeyS256 = sha256.convert(sha256.convert(pubkeyByte).bytes);
|
|
|
|
String pubkeyCheksum = Base58Encode(pubkeyS256.bytes);
|
|
|
|
String pubkeyChecksumShort = truncate(pubkeyCheksum, 3,
|
|
|
|
omission: "", position: TruncatePosition.end);
|
|
|
|
|
|
|
|
String pubkeyShort = truncate(pubkey, 5,
|
|
|
|
omission: String.fromCharCode(0x2026),
|
|
|
|
position: TruncatePosition.end) +
|
|
|
|
truncate(pubkey, 4, omission: "", position: TruncatePosition.start) +
|
|
|
|
':$pubkeyChecksumShort';
|
|
|
|
|
|
|
|
return pubkeyShort;
|
|
|
|
}
|
|
|
|
|
2021-03-21 00:35:32 +01:00
|
|
|
void bluringBalance() {
|
|
|
|
isBalanceBlur = !isBalanceBlur;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:04:11 +01:00
|
|
|
Future changeAvatar() async {
|
|
|
|
File _image;
|
|
|
|
final picker = ImagePicker();
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
|
2021-03-21 23:04:11 +01:00
|
|
|
|
|
|
|
if (pickedFile != null) {
|
|
|
|
_image = File(pickedFile.path);
|
2022-05-21 06:47:26 +02:00
|
|
|
|
|
|
|
////TODO: Store image on disk, store path in walletBox.imagePath
|
|
|
|
|
2021-11-17 06:20:23 +01:00
|
|
|
log.i(pickedFile.path);
|
2021-03-21 23:04:11 +01:00
|
|
|
return _image;
|
|
|
|
} else {
|
2021-04-02 12:05:37 +02:00
|
|
|
log.w('No image selected.');
|
2022-05-21 06:47:26 +02:00
|
|
|
return null;
|
2021-03-21 23:04:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 08:19:20 +01:00
|
|
|
void reloadBuild() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2022-05-19 07:00:25 +02:00
|
|
|
|
|
|
|
String? getAddress(int chest, int derivation) {
|
|
|
|
String? _address;
|
|
|
|
walletBox.toMap().forEach((key, value) {
|
2022-05-19 09:07:26 +02:00
|
|
|
if (value.chest == chest && value.derivation == derivation) {
|
2022-05-19 07:00:25 +02:00
|
|
|
_address = value.address!;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
address.text = _address ?? '';
|
|
|
|
|
|
|
|
return _address;
|
|
|
|
}
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
2022-05-21 06:47:26 +02:00
|
|
|
|
|
|
|
Widget balance(BuildContext context, String address, double size) {
|
|
|
|
String balanceCache = '';
|
|
|
|
|
|
|
|
return Column(children: <Widget>[
|
|
|
|
Consumer<SubstrateSdk>(builder: (context, _sdk, _) {
|
|
|
|
return FutureBuilder(
|
|
|
|
future: _sdk.getBalance(address),
|
|
|
|
builder: (BuildContext context, AsyncSnapshot<num?> _balance) {
|
|
|
|
if (_balance.connectionState != ConnectionState.done ||
|
|
|
|
_balance.hasError) {
|
|
|
|
return Text(balanceCache,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: isTall ? size : size * 0.9,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
balanceCache = "${_balance.data.toString()} $currencyName";
|
|
|
|
return Text(
|
|
|
|
balanceCache,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: isTall ? size : 18,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|