2021-03-29 22:12:38 +02:00
|
|
|
import 'package:dubp/dubp.dart';
|
2021-01-23 16:38:03 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-01-30 19:00:31 +01:00
|
|
|
import 'package:gecko/globals.dart';
|
2021-11-14 19:21:20 +01:00
|
|
|
import 'package:gecko/models/my_wallets.dart';
|
|
|
|
import 'package:gecko/models/wallet_data.dart';
|
2021-02-22 08:49:29 +01:00
|
|
|
import 'package:gecko/screens/history.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';
|
|
|
|
import 'package:qrscan/qrscan.dart' as scanner;
|
2021-01-30 15:24:08 +01:00
|
|
|
import 'dart:math';
|
|
|
|
import 'package:intl/intl.dart';
|
2021-02-16 03:55:01 +01:00
|
|
|
import 'package:truncate/truncate.dart';
|
2021-02-16 17:40:25 +01:00
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
import 'package:fast_base58/fast_base58.dart';
|
2021-01-23 16:38:03 +01:00
|
|
|
|
|
|
|
class HistoryProvider with ChangeNotifier {
|
|
|
|
String pubkey = '';
|
2021-02-16 03:55:01 +01:00
|
|
|
String pubkeyShort = '';
|
2021-01-24 08:53:03 +01:00
|
|
|
HistoryProvider(this.pubkey);
|
2021-02-07 02:59:48 +01:00
|
|
|
final TextEditingController outputPubkey = TextEditingController();
|
2021-01-30 15:48:27 +01:00
|
|
|
List transBC;
|
2021-02-07 02:59:48 +01:00
|
|
|
String fetchMoreCursor;
|
2021-02-07 16:57:50 +01:00
|
|
|
Map pageInfo;
|
2021-02-17 00:15:08 +01:00
|
|
|
bool isHistoryScreen = false;
|
|
|
|
String historySwitchButtun = "Voir l'historique";
|
2021-03-21 00:35:32 +01:00
|
|
|
String rawSvg;
|
2021-04-03 00:07:03 +02:00
|
|
|
TextEditingController payAmount = TextEditingController();
|
|
|
|
TextEditingController payComment = TextEditingController();
|
2021-01-30 15:48:27 +01:00
|
|
|
|
2021-02-18 07:54:35 +01:00
|
|
|
Future scan(context) async {
|
2021-01-23 16:38:03 +01:00
|
|
|
await Permission.camera.request();
|
|
|
|
String barcode;
|
|
|
|
try {
|
|
|
|
barcode = await scanner.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';
|
|
|
|
}
|
|
|
|
if (barcode != null) {
|
2021-11-14 19:21:20 +01:00
|
|
|
outputPubkey.text = barcode;
|
2021-02-18 07:54:35 +01:00
|
|
|
isPubkey(context, barcode);
|
2021-01-23 16:38:03 +01:00
|
|
|
} else {
|
|
|
|
return 'false';
|
|
|
|
}
|
|
|
|
return barcode;
|
|
|
|
}
|
|
|
|
|
2021-04-03 12:50:34 +02:00
|
|
|
Future<String> pay(BuildContext context, String pinCode) async {
|
2021-11-14 03:12:07 +01:00
|
|
|
MyWalletsProvider _myWalletModel = MyWalletsProvider();
|
|
|
|
int currentChest = configBox.get('currentChest');
|
|
|
|
WalletData defaultWallet = _myWalletModel.getDefaultWallet(currentChest);
|
|
|
|
|
|
|
|
String dewif = chestBox.get(currentChest).dewif;
|
|
|
|
|
2021-04-03 00:07:03 +02:00
|
|
|
try {
|
2021-04-03 12:50:34 +02:00
|
|
|
await DubpRust.simplePaymentFromTransparentAccount(
|
2021-04-03 00:07:03 +02:00
|
|
|
accountIndex: defaultWallet.derivation,
|
|
|
|
amount: double.parse(payAmount.text),
|
|
|
|
txComment: payComment.text,
|
|
|
|
dewif: dewif,
|
|
|
|
gvaEndpoint: endPointGVA,
|
|
|
|
secretCode: pinCode,
|
|
|
|
recipient: pubkey);
|
2021-04-03 12:50:34 +02:00
|
|
|
return "Success";
|
2021-04-03 00:07:03 +02:00
|
|
|
} catch (e) {
|
|
|
|
log.e("ERROR DUBP PAYMENTS");
|
|
|
|
log.e(e);
|
2021-04-03 12:50:34 +02:00
|
|
|
return "Payments errors: $e";
|
2021-04-03 00:07:03 +02:00
|
|
|
}
|
2021-03-29 22:12:38 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 00:35:32 +01:00
|
|
|
String isPubkey(context, pubkey, {bool goHistory}) {
|
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,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (regExp.hasMatch(pubkey) == true &&
|
|
|
|
pubkey.length > 42 &&
|
|
|
|
pubkey.length < 45) {
|
2021-04-02 12:05:37 +02:00
|
|
|
log.d("C'est une pubkey !!!");
|
2021-01-23 16:38:03 +01:00
|
|
|
|
|
|
|
this.pubkey = pubkey;
|
2021-02-17 00:15:08 +01:00
|
|
|
getShortPubkey(pubkey);
|
2021-02-16 17:40:25 +01:00
|
|
|
|
2021-11-14 19:21:20 +01:00
|
|
|
outputPubkey.text = pubkey;
|
2021-02-17 00:15:08 +01:00
|
|
|
|
2021-11-14 19:21:20 +01:00
|
|
|
goHistory ??= false;
|
2021-03-21 00:35:32 +01:00
|
|
|
|
|
|
|
if (goHistory) {
|
|
|
|
isHistoryScreen = true;
|
|
|
|
historySwitchButtun = "Payer";
|
|
|
|
} else {
|
|
|
|
isHistoryScreen = false;
|
|
|
|
historySwitchButtun = "Voir l'historique";
|
|
|
|
}
|
|
|
|
|
2021-02-22 08:49:29 +01:00
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(builder: (context) {
|
|
|
|
return HistoryScreen();
|
|
|
|
}),
|
|
|
|
);
|
2021-01-23 16:38:03 +01:00
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
return pubkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2021-01-29 02:20:15 +01:00
|
|
|
|
2021-02-17 00:15:08 +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);
|
|
|
|
|
|
|
|
pubkeyShort = truncate(pubkey, 5,
|
|
|
|
omission: String.fromCharCode(0x2026),
|
|
|
|
position: TruncatePosition.end) +
|
|
|
|
truncate(pubkey, 4, omission: "", position: TruncatePosition.start) +
|
|
|
|
':$pubkeyChecksumShort';
|
|
|
|
|
|
|
|
return pubkeyShort;
|
|
|
|
}
|
|
|
|
|
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-11-14 19:21:20 +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-02-03 18:45:08 +01:00
|
|
|
FetchMoreOptions checkQueryResult(result, opts, _pubkey) {
|
2021-01-30 15:48:27 +01:00
|
|
|
final List<dynamic> blockchainTX =
|
|
|
|
(result.data['txsHistoryBc']['both']['edges'] as List<dynamic>);
|
|
|
|
|
2021-02-07 16:57:50 +01:00
|
|
|
pageInfo = result.data['txsHistoryBc']['both']['pageInfo'];
|
2021-01-30 15:48:27 +01:00
|
|
|
|
2021-02-07 02:59:48 +01:00
|
|
|
fetchMoreCursor = pageInfo['endCursor'];
|
2021-01-30 15:48:27 +01:00
|
|
|
if (fetchMoreCursor != null) {
|
|
|
|
opts = FetchMoreOptions(
|
|
|
|
variables: {'cursor': fetchMoreCursor},
|
|
|
|
updateQuery: (previousResultData, fetchMoreResultData) {
|
|
|
|
final List<dynamic> repos = [
|
|
|
|
...previousResultData['txsHistoryBc']['both']['edges']
|
|
|
|
as List<dynamic>,
|
|
|
|
...fetchMoreResultData['txsHistoryBc']['both']['edges']
|
|
|
|
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() {
|
2021-11-14 19:21:20 +01:00
|
|
|
outputPubkey.text = '';
|
2021-01-30 15:24:08 +01:00
|
|
|
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-02-16 17:40:25 +01:00
|
|
|
snackCopyKey(context) {
|
2021-11-14 19:21:20 +01:00
|
|
|
const snackBar = SnackBar(
|
2021-02-16 17:40:25 +01:00
|
|
|
content:
|
|
|
|
Text("Cette clé publique a été copié dans votre presse-papier."),
|
|
|
|
duration: Duration(seconds: 2));
|
2021-03-08 11:11:01 +01:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
2021-02-16 17:40:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 00:15:08 +01:00
|
|
|
void switchProfileView() {
|
|
|
|
isHistoryScreen = !isHistoryScreen;
|
|
|
|
if (isHistoryScreen) {
|
|
|
|
historySwitchButtun = "Payer";
|
|
|
|
} else {
|
|
|
|
historySwitchButtun = "Voir l'historique";
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-03-21 00:35:32 +01:00
|
|
|
String generateIdenticon(String _pubkey) {
|
|
|
|
return Jdenticon.toSvg(_pubkey);
|
|
|
|
}
|
2021-01-23 16:38:03 +01:00
|
|
|
}
|