Skip to content
Snippets Groups Projects
substrate_sdk.dart 22.6 KiB
Newer Older
Hugo Trentesaux's avatar
Hugo Trentesaux committed
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
poka's avatar
poka committed
import 'package:flutter/services.dart';
import 'package:gecko/globals.dart';
poka's avatar
poka committed
import 'package:gecko/models/chest_data.dart';
import 'package:gecko/models/wallet_data.dart';
import 'package:gecko/providers/home.dart';
import 'package:gecko/providers/my_wallets.dart';
poka's avatar
poka committed
import 'package:polkawallet_sdk/api/apiKeyring.dart';
import 'package:polkawallet_sdk/api/types/networkParams.dart';
poka's avatar
poka committed
import 'package:polkawallet_sdk/api/types/txInfoData.dart';
import 'package:polkawallet_sdk/polkawallet_sdk.dart';
import 'package:polkawallet_sdk/storage/keyring.dart';
poka's avatar
poka committed
import 'package:polkawallet_sdk/storage/types/keyPairData.dart';
import 'package:provider/provider.dart';
poka's avatar
poka committed
import 'package:truncate/truncate.dart';
// import 'package:web_socket_channel/io.dart';

class SubstrateSdk with ChangeNotifier {
  final WalletSDK sdk = WalletSDK();
  final Keyring keyring = Keyring();
poka's avatar
poka committed
  String generatedMnemonic = '';
poka's avatar
poka committed
  bool sdkLoading = false;
poka's avatar
poka committed
  bool importIsLoading = false;
  bool isLoadingEndpoint = false;
poka's avatar
poka committed
  String debugConnection = '';
  String transactionStatus = '';
poka's avatar
poka committed
  TextEditingController jsonKeystore = TextEditingController();
  TextEditingController keystorePassword = TextEditingController();

poka's avatar
poka committed
    sdkLoading = true;
poka's avatar
poka committed
    await keyring.init([ss58]);
    keyring.setSS58(ss58);

