WIP: refactor wallet format

This commit is contained in:
poka 2021-03-31 02:38:44 +02:00
parent d96b184e0a
commit e6ff487b3d
11 changed files with 126 additions and 116 deletions

View File

@ -1,4 +1,5 @@
import 'dart:io';
import 'package:gecko/models/myWallets.dart';
import 'package:shared_preferences/shared_preferences.dart';
// Files paths
@ -7,7 +8,7 @@ Directory walletsDirectory;
File defaultWalletFile;
File currentChestFile;
String defaultWallet;
WalletData defaultWallet;
String appVersion;
SharedPreferences prefs;
String endPointGVA;

View File

@ -49,9 +49,9 @@ class HistoryProvider with ChangeNotifier {
}
void pay(context, amount, comment) {
String dewif = File(walletsDirectory.path +
'${defaultWallet.split(':')[0]}/wallet.dewif')
.readAsLinesSync()[0];
String dewif =
File(walletsDirectory.path + '${defaultWallet.chest}/wallet.dewif')
.readAsLinesSync()[0];
DubpRust.simplePaymentFromTransparentAccount(
accountIndex: 0,
amount: 1,

View File

@ -6,8 +6,7 @@ import 'package:gecko/globals.dart';
import 'package:provider/provider.dart';
class MyWalletsProvider with ChangeNotifier {
String listWallets;
List<WalletData> listWallets = [];
Future initWalletFolder() async {
getDefaultWallet();
@ -49,9 +48,9 @@ class MyWalletsProvider with ChangeNotifier {
return false;
}
final String _walletList = getAllWalletsNames(0);
final List _walletList = readAllWallets(0);
if (_walletList == '') {
if (_walletList.isEmpty) {
print('No wallets detected');
return false;
} else {
@ -60,61 +59,55 @@ class MyWalletsProvider with ChangeNotifier {
}
}
String getAllWalletsNames(int _chest) {
if (listWallets != null && listWallets.isNotEmpty) {
listWallets = '';
}
if (listWallets == null) {
listWallets = '';
}
List readAllWallets(int _chest) {
print(walletsDirectory.path);
listWallets = [];
// int i = 0;
File _walletConfig = File('${walletsDirectory.path}/$_chest/list.conf');
_walletConfig.readAsLinesSync().forEach((element) {
if (listWallets != '') {
listWallets += '\n';
}
listWallets += element;
print(element);
listWallets.add(WalletData(element));
// listWallets += "${element.split(':')[0]}:${element.split(':')[1]}:${element.split(':')[2]}"
});
print(listWallets);
// listWallets.forEach((e) {
// print(e.name);
// });
// print(listWallets);
return listWallets;
}
int getDerivationNbr(String _id) {
WalletData getWalletData(String _id) {
int chest = int.parse(_id.split(':')[0]);
// int nbr = int.parse(_id.split(':')[1]);
final _walletConfig = File('${walletsDirectory.path}/$chest/list.conf');
int derivation;
_walletConfig.readAsLinesSync().forEach((element) {
if ("${element.split(':')[0]}:${element.split(':')[1]}" == _id) {
derivation = int.parse(element.split(':')[3]);
WalletData wallet = WalletData(element);
if (_id == "${wallet.chest}:${wallet.number}") {
return wallet;
}
});
return derivation;
return WalletData("0:0:Null:0");
}
void getDefaultWallet() {
defaultWalletFile = File('${appPath.path}/defaultWallet');
bool isdefaultWalletFile = defaultWalletFile.existsSync();
if (!isdefaultWalletFile) {
if (!defaultWalletFile.existsSync()) {
File(defaultWalletFile.path).createSync();
}
try {
defaultWallet = defaultWalletFile.readAsStringSync();
////////////////////////////////////////////////////////////
defaultWallet = getWalletData(defaultWalletFile.readAsStringSync());
print("found default wallet $defaultWallet");
} catch (e) {
defaultWallet = '0:0';
print("ERROR $e");
defaultWallet = WalletData('0:0:null:0');
}
if (defaultWallet == '') defaultWallet = '0:0';
}
Future<int> deleteAllWallet(context) async {
@ -159,7 +152,7 @@ class MyWalletsProvider with ChangeNotifier {
onPressed: () {
WidgetsBinding.instance.addPostFrameCallback((_) {
_myWalletProvider.listWallets =
_myWalletProvider.getAllWalletsNames(getCurrentChest());
_myWalletProvider.readAllWallets(getCurrentChest());
_myWalletProvider.rebuildWidget();
});
Navigator.pop(context, true);
@ -203,3 +196,37 @@ class MyWalletsProvider with ChangeNotifier {
notifyListeners();
}
}
// wallet data contains elements identifying wallet
class WalletData {
int chest;
int number;
String name;
int derivation;
// constructor from ':'-separated string
WalletData(String element) {
List parts = element.split(':');
this.chest = int.parse(parts[0]);
this.number = int.parse(parts[1]);
this.name = parts[2];
this.derivation = int.parse(parts[3]);
}
// representation of WalletData when debugging
@override
String toString() {
return this.name;
}
// creates the ':'-separated string from the WalletData
String inLine() {
return "${this.chest}:${this.number}:${this.name}:${this.derivation}";
}
// returns only the id part of the ':'-separated string
String id() {
return "${this.chest}:${this.number}";
}
}

View File

@ -1,3 +1,4 @@
// import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
@ -7,6 +8,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:gecko/globals.dart';
import 'package:gecko/models/myWallets.dart';
import 'package:image_picker/image_picker.dart';
import 'package:truncate/truncate.dart';
import 'package:qrscan/qrscan.dart' as scanner;
@ -74,15 +76,16 @@ class WalletOptionsProvider with ChangeNotifier {
}
Future readLocalWallet(
int _walletNbr, String _pin, int _pinLenght, int derivation) async {
context, WalletData _wallet, String _pin, int _pinLenght) async {
isWalletUnlock = false;
// MyWalletsProvider _myWalletProvider = MyWalletsProvider();
try {
File _walletFile = File('${walletsDirectory.path}/0/wallet.dewif');
String _localDewif = await _walletFile.readAsString();
String _localPubkey;
if ((_localPubkey = await _getPubkeyFromDewif(
_localDewif, _pin, _pinLenght, derivation)) !=
_localDewif, _pin, _pinLenght, _wallet.derivation)) !=
'false') {
this.pubkey.text = _localPubkey;
isWalletUnlock = true;
@ -136,27 +139,15 @@ class WalletOptionsProvider with ChangeNotifier {
String newConfig =
await _walletConfig.readAsLines().then((List<String> lines) {
int nbrLines = lines.length;
// print(lines);
// print(nbrLines);
// int _index = lines.indexOf('0:$_walletNbr:$_walletName:$_derivation');
if (nbrLines != 1) {
for (String wLine in lines) {
String wID = "${wLine.split(':')[0]}:${wLine.split(':')[1]}";
print(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
print(wLine);
String deri = wLine.split(':')[3];
print("($wID == $_walletID ???");
if (wID == _walletID) {
lines.remove(wLine);
lines.add('$_walletID:$_newName:$deri');
// return '$_walletID:$_newName:$deri';
print('OOUUUUUUUIIIIIIIIIIIIIIIIIII');
}
}
// lines.removeWhere((element) =>
// '${element.split(':')[0]}:${element.split(':')[1]}' == _walletID);
// lines.add('$_walletID:$_newName:$deri');
return lines.join('\n');
} else {
return 'true';
@ -229,17 +220,16 @@ class WalletOptionsProvider with ChangeNotifier {
return nameState;
}
Future<int> deleteWallet(context, _walletNbr, _name, _derivation) async {
final bool _answer = await _confirmDeletingWallet(context, _name);
Future<int> deleteWallet(context, wallet) async {
final bool _answer = await _confirmDeletingWallet(context, wallet.name);
if (_answer) {
final _walletConfig = File('${walletsDirectory.path}/0/list.conf');
if (_derivation != -1) {
if (wallet.derivation != -1) {
String newConfig =
await _walletConfig.readAsLines().then((List<String> lines) {
lines.removeWhere((element) =>
element.contains('0:$_walletNbr:$_name:$_derivation'));
lines.removeWhere((element) => element.contains(wallet.inLine()));
return lines.join('\n');
});
@ -247,7 +237,8 @@ class WalletOptionsProvider with ChangeNotifier {
await _walletConfig.delete();
await _walletConfig.writeAsString(newConfig);
} else {
final _walletFile = Directory('${walletsDirectory.path}/$_walletNbr');
final _walletFile =
Directory('${walletsDirectory.path}/${wallet.number}');
await _walletFile.delete(recursive: true);
}
Navigator.popUntil(

View File

@ -384,8 +384,7 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier {
// _historyProvider.pay(payAmount.text, payComment.text);
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return UnlockingWallet(
walletNbr: int.parse(defaultWallet));
return UnlockingWallet(wallet: defaultWallet);
}));
},
child: Padding(

View File

@ -138,8 +138,8 @@ class ConfirmStoreWallet extends StatelessWidget with ChangeNotifier {
WidgetsBinding.instance
.addPostFrameCallback((_) {
_myWalletProvider.listWallets =
_myWalletProvider.getAllWalletsNames(
_currentChest);
_myWalletProvider
.readAllWallets(_currentChest);
_myWalletProvider.rebuildWidget();
});
}

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:dubp/dubp.dart';
import 'package:flutter/services.dart';
import 'package:gecko/models/myWallets.dart';
import 'package:gecko/models/walletOptions.dart';
import 'package:gecko/screens/commonElements.dart';
import 'package:flutter/material.dart';
@ -15,11 +16,9 @@ import 'package:provider/provider.dart';
class UnlockingWallet extends StatelessWidget {
UnlockingWallet({
Key keyUnlockWallet,
@required this.walletNbr,
@required this.wallet,
}) : super(key: keyUnlockWallet);
int walletNbr;
String walletName;
int derivation;
WalletData wallet;
// ignore: close_sinks
StreamController<ErrorAnimationType> errorController;
@ -35,7 +34,7 @@ class UnlockingWallet extends StatelessWidget {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
WalletOptionsProvider _walletOptions =
Provider.of<WalletOptionsProvider>(context);
final int _pinLenght = _walletOptions.getPinLenght(this.walletNbr);
final int _pinLenght = _walletOptions.getPinLenght(this.wallet.number);
errorController = StreamController<ErrorAnimationType>();
return Scaffold(
@ -55,7 +54,7 @@ class UnlockingWallet extends StatelessWidget {
fontWeight: FontWeight.w400),
),
SizedBox(height: 50),
pinForm(context, _pinLenght, walletNbr, derivation),
pinForm(context, _pinLenght, wallet.number, wallet.derivation),
]),
),
GestureDetector(
@ -128,10 +127,7 @@ class UnlockingWallet extends StatelessWidget {
onCompleted: (_pin) async {
print("Completed");
final resultWallet = await _walletOptions.readLocalWallet(
this.walletNbr,
_pin.toUpperCase(),
_pinLenght,
this.derivation);
context, this.wallet, _pin.toUpperCase(), _pinLenght);
if (resultWallet == 'bad') {
errorController.add(ErrorAnimationType
.shake); // Triggering error shake animation
@ -144,7 +140,9 @@ class UnlockingWallet extends StatelessWidget {
Navigator.push(
formKey.currentContext,
SmoothTransition(
page: WalletOptions(walletNbr: walletNbr)));
page: WalletOptions(
wallet: wallet,
)));
}
},
onChanged: (value) {

View File

@ -111,7 +111,7 @@ class WalletOptionsOld extends StatelessWidget with ChangeNotifier {
_myWalletProvider
.listWallets =
_myWalletProvider
.getAllWalletsNames(
.readAllWallets(
_currentChest);
_myWalletProvider
.rebuildWidget();
@ -137,13 +137,15 @@ class WalletOptionsOld extends StatelessWidget with ChangeNotifier {
onPrimary: Colors.black, // foreground
),
onPressed: () async {
await _walletOptions.deleteWallet(context,
walletNbr, walletName, derivation);
await _walletOptions.deleteWallet(
context,
_myWalletProvider.getWalletData(
_walletOptions.walletID));
WidgetsBinding.instance
.addPostFrameCallback((_) {
_myWalletProvider.listWallets =
_myWalletProvider
.getAllWalletsNames(_currentChest);
.readAllWallets(_currentChest);
_myWalletProvider.rebuildWidget();
});
},

View File

@ -13,11 +13,9 @@ import 'package:flutter/services.dart';
// ignore: must_be_immutable
class WalletOptions extends StatelessWidget {
WalletOptions({Key keyMyWallets, @required this.walletNbr})
WalletOptions({Key keyMyWallets, @required this.wallet})
: super(key: keyMyWallets);
int walletNbr;
String walletName;
int derivation;
WalletData wallet;
int _nbrLinesName = 1;
bool _isNewNameValid = false;
@ -37,9 +35,9 @@ class WalletOptions extends StatelessWidget {
if (_walletOptions.nameController.text == null ||
_isNewNameValid == false) {
_walletOptions.nameController.text = walletName;
_walletOptions.nameController.text = wallet.name;
} else {
walletName = _walletOptions.nameController.text;
wallet.name = _walletOptions.nameController.text;
}
_walletOptions.nameController.text.length >= 15
@ -48,13 +46,17 @@ class WalletOptions extends StatelessWidget {
if (_walletOptions.nameController.text.length >= 26 && isTall)
_nbrLinesName = 3;
_walletOptions.walletID = '0:$walletNbr';
_walletOptions.walletID = '0:${wallet.number}';
_myWalletProvider.getDefaultWallet();
defaultWallet == _walletOptions.walletID
? _walletOptions.isDefaultWallet = true
: _walletOptions.isDefaultWallet = false;
_walletOptions.isDefaultWallet =
(defaultWallet.id() == _walletOptions.walletID);
int currentChest = _myWalletProvider.getCurrentChest();
print('roooooooooooooo');
print("$currentChest:${wallet.number}");
// print(_walletOptions.generateQRcode(_walletOptions.pubkey.text));
@ -248,7 +250,7 @@ class WalletOptions extends StatelessWidget {
// .addPostFrameCallback((_) {
// _myWalletProvider.listWallets =
// _myWalletProvider
// .getAllWalletsNames(
// .readAllWallets(
// _currentChest);
// _myWalletProvider.rebuildWidget();
// });
@ -365,10 +367,9 @@ class WalletOptions extends StatelessWidget {
InkWell(
onTap: !_walletOptions.isDefaultWallet
? () {
defaultWallet = '0:$walletNbr';
_walletOptions
.defAsDefaultWallet(_walletOptions.walletID);
_myWalletProvider.getAllWalletsNames(_currentChest);
defaultWallet = wallet;
_walletOptions.defAsDefaultWallet(wallet.id());
_myWalletProvider.readAllWallets(_currentChest);
}
: null,
child: SizedBox(
@ -395,11 +396,10 @@ class WalletOptions extends StatelessWidget {
SizedBox(height: 17 * ratio),
InkWell(
onTap: () async {
await _walletOptions.deleteWallet(
context, walletNbr, walletName, derivation);
await _walletOptions.deleteWallet(context, wallet);
WidgetsBinding.instance.addPostFrameCallback((_) {
_myWalletProvider.listWallets =
_myWalletProvider.getAllWalletsNames(_currentChest);
_myWalletProvider.readAllWallets(_currentChest);
_myWalletProvider.rebuildWidget();
});
},

View File

@ -24,12 +24,11 @@ class WalletsHome extends StatelessWidget {
final int _currentChest = myWalletProvider.getCurrentChest();
myWalletProvider.listWallets =
myWalletProvider.getAllWalletsNames(_currentChest);
myWalletProvider.readAllWallets(_currentChest);
final bool isWalletsExists = myWalletProvider.checkIfWalletExist();
if (myWalletProvider.listWallets != '') {
firstWalletDerivation =
int.parse(myWalletProvider.listWallets.split('\n')[0].split(':')[3]);
if (myWalletProvider.listWallets.isEmpty) {
firstWalletDerivation = myWalletProvider.listWallets[0].derivation;
myWalletProvider.getDefaultWallet();
}
@ -77,7 +76,7 @@ class WalletsHome extends StatelessWidget {
return Text('');
}
if (_myWalletProvider.listWallets == '') {
if (_myWalletProvider.listWallets.isEmpty) {
return Expanded(
child: Column(children: <Widget>[
Center(
@ -88,7 +87,7 @@ class WalletsHome extends StatelessWidget {
]));
}
List _listWallets = _myWalletProvider.listWallets.split('\n');
List _listWallets = _myWalletProvider.listWallets;
// final int nbrOfWallets = _listWallets.length;
// print(_listWallets);
// print("${_listWallets[0].split(':')[0]}:${_listWallets[0].split(':')[2]}");
@ -100,15 +99,14 @@ class WalletsHome extends StatelessWidget {
crossAxisSpacing: 0,
mainAxisSpacing: 0,
children: <Widget>[
for (String _repository in _listWallets)
for (WalletData _repository in _listWallets)
Padding(
padding: EdgeInsets.all(16),
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return UnlockingWallet(
walletNbr: int.parse(_repository.split(':')[1]));
return UnlockingWallet(wallet: _repository);
}));
},
child: ClipRRect(
@ -135,11 +133,9 @@ class WalletsHome extends StatelessWidget {
)),
ListTile(
// contentPadding: const EdgeInsets.only(left: 7.0),
tileColor:
"${_repository.split(':')[0]}:${_repository.split(':')[1]}" ==
defaultWallet
? Color(0xffD28928)
: Color(0xffFFD58D),
tileColor: _repository == defaultWallet
? Color(0xffD28928)
: Color(0xffFFD58D),
// leading: Text('IMAGE'),
// subtitle: Text(_repository.split(':')[3],
@ -148,22 +144,18 @@ class WalletsHome extends StatelessWidget {
child: Padding(
padding:
EdgeInsets.symmetric(horizontal: 5),
child: Text(_repository.split(':')[2],
child: Text(_repository.name,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16.0,
color:
"${_repository.split(':')[0]}:${_repository.split(':')[1]}" ==
defaultWallet
? Color(0xffF9F9F1)
: Colors.black)))),
color: _repository == defaultWallet
? Color(0xffF9F9F1)
: Colors.black)))),
// dense: true,
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return UnlockingWallet(
walletNbr:
int.parse(_repository.split(':')[1]));
return UnlockingWallet(wallet: _repository);
}));
},
)

View File

@ -120,7 +120,7 @@ class OnboardingStepFourteen extends StatelessWidget {
print(generatedWallet.pin);
await _generateWalletProvider.storeHDWChest(
generatedWallet, 'Mon portefeuille courant', context);
_myWalletProvider.getAllWalletsNames(_currentChest);
_myWalletProvider.readAllWallets(_currentChest);
_walletOptions.reloadBuild();
_myWalletProvider.rebuildWidget();
Navigator.push(