2021-12-20 21:33:03 +01:00
|
|
|
import 'dart:io';
|
2021-01-23 16:38:03 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-01-30 19:00:31 +01:00
|
|
|
import 'package:gecko/globals.dart';
|
2022-05-28 00:17:50 +02:00
|
|
|
import 'package:gecko/providers/substrate_sdk.dart';
|
2021-11-29 04:05:08 +01:00
|
|
|
import 'package:gecko/screens/wallet_view.dart';
|
2021-01-30 15:48:27 +01:00
|
|
|
import 'package:graphql_flutter/graphql_flutter.dart';
|
2021-03-21 00:35:32 +01:00
|
|
|
import 'package:jdenticon_dart/jdenticon_dart.dart';
|
2021-01-23 16:38:03 +01:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-06-01 23:59:56 +02:00
|
|
|
// import 'package:qrscan/qrscan.dart' as scanner;
|
|
|
|
import 'package:barcode_scan2/barcode_scan2.dart';
|
2021-01-30 15:24:08 +01:00
|
|
|
import 'dart:math';
|
|
|
|
import 'package:intl/intl.dart';
|
2021-01-23 16:38:03 +01:00
|
|
|
|
2021-11-30 01:25:48 +01:00
|
|
|
class WalletsProfilesProvider with ChangeNotifier {
|
2022-05-30 15:32:00 +02:00
|
|
|
WalletsProfilesProvider(this.address);
|
2021-11-30 01:25:48 +01:00
|
|
|
|
2022-05-30 15:32:00 +02:00
|
|
|
String? address = '';
|
2021-02-16 03:55:01 +01:00
|
|
|
String pubkeyShort = '';
|
2021-12-23 12:36:09 +01:00
|
|
|
List? transBC;
|
|
|
|
String? fetchMoreCursor;
|
|
|
|
Map? pageInfo;
|
2021-02-17 00:15:08 +01:00
|
|
|
bool isHistoryScreen = false;
|
|
|
|
String historySwitchButtun = "Voir l'historique";
|
2021-12-23 12:36:09 +01:00
|
|
|
String? rawSvg;
|
2021-04-03 00:07:03 +02:00
|
|
|
TextEditingController payAmount = TextEditingController();
|
|
|
|
TextEditingController payComment = TextEditingController();
|
2021-12-23 12:36:09 +01:00
|
|
|
num? balance;
|
2021-12-02 07:57:05 +01:00
|
|
|
int nRepositories = 20;
|
2021-12-02 07:23:12 +01:00
|
|
|
int nPage = 1;
|
2021-01-30 15:48:27 +01:00
|
|
|
|
2022-05-30 14:48:12 +02:00
|
|
|
Future<String> scan(context) async {
|
2021-12-20 21:33:03 +01:00
|
|
|
if (Platform.isAndroid || Platform.isIOS) {
|
|
|
|
await Permission.camera.request();
|
|
|
|
}
|
2022-06-01 23:59:56 +02:00
|
|
|
ScanResult? barcode;
|
2021-01-23 16:38:03 +01:00
|
|
|
try {
|
2022-06-01 23:59:56 +02:00
|
|
|
barcode = await BarcodeScanner.scan();
|
2021-02-18 04:42:57 +01:00
|
|
|
} catch (e) {
|
2021-04-02 12:05:37 +02:00
|
|
|
log.e(e);
|
2021-01-23 16:38:03 +01:00
|
|
|
return 'false';
|
|
|
|
}
|
2022-06-01 23:59:56 +02:00
|
|
|
if (isAddress(barcode.rawContent)) {
|
2021-11-30 01:25:48 +01:00
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(builder: (context) {
|
2022-06-01 23:59:56 +02:00
|
|
|
return WalletViewScreen(pubkey: barcode!.rawContent);
|
2021-11-30 01:25:48 +01:00
|
|
|
}),
|
|
|
|
);
|
2021-01-23 16:38:03 +01:00
|
|
|
} else {
|
|
|
|
return 'false';
|
|
|
|
}
|
2022-06-01 23:59:56 +02:00
|
|
|
return barcode.rawContent;
|
2021-01-23 16:38:03 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 13:44:22 +02:00
|
|
|
// Future<String> pay(BuildContext context, {int? derivation}) async {
|
|
|
|
// MyWalletsProvider _myWalletProvider =
|
|
|
|
// Provider.of<MyWalletsProvider>(context, listen: false);
|
|
|
|
// int? currentChest = configBox.get('currentChest');
|
|
|
|
// String result;
|
2021-12-03 10:25:36 +01:00
|
|
|
|
2022-05-19 13:44:22 +02:00
|
|
|
// derivation ??=
|
|
|
|
// _myWalletProvider.getDefaultWallet(currentChest)!.derivation!;
|
|
|
|
// result = await Gva(node: endPointGVA).pay(
|
|
|
|
// recipient: pubkey!,
|
|
|
|
// amount: double.parse(payAmount.text),
|
|
|
|
// mnemonic: _myWalletProvider.mnemonic,
|
|
|
|
// comment: payComment.text,
|
|
|
|
// derivation: derivation,
|
|
|
|
// lang: appLang);
|
2021-11-14 03:12:07 +01:00
|
|
|
|
2022-05-19 13:44:22 +02:00
|
|
|
// return result;
|
|
|
|
// }
|
2021-03-29 22:12:38 +02:00
|
|
|
|
2022-05-30 15:32:00 +02:00
|
|
|
bool isAddress(address) {
|
2021-11-14 19:21:20 +01:00
|
|
|
final RegExp regExp = RegExp(
|
2021-01-23 16:38:03 +01:00
|
|
|
r'^[a-zA-Z0-9]+$',
|
|
|
|
caseSensitive: false,
|
|
|
|
multiLine: false,
|
|
|
|
);
|
|
|
|
|
2022-05-30 15:32:00 +02:00
|
|
|
if (regExp.hasMatch(address) == true &&
|
2022-05-30 20:30:06 +02:00
|
|
|
address.length > 45 &&
|
|
|
|
address.length < 52) {
|
2022-05-30 15:32:00 +02:00
|
|
|
log.d("C'est une adresse !");
|
2021-01-23 16:38:03 +01:00
|
|
|
|
2022-05-30 15:32:00 +02:00
|
|
|
this.address = address;
|
2021-03-21 00:35:32 +01:00
|
|
|
|
2021-11-30 01:25:48 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2021-01-23 16:38:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 02:20:15 +01:00
|
|
|
|
2021-04-03 12:50:34 +02:00
|
|
|
// poka: Do99s6wQR2JLfhirPdpAERSjNbmjjECzGxHNJMiNKT3P
|
2021-02-06 08:31:14 +01:00
|
|
|
// Pi: D2meevcAHFTS2gQMvmRW5Hzi25jDdikk4nC4u1FkwRaU // For debug
|
2021-02-03 18:45:08 +01:00
|
|
|
// Boris: JE6mkuzSpT3ePciCPRTpuMT9fqPUVVLJz2618d33p7tn
|
2021-02-06 08:31:14 +01:00
|
|
|
// Matograine portefeuille: 9p5nHsES6xujFR7pw2yGy4PLKKHgWsMvsDHaHF64Uj25.
|
|
|
|
// Lion simone: 78jhpprYkMNF6i5kQPXfkAVBpd2aqcpieNsXTSW4c21f
|
2021-02-03 18:45:08 +01:00
|
|
|
|
|
|
|
List parseHistory(txs, _pubkey) {
|
2021-01-30 15:24:08 +01:00
|
|
|
var transBC = [];
|
|
|
|
int i = 0;
|
|
|
|
|
2021-11-14 19:21:20 +01:00
|
|
|
const currentBase = 0;
|
2021-01-30 15:24:08 +01:00
|
|
|
double currentUD = 10.54;
|
|
|
|
|
|
|
|
for (final trans in txs) {
|
|
|
|
var direction = trans['direction'];
|
|
|
|
final transaction = trans['node'];
|
2021-12-23 12:36:09 +01:00
|
|
|
String? output;
|
2021-02-03 20:59:12 +01:00
|
|
|
if (direction == "RECEIVED") {
|
|
|
|
for (String line in transaction['outputs']) {
|
|
|
|
if (line.contains(_pubkey)) {
|
|
|
|
output = line;
|
|
|
|
}
|
2021-02-03 18:45:08 +01:00
|
|
|
}
|
2021-02-03 20:59:12 +01:00
|
|
|
} else {
|
|
|
|
output = transaction['outputs'][0];
|
2021-02-03 18:45:08 +01:00
|
|
|
}
|
|
|
|
if (output == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-30 15:24:08 +01:00
|
|
|
|
|
|
|
transBC.add(i);
|
|
|
|
transBC[i] = [];
|
|
|
|
final dateBrut = DateTime.fromMillisecondsSinceEpoch(
|
|
|
|
transaction['writtenTime'] * 1000);
|
|
|
|
final DateFormat formatter = DateFormat('dd-MM-yy\nHH:mm');
|
|
|
|
final date = formatter.format(dateBrut);
|
|
|
|
transBC[i].add(transaction['writtenTime']);
|
|
|
|
transBC[i].add(date);
|
2021-03-19 06:33:48 +01:00
|
|
|
final int amountBrut = int.parse(output.split(':')[0]);
|
2021-01-30 15:24:08 +01:00
|
|
|
final base = int.parse(output.split(':')[1]);
|
|
|
|
final int applyBase = base - currentBase;
|
|
|
|
final num amount =
|
|
|
|
removeDecimalZero(amountBrut * pow(10, applyBase) / 100);
|
|
|
|
num amountUD = amount / currentUD;
|
|
|
|
if (direction == "RECEIVED") {
|
|
|
|
transBC[i].add(transaction['issuers'][0]);
|
2021-02-17 00:15:08 +01:00
|
|
|
transBC[i].add(getShortPubkey(transaction['issuers'][0]));
|
2021-01-30 15:24:08 +01:00
|
|
|
transBC[i].add(amount.toString());
|
|
|
|
transBC[i].add(amountUD.toStringAsFixed(2));
|
|
|
|
} else if (direction == "SENT") {
|
|
|
|
final outPubkey = output.split("SIG(")[1].replaceAll(')', '');
|
|
|
|
transBC[i].add(outPubkey);
|
2021-02-17 00:15:08 +01:00
|
|
|
transBC[i].add(getShortPubkey(outPubkey));
|
2021-01-30 15:24:08 +01:00
|
|
|
transBC[i].add('- ' + amount.toString());
|
|
|
|
transBC[i].add(amountUD.toStringAsFixed(2));
|
|
|
|
}
|
|
|
|
transBC[i].add(transaction['comment']);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return transBC;
|
|
|
|
}
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
FetchMoreOptions? checkQueryResult(result, opts, _pubkey) {
|
|
|
|
final List<dynamic>? blockchainTX =
|
|
|
|
(result.data['txsHistoryBc']['both']['edges'] as List<dynamic>?);
|
2021-12-03 10:25:36 +01:00
|
|
|
// final List<dynamic> mempoolTX =
|
|
|
|
// (result.data['txsHistoryMp']['receiving'] as List<dynamic>);
|
2021-01-30 15:48:27 +01:00
|
|
|
|
2021-02-07 16:57:50 +01:00
|
|
|
pageInfo = result.data['txsHistoryBc']['both']['pageInfo'];
|
2021-12-23 12:36:09 +01:00
|
|
|
fetchMoreCursor = pageInfo!['endCursor'];
|
2021-12-02 07:23:12 +01:00
|
|
|
if (fetchMoreCursor == null) nPage = 1;
|
|
|
|
|
|
|
|
if (nPage == 1) {
|
2021-12-02 07:57:05 +01:00
|
|
|
nRepositories = 40;
|
2021-12-02 07:23:12 +01:00
|
|
|
} else if (nPage == 2) {
|
|
|
|
nRepositories = 100;
|
|
|
|
}
|
|
|
|
nPage++;
|
|
|
|
|
2021-01-30 15:48:27 +01:00
|
|
|
if (fetchMoreCursor != null) {
|
|
|
|
opts = FetchMoreOptions(
|
2021-12-02 07:23:12 +01:00
|
|
|
variables: {'cursor': fetchMoreCursor, 'number': nRepositories},
|
2021-01-30 15:48:27 +01:00
|
|
|
updateQuery: (previousResultData, fetchMoreResultData) {
|
|
|
|
final List<dynamic> repos = [
|
2021-12-23 12:36:09 +01:00
|
|
|
...previousResultData!['txsHistoryBc']['both']['edges']
|
2021-01-30 15:48:27 +01:00
|
|
|
as List<dynamic>,
|
2021-12-23 12:36:09 +01:00
|
|
|
...fetchMoreResultData!['txsHistoryBc']['both']['edges']
|
2021-01-30 15:48:27 +01:00
|
|
|
as List<dynamic>
|
|
|
|
];
|
|
|
|
|
|
|
|
fetchMoreResultData['txsHistoryBc']['both']['edges'] = repos;
|
|
|
|
return fetchMoreResultData;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-02 12:05:37 +02:00
|
|
|
log.d(
|
2021-01-30 15:48:27 +01:00
|
|
|
"###### DEBUG H Parse blockchainTX list. Cursor: $fetchMoreCursor ######");
|
|
|
|
if (fetchMoreCursor != null) {
|
2021-02-03 18:45:08 +01:00
|
|
|
transBC = parseHistory(blockchainTX, _pubkey);
|
2021-01-30 15:48:27 +01:00
|
|
|
} else {
|
2021-04-02 12:05:37 +02:00
|
|
|
log.i("###### DEBUG H - Début de l'historique");
|
2021-01-30 15:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:24:08 +01:00
|
|
|
void resetdHistory() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
num removeDecimalZero(double n) {
|
2021-02-02 20:33:09 +01:00
|
|
|
String result = n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2);
|
2021-01-30 15:24:08 +01:00
|
|
|
return num.parse(result);
|
|
|
|
}
|
|
|
|
|
2021-03-21 00:35:32 +01:00
|
|
|
String generateIdenticon(String _pubkey) {
|
|
|
|
return Jdenticon.toSvg(_pubkey);
|
|
|
|
}
|
2021-11-30 10:28:13 +01:00
|
|
|
|
2021-12-01 08:14:07 +01:00
|
|
|
// Future<num> getBalance(String _pubkey) async {
|
|
|
|
// final url = Uri.parse(
|
|
|
|
// '$endPointGVA?query={%20balance(script:%20%22$_pubkey%22)%20{%20amount%20base%20}%20}');
|
|
|
|
// final response = await http.get(url);
|
|
|
|
// final result = json.decode(response.body);
|
|
|
|
|
|
|
|
// if (result['data']['balance'] == null) {
|
|
|
|
// balance = 0.0;
|
|
|
|
// } else {
|
|
|
|
// balance = removeDecimalZero(result['data']['balance']['amount'] / 100);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return balance;
|
|
|
|
// }
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
Future<num?> getBalance(String? _pubkey) async {
|
2021-12-01 08:14:07 +01:00
|
|
|
while (balance == null) {
|
|
|
|
await Future.delayed(const Duration(milliseconds: 50));
|
2021-11-30 10:28:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return balance;
|
|
|
|
}
|
2022-05-25 20:40:55 +02:00
|
|
|
|
|
|
|
void reload() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-01-23 16:38:03 +01:00
|
|
|
}
|
2022-05-21 06:47:26 +02:00
|
|
|
|
|
|
|
snackCopyKey(context) {
|
|
|
|
const snackBar = SnackBar(
|
|
|
|
padding: EdgeInsets.all(20),
|
2022-05-29 00:00:57 +02:00
|
|
|
content: Text("Cette adresse a été copié dans votre presse-papier.",
|
2022-05-21 06:47:26 +02:00
|
|
|
style: TextStyle(fontSize: 16)),
|
|
|
|
duration: Duration(seconds: 2));
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
|
|
|
}
|