dvote 0.4.1 copy "dvote: ^0.4.1" to clipboard
dvote: ^0.4.1 copied to clipboard

outdated

Flutter plugin to allow native mobile apps to interact with Vocdoni decentralized votes.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:dvote/dvote.dart';

final vocGateway = "ws://gateway:2082/dvote";
final web3Gateway = "http://gateway:8545/web3";
final resolverAddress = "0x0dCA233CE5152d58c74E74693A3C496D01542244";
final entityId =
    "0x180dd5765d9f7ecef810b565a2e5bd14a3ccd536c442b3de74867df552855e85";
final privKeyToSign =
    "fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19";
final publicKey =
    "0x049a7df67f79246283fdc93af76d4f8cdd62c4886e8cd870944e817dd0b97934fdd7719d0810951e03418205868a5c1b40b192451367f28e0088dd75e15de40c05";
final messageToSign = "hello";
final messageToEncrypt = 'This is the super secret text message';
final passphrase = "One key to rule them all";

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _mnemonic = '(mnemonic)';
  String _privKey = '(priv key)';
  String _pubKey = '(pub key)';
  String _address = '(addr)';
  String _signature = "-";
  bool _validSignature = false;
  String _cipherText = "-";
  String _decryptedMessage = "-";
  Entity _entityMeta;

  String _error;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String mnemonic, privKey, pubKey, addr;
    String signature;
    bool valid;
    String cipherText, decryptedText;
    Entity entityMeta;
    String error;

    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      mnemonic = await Dvote.generateMnemonic(size: 192);
      privKey = await Dvote.mnemonicToPrivateKey(mnemonic);
      pubKey = await Dvote.mnemonicToPublicKey(mnemonic);
      addr = await Dvote.mnemonicToAddress(mnemonic);

      signature = await Dvote.signString(messageToSign, privKeyToSign);
      valid = await Dvote.verifySignature(signature, messageToSign, publicKey);

      cipherText = await Dvote.encryptString(messageToEncrypt, passphrase);
      decryptedText = await Dvote.decryptString(cipherText, passphrase);
    } on PlatformException catch (err) {
      error = err.message;
    } catch (err) {
      error = err;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    if (error != null) {
      setState(() {
        _error = error;
      });
      return;
    }

    setState(() {
      _mnemonic = mnemonic;
      _privKey = privKey;
      _pubKey = pubKey;
      _address = addr;

      _signature = signature;
      _validSignature = valid ?? false;

      _cipherText = cipherText;
      _decryptedMessage = decryptedText;
    });

    try {
      entityMeta = await fetchEntity(
          entityId, resolverAddress, vocGateway, web3Gateway,
          networkId: "1234", entryPoints: []);
    } catch (err) {
      if (err is String) {
        error = err;
        print(err);
      } else {
        error = "Connecting to the network failed";
      }
    }

    setState(() {
      _entityMeta = entityMeta;
      _error = error;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_error != null) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('DVote plugin example'),
          ),
          body: Container(
            child: Text("Error: " + _error),
          ),
        ),
      );
    }

    final signingData =
        "Signing '$messageToSign' with $privKeyToSign:\n\n$_signature\n\nThe signature is ${_validSignature ? "valid" : "not valid"}";
    final encryptionData = '''Original message: \n > $messageToEncrypt\n
Encryption key: \n > $passphrase\n
Encrypted message: \n > $_cipherText\n
Decrypted message: \n > $_decryptedMessage''';

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('DVote plugin example'),
        ),
        body: ListView(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                children: <Widget>[
                  Text("HD WALLET:"),
                  SizedBox(height: 30),
                  Text('Mnemonic: \n$_mnemonic'),
                  SizedBox(height: 15),
                  Text('Private key: \n$_privKey'),
                  SizedBox(height: 15),
                  Text('Public key: \n$_pubKey'),
                  SizedBox(height: 15),
                  Text('Address: \n$_address'),
                ],
              ),
            ),
            Divider(),
            Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                children: <Widget>[
                  Text("SIGNING:"),
                  SizedBox(height: 30),
                  Text(signingData),
                ],
              ),
            ),
            Divider(),
            Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                children: <Widget>[
                  Text("ENCRYPTION:"),
                  SizedBox(height: 30),
                  Text(encryptionData),
                ],
              ),
            ),
            Divider(),
            Padding(
              padding: EdgeInsets.all(16),
              child: _entityMeta == null
                  ? null
                  : Column(
                      children: <Widget>[
                        Text("ENTITY META:"),
                        SizedBox(height: 30),
                        Text(jsonEncode(_entityMeta.toJson())),
                      ],
                    ),
            ),
          ],
        ),
      ),
    );
  }
}
2
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Flutter plugin to allow native mobile apps to interact with Vocdoni decentralized votes.

Homepage
Repository (GitLab)
View/report issues

Documentation

Documentation

License

unknown (LICENSE)

Dependencies

convert, flutter, http, web3dart, web_socket_channel

More

Packages that depend on dvote