@twin.org/node-core 0.0.2-next.26 → 0.0.2-next.27

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.
Files changed (89) hide show
  1. package/README.md +6 -20
  2. package/dist/es/bootstrap.js +374 -0
  3. package/dist/es/bootstrap.js.map +1 -0
  4. package/dist/es/builders/engineEnvBuilder.js +1051 -0
  5. package/dist/es/builders/engineEnvBuilder.js.map +1 -0
  6. package/dist/es/builders/engineServerEnvBuilder.js +197 -0
  7. package/dist/es/builders/engineServerEnvBuilder.js.map +1 -0
  8. package/dist/es/builders/extensionsBuilder.js +100 -0
  9. package/dist/es/builders/extensionsBuilder.js.map +1 -0
  10. package/dist/es/defaults.js +9 -0
  11. package/dist/es/defaults.js.map +1 -0
  12. package/dist/es/identity.js +169 -0
  13. package/dist/es/identity.js.map +1 -0
  14. package/dist/es/index.js +23 -0
  15. package/dist/es/index.js.map +1 -0
  16. package/dist/es/models/ICacheMetadata.js +4 -0
  17. package/dist/es/models/ICacheMetadata.js.map +1 -0
  18. package/dist/es/models/IEngineEnvironmentVariables.js +4 -0
  19. package/dist/es/models/IEngineEnvironmentVariables.js.map +1 -0
  20. package/dist/es/models/IEngineServerEnvironmentVariables.js +2 -0
  21. package/dist/es/models/IEngineServerEnvironmentVariables.js.map +1 -0
  22. package/dist/es/models/IModuleProtocol.js +2 -0
  23. package/dist/es/models/IModuleProtocol.js.map +1 -0
  24. package/dist/es/models/INodeEngineConfig.js +2 -0
  25. package/dist/es/models/INodeEngineConfig.js.map +1 -0
  26. package/dist/es/models/INodeEngineState.js +2 -0
  27. package/dist/es/models/INodeEngineState.js.map +1 -0
  28. package/dist/es/models/INodeEnvironmentVariables.js +2 -0
  29. package/dist/es/models/INodeEnvironmentVariables.js.map +1 -0
  30. package/dist/es/models/INodeOptions.js +2 -0
  31. package/dist/es/models/INodeOptions.js.map +1 -0
  32. package/dist/es/models/IProtocolHandlerResult.js +4 -0
  33. package/dist/es/models/IProtocolHandlerResult.js.map +1 -0
  34. package/dist/es/models/moduleProtocol.js +29 -0
  35. package/dist/es/models/moduleProtocol.js.map +1 -0
  36. package/dist/es/models/nodeExtensionMethods.js +2 -0
  37. package/dist/es/models/nodeExtensionMethods.js.map +1 -0
  38. package/dist/es/models/nodeFeatures.js +21 -0
  39. package/dist/es/models/nodeFeatures.js.map +1 -0
  40. package/dist/es/node.js +265 -0
  41. package/dist/es/node.js.map +1 -0
  42. package/dist/es/server.js +74 -0
  43. package/dist/es/server.js.map +1 -0
  44. package/dist/es/utils.js +418 -0
  45. package/dist/es/utils.js.map +1 -0
  46. package/dist/types/bootstrap.d.ts +27 -10
  47. package/dist/types/builders/engineEnvBuilder.d.ts +3 -2
  48. package/dist/types/builders/engineServerEnvBuilder.d.ts +3 -2
  49. package/dist/types/builders/extensionsBuilder.d.ts +2 -2
  50. package/dist/types/identity.d.ts +14 -0
  51. package/dist/types/index.d.ts +20 -19
  52. package/dist/types/models/IEngineEnvironmentVariables.d.ts +12 -5
  53. package/dist/types/models/IEngineServerEnvironmentVariables.d.ts +1 -1
  54. package/dist/types/models/IModuleProtocol.d.ts +1 -1
  55. package/dist/types/models/INodeEngineState.d.ts +22 -0
  56. package/dist/types/models/INodeEnvironmentVariables.d.ts +24 -8
  57. package/dist/types/models/INodeOptions.d.ts +13 -3
  58. package/dist/types/models/nodeExtensionMethods.d.ts +2 -2
  59. package/dist/types/models/nodeFeatures.d.ts +5 -5
  60. package/dist/types/node.d.ts +14 -5
  61. package/dist/types/server.d.ts +7 -6
  62. package/dist/types/utils.d.ts +5 -5
  63. package/docs/detailed-guide.md +14 -14
  64. package/docs/reference/functions/bootstrap.md +1 -1
  65. package/docs/reference/functions/bootstrapAuth.md +1 -1
  66. package/docs/reference/functions/bootstrapBlobEncryption.md +1 -1
  67. package/docs/reference/functions/bootstrapContextIdHandlers.md +35 -0
  68. package/docs/reference/functions/bootstrapImmutableProofMethod.md +1 -1
  69. package/docs/reference/functions/{bootstrapNodeUser.md → bootstrapNodeAdminUser.md} +3 -3
  70. package/docs/reference/functions/{bootstrapNodeIdentity.md → bootstrapNodeId.md} +3 -3
  71. package/docs/reference/functions/bootstrapSynchronisedStorage.md +1 -1
  72. package/docs/reference/functions/bootstrapTenantId.md +35 -0
  73. package/docs/reference/functions/buildConfiguration.md +2 -2
  74. package/docs/reference/functions/buildEngineConfiguration.md +7 -1
  75. package/docs/reference/functions/buildEngineServerConfiguration.md +7 -1
  76. package/docs/reference/functions/run.md +3 -3
  77. package/docs/reference/functions/start.md +8 -2
  78. package/docs/reference/index.md +5 -2
  79. package/docs/reference/interfaces/IEngineEnvironmentVariables.md +24 -9
  80. package/docs/reference/interfaces/IEngineServerEnvironmentVariables.md +36 -13
  81. package/docs/reference/interfaces/INodeEngineConfig.md +430 -0
  82. package/docs/reference/interfaces/INodeEngineState.md +39 -0
  83. package/docs/reference/interfaces/INodeEnvironmentVariables.md +79 -24
  84. package/docs/reference/interfaces/INodeOptions.md +21 -1
  85. package/docs/reference/variables/NodeFeatures.md +7 -7
  86. package/locales/en.json +10 -7
  87. package/package.json +40 -8
  88. package/dist/cjs/index.cjs +0 -2570
  89. package/dist/esm/index.mjs +0 -2508
