mpc_dart 0.2.2 mpc_dart: ^0.2.2 copied to clipboard
Safeamtrix mpc dart package, ecdsa and bls supported.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:mpc_dart/mpc_dart.dart';
import 'package:mpc_dart/multi_mpc_dart.dart';
import 'package:http/http.dart' as http;
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 = '';
List<MultiKeypair> multiResult = [];
String multiAddress = '';
String multiSignature = '';
String? multiKey;
String privateKey = '';
String blsPrivateKey = '';
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
bottom: const TabBar(tabs: [
Tab(text: 'Multi Mpc'),
Tab(text: 'Ecdsa'),
Tab(text: 'Bls')
])),
body: TabBarView(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () => _multiGenerate(),
child: const Text('Generate')),
Text(multiResult.toString()),
TextButton(
onPressed: _multiAddress, child: const Text('Address')),
Text(multiAddress),
TextButton(
onPressed: _multiRecover, child: const Text('Recover')),
Text(multiKey ?? 'Error'),
TextButton(onPressed: _multiSign, child: const Text('Sign')),
Text(multiSignature)
],
),
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)
],
),
],
),
),
),
);
}
_multiGenerate() async {
final result = await MultiMpc.generate(1, 3);
setState(() {
multiResult = result;
});
}
_multiAddress() {
setState(() {
multiAddress = MultiMpc.address(multiResult[0]);
});
}
_multiSign() async {
final msgs = [List<int>.filled(32, 1)];
final uuid = MultiMpc.uuid();
final params = MultiSignParams(
keypair: multiResult[0],
msgs: msgs,
rawMsg: '',
url: 'http://192.168.52.108:8888',
get: 'get',
set: 'set',
uid: uuid);
final key = MultiMpc.auxToKeypair(multiResult[1]);
final remoteSign = {
'metadata': {
'uid': uuid,
'party_ind': 2,
'engine': 'ECDSA',
},
'party_ind': 2,
'key': jsonDecode(key)['aux']['key'],
'raw_msg': "",
'sign_msgs': msgs,
'parameters': {
'threshold': 1,
'share_count': 3,
},
'round_timeout': null,
};
await http.post(Uri.parse('http://192.168.52.108:8888/sign'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(remoteSign));
final signature = await MultiMpc.sign(params);
setState(() {
multiSignature = signature;
});
}
_multiRecover() async {
final result = await MultiMpc.recover(
[multiResult[1], multiResult[2]], multiResult[0].aux);
setState(() {
multiKey = result.sk;
});
}
_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));
}
});
}
}