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