-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
449 lines (369 loc) · 11 KB
/
index.html
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!doctype html5>
<html>
<head>
<title>TreeKEM</title>
<script src="./dist/index.js"></script>
<script src="./web/jquery-3.3.1.min.js"></script>
<script>
const ok = (x => { window.pout = x; console.log("ok", x); });
const err = (x => { window.pout = x; console.log("err", x); });
function hex(ab) {
const arr = Array.from(new Uint8Array(ab));
return arr.map(x => ('0' + x.toString(16)).slice(-2)).join('');
}
let stateTypes = {
"TreeKEM": TreeKEMState,
"ART": ARTState,
"Flat": FlatState,
};
let stateTypeLegends = {
"TreeKEM": "Colors represent hash chains. Lightness represents distance along the chain.",
"ART": "DH is indicated by color 'averaging'",
"Flat": "Colors indicate different public keys",
};
let StateType = FlatState;
let members = [];
let renderers = {};
let treeCache = new Tree();
let operationLock = false;
let compactEnabled = false;
async function init() {
$("div.tree").remove();
$("button.update").remove();
let leaf = base64.random(32);
let creator = await StateType.oneMemberGroup(leaf);
treeCache.size = 1;
treeCache.merge(creator.nodes);
addMember(creator);
}
function selectStateType(label) {
let id = "stateType-" + label;
$("button.stateType").removeClass("selectedState");
$("#" + id).addClass("selectedState");
$("#stateTypeLegend").text(stateTypeLegends[label]);
StateType = stateTypes[label];
return init();
}
function lockWrap(action) {
return () => {
operationLock = true;
$("#buttons button").prop("disabled", true);
$("#buttons button.persistent").prop("disabled", false);
action()
.then(() => {
operationLock = false;
$("#buttons button").prop("disabled", false);
});
};
}
function addMember(m) {
m.id = Symbol(m.index);
members.push(m);
// Add a tree
let index = m.index;
let divID = `tree-${index}`;
let tag = $(`<div class="number">${index}</div>`);
let div = $(`<div id="${divID}" class="tree"></div>`);
div.append(tag);
$("#members").append(div);
// Render
let r = new Renderer(divID);
r.render(m.size, m.nodes);
renderers[m.id] = r;
// Add an update button
let ubutton = $(`<button class="update">Update(${index})</button>`);
ubutton.attr("id", `btn-update-${index}`);
ubutton.click(lockWrap(() => { return update(index); }));
$("#buttons").append(ubutton);
// Add a delete button
let rbutton = $(`<button class="remove">Remove(${index})</button>`);
rbutton.attr("id", `btn-remove-${index}`);
rbutton.click(lockWrap(() => { return remove(index); }));
$("#buttons").append(rbutton);
// Add a move button
let mbutton = $(`<button class="move">Move(${index})</button>`);
mbutton.attr("id", `btn-move-${index}`);
mbutton.click(lockWrap(() => { return moveToLowestVacant(index); }));
$("#buttons").append(mbutton);
// Add padding
$("#buttons").append(document.createTextNode(' '));
// Activate all buttons
$("#buttons button").attr('disabled', false);
}
function removeMember(index) {
removed = members.filter(m => m.index == index)[0];
members = members.filter(m => m.index != index);
delete renderers[removed.id];
// Remove the associated DOM elements
$(`#tree-${index}`).remove();
$(`#btn-update-${index}`).remove();
$(`#btn-remove-${index}`).remove();
$(`#btn-move-${index}`).remove();
if (members.length == 1) {
let lastRemaining = members[0].index;
$(`#btn-remove-${lastRemaining}`).attr('disabled', true);
}
}
function moveMember(index, newIndex) {
// Renumber the associated DOM elements
$(`#tree-${index}`).attr('id', `tree-${newIndex}`);
$(`#btn-update-${index}`).attr('id', `btn-update-${newIndex}`);
$(`#btn-remove-${index}`).attr('id', `btn-remove-${newIndex}`);
$(`#btn-move-${index}`).attr('id', `btn-move-${newIndex}`);
// Relabel everything
$(`#tree-${newIndex} div.number`).text(newIndex);
$(`#btn-update-${newIndex}`).text(`Update(${newIndex})`);
$(`#btn-remove-${newIndex}`).text(`Remove(${newIndex})`);
$(`#btn-move-${newIndex}`).text(`Move(${newIndex})`);
// Reassign actions
$(`#btn-update-${newIndex}`).unbind('click');
$(`#btn-remove-${newIndex}`).unbind('click');
$(`#btn-move-${newIndex}`).unbind('click');
$(`#btn-update-${newIndex}`).click(lockWrap(() => { return update(newIndex); }));
$(`#btn-remove-${newIndex}`).click(lockWrap(() => { return remove(newIndex); }));
$(`#btn-move-${newIndex}`).click(lockWrap(() => { return moveToLowestVacant(newIndex); }));
}
function lowestVacant() {
let memberIx = members.map(m => m.index);
let allIx = [...Array(treeCache.size).keys()];
return allIx.filter(i => !memberIx.includes(i))
.reduce((a, b) => (a < b)? a : b, Infinity);
}
function highestOccupied() {
return members.map(m => m.index)
.reduce((a, b) => (a > b)? a : b, -Infinity);
}
async function userAdd() {
let last = members[members.length - 1];
let leaf = base64.random(32);
let gik = last.groupInitKey;
let ua = await StateType.join(leaf, gik);
for (let m of members) {
await m.handleUserAdd(ua);
renderers[m.id].render(m.size, m.nodes);
}
treeCache.size += 1;
treeCache.merge(ua.nodes);
let joiner = await StateType.fromUserAdd(leaf, ua, gik);
addMember(joiner);
}
async function groupAdd() {
let last = members[members.length - 1];
let initLeaf = base64.random(32);
let initKP = await iota(initLeaf);
let ga = await last.add(initKP.publicKey);
for (let m of members) {
await m.handleGroupAdd(ga);
renderers[m.id].render(m.size, m.nodes);
}
treeCache.size += 1;
treeCache.merge(ga.forGroup.nodes);
let joiner = await StateType.fromGroupAdd(initLeaf, ga);
addMember(joiner);
}
async function update(k) {
let leaf = base64.random(32);
let member = members.filter(m => m.index == k)[0];
let update = await member.update(leaf);
for (let m2 of members) {
if (m2.index == k) {
await m2.handleSelfUpdate(update, leaf);
} else {
await m2.handleUpdate(update);
}
renderers[m2.id].render(m2.size, m2.nodes);
}
treeCache.merge(update.nodes);
}
async function remove(k) {
let leaf = base64.random(32);
let candidates = members.filter(m => m.index != k);
if (candidates.length == 0) {
console.log("Cannot remove last member");
return;
}
let remover = candidates[Math.floor(Math.random() * candidates.length)];
let removed = members.filter(m => m.index == k)[0];
let remove = await remover.remove(leaf, k, removed.copath);
let newSize = members.map(m => m.index)
.filter(i => i != k)
.reduce((a, b) => (a > b)? a : b, -Infinity);
newSize += 1;
for (let m2 of members) {
if (m2.index == k) {
continue;
}
await m2.handleRemove(remove);
m2.trim(newSize);
renderers[m2.id].render(m2.size, m2.nodes);
}
treeCache.remove(k);
removeMember(k);
if (compactEnabled) {
compact();
}
}
async function moveToLowestVacant(k) {
let minVacant = lowestVacant();
if (k < minVacant) {
console.log(`Member ${k} is already in the lowest available slot`);
return
}
let leaf = base64.random(32);
let copath = treeCache.copath(minVacant);
let mover = members.filter(m => m.index == k)[0];
let move = await mover.move(leaf, minVacant, copath);
for (let m2 of members) {
if (m2.index == k) {
await m2.handleSelfMove(move, leaf);
} else {
await m2.handleMove(move);
}
}
treeCache.remove(move.from);
treeCache.merge(move.nodes);
moveMember(k, minVacant);
let newSize = highestOccupied() + 1;
for (let m2 of members) {
m2.trim(newSize);
renderers[m2.id].render(m2.size, m2.nodes);
}
}
async function compact() {
for (let i of members.map(m => m.index).sort()) {
await moveToLowestVacant(i);
}
}
let chaosEnabled = false;
let chaosCount = 0;
const chaosOnLabel = "😈"; // U+1F608 SMILING FACE WITH HORNS
const chaosOffLabel = "😐"; // U+1F610 NEUTRAL FACE
const chaosDistribution = {
"add": 0.3,
"update": 0.5,
"move": 0.1,
"remove": 0.1,
}
async function startChaos() {
const chaosInterval = 1000;
const sleep = () => new Promise(res => { setTimeout(res, chaosInterval); });
while (chaosEnabled) {
while (operationLock) {
await sleep();
}
// Pick a random button class
let dice = Math.random();
let target;
let cdf = 0;
for (let key in chaosDistribution) {
cdf += chaosDistribution[key];
if (dice < cdf) {
target = key;
break;
}
}
// Click a random button of that class
let buttons = $(`#buttons button.${target}`);
if (buttons.length == 0) {
continue;
}
let pick = Math.floor(Math.random() * buttons.length);
console.log(`chaos click (${chaosCount++}):`, buttons[pick].innerText);
buttons[pick].click();
await sleep();
}
}
const compactOnLabel = "🌒"; // U+1F312 WAXING CRESENT MOON
const compactOffLabel = "🌝"; // U+1F312 FULL MOON FACE
$(document).ready(async () => {
for (let label in stateTypes) {
let id = "stateType-" + label;
let button = $(`<button id="${id}" class="stateType">${label}</button>`);
button.click(lockWrap(() => { return selectStateType(label); }));
$("#stateTypes").append(button);
}
$("#userAdd").click(lockWrap(userAdd));
$("#groupAdd").click(lockWrap(groupAdd));
$("#chaos").html(chaosOffLabel);
$("#chaos").click(() => {
chaosEnabled = !chaosEnabled;
$("#chaos").html((chaosEnabled)? chaosOnLabel : chaosOffLabel);
if (chaosEnabled) {
startChaos();
}
});
$("#compact").html(compactOffLabel);
$("#compact").click(() => {
compactEnabled = !compactEnabled;
$("#compact").html((compactEnabled)? compactOnLabel : compactOffLabel);
});
selectStateType("TreeKEM");
});
</script>
<style>
body {
font-family: Menlo, monospace;
font-size: 10pt;
margin: 0;
}
#stateTypes, #buttons, div.legend, #members {
padding: 2ex;
width: 100%;
}
#stateTypes, #buttons, div.legend {
border-bottom: 1px solid #999;
}
#github {
float: right;
margin: 1em;
}
div.legend {
background: #ccc;
padding: 0 1em;
}
button {
border: 1px solid #999;
background: #ccc;
font-family: Menlo, monospace;
font-size: 10pt;
padding: 1ex;
box-shadow: 1px 1px;
}
button.selectedState {
background: #99c;
}
div.number {
margin: 0;
color: #000;
background: #ccc;
text-align: center;
}
div.tree {
float: left;
margin: 1ex;
padding: 0;
border: 1px solid black;
}
div.tree svg {
margin: 1ex;
}
</style>
</head>
<body>
<div id="github"><a href="https://github.com/bifurcation/treekem">GitHub</a></div>
<div id="stateTypes"></div>
<div id="buttons">
<button id="chaos" title="chaos mode" class="persistent"></button>
<button id="compact" title="auto-compact" class="persistent"></button>
<button id="userAdd" class="add">UserAdd</button>
<button id="groupAdd" class="add">GroupAdd</button>
</div>
<div class="legend">
Filled boxes are nodes with private keys.
Empty boxes are nodes with only public keys.
Grey boxes are nodes where there is no data.
<span id="stateTypeLegend"></span>
</div>
<div id="members"></div>
</body>
</html>