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