/
org.sovrin.agent.krl
616 lines (615 loc) · 18.5 KB
/
org.sovrin.agent.krl
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
ruleset org.sovrin.agent {
meta {
use module org.sovrin.agent_message alias a_msg
use module org.sovrin.agent.ui alias invite
use module io.picolabs.wrangler alias wrangler
use module io.picolabs.visual_params alias vp
use module webfinger alias wf
shares __testing, html, ui, getEndpoint, connections, pendingConnections,
get_last_http_response, get_routing_response
provides connections, ui
}
global {
__testing = { "queries":
[ { "name": "__testing" }
, { "name": "connections", "args": [ "label" ] }
, { "name": "getEndpoint", "args" : ["my_did", "endpoint"] }
, { "name": "pendingConnections" }
] , "events":
[ { "domain": "webfinger", "type": "webfinger_wanted" }
, { "domain": "sovrin", "type": "update_endpoint", "attrs" : ["my_did", "their_host"] }
, { "domain": "agent", "type": "pending_connections_cleanup_requested" }
, { "domain": "agent", "type": "connections_cleanup_requested" }
]
}
html = function(c_i){
invite:html(c_i)
}
ui = function(){
connections = ent:connections
.values()
.sort(function(a,b){
a{"created"} cmp b{"created"}
});
{
"name": ent:label,
"connections": connections.length() => connections | null,
"invitation": invitation(),
"wf": ent:webfinger,
}
}
invitation = function(){
uKR = wrangler:channel("agent");
eci = uKR{"id"};
im = a_msg:connInviteMap(
null, // @id
ent:label,
uKR{["sovrin","indyPublic"]},
a_msg:localServiceEndpoint(eci)
);
ep = <<#{meta:host}/sky/cloud/#{eci}/#{meta:rid}/html.html>>;
ep + "?c_i=" + math:base64encode(im.encode())
}
//Function added by Jace, Beto, and Michael to update the connection's endpoints
getEndpoint = function(my_did, endpoint) {
endpoint + ent:connections.filter(function(v) {
v{"my_did"} == my_did
}).values().map(function(x) {
x{"their_endpoint"}
})[0].extract(re#(/sky/event/.*)#).head()//.split("/").slice(3, 8).join("/")
}
connections = function(label) {
matching = function(x){x{"label"}==label};
label => ent:connections.filter(matching).values().head()
| ent:connections
}
pendingConnections = function(){
ent:pending_conn
}
get_last_http_response = function(){
ent:last_http_response
}
get_routing_response = function(){
ent:routing_response
}
}
//
// on ruleset_added
//
rule on_installation {
select when wrangler ruleset_added where event:attr("rids") >< meta:rid
pre {
label = event:attr("label")
|| event:attr("rs_attrs"){"label"}
}
if wrangler:channel("agent").isnull() then
wrangler:createChannel(meta:picoId,"agent","sovrin")
always {
ent:label := label || wrangler:name();
ent:connections := {};
raise wrangler event "ruleset_needs_cleanup_period" attributes {
"domain": meta:rid
}
}
}
rule webfinger_check {
select when webfinger webfinger_wanted
fired {
ent:webfinger := wf:webfinger(wrangler:name())
}
}
rule check_label {
select when agent check_state
pre {
bad = ent:label.isnull() || typeof(ent:label) != "String" || ent:label == ""
}
if bad then noop()
fired {
ent:label := wrangler:name()
}
}
//
// send ssi_agent_wire message
//
rule send_ssi_agent_wire_message {
select when sovrin new_ssi_agent_wire_message
pre {
se = event:attr("serviceEndpoint")
pm = event:attr("packedMessage")
}
http:post(
se,
body=pm,
headers={"content-type":"application/ssi-agent-wire"},
autosend = {"eci": meta:eci, "domain": "http", "type": "post"}
)
}
rule save_last_http_response {
select when http post
fired {
ent:last_http_response := event:attrs
}
}
rule record_eci_not_found_error {
select when http post status_code re#404#
pre {
content = event:attr("content").decode()
eci = content{"error"}.extract(re#ECI not found: (.+)#).head()
conn = ent:connections.filter(function(c){
c{"their_did"} == eci
})
vk = conn.keys().head()
}
if eci && conn then noop()
fired {
ent:connections{[vk,"error"]} := "ECI not found: "+eci
}
}
//
// accept invitation
//
rule accept_invitation {
select when sovrin new_invitation
url re#(http.+[?].*((c_i=)|(d_m=)).+)# setting(url)
pre {
qs = url.split("?").tail().join("?")
args = qs.split("&")
.map(function(x){x.split("=")})
.collect(function(x){x[0]})
.map(function(x){x[0][1]})
c_i = args{"c_i"} || args{"d_m"}
im = math:base64decode(c_i).decode()
their_label = im{"label"}
need_router_connection = event:attr("need_router_connection")
}
if im && their_label then
wrangler:createChannel(meta:picoId,their_label,"connection")
setting(channel)
fired {
raise agent event "check_state" attributes {};
raise edge event "need_router_connection"
attributes {
"invitation": im,
"channel": channel,
"label": their_label,
"txnId": meta:txnId,
"sovrin_event": "invitation_accepted",
} if need_router_connection;
raise sovrin event "invitation_accepted"
attributes {
"invitation": im,
"channel": channel
} if need_router_connection.isnull()
}
}
//
// initiate connections request
//
rule initiate_connection_request {
select when sovrin invitation_accepted
pre {
im = event:attr("invitation")
chann = event:attr("channel")
my_did = chann{"id"}
my_vk = chann{["sovrin","indyPublic"]}
ri = event:attr("routing").klog("routing information")
rks = ri => ri{"their_routing"} | null
endpoint = ri => ri{"endpoint"} | a_msg:localServiceEndpoint(my_did)
rm = a_msg:connReqMap(ent:label,my_did,my_vk,endpoint,rks,im{"@id"})
.klog("connections request")
reqURL = im{"serviceEndpoint"}
pc = {
"label": im{"label"},
"my_did": my_did,
"@id": rm{"@id"},
"my_did": my_did,
"their_vk": im{"recipientKeys"}.head(),
"their_routing": im{"routingKeys"}.defaultsTo([]),
}
packedBody = a_msg:packMsg(pc,rm)
}
fired {
ent:pending_conn := ent:pending_conn.defaultsTo([]).append(pc);
raise sovrin event "new_ssi_agent_wire_message" attributes {
"serviceEndpoint": reqURL, "packedMessage": packedBody
}
}
}
//
// receive messages
//
rule route_new_message {
select when sovrin new_message protected re#(.+)# setting(protected)
pre {
outer = math:base64decode(protected).decode()
kids = outer{"recipients"}
.map(function(x){x{["header","kid"]}})
my_vk = wrangler:channel(meta:eci){["sovrin","indyPublic"]}
sanity = (kids >< my_vk)
.klog("sanity")
all = indy:unpack(event:attrs,meta:eci)
msg = all{"message"}.decode()
event_type = a_msg:specToEventType(msg{"@type"})
}
if event_type then
send_directive("message routed",{"event_type":event_type})
fired {
raise agent event "check_state" attributes {};
raise sovrin event event_type attributes
all.put("message",msg)
.put("need_router_connection",event:attr("need_router_connection"))
}
}
//
// connections/request
//
rule handle_connections_request {
select when sovrin connections_request
pre {
msg = event:attr("message")
their_label = msg{"label"}
need_router_connection = event:attr("need_router_connection")
}
if their_label then
wrangler:createChannel(meta:picoId,their_label,"connection")
setting(channel)
fired {
raise edge event "need_router_connection"
attributes {
"message": msg,
"channel": channel,
"label": their_label,
"txnId": meta:txnId,
"sovrin_event": "connections_request_accepted",
} if need_router_connection;
raise sovrin event "connections_request_accepted"
attributes {
"message": msg,
"channel": channel,
} if need_router_connection.isnull()
}
}
rule having_router_connection_pursue_connection {
select when edge new_router_connection_recorded
where event:attr("txnId") == meta:txnId
fired {
ent:routing_response := event:attrs;
raise sovrin event event:attr("sovrin_event")
attributes event:attrs
}
}
rule initiate_connections_response {
select when sovrin connections_request_accepted
pre {
msg = event:attr("message")
req_id = msg{"@id"}
connection = msg{"connection"}
their_did = connection{"DID"}
publicKeys = connection{["DIDDoc","publicKey"]}
.map(function(x){x{"publicKeyBase58"}})
their_vk = publicKeys.head()
service = connection{["DIDDoc","service"]}
.filter(function(x){
x{"type"}=="IndyAgent"
&& x{"id"}.match(their_did+";indy")
}).head()
se = service{"serviceEndpoint"}
their_rks = service{"routingKeys"}.defaultsTo([])
chann = event:attr("channel")
my_did = chann{"id"}
my_vk = chann{["sovrin","indyPublic"]}
ri = event:attr("routing").klog("routing information")
rks = ri => ri{"their_routing"} | null
endpoint = ri => ri{"endpoint"} | a_msg:localServiceEndpoint(my_did)
rm = a_msg:connResMap(req_id, my_did, my_vk, endpoint,rks)
c = {
"created": time:now(),
"label": msg{"label"},
"my_did": my_did,
"their_did": their_did,
"their_vk": their_vk,
"their_endpoint": se,
"their_routing": their_rks,
}
pm = a_msg:packMsg(c,rm,meta:eci)
}
fired {
raise agent event "new_connection" attributes c;
raise sovrin event "new_ssi_agent_wire_message" attributes {
"serviceEndpoint": se, "packedMessage": pm
}
}
}
//
// connections/response
//
rule handle_connections_response {
select when sovrin connections_response
pre {
msg = event:attr("message")
verified = a_msg:verify_signatures(msg)
connection = verified{"connection"}
service = connection && connection{["DIDDoc","service"]}
.filter(function(x){x{"type"}=="IndyAgent"})
.head()
their_vk = service{"recipientKeys"}.head()
their_rks = service{"routingKeys"}.defaultsTo([])
cid = msg{["~thread","thid"]}
index = ent:pending_conn.defaultsTo([])
.reduce(function(a,p,i){
a<0 && p{"@id"}==cid => i | a
},-1)
c = index < 0 => null | ent:pending_conn[index]
.delete("@id")
.put({
"created": time:now(),
"their_did": connection{"DID"},
"their_vk": their_vk,
"their_endpoint": service{"serviceEndpoint"},
"their_routing": their_rks,
})
}
if typeof(index) == "Number" && index >= 0 then noop()
fired {
raise agent event "new_connection" attributes c;
ent:pending_conn := ent:pending_conn.splice(index,1)
}
}
//
// record a new connection
//
rule record_new_connection {
select when agent new_connection
pre {
their_vk = event:attr("their_vk")
}
fired {
ent:connections{their_vk} := event:attrs;
raise agent event "connections_changed"
attributes { "connections": ent:connections }
}
}
//
// trust_ping/ping
//
rule handle_trust_ping_request {
select when sovrin trust_ping_ping
pre {
msg = event:attr("message")
rm =a_msg:trustPingResMap(msg{"@id"})
their_key = event:attr("sender_key")
conn = ent:connections{their_key}
pm = a_msg:packMsg(conn,rm)
se = conn{"their_endpoint"}
may_respond = msg{"response_requested"} == false => false | true
}
if se && may_respond then noop()
fired {
raise sovrin event "new_ssi_agent_wire_message" attributes {
"serviceEndpoint": se, "packedMessage": pm
}
}
}
//
// trust_ping/ping_response
//
rule handle_trust_ping_ping_response {
select when sovrin trust_ping_ping_response
}
//
// initiate trust ping
//
rule initiate_trust_ping {
select when sovrin trust_ping_requested
pre {
their_vk = event:attr("their_vk")
conn = ent:connections{their_vk}
rm = a_msg:trustPingMap()
pm = a_msg:packMsg(conn,rm)
se = conn{"their_endpoint"}
}
if se then noop()
fired {
raise sovrin event "new_ssi_agent_wire_message" attributes {
"serviceEndpoint": se, "packedMessage": pm
}
}
}
//
// basicmessage/message
//
rule handle_basicmessage_message {
select when sovrin basicmessage_message
pre {
their_key = event:attr("sender_key")
conn = ent:connections{their_key}
msg = event:attr("message")
wmsg = conn.put(
"messages",
conn{"messages"}.defaultsTo([])
.append(msg.put("from","incoming"))
)
}
fired {
ent:connections{their_key} := wmsg
}
}
//
// initiate basicmessage
//
rule initiate_basicmessage {
select when sovrin send_basicmessage
pre {
their_key = event:attr("their_vk")
conn = ent:connections{their_key}
content = event:attr("content").decode()
bm = a_msg:basicMsgMap(content)
pm = a_msg:packMsg(conn,bm)
se = conn{"their_endpoint"}
wmsg = conn.put(
"messages",
conn{"messages"}.defaultsTo([])
.append(bm.put("from","outgoing")
.put("color",vp:colorRGB().join(","))
)
)
}
if se then noop()
fired {
raise sovrin event "new_ssi_agent_wire_message" attributes {
"serviceEndpoint": se, "packedMessage": pm
};
ent:connections{their_key} := wmsg
}
}
//
// convenience rule to clean up known expired connection
//
rule delete_connection {
select when sovrin connection_expired
pre {
their_vk = event:attr("their_vk")
pairwise = ent:connections{their_vk}
the_did = pairwise{"my_did"}
override = event:attr("final_cleanup")
}
if the_did == meta:eci || override then
send_directive("delete",{"connection":pairwise})
fired {
clear ent:connections{their_vk};
raise edge event "router_connection_deletion_requested"
attributes {"label":pairwise{"label"}};
raise wrangler event "channel_deletion_requested"
attributes {"eci": the_did} if wrangler:channel(the_did);
raise agent event "connections_changed"
attributes { "connections": ent:connections }
}
}
rule update_connection_endpoint {
select when sovrin update_endpoint
pre {
my_did = event:attr("my_did")
new_endpoint_host = event:attr("their_host")
new_endpoint = getEndpoint(my_did, new_endpoint_host)
}
fired {
ent:connections := ent:connections.map(function(v, k) {
(v{"my_did"} == my_did) => v.set(["their_endpoint"], new_endpoint)| v
})
}
}
//
// clean up internal data structures as needed
//
rule clean_up_pending_connections {
select when agent pending_connections_cleanup_requested
foreach ent:pending_conn.defaultsTo([]) setting(conn)
pre {
eci = conn{"my_did"}
label = conn{"label"}
}
if eci && label then noop()
fired {
raise edge event "router_connection_deletion_requested"
attributes {"label":label};
raise wrangler event "channel_deletion_requested"
attributes {"eci":eci} if wrangler:channel(eci)
}
finally {
raise agent event "pending_connections_cleanup_completed" attributes {} on final
}
}
rule finalize_clean_up_pending_connections {
select when agent pending_connections_cleanup_completed
fired {
clear ent:pending_conn;
raise agent event "pending_connections_cleared" attributes {}
}
}
rule clean_up_prior_to_pico_deletion_part0 {
select when wrangler rulesets_need_to_cleanup
where wrangler:channel("agent")
fired {
raise wrangler event "channel_deletion_requested"
attributes {"name":"agent"}
}
}
rule clean_up_prior_to_pico_deletion_part1a {
select when wrangler rulesets_need_to_cleanup
fired {
raise agent event "pending_connections_cleanup_requested" attributes {}
}
}
rule clean_up_prior_to_pico_deletion_part1b {
select when wrangler rulesets_need_to_cleanup
where ent:pending_conn.isnull()
fired {
raise agent event "pending_connections_cleared" attributes {}
}
}
rule clean_up_prior_to_pico_deletion_part2a {
select when wrangler rulesets_need_to_cleanup
foreach ent:connections.keys() setting(vk)
fired {
raise sovrin event "connection_expired"
attributes {"their_vk":vk,"final_cleanup":true}
}
finally {
raise agent event "connections_cleared" attributes {} on final
}
}
rule clean_up_prior_to_pico_deletion_part2b {
select when wrangler rulesets_need_to_cleanup
where ent:connections.keys().length() == 0
fired {
raise agent event "connections_cleared" attributes {}
}
}
rule clean_up_prior_to_pico_deletion_part3 {
select when wrangler rulesets_need_to_cleanup
before (agent pending_connections_cleared
and agent connections_cleared)
fired {
raise wrangler event "cleanup_finished"
attributes {"domain": meta:rid}
}
}
rule prepare_to_clean_up_connections {
select when agent connections_cleanup_requested
pre {
bad_connections =
ent:connections
.filter(function(v){v{"their_vk"}.isnull()})
clean_connections =
ent:connections
.filter(function(v){v{"their_vk"}})
}
send_directive("clean_connections",{
"bad_connections":bad_connections,
"clean_connections":clean_connections
})
fired {
raise agent event "ready_to_clean_up_connections" attributes {
"bad_connections":bad_connections,
"clean_connections":clean_connections
}
}
}
rule clean_up_connections {
select when agent ready_to_clean_up_connections
foreach event:attr("bad_connections") setting(conn)
pre {
label = conn{"label"}
eci = conn{"my_did"}
}
fired {
raise edge event "router_connection_deletion_requested"
attributes {"label":label};
raise wrangler event "channel_deletion_requested"
attributes {"eci":eci} if wrangler:channel(eci)
}
finally {
ent:connections := event:attr("clean_connections") on final
}
}
}