@did-btcr2/cli 0.1.0 → 0.2.0

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 (46) hide show
  1. package/dist/cjs/cli.js +233 -115
  2. package/dist/cjs/cli.js.map +1 -1
  3. package/dist/cjs/command.js +27 -33
  4. package/dist/cjs/command.js.map +1 -1
  5. package/dist/cjs/error.js +10 -0
  6. package/dist/cjs/error.js.map +1 -0
  7. package/dist/cjs/index.js +1 -0
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/esm/bin/btcr2.js +5 -0
  10. package/dist/esm/bin/btcr2.js.map +1 -0
  11. package/dist/esm/src/cli.js +294 -0
  12. package/dist/esm/src/cli.js.map +1 -0
  13. package/dist/esm/src/command.js +35 -0
  14. package/dist/esm/src/command.js.map +1 -0
  15. package/dist/esm/src/error.js +10 -0
  16. package/dist/esm/src/error.js.map +1 -0
  17. package/dist/esm/src/index.js +4 -0
  18. package/dist/esm/src/index.js.map +1 -0
  19. package/dist/types/bin/btcr2.d.ts +3 -0
  20. package/dist/types/bin/btcr2.d.ts.map +1 -0
  21. package/dist/types/cli.d.ts +99 -18
  22. package/dist/types/command.d.ts +50 -9
  23. package/dist/types/error.d.ts +7 -0
  24. package/dist/types/index.d.ts +1 -0
  25. package/dist/types/src/cli.d.ts +113 -0
  26. package/dist/types/src/cli.d.ts.map +1 -0
  27. package/dist/types/src/command.d.ts +56 -0
  28. package/dist/types/src/command.d.ts.map +1 -0
  29. package/dist/types/src/error.d.ts +8 -0
  30. package/dist/types/src/error.d.ts.map +1 -0
  31. package/dist/types/src/index.d.ts +4 -0
  32. package/dist/types/src/index.d.ts.map +1 -0
  33. package/package.json +16 -20
  34. package/src/cli.ts +302 -161
  35. package/src/command.ts +67 -38
  36. package/src/error.ts +10 -0
  37. package/src/index.ts +2 -1
  38. package/dist/esm/cli.js +0 -176
  39. package/dist/esm/cli.js.map +0 -1
  40. package/dist/esm/command.js +0 -41
  41. package/dist/esm/command.js.map +0 -1
  42. package/dist/esm/index.js +0 -3
  43. package/dist/esm/index.js.map +0 -1
  44. package/dist/types/cli.d.ts.map +0 -1
  45. package/dist/types/command.d.ts.map +0 -1
  46. package/dist/types/index.d.ts.map +0 -1
