@twin.org/node-core 0.0.3-next.2 → 0.0.3-next.21

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 (151) hide show
  1. package/dist/es/builders/engineEnvBuilder.js +168 -105
  2. package/dist/es/builders/engineEnvBuilder.js.map +1 -1
  3. package/dist/es/builders/engineServerEnvBuilder.js +49 -25
  4. package/dist/es/builders/engineServerEnvBuilder.js.map +1 -1
  5. package/dist/es/cli.js +247 -0
  6. package/dist/es/cli.js.map +1 -0
  7. package/dist/es/commands/bootstrapLegacy.js +183 -0
  8. package/dist/es/commands/bootstrapLegacy.js.map +1 -0
  9. package/dist/es/commands/help.js +85 -0
  10. package/dist/es/commands/help.js.map +1 -0
  11. package/dist/es/commands/identityCreate.js +316 -0
  12. package/dist/es/commands/identityCreate.js.map +1 -0
  13. package/dist/es/commands/identityImports.js +82 -0
  14. package/dist/es/commands/identityImports.js.map +1 -0
  15. package/dist/es/commands/identityVerifiableCredentialCreate.js +146 -0
  16. package/dist/es/commands/identityVerifiableCredentialCreate.js.map +1 -0
  17. package/dist/es/commands/identityVerificationMethodCreate.js +214 -0
  18. package/dist/es/commands/identityVerificationMethodCreate.js.map +1 -0
  19. package/dist/es/commands/identityVerificationMethodImport.js +126 -0
  20. package/dist/es/commands/identityVerificationMethodImport.js.map +1 -0
  21. package/dist/es/commands/nodeSetIdentity.js +64 -0
  22. package/dist/es/commands/nodeSetIdentity.js.map +1 -0
  23. package/dist/es/commands/nodeSetTenant.js +68 -0
  24. package/dist/es/commands/nodeSetTenant.js.map +1 -0
  25. package/dist/es/commands/tenantCreate.js +139 -0
  26. package/dist/es/commands/tenantCreate.js.map +1 -0
  27. package/dist/es/commands/tenantImport.js +97 -0
  28. package/dist/es/commands/tenantImport.js.map +1 -0
  29. package/dist/es/commands/tenantUpdate.js +94 -0
  30. package/dist/es/commands/tenantUpdate.js.map +1 -0
  31. package/dist/es/commands/userCreate.js +212 -0
  32. package/dist/es/commands/userCreate.js.map +1 -0
  33. package/dist/es/commands/userUpdate.js +132 -0
  34. package/dist/es/commands/userUpdate.js.map +1 -0
  35. package/dist/es/commands/vaultKeyCreate.js +191 -0
  36. package/dist/es/commands/vaultKeyCreate.js.map +1 -0
  37. package/dist/es/commands/vaultKeyImport.js +104 -0
  38. package/dist/es/commands/vaultKeyImport.js.map +1 -0
  39. package/dist/es/defaults.js +19 -1
  40. package/dist/es/defaults.js.map +1 -1
  41. package/dist/es/index.js +7 -3
  42. package/dist/es/index.js.map +1 -1
  43. package/dist/es/models/ICliArgs.js +4 -0
  44. package/dist/es/models/ICliArgs.js.map +1 -0
  45. package/dist/es/models/ICliCommand.js +2 -0
  46. package/dist/es/models/ICliCommand.js.map +1 -0
  47. package/dist/es/models/ICliCommandDefinition.js +2 -0
  48. package/dist/es/models/ICliCommandDefinition.js.map +1 -0
  49. package/dist/es/models/ICliCommandDefinitionParam.js +2 -0
  50. package/dist/es/models/ICliCommandDefinitionParam.js.map +1 -0
  51. package/dist/es/models/IEngineEnvironmentVariables.js.map +1 -1
  52. package/dist/es/models/IEngineServerEnvironmentVariables.js.map +1 -1
  53. package/dist/es/models/INodeEngineState.js.map +1 -1
  54. package/dist/es/models/INodeEnvironmentVariables.js.map +1 -1
  55. package/dist/es/models/INodeOptions.js.map +1 -1
  56. package/dist/es/models/cliCommandParamType.js +4 -0
  57. package/dist/es/models/cliCommandParamType.js.map +1 -0
  58. package/dist/es/node.js +78 -41
  59. package/dist/es/node.js.map +1 -1
  60. package/dist/es/start.js +139 -0
  61. package/dist/es/start.js.map +1 -0
  62. package/dist/es/utils.js +11 -21
  63. package/dist/es/utils.js.map +1 -1
  64. package/dist/types/builders/engineEnvBuilder.d.ts +4 -1
  65. package/dist/types/builders/engineServerEnvBuilder.d.ts +5 -2
  66. package/dist/types/cli.d.ts +56 -0
  67. package/dist/types/commands/bootstrapLegacy.d.ts +66 -0
  68. package/dist/types/commands/help.d.ts +23 -0
  69. package/dist/types/commands/identityCreate.d.ts +39 -0
  70. package/dist/types/commands/identityImports.d.ts +24 -0
  71. package/dist/types/commands/identityVerifiableCredentialCreate.d.ts +43 -0
  72. package/dist/types/commands/identityVerificationMethodCreate.d.ts +47 -0
  73. package/dist/types/commands/identityVerificationMethodImport.d.ts +31 -0
  74. package/dist/types/commands/nodeSetIdentity.d.ts +22 -0
  75. package/dist/types/commands/nodeSetTenant.d.ts +22 -0
  76. package/dist/types/commands/tenantCreate.d.ts +38 -0
  77. package/dist/types/commands/tenantImport.d.ts +26 -0
  78. package/dist/types/commands/tenantUpdate.d.ts +26 -0
  79. package/dist/types/commands/userCreate.d.ts +49 -0
  80. package/dist/types/commands/userUpdate.d.ts +38 -0
  81. package/dist/types/commands/vaultKeyCreate.d.ts +43 -0
  82. package/dist/types/commands/vaultKeyImport.d.ts +28 -0
  83. package/dist/types/defaults.d.ts +11 -1
  84. package/dist/types/index.d.ts +7 -3
  85. package/dist/types/models/ICliArgs.d.ts +20 -0
  86. package/dist/types/models/ICliCommand.d.ts +17 -0
  87. package/dist/types/models/ICliCommandDefinition.d.ts +46 -0
  88. package/dist/types/models/ICliCommandDefinitionParam.d.ts +35 -0
  89. package/dist/types/models/IEngineEnvironmentVariables.d.ts +62 -53
  90. package/dist/types/models/IEngineServerEnvironmentVariables.d.ts +4 -0
  91. package/dist/types/models/INodeEngineState.d.ts +0 -8
  92. package/dist/types/models/INodeEnvironmentVariables.d.ts +0 -38
  93. package/dist/types/models/INodeOptions.d.ts +6 -2
  94. package/dist/types/models/cliCommandParamType.d.ts +4 -0
  95. package/dist/types/node.d.ts +8 -4
  96. package/dist/types/{server.d.ts → start.d.ts} +7 -2
  97. package/dist/types/utils.d.ts +6 -8
  98. package/docs/changelog.md +136 -0
  99. package/docs/reference/functions/buildConfiguration.md +3 -3
  100. package/docs/reference/functions/buildEngineConfiguration.md +1 -1
  101. package/docs/reference/functions/buildEngineServerConfiguration.md +3 -3
  102. package/docs/reference/functions/constructCliCommand.md +27 -0
  103. package/docs/reference/functions/executeCommand.md +29 -0
  104. package/docs/reference/functions/getEnvDefaults.md +19 -0
  105. package/docs/reference/functions/getScriptDirectory.md +19 -0
  106. package/docs/reference/functions/parseCommandLineArgs.md +19 -0
  107. package/docs/reference/functions/processEnvOptions.md +27 -0
  108. package/docs/reference/functions/registerCommands.md +9 -0
  109. package/docs/reference/functions/run.md +8 -2
  110. package/docs/reference/functions/start.md +10 -4
  111. package/docs/reference/functions/substituteEnvOptions.md +25 -0
  112. package/docs/reference/index.md +16 -13
  113. package/docs/reference/interfaces/ICliArgs.md +35 -0
  114. package/docs/reference/interfaces/ICliCommand.md +23 -0
  115. package/docs/reference/interfaces/ICliCommandDefinition.md +101 -0
  116. package/docs/reference/interfaces/ICliCommandDefinitionParam.md +65 -0
  117. package/docs/reference/interfaces/IEngineEnvironmentVariables.md +110 -85
  118. package/docs/reference/interfaces/IEngineServerEnvironmentVariables.md +166 -117
  119. package/docs/reference/interfaces/INodeEngineState.md +0 -16
  120. package/docs/reference/interfaces/INodeEnvironmentVariables.md +170 -201
  121. package/docs/reference/interfaces/INodeOptions.md +10 -2
  122. package/docs/reference/type-aliases/CliCommandParamType.md +5 -0
  123. package/docs/reference/variables/CONTEXT_ID_HANDLER_FEATURE_DID.md +3 -0
  124. package/docs/reference/variables/CONTEXT_ID_HANDLER_FEATURE_TENANT.md +3 -0
  125. package/docs/reference/variables/TRUST_VERIFICATION_METHOD_ID.md +3 -0
  126. package/locales/en.json +589 -30
  127. package/package.json +3 -1
  128. package/dist/es/bootstrap.js +0 -374
  129. package/dist/es/bootstrap.js.map +0 -1
  130. package/dist/es/identity.js +0 -169
  131. package/dist/es/identity.js.map +0 -1
  132. package/dist/es/models/nodeFeatures.js +0 -21
  133. package/dist/es/models/nodeFeatures.js.map +0 -1
  134. package/dist/es/server.js +0 -78
  135. package/dist/es/server.js.map +0 -1
  136. package/dist/types/bootstrap.d.ts +0 -76
  137. package/dist/types/identity.d.ts +0 -14
  138. package/dist/types/models/nodeFeatures.d.ts +0 -21
  139. package/docs/reference/functions/bootstrap.md +0 -29
  140. package/docs/reference/functions/bootstrapAuth.md +0 -35
  141. package/docs/reference/functions/bootstrapBlobEncryption.md +0 -35
  142. package/docs/reference/functions/bootstrapContextIdHandlers.md +0 -35
  143. package/docs/reference/functions/bootstrapImmutableProofMethod.md +0 -35
  144. package/docs/reference/functions/bootstrapNodeAdminUser.md +0 -35
  145. package/docs/reference/functions/bootstrapNodeId.md +0 -35
  146. package/docs/reference/functions/bootstrapSynchronisedStorage.md +0 -35
  147. package/docs/reference/functions/bootstrapTenantId.md +0 -35
  148. package/docs/reference/functions/getFeatures.md +0 -19
  149. package/docs/reference/type-aliases/NodeFeatures.md +0 -5
  150. package/docs/reference/variables/NodeFeatures.md +0 -25
  151. package/docs/reference/variables/VC_AUTHENTICATION_VERIFICATION_METHOD_ID.md +0 -3
