pico-engine 1.2.0 → 1.4.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
@@ -40,6 +40,8 @@ pico-engine uses [leveldb](http://leveldb.org) to store data. It's a C++ library
40
40
 
41
41
  Open command prompt as Administrator then run `npm install --global --production windows-build-tools` That will configure python and c++ compiler for npm.
42
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
+
43
45
  **Mac / Linux**
44
46
 
45
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`
@@ -11,18 +11,16 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  return t;
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.RulesetRegistryLoaderFs = void 0;
14
+ exports.RulesetRegistryLoaderFs = RulesetRegistryLoaderFs;
15
15
  const cross_fetch_1 = require("cross-fetch");
16
16
  const fs = require("fs");
17
- const leveldown_1 = require("leveldown");
18
- const levelup_1 = require("levelup");
17
+ const classic_level_1 = require("classic-level");
19
18
  const makeDir = require("make-dir");
20
19
  const path = require("path");
21
20
  const urlLib = require("url");
22
21
  const krlCompiler = require("krl-compiler");
23
22
  const krlCompilerVersion = require("krl-compiler/package.json").version;
24
23
  const charwise = require("charwise");
25
- const encode = require("encoding-down");
26
24
  const safeJsonCodec = require("level-json-coerce-null");
27
25
  function getUrlFilename(url) {
28
26
  try {
@@ -36,10 +34,10 @@ function getUrlFilename(url) {
36
34
  }
37
35
  function RulesetRegistryLoaderFs(engineHomeDir) {
38
36
  const rulesetDir = path.resolve(engineHomeDir, "rulesets");
39
- const db = (0, levelup_1.default)(encode((0, leveldown_1.default)(path.resolve(engineHomeDir, "rulesets-db")), {
37
+ const db = new classic_level_1.ClassicLevel(path.resolve(engineHomeDir, "rulesets-db"), {
40
38
  keyEncoding: charwise,
41
39
  valueEncoding: safeJsonCodec,
42
- }));
40
+ });
43
41
  async function compileAndLoad(url, krl, hash) {
44
42
  const jsFile = path.resolve(rulesetDir, krlCompilerVersion, hash.substr(0, 2), hash.substr(2, 2), hash + ".js");
45
43
  let compileWarnings = [];
@@ -93,7 +91,6 @@ function RulesetRegistryLoaderFs(engineHomeDir) {
93
91
  },
94
92
  };
95
93
  }
96
- exports.RulesetRegistryLoaderFs = RulesetRegistryLoaderFs;
97
94
  async function fetchKrl(url) {
98
95
  const urlParsed = urlLib.parse(url);
99
96
  if (urlParsed.protocol === "file:") {
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
  `);
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.startEngine = void 0;
3
+ exports.startEngine = startEngine;
4
4
  const krl_stdlib_1 = require("krl-stdlib");
5
- const leveldown_1 = require("leveldown");
5
+ const classic_level_1 = require("classic-level");
6
6
  const _ = require("lodash");
7
7
  const makeDir = require("make-dir");
8
8
  const path = require("path");
@@ -11,6 +11,8 @@ const logging_1 = require("./logging");
11
11
  const RulesetRegistryLoaderFs_1 = require("./RulesetRegistryLoaderFs");
12
12
  const server_1 = require("./server");
13
13
  const toFileUrl_1 = require("./utils/toFileUrl");
14
+ const charwise = require("charwise");
15
+ const safeJsonCodec = require("level-json-coerce-null");
14
16
  const homeDir = require("home-dir");
15
17
  const version = require("../package.json").version;
16
18
  async function startEngine(configuration = {}) {
@@ -26,7 +28,10 @@ async function startEngine(configuration = {}) {
26
28
  ? configuration.log
27
29
  : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeRotatingFileLogWriter)(logFilePath));