    await sdk.init(keyring);
    sdkReady = true;
poka's avatar
poka committed
    sdkLoading = false;
  Future<void> connectNode(BuildContext ctx) async {
    HomeProvider homeProvider = Provider.of<HomeProvider>(ctx, listen: false);
    // var connectivityResult = await (Connectivity().checkConnectivity());
    // if (connectivityResult == ConnectivityResult.mobile ||
    //     connectivityResult == ConnectivityResult.wifi) {
    //   _homeProvider.changeMessage("Vous n'êtes pas connecté à internet", 0);
    //   return;
    // }
    homeProvider.changeMessage("connectionPending".tr(), 0);
poka's avatar
poka committed

poka's avatar
poka committed
    // configBox.delete('customEndpoint');
    final List<NetworkParams> listEndpoints =
        configBox.containsKey('customEndpoint')
            ? [getDuniterCustomEndpoint()]
            : getDuniterBootstrap();

    // final nodes = getDuniterBootstrap();

poka's avatar
poka committed
    int timeout = 10000;

    // if (n.endpoint!.startsWith('ws://')) {
    //   timeout = 5000;
    // }

    //// Check websocket conenction - only for wss
    // final channel = IOWebSocketChannel.connect(
    //   Uri.parse('wss://192.168.1.72:9944'),
    // );

    // channel.stream.listen(
    //   (dynamic message) {
    //     log.d('message $message');
    //   },
    //   onDone: () {
    //     log.d('ws channel closed');
    //   },
    //   onError: (error) {
    //     log.d('ws error $error');
    //   },
    // );

    if (sdk.api.connectedNode?.endpoint != null) {
      await sdk.api.setting.unsubscribeBestNumber();
poka's avatar
poka committed
    }

    isLoadingEndpoint = true;
    notifyListeners();
poka's avatar
poka committed
    final res = await sdk.api.connectNode(keyring, listEndpoints).timeout(
          Duration(milliseconds: timeout),
poka's avatar
poka committed
          onTimeout: () => null,
        );
    isLoadingEndpoint = false;
    notifyListeners();
    if (res != null) {
      nodeConnected = true;
poka's avatar
poka committed

      // Subscribe bloc number
      sdk.api.setting.subscribeBestNumber((res) {
        blocNumber = int.parse(res.toString());
        // log.d(sdk.api.connectedNode?.endpoint);
poka's avatar
poka committed
        if (sdk.api.connectedNode?.endpoint == null) {
          homeProvider.changeMessage("networkLost".tr(), 0);
poka's avatar
poka committed
        }
poka's avatar
poka committed
        notifyListeners();
      });
poka's avatar
poka committed
      // currencyName = await getCurencyName();
      homeProvider.changeMessage(
poka's avatar
poka committed
          "wellConnectedToNode"
              .tr(args: [getConnectedEndpoint()!.split('/')[2]]),
poka's avatar
poka committed
          5);
      // snackNode(ctx, true);
    } else {
      nodeConnected = false;
poka's avatar
poka committed
      debugConnection = res.toString();
      notifyListeners();
      homeProvider.changeMessage("noDuniterEndointAvailable".tr(), 0);
      // snackNode(ctx, false);
    log.d(sdk.api.connectedNode?.endpoint);
poka's avatar
poka committed

poka's avatar
poka committed
  List<NetworkParams> getDuniterBootstrap() {
    List<NetworkParams> node = [];

    for (String endpoint in configBox.get('endpoint')) {
poka's avatar
poka committed
      final n = NetworkParams();
      n.name = currencyName;
      n.endpoint = endpoint;
poka's avatar
poka committed
      n.ss58 = ss58;
      node.add(n);
    }
    return node;
  }

  NetworkParams getDuniterCustomEndpoint() {
    final nodeParams = NetworkParams();
    nodeParams.name = currencyName;
    nodeParams.endpoint = configBox.get('customEndpoint');
    nodeParams.ss58 = ss58;
    return nodeParams;
  }

poka's avatar
poka committed
  Future<String> importAccount(
      {String mnemonic = '',
      bool fromMnemonic = false,
      String derivePath = '',
      String password = ''}) async {
poka's avatar
poka committed
    // toy exercise immense month enter answer table prefer speed cycle gold phone
    final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
poka's avatar
poka committed
    if (mnemonic != '') {
      fromMnemonic = true;
      generatedMnemonic = mnemonic;
    } else if (clipboardData!.text!.split(' ').length == 12) {
poka's avatar
poka committed
      fromMnemonic = true;
      generatedMnemonic = clipboardData.text!;
    }

    if (password == '') {
      password = keystorePassword.text;
    }

poka's avatar
poka committed
    final KeyType keytype;
    final String keyToImport;
    if (fromMnemonic) {
      keytype = KeyType.mnemonic;
      keyToImport = generatedMnemonic;
    } else {
      keytype = KeyType.keystore;
      keyToImport = jsonKeystore.text.replaceAll("'", "\\'");
    }

poka's avatar
poka committed
    importIsLoading = true;
    notifyListeners();
poka's avatar
poka committed
    if (clipboardData?.text != null) jsonKeystore.text = clipboardData!.text!;
    var json = await sdk.api.keyring
        .importAccount(keyring,
            keyType: keytype,
            key: keyToImport,
            name: derivePath,
            password: password,
poka's avatar
poka committed
            derivePath: derivePath,
            cryptoType: CryptoType.sr25519)
poka's avatar
poka committed
        .catchError((e) {
      importIsLoading = false;
      notifyListeners();
    });
poka's avatar
poka committed
    if (json == null) return '';
    log.d(json);
poka's avatar
poka committed
    try {
      await sdk.api.keyring.addAccount(
poka's avatar
poka committed
        keyring,
poka's avatar
poka committed
        keyType: keytype,
poka's avatar
poka committed
        acc: json,
        password: password,
poka's avatar
poka committed
      );
poka's avatar
poka committed
      // Clipboard.setData(ClipboardData(text: jsonEncode(acc.toJson())));
poka's avatar
poka committed
    } catch (e) {
poka's avatar
poka committed
      importIsLoading = false;
      notifyListeners();
poka's avatar
poka committed
    }
poka's avatar
poka committed

poka's avatar
poka committed
    importIsLoading = false;
poka's avatar
poka committed
    notifyListeners();
    return keyring.allAccounts.last.address!;
poka's avatar
poka committed
  }

  void reload() {
    notifyListeners();
  }
poka's avatar
poka committed

poka's avatar
poka committed
  Future<List<AddressInfo>> getKeyStoreAddress() async {
    List<AddressInfo> result = [];
poka's avatar
poka committed

poka's avatar
poka committed
    // sdk.api.account.unsubscribeBalance();
    for (var element in keyring.allAccounts) {
poka's avatar
poka committed
      // Clipboard.setData(ClipboardData(text: jsonEncode(element)));
poka's avatar
poka committed
      final account = AddressInfo(address: element.address);
      // await sdk.api.account.subscribeBalance(element.address, (p0) {
      //   account.balance = int.parse(p0.freeBalance) / 100;
      // });
      // sdk.api.setting.unsubscribeBestNumber();
      account.balance = await getBalance(element.address!);
poka's avatar
poka committed
      result.add(account);
poka's avatar
poka committed
    }

    return result;
  }

poka's avatar
poka committed
  Future<List<int>> getCerts(String address) async {
    final idtyIndex = await sdk.webView!
        .evalJavascript('api.query.identity.identityIndexOf("$address")');
poka's avatar
poka committed
    // log.d('u32: ' + idtyIndex.toString());
    final certsReceiver = await sdk.webView!
poka's avatar
poka committed
            .evalJavascript('api.query.cert.storageIdtyCertMeta($idtyIndex)') ??
        [];

    return [certsReceiver['receivedCount'], certsReceiver['issuedCount']];
  Future<Map> getCertData(String from, String to) async {
    final idtyIndexFrom = await sdk.webView!
        .evalJavascript('api.query.identity.identityIndexOf("$from")');

    final idtyIndexTo = await sdk.webView!
        .evalJavascript('api.query.identity.identityIndexOf("$to")');

    if (idtyIndexFrom == null || idtyIndexTo == null) return {};

    final certData = await sdk.webView!.evalJavascript(
            'api.query.cert.storageCertsByIssuer($idtyIndexFrom, $idtyIndexTo)') ??
        '';

    if (certData == '') return {};
poka's avatar
poka committed
    // log.d(_certData);
    return certData;
  Future<bool> hasAccountConsumers(String address) async {
    final accountInfo = await sdk.webView!
        .evalJavascript('api.query.system.account("$address")');
    final consumers = accountInfo['consumers'];
    // log.d('Consumers: $_consumers');
    return consumers == 0 ? false : true;
  Future<double> getBalance(String address, {bool isUd = false}) async {
    double balance = 0.0;
    // log.d('nodeConnected: ' + nodeConnected.toString());
    if (nodeConnected) {
      final brutBalance = await sdk.api.account.queryBalance(address);
      balance = int.parse(brutBalance!.freeBalance) / 100;
poka's avatar
poka committed
  Future<double> subscribeBalance(String address, {bool isUd = false}) async {
    double balance = 0.0;
    if (nodeConnected) {
      await sdk.api.account.subscribeBalance(address, (balanceData) {
        balance = int.parse(balanceData.freeBalance) / 100;
poka's avatar
poka committed
        notifyListeners();
      });
    }

    return balance;
  }

  KeyPairData getKeypair(String address) {
    return keyring.keyPairs.firstWhere((kp) => kp.address == address,
        orElse: (() => KeyPairData()));
  }

  Future<bool> checkPassword(String address, String pass) async {
    final account = getKeypair(address);
    return await sdk.api.keyring.checkPassword(account, pass);
  }

  Future<String> getSeed(String address, String pin) async {
poka's avatar
poka committed
    final account = getKeypair(address);
    keyring.setCurrent(account);

    final seed = await sdk.api.keyring.getDecryptedSeed(keyring, pin);
    String seedText;
    if (seed == null) {
      seedText = '';
poka's avatar
poka committed
    } else {
      seedText = seed.seed!.split('//')[0];
    log.d(seedText);
    return seedText;
  int getDerivationNumber(String address) {
    final account = getKeypair(address);
    final deriveNbr = account.name!.split('//')[1];
    return int.parse(deriveNbr);
  }

  Future<KeyPairData?> changePassword(BuildContext context, String address,
      String passOld, String? passNew) async {
    final account = getKeypair(address);
    MyWalletsProvider myWalletProvider =
        Provider.of<MyWalletsProvider>(context, listen: false);
    keyring.setCurrent(account);
    myWalletProvider.resetPinCode();

    return await sdk.api.keyring.changePassword(keyring, passOld, passNew);
  }

poka's avatar
poka committed
  Future<void> deleteAllAccounts() async {
    for (var account in keyring.allAccounts) {
      await sdk.api.keyring.deleteAccount(keyring, account);
    }
  }

poka's avatar
poka committed
  Future<void> deleteAccounts(List<String> address) async {
    for (var a in address) {
      final account = getKeypair(a);
      await sdk.api.keyring.deleteAccount(keyring, account);
    }
  }

  Future<String> generateMnemonic({String lang = appLang}) async {
poka's avatar
poka committed
    final gen = await sdk.api.keyring.generateMnemonic(ss58);
poka's avatar
poka committed
    generatedMnemonic = gen.mnemonic!;
poka's avatar
poka committed

poka's avatar
poka committed
    // final res = await importAccount(fromMnemonic: true);
poka's avatar
poka committed
    // await Clipboard.setData(ClipboardData(text: generatedMnemonic));
poka's avatar
poka committed
    return gen.mnemonic!;
poka's avatar
poka committed
  }
poka's avatar
poka committed

  Future<String> setCurrentWallet(WalletData wallet) async {
poka's avatar
poka committed
    final currentChestNumber = configBox.get('currentChest');
    ChestData newChestData = chestBox.get(currentChestNumber)!;
    newChestData.defaultWallet = wallet.number;
    await chestBox.put(currentChestNumber, newChestData);
poka's avatar
poka committed

poka's avatar
poka committed
    try {
      final acc = getKeypair(wallet.address!);
poka's avatar
poka committed
      keyring.setCurrent(acc);
      return acc.address!;
    } catch (e) {
      return (e.toString());
    }
  }

  KeyPairData getCurrentWallet() {
    try {
      final acc = keyring.current;
      return acc;
    } catch (e) {
      return KeyPairData();
    }
  }

  Future<String> pay(
poka's avatar
poka committed
      {required String fromAddress,
      required String destAddress,
      required double amount,
      required String password}) async {
    transactionStatus = '';

poka's avatar
poka committed
    // setCurrentWallet(fromAddress);
poka's avatar
poka committed

poka's avatar
poka committed
    log.d(keyring.current.address);
    log.d(fromAddress);
    log.d(password);
    // log.d(await checkPassword(fromAddress, password));
    final fromPubkey = await sdk.api.account.decodeAddress([fromAddress]);
    log.d(fromPubkey!.keys.first);
poka's avatar
poka committed
    final sender = TxSenderData(
poka's avatar
poka committed
    );
    final txInfo = TxInfoData(
        'balances', amount == -1 ? 'transferAll' : 'transferKeepAlive', sender);

    final int amountUnit = (amount * 100).toInt();
poka's avatar
poka committed
    try {
      final hash = await sdk.api.tx.signAndSend(
        txInfo,
        [destAddress, amount == -1 ? false : amountUnit],
poka's avatar
poka committed
        password,
        onStatusChange: (status) {
          log.d('Transaction status: $status');
poka's avatar
poka committed
          transactionStatus = status;
          notifyListeners();
poka's avatar
poka committed
        },
      ).timeout(
        const Duration(seconds: 12),
        onTimeout: () => {},
poka's avatar
poka committed
      );
      log.d(hash.toString());
      if (hash.isEmpty) {
        transactionStatus = 'timeout';
        notifyListeners();

        return 'timeout';
      } else {
        transactionStatus = hash.toString();
        notifyListeners();
        return hash.toString();
      }
    } catch (e) {
      transactionStatus = e.toString();
      notifyListeners();
      return e.toString();
    }
  }

  Future<String> certify(
      String fromAddress, String password, String toAddress) async {
    transactionStatus = '';

poka's avatar
poka committed
    // setCurrentWallet(fromAddress);
    log.d('me: $fromAddress');
    log.d('to: $toAddress');
    final myIdtyStatus = await idtyStatus(fromAddress);
    final toIdtyStatus = await idtyStatus(toAddress);
    log.d(myIdtyStatus);
    log.d(toIdtyStatus);
    if (myIdtyStatus != 'Validated') {
      transactionStatus = 'notMember';
      notifyListeners();
      return 'notMember';
    }

    final sender = TxSenderData(
      keyring.current.address,
      keyring.current.pubKey,
    );
    TxInfoData txInfo;

    if (toIdtyStatus == 'noid') {
      txInfo = TxInfoData(
        'identity',
        'createIdentity',
        sender,
      );
    } else if (toIdtyStatus == 'Validated' ||
        toIdtyStatus == 'ConfirmedByOwner') {
      txInfo = TxInfoData(
        'cert',
        'addCert',
        sender,
      );
    } else {
      transactionStatus = 'cantBeCert';
      notifyListeners();
      return 'cantBeCert';
    }

    log.d('Cert action: ${txInfo.call!}');

    try {
      final hash = await sdk.api.tx
          .signAndSend(
            txInfo,
            [toAddress],
            password,
          )
          .timeout(
            const Duration(seconds: 12),
            onTimeout: () => {},
          );
      log.d(hash);
      if (hash.isEmpty) {
        transactionStatus = 'timeout';
        notifyListeners();

        return 'timeout';
      } else {
        transactionStatus = hash.toString();
        notifyListeners();
        return hash.toString();
      }
poka's avatar
poka committed
    } catch (e) {
      transactionStatus = e.toString();
      notifyListeners();
poka's avatar
poka committed
      return e.toString();
poka's avatar
poka committed
    }
  }
poka's avatar
poka committed
  Future<String> idtyStatus(String address, [bool smooth = true]) async {
poka's avatar
poka committed
    //   var tata = await sdk.webView!
    //       .evalJavascript('api.query.system.account("$address")');

    var idtyIndex = await sdk.webView!
        .evalJavascript('api.query.identity.identityIndexOf("$address")');

    if (idtyIndex == null) {
      return 'noid';
    }

    final idtyStatus = await sdk.webView!
        .evalJavascript('api.query.identity.identities($idtyIndex)');

    if (idtyStatus != null) {
      final String status = idtyStatus['status'];
      // log.d('Status $address: $_status');
      return (status);
poka's avatar
poka committed
    } else {
      return 'expired';
    }
  }

  Future<String> confirmIdentity(
      String fromAddress, String name, String password) async {
poka's avatar
poka committed
    // Confirm identity
poka's avatar
poka committed
    // setCurrentWallet(fromAddress);
    log.d('me: ${keyring.current.address!}');
poka's avatar
poka committed

    final sender = TxSenderData(
      keyring.current.address,
      keyring.current.pubKey,
    );

    final txInfo = TxInfoData(
      'identity',
      'confirmIdentity',
      sender,
    );

    try {
poka's avatar
poka committed
      final hash = await sdk.api.tx.signAndSend(
poka's avatar
poka committed
        txInfo,
        [name],
        password,
poka's avatar
poka committed
        onStatusChange: (status) {
          log.d('Transaction status: $status');
poka's avatar
poka committed
          transactionStatus = status;
          notifyListeners();
        },
      ).timeout(
        const Duration(seconds: 12),
        onTimeout: () => {},
poka's avatar
poka committed
      );
poka's avatar
poka committed
      log.d(hash);
      if (hash.isEmpty) {
        transactionStatus = 'timeout';
        notifyListeners();

        return 'timeout';
      } else {
        transactionStatus = hash.toString();
        notifyListeners();
        return hash.toString();
      }
poka's avatar
poka committed
    } on Exception catch (e) {
      log.e(e);
poka's avatar
poka committed
      transactionStatus = e.toString();
      notifyListeners();
poka's avatar
poka committed
      return e.toString();
    }
  }

  Future<bool> isMemberGet(String address) async {
    return await idtyStatus(address) == 'Validated';
  }

poka's avatar
poka committed
  Future<String> getMemberAddress() async {
    // TODOO: Continue digging memberAddress detection
poka's avatar
poka committed
    String memberAddress = '';
    walletBox.toMap().forEach((key, value) async {
      final bool isMember = await isMemberGet(value.address!);
      log.d(isMember);
      if (isMember) {
poka's avatar
poka committed
        final currentChestNumber = configBox.get('currentChest');
        ChestData newChestData = chestBox.get(currentChestNumber)!;
        newChestData.memberWallet = value.number;
        await chestBox.put(currentChestNumber, newChestData);
poka's avatar
poka committed
        memberAddress = value.address!;
        return;
      }
    });
    log.d(memberAddress);
    return memberAddress;
  }

poka's avatar
poka committed
  Future<Map<String, int>> certState(String from, String to) async {
    Map<String, int> result = {};
    if (from != to && await isMemberGet(from)) {
      final certData = await getCertData(from, to);
      final certMeta = await getCertMeta(from);
      final int removableOn = certData['removableOn'] ?? 0;
      final int nextIssuableOn = certMeta['nextIssuableOn'] ?? 0;
      final certRemovableDuration = (removableOn - blocNumber) * 6;
      const int renewDelay = 2 * 30 * 24 * 3600; // 2 months

      if (certRemovableDuration >= renewDelay) {
        final certRenewDuration = certRemovableDuration - renewDelay;
        result.putIfAbsent('certRenewable', () => certRenewDuration);
      } else if (nextIssuableOn > blocNumber) {
        final certDelayDuration = (nextIssuableOn - blocNumber) * 6;
        result.putIfAbsent('certDelay', () => certDelayDuration);
poka's avatar
poka committed
      } else {
        result.putIfAbsent('canCert', () => 0);
    return result;
  }

  Future<Map> getCertMeta(String address) async {
    var idtyIndex = await sdk.webView!
        .evalJavascript('api.query.identity.identityIndexOf("$address")');

    final certMeta = await sdk.webView!
            .evalJavascript('api.query.cert.storageIdtyCertMeta($idtyIndex)') ??
        '';
    // if (_certMeta['nextIssuableOn'] != 0) return {};

    return certMeta;
poka's avatar
poka committed
  Future revokeIdentity(String address, String password) async {
    final idtyIndex = await sdk.webView!
        .evalJavascript('api.query.identity.identityIndexOf("$address")');

    final sender = TxSenderData(
      keyring.current.address,
      keyring.current.pubKey,
    );

    log.d(sender.address);
    TxInfoData txInfo;

    txInfo = TxInfoData(
      'membership',
      'revokeMembership',
      sender,
    );

    try {
      final hash = await sdk.api.tx
          .signAndSend(
            txInfo,
            [idtyIndex],
            password,
          )
          .timeout(
            const Duration(seconds: 12),
            onTimeout: () => {},
          );
      log.d(hash);
      if (hash.isEmpty) {
        transactionStatus = 'timeout';
        notifyListeners();

        return 'timeout';
      } else {
        transactionStatus = hash.toString();
        notifyListeners();
        return hash.toString();
      }
    } catch (e) {
      transactionStatus = e.toString();
      notifyListeners();
      return e.toString();
    }
  }

poka's avatar
poka committed
  Future getCurencyName() async {}

  Future<String> derive(
poka's avatar
poka committed
      BuildContext context, String address, int number, String password) async {
    final keypair = getKeypair(address);
poka's avatar
poka committed
    final seedMap =
        await keyring.store.getDecryptedSeed(keypair.pubKey, password);

    if (seedMap?['type'] != 'mnemonic') return '';
    final List seedList = seedMap!['seed'].split('//');
poka's avatar
poka committed
    generatedMnemonic = seedList[0];
poka's avatar
poka committed
    return await importAccount(
        mnemonic: generatedMnemonic,
        fromMnemonic: true,
        derivePath: '//$number',
        password: password);
  }

  Future<String> generateRootKeypair(String address, String password) async {
    final keypair = getKeypair(address);

    final seedMap =
        await keyring.store.getDecryptedSeed(keypair.pubKey, password);

    if (seedMap?['type'] != 'mnemonic') return '';
    final List seedList = seedMap!['seed'].split('//');
    generatedMnemonic = seedList[0];

    return await importAccount(fromMnemonic: true, password: password);
poka's avatar
poka committed

  Future<bool> isMnemonicValid(String mnemonic) async {
    // Needed for bad encoding of UTF-8
    mnemonic = mnemonic.replaceAll('é', 'é');
    mnemonic = mnemonic.replaceAll('è', 'è');

    return await sdk.api.keyring.checkMnemonicValid(mnemonic);
  }

  String? getConnectedEndpoint() {
    return sdk.api.connectedNode?.endpoint;
  }
poka's avatar
poka committed
}

void snack(BuildContext context, String message, {int duration = 2}) {
  final snackBar =
      SnackBar(content: Text(message), duration: Duration(seconds: duration));
  ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

class AddressInfo {
  final String? address;
  double balance;

  AddressInfo({@required this.address, this.balance = 0});
}

void snackNode(BuildContext context, bool isConnected) {
  String message;
  if (!isConnected) {
    message =
        "${"noDuniterNodeAvailableTryLater".tr()}:\n${configBox.get('endpoint').first}";
    SubstrateSdk sub = Provider.of<SubstrateSdk>(context, listen: false);
    message =
        "${"youAreConnectedToNode".tr()}\n${sub.getConnectedEndpoint()!.split('//')[1]}";
poka's avatar
poka committed
  final snackBar = SnackBar(
      padding: const EdgeInsets.all(20),
      content: Text(message, style: const TextStyle(fontSize: 16)),
poka's avatar
poka committed
      duration: const Duration(seconds: 4));
  ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

poka's avatar
poka committed
String getShortPubkey(String pubkey) {
poka's avatar
poka committed
  String pubkeyShort = truncate(pubkey, 7,
poka's avatar
poka committed
          omission: String.fromCharCode(0x2026),
          position: TruncatePosition.end) +
poka's avatar
poka committed
      truncate(pubkey, 6, omission: "", position: TruncatePosition.start);
poka's avatar
poka committed
  return pubkeyShort;
poka's avatar
poka committed

class PasswordException implements Exception {
  String cause;
  PasswordException(this.cause);
}