@twin.org/identity-cli 0.0.1-next.10

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 (51) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +21 -0
  3. package/bin/index.js +10 -0
  4. package/dist/cjs/index.cjs +1122 -0
  5. package/dist/esm/index.mjs +1095 -0
  6. package/dist/locales/en.json +780 -0
  7. package/dist/types/cli.d.ts +17 -0
  8. package/dist/types/commands/identityCreate.d.ts +19 -0
  9. package/dist/types/commands/identityResolve.d.ts +19 -0
  10. package/dist/types/commands/proofCreate.d.ts +21 -0
  11. package/dist/types/commands/proofVerify.d.ts +23 -0
  12. package/dist/types/commands/serviceAdd.d.ts +27 -0
  13. package/dist/types/commands/serviceRemove.d.ts +20 -0
  14. package/dist/types/commands/setupCommands.d.ts +4 -0
  15. package/dist/types/commands/verifiableCredentialCreate.d.ts +25 -0
  16. package/dist/types/commands/verifiableCredentialRevoke.d.ts +20 -0
  17. package/dist/types/commands/verifiableCredentialUnrevoke.d.ts +20 -0
  18. package/dist/types/commands/verifiableCredentialVerify.d.ts +17 -0
  19. package/dist/types/commands/verificationMethodAdd.d.ts +26 -0
  20. package/dist/types/commands/verificationMethodRemove.d.ts +20 -0
  21. package/dist/types/index.d.ts +13 -0
  22. package/docs/changelog.md +5 -0
  23. package/docs/examples.md +211 -0
  24. package/docs/reference/classes/CLI.md +53 -0
  25. package/docs/reference/functions/actionCommandIdentityCreate.md +15 -0
  26. package/docs/reference/functions/actionCommandIdentityResolve.md +15 -0
  27. package/docs/reference/functions/actionCommandProofCreate.md +15 -0
  28. package/docs/reference/functions/actionCommandProofVerify.md +15 -0
  29. package/docs/reference/functions/actionCommandServiceAdd.md +15 -0
  30. package/docs/reference/functions/actionCommandServiceRemove.md +31 -0
  31. package/docs/reference/functions/actionCommandVerifiableCredentialCreate.md +15 -0
  32. package/docs/reference/functions/actionCommandVerifiableCredentialRevoke.md +31 -0
  33. package/docs/reference/functions/actionCommandVerifiableCredentialUnrevoke.md +31 -0
  34. package/docs/reference/functions/actionCommandVerifiableCredentialVerify.md +15 -0
  35. package/docs/reference/functions/actionCommandVerificationMethodAdd.md +15 -0
  36. package/docs/reference/functions/actionCommandVerificationMethodRemove.md +31 -0
  37. package/docs/reference/functions/buildCommandIdentityCreate.md +11 -0
  38. package/docs/reference/functions/buildCommandIdentityResolve.md +11 -0
  39. package/docs/reference/functions/buildCommandProofCreate.md +11 -0
  40. package/docs/reference/functions/buildCommandProofVerify.md +11 -0
  41. package/docs/reference/functions/buildCommandServiceAdd.md +11 -0
  42. package/docs/reference/functions/buildCommandServiceRemove.md +11 -0
  43. package/docs/reference/functions/buildCommandVerifiableCredentialCreate.md +11 -0
  44. package/docs/reference/functions/buildCommandVerifiableCredentialRevoke.md +11 -0
  45. package/docs/reference/functions/buildCommandVerifiableCredentialUnrevoke.md +11 -0
  46. package/docs/reference/functions/buildCommandVerifiableCredentialVerify.md +11 -0
  47. package/docs/reference/functions/buildCommandVerificationMethodAdd.md +11 -0
  48. package/docs/reference/functions/buildCommandVerificationMethodRemove.md +11 -0
  49. package/docs/reference/index.md +32 -0
  50. package/locales/en.json +326 -0
  51. package/package.json +58 -0