28
30
  const core = new pico_engine_core_1.PicoEngineCore({
29
- leveldown: (0, leveldown_1.default)(path.resolve(home, "db")),
31
+ db: new classic_level_1.ClassicLevel(path.resolve(home, "db"), {
32
+ keyEncoding: charwise,
33
+ valueEncoding: safeJsonCodec,
34
+ }),
30
35
  rsRegLoader: (0, RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs)(home),
31
36
  log,
32
37
  modules: configuration.modules,
@@ -42,6 +47,8 @@ async function startEngine(configuration = {}) {
42
47
  (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.pico-engine-ui.krl")),
43
48
  (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
44
49
  (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")),
50
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.did-o.krl")),
51
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.pds.krl")),
45
52
  ];
46
53
  for (const url of krl_urls) {
47
54
  const { ruleset } = await rsRegistry.flush(url);
@@ -108,4 +115,3 @@ async function startEngine(configuration = {}) {
108
115
  rsRegistry,
109
116
  };
110
117
  }
111
- exports.startEngine = startEngine;
package/dist/logging.js CHANGED
@@ -1,11 +1,18 @@
1
1
  "use strict";
2
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
3
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
4
+ var m = o[Symbol.asyncIterator], i;
5
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
6
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
7
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
+ };
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPicoLogs = exports.makeRotatingFileLogWriter = void 0;
10
+ exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
11
+ exports.getPicoLogs = getPicoLogs;
4
12
  const fs = require("fs");
13
+ const readline = require("readline");
5
14
  const krl_stdlib_1 = require("krl-stdlib");
6
15
  const path = require("path");
7
- const split = require("split");
8
- const through2 = require("through2");
9
16
  const rfs = require("rotating-file-stream");
10
17
  const logStreams = {};
11
18
  function getRotatingFileStream(filePath) {
@@ -17,7 +24,7 @@ function getRotatingFileStream(filePath) {
17
24
  return filename + "." + index;
18
25
  }, {
19
26
  path: path.dirname(filePath),
20
- size: "100M",
27
+ size: "100M", // rotate every 10 MegaBytes written
21
28
  maxFiles: 12,
22
29
  });
23
30
  }
@@ -34,17 +41,21 @@ function makeRotatingFileLogWriter(filePath) {
34
41
  }
35
42
  return write;
36
43
  }
37
- exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
38
44
  async function getPicoLogs(filePath, picoId) {
39
- return new Promise((resolve, reject) => {
40
- const output = [];
41
- fs.createReadStream(filePath)
42
- .pipe(split())
43
- .pipe(through2((chunk, enc, next) => {
44
- const line = chunk.toString();
45
+ var _a, e_1, _b, _c;
46
+ const output = [];
47
+ const rl = readline.createInterface({
48
+ input: fs.createReadStream(filePath),
49
+ });
50
+ try {
51
+ for (var _d = true, rl_1 = __asyncValues(rl), rl_1_1; rl_1_1 = await rl_1.next(), _a = rl_1_1.done, !_a; _d = true) {
52
+ _c = rl_1_1.value;
53
+ _d = false;
54
+ const line = _c;
55
+ // Each line in the readline input will be successively available here as
56
+ // `line`.
45
57
  if (line.indexOf(picoId) < 0) {
46
- // not my pico
47
- return next();
58
+ continue; // not my pico
48
59
  }
49
60
  let entry;
50
61
  try {
@@ -52,25 +63,23 @@ async function getPicoLogs(filePath, picoId) {
52
63
  }
53
64
  catch (err) { }
54
65
  if (!entry || entry.picoId !== picoId) {
55
- // not my pico
56
- return next();
66
+ continue; // not my pico
57
67
  }
58
68
  const time = new Date(entry.time);
59
69
  if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
60
- // too old
61
- return next();
70
+ continue; // too old
62
71
  }
63
72
  const out = Object.assign(Object.assign({}, entry), { level: krl_stdlib_1.krlLogLevelCodeToHuman[entry.level] || `${entry.level}`, time: entry.time, txnId: entry.txnId });
64
73
  delete out.picoId;
65
74
  output.push(out);
66
- next();
67
- }))
68
- .on("finish", () => {
69
- resolve(output);
70
- })
71
- .on("error", (err) => {
72
- reject(err);
73
- });
74
- });
75
+ }
76
+ }
77
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
78
+ finally {
79
+ try {
80
+ if (!_d && !_a && (_b = rl_1.return)) await _b.call(rl_1);
81
+ }
82
+ finally { if (e_1) throw e_1.error; }
83
+ }
84
+ return output;
75
85
  }
76
- exports.getPicoLogs = getPicoLogs;
package/dist/server.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.server = void 0;
3
+ exports.server = server;
4
4
  const bodyParser = require("body-parser");
5
5
  const express = require("express");
6
- const helmet = require("helmet");
6
+ const helmet_1 = require("helmet");
7
7
  const _ = require("lodash");
8
8
  const path = require("path");
9
9
  const engineVersion = require("../package.json").version;
@@ -29,7 +29,13 @@ function server(core, uiECI) {
29
29
  next();
30
30
  }
31
31
  });