@@ -0,0 +1,294 @@
1
+ import { Identifier } from '@did-btcr2/method';
2
+ import { Command, CommanderError } from 'commander';
3
+ import { readFile } from 'fs/promises';
4
+ import Btcr2Command from './command.js';
5
+ import { CLIError } from './error.js';
6
+ const SUPPORTED_NETWORKS = ['bitcoin', 'testnet3', 'testnet4', 'signet', 'mutinynet', 'regtest'];
7
+ /**
8
+ * CLI tool for the did:btcr2 method.
9
+ * @type {DidBtcr2Cli}
10
+ * @class DidBtcr2Cli
11
+ */
12
+ export class DidBtcr2Cli {
13
+ program;
14
+ constructor() {
15
+ this.program = new Command('btcr2')
16
+ .version('btcr2 0.1.0', '-v, --version', 'Output the current version')
17
+ .description('CLI tool for the did:btcr2 method');
18
+ this.configureCommands();
19
+ }
20
+ /**
21
+ * Configures the CLI commands.
22
+ * @returns {void}
23
+ */
24
+ configureCommands() {
25
+ // Create command
26
+ this.program
27
+ .command('create')
28
+ .description('Create an identifier and initial DID document')
29
+ .requiredOption('-t, --type <type>', 'Identifier type <k|x>', 'k')
30
+ .requiredOption('-n, --network <network>', 'Identifier bitcoin network <bitcoin|testnet3|testnet4|signet|mutinynet|regtest>')
31
+ .requiredOption('-b, --bytes <bytes>', 'The genesis bytes used to create a DID and DID document as a hex string. ' +
32
+ 'If type=k, MUST be secp256k1 public key. ' +
33
+ 'If type=x, MUST be SHA-256 hash of a genesis document')
34
+ .action(async (options) => {
35
+ const parsedOptions = this.parseCreateOptions(options);
36
+ const result = await this.invokeCommand({ options: parsedOptions, action: 'create', command: new Btcr2Command() });
37
+ this.printResult(result);
38
+ });
39
+ // Resolve command
40
+ this.program
41
+ .command('resolve')
42
+ .alias('read')
43
+ .description('Resolve the DID document of the identifier.')
44
+ .requiredOption('-i, --identifier <identifier>', 'did:btcr2 identifier')
45
+ .option('-r, --resolutionOptions <resolutionOptions>', 'JSON string containing resolution options')
46
+ .option('-p, --resolutionOptionsPath <resolutionOptionsPath>', 'Path to a JSON file containing resolution options')
47
+ .action(async (options) => {
48
+ const parsedOptions = await this.parseResolveOptions(options);
49
+ const result = await this.invokeCommand({ options: parsedOptions, action: 'resolve', command: new Btcr2Command() });
50
+ this.printResult(result);
51
+ });
52
+ // Update command
53
+ this.program
54
+ .command('update')
55
+ .description('Update a did:btcr2 document.')
56
+ .requiredOption('-i, --identifier <identifier>', 'did:btcr2 identifier')
57
+ .requiredOption('-s, --sourceDocument <sourceDocument>', 'Source DID document as JSON string')
58
+ .requiredOption('-v, --sourceVersionId <sourceVersionId>', 'Source version ID as a number')
59
+ .requiredOption('-p, --patch <patch>', 'JSON Patch operations as a JSON string array')
60
+ .requiredOption('-m, --verificationMethodId <verificationMethodId>', 'Did document verification method ID as a string')
61
+ .requiredOption('-b, --beaconIds <beaconIds>', 'Beacon IDs as a JSON string array')
62
+ .action(async (options) => {
63
+ const parsedOptions = this.parseUpdateOptions(options);
64
+ const result = await this.invokeCommand({ options: parsedOptions, action: 'update', command: new Btcr2Command() });
65
+ this.printResult(result);
66
+ });
67
+ // Deactivate command
68
+ this.program
69
+ .command('deactivate')
70
+ .alias('delete')
71
+ .description('Deactivate the did:btcr2 identifier permanently.')
72
+ .action(async () => {
73
+ const result = await this.invokeCommand({ options: {}, action: 'deactivate', command: new Btcr2Command() });
74
+ this.printResult(result);
75
+ });
76
+ }
77
+ /**
78
+ * Invokes a command with the provided request.
79
+ * @param {object} request The command request
80
+ * @param {any} request.options The command options
81
+ * @param {CommandRequest['action']} request.action The command action
82
+ * @param {Btcr2Command} request.command The command instance
83
+ * @returns {Promise<CommandResult>} The command result
84
+ */
85
+ async invokeCommand(request) {
86
+ return await request.command.execute({ action: request.action, options: request.options });
87
+ }
88
+ /**
89
+ * Runs the CLI with the provided argv or process.argv.
90
+ * @param {string[]} [argv] The argv array to use. Defaults to process.argv.
91
+ */
92
+ async run(argv) {
93
+ try {
94
+ const normalized = this.normalizeArgv(argv ?? process.argv);
95
+ await this.program.parseAsync(normalized, { from: 'node' });
96
+ if (!this.program.args.length)
97
+ this.program.outputHelp();
98
+ }
99
+ catch (error) {
100
+ this.handleError(error);
101
+ }
102
+ }
103
+ /**
104
+ * Normalizes argv to ensure it has at least two elements.
105
+ * @param {string[]} argv The argv array to normalize
106
+ * @returns {string[]} Normalized argv array
107
+ */
108
+ normalizeArgv(argv) {
109
+ if (argv.length >= 2)
110
+ return argv;
111
+ if (argv.length === 1)
112
+ return ['node', argv[0]];
113
+ return ['node', 'btcr2'];
114
+ }
115
+ /**
116
+ * Handles errors thrown during CLI execution.
117
+ * @param {unknown} error The error to handle
118
+ * @returns {void}
119
+ */
120
+ handleError(error) {
121
+ if (error instanceof CommanderError && (error.code === 'commander.helpDisplayed' || error.code === 'commander.help')) {
122
+ return;
123
+ }
124
+ if (error instanceof CLIError) {
125
+ console.error(error.message);
126
+ process.exitCode ??= 1;
127
+ return;
128
+ }
129
+ console.error(error);
130
+ process.exitCode ??= 1;
131
+ }
132
+ /**
133
+ * Parses create command options and throws CLIError on invalid input.
134
+ * @param {object} options The create command options
135
+ * @param {string} options.type The identifier type
136
+ * @param {string} options.network The bitcoin network
137
+ * @param {string} options.bytes The genesis bytes as a hex string
138
+ * @returns {CreateCommandOptions} The parsed create command options
139
+ */
140
+ parseCreateOptions(options) {
141
+ if (!['k', 'x'].includes(options.type)) {
142
+ throw new CLIError('Invalid type. Must be "k" or "x".', 'INVALID_ARGUMENT_ERROR', options);
143
+ }
144
+ if (!this.isNetworkValid(options.network)) {
145
+ throw new CLIError('Invalid network. Must be one of "bitcoin", "testnet3", "testnet4", "signet", "mutinynet", or "regtest".', 'INVALID_ARGUMENT_ERROR', options);
146
+ }
147
+ if (Buffer.from(options.bytes, 'hex').length === 0) {
148
+ throw new CLIError('Invalid bytes. Must be a non-empty hex string.', 'INVALID_ARGUMENT_ERROR', options);
149
+ }
150
+ return {
151
+ type: options.type,
152
+ network: options.network,
153
+ bytes: options.bytes,
154
+ };
155
+ }
156
+ /**
157
+ * Parses resolve command options and throws CLIError on invalid input.
158
+ * @param {object} options The resolve command options
159
+ * @param {string} options.identifier The did:btcr2 identifier
160
+ * @param {string} [options.resolutionOptions] JSON string of resolution options
161
+ * @param {string} [options.resolutionOptionsPath] Path to a JSON file of resolution options
162
+ * @returns {Promise<ResolveCommandOptions>} The parsed resolve command options
163
+ */
164
+ async parseResolveOptions(options) {
165
+ this.validateIdentifier(options.identifier, options);
166
+ const resolutionOptions = await this.parseResolutionOptions(options);
167
+ return {
168
+ identifier: options.identifier,
169
+ ...(resolutionOptions && { options: resolutionOptions }),
170
+ };
171
+ }
172
+ /**
173
+ * Parses update command options and throws CLIError on invalid input.
174
+ * @param {object} options The update command options
175
+ * @param {string} options.identifier The did:btcr2 identifier
176
+ * @param {string} options.sourceDocument The source DID document as a JSON string
177
+ * @param {number} options.sourceVersionId The source version ID as a number
178
+ * @param {string} options.patch The JSON Patch operations as a JSON string array
179
+ * @param {string} options.verificationMethodId The DID document verification method ID as a string
180
+ * @param {string} options.beaconIds The beacon IDs as a JSON string array
181
+ * @returns {UpdateCommandOptions} The parsed update command options
182
+ */
183
+ parseUpdateOptions(options) {
184
+ this.validateIdentifier(options.identifier, options);
185
+ const sourceDocument = this.parseJsonOption(options.sourceDocument, 'Invalid options. Must be a valid JSON string.', options);
186
+ const patch = this.parseJsonOption(options.patch, 'Invalid options. Must be a valid JSON string.', options);
187
+ const beaconIds = this.parseJsonOption(options.beaconIds, 'Invalid options. Must be a valid JSON string.', options);
188
+ return {
189
+ identifier: options.identifier,
190
+ sourceDocument,
191
+ sourceVersionId: Number(options.sourceVersionId),
192
+ patch,
193
+ verificationMethodId: options.verificationMethodId,
194
+ beaconIds,
195
+ };
196
+ }
197
+ /**
198
+ * Parses a JSON option and throws a CLIError on failure.
199
+ * @param {string} value JSON string to parse
200
+ * @param {string} errorMessage Error message to use on failure
201
+ * @param {Record<string, any>} data Additional data to include in the error
202
+ * @returns {T} Parsed JSON object
203
+ */
204
+ parseJsonOption(value, errorMessage, data) {
205
+ try {
206
+ return JSON.parse(value);
207
+ }
208
+ catch {
209
+ throw new CLIError(errorMessage, 'INVALID_ARGUMENT_ERROR', data);
210
+ }
211
+ }
212
+ /**
213
+ * Parses resolution options from JSON string or file path.
214
+ * @param {object} options The options containing resolution options
215
+ * @param {string} [options.resolutionOptions] JSON string of resolution options
216
+ * @param {string} [options.resolutionOptionsPath] Path to a JSON file of resolution options
217
+ * @returns {Promise<any>} The parsed resolution options
218
+ */
219
+ async parseResolutionOptions(options) {
220
+ if (options.resolutionOptions) {
221
+ return this.parseJsonOption(options.resolutionOptions, 'Invalid options. Must be a valid JSON string.', options);
222
+ }
223
+ if (options.resolutionOptionsPath) {
224
+ try {
225
+ const data = await readFile(options.resolutionOptionsPath, 'utf-8');
226
+ return JSON.parse(data);
227
+ }
228
+ catch {
229
+ throw new CLIError('Invalid options path. Must be a valid path to a JSON file.', 'INVALID_ARGUMENT_ERROR', options);
230
+ }
231
+ }
232
+ return undefined;
233
+ }
234
+ /**
235
+ * Validates the did:btcr2 identifier format.
236
+ * @param {string} identifier The identifier to validate
237
+ * @param {Record<string, any>} data Additional data to include in the error
238
+ * @returns {void}
239
+ */
240
+ validateIdentifier(identifier, data) {
241
+ try {
242
+ Identifier.decode(identifier);
243
+ }
244
+ catch {
245
+ throw new CLIError('Invalid identifier. Must be a valid did:btcr2 identifier.', 'INVALID_ARGUMENT_ERROR', data);
246
+ }
247
+ }
248
+ /**
249
+ * Validates if the provided network is supported.
250
+ * @param {string} network The network to validate
251
+ * @returns {boolean} True if the network is valid
252
+ */
253
+ isNetworkValid(network) {
254
+ return SUPPORTED_NETWORKS.includes(network);
255
+ }
256
+ /**
257
+ * Prints the command result to the console.
258
+ * @param {CommandResult} result The command result to print
259
+ * @returns {void}
260
+ */
261
+ printResult(result) {
262
+ switch (result.action) {
263
+ case 'create':
264
+ this.log(result.did);
265
+ break;
266
+ case 'resolve':
267
+ case 'read':
268
+ this.log(result.resolution);
269
+ break;
270
+ case 'update':
271
+ this.log(result.sidecar);
272
+ break;
273
+ case 'deactivate':
274
+ case 'delete':
275
+ this.log(result.message);
276
+ break;
277
+ default:
278
+ this.log(result);
279
+ }
280
+ }
281
+ /**
282
+ * Logs a payload to the console, formatting objects as JSON.
283
+ * @param {unknown} payload The payload to log
284
+ * @returns {void}
285
+ */
286
+ log(payload) {
287
+ if (typeof payload === 'string') {
288
+ console.log(payload);
289
+ return;
290
+ }
291
+ console.log(JSON.stringify(payload, null, 2));
292
+ }
293
+ }
294
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,YAON,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,kBAAkB,GAAoB,CAAC,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC;AAElH;;;;GAIG;AACH,MAAM,OAAO,WAAW;IACN,OAAO,CAAU;IAEjC;QACE,IAAI,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC;aAChC,OAAO,CAAC,aAAa,EAAE,eAAe,EAAE,4BAA4B,CAAC;aACrE,WAAW,CAAC,mCAAmC,CAAC,CAAC;QAEpD,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACK,iBAAiB;QACvB,iBAAiB;QACjB,IAAI,CAAC,OAAO;aACT,OAAO,CAA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C,GAAG,CAAC,OAAO,CAAC,CAAC;YACrB,OAAO;QACT,CAAC;QACD,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAChD,CAAC;CACF"}
@@ -0,0 +1,35 @@
1
+ import { IdentifierTypes, Logger, MethodError } from '@did-btcr2/common';
2
+ import { DidBtcr2 } from '@did-btcr2/method';
3
+ export default class Btcr2Command {
4
+ async execute(request) {
5
+ const action = request.action;
6
+ switch (action) {
7
+ case 'create': {
8
+ const { type, bytes, network } = request.options;
9
+ const idType = type === 'k' ? IdentifierTypes.KEY : IdentifierTypes.EXTERNAL;
10
+ const genesisBytes = Buffer.from(bytes, 'hex');
11
+ const did = await DidBtcr2.create(genesisBytes, { idType, network });
12
+ return { action: 'create', did };
13
+ }
14
+ case 'read':
15
+ case 'resolve': {
16
+ const { identifier, options } = request.options;
17
+ const resolution = await DidBtcr2.resolve(identifier, options);
18
+ return { action: request.action, resolution };
19
+ }
20
+ case 'update': {
21
+ const sidecar = await DidBtcr2.update(request.options);
22
+ return { action: 'update', sidecar };
23
+ }
24
+ case 'delete':
25
+ case 'deactivate': {
26
+ Logger.warn('// TODO: Update once DidBtcr2.deactivate implemented');
27
+ return { action: 'deactivate', message: 'Deactivate not yet implemented' };
28
+ }
29
+ default: {
30
+ throw new MethodError(`Invalid command: ${action}`, 'INVALID_COMMAND');
31
+ }
32
+ }
33
+ }
34
+ }
35
+ //# sourceMappingURL=command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.js","sourceRoot":"","sources":["../../../src/command.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAkB,MAAM,mBAAmB,CAAC;AACzF,OAAO,EAAE,QAAQ,EAA+C,MAAM,mBAAmB,CAAC;AA4C1F,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,KAAK,CAAC,OAAO,CAAC,OAAuB;QACn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AAY,EAAE,OAAO,EAAE,gCAAgC,EAAE,CAAC;YAC7E,CAAC;YACD,OAAO,CAAC,CAAA,CAAC;gBACP,MAAM,IAAI,WAAW,CAAC,oBAAoB,MAAM,EAAE,EAAE,iBAAiB,CAAC,CAAC;YACzE,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,10 @@
1
+ import { DidMethodError } from '@did-btcr2/common';
2
+ /**
3
+ * Custom CLI Error class extending DidMethodError.
4
+ */
5
+ export class CLIError extends DidMethodError {
6
+ constructor(message, type = 'CLIError', data) {
7
+ super(message, { type, name: type, data });
8
+ }
9
+ }
10
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../../../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,cAAc;IAC1C,YAAY,OAAe,EAAE,OAAe,UAAU,EAAE,IAA0B;QAChF,KAAK,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7C,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ export * from './cli.js';
2
+ export * from './command.js';
3
+ export * from './error.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=btcr2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"btcr2.d.ts","sourceRoot":"","sources":["../../../bin/btcr2.ts"],"names":[],"mappings":""}
@@ -1,31 +1,112 @@
1
- import { DidMethodError } from '@did-btcr2/common';
1
+ import { Command } from 'commander';
2
2
  /**
3
- * Custom CLI Error class extending DidMethodError.
4
- */
5
- export declare class CLIError extends DidMethodError {
6
- constructor(message: string, type?: string, data?: Record<string, any>);
7
- }
8
- /**
9
- * A class-based CLI using Commander.
10
- * - No forced process.exit().
11
- * - Configurable by calling `run(argv?)`.
3
+ * CLI tool for the did:btcr2 method.
4
+ * @type {DidBtcr2Cli}
5
+ * @class DidBtcr2Cli
12
6
  */
