@twin.org/node-core 0.0.2-next.8 → 0.0.3-next.1

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