@@ -1,374 +0,0 @@
1
- // Copyright 2024 IOTA Stiftung.
2
- // SPDX-License-Identifier: Apache-2.0.
3
- import { PasswordHelper } from "@twin.org/api-auth-entity-storage-service";
4
- import { TenantIdContextIdHandler, TenantIdHelper } from "@twin.org/api-tenant-processor";
5
- import { ContextIdHandlerFactory, ContextIdKeys, ContextIdStore } from "@twin.org/context";
6
- import { Coerce, ComponentFactory, Converter, I18n, Is, RandomHelper } from "@twin.org/core";
7
- import { PasswordGenerator } from "@twin.org/crypto";
8
- import { AuthenticationComponentType } from "@twin.org/engine-server-types";
9
- import { EntityStorageConnectorFactory } from "@twin.org/entity-storage-models";
10
- import { DidContextIdHandler, DocumentHelper, IdentityConnectorFactory, IdentityProfileConnectorFactory, IdentityResolverConnectorFactory } from "@twin.org/identity-models";
11
- import { VaultConnectorFactory, VaultKeyType } from "@twin.org/vault-models";
12
- import { ATTESTATION_VERIFICATION_METHOD_ID, AUTH_SIGNING_KEY_ID, BLOB_STORAGE_ENCRYPTION_KEY_ID, IMMUTABLE_PROOF_VERIFICATION_METHOD_ID, SYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID, VC_AUTHENTICATION_VERIFICATION_METHOD_ID } from "./defaults.js";
13
- import { createIdentity } from "./identity.js";
14
- import { NodeFeatures } from "./models/nodeFeatures.js";
15
- import { getFeatures } from "./utils.js";
16
- const DEFAULT_NODE_ADMIN_USERNAME = "admin@node";
17
- /**
18
- * Bootstrap the application.
19
- * @param engineCore The engine core for the node.
20
- * @param context The context for the node.
21
- * @param envVars The environment variables for the node.
22
- */
23
- export async function bootstrap(engineCore, context, envVars) {
24
- const features = getFeatures(envVars);
25
- await bootstrapContextIdHandlers(engineCore, context, envVars, features);
26
- await bootstrapNodeId(engineCore, context, envVars, features);
27
- await ContextIdStore.run(engineCore.getContextIds() ?? {}, async () => {
28
- await bootstrapTenantId(engineCore, context, envVars, features);
29
- await bootstrapNodeAdminUser(engineCore, context, envVars, features);
30
- await bootstrapAuth(engineCore, context, envVars, features);
31
- await bootstrapBlobEncryption(engineCore, context, envVars, features);
32
- const defaultAttestationConnectorType = engineCore.getRegisteredInstanceTypeOptional("attestationConnector");
33
- if (!Is.empty(defaultAttestationConnectorType) &&
34
- Is.stringValue(context.state.nodeOrganizationId)) {
35
- await addVerificationMethod(engineCore, context, context.state.nodeOrganizationId, "attestation", envVars.attestationVerificationMethodId ?? ATTESTATION_VERIFICATION_METHOD_ID);
36
- }
37
- const defaultImmutableProofComponentType = engineCore.getRegisteredInstanceTypeOptional("immutableProofComponent");
38
- if (!Is.empty(defaultImmutableProofComponentType) &&
39
- Is.stringValue(context.state.nodeOrganizationId)) {
40
- await addVerificationMethod(engineCore, context, context.state.nodeOrganizationId, "immutable proof", envVars.immutableProofVerificationMethodId ?? IMMUTABLE_PROOF_VERIFICATION_METHOD_ID);
41
- }
42
- if ((Coerce.boolean(envVars.vcAuthenticationEnabled) ?? false) &&
43
- Is.stringValue(context.state.nodeId)) {
44
- await addVerificationMethod(engineCore, context, context.state.nodeId, "verifiable credential authentication", envVars.vcAuthenticationVerificationMethodId ?? VC_AUTHENTICATION_VERIFICATION_METHOD_ID);
45
- }
46
- await bootstrapSynchronisedStorage(engineCore, context, envVars, features);
47
- });
48
- }
49
- /**
50
- * Bootstrap the context id handlers creating any necessary resources.
51
- * @param engineCore The engine core for the node.
52
- * @param context The context for the node.
53
- * @param envVars The environment variables for the node.
54
- * @param features The features that are enabled on the node. The features that are enabled on the node.
55
- */
56
- export async function bootstrapContextIdHandlers(engineCore, context, envVars, features) {
57
- ContextIdHandlerFactory.register(ContextIdKeys.Node, () => new DidContextIdHandler());
58
- ContextIdHandlerFactory.register(ContextIdKeys.Tenant, () => new TenantIdContextIdHandler());
59
- ContextIdHandlerFactory.register(ContextIdKeys.Organization, () => new DidContextIdHandler());
60
- ContextIdHandlerFactory.register(ContextIdKeys.User, () => new DidContextIdHandler());
61
- }
62
- /**
63
- * Bootstrap the node creating any necessary resources.
64
- * @param engineCore The engine core for the node.
65
- * @param context The context for the node.
66
- * @param envVars The environment variables for the node.
67
- * @param features The features that are enabled on the node. The features that are enabled on the node.
68
- */
69
- export async function bootstrapNodeId(engineCore, context, envVars, features) {
70
- if (features.includes(NodeFeatures.NodeId)) {
71
- const existingNodeId = envVars.nodeIdentity ?? context.state.nodeId;
72
- context.state.nodeId = await createIdentity(engineCore, envVars, existingNodeId, envVars.nodeMnemonic, existingNodeId, "node", features.includes(NodeFeatures.NodeWallet));
73
- context.stateDirty = true;
74
- engineCore.logInfo(I18n.formatMessage("node.nodeId", {
75
- identity: context.state.nodeId
76
- }));
77
- engineCore.addContextId(ContextIdKeys.Node, context.state.nodeId);
78
- }
79
- }
80
- /**
81
- * Bootstrap the node creating any necessary resources.
82
- * @param engineCore The engine core for the node.
83
- * @param context The context for the node.
84
- * @param envVars The environment variables for the node.
85
- * @param features The features that are enabled on the node. The features that are enabled on the node.
86
- */
87
- export async function bootstrapTenantId(engineCore, context, envVars, features) {
88
- // If tenants are enabled we need to add a context id for the node
89
- // so that services such a logging have a default tenant context id
90
- // this will get overwritten by any incoming API requests with the tenant context id
91
- if (Coerce.boolean(envVars.tenantEnabled) ?? false) {
92
- let tenantId = envVars.tenantId ?? context.state.nodeTenantId;
93
- if (!Is.stringValue(tenantId)) {
94
- const tenantAdminServiceComponentType = engineCore.getRegisteredInstanceType("tenantAdminComponent");
95
- const tenantAdminService = ComponentFactory.get(tenantAdminServiceComponentType);
96
- tenantId = TenantIdHelper.generateTenantId();
97
- const apiKey = envVars.tenantApiKey ?? TenantIdHelper.generateApiKey();
98
- await tenantAdminService.set({
99
- id: tenantId,
100
- apiKey,
101
- dateCreated: new Date(Date.now()).toISOString(),
102
- label: "node-tenant"
103
- });
104
- engineCore.logInfo(I18n.formatMessage("node.createdTenantId", {
105
- identity: tenantId,
106
- apiKey
107
- }));
108
- }
109
- else {
110
- engineCore.logInfo(I18n.formatMessage("node.existingTenantId", {
111
- identity: context.state.nodeTenantId
112
- }));
113
- }
114
- context.state.nodeTenantId = tenantId;
115
- context.stateDirty = true;
116
- engineCore.addContextId(ContextIdKeys.Tenant, context.state.nodeTenantId);
117
- }
118
- }
119
- /**
120
- * Bootstrap the user.
121
- * @param engineCore The engine core for the node.
122
- * @param context The context for the node.
123
- * @param envVars The environment variables for the node.
124
- * @param features The features that are enabled on the node.
125
- */
126
- export async function bootstrapNodeAdminUser(engineCore, context, envVars, features) {
127
- if (features.includes(NodeFeatures.NodeAdminUser)) {
128
- context.state.nodeOrganizationId =
129
- envVars.organizationIdentity ?? context.state.nodeOrganizationId;
130
- context.state.nodeAdminUserId = envVars.adminUserIdentity ?? context.state.nodeAdminUserId;
131
- const defaultAuthenticationComponentType = engineCore.getRegisteredInstanceType("authenticationComponent");
132
- if (defaultAuthenticationComponentType.startsWith(AuthenticationComponentType.EntityStorage) &&
133
- Is.stringValue(context.state.nodeId)) {
134
- const authUserEntityStorage = EntityStorageConnectorFactory.get("authentication-user");
135
- // If we don't have an organization identity, create one
136
- if (!Is.stringValue(context.state.nodeOrganizationId)) {
137
- context.state.nodeOrganizationId = await createIdentity(engineCore, envVars, context.state.nodeOrganizationId, envVars.organizationMnemonic, context.state.nodeId, "organization", features.includes(NodeFeatures.NodeWallet));
138
- context.stateDirty = true;
139
- }
140
- if (!Is.stringValue(context.state.nodeAdminUserId)) {
141
- context.state.nodeAdminUserId = await createIdentity(engineCore, envVars, context.state.nodeAdminUserId, envVars.adminUserMnemonic, context.state.nodeOrganizationId, "user", false);
142
- context.stateDirty = true;
143
- }
144
- const adminEmail = envVars.adminUserName ?? DEFAULT_NODE_ADMIN_USERNAME;
145
- let nodeAdminUser = await authUserEntityStorage.get(adminEmail);
146
- // If the node admin user doesn't exist, create it
147
- if (Is.empty(nodeAdminUser)) {
148
- engineCore.logInfo(I18n.formatMessage("node.creatingUser", { email: adminEmail }));
149
- const generatedPassword = envVars.adminUserPassword ?? PasswordGenerator.generate(16);
150
- const passwordBytes = Converter.utf8ToBytes(generatedPassword);
151
- const saltBytes = RandomHelper.generate(16);
152
- const hashedPassword = await PasswordHelper.hashPassword(passwordBytes, saltBytes);
153
- nodeAdminUser = {
154
- email: adminEmail,
155
- password: hashedPassword,
156
- salt: Converter.bytesToBase64(saltBytes),
157
- identity: context.state.nodeAdminUserId,
158
- organization: context.state.nodeOrganizationId
159
- };
160
- engineCore.logInfo(I18n.formatMessage("node.nodeAdminUserEmail", { email: adminEmail }));
161
- engineCore.logInfo(I18n.formatMessage("node.nodeAdminUserPassword", { password: generatedPassword }));
162
- await authUserEntityStorage.set(nodeAdminUser);
163
- }
164
- else {
165
- engineCore.logInfo(I18n.formatMessage("node.existingUser", { email: adminEmail }));
166
- // The user already exists, so double check the other details match
167
- let needsUpdate = false;
168
- if (nodeAdminUser.identity !== context.state.nodeAdminUserId) {
169
- nodeAdminUser.identity = context.state.nodeAdminUserId;
170
- needsUpdate = true;
171
- }
172
- if (Is.stringValue(envVars.adminUserPassword)) {
173
- const passwordBytes = Converter.utf8ToBytes(envVars.adminUserPassword);
174
- const saltBytes = Converter.base64ToBytes(nodeAdminUser.salt);
175
- const hashedPassword = await PasswordHelper.hashPassword(passwordBytes, saltBytes);
176
- if (nodeAdminUser.password !== hashedPassword) {
177
- nodeAdminUser.password = hashedPassword;
178
- needsUpdate = true;
179
- }
180
- }
181
- if (needsUpdate) {
182
- await authUserEntityStorage.set(nodeAdminUser);
183
- }
184
- }
185
- // We have create a node user, now we need to create a profile for the user
186
- const defaultIdentityProfileConnectorType = engineCore.getRegisteredInstanceType("identityProfileConnector");
187
- const identityProfileConnector = IdentityProfileConnectorFactory.get(defaultIdentityProfileConnectorType);
188
- if (identityProfileConnector) {
189
- // Add the organization context id when creating the profile
190
- // so that it is partitioned under the organization
191
- const contextIds = (await ContextIdStore.getContextIds()) ?? {};
192
- contextIds[ContextIdKeys.Organization] = context.state.nodeOrganizationId;
193
- await ContextIdStore.run(contextIds, async () => {
194
- let userProfile;
195
- if (Is.stringValue(nodeAdminUser.identity)) {
196
- try {
197
- userProfile = await identityProfileConnector.get(nodeAdminUser.identity);
198
- }
199
- catch { }
200
- }
201
- if (Is.empty(userProfile)) {
202
- engineCore.logInfo(I18n.formatMessage("node.creatingUserProfile", {
203
- identity: nodeAdminUser.identity
204
- }));
205
- const publicProfile = {
206
- "@context": "https://schema.org",
207
- "@type": "Person",
208
- name: "Node Administrator"
209
- };
210
- const privateProfile = {
211
- "@context": "https://schema.org",
212
- "@type": "Person",
213
- givenName: "Node",
214
- familyName: "Administrator",
215
- email: adminEmail
216
- };
217
- await identityProfileConnector.create(nodeAdminUser.identity, publicProfile, privateProfile);
218
- }
219
- else {
220
- engineCore.logInfo(I18n.formatMessage("node.existingUserProfile", {
221
- identity: nodeAdminUser.identity
222
- }));
223
- }
224
- });
225
- }
226
- }
227
- }
228
- }
229
- /**
230
- * Bootstrap the immutable proof verification methods.
231
- * @param engineCore The engine core for the node.
232
- * @param context The context for the node.
233
- * @param envVars The environment variables for the node.
234
- * @param features The features that are enabled on the node.
235
- */
236
- export async function bootstrapImmutableProofMethod(engineCore, context, envVars, features) { }
237
- /**
238
- * Bootstrap the keys for blob encryption.
239
- * @param engineCore The engine core for the node.
240
- * @param context The context for the node.
241
- * @param envVars The environment variables for the node.
242
- * @param features The features that are enabled on the node.
243
- */
244
- export async function bootstrapBlobEncryption(engineCore, context, envVars, features) {
245
- if ((Coerce.boolean(envVars.blobStorageEnableEncryption) ?? false) &&
246
- Is.stringValue(context.state.nodeOrganizationId)) {
247
- // Create a new key for encrypting blobs
248
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
249
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
250
- const keyName = `${context.state.nodeOrganizationId}/${envVars.blobStorageEncryptionKeyId ?? BLOB_STORAGE_ENCRYPTION_KEY_ID}`;
251
- let existingKey;
252
- try {
253
- existingKey = await vaultConnector.getKey(keyName);
254
- }
255
- catch { }
256
- if (Is.empty(existingKey)) {
257
- if (Is.stringBase64(envVars.blobStorageSymmetricEncryptionKey)) {
258
- engineCore.logInfo(I18n.formatMessage("node.addingBlobEncryptionKey", { keyName }));
259
- await vaultConnector.addKey(keyName, VaultKeyType.ChaCha20Poly1305, Converter.base64ToBytes(envVars.blobStorageSymmetricEncryptionKey));
260
- }
261
- else {
262
- engineCore.logInfo(I18n.formatMessage("node.creatingBlobEncryptionKey", { keyName }));
263
- const key = await vaultConnector.createKey(keyName, VaultKeyType.ChaCha20Poly1305);
264
- engineCore.logInfo(I18n.formatMessage("node.createdBlobEncryptionKey", {
265
- keyName,
266
- keyValue: Converter.bytesToBase64(key)
267
- }));
268
- }
269
- }
270
- else {
271
- engineCore.logInfo(I18n.formatMessage("node.existingBlobEncryptionKey", { keyName }));
272
- }
273
- }
274
- }
275
- /**
276
- * Bootstrap the JWT signing key.
277
- * @param engineCore The engine core for the node.
278
- * @param context The context for the node.
279
- * @param envVars The environment variables for the node.
280
- * @param features The features that are enabled on the node.
281
- */
282
- export async function bootstrapAuth(engineCore, context, envVars, features) {
283
- const defaultAuthenticationComponentType = engineCore.getRegisteredInstanceTypeOptional("authenticationComponent");
284
- if (Is.stringValue(defaultAuthenticationComponentType) &&
285
- defaultAuthenticationComponentType.startsWith(AuthenticationComponentType.EntityStorage) &&
286
- Is.stringValue(context.state.nodeId)) {
287
- // Create a new JWT signing key and a user login for the node
288
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
289
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
290
- const keyName = `${context.state.nodeId}/${envVars.authSigningKeyId ?? AUTH_SIGNING_KEY_ID}`;
291
- let existingKey;
292
- try {
293
- existingKey = await vaultConnector.getKey(keyName);
294
- }
295
- catch { }
296
- if (Is.empty(existingKey)) {
297
- engineCore.logInfo(I18n.formatMessage("node.creatingAuthKey", { keyName }));
298
- await vaultConnector.createKey(keyName, VaultKeyType.Ed25519);
299
- }
300
- else {
301
- engineCore.logInfo(I18n.formatMessage("node.existingAuthKey", { keyName }));
302
- }
303
- }
304
- }
305
- /**
306
- * Bootstrap the synchronised storage blob encryption and verification methods.
307
- * @param engineCore The engine core for the node.
308
- * @param context The context for the node.
309
- * @param envVars The environment variables for the node.
310
- * @param features The features that are enabled on the node.
311
- */
312
- export async function bootstrapSynchronisedStorage(engineCore, context, envVars, features) {
313
- if (Coerce.boolean(envVars.synchronisedStorageEnabled) ?? false) {
314
- // If this is a trusted node we need to add the blob encryption key pair
315
- if (Is.stringBase64(envVars.synchronisedStorageBlobStorageKey)) {
316
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
317
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
318
- const keyName = envVars.synchronisedStorageBlobStorageEncryptionKeyId ??
319
- SYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID;
320
- let existingKey;
321
- try {
322
- existingKey = await vaultConnector.getKey(keyName);
323
- }
324
- catch { }
325
- if (Is.empty(existingKey)) {
326
- engineCore.logInfo(I18n.formatMessage("node.addingSynchronisedStorageBlobEncryptionKey", { keyName }));
327
- await vaultConnector.addKey(keyName, VaultKeyType.ChaCha20Poly1305, Converter.base64ToBytes(envVars.synchronisedStorageBlobStorageKey));
328
- }
329
- else {
330
- engineCore.logInfo(I18n.formatMessage("node.existingSynchronisedStorageBlobEncryptionKey", { keyName }));
331
- }
332
- }
333
- }
334
- }
335
- /**
336
- * Add a verification method if it doesn't exist.
337
- * @param engineCore The engine core for the node.
338
- * @param context The context for the node.
339
- * @param identity The identity to add the verification method to.
340
- * @param verificationMethodTitle The verification method title.
341
- * @param verificationMethodId The verification method ID.
342
- */
343
- async function addVerificationMethod(engineCore, context, identity, verificationMethodTitle, verificationMethodId) {
344
- if (Is.stringValue(identity) &&
345
- Is.arrayValue(context.config.types.identityConnector) &&
346
- Is.stringValue(verificationMethodId)) {
347
- const defaultIdentityConnectorType = engineCore.getRegisteredInstanceType("identityConnector");
348
- const identityConnector = IdentityConnectorFactory.get(defaultIdentityConnectorType);
349
- const defaultIdentityResolverConnectorType = engineCore.getRegisteredInstanceType("identityResolverConnector");
350
- const identityResolverConnector = IdentityResolverConnectorFactory.get(defaultIdentityResolverConnectorType);
351
- const identityDocument = await identityResolverConnector.resolveDocument(identity);
352
- const fullMethodId = `${identityDocument.id}#${verificationMethodId}`;
353
- let exists = false;
354
- try {
355
- DocumentHelper.getVerificationMethod(identityDocument, fullMethodId, "assertionMethod");
356
- exists = true;
357
- }
358
- catch { }
359
- if (!exists) {
360
- engineCore.logInfo(I18n.formatMessage("node.addingVerificationMethod", {
361
- title: verificationMethodTitle,
362
- methodId: fullMethodId
363
- }));
364
- await identityConnector.addVerificationMethod(identity, identity, "assertionMethod", verificationMethodId);
365
- }
366
- else {
367
- engineCore.logInfo(I18n.formatMessage("node.existingVerificationMethod", {
368
- title: verificationMethodTitle,
369
- methodId: fullMethodId
370
- }));
371
- }
372
- }
373
- }
374
- //# sourceMappingURL=bootstrap.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../../src/bootstrap.ts"],"names":[],"mappings":"AAAA,gCAAgC;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aACtB,CAAC,CACF,CAAC;YACF,MAAM,iBAAiB,CAAC,qBAAqB,CAC5C,QAAQ,EACR,QAAQ,EACR,iBAAiB,EACjB,oBAAoB,CACpB,CAAC;QACH,CAAC;aAAM,CAAC;YACP,UAAU,CAAC,OAAO,CACjB,IAAI,CAAC,aAAa,CAAC,iCAAiC,EAAE;gBACrD,KAAK,EAAE,uBAAuB;gBAC9B,QAAQ,EAAE,YAAY;aACtB,CAAC,CACF,CAAC;QACH,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { PasswordHelper, type AuthenticationUser } from \"@twin.org/api-auth-entity-storage-service\";\nimport {\n\tTenantIdContextIdHandler,\n\tTenantIdHelper,\n\ttype ITenantAdminComponent\n} from \"@twin.org/api-tenant-processor\";\nimport { ContextIdHandlerFactory, ContextIdKeys, ContextIdStore } from \"@twin.org/context\";\nimport { Coerce, ComponentFactory, Converter, I18n, Is, RandomHelper } from \"@twin.org/core\";\nimport { PasswordGenerator } from \"@twin.org/crypto\";\nimport type { IEngineCore, IEngineCoreContext } from \"@twin.org/engine-models\";\nimport {\n\tAuthenticationComponentType,\n\ttype IEngineServerConfig\n} from \"@twin.org/engine-server-types\";\nimport {\n\tEntityStorageConnectorFactory,\n\ttype IEntityStorageConnector\n} from \"@twin.org/entity-storage-models\";\nimport {\n\tDidContextIdHandler,\n\tDocumentHelper,\n\tIdentityConnectorFactory,\n\tIdentityProfileConnectorFactory,\n\tIdentityResolverConnectorFactory\n} from \"@twin.org/identity-models\";\nimport { nameofKebabCase } from \"@twin.org/nameof\";\nimport { VaultConnectorFactory, VaultKeyType } from \"@twin.org/vault-models\";\nimport type { Person, WithContext } from \"schema-dts\";\nimport {\n\tATTESTATION_VERIFICATION_METHOD_ID,\n\tAUTH_SIGNING_KEY_ID,\n\tBLOB_STORAGE_ENCRYPTION_KEY_ID,\n\tIMMUTABLE_PROOF_VERIFICATION_METHOD_ID,\n\tSYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID,\n\tVC_AUTHENTICATION_VERIFICATION_METHOD_ID\n} from \"./defaults.js\";\nimport { createIdentity } from \"./identity.js\";\nimport type { INodeEngineState } from \"./models/INodeEngineState.js\";\nimport type { INodeEnvironmentVariables } from \"./models/INodeEnvironmentVariables.js\";\nimport { NodeFeatures } from \"./models/nodeFeatures.js\";\nimport { getFeatures } from \"./utils.js\";\n\nconst DEFAULT_NODE_ADMIN_USERNAME = \"admin@node\";\n\n/**\n * Bootstrap the application.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n */\nexport async function bootstrap(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables\n): Promise<void> {\n\tconst features = getFeatures(envVars);\n\n\tawait bootstrapContextIdHandlers(engineCore, context, envVars, features);\n\n\tawait bootstrapNodeId(engineCore, context, envVars, features);\n\n\tawait ContextIdStore.run(engineCore.getContextIds() ?? {}, async () => {\n\t\tawait bootstrapTenantId(engineCore, context, envVars, features);\n\n\t\tawait bootstrapNodeAdminUser(engineCore, context, envVars, features);\n\t\tawait bootstrapAuth(engineCore, context, envVars, features);\n\t\tawait bootstrapBlobEncryption(engineCore, context, envVars, features);\n\n\t\tconst defaultAttestationConnectorType =\n\t\t\tengineCore.getRegisteredInstanceTypeOptional(\"attestationConnector\");\n\t\tif (\n\t\t\t!Is.empty(defaultAttestationConnectorType) &&\n\t\t\tIs.stringValue(context.state.nodeOrganizationId)\n\t\t) {\n\t\t\tawait addVerificationMethod(\n\t\t\t\tengineCore,\n\t\t\t\tcontext,\n\t\t\t\tcontext.state.nodeOrganizationId,\n\t\t\t\t\"attestation\",\n\t\t\t\tenvVars.attestationVerificationMethodId ?? ATTESTATION_VERIFICATION_METHOD_ID\n\t\t\t);\n\t\t}\n\n\t\tconst defaultImmutableProofComponentType =\n\t\t\tengineCore.getRegisteredInstanceTypeOptional(\"immutableProofComponent\");\n\n\t\tif (\n\t\t\t!Is.empty(defaultImmutableProofComponentType) &&\n\t\t\tIs.stringValue(context.state.nodeOrganizationId)\n\t\t) {\n\t\t\tawait addVerificationMethod(\n\t\t\t\tengineCore,\n\t\t\t\tcontext,\n\t\t\t\tcontext.state.nodeOrganizationId,\n\t\t\t\t\"immutable proof\",\n\t\t\t\tenvVars.immutableProofVerificationMethodId ?? IMMUTABLE_PROOF_VERIFICATION_METHOD_ID\n\t\t\t);\n\t\t}\n\n\t\tif (\n\t\t\t(Coerce.boolean(envVars.vcAuthenticationEnabled) ?? false) &&\n\t\t\tIs.stringValue(context.state.nodeId)\n\t\t) {\n\t\t\tawait addVerificationMethod(\n\t\t\t\tengineCore,\n\t\t\t\tcontext,\n\t\t\t\tcontext.state.nodeId,\n\t\t\t\t\"verifiable credential authentication\",\n\t\t\t\tenvVars.vcAuthenticationVerificationMethodId ?? VC_AUTHENTICATION_VERIFICATION_METHOD_ID\n\t\t\t);\n\t\t}\n\n\t\tawait bootstrapSynchronisedStorage(engineCore, context, envVars, features);\n\t});\n}\n\n/**\n * Bootstrap the context id handlers creating any necessary resources.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node. The features that are enabled on the node.\n */\nexport async function bootstrapContextIdHandlers(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {\n\tContextIdHandlerFactory.register(ContextIdKeys.Node, () => new DidContextIdHandler());\n\tContextIdHandlerFactory.register(ContextIdKeys.Tenant, () => new TenantIdContextIdHandler());\n\tContextIdHandlerFactory.register(ContextIdKeys.Organization, () => new DidContextIdHandler());\n\tContextIdHandlerFactory.register(ContextIdKeys.User, () => new DidContextIdHandler());\n}\n\n/**\n * Bootstrap the node creating any necessary resources.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node. The features that are enabled on the node.\n */\nexport async function bootstrapNodeId(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {\n\tif (features.includes(NodeFeatures.NodeId)) {\n\t\tconst existingNodeId = envVars.nodeIdentity ?? context.state.nodeId;\n\n\t\tcontext.state.nodeId = await createIdentity(\n\t\t\tengineCore,\n\t\t\tenvVars,\n\t\t\texistingNodeId,\n\t\t\tenvVars.nodeMnemonic,\n\t\t\texistingNodeId,\n\t\t\t\"node\",\n\t\t\tfeatures.includes(NodeFeatures.NodeWallet)\n\t\t);\n\t\tcontext.stateDirty = true;\n\n\t\tengineCore.logInfo(\n\t\t\tI18n.formatMessage(\"node.nodeId\", {\n\t\t\t\tidentity: context.state.nodeId\n\t\t\t})\n\t\t);\n\n\t\tengineCore.addContextId(ContextIdKeys.Node, context.state.nodeId);\n\t}\n}\n\n/**\n * Bootstrap the node creating any necessary resources.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node. The features that are enabled on the node.\n */\nexport async function bootstrapTenantId(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {\n\t// If tenants are enabled we need to add a context id for the node\n\t// so that services such a logging have a default tenant context id\n\t// this will get overwritten by any incoming API requests with the tenant context id\n\tif (Coerce.boolean(envVars.tenantEnabled) ?? false) {\n\t\tlet tenantId = envVars.tenantId ?? context.state.nodeTenantId;\n\n\t\tif (!Is.stringValue(tenantId)) {\n\t\t\tconst tenantAdminServiceComponentType =\n\t\t\t\tengineCore.getRegisteredInstanceType(\"tenantAdminComponent\");\n\n\t\t\tconst tenantAdminService = ComponentFactory.get<ITenantAdminComponent>(\n\t\t\t\ttenantAdminServiceComponentType\n\t\t\t);\n\n\t\t\ttenantId = TenantIdHelper.generateTenantId();\n\t\t\tconst apiKey = envVars.tenantApiKey ?? TenantIdHelper.generateApiKey();\n\n\t\t\tawait tenantAdminService.set({\n\t\t\t\tid: tenantId,\n\t\t\t\tapiKey,\n\t\t\t\tdateCreated: new Date(Date.now()).toISOString(),\n\t\t\t\tlabel: \"node-tenant\"\n\t\t\t});\n\n\t\t\tengineCore.logInfo(\n\t\t\t\tI18n.formatMessage(\"node.createdTenantId\", {\n\t\t\t\t\tidentity: tenantId,\n\t\t\t\t\tapiKey\n\t\t\t\t})\n\t\t\t);\n\t\t} else {\n\t\t\tengineCore.logInfo(\n\t\t\t\tI18n.formatMessage(\"node.existingTenantId\", {\n\t\t\t\t\tidentity: context.state.nodeTenantId\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\n\t\tcontext.state.nodeTenantId = tenantId;\n\t\tcontext.stateDirty = true;\n\n\t\tengineCore.addContextId(ContextIdKeys.Tenant, context.state.nodeTenantId);\n\t}\n}\n\n/**\n * Bootstrap the user.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node.\n */\nexport async function bootstrapNodeAdminUser(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {\n\tif (features.includes(NodeFeatures.NodeAdminUser)) {\n\t\tcontext.state.nodeOrganizationId =\n\t\t\tenvVars.organizationIdentity ?? context.state.nodeOrganizationId;\n\t\tcontext.state.nodeAdminUserId = envVars.adminUserIdentity ?? context.state.nodeAdminUserId;\n\n\t\tconst defaultAuthenticationComponentType =\n\t\t\tengineCore.getRegisteredInstanceType(\"authenticationComponent\");\n\t\tif (\n\t\t\tdefaultAuthenticationComponentType.startsWith(AuthenticationComponentType.EntityStorage) &&\n\t\t\tIs.stringValue(context.state.nodeId)\n\t\t) {\n\t\t\tconst authUserEntityStorage =\n\t\t\t\tEntityStorageConnectorFactory.get<IEntityStorageConnector<AuthenticationUser>>(\n\t\t\t\t\tnameofKebabCase<AuthenticationUser>()\n\t\t\t\t);\n\n\t\t\t// If we don't have an organization identity, create one\n\t\t\tif (!Is.stringValue(context.state.nodeOrganizationId)) {\n\t\t\t\tcontext.state.nodeOrganizationId = await createIdentity(\n\t\t\t\t\tengineCore,\n\t\t\t\t\tenvVars,\n\t\t\t\t\tcontext.state.nodeOrganizationId,\n\t\t\t\t\tenvVars.organizationMnemonic,\n\t\t\t\t\tcontext.state.nodeId,\n\t\t\t\t\t\"organization\",\n\t\t\t\t\tfeatures.includes(NodeFeatures.NodeWallet)\n\t\t\t\t);\n\t\t\t\tcontext.stateDirty = true;\n\t\t\t}\n\n\t\t\tif (!Is.stringValue(context.state.nodeAdminUserId)) {\n\t\t\t\tcontext.state.nodeAdminUserId = await createIdentity(\n\t\t\t\t\tengineCore,\n\t\t\t\t\tenvVars,\n\t\t\t\t\tcontext.state.nodeAdminUserId,\n\t\t\t\t\tenvVars.adminUserMnemonic,\n\t\t\t\t\tcontext.state.nodeOrganizationId,\n\t\t\t\t\t\"user\",\n\t\t\t\t\tfalse\n\t\t\t\t);\n\t\t\t\tcontext.stateDirty = true;\n\t\t\t}\n\n\t\t\tconst adminEmail = envVars.adminUserName ?? DEFAULT_NODE_ADMIN_USERNAME;\n\n\t\t\tlet nodeAdminUser = await authUserEntityStorage.get(adminEmail);\n\n\t\t\t// If the node admin user doesn't exist, create it\n\t\t\tif (Is.empty(nodeAdminUser)) {\n\t\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.creatingUser\", { email: adminEmail }));\n\n\t\t\t\tconst generatedPassword = envVars.adminUserPassword ?? PasswordGenerator.generate(16);\n\t\t\t\tconst passwordBytes = Converter.utf8ToBytes(generatedPassword);\n\t\t\t\tconst saltBytes = RandomHelper.generate(16);\n\t\t\t\tconst hashedPassword = await PasswordHelper.hashPassword(passwordBytes, saltBytes);\n\n\t\t\t\tnodeAdminUser = {\n\t\t\t\t\temail: adminEmail,\n\t\t\t\t\tpassword: hashedPassword,\n\t\t\t\t\tsalt: Converter.bytesToBase64(saltBytes),\n\t\t\t\t\tidentity: context.state.nodeAdminUserId,\n\t\t\t\t\torganization: context.state.nodeOrganizationId\n\t\t\t\t};\n\n\t\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.nodeAdminUserEmail\", { email: adminEmail }));\n\t\t\t\tengineCore.logInfo(\n\t\t\t\t\tI18n.formatMessage(\"node.nodeAdminUserPassword\", { password: generatedPassword })\n\t\t\t\t);\n\n\t\t\t\tawait authUserEntityStorage.set(nodeAdminUser);\n\t\t\t} else {\n\t\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.existingUser\", { email: adminEmail }));\n\n\t\t\t\t// The user already exists, so double check the other details match\n\t\t\t\tlet needsUpdate = false;\n\n\t\t\t\tif (nodeAdminUser.identity !== context.state.nodeAdminUserId) {\n\t\t\t\t\tnodeAdminUser.identity = context.state.nodeAdminUserId;\n\t\t\t\t\tneedsUpdate = true;\n\t\t\t\t}\n\n\t\t\t\tif (Is.stringValue(envVars.adminUserPassword)) {\n\t\t\t\t\tconst passwordBytes = Converter.utf8ToBytes(envVars.adminUserPassword);\n\t\t\t\t\tconst saltBytes = Converter.base64ToBytes(nodeAdminUser.salt);\n\t\t\t\t\tconst hashedPassword = await PasswordHelper.hashPassword(passwordBytes, saltBytes);\n\n\t\t\t\t\tif (nodeAdminUser.password !== hashedPassword) {\n\t\t\t\t\t\tnodeAdminUser.password = hashedPassword;\n\t\t\t\t\t\tneedsUpdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (needsUpdate) {\n\t\t\t\t\tawait authUserEntityStorage.set(nodeAdminUser);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// We have create a node user, now we need to create a profile for the user\n\t\t\tconst defaultIdentityProfileConnectorType = engineCore.getRegisteredInstanceType(\n\t\t\t\t\"identityProfileConnector\"\n\t\t\t);\n\t\t\tconst identityProfileConnector = IdentityProfileConnectorFactory.get(\n\t\t\t\tdefaultIdentityProfileConnectorType\n\t\t\t);\n\n\t\t\tif (identityProfileConnector) {\n\t\t\t\t// Add the organization context id when creating the profile\n\t\t\t\t// so that it is partitioned under the organization\n\t\t\t\tconst contextIds = (await ContextIdStore.getContextIds()) ?? {};\n\t\t\t\tcontextIds[ContextIdKeys.Organization] = context.state.nodeOrganizationId;\n\t\t\t\tawait ContextIdStore.run(contextIds, async () => {\n\t\t\t\t\tlet userProfile;\n\t\t\t\t\tif (Is.stringValue(nodeAdminUser.identity)) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tuserProfile = await identityProfileConnector.get(nodeAdminUser.identity);\n\t\t\t\t\t\t} catch {}\n\t\t\t\t\t}\n\t\t\t\t\tif (Is.empty(userProfile)) {\n\t\t\t\t\t\tengineCore.logInfo(\n\t\t\t\t\t\t\tI18n.formatMessage(\"node.creatingUserProfile\", {\n\t\t\t\t\t\t\t\tidentity: nodeAdminUser.identity\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tconst publicProfile: WithContext<Person> = {\n\t\t\t\t\t\t\t\"@context\": \"https://schema.org\",\n\t\t\t\t\t\t\t\"@type\": \"Person\",\n\t\t\t\t\t\t\tname: \"Node Administrator\"\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst privateProfile: WithContext<Person> = {\n\t\t\t\t\t\t\t\"@context\": \"https://schema.org\",\n\t\t\t\t\t\t\t\"@type\": \"Person\",\n\t\t\t\t\t\t\tgivenName: \"Node\",\n\t\t\t\t\t\t\tfamilyName: \"Administrator\",\n\t\t\t\t\t\t\temail: adminEmail\n\t\t\t\t\t\t};\n\t\t\t\t\t\tawait identityProfileConnector.create(\n\t\t\t\t\t\t\tnodeAdminUser.identity,\n\t\t\t\t\t\t\tpublicProfile,\n\t\t\t\t\t\t\tprivateProfile\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tengineCore.logInfo(\n\t\t\t\t\t\t\tI18n.formatMessage(\"node.existingUserProfile\", {\n\t\t\t\t\t\t\t\tidentity: nodeAdminUser.identity\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Bootstrap the immutable proof verification methods.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node.\n */\nexport async function bootstrapImmutableProofMethod(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {}\n\n/**\n * Bootstrap the keys for blob encryption.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node.\n */\nexport async function bootstrapBlobEncryption(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {\n\tif (\n\t\t(Coerce.boolean(envVars.blobStorageEnableEncryption) ?? false) &&\n\t\tIs.stringValue(context.state.nodeOrganizationId)\n\t) {\n\t\t// Create a new key for encrypting blobs\n\t\tconst defaultVaultConnectorType = engineCore.getRegisteredInstanceType(\"vaultConnector\");\n\t\tconst vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);\n\n\t\tconst keyName = `${context.state.nodeOrganizationId}/${envVars.blobStorageEncryptionKeyId ?? BLOB_STORAGE_ENCRYPTION_KEY_ID}`;\n\n\t\tlet existingKey;\n\n\t\ttry {\n\t\t\texistingKey = await vaultConnector.getKey(keyName);\n\t\t} catch {}\n\n\t\tif (Is.empty(existingKey)) {\n\t\t\tif (Is.stringBase64(envVars.blobStorageSymmetricEncryptionKey)) {\n\t\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.addingBlobEncryptionKey\", { keyName }));\n\t\t\t\tawait vaultConnector.addKey(\n\t\t\t\t\tkeyName,\n\t\t\t\t\tVaultKeyType.ChaCha20Poly1305,\n\t\t\t\t\tConverter.base64ToBytes(envVars.blobStorageSymmetricEncryptionKey)\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.creatingBlobEncryptionKey\", { keyName }));\n\t\t\t\tconst key = await vaultConnector.createKey(keyName, VaultKeyType.ChaCha20Poly1305);\n\t\t\t\tengineCore.logInfo(\n\t\t\t\t\tI18n.formatMessage(\"node.createdBlobEncryptionKey\", {\n\t\t\t\t\t\tkeyName,\n\t\t\t\t\t\tkeyValue: Converter.bytesToBase64(key)\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\t}\n\t\t} else {\n\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.existingBlobEncryptionKey\", { keyName }));\n\t\t}\n\t}\n}\n\n/**\n * Bootstrap the JWT signing key.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node.\n */\nexport async function bootstrapAuth(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {\n\tconst defaultAuthenticationComponentType =\n\t\tengineCore.getRegisteredInstanceTypeOptional(\"authenticationComponent\");\n\tif (\n\t\tIs.stringValue(defaultAuthenticationComponentType) &&\n\t\tdefaultAuthenticationComponentType.startsWith(AuthenticationComponentType.EntityStorage) &&\n\t\tIs.stringValue(context.state.nodeId)\n\t) {\n\t\t// Create a new JWT signing key and a user login for the node\n\t\tconst defaultVaultConnectorType = engineCore.getRegisteredInstanceType(\"vaultConnector\");\n\t\tconst vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);\n\n\t\tconst keyName = `${context.state.nodeId}/${envVars.authSigningKeyId ?? AUTH_SIGNING_KEY_ID}`;\n\n\t\tlet existingKey;\n\t\ttry {\n\t\t\texistingKey = await vaultConnector.getKey(keyName);\n\t\t} catch {}\n\n\t\tif (Is.empty(existingKey)) {\n\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.creatingAuthKey\", { keyName }));\n\t\t\tawait vaultConnector.createKey(keyName, VaultKeyType.Ed25519);\n\t\t} else {\n\t\t\tengineCore.logInfo(I18n.formatMessage(\"node.existingAuthKey\", { keyName }));\n\t\t}\n\t}\n}\n\n/**\n * Bootstrap the synchronised storage blob encryption and verification methods.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param envVars The environment variables for the node.\n * @param features The features that are enabled on the node.\n */\nexport async function bootstrapSynchronisedStorage(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\tfeatures: NodeFeatures[]\n): Promise<void> {\n\tif (Coerce.boolean(envVars.synchronisedStorageEnabled) ?? false) {\n\t\t// If this is a trusted node we need to add the blob encryption key pair\n\t\tif (Is.stringBase64(envVars.synchronisedStorageBlobStorageKey)) {\n\t\t\tconst defaultVaultConnectorType = engineCore.getRegisteredInstanceType(\"vaultConnector\");\n\t\t\tconst vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);\n\n\t\t\tconst keyName =\n\t\t\t\tenvVars.synchronisedStorageBlobStorageEncryptionKeyId ??\n\t\t\t\tSYNCHRONISED_STORAGE_BLOB_STORAGE_ENCRYPTION_KEY_ID;\n\t\t\tlet existingKey;\n\n\t\t\ttry {\n\t\t\t\texistingKey = await vaultConnector.getKey(keyName);\n\t\t\t} catch {}\n\n\t\t\tif (Is.empty(existingKey)) {\n\t\t\t\tengineCore.logInfo(\n\t\t\t\t\tI18n.formatMessage(\"node.addingSynchronisedStorageBlobEncryptionKey\", { keyName })\n\t\t\t\t);\n\t\t\t\tawait vaultConnector.addKey(\n\t\t\t\t\tkeyName,\n\t\t\t\t\tVaultKeyType.ChaCha20Poly1305,\n\t\t\t\t\tConverter.base64ToBytes(envVars.synchronisedStorageBlobStorageKey)\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tengineCore.logInfo(\n\t\t\t\t\tI18n.formatMessage(\"node.existingSynchronisedStorageBlobEncryptionKey\", { keyName })\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Add a verification method if it doesn't exist.\n * @param engineCore The engine core for the node.\n * @param context The context for the node.\n * @param identity The identity to add the verification method to.\n * @param verificationMethodTitle The verification method title.\n * @param verificationMethodId The verification method ID.\n */\nasync function addVerificationMethod(\n\tengineCore: IEngineCore,\n\tcontext: IEngineCoreContext<IEngineServerConfig, INodeEngineState>,\n\tidentity: string,\n\tverificationMethodTitle: string,\n\tverificationMethodId: string | undefined\n): Promise<void> {\n\tif (\n\t\tIs.stringValue(identity) &&\n\t\tIs.arrayValue(context.config.types.identityConnector) &&\n\t\tIs.stringValue(verificationMethodId)\n\t) {\n\t\tconst defaultIdentityConnectorType = engineCore.getRegisteredInstanceType(\"identityConnector\");\n\t\tconst identityConnector = IdentityConnectorFactory.get(defaultIdentityConnectorType);\n\n\t\tconst defaultIdentityResolverConnectorType = engineCore.getRegisteredInstanceType(\n\t\t\t\"identityResolverConnector\"\n\t\t);\n\t\tconst identityResolverConnector = IdentityResolverConnectorFactory.get(\n\t\t\tdefaultIdentityResolverConnectorType\n\t\t);\n\n\t\tconst identityDocument = await identityResolverConnector.resolveDocument(identity);\n\n\t\tconst fullMethodId = `${identityDocument.id}#${verificationMethodId}`;\n\n\t\tlet exists = false;\n\t\ttry {\n\t\t\tDocumentHelper.getVerificationMethod(identityDocument, fullMethodId, \"assertionMethod\");\n\t\t\texists = true;\n\t\t} catch {}\n\n\t\tif (!exists) {\n\t\t\tengineCore.logInfo(\n\t\t\t\tI18n.formatMessage(\"node.addingVerificationMethod\", {\n\t\t\t\t\ttitle: verificationMethodTitle,\n\t\t\t\t\tmethodId: fullMethodId\n\t\t\t\t})\n\t\t\t);\n\t\t\tawait identityConnector.addVerificationMethod(\n\t\t\t\tidentity,\n\t\t\t\tidentity,\n\t\t\t\t\"assertionMethod\",\n\t\t\t\tverificationMethodId\n\t\t\t);\n\t\t} else {\n\t\t\tengineCore.logInfo(\n\t\t\t\tI18n.formatMessage(\"node.existingVerificationMethod\", {\n\t\t\t\t\ttitle: verificationMethodTitle,\n\t\t\t\t\tmethodId: fullMethodId\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n}\n"]}
@@ -1,169 +0,0 @@
1
- // Copyright 2024 IOTA Stiftung.
2
- // SPDX-License-Identifier: Apache-2.0.
3
- import { Converter, I18n, Is, RandomHelper, Urn } from "@twin.org/core";
4
- import { Bip39 } from "@twin.org/crypto";
5
- import { IdentityConnectorType, WalletConnectorType } from "@twin.org/engine-types";
6
- import { EntityStorageConnectorFactory } from "@twin.org/entity-storage-models";
7
- import { IdentityConnectorFactory, IdentityResolverConnectorFactory } from "@twin.org/identity-models";
8
- import { VaultConnectorFactory } from "@twin.org/vault-models";
9
- import { WalletConnectorFactory } from "@twin.org/wallet-models";
10
- /**
11
- * Generate an identity and fund it.
12
- * @param engineCore The engine core for the node.
13
- * @param envVars The environment variables for the node.
14
- * @param configIdentity A identity from config to use.
15
- * @param configMnemonic A mnemonic from config to use.
16
- * @param controller The controller for the identity.
17
- * @param identityType The type of identity.
18
- * @param addWallet Whether to add a wallet for the identity.
19
- * @returns The identity that was generated.
20
- */
21
- export async function createIdentity(engineCore, envVars, configIdentity, configMnemonic, controller, identityType, addWallet) {
22
- engineCore.logInfo(I18n.formatMessage("node.processingIdentity", { identityType }));
23
- // We have a chicken and egg problem in that we can't create the identity
24
- // to store the mnemonic in the vault without an identity. We use a temporary identity
25
- // and then replace it with the new identity later in the process.
26
- const defaultVaultConnectorType = engineCore.getRegisteredInstanceType("vaultConnector");
27
- const vaultConnector = VaultConnectorFactory.get(defaultVaultConnectorType);
28
- const workingIdentity = configIdentity ?? `bootstrap-temp-${Converter.bytesToHex(RandomHelper.generate(16))}`;
29
- const workingController = controller ?? workingIdentity;
30
- await bootstrapMnemonic(engineCore, vaultConnector, workingIdentity, configMnemonic);
31
- const addresses = addWallet ? await generateWallet(engineCore, envVars, workingIdentity) : [];
32
- const finalIdentity = await generateIdentity(engineCore, envVars, workingController, workingIdentity, identityType);
33
- await finaliseWallet(engineCore, envVars, finalIdentity, addresses);
34
- await finaliseMnemonic(vaultConnector, workingIdentity, finalIdentity);
35
- return finalIdentity;
36
- }
37
- /**
38
- * Generate a mnemonic for the node identity.
39
- * @param engineCore The engine core for the node.
40
- * @param vaultConnector The vault connector to use.
41
- * @param identity The identity of the node.
42
- * @param existingMnemonic An existing mnemonic to use.
43
- */
44
- async function bootstrapMnemonic(engineCore, vaultConnector, identity, existingMnemonic) {
45
- let mnemonic = existingMnemonic;
46
- let storeMnemonic = false;
47
- try {
48
- const storedMnemonic = await vaultConnector.getSecret(`${identity}/mnemonic`);
49
- storeMnemonic = storedMnemonic !== mnemonic;
50
- mnemonic = storedMnemonic;
51
- }
52
- catch {
53
- storeMnemonic = true;
54
- }
55
- // If there is no mnemonic then we need to generate one
56
- if (Is.empty(mnemonic)) {
57
- mnemonic = Bip39.randomMnemonic();
58
- storeMnemonic = true;
59
- engineCore.logInfo(I18n.formatMessage("node.generatingMnemonic", { mnemonic }));
60
- }
61
- // If there is no mnemonic stored in the vault then we need to store it
62
- if (storeMnemonic) {
63
- engineCore.logInfo(I18n.formatMessage("node.storingMnemonic"));
64
- await vaultConnector.setSecret(`${identity}/mnemonic`, mnemonic);
65
- }
66
- else {
67
- engineCore.logInfo(I18n.formatMessage("node.existingMnemonic"));
68
- }
69
- }
70
- /**
71
- * Finalise the mnemonic for the node identity.
72
- * @param vaultConnector The vault connector to use.
73
- * @param workingIdentity The identity of the node.
74
- * @param finalIdentity The final identity for the node.
75
- */
76
- async function finaliseMnemonic(vaultConnector, workingIdentity, finalIdentity) {
77
- // Now that we have an identity we can remove the temporary one
78
- // and store the mnemonic with the new identity
79
- if (workingIdentity.startsWith("bootstrap-temp-") && workingIdentity !== finalIdentity) {
80
- const mnemonic = await vaultConnector.getSecret(`${workingIdentity}/mnemonic`);
81
- await vaultConnector.setSecret(`${finalIdentity}/mnemonic`, mnemonic);
82
- await vaultConnector.removeSecret(`${workingIdentity}/mnemonic`);
83
- }
84
- }
85
- /**
86
- * Bootstrap the identity for the node.
87
- * @param engineCore The engine core for the node.
88
- * @param envVars The environment variables for the node.
89
- * @param finalIdentity The identity of the node.
90
- * @param addresses The addresses for the wallet.
91
- */
92
- async function finaliseWallet(engineCore, envVars, finalIdentity, addresses) {
93
- if (Is.arrayValue(addresses)) {
94
- const defaultWalletConnectorType = engineCore.getRegisteredInstanceType("walletConnector");
95
- // If we are using entity storage for wallet the identity associated with the
96
- // address will be wrong, so fix it
97
- if (defaultWalletConnectorType.startsWith(WalletConnectorType.EntityStorage)) {
98
- const walletAddress = EntityStorageConnectorFactory.get("wallet-address");
99
- const addr = await walletAddress.get(addresses[0]);
100
- if (!Is.empty(addr)) {
101
- addr.identity = finalIdentity;
102
- await walletAddress.set(addr);
103
- }
104
- }
105
- }
106
- }
107
- /**
108
- * Bootstrap the wallet for the node.
109
- * @param engineCore The engine core for the node.
110
- * @param envVars The environment variables for the node.
111
- * @param identity The identity to create the wallet for.
112
- * @returns The addresses for the wallet.
113
- */
114
- async function generateWallet(engineCore, envVars, identity) {
115
- const defaultWalletConnectorType = engineCore.getRegisteredInstanceType("walletConnector");
116
- const walletConnector = WalletConnectorFactory.get(defaultWalletConnectorType);
117
- const addresses = await walletConnector.getAddresses(identity, 0, 0, 5);
118
- const balance = await walletConnector.getBalance(identity, addresses[0]);
119
- if (balance === 0n) {
120
- let address0 = addresses[0];
121
- if (defaultWalletConnectorType.startsWith(WalletConnectorType.Iota)) {
122
- address0 = `${envVars.iotaExplorerEndpoint}address/${address0}?network=${envVars.iotaNetwork}`;
123
- }
124
- engineCore.logInfo(I18n.formatMessage("node.fundingWallet", { address: address0 }));
125
- // Add some funds to the wallet from the faucet
126
- await walletConnector.ensureBalance(identity, addresses[0], 1000000000n);
127
- }
128
- else {
129
- engineCore.logInfo(I18n.formatMessage("node.fundedWallet"));
130
- }
131
- return addresses;
132
- }
133
- /**
134
- * Bootstrap the identity for the node.
135
- * @param engineCore The engine core for the node.
136
- * @param envVars The environment variables for the node.
137
- * @param controller The controller for the identity.
138
- * @param identity The existing identity if there is one.
139
- * @param identityType The type of identity.
140
- * @returns The addresses for the wallet.
141
- */
142
- async function generateIdentity(engineCore, envVars, controller, identity, identityType) {
143
- const defaultIdentityConnectorType = engineCore.getRegisteredInstanceType("identityConnector");
144
- // Now create an identity for the node controlled by the address we just funded
145
- const identityConnector = IdentityConnectorFactory.get(defaultIdentityConnectorType);
146
- let identityDocument;
147
- try {
148
- const defaultIdentityResolverConnectorType = engineCore.getRegisteredInstanceType("identityResolverConnector");
149
- const identityResolverConnector = IdentityResolverConnectorFactory.get(defaultIdentityResolverConnectorType);
150
- identityDocument = await identityResolverConnector.resolveDocument(identity);
151
- engineCore.logInfo(I18n.formatMessage("node.existingIdentity", { identity }));
152
- }
153
- catch { }
154
- if (Is.empty(identityDocument)) {
155
- engineCore.logInfo(I18n.formatMessage("node.generatingIdentity", { identityType }));
156
- identityDocument = await identityConnector.createDocument(controller);
157
- engineCore.logInfo(I18n.formatMessage("node.createdIdentity", { identity: identityDocument.id }));
158
- }
159
- if (defaultIdentityConnectorType.startsWith(IdentityConnectorType.Iota)) {
160
- const didUrn = Urn.fromValidString(identityDocument.id);
161
- const didParts = didUrn.parts();
162
- const objectId = didParts[3];
163
- engineCore.logInfo(I18n.formatMessage("node.identityExplorer", {
164
- url: `${envVars.iotaExplorerEndpoint}object/${objectId}?network=${envVars.iotaNetwork}`
165
- }));
166
- }
167
- return identityDocument.id;
168
- }
169
- //# sourceMappingURL=identity.js.map