Skip to content
Snippets Groups Projects
walletOptions.dart 7.38 KiB
import 'dart:io';
import 'package:dubp/dubp.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:sentry/sentry.dart' as sentry;
import 'package:gecko/globals.dart';

class WalletOptionsProvider with ChangeNotifier {
  TextEditingController pubkey = new TextEditingController();
  TextEditingController _newWalletName = new TextEditingController();
  bool isWalletUnlock = false;
  bool ischangedPin = false;
  TextEditingController newPin = new TextEditingController();

  Future<NewWallet> get badWallet => null;

  Future _getPubkeyFromDewif(
      String _dewif, _pin, int _pinLenght, int derivation) async {
    String _pubkey;
    RegExp regExp = new RegExp(
      r'^[A-Z0-9]+$',
      caseSensitive: false,
      multiLine: false,
    );

    if (regExp.hasMatch(_pin) == true && _pin.length == _pinLenght) {
      print("Le format du code PIN est correct.");
    } else {
      print('Format de code PIN invalide');
      return 'false';
    }
    try {
      List _pubkeysTmp = await DubpRust.getBip32DewifAccountsPublicKeys(
          dewif: _dewif, secretCode: _pin, accountsIndex: [derivation]);
      _pubkey = _pubkeysTmp[0];
      this.pubkey.text = _pubkey;
      notifyListeners();

      return _pubkey;
    } catch (e, stack) {
      print('Bad PIN code !');
      print(e);
      if (kReleaseMode) {
        await sentry.Sentry.captureException(
          e,
          stackTrace: stack,
        );
      }
      notifyListeners();

      return 'false';
    }
  }

  Future readLocalWallet(int _walletNbr, String _name, String _pin,
      int _pinLenght, int derivation) async {
    isWalletUnlock = false;
    try {
      File _walletFile =
          File('${walletsDirectory.path}/$_walletNbr/wallet.dewif');
      String _localDewif = await _walletFile.readAsString();
      String _localPubkey;

      if ((_localPubkey = await _getPubkeyFromDewif(
              _localDewif, _pin, _pinLenght, derivation)) !=
          'false') {
        this.pubkey.text = _localPubkey;
        isWalletUnlock = true;
        notifyListeners();