13
7
  export declare class DidBtcr2Cli {
14
- private CLI;
8
+ readonly program: Command;
15
9
  constructor();
16
10
  /**
17
- * Configure the CLI commands and options.
18
- * @private
11
+ * Configures the CLI commands.
12
+ * @returns {void}
19
13
  */
20
14
  private configureCommands;
21
15
  /**
22
- * A helper to invoke the command logic without forcibly exiting.
16
+ * Invokes a command with the provided request.
17
+ * @param {object} request The command request
18
+ * @param {any} request.options The command options
19
+ * @param {CommandRequest['action']} request.action The command action
20
+ * @param {Btcr2Command} request.command The command instance
21
+ * @returns {Promise<CommandResult>} The command result
23
22
  */
24
23
  private invokeCommand;
25
24
  /**
26
- * Parse and run the CLI.
25
+ * Runs the CLI with the provided argv or process.argv.
26
+ * @param {string[]} [argv] The argv array to use. Defaults to process.argv.
27
+ */
28
+ run(argv?: string[]): Promise<void>;
29
+ /**
30
+ * Normalizes argv to ensure it has at least two elements.
31
+ * @param {string[]} argv The argv array to normalize
32
+ * @returns {string[]} Normalized argv array
33
+ */
34
+ private normalizeArgv;
35
+ /**
36
+ * Handles errors thrown during CLI execution.
37
+ * @param {unknown} error The error to handle
38
+ * @returns {void}
39
+ */
40
+ private handleError;
41
+ /**
42
+ * Parses create command options and throws CLIError on invalid input.
43
+ * @param {object} options The create command options
44
+ * @param {string} options.type The identifier type
45
+ * @param {string} options.network The bitcoin network
46
+ * @param {string} options.bytes The genesis bytes as a hex string
47
+ * @returns {CreateCommandOptions} The parsed create command options
48
+ */
49
+ private parseCreateOptions;
50
+ /**
51
+ * Parses resolve command options and throws CLIError on invalid input.
52
+ * @param {object} options The resolve command options
53
+ * @param {string} options.identifier The did:btcr2 identifier
54
+ * @param {string} [options.resolutionOptions] JSON string of resolution options
55
+ * @param {string} [options.resolutionOptionsPath] Path to a JSON file of resolution options
56
+ * @returns {Promise<ResolveCommandOptions>} The parsed resolve command options
57
+ */
58
+ private parseResolveOptions;
59
+ /**
60
+ * Parses update command options and throws CLIError on invalid input.
61
+ * @param {object} options The update command options
62
+ * @param {string} options.identifier The did:btcr2 identifier
63
+ * @param {string} options.sourceDocument The source DID document as a JSON string
64
+ * @param {number} options.sourceVersionId The source version ID as a number
65
+ * @param {string} options.patch The JSON Patch operations as a JSON string array
66
+ * @param {string} options.verificationMethodId The DID document verification method ID as a string
67
+ * @param {string} options.beaconIds The beacon IDs as a JSON string array
68
+ * @returns {UpdateCommandOptions} The parsed update command options
69
+ */
70
+ private parseUpdateOptions;
71
+ /**
72
+ * Parses a JSON option and throws a CLIError on failure.
73
+ * @param {string} value JSON string to parse
74
+ * @param {string} errorMessage Error message to use on failure
75
+ * @param {Record<string, any>} data Additional data to include in the error
76
+ * @returns {T} Parsed JSON object
77
+ */
78
+ private parseJsonOption;
79
+ /**
80
+ * Parses resolution options from JSON string or file path.
81
+ * @param {object} options The options containing resolution options
82
+ * @param {string} [options.resolutionOptions] JSON string of resolution options
83
+ * @param {string} [options.resolutionOptionsPath] Path to a JSON file of resolution options
84
+ * @returns {Promise<any>} The parsed resolution options
85
+ */
86
+ private parseResolutionOptions;
87
+ /**
88
+ * Validates the did:btcr2 identifier format.
89
+ * @param {string} identifier The identifier to validate
90
+ * @param {Record<string, any>} data Additional data to include in the error
91
+ * @returns {void}
92
+ */
93
+ private validateIdentifier;
94
+ /**
95
+ * Validates if the provided network is supported.
96
+ * @param {string} network The network to validate
97
+ * @returns {boolean} True if the network is valid
98
+ */
99
+ private isNetworkValid;
100
+ /**
101
+ * Prints the command result to the console.
102
+ * @param {CommandResult} result The command result to print
103
+ * @returns {void}
104
+ */
105
+ private printResult;
106
+ /**
107
+ * Logs a payload to the console, formatting objects as JSON.
108
+ * @param {unknown} payload The payload to log
109
+ * @returns {void}
27
110
  */
28
- run(argv?: string[]): void;
111
+ private log;
29
112
  }