32
- app.use(helmet());
32
+ // Helmet 7+ CSP defaults include upgrade-insecure-requests, which breaks the
33
+ // UI on plain HTTP (browser upgrades /api/* to https and TLS fails).
34
+ app.use((0, helmet_1.default)({
35
+ contentSecurityPolicy: {
36
+ directives: Object.assign(Object.assign({}, helmet_1.default.contentSecurityPolicy.getDefaultDirectives()), { "upgrade-insecure-requests": null }),
37
+ },
38
+ }));
33
39
  app.use(express.static(path.resolve(__dirname, "..", "public")));
34
40
  app.use(bodyParser.json({
35
41
  type: [
@@ -226,4 +232,3 @@ function server(core, uiECI) {
226
232
  });
227
233
  return app;
228
234
  }
229
- exports.server = server;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toFileUrl = void 0;
3
+ exports.toFileUrl = toFileUrl;
4
4
  const path = require("path");
5
5
  function toFileUrl(str) {
6
6
  var pathName = path.resolve(str).replace(/\\/g, "/");
@@ -9,4 +9,3 @@ function toFileUrl(str) {
9
9
  }
10
10
  return encodeURI("file://" + pathName);
11
11
  }
12
- exports.toFileUrl = toFileUrl;
@@ -0,0 +1,345 @@
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, generate_message
28
+
29
+ shares routes, addRoute, didDocs, clearDidDocs, didMap, clearDidMap, pendingRotations, clearPendingRotations, generate_invitation, generate_message
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
+ * Generates and returns a DIDComm v2 message with the given message.
144
+ * message is formatted as follows:
145
+ * {
146
+ * type: string,
147
+ * body: any,
148
+ * from?: string,
149
+ * to?: [string],
150
+ * thid?: string,
151
+ * pthid?: string,
152
+ * expires_time?: number,
153
+ * attachments?: [Attachment]
154
+ * }
155
+ *
156
+ * Refer to didcomm-node package for Attachment object specifics.
157
+ */
158
+ generate_message = function(message) {
159
+ dido:generateMessage(message)
160
+ }
161
+
162
+ /**
163
+ * Creates an invitation OOB URL that can be used to establish DID based relationships.
164
+ * This automates the DID, channel, and message creation and encodes it into a base64 Out-of-Band URL.
165
+ */
166
+ generate_invitation = function(label) {
167
+ DIDdoc = dido:generateDID(true)
168
+ new_did = DIDdoc{"id"}
169
+ invitation = dido:generateMessage({
170
+ "type": "https://didcomm.org/out-of-band/2.0/invitation",
171
+ "from": new_did,
172
+ "body": {
173
+ "goal_code": "exchange-did",
174
+ "goal": "ExchangeDid",
175
+ "label": label || "PicoInvite",
176
+ "accept": [
177
+ "didcomm/v2"
178
+ ]
179
+ }
180
+ })
181
+ base64 = math:base64encode(invitation.encode())
182
+ dido:createInviteUrl(base64)
183
+ }
184
+
185
+ /**
186
+ * Creates a trust ping message according to the DIDComm v2 protocol and returns it. The to parameter is the DID of the recipient.
187
+ */
188
+ generate_trust_ping_message = function(to) {
189
+ dido:generateMessage({
190
+ "type": "https://didcomm.org/trust_ping/2.0/ping",
191
+ "from": ent:didMap{to},
192
+ "to": [to],
193
+ "body": {
194
+ "response_requested": true
195
+ }
196
+ })
197
+ }
198
+
199
+ /**
200
+ * Creates a trust ping response message according to the DIDComm v2 protocol and returns it.
201
+ * The thid parameter is the id of the ping that you are responding to, and the to parameter is the DID of the recipient.
202
+ */
203
+ generate_trust_ping_response = function(thid, to) {
204
+ dido:generateMessage({
205
+ "type": "https://didcomm.org/trust_ping/2.0/ping_response",
206
+ "thid": thid,
207
+ "from": ent:didMap{to},
208
+ "to": [to],
209
+ "body": {}
210
+ })
211
+ }
212
+ }
213
+
214
+ ///////////////////////////////////////////// INITIAILIZATION //////////////////////////////////////////////
215
+
216
+ /**
217
+ * The initialize rule subscribes to the wrangler:ruleset_installed event and is called when the ruleset is installed and initializes the routes.
218
+ */
219
+ rule intialize {
220
+ select when wrangler ruleset_installed where event:attrs{"rids"} >< meta:rid
221
+ pre {
222
+ ping = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping", "dido", "receive_trust_ping")
223
+ ping_response = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping_response", "dido", "receive_trust_ping_response")
224
+ }
225
+ }
226
+
227
+ /**
228
+ * The pico_root_created rule subscribes to the engine_ui:setup event and is called when the engine starts and initializes the routes.
229
+ * This is used in addition to the initialize rule to verify routes exist when a new engine is started.
230
+ */
231
+ rule pico_root_created {
232
+ select when engine_ui setup
233
+ if ent:routes.isnull() then noop()
234
+ fired {
235
+ ping = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping", "dido", "receive_trust_ping")
236
+ ping_response = dido:addRoute("https://didcomm.org/trust_ping/2.0/ping_response", "dido", "receive_trust_ping_response")
237
+ }
238
+ }
239
+
240
+ ///////////////////////////////////////////// ROUTING //////////////////////////////////////////////
241
+
242
+ /**
243
+ * The route_message rule subscribes to the dido:didcommv2_message event. It calls the dido library,
244
+ * unpacks the message, and looks for the proper route based on the message type. It then raises the
245
+ * appropriate event and passes along the unpacked message.
246
+ */
247
+ rule route_message {
248
+ select when dido didcommv2_message
249
+ pre {
250
+ response = dido:route(event:attrs.delete("_headers"))
251
+ }
252
+ if response then send_directive("response", {}.put(response))
253
+ }
254
+
255
+ ///////////////////////////////////////////// INVITATIONS //////////////////////////////////////////////
256
+
257
+ /**
258
+ * The receive_invite rule subscribes to the dido:receive_invite event and receives an invite URL (example.com/invite?_oob=abc...123)
259
+ * from event:attrs{"invite"}. The invite URL is a base 64 encoded invite. The rule then creates a new DID and channel.
260
+ * After that, it raises the dido:send_trust_ping event.
261
+ */
262
+ rule receive_invite {
263
+ select when dido receive_invite
264
+ pre {
265
+ url = event:attrs{"invite"}
266
+ base64 = url.split("_oob=")[1]
267
+ invite = math:base64decode(base64).decode()
268
+ new_did = dido:generateDID()
269
+ didMap = dido:mapDid(invite{"from"}, new_did{"id"})
270
+ stored_doc = dido:storeDidDoc(invite{"from"})
271
+ }
272
+ fired {
273
+ raise dido event "send_trust_ping" attributes event:attrs.put("did", invite{"from"})
274
+ }
275
+ }
276
+
277
+ ///////////////////////////////////////////// TRUST PING //////////////////////////////////////////////
278
+
279
+ /**
280
+ * The send_trust_ping rule subscribes to the dido:send_trust_ping event and requires the DID of the recipient
281
+ * from event:attrs{"did"}. It then sends a generated trust ping message to the provided DID.
282
+ */
283
+ rule send_trust_ping {
284
+ select when dido send_trust_ping
285
+ pre {
286
+ their_did = event:attrs{"did"}
287
+ message = generate_trust_ping_message(their_did)
288
+ send = dido:send(their_did, message)
289
+ }
290
+ }
291
+
292
+ /**
293
+ * The receive_trust_ping rule subscibes to the did:receive_trust_ping event and requires the ping message from
294
+ * event:attrs{"message"}. It then checks if the message was received from an invite. If it was, it rotates the
295
+ * invite DID. Then it generates the response and sends it back.
296
+ */
297
+ rule receive_trust_ping {
298
+ select when dido receive_trust_ping
299
+ pre {
300
+ message = event:attrs{"message"}
301
+ their_did = message{"from"}
302
+ needsRotation = dido:rotateInviteDID(message{"to"}[0], message{"from"})
303
+ response = generate_trust_ping_response(message{"id"}, their_did)
304
+ send = dido:send(their_did, response)
305
+ }
306
+ }
307
+ }
308
+
309
+
310
+ /*
311
+ DID-O - V 1.0.0
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
+ */