mpc_dart 0.1.2 mpc_dart: ^0.1.2 copied to clipboard
Safeamtrix mpc dart package, ecdsa and bls supported.
import 'package:flutter/material.dart';
import 'package:mpc_dart/mpc_dart.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<KeyPair> ecdsaResult = [];
String ecdsaAddress = '';
KeyPair? ecdsaKey;
String ecdsaSignature = '';
List<KeyPair> blsResult = [];
String blsAddress = '';
KeyPair? blsKey;
String blsSignature = '';
String privateKey = '';
String blsPrivateKey = '';
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
bottom:
const TabBar(tabs: [Tab(text: 'Ecdsa'), Tab(text: 'Bls')])),
body: TabBarView(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () => _generate(),
child: const Text('Generate')),
Text(ecdsaResult.toString()),
TextButton(onPressed: _address, child: const Text('Address')),
Text(ecdsaAddress),
TextButton(onPressed: _recover, child: const Text('Recover')),
Text(ecdsaKey?.toJson() ?? 'Error'),
TextButton(onPressed: _sign, child: const Text('Sign')),
Text(ecdsaSignature),
TextButton(
onPressed: () => _privateKey(),
child: const Text('Private key')),
Text(privateKey)
],
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () => _generate(isEcdsa: false),
child: const Text('Generate')),
Text(blsResult.toString()),
TextButton(
onPressed: () => _address(isEcdsa: false),
child: const Text('Address')),
Text(blsAddress),
TextButton(
onPressed: () => _recover(isEcdsa: false),
child: const Text('Recover')),
Text(blsKey?.toJson() ?? 'Error'),
TextButton(
onPressed: () => _sign(isEcdsa: false),
child: const Text('Sign')),
Text(blsSignature),
TextButton(
onPressed: () => _privateKey(isEcdsa: false),
child: const Text('Private key')),
Text(blsPrivateKey)
],
),
],
),
),
),
);
}
_generate({isEcdsa = true}) {
setState(() {
if (isEcdsa) {
ecdsaResult = Ecdsa.generate(1, 3);
} else {
blsResult = Bls.generate(1, 3);
}
});
return null;
}
_address({isEcdsa = true}) {
setState(() {
if (isEcdsa) {
ecdsaAddress = Ecdsa.address(ecdsaResult[0]);
} else {
blsAddress = Bls.address(blsResult[0]);
}
});
}
_recover({isEcdsa = true}) {
setState(() {
if (isEcdsa) {
ecdsaKey = Ecdsa.recover([ecdsaResult[0], ecdsaResult[1]]);
} else {
blsKey = Bls.recover([blsResult[0], blsResult[1]]);
}
});
}
_privateKey({isEcdsa = true}) {
setState(() {
if (isEcdsa) {
privateKey = Ecdsa.privateKey([ecdsaResult[0], ecdsaResult[1]]);
} else {
blsPrivateKey = Bls.privateKey([blsResult[0], blsResult[1]]);
}
});
}
_sign({isEcdsa = true}) {
setState(() {
final msgs = [List<int>.filled(32, 1)];
if (isEcdsa) {
ecdsaSignature = Ecdsa.sign(SignParams(msgs, 1, ecdsaResult));
} else {
blsSignature = Bls.sign(SignParams(msgs, 1, blsResult));
}
});
}
}