@@ -0,0 +1,1095 @@
1
+ import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { CLIOptions, CLIParam, CLIDisplay, CLIUtils, CLIBase } from '@twin.org/cli-core';
4
+ import { buildCommandMnemonic, buildCommandAddress } from '@twin.org/crypto-cli';
5
+ import { buildCommandFaucet, buildCommandTransfer } from '@twin.org/wallet-cli';
6
+ import { I18n, Converter, Is, StringHelper, Coerce, GeneralError } from '@twin.org/core';
7
+ import { IotaIdentityConnector, IotaIdentityUtils } from '@twin.org/identity-connector-iota';
8
+ import { VaultConnectorFactory, VaultKeyType } from '@twin.org/vault-models';
9
+ import { IotaWalletConnector } from '@twin.org/wallet-connector-iota';
10
+ import { WalletConnectorFactory } from '@twin.org/wallet-models';
11
+ import { Command, Option } from 'commander';
12
+ import { MemoryEntityStorageConnector } from '@twin.org/entity-storage-connector-memory';
13
+ import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
14
+ import { initSchema, EntityStorageVaultConnector } from '@twin.org/vault-connector-entity-storage';
15
+ import { DocumentHelper } from '@twin.org/identity-models';
16
+ import { DidVerificationMethodType } from '@twin.org/standards-w3c-did';
17
+
18
+ // Copyright 2024 IOTA Stiftung.
19
+ // SPDX-License-Identifier: Apache-2.0.
20
+ /**
21
+ * Setup the vault for use in the CLI commands.
22
+ */
23
+ function setupVault() {
24
+ initSchema();
25
+ EntityStorageConnectorFactory.register("vault-key", () => new MemoryEntityStorageConnector({
26
+ entitySchema: "VaultKey"
27
+ }));
28
+ EntityStorageConnectorFactory.register("vault-secret", () => new MemoryEntityStorageConnector({
29
+ entitySchema: "VaultSecret"
30
+ }));
31
+ const vaultConnector = new EntityStorageVaultConnector();
32
+ VaultConnectorFactory.register("vault", () => vaultConnector);
33
+ }
34
+
35
+ // Copyright 2024 IOTA Stiftung.
36
+ // SPDX-License-Identifier: Apache-2.0.
37
+ /**
38
+ * Build the identity create command for the CLI.
39
+ * @returns The command.
40
+ */
41
+ function buildCommandIdentityCreate() {
42
+ const command = new Command();
43
+ command
44
+ .name("identity-create")
45
+ .summary(I18n.formatMessage("commands.identity-create.summary"))
46
+ .description(I18n.formatMessage("commands.identity-create.description"))
47
+ .requiredOption(I18n.formatMessage("commands.identity-create.options.seed.param"), I18n.formatMessage("commands.identity-create.options.seed.description"));
48
+ CLIOptions.output(command, {
49
+ noConsole: true,
50
+ json: true,
51
+ env: true,
52
+ mergeJson: true,
53
+ mergeEnv: true
54
+ });
55
+ command
56
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
57
+ .option(I18n.formatMessage("commands.common.options.explorer.param"), I18n.formatMessage("commands.common.options.explorer.description"), "!EXPLORER_URL")
58
+ .action(actionCommandIdentityCreate);
59
+ return command;
60
+ }
61
+ /**
62
+ * Action the identity create command.
63
+ * @param opts The options for the command.
64
+ * @param opts.seed The private key for the controller.
65
+ * @param opts.node The node URL.
66
+ * @param opts.explorer The explorer URL.
67
+ */
68
+ async function actionCommandIdentityCreate(opts) {
69
+ const seed = CLIParam.hexBase64("seed", opts.seed);
70
+ const nodeEndpoint = CLIParam.url("node", opts.node);
71
+ const explorerEndpoint = CLIParam.url("explorer", opts.explorer);
72
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
73
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explorer"), explorerEndpoint);
74
+ CLIDisplay.break();
75
+ setupVault();
76
+ const vaultSeedId = "local-seed";
77
+ const localIdentity = "local";
78
+ const vaultConnector = VaultConnectorFactory.get("vault");
79
+ await vaultConnector.setSecret(`${localIdentity}/${vaultSeedId}`, Converter.bytesToBase64(seed));
80
+ const iotaWalletConnector = new IotaWalletConnector({
81
+ config: {
82
+ clientOptions: {
83
+ nodes: [nodeEndpoint],
84
+ localPow: true
85
+ },
86
+ vaultSeedId
87
+ }
88
+ });
89
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
90
+ const iotaIdentityConnector = new IotaIdentityConnector({
91
+ config: {
92
+ clientOptions: {
93
+ nodes: [nodeEndpoint],
94
+ localPow: true
95
+ },
96
+ vaultSeedId
97
+ }
98
+ });
99
+ CLIDisplay.task(I18n.formatMessage("commands.identity-create.progress.creatingIdentity"));
100
+ CLIDisplay.break();
101
+ CLIDisplay.spinnerStart();
102
+ const document = await iotaIdentityConnector.createDocument(localIdentity);
103
+ CLIDisplay.spinnerStop();
104
+ if (opts.console) {
105
+ CLIDisplay.value(I18n.formatMessage("commands.identity-create.labels.identity"), document.id);
106
+ CLIDisplay.break();
107
+ }
108
+ if (Is.stringValue(opts?.json)) {
109
+ await CLIUtils.writeJsonFile(opts.json, { did: document.id }, opts.mergeJson);
110
+ }
111
+ if (Is.stringValue(opts?.env)) {
112
+ await CLIUtils.writeEnvFile(opts.env, [`DID="${document.id}"`], opts.mergeEnv);
113
+ }
114
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explore"), `${StringHelper.trimTrailingSlashes(explorerEndpoint)}/addr/${IotaIdentityUtils.didToAddress(document.id)}?tab=DID`);
115
+ CLIDisplay.break();
116
+ CLIDisplay.done();
117
+ }
118
+
119
+ // Copyright 2024 IOTA Stiftung.
120
+ // SPDX-License-Identifier: Apache-2.0.
121
+ /**
122
+ * Build the identity resolve command for the CLI.
123
+ * @returns The command.
124
+ */
125
+ function buildCommandIdentityResolve() {
126
+ const command = new Command();
127
+ command
128
+ .name("identity-resolve")
129
+ .summary(I18n.formatMessage("commands.identity-resolve.summary"))
130
+ .description(I18n.formatMessage("commands.identity-resolve.description"))
131
+ .requiredOption(I18n.formatMessage("commands.identity-resolve.options.did.param"), I18n.formatMessage("commands.identity-resolve.options.did.description"));
132
+ CLIOptions.output(command, {
133
+ noConsole: true,
134
+ json: true,
135
+ env: false,
136
+ mergeJson: true,
137
+ mergeEnv: false
138
+ });
139
+ command
140
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
141
+ .option(I18n.formatMessage("commands.common.options.explorer.param"), I18n.formatMessage("commands.common.options.explorer.description"), "!EXPLORER_URL")
142
+ .action(actionCommandIdentityResolve);
143
+ return command;
144
+ }
145
+ /**
146
+ * Action the identity resolve command.
147
+ * @param opts The options for the command.
148
+ * @param opts.did The identity to resolve.
149
+ * @param opts.node The node URL.
150
+ * @param opts.explorer The explorer URL.
151
+ */
152
+ async function actionCommandIdentityResolve(opts) {
153
+ const did = CLIParam.stringValue("did", opts.did);
154
+ const nodeEndpoint = CLIParam.url("node", opts.node);
155
+ const explorerEndpoint = CLIParam.url("explorer", opts.explorer);
156
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.did"), did);
157
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
158
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explorer"), explorerEndpoint);
159
+ CLIDisplay.break();
160
+ setupVault();
161
+ const iotaWalletConnector = new IotaWalletConnector({
162
+ config: {
163
+ clientOptions: {
164
+ nodes: [nodeEndpoint],
165
+ localPow: true
166
+ }
167
+ }
168
+ });
169
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
170
+ const iotaIdentityConnector = new IotaIdentityConnector({
171
+ config: {
172
+ clientOptions: {
173
+ nodes: [nodeEndpoint],
174
+ localPow: true
175
+ }
176
+ }
177
+ });
178
+ CLIDisplay.task(I18n.formatMessage("commands.identity-resolve.progress.resolvingIdentity"));
179
+ CLIDisplay.break();
180
+ CLIDisplay.spinnerStart();
181
+ const document = await iotaIdentityConnector.resolveDocument(did);
182
+ CLIDisplay.spinnerStop();
183
+ if (opts.console) {
184
+ CLIDisplay.section(I18n.formatMessage("commands.identity-resolve.labels.didDocument"));
185
+ CLIDisplay.json(document);
186
+ CLIDisplay.break();
187
+ }
188
+ if (Is.stringValue(opts?.json)) {
189
+ await CLIUtils.writeJsonFile(opts.json, document, opts.mergeJson);
190
+ }
191
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explore"), `${StringHelper.trimTrailingSlashes(explorerEndpoint)}/addr/${IotaIdentityUtils.didToAddress(document.id)}?tab=DID`);
192
+ CLIDisplay.break();
193
+ CLIDisplay.done();
194
+ }
195
+
196
+ // Copyright 2024 IOTA Stiftung.
197
+ // SPDX-License-Identifier: Apache-2.0.
198
+ /**
199
+ * Build the proof create command for the CLI.
200
+ * @returns The command.
201
+ */
202
+ function buildCommandProofCreate() {
203
+ const command = new Command();
204
+ command
205
+ .name("proof-create")
206
+ .summary(I18n.formatMessage("commands.proof-create.summary"))
207
+ .description(I18n.formatMessage("commands.proof-create.description"))
208
+ .requiredOption(I18n.formatMessage("commands.proof-create.options.id.param"), I18n.formatMessage("commands.proof-create.options.id.description"))
209
+ .requiredOption(I18n.formatMessage("commands.proof-create.options.private-key.param"), I18n.formatMessage("commands.proof-create.options.private-key.description"))
210
+ .requiredOption(I18n.formatMessage("commands.proof-create.options.data.param"), I18n.formatMessage("commands.proof-create.options.data.description"));
211
+ CLIOptions.output(command, {
212
+ noConsole: true,
213
+ json: true,
214
+ env: true,
215
+ mergeJson: true,
216
+ mergeEnv: true
217
+ });
218
+ command
219
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
220
+ .action(actionCommandProofCreate);
221
+ return command;
222
+ }
223
+ /**
224
+ * Action the proof create command.
225
+ * @param opts The options for the command.
226
+ * @param opts.id The id of the verification method to use for the credential.
227
+ * @param opts.privateKey The private key for the verification method.
228
+ * @param opts.data The data to create the proof for.
229
+ * @param opts.node The node URL.
230
+ */
231
+ async function actionCommandProofCreate(opts) {
232
+ const id = CLIParam.stringValue("id", opts.id);
233
+ const privateKey = CLIParam.hexBase64("private-key", opts.privateKey);
234
+ const data = CLIParam.hexBase64("data", opts.data);
235
+ const nodeEndpoint = CLIParam.url("node", opts.node);
236
+ CLIDisplay.value(I18n.formatMessage("commands.proof-create.labels.verificationMethodId"), id);
237
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
238
+ CLIDisplay.break();
239
+ setupVault();
240
+ const localIdentity = "local";
241
+ const vaultConnector = VaultConnectorFactory.get("vault");
242
+ await vaultConnector.addKey(`${localIdentity}/${id}`, VaultKeyType.Ed25519, privateKey, new Uint8Array());
243
+ const iotaWalletConnector = new IotaWalletConnector({
244
+ config: {
245
+ clientOptions: {
246
+ nodes: [nodeEndpoint],
247
+ localPow: true
248
+ }
249
+ }
250
+ });
251
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
252
+ const iotaIdentityConnector = new IotaIdentityConnector({
253
+ config: {
254
+ clientOptions: {
255
+ nodes: [nodeEndpoint],
256
+ localPow: true
257
+ }
258
+ }
259
+ });
260
+ CLIDisplay.task(I18n.formatMessage("commands.proof-create.progress.creatingProof"));
261
+ CLIDisplay.break();
262
+ CLIDisplay.spinnerStart();
263
+ const proof = await iotaIdentityConnector.createProof(localIdentity, id, data);
264
+ const proofValue = Converter.bytesToBase64(proof.value);
265
+ CLIDisplay.spinnerStop();
266
+ if (opts.console) {
267
+ CLIDisplay.value(I18n.formatMessage("commands.proof-create.labels.type"), proof.type);
268
+ CLIDisplay.value(I18n.formatMessage("commands.proof-create.labels.value"), proofValue);
269
+ CLIDisplay.break();
270
+ }
271
+ if (Is.stringValue(opts?.json)) {
272
+ await CLIUtils.writeJsonFile(opts.json, { type: proof.type, value: proofValue }, opts.mergeJson);
273
+ }
274
+ if (Is.stringValue(opts?.env)) {
275
+ await CLIUtils.writeEnvFile(opts.env, [`DID_PROOF_TYPE="${proof.type}"`, `DID_PROOF_VALUE="${proofValue}"`], opts.mergeEnv);
276
+ }
277
+ CLIDisplay.done();
278
+ }
279
+
280
+ // Copyright 2024 IOTA Stiftung.
281
+ // SPDX-License-Identifier: Apache-2.0.
282
+ /**
283
+ * Build the proof verify command for the CLI.
284
+ * @returns The command.
285
+ */
286
+ function buildCommandProofVerify() {
287
+ const command = new Command();
288
+ command
289
+ .name("proof-verify")
290
+ .summary(I18n.formatMessage("commands.proof-verify.summary"))
291
+ .description(I18n.formatMessage("commands.proof-verify.description"))
292
+ .requiredOption(I18n.formatMessage("commands.proof-verify.options.id.param"), I18n.formatMessage("commands.proof-verify.options.id.description"))
293
+ .requiredOption(I18n.formatMessage("commands.proof-verify.options.data.param"), I18n.formatMessage("commands.proof-verify.options.data.description"))
294
+ .requiredOption(I18n.formatMessage("commands.proof-verify.options.type.param"), I18n.formatMessage("commands.proof-verify.options.type.description"))
295
+ .requiredOption(I18n.formatMessage("commands.proof-verify.options.value.param"), I18n.formatMessage("commands.proof-verify.options.value.description"));
296
+ CLIOptions.output(command, {
297
+ noConsole: true,
298
+ json: true,
299
+ env: true,
300
+ mergeJson: true,
301
+ mergeEnv: true
302
+ });
303
+ command
304
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
305
+ .action(actionCommandProofVerify);
306
+ return command;
307
+ }
308
+ /**
309
+ * Action the proof verify command.
310
+ * @param opts The options for the command.
311
+ * @param opts.id The id of the verification method to use for the credential.
312
+ * @param opts.data The data to verify the proof for.
313
+ * @param opts.type The type of the proof.
314
+ * @param opts.value The proof value.
315
+ * @param opts.node The node URL.
316
+ */
317
+ async function actionCommandProofVerify(opts) {
318
+ const id = CLIParam.stringValue("id", opts.id);
319
+ const data = CLIParam.hexBase64("data", opts.data);
320
+ const type = CLIParam.stringValue("type", opts.type);
321
+ const value = CLIParam.hexBase64("value", opts.value);
322
+ const nodeEndpoint = CLIParam.url("node", opts.node);
323
+ CLIDisplay.value(I18n.formatMessage("commands.proof-verify.labels.verificationMethodId"), id);
324
+ CLIDisplay.value(I18n.formatMessage("commands.proof-verify.labels.type"), type);
325
+ CLIDisplay.value(I18n.formatMessage("commands.proof-verify.labels.value"), Converter.bytesToBase64(value));
326
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
327
+ CLIDisplay.break();
328
+ setupVault();
329
+ const iotaWalletConnector = new IotaWalletConnector({
330
+ config: {
331
+ clientOptions: {
332
+ nodes: [nodeEndpoint],
333
+ localPow: true
334
+ }
335
+ }
336
+ });
337
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
338
+ const iotaIdentityConnector = new IotaIdentityConnector({
339
+ config: {
340
+ clientOptions: {
341
+ nodes: [nodeEndpoint],
342
+ localPow: true
343
+ }
344
+ }
345
+ });
346
+ CLIDisplay.task(I18n.formatMessage("commands.proof-verify.progress.verifyingProof"));
347
+ CLIDisplay.break();
348
+ CLIDisplay.spinnerStart();
349
+ const isVerified = await iotaIdentityConnector.verifyProof(id, data, type, value);
350
+ CLIDisplay.spinnerStop();
351
+ if (opts.console) {
352
+ CLIDisplay.value(I18n.formatMessage("commands.proof-verify.labels.isVerified"), isVerified);
353
+ CLIDisplay.break();
354
+ }
355
+ if (Is.stringValue(opts?.json)) {
356
+ await CLIUtils.writeJsonFile(opts.json, { isVerified }, opts.mergeJson);
357
+ }
358
+ if (Is.stringValue(opts?.env)) {
359
+ await CLIUtils.writeEnvFile(opts.env, [`DID_PROOF_VERIFIED="${isVerified}"`], opts.mergeEnv);
360
+ }
361
+ CLIDisplay.done();
362
+ }
363
+
364
+ // Copyright 2024 IOTA Stiftung.
365
+ // SPDX-License-Identifier: Apache-2.0.
366
+ /**
367
+ * Build the service add command for the CLI.
368
+ * @returns The command.
369
+ */
370
+ function buildCommandServiceAdd() {
371
+ const command = new Command();
372
+ command
373
+ .name("service-add")
374
+ .summary(I18n.formatMessage("commands.service-add.summary"))
375
+ .description(I18n.formatMessage("commands.service-add.description"))
376
+ .requiredOption(I18n.formatMessage("commands.service-add.options.seed.param"), I18n.formatMessage("commands.service-add.options.seed.description"))
377
+ .requiredOption(I18n.formatMessage("commands.service-add.options.did.param"), I18n.formatMessage("commands.service-add.options.did.description"))
378
+ .requiredOption(I18n.formatMessage("commands.service-add.options.id.param"), I18n.formatMessage("commands.service-add.options.id.description"))
379
+ .requiredOption(I18n.formatMessage("commands.service-add.options.type.param"), I18n.formatMessage("commands.service-add.options.type.description"))
380
+ .requiredOption(I18n.formatMessage("commands.service-add.options.endpoint.param"), I18n.formatMessage("commands.service-add.options.endpoint.description"));
381
+ CLIOptions.output(command, {
382
+ noConsole: true,
383
+ json: true,
384
+ env: true,
385
+ mergeJson: true,
386
+ mergeEnv: true
387
+ });
388
+ command
389
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
390
+ .option(I18n.formatMessage("commands.common.options.explorer.param"), I18n.formatMessage("commands.common.options.explorer.description"), "!EXPLORER_URL")
391
+ .action(actionCommandServiceAdd);
392
+ return command;
393
+ }
394
+ /**
395
+ * Action the service add command.
396
+ * @param opts The options for the command.
397
+ * @param opts.seed The private key for the controller.
398
+ * @param opts.did The identity of the document to add to.
399
+ * @param opts.id The id of the service to add.
400
+ * @param opts.type The type of the service to add.
401
+ * @param opts.endpoint The service endpoint.
402
+ * @param opts.node The node URL.
403
+ * @param opts.explorer The explorer URL.
404
+ */
405
+ async function actionCommandServiceAdd(opts) {
406
+ const seed = CLIParam.hexBase64("seed", opts.seed);
407
+ const did = CLIParam.stringValue("did", opts.did);
408
+ const id = CLIParam.stringValue("id", opts.id);
409
+ const type = CLIParam.stringValue("type", opts.type);
410
+ const endpoint = CLIParam.url("endpoint", opts.endpoint);
411
+ const nodeEndpoint = CLIParam.url("node", opts.node);
412
+ const explorerEndpoint = CLIParam.url("explorer", opts.explorer);
413
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.did"), did);
414
+ CLIDisplay.value(I18n.formatMessage("commands.service-add.labels.serviceId"), id);
415
+ CLIDisplay.value(I18n.formatMessage("commands.service-add.labels.serviceType"), type);
416
+ CLIDisplay.value(I18n.formatMessage("commands.service-add.labels.serviceEndpoint"), endpoint);
417
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
418
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explorer"), explorerEndpoint);
419
+ CLIDisplay.break();
420
+ setupVault();
421
+ const vaultSeedId = "local-seed";
422
+ const localIdentity = "local";
423
+ const vaultConnector = VaultConnectorFactory.get("vault");
424
+ await vaultConnector.setSecret(`${localIdentity}/${vaultSeedId}`, Converter.bytesToBase64(seed));
425
+ const iotaWalletConnector = new IotaWalletConnector({
426
+ config: {
427
+ clientOptions: {
428
+ nodes: [nodeEndpoint],
429
+ localPow: true
430
+ },
431
+ vaultSeedId
432
+ }
433
+ });
434
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
435
+ const iotaIdentityConnector = new IotaIdentityConnector({
436
+ config: {
437
+ clientOptions: {
438
+ nodes: [nodeEndpoint],
439
+ localPow: true
440
+ },
441
+ vaultSeedId
442
+ }
443
+ });
444
+ CLIDisplay.task(I18n.formatMessage("commands.service-add.progress.addingService"));
445
+ CLIDisplay.break();
446
+ CLIDisplay.spinnerStart();
447
+ const service = await iotaIdentityConnector.addService(localIdentity, did, id, type, endpoint);
448
+ CLIDisplay.spinnerStop();
449
+ if (Is.stringValue(opts?.json)) {
450
+ await CLIUtils.writeJsonFile(opts.json, service, opts.mergeJson);
451
+ }
452
+ if (Is.stringValue(opts?.env)) {
453
+ await CLIUtils.writeEnvFile(opts.env, [
454
+ `DID_SERVICE_ID="${service.id}"`,
455
+ `DID_SERVICE_TYPE="${service.type}"`,
456
+ `DID_SERVICE_ENDPOINT="${service.serviceEndpoint}"`
457
+ ], opts.mergeEnv);
458
+ }
459
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explore"), `${StringHelper.trimTrailingSlashes(explorerEndpoint)}/addr/${IotaIdentityUtils.didToAddress(did)}?tab=DID`);
460
+ CLIDisplay.break();
461
+ CLIDisplay.done();
462
+ }
463
+
464
+ // Copyright 2024 IOTA Stiftung.
465
+ // SPDX-License-Identifier: Apache-2.0.
466
+ /**
467
+ * Build the service remove command for the CLI.
468
+ * @returns The command.
469
+ */
470
+ function buildCommandServiceRemove() {
471
+ const command = new Command();
472
+ command
473
+ .name("service-remove")
474
+ .summary(I18n.formatMessage("commands.service-remove.summary"))
475
+ .description(I18n.formatMessage("commands.service-remove.description"))
476
+ .requiredOption(I18n.formatMessage("commands.service-remove.options.seed.param"), I18n.formatMessage("commands.service-remove.options.seed.description"))
477
+ .requiredOption(I18n.formatMessage("commands.service-remove.options.id.param"), I18n.formatMessage("commands.service-remove.options.id.description"));
478
+ CLIOptions.output(command, {
479
+ noConsole: true,
480
+ json: true,
481
+ env: true,
482
+ mergeJson: true,
483
+ mergeEnv: true
484
+ });
485
+ command
486
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
487
+ .option(I18n.formatMessage("commands.common.options.explorer.param"), I18n.formatMessage("commands.common.options.explorer.description"), "!EXPLORER_URL")
488
+ .action(actionCommandServiceRemove);
489
+ return command;
490
+ }
491
+ /**
492
+ * Action the service remove command.
493
+ * @param opts The options for the command.
494
+ * @param opts.seed The private key for the controller.
495
+ * @param opts.id The id of the service to remove.
496
+ * @param opts.node The node URL.
497
+ * @param opts.explorer The explorer URL.
498
+ */
499
+ async function actionCommandServiceRemove(opts) {
500
+ const seed = CLIParam.hexBase64("seed", opts.seed);
501
+ const id = CLIParam.stringValue("id", opts.id);
502
+ const nodeEndpoint = CLIParam.url("node", opts.node);
503
+ const explorerEndpoint = CLIParam.url("explorer", opts.explorer);
504
+ CLIDisplay.value(I18n.formatMessage("commands.service-remove.labels.serviceId"), id);
505
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
506
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explorer"), explorerEndpoint);
507
+ CLIDisplay.break();
508
+ setupVault();
509
+ const vaultSeedId = "local-seed";
510
+ const localIdentity = "local";
511
+ const vaultConnector = VaultConnectorFactory.get("vault");
512
+ await vaultConnector.setSecret(`${localIdentity}/${vaultSeedId}`, Converter.bytesToBase64(seed));
513
+ const iotaWalletConnector = new IotaWalletConnector({
514
+ config: {
515
+ clientOptions: {
516
+ nodes: [nodeEndpoint],
517
+ localPow: true
518
+ },
519
+ vaultSeedId
520
+ }
521
+ });
522
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
523
+ const iotaIdentityConnector = new IotaIdentityConnector({
524
+ config: {
525
+ clientOptions: {
526
+ nodes: [nodeEndpoint],
527
+ localPow: true
528
+ },
529
+ vaultSeedId
530
+ }
531
+ });
532
+ CLIDisplay.task(I18n.formatMessage("commands.service-remove.progress.removingService"));
533
+ CLIDisplay.break();
534
+ CLIDisplay.spinnerStart();
535
+ await iotaIdentityConnector.removeService(localIdentity, id);
536
+ CLIDisplay.spinnerStop();
537
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explore"), `${StringHelper.trimTrailingSlashes(explorerEndpoint)}/addr/${IotaIdentityUtils.didToAddress(DocumentHelper.parse(id).id)}?tab=DID`);
538
+ CLIDisplay.break();
539
+ CLIDisplay.done();
540
+ }
541
+
542
+ // Copyright 2024 IOTA Stiftung.
543
+ // SPDX-License-Identifier: Apache-2.0.
544
+ /**
545
+ * Build the verifiable credential create command for the CLI.
546
+ * @returns The command.
547
+ */
548
+ function buildCommandVerifiableCredentialCreate() {
549
+ const command = new Command();
550
+ command
551
+ .name("verifiable-credential-create")
552
+ .summary(I18n.formatMessage("commands.verifiable-credential-create.summary"))
553
+ .description(I18n.formatMessage("commands.verifiable-credential-create.description"))
554
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-create.options.id.param"), I18n.formatMessage("commands.verifiable-credential-create.options.id.description"))
555
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-create.options.private-key.param"), I18n.formatMessage("commands.verifiable-credential-create.options.private-key.description"))
556
+ .option(I18n.formatMessage("commands.verifiable-credential-create.options.credential-id.param"), I18n.formatMessage("commands.verifiable-credential-create.options.credential-id.description"))
557
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-create.options.subject-json.param"), I18n.formatMessage("commands.verifiable-credential-create.options.subject-json.description"))
558
+ .option(I18n.formatMessage("commands.verifiable-credential-create.options.revocation-index.param"), I18n.formatMessage("commands.verifiable-credential-create.options.revocation-index.description"));
559
+ CLIOptions.output(command, {
560
+ noConsole: true,
561
+ json: true,
562
+ env: true,
563
+ mergeJson: true,
564
+ mergeEnv: true
565
+ });
566
+ command
567
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
568
+ .action(actionCommandVerifiableCredentialCreate);
569
+ return command;
570
+ }
571
+ /**
572
+ * Action the verifiable credential create command.
573
+ * @param opts The options for the command.
574
+ * @param opts.id The id of the verification method to use for the credential.
575
+ * @param opts.privateKey The private key for the verification method.
576
+ * @param opts.credentialId The id of the credential.
577
+ * @param opts.subjectJson The JSON data for the subject.
578
+ * @param opts.revocationIndex The revocation index for the credential.
579
+ * @param opts.node The node URL.
580
+ */
581
+ async function actionCommandVerifiableCredentialCreate(opts) {
582
+ const id = CLIParam.stringValue("id", opts.id);
583
+ const privateKey = CLIParam.hexBase64("private-key", opts.privateKey);
584
+ const credentialId = CLIParam.stringValue("credential-id", opts.credentialId);
585
+ const subjectJson = path.resolve(CLIParam.stringValue("subject-json", opts.subjectJson));
586
+ const revocationIndex = Coerce.number(opts.revocationIndex);
587
+ const nodeEndpoint = CLIParam.url("node", opts.node);
588
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-create.labels.verificationMethodId"), id);
589
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-create.labels.credentialId"), credentialId);
590
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-create.labels.subjectJson"), subjectJson);
591
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-create.labels.revocationIndex"), revocationIndex);
592
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
593
+ CLIDisplay.break();
594
+ setupVault();
595
+ const localIdentity = "local";
596
+ const vaultConnector = VaultConnectorFactory.get("vault");
597
+ await vaultConnector.addKey(`${localIdentity}/${id}`, VaultKeyType.Ed25519, privateKey, new Uint8Array());
598
+ const iotaWalletConnector = new IotaWalletConnector({
599
+ config: {
600
+ clientOptions: {
601
+ nodes: [nodeEndpoint],
602
+ localPow: true
603
+ }
604
+ }
605
+ });
606
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
607
+ const iotaIdentityConnector = new IotaIdentityConnector({
608
+ config: {
609
+ clientOptions: {
610
+ nodes: [nodeEndpoint],
611
+ localPow: true
612
+ }
613
+ }
614
+ });
615
+ CLIDisplay.task(I18n.formatMessage("commands.verifiable-credential-create.progress.loadingSubjectData"));
616
+ CLIDisplay.break();
617
+ const jsonData = await CLIUtils.readJsonFile(subjectJson);
618
+ if (Is.undefined(jsonData)) {
619
+ throw new GeneralError("commands", "commands.verifiable-credential-create.subjectJsonFileNotFound");
620
+ }
621
+ CLIDisplay.task(I18n.formatMessage("commands.verifiable-credential-create.progress.creatingVerifiableCredential"));
622
+ CLIDisplay.break();
623
+ CLIDisplay.spinnerStart();
624
+ const verifiableCredential = await iotaIdentityConnector.createVerifiableCredential(localIdentity, id, credentialId, jsonData, revocationIndex);
625
+ CLIDisplay.spinnerStop();
626
+ if (opts.console) {
627
+ CLIDisplay.section(I18n.formatMessage("commands.verifiable-credential-create.labels.verifiableCredential"));
628
+ CLIDisplay.write(verifiableCredential.jwt);
629
+ CLIDisplay.break();
630
+ CLIDisplay.break();
631
+ }
632
+ if (Is.stringValue(opts?.json)) {
633
+ await CLIUtils.writeJsonFile(opts.json, { verifiableCredentialJwt: verifiableCredential.jwt }, opts.mergeJson);
634
+ }
635
+ if (Is.stringValue(opts?.env)) {
636
+ await CLIUtils.writeEnvFile(opts.env, [`DID_VERIFIABLE_CREDENTIAL_JWT="${verifiableCredential.jwt}"`], opts.mergeEnv);
637
+ }
638
+ CLIDisplay.done();
639
+ }
640
+
641
+ // Copyright 2024 IOTA Stiftung.
642
+ // SPDX-License-Identifier: Apache-2.0.
643
+ /**
644
+ * Build the verifiable credential revoke command for the CLI.
645
+ * @returns The command.
646
+ */
647
+ function buildCommandVerifiableCredentialRevoke() {
648
+ const command = new Command();
649
+ command
650
+ .name("verifiable-credential-revoke")
651
+ .summary(I18n.formatMessage("commands.verifiable-credential-revoke.summary"))
652
+ .description(I18n.formatMessage("commands.verifiable-credential-revoke.description"))
653
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-revoke.options.seed.param"), I18n.formatMessage("commands.verifiable-credential-revoke.options.seed.description"))
654
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-revoke.options.did.param"), I18n.formatMessage("commands.verifiable-credential-revoke.options.did.description"))
655
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-revoke.options.revocation-index.param"), I18n.formatMessage("commands.verifiable-credential-revoke.options.revocation-index.description"));
656
+ command
657
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
658
+ .action(actionCommandVerifiableCredentialRevoke);
659
+ return command;
660
+ }
661
+ /**
662
+ * Action the verifiable credential revoke command.
663
+ * @param opts The options for the command.
664
+ * @param opts.seed The seed to generate the private key for the controller.
665
+ * @param opts.did The id of the document to revoke the index.
666
+ * @param opts.revocationIndex The revocation index for the credential.
667
+ * @param opts.node The node URL.
668
+ */
669
+ async function actionCommandVerifiableCredentialRevoke(opts) {
670
+ const seed = CLIParam.hexBase64("seed", opts.seed);
671
+ const did = CLIParam.stringValue("did", opts.did);
672
+ const revocationIndex = CLIParam.integer("revocation-index", opts.revocationIndex);
673
+ const nodeEndpoint = CLIParam.url("node", opts.node);
674
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.did"), did);
675
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-revoke.labels.revocationIndex"), revocationIndex);
676
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
677
+ CLIDisplay.break();
678
+ setupVault();
679
+ const vaultSeedId = "local-seed";
680
+ const localIdentity = "local";
681
+ const vaultConnector = VaultConnectorFactory.get("vault");
682
+ await vaultConnector.setSecret(`${localIdentity}/${vaultSeedId}`, Converter.bytesToBase64(seed));
683
+ const iotaWalletConnector = new IotaWalletConnector({
684
+ config: {
685
+ clientOptions: {
686
+ nodes: [nodeEndpoint],
687
+ localPow: true
688
+ },
689
+ vaultSeedId
690
+ }
691
+ });
692
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
693
+ const iotaIdentityConnector = new IotaIdentityConnector({
694
+ config: {
695
+ clientOptions: {
696
+ nodes: [nodeEndpoint],
697
+ localPow: true
698
+ },
699
+ vaultSeedId
700
+ }
701
+ });
702
+ CLIDisplay.task(I18n.formatMessage("commands.verifiable-credential-revoke.progress.revokingCredential"));
703
+ CLIDisplay.break();
704
+ CLIDisplay.spinnerStart();
705
+ await iotaIdentityConnector.revokeVerifiableCredentials(localIdentity, did, [revocationIndex]);
706
+ CLIDisplay.spinnerStop();
707
+ CLIDisplay.done();
708
+ }
709
+
710
+ // Copyright 2024 IOTA Stiftung.
711
+ // SPDX-License-Identifier: Apache-2.0.
712
+ /**
713
+ * Build the verifiable credential unrevoke command for the CLI.
714
+ * @returns The command.
715
+ */
716
+ function buildCommandVerifiableCredentialUnrevoke() {
717
+ const command = new Command();
718
+ command
719
+ .name("verifiable-credential-unrevoke")
720
+ .summary(I18n.formatMessage("commands.verifiable-credential-unrevoke.summary"))
721
+ .description(I18n.formatMessage("commands.verifiable-credential-unrevoke.description"))
722
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-unrevoke.options.seed.param"), I18n.formatMessage("commands.verifiable-credential-unrevoke.options.seed.description"))
723
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-unrevoke.options.did.param"), I18n.formatMessage("commands.verifiable-credential-unrevoke.options.did.description"))
724
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-unrevoke.options.revocation-index.param"), I18n.formatMessage("commands.verifiable-credential-unrevoke.options.revocation-index.description"));
725
+ command
726
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
727
+ .action(actionCommandVerifiableCredentialUnrevoke);
728
+ return command;
729
+ }
730
+ /**
731
+ * Action the verifiable credential unrevoke command.
732
+ * @param opts The options for the command.
733
+ * @param opts.seed The seed to generate the private key for the controller.
734
+ * @param opts.did The id of the document to unrevoke the index.
735
+ * @param opts.revocationIndex The revocation index for the credential.
736
+ * @param opts.node The node URL.
737
+ */
738
+ async function actionCommandVerifiableCredentialUnrevoke(opts) {
739
+ const seed = CLIParam.hexBase64("seed", opts.seed);
740
+ const did = CLIParam.stringValue("did", opts.did);
741
+ const revocationIndex = CLIParam.integer("revocation-index", opts.revocationIndex);
742
+ const nodeEndpoint = CLIParam.url("node", opts.node);
743
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.did"), did);
744
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-unrevoke.labels.revocationIndex"), revocationIndex);
745
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
746
+ CLIDisplay.break();
747
+ setupVault();
748
+ const vaultSeedId = "local-seed";
749
+ const localIdentity = "local";
750
+ const vaultConnector = VaultConnectorFactory.get("vault");
751
+ await vaultConnector.setSecret(`${localIdentity}/${vaultSeedId}`, Converter.bytesToBase64(seed));
752
+ const iotaWalletConnector = new IotaWalletConnector({
753
+ config: {
754
+ clientOptions: {
755
+ nodes: [nodeEndpoint],
756
+ localPow: true
757
+ },
758
+ vaultSeedId
759
+ }
760
+ });
761
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
762
+ const iotaIdentityConnector = new IotaIdentityConnector({
763
+ config: {
764
+ clientOptions: {
765
+ nodes: [nodeEndpoint],
766
+ localPow: true
767
+ },
768
+ vaultSeedId
769
+ }
770
+ });
771
+ CLIDisplay.task(I18n.formatMessage("commands.verifiable-credential-unrevoke.progress.unrevokingCredential"));
772
+ CLIDisplay.break();
773
+ CLIDisplay.spinnerStart();
774
+ await iotaIdentityConnector.unrevokeVerifiableCredentials(localIdentity, did, [revocationIndex]);
775
+ CLIDisplay.spinnerStop();
776
+ CLIDisplay.done();
777
+ }
778
+
779
+ // Copyright 2024 IOTA Stiftung.
780
+ // SPDX-License-Identifier: Apache-2.0.
781
+ /**
782
+ * Build the verifiable credential verify command for the CLI.
783
+ * @returns The command.
784
+ */
785
+ function buildCommandVerifiableCredentialVerify() {
786
+ const command = new Command();
787
+ command
788
+ .name("verifiable-credential-verify")
789
+ .summary(I18n.formatMessage("commands.verifiable-credential-verify.summary"))
790
+ .description(I18n.formatMessage("commands.verifiable-credential-verify.description"))
791
+ .requiredOption(I18n.formatMessage("commands.verifiable-credential-verify.options.jwt.param"), I18n.formatMessage("commands.verifiable-credential-verify.options.jwt.description"));
792
+ CLIOptions.output(command, {
793
+ noConsole: true,
794
+ json: true,
795
+ env: true,
796
+ mergeJson: true,
797
+ mergeEnv: true
798
+ });
799
+ command
800
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
801
+ .action(actionCommandVerifiableCredentialVerify);
802
+ return command;
803
+ }
804
+ /**
805
+ * Action the verifiable credential verify command.
806
+ * @param opts The options for the command.
807
+ * @param opts.jwt The JSON web token for the verifiable credential.
808
+ * @param opts.node The node URL.
809
+ */
810
+ async function actionCommandVerifiableCredentialVerify(opts) {
811
+ const jwt = CLIParam.stringValue("jwt", opts.jwt);
812
+ const nodeEndpoint = CLIParam.url("node", opts.node);
813
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-verify.labels.jwt"), jwt);
814
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
815
+ CLIDisplay.break();
816
+ setupVault();
817
+ const iotaWalletConnector = new IotaWalletConnector({
818
+ config: {
819
+ clientOptions: {
820
+ nodes: [nodeEndpoint],
821
+ localPow: true
822
+ }
823
+ }
824
+ });
825
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
826
+ const iotaIdentityConnector = new IotaIdentityConnector({
827
+ config: {
828
+ clientOptions: {
829
+ nodes: [nodeEndpoint],
830
+ localPow: true
831
+ }
832
+ }
833
+ });
834
+ CLIDisplay.task(I18n.formatMessage("commands.verifiable-credential-verify.progress.verifyingCredential"));
835
+ CLIDisplay.break();
836
+ CLIDisplay.spinnerStart();
837
+ const verification = await iotaIdentityConnector.checkVerifiableCredential(jwt);
838
+ const isVerified = Is.notEmpty(verification.verifiableCredential);
839
+ const isRevoked = verification.revoked;
840
+ CLIDisplay.spinnerStop();
841
+ if (opts.console) {
842
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-verify.labels.isVerified"), isVerified);
843
+ CLIDisplay.value(I18n.formatMessage("commands.verifiable-credential-verify.labels.isRevoked"), isRevoked);
844
+ CLIDisplay.break();
845
+ }
846
+ if (Is.stringValue(opts?.json)) {
847
+ await CLIUtils.writeJsonFile(opts.json, { isVerified, isRevoked }, opts.mergeJson);
848
+ }
849
+ if (Is.stringValue(opts?.env)) {
850
+ await CLIUtils.writeEnvFile(opts.env, [
851
+ `DID_VERIFIABLE_CREDENTIAL_VERIFIED="${isVerified}"`,
852
+ `DID_VERIFIABLE_CREDENTIAL_REVOKED="${isRevoked}"`
853
+ ], opts.mergeEnv);
854
+ }
855
+ CLIDisplay.done();
856
+ }
857
+
858
+ // Copyright 2024 IOTA Stiftung.
859
+ // SPDX-License-Identifier: Apache-2.0.
860
+ /**
861
+ * Build the verification method add command for the CLI.
862
+ * @returns The command.
863
+ */
864
+ function buildCommandVerificationMethodAdd() {
865
+ const command = new Command();
866
+ command
867
+ .name("verification-method-add")
868
+ .summary(I18n.formatMessage("commands.verification-method-add.summary"))
869
+ .description(I18n.formatMessage("commands.verification-method-add.description"))
870
+ .requiredOption(I18n.formatMessage("commands.verification-method-add.options.seed.param"), I18n.formatMessage("commands.verification-method-add.options.seed.description"))
871
+ .requiredOption(I18n.formatMessage("commands.verification-method-add.options.did.param"), I18n.formatMessage("commands.verification-method-add.options.did.description"))
872
+ .addOption(new Option(I18n.formatMessage("commands.verification-method-add.options.type.param"), I18n.formatMessage("commands.verification-method-add.options.type.description"))
873
+ .choices(Object.values(DidVerificationMethodType))
874
+ .makeOptionMandatory())
875
+ .option(I18n.formatMessage("commands.verification-method-add.options.id.param"), I18n.formatMessage("commands.verification-method-add.options.id.description"));
876
+ CLIOptions.output(command, {
877
+ noConsole: true,
878
+ json: true,
879
+ env: true,
880
+ mergeJson: true,
881
+ mergeEnv: true
882
+ });
883
+ command
884
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
885
+ .option(I18n.formatMessage("commands.common.options.explorer.param"), I18n.formatMessage("commands.common.options.explorer.description"), "!EXPLORER_URL")
886
+ .action(actionCommandVerificationMethodAdd);
887
+ return command;
888
+ }
889
+ /**
890
+ * Action the verification method add command.
891
+ * @param opts The options for the command.
892
+ * @param opts.seed The private key for the controller.
893
+ * @param opts.did The identity of the document to add to.
894
+ * @param opts.type The type of the verification method to add.
895
+ * @param opts.id The id of the verification method to add.
896
+ * @param opts.node The node URL.
897
+ * @param opts.explorer The explorer URL.
898
+ */
899
+ async function actionCommandVerificationMethodAdd(opts) {
900
+ const seed = CLIParam.hexBase64("seed", opts.seed);
901
+ const did = CLIParam.stringValue("did", opts.did);
902
+ const type = CLIParam.stringValue("type", opts.type);
903
+ const nodeEndpoint = CLIParam.url("node", opts.node);
904
+ const explorerEndpoint = CLIParam.url("explorer", opts.explorer);
905
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.did"), did);
906
+ CLIDisplay.value(I18n.formatMessage("commands.verification-method-add.labels.verificationMethodType"), type);
907
+ if (Is.stringValue(opts.id)) {
908
+ CLIDisplay.value(I18n.formatMessage("commands.verification-method-add.labels.verificationMethodId"), opts?.id);
909
+ }
910
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
911
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explorer"), explorerEndpoint);
912
+ CLIDisplay.break();
913
+ setupVault();
914
+ const vaultSeedId = "local-seed";
915
+ const localIdentity = "local";
916
+ const vaultConnector = VaultConnectorFactory.get("vault");
917
+ await vaultConnector.setSecret(`${localIdentity}/${vaultSeedId}`, Converter.bytesToBase64(seed));
918
+ const iotaWalletConnector = new IotaWalletConnector({
919
+ config: {
920
+ clientOptions: {
921
+ nodes: [nodeEndpoint],
922
+ localPow: true
923
+ },
924
+ vaultSeedId
925
+ }
926
+ });
927
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
928
+ const iotaIdentityConnector = new IotaIdentityConnector({
929
+ config: {
930
+ clientOptions: {
931
+ nodes: [nodeEndpoint],
932
+ localPow: true
933
+ },
934
+ vaultSeedId
935
+ }
936
+ });
937
+ CLIDisplay.task(I18n.formatMessage("commands.verification-method-add.progress.addingVerificationMethod"));
938
+ CLIDisplay.break();
939
+ CLIDisplay.spinnerStart();
940
+ const verificationMethod = await iotaIdentityConnector.addVerificationMethod(localIdentity, did, type, opts?.id);
941
+ CLIDisplay.spinnerStop();
942
+ const keyPair = await vaultConnector.getKey(`${localIdentity}/${verificationMethod.id}`);
943
+ const privateKey = Converter.bytesToBase64(keyPair.privateKey);
944
+ const publicKey = Converter.bytesToBase64(keyPair.publicKey);
945
+ if (opts.console) {
946
+ CLIDisplay.value(I18n.formatMessage("commands.verification-method-add.labels.verificationMethodId"), verificationMethod.id);
947
+ CLIDisplay.value(I18n.formatMessage("commands.verification-method-add.labels.privateKey"), privateKey);
948
+ CLIDisplay.value(I18n.formatMessage("commands.verification-method-add.labels.publicKey"), publicKey);
949
+ CLIDisplay.break();
950
+ }
951
+ if (Is.stringValue(opts?.json)) {
952
+ await CLIUtils.writeJsonFile(opts.json, { verificationMethodId: verificationMethod.id, privateKey, publicKey }, opts.mergeJson);
953
+ }
954
+ if (Is.stringValue(opts?.env)) {
955
+ await CLIUtils.writeEnvFile(opts.env, [
956
+ `DID_VERIFICATION_METHOD_ID="${verificationMethod.id}"`,
957
+ `DID_VERIFICATION_METHOD_PRIVATE_KEY="${privateKey}"`,
958
+ `DID_VERIFICATION_METHOD_PUBLIC_KEY="${publicKey}"`
959
+ ], opts.mergeEnv);
960
+ }
961
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explore"), `${StringHelper.trimTrailingSlashes(explorerEndpoint)}/addr/${IotaIdentityUtils.didToAddress(did)}?tab=DID`);
962
+ CLIDisplay.break();
963
+ CLIDisplay.done();
964
+ }
965
+
966
+ // Copyright 2024 IOTA Stiftung.
967
+ // SPDX-License-Identifier: Apache-2.0.
968
+ /**
969
+ * Build the verification method remove command for the CLI.
970
+ * @returns The command.
971
+ */
972
+ function buildCommandVerificationMethodRemove() {
973
+ const command = new Command();
974
+ command
975
+ .name("verification-method-remove")
976
+ .summary(I18n.formatMessage("commands.verification-method-remove.summary"))
977
+ .description(I18n.formatMessage("commands.verification-method-remove.description"))
978
+ .requiredOption(I18n.formatMessage("commands.verification-method-remove.options.seed.param"), I18n.formatMessage("commands.verification-method-remove.options.seed.description"))
979
+ .requiredOption(I18n.formatMessage("commands.verification-method-remove.options.id.param"), I18n.formatMessage("commands.verification-method-remove.options.id.description"));
980
+ CLIOptions.output(command, {
981
+ noConsole: true,
982
+ json: true,
983
+ env: true,
984
+ mergeJson: true,
985
+ mergeEnv: true
986
+ });
987
+ command
988
+ .option(I18n.formatMessage("commands.common.options.node.param"), I18n.formatMessage("commands.common.options.node.description"), "!NODE_URL")
989
+ .option(I18n.formatMessage("commands.common.options.explorer.param"), I18n.formatMessage("commands.common.options.explorer.description"), "!EXPLORER_URL")
990
+ .action(actionCommandVerificationMethodRemove);
991
+ return command;
992
+ }
993
+ /**
994
+ * Action the verification method remove command.
995
+ * @param opts The options for the command.
996
+ * @param opts.seed The private key for the controller.
997
+ * @param opts.id The id of the verification method to remove.
998
+ * @param opts.node The node URL.
999
+ * @param opts.explorer The explorer URL.
1000
+ */
1001
+ async function actionCommandVerificationMethodRemove(opts) {
1002
+ const seed = CLIParam.hexBase64("seed", opts.seed);
1003
+ const id = CLIParam.stringValue("id", opts.id);
1004
+ const nodeEndpoint = CLIParam.url("node", opts.node);
1005
+ const explorerEndpoint = CLIParam.url("explorer", opts.explorer);
1006
+ CLIDisplay.value(I18n.formatMessage("commands.verification-method-add.labels.verificationMethodId"), id);
1007
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.node"), nodeEndpoint);
1008
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explorer"), explorerEndpoint);
1009
+ CLIDisplay.break();
1010
+ setupVault();
1011
+ const vaultSeedId = "local-seed";
1012
+ const localIdentity = "local";
1013
+ const vaultConnector = VaultConnectorFactory.get("vault");
1014
+ await vaultConnector.setSecret(`${localIdentity}/${vaultSeedId}`, Converter.bytesToBase64(seed));
1015
+ const iotaWalletConnector = new IotaWalletConnector({
1016
+ config: {
1017
+ clientOptions: {
1018
+ nodes: [nodeEndpoint],
1019
+ localPow: true
1020
+ },
1021
+ vaultSeedId
1022
+ }
1023
+ });
1024
+ WalletConnectorFactory.register("wallet", () => iotaWalletConnector);
1025
+ const iotaIdentityConnector = new IotaIdentityConnector({
1026
+ config: {
1027
+ clientOptions: {
1028
+ nodes: [nodeEndpoint],
1029
+ localPow: true
1030
+ },
1031
+ vaultSeedId
1032
+ }
1033
+ });
1034
+ CLIDisplay.task(I18n.formatMessage("commands.verification-method-remove.progress.removingVerificationMethod"));
1035
+ CLIDisplay.break();
1036
+ CLIDisplay.spinnerStart();
1037
+ await iotaIdentityConnector.removeVerificationMethod(localIdentity, id);
1038
+ CLIDisplay.spinnerStop();
1039
+ CLIDisplay.value(I18n.formatMessage("commands.common.labels.explore"), `${StringHelper.trimTrailingSlashes(explorerEndpoint)}/addr/${IotaIdentityUtils.didToAddress(DocumentHelper.parse(id).id)}?tab=DID`);
1040
+ CLIDisplay.break();
1041
+ CLIDisplay.done();
1042
+ }
1043
+
1044
+ // Copyright 2024 IOTA Stiftung.
1045
+ // SPDX-License-Identifier: Apache-2.0.
1046
+ /**
1047
+ * The main entry point for the CLI.
1048
+ */
1049
+ class CLI extends CLIBase {
1050
+ /**
1051
+ * Run the app.
1052
+ * @param argv The process arguments.
1053
+ * @param localesDirectory The directory for the locales, default to relative to the script.
1054
+ * @param options Additional options for the CLI.
1055
+ * @param options.overrideOutputWidth The override output width.
1056
+ * @returns The exit code.
1057
+ */
1058
+ async run(argv, localesDirectory, options) {
1059
+ return this.execute({
1060
+ title: "TWIN Identity",
1061
+ appName: "twin-identity",
1062
+ version: "0.0.3-next.18",
1063
+ icon: "🌍",
1064
+ supportsEnvFiles: true,
1065
+ overrideOutputWidth: options?.overrideOutputWidth
1066
+ }, localesDirectory ?? path.join(path.dirname(fileURLToPath(import.meta.url)), "../locales"), argv);
1067
+ }
1068
+ /**
1069
+ * Get the commands for the CLI.
1070
+ * @param program The main program to add the commands to.
1071
+ * @internal
1072
+ */
1073
+ getCommands(program) {
1074
+ return [
1075
+ buildCommandMnemonic(),
1076
+ buildCommandAddress(),
1077
+ buildCommandFaucet(),
1078
+ buildCommandTransfer(),
1079
+ buildCommandIdentityCreate(),
1080
+ buildCommandIdentityResolve(),
1081
+ buildCommandVerificationMethodAdd(),
1082
+ buildCommandVerificationMethodRemove(),
1083
+ buildCommandServiceAdd(),
1084
+ buildCommandServiceRemove(),
1085
+ buildCommandVerifiableCredentialCreate(),
1086
+ buildCommandVerifiableCredentialVerify(),
1087
+ buildCommandVerifiableCredentialRevoke(),
1088
+ buildCommandVerifiableCredentialUnrevoke(),
1089
+ buildCommandProofCreate(),
1090
+ buildCommandProofVerify()
1091
+ ];
1092
+ }
1093
+ }
1094
+
1095
+ export { CLI, actionCommandIdentityCreate, actionCommandIdentityResolve, actionCommandProofCreate, actionCommandProofVerify, actionCommandServiceAdd, actionCommandServiceRemove, actionCommandVerifiableCredentialCreate, actionCommandVerifiableCredentialRevoke, actionCommandVerifiableCredentialUnrevoke, actionCommandVerifiableCredentialVerify, actionCommandVerificationMethodAdd, actionCommandVerificationMethodRemove, buildCommandIdentityCreate, buildCommandIdentityResolve, buildCommandProofCreate, buildCommandProofVerify, buildCommandServiceAdd, buildCommandServiceRemove, buildCommandVerifiableCredentialCreate, buildCommandVerifiableCredentialRevoke, buildCommandVerifiableCredentialUnrevoke, buildCommandVerifiableCredentialVerify, buildCommandVerificationMethodAdd, buildCommandVerificationMethodRemove };