@@ -1,2508 +0,0 @@
1
- import { PasswordHelper } from '@twin.org/api-auth-entity-storage-service';
2
- import { I18n, Is, Converter, GeneralError, BaseError, Coerce, RandomHelper, Urn, EnvHelper } from '@twin.org/core';
3
- import { Sha256, PasswordGenerator, Bip39 } from '@twin.org/crypto';
4
- import { AuthenticationComponentType, InformationComponentType, RestRouteProcessorType, SocketRouteProcessorType, AuthenticationAdminComponentType } from '@twin.org/engine-server-types';
5
- import { WalletConnectorType, IdentityConnectorType, EntityStorageConnectorType, BlobStorageConnectorType, BlobStorageComponentType, VaultConnectorType, DltConfigType, LoggingConnectorType, LoggingComponentType, BackgroundTaskConnectorType, TaskSchedulerComponentType, EventBusConnectorType, EventBusComponentType, TelemetryConnectorType, TelemetryComponentType, MessagingEmailConnectorType, MessagingSmsConnectorType, MessagingPushNotificationConnectorType, MessagingAdminComponentType, MessagingComponentType, FaucetConnectorType, EngineTypeHelper, NftConnectorType, NftComponentType, VerifiableStorageConnectorType, VerifiableStorageComponentType, ImmutableProofComponentType, IdentityComponentType, IdentityResolverConnectorType, IdentityResolverComponentType, IdentityProfileConnectorType, IdentityProfileComponentType, AttestationConnectorType, AttestationComponentType, DataProcessingComponentType, DataConverterConnectorType, DataExtractorConnectorType, AuditableItemGraphComponentType, AuditableItemStreamComponentType, DocumentManagementComponentType, AuthenticationGeneratorComponentType, RightsManagementPapComponentType, RightsManagementPmpComponentType, RightsManagementPipComponentType, RightsManagementPxpComponentType, RightsManagementPdpComponentType, RightsManagementPepComponentType, RightsManagementPnpComponentType, RightsManagementPnapComponentType, RightsManagementDapComponentType, RightsManagementDarpComponentType, SynchronisedStorageComponentType, FederatedCatalogueComponentType, DataSpaceConnectorComponentType } from '@twin.org/engine-types';
6
- import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
7
- import { IdentityProfileConnectorFactory, IdentityConnectorFactory, IdentityResolverConnectorFactory, DocumentHelper } from '@twin.org/identity-models';
8
- import { VaultConnectorFactory, VaultKeyType } from '@twin.org/vault-models';
9
- import { WalletConnectorFactory } from '@twin.org/wallet-models';
10
- import { execSync } from 'node:child_process';
11
- import { readFile, stat, readdir, mkdir, writeFile, rename } from 'node:fs/promises';
12
- import { get } from 'node:https';
13
- import path from 'node:path';
14
- import { CLIDisplay } from '@twin.org/cli-core';
15
- import { ModuleHelper } from '@twin.org/modules';
16
- import { PolicyNegotiationPointRestClient, DataAccessPointRestClient } from '@twin.org/rights-management-rest-client';
17
- import { addDefaultRestPaths, addDefaultSocketPaths, EngineServer } from '@twin.org/engine-server';
18
- import * as dotenv from 'dotenv';
19
- import { Engine } from '@twin.org/engine';
20
- import { FileStateStorage } from '@twin.org/engine-core';
21
- import { EngineCoreFactory } from '@twin.org/engine-models';
22
-
23
- // Copyright 2024 IOTA Stiftung.
24
- // SPDX-License-Identifier: Apache-2.0.
25
- const ATTESTATION_VERIFICATION_METHOD_ID = "attestation-assertion";
26
- const IMMUTABLE_PROOF_VERIFICATION_METHOD_ID = "immutable-proof-assertion";
27
- const BLOB_STORAGE_ENCRYPTION_KEY_ID = "blob-encryption";
28
- const SYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID = "synchronised-storage-blob-encryption";
29
- const VC_AUTHENTICATION_VERIFICATION_METHOD_ID = "node-authentication-assertion";
30
- const AUTH_SIGNING_KEY_ID = "auth-signing";
31
-
32
- // Copyright 2024 IOTA Stiftung.
33
- // SPDX-License-Identifier: Apache-2.0.
34
- /**
35
- * The features that can be enabled on the node.
36
- */
37
- // eslint-disable-next-line @typescript-eslint/naming-convention
38
- const NodeFeatures = {
39
- /**
40
- * NodeIdentity - generates an identity for the node if not provided in config.
41
- */
42
- NodeIdentity: "node-identity",
43
- /**
44
- * NodeUser - generates a user for the node if not provided in config.
45
- */
46
- NodeUser: "node-user",
47
- /**
48
- * NodeWallet - generates a wallet for the node and funds it when there is a faucet available.
49
- */
50
- NodeWallet: "node-wallet"
51
- };
52
-
53
- // Copyright 2024 IOTA Stiftung.
54
- // SPDX-License-Identifier: Apache-2.0.
55
- /**
56
- * The protocol types for modules.
57
- */
58
- // eslint-disable-next-line @typescript-eslint/naming-convention
59
- const ModuleProtocol = {
60
- /**
61
- * Local module (starts with . or / or file://).
62
- */
63
- Local: "local",
64
- /**
65
- * NPM package (starts with npm:).
66
- */
67
- Npm: "npm",
68
- /**
69
- * HTTPS URL (starts with https://).
70
- */
71
- Https: "https",
72
- /**
73
- * HTTP URL (starts with http://).
74
- */
75
- Http: "http",
76
- /**
77
- * Default/standard module resolution.
78
- */
79
- Default: "default"
80
- };
81
-
82
- // Copyright 2024 IOTA Stiftung.
83
- // SPDX-License-Identifier: Apache-2.0.
84
- /**
85
- * Initialise the locales for the application.
86
- * @param localesDirectory The directory containing the locales.
87
- */
88
- async function initialiseLocales(localesDirectory) {
89
- const localesFile = path.resolve(path.join(localesDirectory, "en.json"));
90
- CLIDisplay.value("Locales File", localesFile);
91
- if (await fileExists(localesFile)) {
92
- const enLangContent = await readFile(localesFile, "utf8");
93
- I18n.addDictionary("en", JSON.parse(enLangContent));
94
- }
95
- else {
96
- CLIDisplay.error(`Locales file not found: ${localesFile}`);
97
- }
98
- }
99
- /**
100
- * Get the directory where the application is being executed.
101
- * @returns The execution directory.
102
- */
103
- function getExecutionDirectory() {
104
- return process.cwd();
105
- }
106
- /**
107
- * Does the specified file exist.
108
- * @param filename The filename to check for existence.
109
- * @returns True if the file exists.
110
- */
111
- async function fileExists(filename) {
112
- try {
113
- const stats = await stat(filename);
114
- return stats.isFile();
115
- }
116
- catch {
117
- return false;
118
- }
119
- }
120
- /**
121
- * Does the specified directory exist.
122
- * @param directory The directory to check for existence.
123
- * @returns True if the directory exists.
124
- */
125
- async function directoryExists(directory) {
126
- try {
127
- const stats = await stat(directory);
128
- return stats.isDirectory();
129
- }
130
- catch {
131
- return false;
132
- }
133
- }
134
- /**
135
- * Get the sub folders for the folder.
136
- * @param directory The directory to get the sub folders.
137
- * @returns The list of sub folders.
138
- */
139
- async function getSubFolders(directory) {
140
- try {
141
- const dir = await readdir(directory);
142
- const folders = [];
143
- for (const dirEntry of dir) {
144
- const fullPath = path.join(directory, dirEntry);
145
- const stats = await stat(fullPath);
146
- if (stats.isDirectory()) {
147
- folders.push(fullPath);
148
- }
149
- }
150
- return folders;
151
- }
152
- catch {
153
- return [];
154
- }
155
- }
156
- /**
157
- * Get the files in the directory.
158
- * @param directory The directory to get the files from.
159
- * @returns The list of files in the directory.
160
- */
161
- async function getFiles(directory) {
162
- try {
163
- const dir = await readdir(directory);
164
- const files = [];
165
- for (const dirEntry of dir) {
166
- const fullPath = path.join(directory, dirEntry);
167
- const stats = await stat(fullPath);
168
- if (stats.isFile()) {
169
- files.push(fullPath);
170
- }
171
- }
172
- return files;
173
- }
174
- catch {
175
- return [];
176
- }
177
- }
178
- /**
179
- * Load the text file.
180
- * @param filename The filename of the text file to load.
181
- * @returns The contents of the text file if it could not be loaded.
182
- */
183
- async function loadTextFile(filename) {
184
- return readFile(filename, "utf8");
185
- }
186
- /**
187
- * Load the JSON file.
188
- * @param filename The filename of the JSON file to load.
189
- * @returns The contents of the JSON file or null if it could not be loaded.
190
- */
191
- async function loadJsonFile(filename) {
192
- const content = await loadTextFile(filename);
193
- return JSON.parse(content);
194
- }
195
- /**
196
- * Get the features that are enabled on the node.
197
- * @param env The environment variables for the node.
198
- * @returns The features that are enabled on the node.
199
- */
200
- function getFeatures(env) {
201
- if (Is.empty(env.features)) {
202
- return [];
203
- }
204
- const features = [];
205
- const allFeatures = Object.values(NodeFeatures);
206
- const splitFeatures = env.features.split(",");
207
- for (const feature of splitFeatures) {
208
- const featureTrimmed = feature.trim();
209
- if (allFeatures.includes(featureTrimmed)) {
210
- features.push(featureTrimmed);
211
- }
212
- }
213
- return features;
214
- }
215
- /**
216
- * Parse the protocol from a module name.
217
- * @param moduleName The module name to parse.
218
- * @returns The parsed protocol information.
219
- */
220
- function parseModuleProtocol(moduleName) {
221
- const trimmed = moduleName.trim();
222
- if (trimmed.startsWith("npm:")) {
223
- return {
224
- protocol: ModuleProtocol.Npm,
225
- identifier: trimmed.slice(4),
226
- original: trimmed
227
- };
228
- }
229
- if (trimmed.startsWith("https://")) {
230
- return {
231
- protocol: ModuleProtocol.Https,
232
- identifier: trimmed,
233
- original: trimmed
234
- };
235
- }
236
- if (trimmed.startsWith("http://")) {
237
- return {
238
- protocol: ModuleProtocol.Http,
239
- identifier: trimmed,
240
- original: trimmed
241
- };
242
- }
243
- if (trimmed.startsWith("file://")) {
244
- return {
245
- protocol: ModuleProtocol.Local,
246
- identifier: trimmed,
247
- original: trimmed
248
- };
249
- }
250
- if (ModuleHelper.isLocalModule(trimmed)) {
251
- return {
252
- protocol: ModuleProtocol.Local,
253
- identifier: trimmed,
254
- original: trimmed
255
- };
256
- }
257
- return {
258
- protocol: ModuleProtocol.Default,
259
- identifier: trimmed,
260
- original: trimmed
261
- };
262
- }
263
- /**
264
- * Hash a URL to create a safe filename.
265
- * @param url The URL to hash.
266
- * @returns A hashed filename safe for the filesystem.
267
- */
268
- function hashUrl(url) {
269
- const urlBytes = Converter.utf8ToBytes(url);
270
- const hashBytes = Sha256.sum256(urlBytes);
271
- const hash = Converter.bytesToHex(hashBytes);
272
- const ext = path.extname(new URL(url).pathname);
273
- return `${hash}${ext}`;
274
- }
275
- /**
276
- * Get the extensions cache directory.
277
- * @param executionDirectory The execution directory.
278
- * @param protocol The protocol type for subdirectory organization.
279
- * @param cacheDirectory The cache directory base path.
280
- * @returns The cache directory path.
281
- */
282
- function getExtensionsCacheDir(executionDirectory, protocol, cacheDirectory) {
283
- // Resolve to absolute path to ensure consistent behavior
284
- const absoluteDir = path.resolve(executionDirectory);
285
- const baseDir = cacheDirectory ?? ".tmp";
286
- return path.join(absoluteDir, baseDir, "extensions", protocol);
287
- }
288
- /**
289
- * Handle the npm: protocol by installing the package if needed.
290
- * @param packageName The npm package name (without npm: prefix).
291
- * @param executionDirectory The execution directory.
292
- * @param cacheDirectory The cache directory base path.
293
- * @returns The resolved path to the installed module.
294
- */
295
- async function handleNpmProtocol(packageName, executionDirectory, cacheDirectory) {
296
- const cacheDir = getExtensionsCacheDir(executionDirectory, ModuleProtocol.Npm, cacheDirectory);
297
- // Extract just the package name (without version) for the directory
298
- // e.g. "picocolors@1.0.0" becomes "picocolors"
299
- // e.g. "@scope/package@1.0.0" becomes "@scope/package"
300
- const lastAtIndex = packageName.lastIndexOf("@");
301
- const packageNameOnly = lastAtIndex > 0 ? packageName.slice(0, lastAtIndex) : packageName;
302
- const packageDir = path.join(cacheDir, "node_modules", packageNameOnly);
303
- const packageJsonPath = path.join(packageDir, "package.json");
304
- const exists = await fileExists(packageJsonPath);
305
- if (exists) {
306
- const mainFile = await resolvePackageEntryPoint(packageDir, packageNameOnly);
307
- const modulePath = path.join(packageDir, mainFile);
308
- return {
309
- resolvedPath: modulePath,
310
- cached: true
311
- };
312
- }
313
- await mkdir(cacheDir, { recursive: true });
314
- CLIDisplay.task(I18n.formatMessage("node.extensionNpmInstalling"), packageName);
315
- try {
316
- // Always pipe stdio to comply with env access restrictions in tests/lint
317
- const stdio = "pipe";
318
- execSync(`npm install ${packageName} --prefix "${cacheDir}" --no-save --no-package-lock`, {
319
- cwd: cacheDir,
320
- stdio
321
- });
322
- }
323
- catch (err) {
324
- throw new GeneralError("node", "extensionNpmInstallFailed", {
325
- package: packageName
326
- }, BaseError.fromError(err));
327
- }
328
- const mainFile = await resolvePackageEntryPoint(packageDir, packageNameOnly);
329
- const modulePath = path.join(packageDir, mainFile);
330
- return {
331
- resolvedPath: modulePath,
332
- cached: false
333
- };
334
- }
335
- /**
336
- * Check if a cached file has expired based on TTL and force refresh settings.
337
- * @param metadataPath Path to the cache metadata file.
338
- * @param ttlHours Time to live in hours.
339
- * @param forceRefresh Whether to force refresh regardless of TTL.
340
- * @returns True if the cache is expired or should be refreshed.
341
- */
342
- async function isCacheExpired(metadataPath, ttlHours, forceRefresh) {
343
- if (forceRefresh) {
344
- return true;
345
- }
346
- try {
347
- const metadata = await loadJsonFile(metadataPath);
348
- const ttlMillis = ttlHours * 60 * 60 * 1000;
349
- const expireTime = metadata.downloadedAt + ttlMillis;
350
- return Date.now() > expireTime;
351
- }
352
- catch {
353
- // If metadata doesn't exist or is corrupted, consider expired
354
- return true;
355
- }
356
- }
357
- /**
358
- * Handle the https: protocol by downloading the module if needed.
359
- * @param url The HTTPS URL to download from.
360
- * @param executionDirectory The execution directory.
361
- * @param maxSizeMb The maximum size in MB for the download.
362
- * @param cacheDirectory The cache directory base path.
363
- * @param ttlHours TTL in hours for cache expiration.
364
- * @param forceRefresh Whether to force refresh the cache.
365
- * @returns The resolved path to the downloaded module.
366
- */
367
- async function handleHttpsProtocol(url, executionDirectory, maxSizeMb, cacheDirectory, ttlHours, forceRefresh) {
368
- const effectiveTtlHours = ttlHours ?? 24;
369
- const effectiveForceRefresh = forceRefresh ?? false;
370
- const cacheDir = getExtensionsCacheDir(executionDirectory, ModuleProtocol.Https, cacheDirectory);
371
- const filename = hashUrl(url);
372
- const cachedPath = path.join(cacheDir, filename);
373
- const metadataPath = `${cachedPath}.meta`;
374
- const exists = await fileExists(cachedPath);
375
- if (exists) {
376
- const expired = await isCacheExpired(metadataPath, effectiveTtlHours, effectiveForceRefresh);
377
- if (!expired) {
378
- return {
379
- resolvedPath: cachedPath,
380
- cached: true
381
- };
382
- }
383
- if (effectiveForceRefresh) {
384
- CLIDisplay.warning(I18n.formatMessage("node.extensionForceRefresh", { url }));
385
- }
386
- else {
387
- CLIDisplay.task(I18n.formatMessage("node.extensionCacheExpired", { url }));
388
- }
389
- }
390
- CLIDisplay.warning(I18n.formatMessage("node.extensionSecurityWarning", { url }));
391
- CLIDisplay.task(I18n.formatMessage("node.extensionHttpsDownloading"), url);
392
- await mkdir(cacheDir, { recursive: true });
393
- const maxSizeBytes = maxSizeMb * 1024 * 1024;
394
- let downloadedSize = 0;
395
- const chunks = [];
396
- try {
397
- await new Promise((resolve, reject) => {
398
- get(url, response => {
399
- if (response.statusCode !== 200) {
400
- reject(new GeneralError("node", "extensionDownloadFailed", {
401
- url,
402
- status: response.statusCode ?? 0
403
- }));
404
- return;
405
- }
406
- response.on("data", (chunk) => {
407
- downloadedSize += chunk.length;
408
- if (downloadedSize > maxSizeBytes) {
409
- response.destroy();
410
- reject(new GeneralError("node", "extensionSizeLimitExceeded", {
411
- size: downloadedSize,
412
- limit: maxSizeBytes
413
- }));
414
- return;
415
- }
416
- chunks.push(chunk);
417
- });
418
- response.on("end", () => {
419
- resolve();
420
- });
421
- response.on("error", err => {
422
- reject(BaseError.fromError(err));
423
- });
424
- }).on("error", err => {
425
- reject(BaseError.fromError(err));
426
- });
427
- });
428
- }
429
- catch (err) {
430
- throw new GeneralError("node", "extensionDownloadFailed", {
431
- url
432
- }, BaseError.fromError(err));
433
- }
434
- const tempPath = `${cachedPath}.tmp`;
435
- await writeFile(tempPath, Buffer.concat(chunks));
436
- // Atomic move from temp to final location
437
- await rename(tempPath, cachedPath);
438
- // Save metadata for TTL tracking
439
- const metadata = {
440
- downloadedAt: Date.now(),
441
- url,
442
- size: Buffer.concat(chunks).length
443
- };
444
- await writeFile(metadataPath, JSON.stringify(metadata, null, 2));
445
- return {
446
- resolvedPath: cachedPath,
447
- cached: false
448
- };
449
- }
450
- /**
451
- * Resolve the main entry point from a package directory using Node.js resolution with fallback.
452
- * Uses require.resolve() when possible for standard Node.js behavior, with manual fallback.
453
- * @param packagePath The absolute path to the package directory.
454
- * @param packageName The package name for require.resolve().
455
- * @param fallback The fallback file name if no entry point is found.
456
- * @returns The resolved entry point file name (relative to package directory).
457
- */
458
- async function resolvePackageEntryPoint(packagePath, packageName, fallback = "index.js") {
459
- try {
460
- // Try require.resolve() first - handles exports, main, module automatically
461
- const resolvedPath = require.resolve(packageName, { paths: [path.dirname(packagePath)] });
462
- // Convert absolute path back to relative filename within package
463
- const relativePath = path.relative(packagePath, resolvedPath);
464
- return relativePath ?? fallback;
465
- }
466
- catch {
467
- // Fallback to manual package.json parsing if require.resolve fails
468
- try {
469
- const packageJsonPath = path.join(packagePath, "package.json");
470
- const packageJsonContent = await loadJsonFile(packageJsonPath);
471
- // Future: Could expand exports field support here
472
- return packageJsonContent.module ?? packageJsonContent.main ?? fallback;
473
- }
474
- catch {
475
- return fallback;
476
- }
477
- }
478
- }
479
- /**
480
- * Convert a file path to an import-compatible URL for cross-platform module loading.
481
- * On Windows, adds the 'file://' protocol prefix required for dynamic imports.
482
- * On other platforms, returns the path unchanged.
483
- * @param filePath The absolute file path to convert.
484
- * @returns A URL string compatible with dynamic import().
485
- */
486
- function createModuleImportUrl(filePath) {
487
- return process.platform === "win32" ? `file://${filePath}` : filePath;
488
- }
489
-
490
- // Copyright 2024 IOTA Stiftung.
491
- // SPDX-License-Identifier: Apache-2.0.
492
- const DEFAULT_NODE_USERNAME = "admin@node";
493
- /**
494
- * Bootstrap the application.
495
- * @param engineCore The engine core for the node.
496
- * @param context The context for the node.
497
- * @param envVars The environment variables for the node.
498
- */
499
- async function bootstrap(engineCore, context, envVars) {
500
- const features = getFeatures(envVars);
501
- await bootstrapNodeIdentity(engineCore, context, envVars, features);
502
- await bootstrapNodeUser(engineCore, context, envVars, features);
503
- await bootstrapAuth(engineCore, context, envVars);
504
- await bootstrapBlobEncryption(engineCore, context, envVars);
505
- const defaultAttestationConnectorType = engineCore.getRegisteredInstanceTypeOptional("attestationConnector");
506
- if (!Is.empty(defaultAttestationConnectorType)) {
507
- await addVerificationMethod(engineCore, context, "attestation", envVars.attestationVerificationMethodId ?? ATTESTATION_VERIFICATION_METHOD_ID);
508
- }
509
- const defaultImmutableProofComponentType = engineCore.getRegisteredInstanceTypeOptional("immutableProofComponent");
510
- if (!Is.empty(defaultImmutableProofComponentType)) {
511
- await addVerificationMethod(engineCore, context, "immutable proof", envVars.immutableProofVerificationMethodId ?? IMMUTABLE_PROOF_VERIFICATION_METHOD_ID);
512
- }
513
- if (Coerce.boolean(envVars.vcAuthenticationEnabled) ?? false) {
514
- await addVerificationMethod(engineCore, context, "verifiable credential authentication", envVars.vcAuthenticationVerificationMethodId ?? VC_AUTHENTICATION_VERIFICATION_METHOD_ID);
515
- }
516
- await bootstrapSynchronisedStorage(engineCore, context, envVars);
517
- }
518
- /**
519
- * Bootstrap the node creating any necessary resources.
520
- * @param engineCore The engine core for the node.
521
- * @param context The context for the node.
522
- * @param envVars The environment variables for the node.
523
- * @param features The features that are enabled on the node. The features that are enabled on the node.
524
- */
525
- async function bootstrapNodeIdentity(engineCore, context, envVars, features) {
526
- if (features.includes(NodeFeatures.NodeIdentity)) {
527
- // When we bootstrap the node we need to generate an identity for it,
528
- // But we have a chicken and egg problem in that we can't create the identity
529
- // to store the mnemonic in the vault without an identity. We use a temporary identity
530
- // and then replace it with the new identity later in the process.
531
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
532
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
533
- const workingIdentity = envVars.identity ??
534
- context.state.nodeIdentity ??
535
- `bootstrap-temp-${Converter.bytesToHex(RandomHelper.generate(16))}`;
536
- await bootstrapMnemonic(engineCore, envVars, features, vaultConnector, workingIdentity);
537
- const addresses = await bootstrapWallet(engineCore, envVars, features, workingIdentity);
538
- const finalIdentity = await bootstrapIdentity(engineCore, envVars, features, workingIdentity);
539
- await finaliseWallet(engineCore, envVars, features, finalIdentity, addresses);
540
- await finaliseMnemonic(vaultConnector, workingIdentity, finalIdentity);
541
- context.state.nodeIdentity = finalIdentity;
542
- context.stateDirty = true;
543
- engineCore.logInfo(I18n.formatMessage("node.nodeIdentity", {
544
- identity: context.state.nodeIdentity
545
- }));
546
- }
547
- }
548
- /**
549
- * Bootstrap the identity for the node.
550
- * @param engineCore The engine core for the node.
551
- * @param envVars The environment variables for the node.
552
- * @param features The features that are enabled on the node. The features that are enabled on the node.
553
- * @param nodeIdentity The identity of the node.
554
- * @returns The addresses for the wallet.
555
- */
556
- async function bootstrapIdentity(engineCore, envVars, features, nodeIdentity) {
557
- const defaultIdentityConnectorType = engineCore.getRegisteredInstanceType("identityConnector");
558
- // Now create an identity for the node controlled by the address we just funded
559
- const identityConnector = IdentityConnectorFactory.get(defaultIdentityConnectorType);
560
- let identityDocument;
561
- try {
562
- const defaultIdentityResolverConnectorType = engineCore.getRegisteredInstanceType("identityResolverConnector");
563
- const identityResolverConnector = IdentityResolverConnectorFactory.get(defaultIdentityResolverConnectorType);
564
- identityDocument = await identityResolverConnector.resolveDocument(nodeIdentity);
565
- engineCore.logInfo(I18n.formatMessage("node.existingNodeIdentity", { identity: nodeIdentity }));
566
- }
567
- catch { }
568
- if (Is.empty(identityDocument)) {
569
- engineCore.logInfo(I18n.formatMessage("node.generatingNodeIdentity"));
570
- identityDocument = await identityConnector.createDocument(nodeIdentity);
571
- engineCore.logInfo(I18n.formatMessage("node.createdNodeIdentity", { identity: identityDocument.id }));
572
- }
573
- if (defaultIdentityConnectorType.startsWith(IdentityConnectorType.Iota)) {
574
- const didUrn = Urn.fromValidString(identityDocument.id);
575
- const didParts = didUrn.parts();
576
- const objectId = didParts[3];
577
- engineCore.logInfo(I18n.formatMessage("node.identityExplorer", {
578
- url: `${envVars.iotaExplorerEndpoint}object/${objectId}?network=${envVars.iotaNetwork}`
579
- }));
580
- }
581
- return identityDocument.id;
582
- }
583
- /**
584
- * Bootstrap the wallet for the node.
585
- * @param engineCore The engine core for the node.
586
- * @param envVars The environment variables for the node.
587
- * @param features The features that are enabled on the node.
588
- * @param nodeIdentity The identity of the node.
589
- * @returns The addresses for the wallet.
590
- */
591
- async function bootstrapWallet(engineCore, envVars, features, nodeIdentity) {
592
- if (features.includes(NodeFeatures.NodeWallet)) {
593
- const defaultWalletConnectorType = engineCore.getRegisteredInstanceType("walletConnector");
594
- const walletConnector = WalletConnectorFactory.get(defaultWalletConnectorType);
595
- const addresses = await walletConnector.getAddresses(nodeIdentity, 0, 0, 5);
596
- const balance = await walletConnector.getBalance(nodeIdentity, addresses[0]);
597
- if (balance === 0n) {
598
- let address0 = addresses[0];
599
- if (defaultWalletConnectorType.startsWith(WalletConnectorType.Iota)) {
600
- address0 = `${envVars.iotaExplorerEndpoint}address/${address0}?network=${envVars.iotaNetwork}`;
601
- }
602
- engineCore.logInfo(I18n.formatMessage("node.fundingWallet", { address: address0 }));
603
- // Add some funds to the wallet from the faucet
604
- await walletConnector.ensureBalance(nodeIdentity, addresses[0], 1000000000n);
605
- }
606
- else {
607
- engineCore.logInfo(I18n.formatMessage("node.fundedWallet"));
608
- }
609
- return addresses;
610
- }
611
- return [];
612
- }
613
- /**
614
- * Bootstrap the identity for the node.
615
- * @param engineCore The engine core for the node.
616
- * @param envVars The environment variables for the node.
617
- * @param features The features that are enabled on the node.
618
- * @param finalIdentity The identity of the node.
619
- * @param addresses The addresses for the wallet.
620
- */
621
- async function finaliseWallet(engineCore, envVars, features, finalIdentity, addresses) {
622
- if (features.includes(NodeFeatures.NodeWallet)) {
623
- const defaultWalletConnectorType = engineCore.getRegisteredInstanceType("walletConnector");
624
- // If we are using entity storage for wallet the identity associated with the
625
- // address will be wrong, so fix it
626
- if (defaultWalletConnectorType.startsWith(WalletConnectorType.EntityStorage)) {
627
- const walletAddress = EntityStorageConnectorFactory.get("wallet-address");
628
- const addr = await walletAddress.get(addresses[0]);
629
- if (!Is.empty(addr)) {
630
- addr.identity = finalIdentity;
631
- await walletAddress.set(addr);
632
- }
633
- }
634
- }
635
- }
636
- /**
637
- * Generate a mnemonic for the node identity.
638
- * @param engineCore The engine core for the node.
639
- * @param envVars The environment variables for the node.
640
- * @param features The features that are enabled on the node.
641
- * @param vaultConnector The vault connector to use.
642
- * @param nodeIdentity The identity of the node.
643
- */
644
- async function bootstrapMnemonic(engineCore, envVars, features, vaultConnector, nodeIdentity) {
645
- let mnemonic = envVars.mnemonic;
646
- let storeMnemonic = false;
647
- try {
648
- const storedMnemonic = await vaultConnector.getSecret(`${nodeIdentity}/mnemonic`);
649
- storeMnemonic = storedMnemonic !== mnemonic;
650
- mnemonic = storedMnemonic;
651
- }
652
- catch {
653
- storeMnemonic = true;
654
- }
655
- // If there is no mnemonic then we need to generate one
656
- if (Is.empty(mnemonic)) {
657
- mnemonic = Bip39.randomMnemonic();
658
- storeMnemonic = true;
659
- engineCore.logInfo(I18n.formatMessage("node.generatingMnemonic", { mnemonic }));
660
- }
661
- // If there is no mnemonic stored in the vault then we need to store it
662
- if (storeMnemonic) {
663
- engineCore.logInfo(I18n.formatMessage("node.storingMnemonic"));
664
- await vaultConnector.setSecret(`${nodeIdentity}/mnemonic`, mnemonic);
665
- }
666
- else {
667
- engineCore.logInfo(I18n.formatMessage("node.existingMnemonic"));
668
- }
669
- }
670
- /**
671
- * Finalise the mnemonic for the node identity.
672
- * @param vaultConnector The vault connector to use.
673
- * @param workingIdentity The identity of the node.
674
- * @param finalIdentity The final identity for the node.
675
- */
676
- async function finaliseMnemonic(vaultConnector, workingIdentity, finalIdentity) {
677
- // Now that we have an identity we can remove the temporary one
678
- // and store the mnemonic with the new identity
679
- if (workingIdentity.startsWith("bootstrap-temp-") && workingIdentity !== finalIdentity) {
680
- const mnemonic = await vaultConnector.getSecret(`${workingIdentity}/mnemonic`);
681
- await vaultConnector.setSecret(`${finalIdentity}/mnemonic`, mnemonic);
682
- await vaultConnector.removeSecret(`${workingIdentity}/mnemonic`);
683
- }
684
- }
685
- /**
686
- * Bootstrap the user.
687
- * @param engineCore The engine core for the node.
688
- * @param context The context for the node.
689
- * @param envVars The environment variables for the node.
690
- * @param features The features that are enabled on the node.
691
- */
692
- async function bootstrapNodeUser(engineCore, context, envVars, features) {
693
- if (features.includes(NodeFeatures.NodeUser)) {
694
- const defaultAuthenticationComponentType = engineCore.getRegisteredInstanceType("authenticationComponent");
695
- if (defaultAuthenticationComponentType.startsWith(AuthenticationComponentType.EntityStorage) &&
696
- Is.stringValue(context.state.nodeIdentity)) {
697
- const authUserEntityStorage = EntityStorageConnectorFactory.get("authentication-user");
698
- const email = envVars.username ?? DEFAULT_NODE_USERNAME;
699
- let nodeAdminUser = await authUserEntityStorage.get(email);
700
- if (Is.empty(nodeAdminUser)) {
701
- engineCore.logInfo(I18n.formatMessage("node.creatingNodeUser", { email }));
702
- const generatedPassword = envVars.password ?? PasswordGenerator.generate(16);
703
- const passwordBytes = Converter.utf8ToBytes(generatedPassword);
704
- const saltBytes = RandomHelper.generate(16);
705
- const hashedPassword = await PasswordHelper.hashPassword(passwordBytes, saltBytes);
706
- nodeAdminUser = {
707
- email,
708
- password: hashedPassword,
709
- salt: Converter.bytesToBase64(saltBytes),
710
- identity: context.state.nodeIdentity
711
- };
712
- engineCore.logInfo(I18n.formatMessage("node.nodeAdminUserEmail", { email: nodeAdminUser.email }));
713
- engineCore.logInfo(I18n.formatMessage("node.nodeAdminUserPassword", { password: generatedPassword }));
714
- await authUserEntityStorage.set(nodeAdminUser);
715
- }
716
- else {
717
- engineCore.logInfo(I18n.formatMessage("node.existingNodeUser", { email }));
718
- // The user already exists, so double check the other details match
719
- let needsUpdate = false;
720
- if (nodeAdminUser.identity !== context.state.nodeIdentity) {
721
- nodeAdminUser.identity = context.state.nodeIdentity;
722
- needsUpdate = true;
723
- }
724
- if (Is.stringValue(envVars.password)) {
725
- const passwordBytes = Converter.utf8ToBytes(envVars.password);
726
- const saltBytes = Converter.base64ToBytes(nodeAdminUser.salt);
727
- const hashedPassword = await PasswordHelper.hashPassword(passwordBytes, saltBytes);
728
- if (nodeAdminUser.password !== hashedPassword) {
729
- nodeAdminUser.password = hashedPassword;
730
- needsUpdate = true;
731
- }
732
- }
733
- if (needsUpdate) {
734
- await authUserEntityStorage.set(nodeAdminUser);
735
- }
736
- }
737
- // We have create a node user, now we need to create a profile for the user
738
- const defaultIdentityProfileConnectorType = engineCore.getRegisteredInstanceType("identityProfileConnector");
739
- const identityProfileConnector = IdentityProfileConnectorFactory.get(defaultIdentityProfileConnectorType);
740
- if (identityProfileConnector) {
741
- let userProfile;
742
- try {
743
- userProfile = await identityProfileConnector.get(context.state.nodeIdentity);
744
- }
745
- catch { }
746
- if (Is.empty(userProfile)) {
747
- engineCore.logInfo(I18n.formatMessage("node.creatingUserProfile", { identity: context.state.nodeIdentity }));
748
- const publicProfile = {
749
- "@context": "https://schema.org",
750
- "@type": "Person",
751
- name: "Node Administrator"
752
- };
753
- const privateProfile = {
754
- "@context": "https://schema.org",
755
- "@type": "Person",
756
- givenName: "Node",
757
- familyName: "Administrator",
758
- email
759
- };
760
- await identityProfileConnector.create(context.state.nodeIdentity, publicProfile, privateProfile);
761
- }
762
- else {
763
- engineCore.logInfo(I18n.formatMessage("node.existingUserProfile", { identity: context.state.nodeIdentity }));
764
- }
765
- }
766
- }
767
- }
768
- }
769
- /**
770
- * Bootstrap the immutable proof verification methods.
771
- * @param engineCore The engine core for the node.
772
- * @param context The context for the node.
773
- * @param envVars The environment variables for the node.
774
- * @param features The features that are enabled on the node.
775
- */
776
- async function bootstrapImmutableProofMethod(engineCore, context, envVars, features) { }
777
- /**
778
- * Bootstrap the keys for blob encryption.
779
- * @param engineCore The engine core for the node.
780
- * @param context The context for the node.
781
- * @param envVars The environment variables for the node.
782
- * @param features The features that are enabled on the node.
783
- */
784
- async function bootstrapBlobEncryption(engineCore, context, envVars, features) {
785
- if ((Coerce.boolean(envVars.blobStorageEnableEncryption) ?? false) &&
786
- Is.stringValue(context.state.nodeIdentity)) {
787
- // Create a new key for encrypting blobs
788
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
789
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
790
- const keyName = `${context.state.nodeIdentity}/${envVars.blobStorageEncryptionKeyId ?? BLOB_STORAGE_ENCRYPTION_KEY_ID}`;
791
- let existingKey;
792
- try {
793
- existingKey = await vaultConnector.getKey(keyName);
794
- }
795
- catch { }
796
- if (Is.empty(existingKey)) {
797
- if (Is.stringBase64(envVars.blobStorageSymmetricEncryptionKey)) {
798
- engineCore.logInfo(I18n.formatMessage("node.addingBlobEncryptionKey", { keyName }));
799
- await vaultConnector.addKey(keyName, VaultKeyType.ChaCha20Poly1305, Converter.base64ToBytes(envVars.blobStorageSymmetricEncryptionKey));
800
- }
801
- else {
802
- engineCore.logInfo(I18n.formatMessage("node.creatingBlobEncryptionKey", { keyName }));
803
- const key = await vaultConnector.createKey(keyName, VaultKeyType.ChaCha20Poly1305);
804
- engineCore.logInfo(I18n.formatMessage("node.createdBlobEncryptionKey", {
805
- keyName,
806
- keyValue: Converter.bytesToBase64(key)
807
- }));
808
- }
809
- }
810
- else {
811
- engineCore.logInfo(I18n.formatMessage("node.existingBlobEncryptionKey", { keyName }));
812
- }
813
- }
814
- }
815
- /**
816
- * Bootstrap the JWT signing key.
817
- * @param engineCore The engine core for the node.
818
- * @param context The context for the node.
819
- * @param envVars The environment variables for the node.
820
- * @param features The features that are enabled on the node.
821
- */
822
- async function bootstrapAuth(engineCore, context, envVars, features) {
823
- const defaultAuthenticationComponentType = engineCore.getRegisteredInstanceTypeOptional("authenticationComponent");
824
- if (Is.stringValue(defaultAuthenticationComponentType) &&
825
- defaultAuthenticationComponentType.startsWith(AuthenticationComponentType.EntityStorage) &&
826
- Is.stringValue(context.state.nodeIdentity)) {
827
- // Create a new JWT signing key and a user login for the node
828
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
829
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
830
- const keyName = `${context.state.nodeIdentity}/${envVars.authSigningKeyId ?? AUTH_SIGNING_KEY_ID}`;
831
- let existingKey;
832
- try {
833
- existingKey = await vaultConnector.getKey(keyName);
834
- }
835
- catch { }
836
- if (Is.empty(existingKey)) {
837
- engineCore.logInfo(I18n.formatMessage("node.creatingAuthKey", { keyName }));
838
- await vaultConnector.createKey(keyName, VaultKeyType.Ed25519);
839
- }
840
- else {
841
- engineCore.logInfo(I18n.formatMessage("node.existingAuthKey", { keyName }));
842
- }
843
- }
844
- }
845
- /**
846
- * Bootstrap the synchronised storage blob encryption and verification methods.
847
- * @param engineCore The engine core for the node.
848
- * @param context The context for the node.
849
- * @param envVars The environment variables for the node.
850
- * @param features The features that are enabled on the node.
851
- */
852
- async function bootstrapSynchronisedStorage(engineCore, context, envVars, features) {
853
- if (Coerce.boolean(envVars.synchronisedStorageEnabled) ?? false) {
854
- // If this is a trusted node we need to add the blob encryption key pair
855
- if (Is.stringBase64(envVars.synchronisedStorageBlobStorageKey)) {
856
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
857
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
858
- const keyName = envVars.synchronisedStorageBlobStorageEncryptionKeyId ??
859
- SYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID;
860
- let existingKey;
861
- try {
862
- existingKey = await vaultConnector.getKey(keyName);
863
- }
864
- catch { }
865
- if (Is.empty(existingKey)) {
866
- engineCore.logInfo(I18n.formatMessage("node.addingSynchronisedStorageBlobEncryptionKey", { keyName }));
867
- await vaultConnector.addKey(keyName, VaultKeyType.ChaCha20Poly1305, Converter.base64ToBytes(envVars.synchronisedStorageBlobStorageKey));
868
- }
869
- else {
870
- engineCore.logInfo(I18n.formatMessage("node.existingSynchronisedStorageBlobEncryptionKey", { keyName }));
871
- }
872
- }
873
- }
874
- }
875
- /**
876
- * Add a verification method if it doesn't exist.
877
- * @param engineCore The engine core for the node.
878
- * @param context The context for the node.
879
- * @param verificationMethodTitle The verification method title.
880
- * @param verificationMethodId The verification method ID.
881
- */
882
- async function addVerificationMethod(engineCore, context, verificationMethodTitle, verificationMethodId) {
883
- if (Is.stringValue(context.state.nodeIdentity) &&
884
- Is.arrayValue(context.config.types.identityConnector) &&
885
- Is.stringValue(verificationMethodId)) {
886
- const defaultIdentityConnectorType = engineCore.getRegisteredInstanceType("identityConnector");
887
- const identityConnector = IdentityConnectorFactory.get(defaultIdentityConnectorType);
888
- const defaultIdentityResolverConnectorType = engineCore.getRegisteredInstanceType("identityResolverConnector");
889
- const identityResolverConnector = IdentityResolverConnectorFactory.get(defaultIdentityResolverConnectorType);
890
- const identityDocument = await identityResolverConnector.resolveDocument(context.state.nodeIdentity);
891
- const fullMethodId = `${identityDocument.id}#${verificationMethodId}`;
892
- let exists = false;
893
- try {
894
- DocumentHelper.getVerificationMethod(identityDocument, fullMethodId, "assertionMethod");
895
- exists = true;
896
- }
897
- catch { }
898
- if (!exists) {
899
- engineCore.logInfo(I18n.formatMessage("node.addingVerificationMethod", {
900
- title: verificationMethodTitle,
901
- methodId: fullMethodId
902
- }));
903
- await identityConnector.addVerificationMethod(context.state.nodeIdentity, context.state.nodeIdentity, "assertionMethod", verificationMethodId);
904
- }
905
- else {
906
- engineCore.logInfo(I18n.formatMessage("node.existingVerificationMethod", {
907
- title: verificationMethodTitle,
908
- methodId: fullMethodId
909
- }));
910
- }
911
- }
912
- }
913
-
914
- // Copyright 2024 IOTA Stiftung.
915
- // SPDX-License-Identifier: Apache-2.0.
916
- /**
917
- * Build the engine core configuration from environment variables.
918
- * @param envVars The environment variables.
919
- * @returns The config for the core.
920
- */
921
- async function buildEngineConfiguration(envVars) {
922
- if (Is.stringValue(envVars.storageFileRoot)) {
923
- envVars.stateFilename ??= "engine-state.json";
924
- envVars.storageFileRoot = path.resolve(envVars.storageFileRoot);
925
- envVars.stateFilename = path.join(envVars.storageFileRoot, envVars.stateFilename);
926
- }
927
- const coreConfig = {
928
- debug: Coerce.boolean(envVars.debug) ?? false,
929
- types: {}
930
- };
931
- await configureEntityStorage(coreConfig, envVars);
932
- await configureBlobStorage(coreConfig, envVars);
933
- await configureVault(coreConfig, envVars);
934
- await configureDlt(coreConfig, envVars);
935
- await configureLogging(coreConfig, envVars);
936
- await configureBackgroundTask(coreConfig, envVars);
937
- await configureTaskScheduler(coreConfig, envVars);
938
- await configureEventBus(coreConfig, envVars);
939
- await configureTelemetry(coreConfig, envVars);
940
- await configureMessaging(coreConfig, envVars);
941
- await configureFaucet(coreConfig, envVars);
942
- await configureWallet(coreConfig, envVars);
943
- await configureNft(coreConfig, envVars);
944
- await configureVerifiableStorage(coreConfig, envVars);
945
- await configureIdentity(coreConfig, envVars);
946
- await configureIdentityResolver(coreConfig, envVars);
947
- await configureIdentityProfile(coreConfig, envVars);
948
- await configureAttestation(coreConfig, envVars);
949
- await configureDataProcessing(coreConfig, envVars);
950
- await configureAuditableItemGraph(coreConfig, envVars);
951
- await configureAuditableItemStream(coreConfig, envVars);
952
- await configureDocumentManagement(coreConfig, envVars);
953
- await configureVerifiableCredentialAuthentication(coreConfig, envVars);
954
- await configureRightsManagement(coreConfig, envVars);
955
- await configureSynchronisedStorage(coreConfig, envVars);
956
- await configureFederatedCatalogue(coreConfig, envVars);
957
- await configureDataSpaceConnector(coreConfig, envVars);
958
- return coreConfig;
959
- }
960
- /**
961
- * Configures the entity storage.
962
- * @param coreConfig The core config.
963
- * @param envVars The environment variables.
964
- */
965
- async function configureEntityStorage(coreConfig, envVars) {
966
- coreConfig.types ??= {};
967
- coreConfig.types.entityStorageConnector ??= [];
968
- const entityStorageConnectorTypes = envVars.entityStorageConnectorType?.split(",") ?? [];
969
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.Memory)) {
970
- coreConfig.types.entityStorageConnector.push({
971
- type: EntityStorageConnectorType.Memory
972
- });
973
- }
974
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.File)) {
975
- coreConfig.types.entityStorageConnector.push({
976
- type: EntityStorageConnectorType.File,
977
- options: {
978
- config: { directory: envVars.storageFileRoot ?? "" },
979
- folderPrefix: envVars.entityStorageTablePrefix
980
- }
981
- });
982
- }
983
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.AwsDynamoDb)) {
984
- coreConfig.types.entityStorageConnector.push({
985
- type: EntityStorageConnectorType.AwsDynamoDb,
986
- options: {
987
- config: {
988
- region: envVars.awsDynamodbRegion ?? "",
989
- authMode: envVars.awsDynamodbAuthMode,
990
- accessKeyId: envVars.awsDynamodbAccessKeyId,
991
- secretAccessKey: envVars.awsDynamodbSecretAccessKey,
992
- endpoint: envVars.awsDynamodbEndpoint
993
- },
994
- tablePrefix: envVars.entityStorageTablePrefix
995
- }
996
- });
997
- }
998
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.AzureCosmosDb)) {
999
- coreConfig.types.entityStorageConnector.push({
1000
- type: EntityStorageConnectorType.AzureCosmosDb,
1001
- options: {
1002
- config: {
1003
- endpoint: envVars.azureCosmosdbEndpoint ?? "",
1004
- key: envVars.azureCosmosdbKey ?? "",
1005
- databaseId: envVars.azureCosmosdbDatabaseId ?? "",
1006
- containerId: envVars.azureCosmosdbContainerId ?? ""
1007
- },
1008
- tablePrefix: envVars.entityStorageTablePrefix
1009
- }
1010
- });
1011
- }
1012
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.GcpFirestoreDb)) {
1013
- coreConfig.types.entityStorageConnector.push({
1014
- type: EntityStorageConnectorType.GcpFirestoreDb,
1015
- options: {
1016
- config: {
1017
- projectId: envVars.gcpFirestoreProjectId ?? "",
1018
- credentials: envVars.gcpFirestoreCredentials ?? "",
1019
- databaseId: envVars.gcpFirestoreDatabaseId ?? "",
1020
- collectionName: envVars.gcpFirestoreCollectionName ?? "",
1021
- endpoint: envVars.gcpFirestoreApiEndpoint ?? ""
1022
- },
1023
- tablePrefix: envVars.entityStorageTablePrefix
1024
- }
1025
- });
1026
- }
1027
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.ScyllaDb)) {
1028
- coreConfig.types.entityStorageConnector.push({
1029
- type: EntityStorageConnectorType.ScyllaDb,
1030
- options: {
1031
- config: {
1032
- hosts: envVars.scylladbHosts?.split(",") ?? [],
1033
- localDataCenter: envVars.scylladbLocalDataCenter ?? "",
1034
- keyspace: envVars.scylladbKeyspace ?? "",
1035
- port: Coerce.integer(envVars.scylladbPort)
1036
- },
1037
- tablePrefix: envVars.entityStorageTablePrefix
1038
- }
1039
- });
1040
- }
1041
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.MySqlDb)) {
1042
- coreConfig.types.entityStorageConnector.push({
1043
- type: EntityStorageConnectorType.MySqlDb,
1044
- options: {
1045
- config: {
1046
- host: envVars.mySqlHost ?? "",
1047
- port: Coerce.integer(envVars.mySqlPort),
1048
- user: envVars.mySqlUser ?? "",
1049
- password: envVars.mySqlPassword ?? "",
1050
- database: envVars.mySqlDatabase ?? ""
1051
- },
1052
- tablePrefix: envVars.entityStorageTablePrefix
1053
- }
1054
- });
1055
- }
1056
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.MongoDb)) {
1057
- coreConfig.types.entityStorageConnector.push({
1058
- type: EntityStorageConnectorType.MongoDb,
1059
- options: {
1060
- config: {
1061
- host: envVars.mongoDbHost ?? "",
1062
- port: Coerce.integer(envVars.mongoDbPort),
1063
- user: envVars.mongoDbUser ?? "",
1064
- password: envVars.mongoDbPassword ?? "",
1065
- database: envVars.mongoDbDatabase ?? ""
1066
- },
1067
- tablePrefix: envVars.entityStorageTablePrefix
1068
- }
1069
- });
1070
- }
1071
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.PostgreSql)) {
1072
- coreConfig.types.entityStorageConnector.push({
1073
- type: EntityStorageConnectorType.PostgreSql,
1074
- options: {
1075
- config: {
1076
- host: envVars.postgreSqlHost ?? "",
1077
- port: Coerce.integer(envVars.postgreSqlPort),
1078
- user: envVars.postgreSqlUser ?? "",
1079
- password: envVars.postgreSqlPassword ?? "",
1080
- database: envVars.postgreSqlDatabase ?? ""
1081
- },
1082
- tablePrefix: envVars.entityStorageTablePrefix
1083
- }
1084
- });
1085
- }
1086
- const defaultEntityStorageConnectorType = envVars.entityStorageConnectorDefault ?? entityStorageConnectorTypes[0];
1087
- if (entityStorageConnectorTypes.includes(EntityStorageConnectorType.Synchronised)) {
1088
- // For synchronised storage we use the default connector as the one we wrap for real DB operations
1089
- coreConfig.types.entityStorageConnector.push({
1090
- type: EntityStorageConnectorType.Synchronised,
1091
- options: {
1092
- entityStorageConnectorType: defaultEntityStorageConnectorType
1093
- }
1094
- });
1095
- }
1096
- if (Is.arrayValue(entityStorageConnectorTypes)) {
1097
- for (const config of coreConfig.types.entityStorageConnector) {
1098
- if (config.type === defaultEntityStorageConnectorType) {
1099
- config.isDefault = true;
1100
- break;
1101
- }
1102
- }
1103
- }
1104
- }
1105
- /**
1106
- * Configures the blob storage.
1107
- * @param coreConfig The core config.
1108
- * @param envVars The environment variables.
1109
- */
1110
- async function configureBlobStorage(coreConfig, envVars) {
1111
- coreConfig.types.blobStorageConnector ??= [];
1112
- const blobStorageConnectorTypes = envVars.blobStorageConnectorType?.split(",") ?? [];
1113
- if (blobStorageConnectorTypes.includes(BlobStorageConnectorType.Memory)) {
1114
- coreConfig.types.blobStorageConnector.push({
1115
- type: BlobStorageConnectorType.Memory
1116
- });
1117
- }
1118
- if (blobStorageConnectorTypes.includes(BlobStorageConnectorType.File)) {
1119
- coreConfig.types.blobStorageConnector.push({
1120
- type: BlobStorageConnectorType.File,
1121
- options: {
1122
- config: {
1123
- directory: Is.stringValue(envVars.storageFileRoot)
1124
- ? path.join(envVars.storageFileRoot, "blob-storage")
1125
- : ""
1126
- },
1127
- storagePrefix: envVars.blobStoragePrefix
1128
- }
1129
- });
1130
- }
1131
- if (blobStorageConnectorTypes.includes(BlobStorageConnectorType.Ipfs)) {
1132
- coreConfig.types.blobStorageConnector.push({
1133
- type: BlobStorageConnectorType.Ipfs,
1134
- options: {
1135
- config: {
1136
- apiUrl: envVars.ipfsApiUrl ?? "",
1137
- bearerToken: envVars.ipfsBearerToken
1138
- }
1139
- }
1140
- });
1141
- }
1142
- if (blobStorageConnectorTypes.includes(BlobStorageConnectorType.AwsS3)) {
1143
- coreConfig.types.blobStorageConnector.push({
1144
- type: BlobStorageConnectorType.AwsS3,
1145
- options: {
1146
- config: {
1147
- region: envVars.awsS3Region ?? "",
1148
- bucketName: envVars.awsS3BucketName ?? "",
1149
- authMode: envVars.awsS3AuthMode,
1150
- accessKeyId: envVars.awsS3AccessKeyId,
1151
- secretAccessKey: envVars.awsS3SecretAccessKey,
1152
- endpoint: envVars.awsS3Endpoint
1153
- },
1154
- storagePrefix: envVars.blobStoragePrefix
1155
- }
1156
- });
1157
- }
1158
- if (blobStorageConnectorTypes.includes(BlobStorageConnectorType.AzureStorage)) {
1159
- coreConfig.types.blobStorageConnector.push({
1160
- type: BlobStorageConnectorType.AzureStorage,
1161
- options: {
1162
- config: {
1163
- accountName: envVars.azureStorageAccountName ?? "",
1164
- accountKey: envVars.azureStorageAccountKey ?? "",
1165
- containerName: envVars.azureStorageContainerName ?? "",
1166
- endpoint: envVars.azureStorageEndpoint ?? ""
1167
- },
1168
- storagePrefix: envVars.blobStoragePrefix
1169
- }
1170
- });
1171
- }
1172
- if (blobStorageConnectorTypes.includes(BlobStorageConnectorType.GcpStorage)) {
1173
- coreConfig.types.blobStorageConnector.push({
1174
- type: BlobStorageConnectorType.GcpStorage,
1175
- options: {
1176
- config: {
1177
- projectId: envVars.gcpStorageProjectId ?? "",
1178
- credentials: envVars.gcpStorageCredentials ?? "",
1179
- bucketName: envVars.gcpStorageBucketName ?? "",
1180
- apiEndpoint: envVars.gcpFirestoreApiEndpoint
1181
- },
1182
- storagePrefix: envVars.blobStoragePrefix
1183
- }
1184
- });
1185
- }
1186
- if (Is.arrayValue(blobStorageConnectorTypes)) {
1187
- const defaultStorageConnectorType = envVars.blobStorageConnectorDefault ?? blobStorageConnectorTypes[0];
1188
- for (const config of coreConfig.types.blobStorageConnector) {
1189
- if (config.type === defaultStorageConnectorType) {
1190
- config.isDefault = true;
1191
- }
1192
- // If this blob storage connector is the one to use for public access
1193
- // then add it as a feature
1194
- if (Is.stringValue(envVars.blobStorageConnectorPublic) &&
1195
- config.type === envVars.blobStorageConnectorPublic) {
1196
- config.features ??= [];
1197
- config.features.push("public");
1198
- break;
1199
- }
1200
- }
1201
- }
1202
- if (coreConfig.types.blobStorageConnector.length > 0) {
1203
- coreConfig.types.blobStorageComponent ??= [];
1204
- coreConfig.types.blobStorageComponent.push({
1205
- type: BlobStorageComponentType.Service,
1206
- options: {
1207
- config: {
1208
- vaultKeyId: (envVars.blobStorageEnableEncryption ?? false)
1209
- ? (envVars.blobStorageEncryptionKeyId ?? BLOB_STORAGE_ENCRYPTION_KEY_ID)
1210
- : undefined
1211
- }
1212
- }
1213
- });
1214
- }
1215
- }
1216
- /**
1217
- * Configures the logging.
1218
- * @param coreConfig The core config.
1219
- * @param envVars The environment variables.
1220
- */
1221
- async function configureLogging(coreConfig, envVars) {
1222
- coreConfig.types.loggingConnector ??= [];
1223
- const loggingConnectorTypes = (envVars.loggingConnector ?? "").split(",");
1224
- let additionalConnectorCount = 0;
1225
- for (const loggingConnector of loggingConnectorTypes) {
1226
- if (loggingConnector === LoggingConnectorType.Console) {
1227
- coreConfig.types.loggingConnector.push({
1228
- type: LoggingConnectorType.Console,
1229
- options: {
1230
- config: {
1231
- translateMessages: true,
1232
- hideGroups: true
1233
- }
1234
- }
1235
- });
1236
- additionalConnectorCount++;
1237
- }
1238
- else if (loggingConnector === LoggingConnectorType.EntityStorage) {
1239
- coreConfig.types.loggingConnector.push({
1240
- type: LoggingConnectorType.EntityStorage
1241
- });
1242
- additionalConnectorCount++;
1243
- }
1244
- }
1245
- // If more than one logging connector, then we need to add a multi connector
1246
- // and set it as the default one
1247
- if (additionalConnectorCount > 1) {
1248
- coreConfig.types.loggingConnector.push({
1249
- type: LoggingConnectorType.Multi,
1250
- options: {
1251
- loggingConnectorTypes
1252
- },
1253
- isDefault: true
1254
- });
1255
- }
1256
- else if (additionalConnectorCount > 0) {
1257
- // If only one connector, then we set it as the default one
1258
- coreConfig.types.loggingConnector[coreConfig.types.loggingConnector.length - 1].isDefault =
1259
- true;
1260
- }
1261
- if (additionalConnectorCount > 0) {
1262
- coreConfig.types.loggingComponent ??= [];
1263
- // We set the isDefault flag so that other components will get this service by default
1264
- // and not the generic one from the engine core
1265
- coreConfig.types.loggingComponent.push({ type: LoggingComponentType.Service, isDefault: true });
1266
- }
1267
- }
1268
- /**
1269
- * Configures the vault.
1270
- * @param coreConfig The core config.
1271
- * @param envVars The environment variables.
1272
- */
1273
- async function configureVault(coreConfig, envVars) {
1274
- coreConfig.types.vaultConnector ??= [];
1275
- if (envVars.vaultConnector === VaultConnectorType.EntityStorage) {
1276
- coreConfig.types.vaultConnector.push({
1277
- type: VaultConnectorType.EntityStorage
1278
- });
1279
- }
1280
- else if (envVars.vaultConnector === VaultConnectorType.Hashicorp) {
1281
- coreConfig.types.vaultConnector.push({
1282
- type: VaultConnectorType.Hashicorp,
1283
- options: {
1284
- config: {
1285
- endpoint: envVars.hashicorpVaultEndpoint ?? "",
1286
- token: envVars.hashicorpVaultToken ?? ""
1287
- }
1288
- }
1289
- });
1290
- }
1291
- }
1292
- /**
1293
- * Configures the background task.
1294
- * @param coreConfig The core config.
1295
- * @param envVars The environment variables.
1296
- */
1297
- async function configureBackgroundTask(coreConfig, envVars) {
1298
- coreConfig.types.backgroundTaskConnector ??= [];
1299
- if (envVars.backgroundTaskConnector === BackgroundTaskConnectorType.EntityStorage) {
1300
- coreConfig.types.backgroundTaskConnector.push({
1301
- type: BackgroundTaskConnectorType.EntityStorage
1302
- });
1303
- }
1304
- }
1305
- /**
1306
- * Configures the event bud.
1307
- * @param coreConfig The core config.
1308
- * @param envVars The environment variables.
1309
- */
1310
- async function configureEventBus(coreConfig, envVars) {
1311
- coreConfig.types.eventBusConnector ??= [];
1312
- if (envVars.eventBusConnector === EventBusConnectorType.Local) {
1313
- coreConfig.types.eventBusConnector.push({
1314
- type: EventBusConnectorType.Local
1315
- });
1316
- }
1317
- if (coreConfig.types.eventBusConnector.length > 0) {
1318
- coreConfig.types.eventBusComponent ??= [];
1319
- coreConfig.types.eventBusComponent.push({ type: EventBusComponentType.Service });
1320
- }
1321
- }
1322
- /**
1323
- * Configures the telemetry.
1324
- * @param coreConfig The core config.
1325
- * @param envVars The environment variables.
1326
- */
1327
- async function configureTelemetry(coreConfig, envVars) {
1328
- coreConfig.types.telemetryConnector ??= [];
1329
- if (envVars.telemetryConnector === TelemetryConnectorType.EntityStorage) {
1330
- coreConfig.types.telemetryConnector.push({
1331
- type: TelemetryConnectorType.EntityStorage
1332
- });
1333
- }
1334
- if (coreConfig.types.telemetryConnector.length > 0) {
1335
- coreConfig.types.telemetryComponent ??= [];
1336
- coreConfig.types.telemetryComponent.push({ type: TelemetryComponentType.Service });
1337
- }
1338
- }
1339
- /**
1340
- * Configures the messaging.
1341
- * @param coreConfig The core config.
1342
- * @param envVars The environment variables.
1343
- */
1344
- async function configureMessaging(coreConfig, envVars) {
1345
- if (Coerce.boolean(envVars.messagingEnabled) ?? false) {
1346
- coreConfig.types.messagingEmailConnector ??= [];
1347
- coreConfig.types.messagingSmsConnector ??= [];
1348
- coreConfig.types.messagingPushNotificationConnector ??= [];
1349
- if (envVars.messagingEmailConnector === MessagingEmailConnectorType.EntityStorage) {
1350
- coreConfig.types.messagingEmailConnector.push({
1351
- type: MessagingEmailConnectorType.EntityStorage
1352
- });
1353
- }
1354
- else if (envVars.messagingEmailConnector === MessagingEmailConnectorType.Aws) {
1355
- coreConfig.types.messagingEmailConnector.push({
1356
- type: MessagingEmailConnectorType.Aws,
1357
- options: {
1358
- config: {
1359
- region: envVars.awsSesRegion ?? "",
1360
- authMode: envVars.awsSesAuthMode,
1361
- accessKeyId: envVars.awsSesAccessKeyId,
1362
- secretAccessKey: envVars.awsSesSecretAccessKey,
1363
- endpoint: envVars.awsSesEndpoint
1364
- }
1365
- }
1366
- });
1367
- }
1368
- if (envVars.messagingSmsConnector === MessagingSmsConnectorType.EntityStorage) {
1369
- coreConfig.types.messagingSmsConnector.push({
1370
- type: MessagingSmsConnectorType.EntityStorage
1371
- });
1372
- }
1373
- else if (envVars.messagingSmsConnector === MessagingSmsConnectorType.Aws) {
1374
- coreConfig.types.messagingSmsConnector.push({
1375
- type: MessagingSmsConnectorType.Aws,
1376
- options: {
1377
- config: {
1378
- region: envVars.awsSesRegion ?? "",
1379
- authMode: envVars.awsSesAuthMode,
1380
- accessKeyId: envVars.awsSesAccessKeyId,
1381
- secretAccessKey: envVars.awsSesSecretAccessKey,
1382
- endpoint: envVars.awsSesEndpoint
1383
- }
1384
- }
1385
- });
1386
- }
1387
- if (envVars.messagingPushNotificationConnector ===
1388
- MessagingPushNotificationConnectorType.EntityStorage) {
1389
- coreConfig.types.messagingPushNotificationConnector.push({
1390
- type: MessagingPushNotificationConnectorType.EntityStorage
1391
- });
1392
- }
1393
- else if (envVars.messagingPushNotificationConnector === MessagingPushNotificationConnectorType.Aws) {
1394
- coreConfig.types.messagingPushNotificationConnector.push({
1395
- type: MessagingPushNotificationConnectorType.Aws,
1396
- options: {
1397
- config: {
1398
- region: envVars.awsSesRegion ?? "",
1399
- authMode: envVars.awsSesAuthMode,
1400
- accessKeyId: envVars.awsSesAccessKeyId,
1401
- secretAccessKey: envVars.awsSesSecretAccessKey,
1402
- endpoint: envVars.awsSesEndpoint,
1403
- applicationsSettings: Is.json(envVars.awsMessagingPushNotificationApplications)
1404
- ? JSON.parse(envVars.awsMessagingPushNotificationApplications)
1405
- : []
1406
- }
1407
- }
1408
- });
1409
- }
1410
- const templates = Is.arrayValue(envVars.messagingTemplates)
1411
- ? envVars.messagingTemplates
1412
- : undefined;
1413
- coreConfig.types.messagingAdminComponent ??= [];
1414
- coreConfig.types.messagingAdminComponent.push({
1415
- type: MessagingAdminComponentType.Service,
1416
- options: {
1417
- config: {
1418
- templates
1419
- }
1420
- }
1421
- });
1422
- coreConfig.types.messagingComponent ??= [];
1423
- coreConfig.types.messagingComponent.push({ type: MessagingComponentType.Service });
1424
- }
1425
- }
1426
- /**
1427
- * Configures the faucet.
1428
- * @param coreConfig The core config.
1429
- * @param envVars The environment variables.
1430
- */
1431
- async function configureFaucet(coreConfig, envVars) {
1432
- coreConfig.types.faucetConnector ??= [];
1433
- if (envVars.faucetConnector === FaucetConnectorType.EntityStorage) {
1434
- coreConfig.types.faucetConnector.push({
1435
- type: FaucetConnectorType.EntityStorage
1436
- });
1437
- }
1438
- else if (envVars.faucetConnector === FaucetConnectorType.Iota) {
1439
- const dltConfig = EngineTypeHelper.getConfigOfType(coreConfig, "dltConfig", DltConfigType.Iota);
1440
- coreConfig.types.faucetConnector.push({
1441
- type: FaucetConnectorType.Iota,
1442
- options: {
1443
- config: {
1444
- endpoint: envVars.iotaFaucetEndpoint ?? "",
1445
- clientOptions: dltConfig?.options?.config?.clientOptions ?? { url: "" },
1446
- network: dltConfig?.options?.config?.network ?? ""
1447
- }
1448
- }
1449
- });
1450
- }
1451
- }
1452
- /**
1453
- * Configures the wallet.
1454
- * @param coreConfig The core config.
1455
- * @param envVars The environment variables.
1456
- */
1457
- async function configureWallet(coreConfig, envVars) {
1458
- coreConfig.types.walletConnector ??= [];
1459
- if (envVars.walletConnector === WalletConnectorType.EntityStorage) {
1460
- coreConfig.types.walletConnector.push({
1461
- type: WalletConnectorType.EntityStorage
1462
- });
1463
- }
1464
- else if (envVars.walletConnector === WalletConnectorType.Iota) {
1465
- const dltConfig = EngineTypeHelper.getConfigOfType(coreConfig, "dltConfig", DltConfigType.Iota);
1466
- coreConfig.types.walletConnector.push({
1467
- type: WalletConnectorType.Iota,
1468
- options: {
1469
- config: dltConfig?.options?.config ?? {}
1470
- }
1471
- });
1472
- }
1473
- }
1474
- /**
1475
- * Configures the NFT.
1476
- * @param coreConfig The core config.
1477
- * @param envVars The environment variables.
1478
- */
1479
- async function configureNft(coreConfig, envVars) {
1480
- coreConfig.types.nftConnector ??= [];
1481
- if (envVars.nftConnector === NftConnectorType.EntityStorage) {
1482
- coreConfig.types.nftConnector.push({
1483
- type: NftConnectorType.EntityStorage
1484
- });
1485
- }
1486
- else if (envVars.nftConnector === NftConnectorType.Iota) {
1487
- const dltConfig = EngineTypeHelper.getConfigOfType(coreConfig, "dltConfig", DltConfigType.Iota);
1488
- coreConfig.types.nftConnector.push({
1489
- type: NftConnectorType.Iota,
1490
- options: {
1491
- config: dltConfig?.options?.config ?? {}
1492
- }
1493
- });
1494
- }
1495
- if (coreConfig.types.nftConnector.length > 0) {
1496
- coreConfig.types.nftComponent ??= [];
1497
- coreConfig.types.nftComponent.push({ type: NftComponentType.Service });
1498
- }
1499
- }
1500
- /**
1501
- * Configures the verifiable storage.
1502
- * @param coreConfig The core config.
1503
- * @param envVars The environment variables.
1504
- */
1505
- async function configureVerifiableStorage(coreConfig, envVars) {
1506
- coreConfig.types.verifiableStorageConnector ??= [];
1507
- if (envVars.verifiableStorageConnector === VerifiableStorageConnectorType.EntityStorage) {
1508
- coreConfig.types.verifiableStorageConnector.push({
1509
- type: VerifiableStorageConnectorType.EntityStorage
1510
- });
1511
- }
1512
- else if (envVars.verifiableStorageConnector === VerifiableStorageConnectorType.Iota) {
1513
- const dltConfig = EngineTypeHelper.getConfigOfType(coreConfig, "dltConfig", DltConfigType.Iota);
1514
- coreConfig.types.verifiableStorageConnector.push({
1515
- type: VerifiableStorageConnectorType.Iota,
1516
- options: {
1517
- config: dltConfig?.options?.config ?? {}
1518
- }
1519
- });
1520
- }
1521
- if (coreConfig.types.verifiableStorageConnector.length > 0) {
1522
- coreConfig.types.verifiableStorageComponent ??= [];
1523
- coreConfig.types.verifiableStorageComponent.push({
1524
- type: VerifiableStorageComponentType.Service
1525
- });
1526
- coreConfig.types.immutableProofComponent ??= [];
1527
- coreConfig.types.immutableProofComponent.push({
1528
- type: ImmutableProofComponentType.Service,
1529
- options: {
1530
- config: {
1531
- verificationMethodId: envVars.immutableProofVerificationMethodId ?? IMMUTABLE_PROOF_VERIFICATION_METHOD_ID
1532
- }
1533
- }
1534
- });
1535
- }
1536
- }
1537
- /**
1538
- * Configures the identity.
1539
- * @param coreConfig The core config.
1540
- * @param envVars The environment variables.
1541
- */
1542
- async function configureIdentity(coreConfig, envVars) {
1543
- coreConfig.types.identityConnector ??= [];
1544
- if (envVars.identityConnector === IdentityConnectorType.EntityStorage) {
1545
- coreConfig.types.identityConnector.push({
1546
- type: IdentityConnectorType.EntityStorage
1547
- });
1548
- }
1549
- else if (envVars.identityConnector === IdentityConnectorType.Iota) {
1550
- const dltConfig = EngineTypeHelper.getConfigOfType(coreConfig, "dltConfig", DltConfigType.Iota);
1551
- coreConfig.types.identityConnector.push({
1552
- type: IdentityConnectorType.Iota,
1553
- options: {
1554
- config: dltConfig?.options?.config ?? {}
1555
- }
1556
- });
1557
- }
1558
- if (coreConfig.types.identityConnector.length > 0) {
1559
- coreConfig.types.identityComponent ??= [];
1560
- coreConfig.types.identityComponent.push({ type: IdentityComponentType.Service });
1561
- }
1562
- }
1563
- /**
1564
- * Configures the identity resolver.
1565
- * @param coreConfig The core config.
1566
- * @param envVars The environment variables.
1567
- */
1568
- async function configureIdentityResolver(coreConfig, envVars) {
1569
- coreConfig.types.identityResolverConnector ??= [];
1570
- if (envVars.identityResolverConnector === IdentityResolverConnectorType.EntityStorage) {
1571
- coreConfig.types.identityResolverConnector.push({
1572
- type: IdentityResolverConnectorType.EntityStorage
1573
- });
1574
- }
1575
- else if (envVars.identityResolverConnector === IdentityResolverConnectorType.Iota) {
1576
- const dltConfig = EngineTypeHelper.getConfigOfType(coreConfig, "dltConfig", DltConfigType.Iota);
1577
- coreConfig.types.identityResolverConnector.push({
1578
- type: IdentityResolverConnectorType.Iota,
1579
- options: {
1580
- config: dltConfig?.options?.config ?? {}
1581
- }
1582
- });
1583
- }
1584
- else if (envVars.identityResolverConnector === IdentityResolverConnectorType.Universal) {
1585
- coreConfig.types.identityResolverConnector.push({
1586
- type: IdentityResolverConnectorType.Universal,
1587
- options: {
1588
- config: {
1589
- endpoint: envVars.universalResolverEndpoint ?? ""
1590
- }
1591
- }
1592
- });
1593
- }
1594
- if (coreConfig.types.identityResolverConnector.length > 0) {
1595
- coreConfig.types.identityResolverComponent ??= [];
1596
- coreConfig.types.identityResolverComponent.push({
1597
- type: IdentityResolverComponentType.Service
1598
- });
1599
- }
1600
- }
1601
- /**
1602
- * Configures the identity profile.
1603
- * @param coreConfig The core config.
1604
- * @param envVars The environment variables.
1605
- */
1606
- async function configureIdentityProfile(coreConfig, envVars) {
1607
- coreConfig.types.identityProfileConnector ??= [];
1608
- if (envVars.identityProfileConnector === IdentityConnectorType.EntityStorage) {
1609
- coreConfig.types.identityProfileConnector.push({
1610
- type: IdentityProfileConnectorType.EntityStorage
1611
- });
1612
- }
1613
- if (coreConfig.types.identityProfileConnector.length > 0) {
1614
- coreConfig.types.identityProfileComponent ??= [];
1615
- coreConfig.types.identityProfileComponent.push({ type: IdentityProfileComponentType.Service });
1616
- }
1617
- }
1618
- /**
1619
- * Configures the attestation.
1620
- * @param coreConfig The core config.
1621
- * @param envVars The environment variables.
1622
- */
1623
- async function configureAttestation(coreConfig, envVars) {
1624
- coreConfig.types.attestationConnector ??= [];
1625
- if (envVars.attestationConnector === AttestationConnectorType.Nft) {
1626
- coreConfig.types.attestationConnector.push({
1627
- type: AttestationConnectorType.Nft
1628
- });
1629
- }
1630
- if (coreConfig.types.attestationConnector.length > 0) {
1631
- coreConfig.types.attestationComponent ??= [];
1632
- coreConfig.types.attestationComponent.push({
1633
- type: AttestationComponentType.Service,
1634
- options: {
1635
- config: {
1636
- verificationMethodId: envVars.attestationVerificationMethodId ?? ATTESTATION_VERIFICATION_METHOD_ID
1637
- }
1638
- }
1639
- });
1640
- }
1641
- }
1642
- /**
1643
- * Configures the auditable item graph.
1644
- * @param coreConfig The core config.
1645
- * @param envVars The environment variables.
1646
- */
1647
- async function configureAuditableItemGraph(coreConfig, envVars) {
1648
- if (Coerce.boolean(envVars.auditableItemGraphEnabled) ?? false) {
1649
- coreConfig.types.auditableItemGraphComponent ??= [];
1650
- coreConfig.types.auditableItemGraphComponent.push({
1651
- type: AuditableItemGraphComponentType.Service
1652
- });
1653
- }
1654
- }
1655
- /**
1656
- * Configures the auditable item stream.
1657
- * @param coreConfig The core config.
1658
- * @param envVars The environment variables.
1659
- */
1660
- async function configureAuditableItemStream(coreConfig, envVars) {
1661
- if (Coerce.boolean(envVars.auditableItemStreamEnabled) ?? false) {
1662
- coreConfig.types.auditableItemStreamComponent ??= [];
1663
- coreConfig.types.auditableItemStreamComponent.push({
1664
- type: AuditableItemStreamComponentType.Service
1665
- });
1666
- }
1667
- }
1668
- /**
1669
- * Configures the data processing.
1670
- * @param coreConfig The core config.
1671
- * @param envVars The environment variables.
1672
- */
1673
- async function configureDataProcessing(coreConfig, envVars) {
1674
- if (Coerce.boolean(envVars.dataProcessingEnabled) ?? false) {
1675
- coreConfig.types.dataProcessingComponent ??= [];
1676
- coreConfig.types.dataProcessingComponent.push({ type: DataProcessingComponentType.Service });
1677
- coreConfig.types.dataConverterConnector ??= [];
1678
- const converterConnectors = envVars.dataConverterConnectors?.split(",") ?? [];
1679
- for (const converterConnector of converterConnectors) {
1680
- if (converterConnector === DataConverterConnectorType.Json) {
1681
- coreConfig.types.dataConverterConnector.push({
1682
- type: DataConverterConnectorType.Json
1683
- });
1684
- }
1685
- else if (converterConnector === DataConverterConnectorType.Xml) {
1686
- coreConfig.types.dataConverterConnector.push({
1687
- type: DataConverterConnectorType.Xml
1688
- });
1689
- }
1690
- }
1691
- coreConfig.types.dataExtractorConnector ??= [];
1692
- const extractorConnectors = envVars.dataExtractorConnectors?.split(",") ?? [];
1693
- for (const extractorConnector of extractorConnectors) {
1694
- if (extractorConnector === DataExtractorConnectorType.JsonPath) {
1695
- coreConfig.types.dataExtractorConnector.push({
1696
- type: DataExtractorConnectorType.JsonPath
1697
- });
1698
- }
1699
- }
1700
- }
1701
- }
1702
- /**
1703
- * Configures the document management.
1704
- * @param coreConfig The core config.
1705
- * @param envVars The environment variables.
1706
- */
1707
- async function configureDocumentManagement(coreConfig, envVars) {
1708
- if (Coerce.boolean(envVars.documentManagementEnabled) ?? false) {
1709
- coreConfig.types.documentManagementComponent ??= [];
1710
- coreConfig.types.documentManagementComponent.push({
1711
- type: DocumentManagementComponentType.Service
1712
- });
1713
- }
1714
- }
1715
- /**
1716
- * Configures the verifiable credential authentication.
1717
- * @param coreConfig The core config.
1718
- * @param envVars The environment variables.
1719
- */
1720
- async function configureVerifiableCredentialAuthentication(coreConfig, envVars) {
1721
- if (Coerce.boolean(envVars.vcAuthenticationEnabled) ?? false) {
1722
- // Can only perform VC authentication if identity component is available
1723
- coreConfig.types.authenticationGeneratorComponent ??= [];
1724
- coreConfig.types.authenticationGeneratorComponent.push({
1725
- type: AuthenticationGeneratorComponentType.VerifiableCredential,
1726
- options: {
1727
- config: {
1728
- verificationMethodId: envVars.vcAuthenticationVerificationMethodId ?? VC_AUTHENTICATION_VERIFICATION_METHOD_ID
1729
- }
1730
- },
1731
- features: ["verifiable-credential"]
1732
- });
1733
- }
1734
- }
1735
- /**
1736
- * Configures the rights management.
1737
- * @param coreConfig The core config.
1738
- * @param envVars The environment variables.
1739
- */
1740
- async function configureRightsManagement(coreConfig, envVars) {
1741
- if (Coerce.boolean(envVars.rightsManagementEnabled) ?? false) {
1742
- coreConfig.types.rightsManagementPapComponent ??= [];
1743
- coreConfig.types.rightsManagementPapComponent.push({
1744
- type: RightsManagementPapComponentType.Service
1745
- });
1746
- coreConfig.types.rightsManagementPmpComponent ??= [];
1747
- coreConfig.types.rightsManagementPmpComponent.push({
1748
- type: RightsManagementPmpComponentType.Service
1749
- });
1750
- coreConfig.types.rightsManagementPipComponent ??= [];
1751
- coreConfig.types.rightsManagementPipComponent.push({
1752
- type: RightsManagementPipComponentType.Service,
1753
- options: {
1754
- informationModulesConfig: Is.arrayValue(envVars.rightsManagementInformationSources)
1755
- ? envVars.rightsManagementInformationSources
1756
- : undefined
1757
- }
1758
- });
1759
- coreConfig.types.rightsManagementPxpComponent ??= [];
1760
- coreConfig.types.rightsManagementPxpComponent.push({
1761
- type: RightsManagementPxpComponentType.Service,
1762
- options: {
1763
- actionModulesConfig: Is.arrayValue(envVars.rightsManagementExecutionActions)
1764
- ? envVars.rightsManagementExecutionActions
1765
- : undefined
1766
- }
1767
- });
1768
- coreConfig.types.rightsManagementPdpComponent ??= [];
1769
- coreConfig.types.rightsManagementPdpComponent.push({
1770
- type: RightsManagementPdpComponentType.Service,
1771
- options: {
1772
- arbiterModulesConfig: Is.arrayValue(envVars.rightsManagementArbiters)
1773
- ? envVars.rightsManagementArbiters
1774
- : undefined
1775
- }
1776
- });
1777
- coreConfig.types.rightsManagementPepComponent ??= [];
1778
- coreConfig.types.rightsManagementPepComponent.push({
1779
- type: RightsManagementPepComponentType.Service,
1780
- options: {
1781
- processorModulesConfig: Is.arrayValue(envVars.rightsManagementEnforcementProcessors)
1782
- ? envVars.rightsManagementEnforcementProcessors
1783
- : undefined
1784
- }
1785
- });
1786
- coreConfig.types.rightsManagementPnpComponent ??= [];
1787
- coreConfig.types.rightsManagementPnpComponent.push({
1788
- type: RightsManagementPnpComponentType.Service,
1789
- options: {
1790
- negotiatorModulesConfig: Is.arrayValue(envVars.rightsManagementNegotiators)
1791
- ? envVars.rightsManagementNegotiators
1792
- : undefined,
1793
- requesterModulesConfig: Is.arrayValue(envVars.rightsManagementRequesters)
1794
- ? envVars.rightsManagementRequesters
1795
- : undefined,
1796
- config: {
1797
- baseCallbackUrl: envVars.rightsManagementBaseCallbackUrl ?? "",
1798
- offers: Is.arrayValue(envVars.rightsManagementOffers)
1799
- ? envVars.rightsManagementOffers
1800
- : [],
1801
- negotiationComponentCreator: async (url) => new PolicyNegotiationPointRestClient({ endpoint: url })
1802
- }
1803
- }
1804
- });
1805
- coreConfig.types.rightsManagementPnapComponent ??= [];
1806
- coreConfig.types.rightsManagementPnapComponent.push({
1807
- type: RightsManagementPnapComponentType.Service
1808
- });
1809
- coreConfig.types.rightsManagementDapComponent ??= [];
1810
- coreConfig.types.rightsManagementDapComponent.push({
1811
- type: RightsManagementDapComponentType.Service
1812
- });
1813
- coreConfig.types.rightsManagementDarpComponent ??= [];
1814
- coreConfig.types.rightsManagementDarpComponent.push({
1815
- type: RightsManagementDarpComponentType.Service,
1816
- options: {
1817
- config: {
1818
- dataAccessComponentCreator: async (url) => new DataAccessPointRestClient({ endpoint: url })
1819
- }
1820
- }
1821
- });
1822
- }
1823
- }
1824
- /**
1825
- * Configures the task scheduler.
1826
- * @param coreConfig The core config.
1827
- * @param envVars The environment variables.
1828
- */
1829
- async function configureTaskScheduler(coreConfig, envVars) {
1830
- if (Coerce.boolean(envVars.taskSchedulerEnabled) ?? false) {
1831
- coreConfig.types.taskSchedulerComponent ??= [];
1832
- coreConfig.types.taskSchedulerComponent.push({
1833
- type: TaskSchedulerComponentType.Service
1834
- });
1835
- }
1836
- }
1837
- /**
1838
- * Configures the synchronised storage.
1839
- * @param coreConfig The core config.
1840
- * @param envVars The environment variables.
1841
- */
1842
- async function configureSynchronisedStorage(coreConfig, envVars) {
1843
- if (Is.arrayValue(coreConfig.types.identityResolverComponent) &&
1844
- (Coerce.boolean(envVars.synchronisedStorageEnabled) ?? false)) {
1845
- // Check if the config provides a custom verifiable storage key id
1846
- let verifiableStorageKeyId = Coerce.string(envVars.synchronisedStorageVerifiableStorageKeyId);
1847
- if (!Is.stringValue(verifiableStorageKeyId)) {
1848
- // No custom key so default to the network setting
1849
- verifiableStorageKeyId = envVars.iotaNetwork;
1850
- }
1851
- coreConfig.types.synchronisedStorageComponent ??= [];
1852
- coreConfig.types.synchronisedStorageComponent.push({
1853
- type: SynchronisedStorageComponentType.Service,
1854
- options: {
1855
- config: {
1856
- verifiableStorageKeyId: verifiableStorageKeyId ?? "",
1857
- blobStorageEncryptionKeyId: envVars.synchronisedStorageBlobStorageEncryptionKeyId ??
1858
- SYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID,
1859
- entityUpdateIntervalMinutes: Coerce.number(envVars.synchronisedStorageEntityUpdateIntervalMinutes),
1860
- consolidationIntervalMinutes: Coerce.number(envVars.synchronisedStorageConsolidationIntervalMinutes),
1861
- consolidationBatchSize: Coerce.number(envVars.synchronisedStorageConsolidationBatchSize),
1862
- maxConsolidations: Coerce.number(envVars.synchronisedStorageMaxConsolidations)
1863
- }
1864
- }
1865
- });
1866
- // If there is a trusted url set, we need to add a client
1867
- // and give it a feature of trusted so that when the synchronised
1868
- // storage is created it can pickup the correct component
1869
- if (Is.stringValue(envVars.synchronisedStorageTrustedUrl)) {
1870
- coreConfig.types.synchronisedStorageComponent.push({
1871
- type: SynchronisedStorageComponentType.RestClient,
1872
- options: {
1873
- endpoint: envVars.synchronisedStorageTrustedUrl
1874
- },
1875
- features: ["trusted"]
1876
- });
1877
- }
1878
- }
1879
- }
1880
- /**
1881
- * Configures the federated catalogue.
1882
- * @param coreConfig The core config.
1883
- * @param envVars The environment variables.
1884
- */
1885
- async function configureFederatedCatalogue(coreConfig, envVars) {
1886
- if (Coerce.boolean(envVars.federatedCatalogueEnabled) ?? false) {
1887
- coreConfig.types.federatedCatalogueComponent ??= [];
1888
- coreConfig.types.federatedCatalogueComponent.push({
1889
- type: FederatedCatalogueComponentType.Service,
1890
- options: {
1891
- config: {
1892
- subResourceCacheTtlMs: Coerce.number(envVars.federatedCatalogueCacheTtlMs),
1893
- clearingHouseApproverList: Coerce.object(envVars.federatedCatalogueClearingHouseApproverList) ?? []
1894
- }
1895
- }
1896
- });
1897
- }
1898
- }
1899
- /**
1900
- * Configures the data space connector.
1901
- * @param coreConfig The core config.
1902
- * @param envVars The environment variables.
1903
- */
1904
- async function configureDataSpaceConnector(coreConfig, envVars) {
1905
- if (Coerce.boolean(envVars.dataSpaceConnectorEnabled) ?? false) {
1906
- coreConfig.types.dataSpaceConnectorComponent ??= [];
1907
- coreConfig.types.dataSpaceConnectorComponent.push({
1908
- type: DataSpaceConnectorComponentType.Service,
1909
- options: {
1910
- config: {
1911
- retainActivityLogsFor: Coerce.number(envVars.dataSpaceConnectorRetainActivityLogsFor),
1912
- activityLogsCleanUpInterval: Coerce.number(envVars.dataSpaceConnectorActivityLogsCleanUpInterval)
1913
- }
1914
- }
1915
- });
1916
- }
1917
- }
1918
- /**
1919
- * Configures the DLT.
1920
- * @param coreConfig The core config.
1921
- * @param envVars The environment variables.
1922
- */
1923
- async function configureDlt(coreConfig, envVars) {
1924
- // Create centralized DLT configuration for IOTA if essential IOTA variables are set
1925
- if (Is.stringValue(envVars.iotaNodeEndpoint) && Is.stringValue(envVars.iotaNetwork)) {
1926
- coreConfig.types.dltConfig ??= [];
1927
- const gasStationConfig = Is.stringValue(envVars.iotaGasStationEndpoint) &&
1928
- Is.stringValue(envVars.iotaGasStationAuthToken)
1929
- ? {
1930
- gasStationUrl: envVars.iotaGasStationEndpoint,
1931
- gasStationAuthToken: envVars.iotaGasStationAuthToken
1932
- }
1933
- : undefined;
1934
- coreConfig.types.dltConfig.push({
1935
- type: DltConfigType.Iota,
1936
- isDefault: true,
1937
- options: {
1938
- config: {
1939
- clientOptions: {
1940
- url: envVars.iotaNodeEndpoint ?? ""
1941
- },
1942
- network: envVars.iotaNetwork ?? "",
1943
- coinType: Coerce.number(envVars.iotaCoinType),
1944
- gasStation: gasStationConfig
1945
- }
1946
- }
1947
- });
1948
- }
1949
- }
1950
-
1951
- /**
1952
- * Handles the configuration of the server.
1953
- * @param envVars The environment variables for the engine server.
1954
- * @param coreEngineConfig The core engine config.
1955
- * @param serverInfo The server information.
1956
- * @param openApiSpecPath The path to the open api spec.
1957
- * @param favIconPath The path to the favicon.
1958
- * @returns The config for the core and the server.
1959
- */
1960
- async function buildEngineServerConfiguration(envVars, coreEngineConfig, serverInfo, openApiSpecPath, favIconPath) {
1961
- const webServerOptions = {
1962
- port: Coerce.number(envVars.port),
1963
- host: Coerce.string(envVars.host),
1964
- methods: Is.stringValue(envVars.httpMethods)
1965
- ? envVars.httpMethods.split(",")
1966
- : undefined,
1967
- allowedHeaders: Is.stringValue(envVars.httpAllowedHeaders)
1968
- ? envVars.httpAllowedHeaders.split(",")
1969
- : undefined,
1970
- exposedHeaders: Is.stringValue(envVars.httpExposedHeaders)
1971
- ? envVars.httpExposedHeaders.split(",")
1972
- : undefined,
1973
- corsOrigins: Is.stringValue(envVars.corsOrigins) ? envVars.corsOrigins.split(",") : undefined
1974
- };
1975
- const serverConfig = {
1976
- ...coreEngineConfig,
1977
- web: webServerOptions,
1978
- types: {
1979
- ...coreEngineConfig.types,
1980
- informationComponent: [
1981
- {
1982
- type: InformationComponentType.Service,
1983
- options: {
1984
- config: {
1985
- serverInfo,
1986
- openApiSpecPath,
1987
- favIconPath
1988
- }
1989
- }
1990
- }
1991
- ]
1992
- }
1993
- };
1994
- if (Is.stringValue(envVars.mimeTypeProcessors)) {
1995
- const mimeTypeProcessors = envVars.mimeTypeProcessors.split(",");
1996
- if (Is.arrayValue(mimeTypeProcessors)) {
1997
- serverConfig.types.mimeTypeProcessor ??= [];
1998
- for (const mimeTypeProcessor of mimeTypeProcessors) {
1999
- serverConfig.types.mimeTypeProcessor.push({
2000
- type: mimeTypeProcessor
2001
- });
2002
- }
2003
- }
2004
- }
2005
- serverConfig.types.restRouteProcessor ??= [];
2006
- serverConfig.types.socketRouteProcessor ??= [];
2007
- const features = getFeatures(envVars);
2008
- const hasNodeIdentity = features.includes(NodeFeatures.NodeIdentity);
2009
- if (hasNodeIdentity) {
2010
- serverConfig.types.restRouteProcessor.push({
2011
- type: RestRouteProcessorType.NodeIdentity
2012
- });
2013
- serverConfig.types.socketRouteProcessor.push({
2014
- type: SocketRouteProcessorType.NodeIdentity
2015
- });
2016
- }
2017
- if (!coreEngineConfig.silent) {
2018
- const includeBody = Coerce.boolean(envVars.routeLoggingIncludeBody) ?? coreEngineConfig.debug;
2019
- const fullBase64 = Coerce.boolean(envVars.routeLoggingFullBase64) ?? false;
2020
- const obfuscateProperties = Is.stringValue(envVars.routeLoggingObfuscateProperties)
2021
- ? envVars.routeLoggingObfuscateProperties.split(",")
2022
- : undefined;
2023
- serverConfig.types.restRouteProcessor.push({
2024
- type: RestRouteProcessorType.Logging,
2025
- options: {
2026
- config: {
2027
- includeBody,
2028
- fullBase64,
2029
- obfuscateProperties
2030
- }
2031
- }
2032
- });
2033
- serverConfig.types.socketRouteProcessor.push({
2034
- type: SocketRouteProcessorType.Logging,
2035
- options: {
2036
- config: {
2037
- includeBody,
2038
- fullBase64,
2039
- obfuscateProperties
2040
- }
2041
- }
2042
- });
2043
- }
2044
- serverConfig.types.restRouteProcessor.push({
2045
- type: RestRouteProcessorType.RestRoute,
2046
- options: {
2047
- config: {
2048
- includeErrorStack: coreEngineConfig.debug
2049
- }
2050
- }
2051
- });
2052
- serverConfig.types.socketRouteProcessor.push({
2053
- type: SocketRouteProcessorType.SocketRoute,
2054
- options: {
2055
- config: {
2056
- includeErrorStack: coreEngineConfig.debug
2057
- }
2058
- }
2059
- });
2060
- const authAdminProcessorType = envVars.authAdminProcessorType;
2061
- if (authAdminProcessorType === AuthenticationAdminComponentType.EntityStorage) {
2062
- serverConfig.types.authenticationAdminComponent ??= [];
2063
- serverConfig.types.authenticationAdminComponent.push({
2064
- type: AuthenticationAdminComponentType.EntityStorage,
2065
- options: {
2066
- config: {}
2067
- }
2068
- });
2069
- }
2070
- const authProcessorType = envVars.authProcessorType;
2071
- if (authProcessorType === AuthenticationComponentType.EntityStorage) {
2072
- serverConfig.types.authenticationComponent ??= [];
2073
- serverConfig.types.authenticationComponent.push({
2074
- type: AuthenticationComponentType.EntityStorage,
2075
- options: {
2076
- config: {
2077
- signingKeyName: envVars.authSigningKeyId ?? AUTH_SIGNING_KEY_ID
2078
- }
2079
- }
2080
- });
2081
- serverConfig.types.restRouteProcessor.push({
2082
- type: RestRouteProcessorType.AuthHeader,
2083
- options: {
2084
- config: {
2085
- signingKeyName: envVars.authSigningKeyId ?? AUTH_SIGNING_KEY_ID
2086
- }
2087
- }
2088
- });
2089
- serverConfig.types.socketRouteProcessor.push({
2090
- type: SocketRouteProcessorType.AuthHeader,
2091
- options: {
2092
- config: {
2093
- signingKeyName: envVars.authSigningKeyId ?? AUTH_SIGNING_KEY_ID
2094
- }
2095
- }
2096
- });
2097
- }
2098
- if (Coerce.boolean(envVars.vcAuthenticationEnabled) ?? false) {
2099
- serverConfig.types.restRouteProcessor.push({
2100
- type: RestRouteProcessorType.AuthVerifiableCredential
2101
- });
2102
- serverConfig.types.socketRouteProcessor.push({
2103
- type: SocketRouteProcessorType.AuthVerifiableCredential
2104
- });
2105
- }
2106
- addDefaultRestPaths(serverConfig);
2107
- addDefaultSocketPaths(serverConfig);
2108
- return serverConfig;
2109
- }
2110
-
2111
- // Copyright 2024 IOTA Stiftung.
2112
- // SPDX-License-Identifier: Apache-2.0.
2113
- /**
2114
- * Handles the configuration of the extensions.
2115
- * @param envVars The environment variables for the node.
2116
- * @param nodeEngineConfig The node engine config.
2117
- * @returns The config for the core and the server.
2118
- */
2119
- async function extensionsConfiguration(envVars, nodeEngineConfig) {
2120
- if (Is.stringValue(envVars.extensions)) {
2121
- const extensions = envVars.extensions.split(",");
2122
- for (const extension of extensions) {
2123
- let initialiseConfigMethod;
2124
- try {
2125
- CLIDisplay.value(I18n.formatMessage("node.extensionLoading"), extension);
2126
- initialiseConfigMethod = await ModuleHelper.getModuleMethod(extension, "extensionInitialise");
2127
- }
2128
- catch (err) {
2129
- throw new GeneralError("node", "extensionLoadingError", { extension }, err);
2130
- }
2131
- if (Is.function(initialiseConfigMethod)) {
2132
- await initialiseConfigMethod(envVars, nodeEngineConfig);
2133
- }
2134
- }
2135
- }
2136
- return nodeEngineConfig;
2137
- }
2138
- /**
2139
- * Handles the initialisation of the extensions when the engine has been constructed.
2140
- * @param envVars The environment variables for the node.
2141
- * @param engineCore The engine core instance.
2142
- * @returns Nothing.
2143
- */
2144
- async function extensionsInitialiseEngine(envVars, engineCore) {
2145
- if (Is.stringValue(envVars.extensions)) {
2146
- const extensions = envVars.extensions.split(",");
2147
- for (const extension of extensions) {
2148
- let initialiseEngineMethod;
2149
- try {
2150
- engineCore.logInfo(I18n.formatMessage("node.extensionInitialisingEngine", { extension }));
2151
- initialiseEngineMethod =
2152
- await ModuleHelper.getModuleMethod(extension, "extensionInitialiseEngine");
2153
- }
2154
- catch { }
2155
- if (Is.function(initialiseEngineMethod)) {
2156
- await initialiseEngineMethod(engineCore);
2157
- }
2158
- }
2159
- }
2160
- }
2161
- /**
2162
- * Handles the initialisation of the extensions when the engine server has been constructed.
2163
- * @param envVars The environment variables for the node.
2164
- * @param engineCore The engine core instance.
2165
- * @param engineServer The engine server instance.
2166
- * @returns Nothing.
2167
- */
2168
- async function extensionsInitialiseEngineServer(envVars, engineCore, engineServer) {
2169
- if (Is.stringValue(envVars.extensions)) {
2170
- const extensions = envVars.extensions.split(",");
2171
- for (const extension of extensions) {
2172
- let initialiseEngineServerMethod;
2173
- try {
2174
- engineCore.logInfo(I18n.formatMessage("node.extensionInitialisingEngineServer", { extension }));
2175
- initialiseEngineServerMethod =
2176
- await ModuleHelper.getModuleMethod(extension, "extensionInitialiseEngineServer");
2177
- }
2178
- catch { }
2179
- if (Is.function(initialiseEngineServerMethod)) {
2180
- await initialiseEngineServerMethod(engineCore, engineServer);
2181
- }
2182
- }
2183
- }
2184
- }
2185
- /**
2186
- * Handles the shutdown of the extensions.
2187
- * @param envVars The environment variables for the node.
2188
- * @param engineCore The engine core instance.
2189
- * @returns Nothing.
2190
- */
2191
- async function shutdownExtensions(envVars, engineCore) {
2192
- if (Is.stringValue(envVars.extensions)) {
2193
- const extensions = envVars.extensions.split(",");
2194
- for (const extension of extensions) {
2195
- let shutdownMethod;
2196
- try {
2197
- engineCore.logInfo(I18n.formatMessage("node.extensionShutdown", { extension }));
2198
- shutdownMethod = await ModuleHelper.getModuleMethod(extension, "extensionShutdown");
2199
- }
2200
- catch { }
2201
- if (Is.function(shutdownMethod)) {
2202
- await shutdownMethod();
2203
- }
2204
- }
2205
- }
2206
- }
2207
-
2208
- // Copyright 2024 IOTA Stiftung.
2209
- // SPDX-License-Identifier: Apache-2.0.
2210
- /**
2211
- * Start the engine server.
2212
- * @param nodeOptions Optional run options for the engine server.
2213
- * @param nodeEngineConfig The configuration for the engine server.
2214
- * @param envVars The environment variables.
2215
- * @returns The engine server.
2216
- */
2217
- async function start(nodeOptions, nodeEngineConfig, envVars) {
2218
- const entityStorageConnectorType = envVars.entityStorageConnectorType?.split(",") ?? [];
2219
- const blobStorageConnectorType = envVars.blobStorageConnectorType?.split(",") ?? [];
2220
- // If the blob storage or entity storage is configured with file connectors
2221
- // then we need to make sure the storageFileRoot is set
2222
- if ((entityStorageConnectorType.includes(EntityStorageConnectorType.File) ||
2223
- blobStorageConnectorType.includes(BlobStorageConnectorType.File) ||
2224
- Is.empty(nodeOptions?.stateStorage)) &&
2225
- !Is.stringValue(envVars.storageFileRoot)) {
2226
- throw new GeneralError("node", "storageFileRootNotSet", {
2227
- storageFileRoot: `${nodeOptions?.envPrefix ?? ""}_STORAGE_FILE_ROOT`
2228
- });
2229
- }
2230
- // Create the engine instance using file state storage unless one is configured in options
2231
- const engine = new Engine({
2232
- config: nodeEngineConfig,
2233
- stateStorage: nodeOptions?.stateStorage ?? new FileStateStorage(envVars.stateFilename ?? ""),
2234
- customBootstrap: async (core, engineContext) => bootstrap(core, engineContext, envVars)
2235
- });
2236
- // Construct the server with the engine.
2237
- const server = new EngineServer({ engineCore: engine });
2238
- // Extend the engine.
2239
- if (Is.function(nodeOptions?.extendEngine)) {
2240
- engine.logInfo(I18n.formatMessage("node.extendingEngine"));
2241
- await nodeOptions.extendEngine(engine);
2242
- }
2243
- await extensionsInitialiseEngine(envVars, engine);
2244
- // Extend the engine server.
2245
- if (Is.function(nodeOptions?.extendEngineServer)) {
2246
- engine.logInfo(I18n.formatMessage("node.extendingEngineServer"));
2247
- await nodeOptions?.extendEngineServer(server);
2248
- }
2249
- await extensionsInitialiseEngineServer(envVars, engine, server);
2250
- // Need to register the engine with the factory so that background tasks
2251
- // can clone it to spawn new instances.
2252
- EngineCoreFactory.register("engine", () => engine);
2253
- // Start the server, which also starts the engine.
2254
- const canContinue = await server.start();
2255
- if (canContinue) {
2256
- return {
2257
- engine,
2258
- server,
2259
- shutdown: async () => {
2260
- await server.stop();
2261
- await shutdownExtensions(envVars, engine);
2262
- }
2263
- };
2264
- }
2265
- }
2266
-
2267
- // Copyright 2024 IOTA Stiftung.
2268
- // SPDX-License-Identifier: Apache-2.0.
2269
- const moduleCache = {};
2270
- /**
2271
- * Run the TWIN Node server.
2272
- * @param nodeOptions Optional configuration options for running the server.
2273
- * @returns A promise that resolves when the server is started.
2274
- */
2275
- async function run(nodeOptions) {
2276
- try {
2277
- nodeOptions ??= {};
2278
- const serverInfo = {
2279
- name: nodeOptions?.serverName ?? "TWIN Node Server",
2280
- version: nodeOptions?.serverVersion ?? "0.0.2-next.26" // x-release-please-version
2281
- };
2282
- CLIDisplay.header(serverInfo.name, serverInfo.version, "🌩️ ");
2283
- if (!Is.stringValue(nodeOptions?.executionDirectory)) {
2284
- nodeOptions.executionDirectory = getExecutionDirectory();
2285
- }
2286
- CLIDisplay.value("Execution Directory", nodeOptions.executionDirectory);
2287
- nodeOptions.localesDirectory =
2288
- nodeOptions?.localesDirectory ??
2289
- path.resolve(path.join(nodeOptions.executionDirectory, "dist", "locales"));
2290
- CLIDisplay.value("Locales Directory", nodeOptions.localesDirectory);
2291
- await initialiseLocales(nodeOptions.localesDirectory);
2292
- if (Is.empty(nodeOptions?.openApiSpecFile)) {
2293
- const specFile = path.resolve(path.join(nodeOptions.executionDirectory ?? "", "docs", "open-api", "spec.json"));
2294
- CLIDisplay.value("Default OpenAPI Spec File", specFile);
2295
- if (await fileExists(specFile)) {
2296
- nodeOptions ??= {};
2297
- nodeOptions.openApiSpecFile = specFile;
2298
- }
2299
- }
2300
- else {
2301
- CLIDisplay.value("OpenAPI Spec File", nodeOptions.openApiSpecFile);
2302
- }
2303
- if (Is.empty(nodeOptions?.favIconFile)) {
2304
- const favIconFile = path.resolve(path.join(nodeOptions.executionDirectory ?? "", "static", "favicon.png"));
2305
- CLIDisplay.value("Default Favicon File", favIconFile);
2306
- if (await fileExists(favIconFile)) {
2307
- nodeOptions ??= {};
2308
- nodeOptions.favIconFile = favIconFile;
2309
- }
2310
- }
2311
- else {
2312
- CLIDisplay.value("Favicon File", nodeOptions.favIconFile);
2313
- }
2314
- nodeOptions.envPrefix ??= "TWIN_NODE_";
2315
- CLIDisplay.value("Environment Variable Prefix", nodeOptions.envPrefix);
2316
- overrideModuleImport(nodeOptions.executionDirectory ?? "");
2317
- const { nodeEngineConfig, nodeEnvVars: envVars } = await buildConfiguration(
2318
- // This is the only location in the code base that should access process.env directly
2319
- // So we can safely disable the linting rule here.
2320
- // eslint-disable-next-line no-restricted-syntax
2321
- process.env, nodeOptions, serverInfo);
2322
- CLIDisplay.break();
2323
- const startResult = await start(nodeOptions, nodeEngineConfig, envVars);
2324
- if (!Is.empty(startResult)) {
2325
- for (const signal of ["SIGHUP", "SIGINT", "SIGTERM"]) {
2326
- process.on(signal, async () => {
2327
- CLIDisplay.value("Terminate Signal", signal);
2328
- await startResult.shutdown();
2329
- });
2330
- }
2331
- }
2332
- }
2333
- catch (err) {
2334
- CLIDisplay.error(err);
2335
- // eslint-disable-next-line unicorn/no-process-exit
2336
- process.exit(1);
2337
- }
2338
- }
2339
- /**
2340
- * Build the configuration for the TWIN Node server.
2341
- * @param processEnv The environment variables from the process.
2342
- * @param options The options for running the server.
2343
- * @param serverInfo The server information.
2344
- * @returns A promise that resolves to the engine server configuration, environment prefix, environment variables,
2345
- * and options.
2346
- */
2347
- async function buildConfiguration(processEnv, options, serverInfo) {
2348
- let defaultEnvOnly = false;
2349
- if (Is.empty(options?.envFilenames)) {
2350
- const envFile = path.resolve(path.join(options.executionDirectory ?? "", ".env"));
2351
- CLIDisplay.value("Default Environment File", envFile);
2352
- options ??= {};
2353
- options.envFilenames = [envFile];
2354
- defaultEnvOnly = true;
2355
- }
2356
- if (Is.arrayValue(options?.envFilenames)) {
2357
- const output = dotenv.config({
2358
- path: options?.envFilenames,
2359
- quiet: true
2360
- });
2361
- // We don't want to throw an error if the default environment file is not found.
2362
- // Only if we have custom environment files.
2363
- if (!defaultEnvOnly && output.error) {
2364
- throw output.error;
2365
- }
2366
- if (Is.objectValue(output.parsed)) {
2367
- Object.assign(processEnv, output.parsed);
2368
- }
2369
- }
2370
- const envVars = EnvHelper.envToJson(processEnv, options.envPrefix ?? "");
2371
- // Expand any environment variables that use the @file: syntax
2372
- const keys = Object.keys(envVars);
2373
- for (const key of keys) {
2374
- if (Is.stringValue(envVars[key]) &&
2375
- (envVars[key].startsWith("@text:") || envVars[key].startsWith("@json:"))) {
2376
- const filePath = envVars[key].slice(6).trim();
2377
- const embeddedFile = path.resolve(path.join(options.executionDirectory ?? "", filePath));
2378
- if (envVars[key].startsWith("@text:")) {
2379
- CLIDisplay.value(`Expanding Environment Variable: ${key} from text file`, embeddedFile);
2380
- envVars[key] = await loadTextFile(embeddedFile);
2381
- }
2382
- else if (envVars[key].startsWith("@json:")) {
2383
- CLIDisplay.value(`Expanding Environment Variable: ${key} from JSON file`, embeddedFile);
2384
- envVars[key] = await loadJsonFile(embeddedFile);
2385
- }
2386
- }
2387
- }
2388
- // Extend the environment variables with any additional custom configuration.
2389
- if (Is.function(options?.extendEnvVars)) {
2390
- CLIDisplay.task("Extending Environment Variables");
2391
- await options.extendEnvVars(envVars);
2392
- }
2393
- // Build the engine configuration from the environment variables.
2394
- const coreConfig = await buildEngineConfiguration(envVars);
2395
- const engineServerConfig = await buildEngineServerConfiguration(envVars, coreConfig, serverInfo, options?.openApiSpecFile, options?.favIconFile);
2396
- // Merge any custom configuration provided in the options.
2397
- if (Is.arrayValue(options?.configFilenames)) {
2398
- for (const configFile of options.configFilenames) {
2399
- CLIDisplay.value("Loading Configuration File", configFile);
2400
- const configFilePath = path.resolve(path.join(options.executionDirectory ?? "", configFile));
2401
- const config = await loadJsonFile(configFilePath);
2402
- Object.assign(engineServerConfig, config);
2403
- }
2404
- }
2405
- if (Is.objectValue(options?.config)) {
2406
- CLIDisplay.task("Merging Custom Configuration");
2407
- Object.assign(engineServerConfig, options.config);
2408
- }
2409
- // Merge any custom configuration provided in the options.
2410
- if (Is.function(options?.extendConfig)) {
2411
- CLIDisplay.task("Extending Configuration");
2412
- await options.extendConfig(envVars, engineServerConfig);
2413
- }
2414
- const nodeEngineConfig = await extensionsConfiguration(envVars, engineServerConfig);
2415
- return { nodeEngineConfig, nodeEnvVars: envVars };
2416
- }
2417
- /**
2418
- * Override module imports to support protocol-based loading (npm:, https:) and local files.
2419
- * @param executionDirectory The execution directory for resolving local module paths.
2420
- * @param envVars The environment variables containing extension configuration (optional, uses defaults if not provided).
2421
- */
2422
- function overrideModuleImport(executionDirectory, envVars) {
2423
- const maxSizeMb = Coerce.number(envVars?.extensionsMaxSizeMb) ?? 10;
2424
- const cacheDirectory = envVars?.extensionsCacheDirectory;
2425
- ModuleHelper.overrideImport(async (moduleName) => {
2426
- if (moduleCache[moduleName]) {
2427
- return {
2428
- module: moduleCache[moduleName],
2429
- useDefault: false
2430
- };
2431
- }
2432
- const parsed = parseModuleProtocol(moduleName);
2433
- let resolvedPath;
2434
- switch (parsed.protocol) {
2435
- case ModuleProtocol.Npm: {
2436
- const result = await handleNpmProtocol(parsed.identifier, executionDirectory, cacheDirectory);
2437
- resolvedPath = result.resolvedPath;
2438
- break;
2439
- }
2440
- case ModuleProtocol.Https: {
2441
- const result = await handleHttpsProtocol(parsed.identifier, executionDirectory, maxSizeMb, cacheDirectory, envVars?.extensionsCacheTtlHours, envVars?.extensionsForceRefresh);
2442
- resolvedPath = result.resolvedPath;
2443
- break;
2444
- }
2445
- case ModuleProtocol.Http: {
2446
- throw new GeneralError("node", "insecureProtocol", { protocol: ModuleProtocol.Http });
2447
- }
2448
- case ModuleProtocol.Local: {
2449
- let localFilename = path.resolve(moduleName);
2450
- let exists = await fileExists(localFilename);
2451
- if (!exists) {
2452
- localFilename = path.resolve(executionDirectory, moduleName);
2453
- exists = await fileExists(localFilename);
2454
- }
2455
- if (exists) {
2456
- resolvedPath = localFilename;
2457
- }
2458
- break;
2459
- }
2460
- case ModuleProtocol.Default: {
2461
- try {
2462
- const npmRoot = execSync("npm root").toString().trim().replace(/\\/g, "/");
2463
- const packagePath = path.resolve(npmRoot, moduleName);
2464
- const mainFile = await resolvePackageEntryPoint(packagePath, moduleName);
2465
- const modulePath = path.resolve(packagePath, mainFile);
2466
- const exists = await fileExists(modulePath);
2467
- if (exists) {
2468
- resolvedPath = modulePath;
2469
- break;
2470
- }
2471
- }
2472
- catch {
2473
- // Continue to fallback resolution
2474
- }
2475
- // Fallback: resolve from npm protocol cache directory (installed via handleNpmProtocol)
2476
- try {
2477
- const cacheNpmRoot = path.resolve(getExtensionsCacheDir(executionDirectory, ModuleProtocol.Npm, cacheDirectory), "node_modules");
2478
- const packagePath = path.resolve(cacheNpmRoot, moduleName);
2479
- const mainFile = await resolvePackageEntryPoint(packagePath, moduleName);
2480
- const modulePath = path.resolve(packagePath, mainFile);
2481
- const exists = await fileExists(modulePath);
2482
- if (exists) {
2483
- resolvedPath = modulePath;
2484
- }
2485
- }
2486
- catch {
2487
- // No cached resolution either; fall through
2488
- }
2489
- break;
2490
- }
2491
- }
2492
- // Common module loading and caching logic
2493
- if (resolvedPath) {
2494
- const module = await import(createModuleImportUrl(resolvedPath));
2495
- moduleCache[moduleName] = module;
2496
- return {
2497
- module,
2498
- useDefault: false
2499
- };
2500
- }
2501
- return {
2502
- module: undefined,
2503
- useDefault: true
2504
- };
2505
- });
2506
- }
2507
-
2508
- export { ATTESTATION_VERIFICATION_METHOD_ID, AUTH_SIGNING_KEY_ID, BLOB_STORAGE_ENCRYPTION_KEY_ID, IMMUTABLE_PROOF_VERIFICATION_METHOD_ID, ModuleProtocol, NodeFeatures, SYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID, VC_AUTHENTICATION_VERIFICATION_METHOD_ID, bootstrap, bootstrapAuth, bootstrapBlobEncryption, bootstrapImmutableProofMethod, bootstrapNodeIdentity, bootstrapNodeUser, bootstrapSynchronisedStorage, buildConfiguration, buildEngineConfiguration, buildEngineServerConfiguration, createModuleImportUrl, directoryExists, extensionsConfiguration, extensionsInitialiseEngine, extensionsInitialiseEngineServer, fileExists, getExecutionDirectory, getExtensionsCacheDir, getFeatures, getFiles, getSubFolders, handleHttpsProtocol, handleNpmProtocol, hashUrl, initialiseLocales, isCacheExpired, loadJsonFile, loadTextFile, overrideModuleImport, parseModuleProtocol, resolvePackageEntryPoint, run, shutdownExtensions, start };