30
- declare const _default: void;
31
- export default _default;
@@ -1,14 +1,55 @@
1
+ import { DidResolutionResult } from '@web5/dids';
2
+ import { PatchOperation } from '@did-btcr2/common';
3
+ import { DidDocument, ResolutionOptions, SidecarData } from '@did-btcr2/method';
4
+ export type NetworkOption = 'bitcoin' | 'testnet3' | 'testnet4' | 'signet' | 'mutinynet' | 'regtest';
1
5
  export interface CommandInterface {
2
- execute(params: {
3
- options?: any;
4
- action?: string;
5
- }): Promise<void>;
6
+ execute(request: CommandRequest): Promise<CommandResult>;
6
7
  }
7
- interface ExecuteParams {
8
- options?: any;
9
- action?: string;
8
+ export interface CreateCommandOptions {
9
+ type: 'k' | 'x';
10
+ bytes: string;
11
+ network: NetworkOption;
10
12
  }
13
+ export interface ResolveCommandOptions {
14
+ identifier: string;
15
+ options?: ResolutionOptions;
16
+ }
17
+ export interface UpdateCommandOptions {
18
+ identifier: string;
19
+ sourceDocument: DidDocument;
20
+ sourceVersionId: number;
21
+ patch: PatchOperation[];
22
+ verificationMethodId: string;
23
+ beaconIds: string[];
24
+ }
25
+ export interface DeactivateCommandOptions {
26
+ }
27
+ export type CommandRequest = {
28
+ action: 'create';
29
+ options: CreateCommandOptions;
30
+ } | {
31
+ action: 'resolve' | 'read';
32
+ options: ResolveCommandOptions;
33
+ } | {
34
+ action: 'update';
35
+ options: UpdateCommandOptions;
36
+ } | {
37
+ action: 'deactivate' | 'delete';
38
+ options: DeactivateCommandOptions;
39
+ };
40
+ export type CommandResult = {
41
+ action: 'create';
42
+ did: string;
43
+ } | {
44
+ action: 'resolve' | 'read';
45
+ resolution: DidResolutionResult;
46
+ } | {
47
+ action: 'update';
48
+ sidecar: SidecarData;
49
+ } | {
50
+ action: 'deactivate' | 'delete';
51
+ message: string;
52
+ };
11
53
  export default class Btcr2Command implements CommandInterface {
12
- execute({ options, action }: ExecuteParams): Promise<void>;
54
+ execute(request: CommandRequest): Promise<CommandResult>;
13
55
  }
14
- export {};
@@ -0,0 +1,7 @@
1
+ import { DidMethodError } from '@did-btcr2/common';
2
+ /**
3
+ * Custom CLI Error class extending DidMethodError.
4
+ */
5
+ export declare class CLIError extends DidMethodError {
6
+ constructor(message: string, type?: string, data?: Record<string, any>);
7
+ }
@@ -1,2 +1,3 @@
1
1
  export * from './cli.js';
2
2
  export * from './command.js';
3
+ export * from './error.js';