-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
383 lines (373 loc) · 16.7 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import 'dart:io';
import 'package:asymmetric_crypto_primitives/asymmetric_crypto_primitives.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:keri_platform_interface/keri_platform_interface.dart';
import 'package:keri_platform_interface/bridge_generated.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late var signer;
String currentKey = '';
String nextKey = '';
String icpEvent = '';
List<PublicKey> vec1 = [];
List<PublicKey> vec2 = [];
List<String> vec3 = [];
String signature = '';
late Identifier controller;
String controllerId = '';
List<PublicKey> currentKeys = [];
List<PublicKey> newNextKeys = [];
String rotationEvent = '';
String signature2 = '';
bool finalizedEvent = false;
String dataForAnchor = 'important data';
String anchorEvent = '';
String signature3 = '';
bool finalizedAnchor = false;
var dataToSign = '{"hello":"world"}';
bool isCesrSigned = false;
bool isCesrVerified = false;
late var signed;
@override
void initState() {
initKel();
super.initState();
}
Future<void> initKel() async {
signer = await AsymmetricCryptoPrimitives.establishForEd25519();
var dir = await getLocalPath();
var inited = await KeriPlatformInterface.instance.initKel(inputAppDir: dir);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: SingleChildScrollView(
child: Center(
child: Column(
children: [
RawMaterialButton(
onPressed: () async {
currentKey = await signer.getCurrentPubKey();
nextKey = await signer.getNextPubKey();
vec1.add(await KeriPlatformInterface.instance
.newPublicKey(
kt: KeyType.Ed25519, keyB64: currentKey));
vec2.add(await KeriPlatformInterface.instance
.newPublicKey(kt: KeyType.Ed25519, keyB64: nextKey));
setState(() {});
},
child: const Text('Get keys'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1))),
currentKey.isNotEmpty
? Text(
"Public keys",
style: TextStyle(color: Colors.green),
)
: Container(),
Text(currentKey),
Text(nextKey),
currentKey.isNotEmpty ? const Divider() : Container(),
currentKey.isNotEmpty
? RawMaterialButton(
onPressed: () async {
icpEvent = await KeriPlatformInterface.instance
.incept(
publicKeys: vec1,
nextPubKeys: vec2,
witnesses: vec3,
witnessThreshold: 0);
setState(() {});
},
child: const Text('Incept'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
icpEvent.isNotEmpty
? Text(
"ICP event",
style: TextStyle(color: Colors.green),
)
: Container(),
icpEvent.isNotEmpty ? Text(icpEvent) : Container(),
icpEvent.isNotEmpty ? const Divider() : Container(),
icpEvent.isNotEmpty
? RawMaterialButton(
onPressed: () async {
signature = await signer.sign(icpEvent);
setState(() {});
},
child: const Text('Sign event'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
signature.isNotEmpty
? Text(
"Signature",
style: TextStyle(color: Colors.green),
)
: Container(),
signature.isNotEmpty ? Text(signature) : Container(),
signature.isNotEmpty ? const Divider() : Container(),
signature.isNotEmpty
? RawMaterialButton(
onPressed: () async {
controller = await KeriPlatformInterface.instance
.finalizeInception(
event: icpEvent,
signature: await KeriPlatformInterface
.instance
.signatureFromHex(
st: SignatureType.Ed25519Sha512,
signature: signature));
controllerId = controller.id;
setState(() {});
},
child: const Text('Finalize Inception'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
controllerId.isNotEmpty
? Text(
"Controller identifier",
style: TextStyle(color: Colors.green),
)
: Container(),
controllerId.isNotEmpty ? Text(controllerId) : Container(),
controllerId.isNotEmpty ? const Divider() : Container(),
controllerId.isNotEmpty
? RawMaterialButton(
onPressed: () async {
await signer.rotateForEd25519();
currentKey = await signer.getCurrentPubKey();
nextKey = await signer.getNextPubKey();
currentKeys.add(await KeriPlatformInterface.instance
.newPublicKey(
kt: KeyType.Ed25519, keyB64: currentKey));
newNextKeys.add(await KeriPlatformInterface.instance
.newPublicKey(
kt: KeyType.Ed25519, keyB64: nextKey));
rotationEvent = await KeriPlatformInterface.instance
.rotate(
controller: controller,
currentKeys: currentKeys,
newNextKeys: newNextKeys,
witnessToAdd: [],
witnessToRemove: [],
witnessThreshold: 0);
setState(() {});
},
child: const Text('Rotate'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
rotationEvent.isNotEmpty
? Text(
"Rotation event",
style: TextStyle(color: Colors.green),
)
: Container(),
rotationEvent.isNotEmpty ? Text(rotationEvent) : Container(),
rotationEvent.isNotEmpty ? const Divider() : Container(),
rotationEvent.isNotEmpty
? RawMaterialButton(
onPressed: () async {
signature2 = await signer.sign(rotationEvent);
setState(() {});
},
child: const Text('Sign event'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
signature2.isNotEmpty
? Text(
"Signature",
style: TextStyle(color: Colors.green),
)
: Container(),
signature2.isNotEmpty ? Text(signature2) : Container(),
signature2.isNotEmpty ? const Divider() : Container(),
signature2.isNotEmpty
? RawMaterialButton(
onPressed: () async {
finalizedEvent = await KeriPlatformInterface.instance
.finalizeEvent(
identifier: controller,
event: rotationEvent,
signature: await KeriPlatformInterface
.instance
.signatureFromHex(
st: SignatureType.Ed25519Sha512,
signature: signature2));
setState(() {});
},
child: const Text('Finalize event'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
finalizedEvent
? Text(
"Rotation event finalized",
style: TextStyle(color: Colors.green),
)
: Container(),
finalizedEvent ? const Divider() : Container(),
finalizedEvent
? Text(
"Data for anchor",
style: TextStyle(color: Colors.green),
)
: Container(),
finalizedEvent ? Text(dataForAnchor) : Container(),
finalizedEvent ? const Divider() : Container(),
finalizedEvent
? RawMaterialButton(
onPressed: () async {
anchorEvent = await KeriPlatformInterface.instance
.anchor(
controller: controller,
data: dataForAnchor,
algo: DigestType.blake3256());
setState(() {});
},
child: const Text('Anchor'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
anchorEvent.isNotEmpty
? Text(
"Anchor event",
style: TextStyle(color: Colors.green),
)
: Container(),
anchorEvent.isNotEmpty ? Text(anchorEvent) : Container(),
anchorEvent.isNotEmpty ? const Divider() : Container(),
anchorEvent.isNotEmpty
? RawMaterialButton(
onPressed: () async {
signature3 = await signer.sign(anchorEvent);
setState(() {});
},
child: const Text('Sign event'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
signature3.isNotEmpty
? Text(
"Signature",
style: TextStyle(color: Colors.green),
)
: Container(),
signature3.isNotEmpty ? Text(signature3) : Container(),
signature3.isNotEmpty ? const Divider() : Container(),
signature3.isNotEmpty
? RawMaterialButton(
onPressed: () async {
finalizedAnchor = await KeriPlatformInterface.instance
.finalizeEvent(
identifier: controller,
event: anchorEvent,
signature: await KeriPlatformInterface
.instance
.signatureFromHex(
st: SignatureType.Ed25519Sha512,
signature: signature3));
setState(() {});
},
child: const Text('Finalize event'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
finalizedAnchor
? Text(
"Anchor event finalized",
style: TextStyle(color: Colors.green),
)
: Container(),
finalizedAnchor ? const Divider() : Container(),
Divider(),
Text("data to sign: $dataToSign"),
controllerId.isNotEmpty
? RawMaterialButton(
onPressed: () async {
var hexSig = await signer.sign(dataToSign);
await KeriPlatformInterface.instance
.signToCesr(
identifier: controller,
data: dataToSign,
signature: await KeriPlatformInterface
.instance
.signatureFromHex(
st: SignatureType.Ed25519Sha512,
signature: hexSig))
.then((value) {
if (value.isNotEmpty) {
print(value);
setState(() {
isCesrSigned = true;
signed = value;
});
}
});
},
child: const Text('Sign'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
isCesrSigned ? Text("Successfully signed!") : Container(),
isCesrSigned
? RawMaterialButton(
onPressed: () async {
var verified = await KeriPlatformInterface.instance
.verifyFromCesr(stream: signed)
.then((value) {
if (value) {
setState(() {
isCesrVerified = true;
});
}
});
},
child: const Text('Verify'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(width: 1)))
: Container(),
isCesrVerified ? Text("Successfully verified!") : Container(),
],
),
),
),
),
);
}
Future<String> getLocalPath() async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
}