dvote 0.2.0 dvote: ^0.2.0 copied to clipboard
Flutter plugin to allow mobile apps to interact with Vocdoni decentralized votes.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:dvote/dvote.dart';
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 _message = 'This is the super secret text message';
String _passphrase = "One key to rule them all";
String _cipherText = "-";
String _decryptedMessage = "-";
@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 cipherText, decryptedText;
// 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);
cipherText = await Dvote.encryptString(_message, _passphrase);
decryptedText = await Dvote.decryptString(cipherText, _passphrase);
} on PlatformException {
mnemonic = "(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;
setState(() {
_mnemonic = mnemonic;
_privKey = privKey;
_pubKey = pubKey;
_address = addr;
_cipherText = cipherText;
_decryptedMessage = decryptedText;
});
}
@override
Widget build(BuildContext context) {
final encryptionData = '''Original message: \n > $_message\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("ENCRYPTION:"),
SizedBox(height: 30),
Text(encryptionData),
],
),
),
],
),
),
);
}
}