pico-engine 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # pico-engine
2
2
 
3
- [![Build Status](https://travis-ci.org/Picolab/pico-engine.svg?branch=master)](https://travis-ci.org/Picolab/pico-engine)
4
3
  [![Node version](https://img.shields.io/node/v/pico-engine.svg)](https://nodejs.org/en/download/)
5
4
 
6
5
  An implementation of the [pico-engine](http://www.windley.com/archives/2016/03/rebuilding_krl.shtml) hosted on node.js
@@ -41,6 +40,8 @@ pico-engine uses [leveldb](http://leveldb.org) to store data. It's a C++ library
41
40
 
42
41
  Open command prompt as Administrator then run `npm install --global --production windows-build-tools` That will configure python and c++ compiler for npm.
43
42
 
43
+ We have had reports that the `pico-engine` command stops working with earlier versions of `node.js`, so update to the latest version.
44
+
44
45
  **Mac / Linux**
45
46
 
46
47
  Be sure you have python 2.7 installed. If python 3 is the system default, all you need to do is configure npm to use python 2.7 like so `npm config set python /path/to/executable/python2.7`
@@ -36,7 +36,7 @@ function getUrlFilename(url) {
36
36
  }
37
37
  function RulesetRegistryLoaderFs(engineHomeDir) {
38
38
  const rulesetDir = path.resolve(engineHomeDir, "rulesets");
39
- const db = levelup_1.default(encode(leveldown_1.default(path.resolve(engineHomeDir, "rulesets-db")), {
39
+ const db = (0, levelup_1.default)(encode((0, leveldown_1.default)(path.resolve(engineHomeDir, "rulesets-db")), {
40
40
  keyEncoding: charwise,
41
41
  valueEncoding: safeJsonCodec,
42
42
  }));
@@ -106,7 +106,7 @@ async function fetchKrl(url) {
106
106
  });
107
107
  });
108
108
  }
109
- const res = await cross_fetch_1.default(url);
109
+ const res = await (0, cross_fetch_1.default)(url);
110
110
  if (res.status < 200 || res.status >= 300) {
111
111
  throw new Error("Got a statusCode=" + res.status + " for: " + url);
112
112
  }
package/dist/cli.js CHANGED
@@ -20,7 +20,7 @@ USAGE
20
20
 
21
21
  Environment Variables:
22
22
 
23
- PORT - The port the http server should listen on. By default it's 8080
23
+ PORT - The port the http server should listen on. By default it's 3000
24
24
  PICO_ENGINE_HOME - Where the database and other files should be stored. By default it's ~/.pico-engine/
25
25
  PICO_ENGINE_BASE_URL - The public url prefix to reach this engine. By default it's http://localhost:$PORT
26
26
  `);
@@ -41,7 +41,7 @@ if (typeof process.env.PICO_ENGINE_BASE_URL === "string" &&
41
41
  process.env.PICO_ENGINE_BASE_URL.length > 0) {
42
42
  env.base_url = process.env.PICO_ENGINE_BASE_URL;
43
43
  }
44
- index_1.startEngine(env).catch((err) => {
44
+ (0, index_1.startEngine)(env).catch((err) => {
45
45
  console.error("Failed to start engine.");
46
46
  console.error(err);
47
47
  process.exit(1);
package/dist/index.js CHANGED
@@ -24,24 +24,25 @@ async function startEngine(configuration = {}) {
24
24
  const logFilePath = path.resolve(home, "pico-engine.log");
25
25
  const log = configuration.log
26
26
  ? configuration.log
27
- : krl_stdlib_1.makeKrlLogger(logging_1.makeRotatingFileLogWriter(logFilePath));
27
+ : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeRotatingFileLogWriter)(logFilePath));
28
28
  const core = new pico_engine_core_1.PicoEngineCore({
29
- leveldown: leveldown_1.default(path.resolve(home, "db")),
30
- rsRegLoader: RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs(home),
29
+ leveldown: (0, leveldown_1.default)(path.resolve(home, "db")),
30
+ rsRegLoader: (0, RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs)(home),
31
31
  log,
32
32
  modules: configuration.modules,
33
33
  useEventInputTime: configuration.useEventInputTime,
34
34
  getPicoLogs(picoId) {
35
- return logging_1.getPicoLogs(logFilePath, picoId);
35
+ return (0, logging_1.getPicoLogs)(logFilePath, picoId);
36
36
  },
37
37
  });
38
38
  await core.start();
39
39
  const rsRegistry = core.rsRegistry;
40
40
  const pf = core.picoFramework;
41
41
  const krl_urls = [
42
- toFileUrl_1.toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.pico-engine-ui.krl")),
43
- toFileUrl_1.toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
44
- toFileUrl_1.toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")),
42
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.pico-engine-ui.krl")),
43
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
44
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")),
45
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.did-o.krl")),
45
46
  ];
46
47
  for (const url of krl_urls) {
47
48
  const { ruleset } = await rsRegistry.flush(url);
@@ -67,7 +68,7 @@ async function startEngine(configuration = {}) {
67
68
  time: 0,
68
69
  });
69
70
  const uiECI = uiChannel.id;
70
- const app = server_1.server(core, uiECI);
71
+ const app = (0, server_1.server)(core, uiECI);
71
72
  if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
72
73
  port = 3000;
73
74
  }
@@ -0,0 +1,325 @@
1
+ /*
2
+ DID-O V 1.0.0
3
+ ⠀⠀⠀⢠⡜⠛⠛⠿⣤⠀⠀⣤⡼⠿⠿⢧⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
4
+ ⠀⣀⡶⠎⠁⠀⠀⠀⠉⠶⠶⠉⠁⠀⠀⠈⠹⢆⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
5
+ ⣀⡿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠶⠶⠶⠶⣆⡀⠀⠀⠀⠀
6
+ ⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢣⡄⠀⠀⠀
7
+ ⠛⣧⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀
8
+ ⠀⠛⣧⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⠀⠀⠀⠀⢠⡼⠃⠀⠀
9
+ ⠀⠀⠿⢇⡀⠀⠀⠀⠀⠀⠀⠀⠰⠶⠶⢆⣀⣀⣀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀
10
+ ⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠉⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀
11
+ ⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀
12
+ ⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢣⣤
13
+ ⠀⣶⡏⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿
14
+ ⠀⠿⣇⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣀⣀⣀⣀⣀⠀⠀⠀⠀⢀⣀⣸⠿
15
+ ⠀⠀⠙⢳⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⡞⠛⠛⠛⠛⠛⠛⣶⣶⣶⣶⡞⠛⠃⠀
16
+ */
17
+
18
+ ruleset io.picolabs.did-o {
19
+ meta {
20
+ name "did-o"
21
+ description
22
+ <<
23
+ Ruleset for DIDComm v2 Communication Using Picos
24
+ >>
25
+ author "Rembrand Paul Pardo, Kekoapoaono Montalbo, Josh Mann"
26
+
27
+ provides addRoute, routes, send, sendEvent, sendQuery, didMap, clearDidMap, didDocs, clearDidDocs, pendingRotations, clearPendingRotations, generate_invitation
28
+
29
+ shares routes, addRoute, didDocs, clearDidDocs, didMap, clearDidMap, pendingRotations, clearPendingRotations, generate_invitation
30
+
31
+ use module io.picolabs.wrangler alias wrangler
32
+ }
33
+
34
+
35
+ global {
36
+ ///////////////////////////////////////////// ROUTING //////////////////////////////////////////////
37
+
38
+ /**
39
+ * Adds a route to the routes stored in the dido.ts library. The type should be a valid Protocol URI.
40
+ * The domain and name are the domain and name of the event that will be raised when a message of the specified type is received.
41
+ */
42
+ addRoute = function(type, _domain, name) {
43
+ dido:addRoute(type, _domain, name)
44
+ }
45
+
46
+ /**
47
+ * Queries the existing routes.
48
+ */
49
+ routes = function() {
50
+ ent:routes
51
+ }
52
+
53
+ /**
54
+ * Send a message to the specified DID. did is the recipient DID and message is a valid DIDComm v2 message.
55
+ */
56
+ send = function(did, message) {
57
+ dido:send(did, message)
58
+ }
59
+
60
+ /**
61
+ * Send an event to the specified DID. did is the recipient DID and the event is formatted as follows:
62
+ * event = {
63
+ * "domain": domain: string,
64
+ * "name": name: string,
65
+ * "attrs": attrs: {[name: string]: any}
66
+ * }
67
+ */
68
+ sendEvent = function(did, event) {
69
+ message = dido:generateMessage({
70
+ "type": "https://picolabs.io/event/1.0/event",
71
+ "from": ent:didMap{did},
72
+ "to": [did],
73
+ "body": event
74
+ })
75
+ dido:send(did, message)
76
+ }
77
+
78
+ /**
79
+ * Send a query to the specified DID. did is the recipient DID and the query is formatted as follows:
80
+ * query = {
81
+ * "rid": rid: string,
82
+ * "name": name: string,
83
+ * "args": args: {[name: string]: any}
84
+ * }
85
+ */
86
+ sendQuery = function(did, _query) {
87
+ message = dido:generateMessage({
88
+ "type": "https://picolabs.io/query/1.0/query",
89
+ "from": ent:didMap{did},
90
+ "to": [did],
91
+ "body": _query
92
+ })
93
+ dido:sendQuery(did, message)
94
+ }
95
+
96
+ ///////////////////////////////////////////// DID MANAGEMENT //////////////////////////////////////////////
97
+
98
+ /**
99
+ * Return the map of DID relationships stored in the pattern [ thier_did : your_did ].
100
+ */
101
+ didMap = function() {
102
+ ent:didMap
103
+ }
104
+
105
+ /**
106
+ * Clear the map of DID relationships.
107
+ */
108
+ clearDidMap = function() {
109
+ dido:clearDidMap()
110
+ }
111
+
112
+ /**
113
+ * Return all the DIDs and DIDDocs stored in the pattern [ did : diddoc ].
114
+ */
115
+ didDocs = function() {
116
+ ent:didDocs
117
+ }
118
+
119
+ /**
120
+ * Clear all the DIDs and DIDDocs.
121
+ */
122
+ clearDidDocs = function() {
123
+ dido:clearDidDocs()
124
+ }
125
+
126
+ /**
127
+ * Return all the pending rotations stored in the pattern [ new_did : from_prior ].
128
+ */
129
+ pendingRotations = function() {
130
+ ent:pendingRotations
131
+ }
132
+
133
+ /**
134
+ * Clear all the pending rotations.
135
+ */
136
+ clearPendingRotations = function() {
137
+ dido:clearPendingRotations()
138
+ }
139
+
140
+ ///////////////////////////////////////////// MESSAGE CREATORS //////////////////////////////////////////////
141
+
142
+ /**
143
+ * Creates an invitation OOB URL that can be used to establish DID based relationships.
144
+ * This automates the DID, channel, and message creation and encodes it into a base64 Out-of-Band URL.
145
+ */
146
+ generate_invitation = function() {
147
+ DIDdoc = dido:generateDID(true)
148
+ new_did = DIDdoc{"id"}
149
+ invitation = dido:generateMessage({
150
+ "type": "https://didcomm.org/out-of-band/2.0/invitation",
151
+ "from": new_did,
152
+ "body": {
153
+ "goal_code": "exchange-did",
154
+ "goal": "ExchangeDid",
155
+ "label": "PicoInvite",
156
+ "accept": [
157
+ "didcomm/v2"
158
+ ]
159
+ }
160
+ })
161
+ base64 = math:base64encode(invitation.encode())
162
+ dido:createInviteUrl(base64)
163
+ }
164
+
165
+ /**
166
+ * Creates a trust ping message according to the DIDComm v2 protocol and returns it. The to parameter is the DID of the recipient.
167
+ */
168
+ generate_trust_ping_message = function(to) {
169
+ dido:generateMessage({
170
+ "type": "https://didcomm.org/trust_ping/2.0/ping",
171
+ "from": ent:didMap{to},
172
+ "to": [to],
173
+ "body": {
174
+ "response_requested": true
175
+ }
176
+ })
177
+ }
178
+
179
+ /**
180
+ * Creates a trust ping response message according to the DIDComm v2 protocol and returns it.
181
+ * The thid parameter is the id of the ping that you are responding to, and the to parameter is the DID of the recipient.
182
+ */
183
+ generate_trust_ping_response = function(thid, to) {
184
+ dido:generateMessage({
185
+ "type": "https://didcomm.org/trust_ping/2.0/ping_response",
186
+ "thid": thid,
187
+ "from": ent:didMap{to},
188
+ "to": [to],
189
+ "body": {}
190
+ })
191
+ }
192
+ }
193
+
194
+ ///////////////////////////////////////////// INITIAILIZATION //////////////////////////////////////////////
195
+
196
+ /**
197
+ * The initialize rule subscribes to the wrangler:ruleset_installed event and is called when the ruleset is installed and initializes the routes.
198
+ */
199
+ rule intialize {
200
+ select when wrangler ruleset_installed where event:attrs{"rids"} >< meta:rid
201
+ pre {
202
+ ping = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping", "dido", "receive_trust_ping")
203
+ ping_response = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping_response", "dido", "receive_trust_ping_response")
204
+ }
205
+ }
206
+
207
+ /**
208
+ * The pico_root_created rule subscribes to the engine_ui:setup event and is called when the engine starts and initializes the routes.
209
+ * This is used in addition to the initialize rule to verify routes exist when a new engine is started.
210
+ */
211
+ rule pico_root_created {
212
+ select when engine_ui setup
213
+ if ent:routes.isnull() then noop()
214
+ fired {
215
+ ping = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping", "dido", "receive_trust_ping")
216
+ ping_response = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping_response", "dido", "receive_trust_ping_response")
217
+ }
218
+ }
219
+
220
+ ///////////////////////////////////////////// ROUTING //////////////////////////////////////////////
221
+
222
+ /**
223
+ * The route_message rule subscribes to the dido:didcommv2_message event. It calls the dido library,
224
+ * unpacks the message, and looks for the proper route based on the message type. It then raises the
225
+ * appropriate event and passes along the unpacked message.
226
+ */
227
+ rule route_message {
228
+ select when dido didcommv2_message
229
+ pre {
230
+ response = dido:route(event:attrs.delete("_headers"))
231
+ }
232
+ if response then send_directive("response", {}.put(response))
233
+ }
234
+
235
+ ///////////////////////////////////////////// INVITATIONS //////////////////////////////////////////////
236
+
237
+ /**
238
+ * The receive_invite rule subscribes to the dido:receive_invite event and receives an invite URL (example.com/invite?_oob=abc...123)
239
+ * from event:attrs{"invite"}. The invite URL is a base 64 encoded invite. The rule then creates a new DID and channel.
240
+ * After that, it raises the dido:send_trust_ping event.
241
+ */
242
+ rule receive_invite {
243
+ select when dido receive_invite
244
+ pre {
245
+ url = event:attrs{"invite"}
246
+ base64 = url.split("_oob=")[1]
247
+ invite = math:base64decode(base64).decode()
248
+ new_did = dido:generateDID()
249
+ didMap = dido:mapDid(invite{"from"}, new_did{"id"})
250
+ stored_doc = dido:storeDidDoc(invite{"from"})
251
+ }
252
+ fired {
253
+ raise dido event "send_trust_ping" attributes event:attrs.put("did", invite{"from"})
254
+ }
255
+ }
256
+
257
+ ///////////////////////////////////////////// TRUST PING //////////////////////////////////////////////
258
+
259
+ /**
260
+ * The send_trust_ping rule subscribes to the dido:send_trust_ping event and requires the DID of the recipient
261
+ * from event:attrs{"did"}. It then sends a generated trust ping message to the provided DID.
262
+ */
263
+ rule send_trust_ping {
264
+ select when dido send_trust_ping
265
+ pre {
266
+ their_did = event:attrs{"did"}
267
+ message = generate_trust_ping_message(their_did)
268
+ send = dido:send(their_did, message)
269
+ }
270
+ }
271
+
272
+ /**
273
+ * The receive_trust_ping rule subscibes to the did:receive_trust_ping event and requires the ping message from
274
+ * event:attrs{"message"}. It then checks if the message was received from an invite. If it was, it rotates the
275
+ * invite DID. Then it generates the response and sends it back.
276
+ */
277
+ rule receive_trust_ping {
278
+ select when dido receive_trust_ping
279
+ pre {
280
+ message = event:attrs{"message"}
281
+ their_did = message{"from"}
282
+ needsRotation = dido:rotateInviteDID(message{"to"}[0], message{"from"})
283
+ response = generate_trust_ping_response(message{"id"}, their_did)
284
+ send = dido:send(their_did, response)
285
+ }
286
+ }
287
+ }
288
+
289
+
290
+ /*
291
+ DID-O - V 1.0.0
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
+ */
@@ -13,7 +13,8 @@ ruleset io.picolabs.pico-engine-ui {
13
13
  .head()
14
14
  }
15
15
  getOtherUiECI = function(eci){
16
- return eci => ctx:query(eci, ctx:rid, "uiECI") | null
16
+ thisPico = ctx:channels.any(function(c){c{"id"}==eci})
17
+ return (eci && not thisPico) => ctx:query(eci, ctx:rid, "uiECI") | null
17
18
  }
18
19
  testingECI = function(){
19
20
  return ent:testingECI
@@ -87,10 +88,13 @@ ruleset io.picolabs.pico-engine-ui {
87
88
  };
88
89
  entryMap = function(a,e){
89
90
  // a.head() is array of entries; a[1] is whether last entry is eats
91
+ // a[2] is domain:name of the event
90
92
  eats = e{"msg"} == "event added to schedule"
91
- a[1] && eats
92
- => [a.head(),true] // omit an eats
93
- | [a.head().append(episode_line(e)),eats]
93
+ event_dt = eats => e{["event","domain"]} + ":" + e{["event","name"]}
94
+ | ""
95
+ a[1] && eats && a[2]==event_dt
96
+ => [a.head(),true,event_dt] // omit a duplicate eats
97
+ | [a.head().append(episode_line(e)),eats,event_dt]
94
98
  }
95
99
  episodes = ctx:logs()
96
100
  .collect(function(e){e.get("txnId")})
@@ -98,7 +102,7 @@ ruleset io.picolabs.pico-engine-ui {
98
102
  .map(function(k){
99
103
  episode = episodes.get(k)
100
104
  entries = episode
101
- .reduce(entryMap,[[],false]).head()
105
+ .reduce(entryMap,[[],false,""]).head()
102
106
  return {
103
107
  "txnId": k,
104
108
  "time": episode.head().get("time"),
@@ -107,8 +111,8 @@ ruleset io.picolabs.pico-engine-ui {
107
111
  }
108
112
  })
109
113
  .filter(function(g){
110
- g.header.match(re#^QUERY .*io.picolabs.pico-engine-ui/#) => false |
111
- g.header.match(re#^QUERY .*io.picolabs.subscription/established#) => false |
114
+ g{"header"}.match(re#^QUERY .*io.picolabs.pico-engine-ui/#) => false |
115
+ g{"header"}.match(re#^QUERY .*io.picolabs.subscription/established#) => false |
112
116
  true
113
117
  })
114
118
  .reverse()