@did-btcr2/cli 0.3.1 → 0.4.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.
- package/LICENSE +373 -1
- package/dist/cjs/cli.js +56 -264
- package/dist/cjs/cli.js.map +1 -1
- package/dist/cjs/commands/create.js +49 -0
- package/dist/cjs/commands/create.js.map +1 -0
- package/dist/cjs/commands/deactivate.js +41 -0
- package/dist/cjs/commands/deactivate.js.map +1 -0
- package/dist/cjs/commands/index.js +5 -0
- package/dist/cjs/commands/index.js.map +1 -0
- package/dist/cjs/commands/resolve.js +59 -0
- package/dist/cjs/commands/resolve.js.map +1 -0
- package/dist/cjs/commands/update.js +39 -0
- package/dist/cjs/commands/update.js.map +1 -0
- package/dist/cjs/error.js +1 -1
- package/dist/cjs/error.js.map +1 -1
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/output.js +16 -0
- package/dist/cjs/output.js.map +1 -0
- package/dist/cjs/types.js +4 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/version.js +22 -0
- package/dist/cjs/version.js.map +1 -0
- package/dist/esm/src/cli.js +56 -264
- package/dist/esm/src/cli.js.map +1 -1
- package/dist/esm/src/commands/create.js +49 -0
- package/dist/esm/src/commands/create.js.map +1 -0
- package/dist/esm/src/commands/deactivate.js +41 -0
- package/dist/esm/src/commands/deactivate.js.map +1 -0
- package/dist/esm/src/commands/index.js +5 -0
- package/dist/esm/src/commands/index.js.map +1 -0
- package/dist/esm/src/commands/resolve.js +59 -0
- package/dist/esm/src/commands/resolve.js.map +1 -0
- package/dist/esm/src/commands/update.js +39 -0
- package/dist/esm/src/commands/update.js.map +1 -0
- package/dist/esm/src/error.js +1 -1
- package/dist/esm/src/error.js.map +1 -1
- package/dist/esm/src/index.js +4 -1
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/output.js +16 -0
- package/dist/esm/src/output.js.map +1 -0
- package/dist/esm/src/types.js +4 -0
- package/dist/esm/src/types.js.map +1 -0
- package/dist/esm/src/version.js +22 -0
- package/dist/esm/src/version.js.map +1 -0
- package/dist/types/cli.d.ts +7 -98
- package/dist/types/commands/create.d.ts +3 -0
- package/dist/types/commands/deactivate.d.ts +3 -0
- package/dist/types/commands/index.d.ts +4 -0
- package/dist/types/commands/resolve.d.ts +3 -0
- package/dist/types/commands/update.d.ts +3 -0
- package/dist/types/index.d.ts +4 -1
- package/dist/types/output.d.ts +10 -0
- package/dist/types/src/cli.d.ts +7 -98
- package/dist/types/src/cli.d.ts.map +1 -1
- package/dist/types/src/commands/create.d.ts +4 -0
- package/dist/types/src/commands/create.d.ts.map +1 -0
- package/dist/types/src/commands/deactivate.d.ts +4 -0
- package/dist/types/src/commands/deactivate.d.ts.map +1 -0
- package/dist/types/src/commands/index.d.ts +5 -0
- package/dist/types/src/commands/index.d.ts.map +1 -0
- package/dist/types/src/commands/resolve.d.ts +4 -0
- package/dist/types/src/commands/resolve.d.ts.map +1 -0
- package/dist/types/src/commands/update.d.ts +4 -0
- package/dist/types/src/commands/update.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/output.d.ts +11 -0
- package/dist/types/src/output.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +54 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/version.d.ts +2 -0
- package/dist/types/src/version.d.ts.map +1 -0
- package/dist/types/types.d.ts +53 -0
- package/dist/types/version.d.ts +1 -0
- package/package.json +6 -7
- package/src/cli.ts +67 -347
- package/src/commands/create.ts +88 -0
- package/src/commands/deactivate.ts +71 -0
- package/src/commands/index.ts +4 -0
- package/src/commands/resolve.ts +89 -0
- package/src/commands/update.ts +73 -0
- package/src/error.ts +2 -2
- package/src/index.ts +4 -1
- package/src/output.ts +17 -0
- package/src/types.ts +61 -0
- package/src/version.ts +21 -0
- package/dist/cjs/command.js +0 -35
- package/dist/cjs/command.js.map +0 -1
- package/dist/esm/src/command.js +0 -35
- package/dist/esm/src/command.js.map +0 -1
- package/dist/types/command.d.ts +0 -55
- package/dist/types/src/command.d.ts +0 -56
- package/dist/types/src/command.d.ts.map +0 -1
- package/src/command.ts +0 -77
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a CommandResult for console output.
|
|
3
|
+
* In 'json' mode, the full result is serialized.
|
|
4
|
+
* In 'text' mode, only the relevant payload is printed.
|
|
5
|
+
* @param {CommandResult} result - The result to format.
|
|
6
|
+
* @param {GlobalOptions} options - The global options to determine output format.
|
|
7
|
+
* @returns {string} - The formatted output string.
|
|
8
|
+
*/
|
|
9
|
+
export function formatResult(result, options) {
|
|
10
|
+
if (options.output === 'json') {
|
|
11
|
+
return JSON.stringify(result, null, 2);
|
|
12
|
+
}
|
|
13
|
+
const { data } = result;
|
|
14
|
+
return typeof data === 'string' ? data : JSON.stringify(data, null, 2);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output.js","sourceRoot":"","sources":["../../src/output.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,MAAqB,EAAE,OAAsB;IACxE,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IACzC,CAAC;IACD,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;IACxB,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACzE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAQA,MAAM,CAAC,MAAM,kBAAkB,GAAoB;IACjD,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS;CACpE,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
/**
|
|
5
|
+
* Walks up from the compiled file location to find the CLI package.json.
|
|
6
|
+
* Works from both dist/esm/ and tests/compiled/src/.
|
|
7
|
+
*/
|
|
8
|
+
function readVersion() {
|
|
9
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
for (let i = 0; i < 5; i++) {
|
|
11
|
+
try {
|
|
12
|
+
const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf-8'));
|
|
13
|
+
if (pkg.name === '@did-btcr2/cli')
|
|
14
|
+
return pkg.version;
|
|
15
|
+
}
|
|
16
|
+
catch { /* not found, go up */ }
|
|
17
|
+
dir = dirname(dir);
|
|
18
|
+
}
|
|
19
|
+
return '0.0.0';
|
|
20
|
+
}
|
|
21
|
+
export const VERSION = readVersion();
|
|
22
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC;;;GAGG;AACH,SAAS,WAAW;IAClB,IAAI,GAAG,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAClD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;YACzE,IAAI,GAAG,CAAC,IAAI,KAAK,gBAAgB;gBAAE,OAAO,GAAG,CAAC,OAAO,CAAC;QACxD,CAAC;QAAC,MAAM,CAAC,CAAC,sBAAsB,CAAC,CAAC;QAClC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACrB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,CAAC,MAAM,OAAO,GAAW,WAAW,EAAE,CAAC"}
|
package/dist/esm/src/cli.js
CHANGED
|
@@ -1,291 +1,83 @@
|
|
|
1
|
-
import { Identifier } from '@did-btcr2/method';
|
|
2
1
|
import { Command, CommanderError } from 'commander';
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
2
|
+
import { DidBtcr2 } from '@did-btcr2/method';
|
|
3
|
+
import { registerCreateCommand, registerDeactivateCommand, registerResolveCommand, registerUpdateCommand, } from './commands/index.js';
|
|
5
4
|
import { CLIError } from './error.js';
|
|
6
|
-
|
|
5
|
+
import { VERSION } from './version.js';
|
|
6
|
+
/** Default MethodOperations delegating to DidBtcr2 static methods. */
|
|
7
|
+
const defaultOps = {
|
|
8
|
+
create: (genesisBytes, options) => DidBtcr2.create(genesisBytes, options),
|
|
9
|
+
resolve: (identifier, options) => DidBtcr2.resolve(identifier, options),
|
|
10
|
+
update: (params) => DidBtcr2.update(params),
|
|
11
|
+
};
|
|
7
12
|
/**
|
|
8
13
|
* CLI tool for the did:btcr2 method.
|
|
9
|
-
* @type {DidBtcr2Cli}
|
|
10
|
-
* @class DidBtcr2Cli
|
|
11
14
|
*/
|
|
12
15
|
export class DidBtcr2Cli {
|
|
13
16
|
program;
|
|
14
|
-
|
|
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
|
-
}
|
|
17
|
+
ops;
|
|
77
18
|
/**
|
|
78
|
-
*
|
|
79
|
-
* @param {
|
|
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
|
|
19
|
+
* Initializes the CLI with optional custom MethodOperations.
|
|
20
|
+
* @param {MethodOperations} ops - Custom operations for create, resolve, and update.
|
|
84
21
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
22
|
+
constructor(ops = defaultOps) {
|
|
23
|
+
this.ops = ops;
|
|
24
|
+
this.program = new Command('btcr2')
|
|
25
|
+
.version(`btcr2 ${VERSION}`, '-v, --version', 'Output the current version')
|
|
26
|
+
.description('CLI tool for the did:btcr2 method')
|
|
27
|
+
.option('-o, --output <format>', 'Output format <json|text>', 'text')
|
|
28
|
+
.option('--verbose', 'Verbose output', false)
|
|
29
|
+
.option('--quiet', 'Suppress non-essential output', false);
|
|
30
|
+
const globals = () => this.program.opts();
|
|
31
|
+
registerCreateCommand(this.program, this.ops, globals);
|
|
32
|
+
registerResolveCommand(this.program, this.ops, globals);
|
|
33
|
+
registerUpdateCommand(this.program, this.ops, globals);
|
|
34
|
+
registerDeactivateCommand(this.program, this.ops, globals);
|
|
87
35
|
}
|
|
88
36
|
/**
|
|
89
37
|
* Runs the CLI with the provided argv or process.argv.
|
|
90
|
-
* @param {string[]} [argv]
|
|
38
|
+
* @param {string[]} [argv] - Optional array of command-line arguments.
|
|
39
|
+
* @returns {Promise<void>} - Resolves when execution is complete.
|
|
91
40
|
*/
|
|
92
41
|
async run(argv) {
|
|
93
42
|
try {
|
|
94
|
-
const normalized =
|
|
43
|
+
const normalized = normalizeArgv(argv ?? process.argv);
|
|
95
44
|
await this.program.parseAsync(normalized, { from: 'node' });
|
|
96
45
|
if (!this.program.args.length)
|
|
97
46
|
this.program.outputHelp();
|
|
98
47
|
}
|
|
99
48
|
catch (error) {
|
|
100
|
-
|
|
49
|
+
handleError(error);
|
|
101
50
|
}
|
|
102
51
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return ['node',
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Normalizes argv to ensure it has at least two elements.
|
|
55
|
+
* @param {string[]} argv - The original argv array.
|
|
56
|
+
* @returns {string[]} - The normalized argv array.
|
|
57
|
+
*/
|
|
58
|
+
function normalizeArgv(argv) {
|
|
59
|
+
if (argv.length >= 2)
|
|
60
|
+
return argv;
|
|
61
|
+
if (argv.length === 1)
|
|
62
|
+
return ['node', argv[0]];
|
|
63
|
+
return ['node', 'btcr2'];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Handles errors thrown during CLI execution.
|
|
67
|
+
* @param {unknown} error - The error to handle.
|
|
68
|
+
* @returns {void}
|
|
69
|
+
*/
|
|
70
|
+
function handleError(error) {
|
|
71
|
+
if (error instanceof CommanderError &&
|
|
72
|
+
(error.code === 'commander.helpDisplayed' || error.code === 'commander.help')) {
|
|
73
|
+
return;
|
|
114
74
|
}
|
|
115
|
-
|
|
116
|
-
|
|
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);
|
|
75
|
+
if (error instanceof CLIError) {
|
|
76
|
+
console.error(error.message);
|
|
130
77
|
process.exitCode ??= 1;
|
|
78
|
+
return;
|
|
131
79
|
}
|
|
132
|
-
|
|
133
|
-
|
|
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.sourceDocument The source DID document as a JSON string
|
|
176
|
-
* @param {number} options.sourceVersionId The source version ID as a number
|
|
177
|
-
* @param {string} options.patches The JSON Patch operations as a JSON string array
|
|
178
|
-
* @param {string} options.verificationMethodId The DID document verification method ID as a string
|
|
179
|
-
* @param {string} options.beaconId The beacon IDs as a JSON string array
|
|
180
|
-
* @returns {UpdateCommandOptions} The parsed update command options
|
|
181
|
-
*/
|
|
182
|
-
parseUpdateOptions(options) {
|
|
183
|
-
const sourceDocument = this.parseJsonOption(options.sourceDocument, 'Invalid options. Must be a valid JSON string.', options);
|
|
184
|
-
const patches = this.parseJsonOption(options.patches, 'Invalid options. Must be a valid JSON string.', options);
|
|
185
|
-
const beaconId = this.parseJsonOption(options.beaconId, 'Invalid options. Must be a valid JSON string.', options);
|
|
186
|
-
return {
|
|
187
|
-
sourceDocument,
|
|
188
|
-
sourceVersionId: Number(options.sourceVersionId),
|
|
189
|
-
patches,
|
|
190
|
-
verificationMethodId: options.verificationMethodId,
|
|
191
|
-
beaconId,
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Parses a JSON option and throws a CLIError on failure.
|
|
196
|
-
* @param {string} value JSON string to parse
|
|
197
|
-
* @param {string} errorMessage Error message to use on failure
|
|
198
|
-
* @param {Record<string, any>} data Additional data to include in the error
|
|
199
|
-
* @returns {T} Parsed JSON object
|
|
200
|
-
*/
|
|
201
|
-
parseJsonOption(value, errorMessage, data) {
|
|
202
|
-
try {
|
|
203
|
-
return JSON.parse(value);
|
|
204
|
-
}
|
|
205
|
-
catch {
|
|
206
|
-
throw new CLIError(errorMessage, 'INVALID_ARGUMENT_ERROR', data);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Parses resolution options from JSON string or file path.
|
|
211
|
-
* @param {object} options The options containing resolution options
|
|
212
|
-
* @param {string} [options.resolutionOptions] JSON string of resolution options
|
|
213
|
-
* @param {string} [options.resolutionOptionsPath] Path to a JSON file of resolution options
|
|
214
|
-
* @returns {Promise<any>} The parsed resolution options
|
|
215
|
-
*/
|
|
216
|
-
async parseResolutionOptions(options) {
|
|
217
|
-
if (options.resolutionOptions) {
|
|
218
|
-
return this.parseJsonOption(options.resolutionOptions, 'Invalid options. Must be a valid JSON string.', options);
|
|
219
|
-
}
|
|
220
|
-
if (options.resolutionOptionsPath) {
|
|
221
|
-
try {
|
|
222
|
-
const data = await readFile(options.resolutionOptionsPath, 'utf-8');
|
|
223
|
-
return JSON.parse(data);
|
|
224
|
-
}
|
|
225
|
-
catch {
|
|
226
|
-
throw new CLIError('Invalid options path. Must be a valid path to a JSON file.', 'INVALID_ARGUMENT_ERROR', options);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return undefined;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Validates the did:btcr2 identifier format.
|
|
233
|
-
* @param {string} identifier The identifier to validate
|
|
234
|
-
* @param {Record<string, any>} data Additional data to include in the error
|
|
235
|
-
* @returns {void}
|
|
236
|
-
*/
|
|
237
|
-
validateIdentifier(identifier, data) {
|
|
238
|
-
try {
|
|
239
|
-
Identifier.decode(identifier);
|
|
240
|
-
}
|
|
241
|
-
catch {
|
|
242
|
-
throw new CLIError('Invalid identifier. Must be a valid did:btcr2 identifier.', 'INVALID_ARGUMENT_ERROR', data);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Validates if the provided network is supported.
|
|
247
|
-
* @param {string} network The network to validate
|
|
248
|
-
* @returns {boolean} True if the network is valid
|
|
249
|
-
*/
|
|
250
|
-
isNetworkValid(network) {
|
|
251
|
-
return SUPPORTED_NETWORKS.includes(network);
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Prints the command result to the console.
|
|
255
|
-
* @param {CommandResult} result The command result to print
|
|
256
|
-
* @returns {void}
|
|
257
|
-
*/
|
|
258
|
-
printResult(result) {
|
|
259
|
-
switch (result.action) {
|
|
260
|
-
case 'create':
|
|
261
|
-
this.log(result.did);
|
|
262
|
-
break;
|
|
263
|
-
case 'resolve':
|
|
264
|
-
case 'read':
|
|
265
|
-
this.log(result.resolution);
|
|
266
|
-
break;
|
|
267
|
-
case 'update':
|
|
268
|
-
this.log(result.signed);
|
|
269
|
-
break;
|
|
270
|
-
case 'deactivate':
|
|
271
|
-
case 'delete':
|
|
272
|
-
this.log(result.message);
|
|
273
|
-
break;
|
|
274
|
-
default:
|
|
275
|
-
this.log(result);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Logs a payload to the console, formatting objects as JSON.
|
|
280
|
-
* @param {unknown} payload The payload to log
|
|
281
|
-
* @returns {void}
|
|
282
|
-
*/
|
|
283
|
-
log(payload) {
|
|
284
|
-
if (typeof payload === 'string') {
|
|
285
|
-
console.log(payload);
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
console.log(JSON.stringify(payload, null, 2));
|
|
289
|
-
}
|
|
80
|
+
console.error(error);
|
|
81
|
+
process.exitCode ??= 1;
|
|
290
82
|
}
|
|
291
83
|
//# sourceMappingURL=cli.js.map
|
package/dist/esm/src/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EACL,qBAAqB,EACrB,yBAAyB,EACzB,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,sEAAsE;AACtE,MAAM,UAAU,GAAqB;IACnC,MAAM,EAAI,CAAC,YAAY,EAAE,OAAO,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC;IAC3E,OAAO,EAAG,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC;IACxE,M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AU,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;YAC5D,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM;gBAAE,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;QAC3D,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACxB,WAAW,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;IACH,CAAC;CACF;AAED;;;;GAIG;AACH,SAAS,aAAa,CAAC,IAAc;IACnC,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAClC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC3B,CAAC;AAED;;;;GAIG;AACH,SAAS,WAAW,CAAC,KAAc;IACjC,IACE,KAAK,YAAY,cAAc;QAC/B,CAAC,KAAK,CAAC,IAAI,KAAK,yBAAyB,IAAI,KAAK,CAAC,IAAI,KAAK,gBAAgB,CAAC,EAC7E,CAAC;QACD,OAAO;IACT,CAAC;IACD,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;QAC9B,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC7B,OAAO,CAAC,QAAQ,KAAK,CAAC,CAAC;QACvB,OAAO;IACT,CAAC;IACD,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACrB,OAAO,CAAC,QAAQ,KAAK,CAAC,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { IdentifierTypes } from '@did-btcr2/common';
|
|
2
|
+
import { CLIError } from '../error.js';
|
|
3
|
+
import { formatResult } from '../output.js';
|
|
4
|
+
import { SUPPORTED_NETWORKS, } from '../types.js';
|
|
5
|
+
/** Expected byte length per identifier type: compressed secp256k1 = 33, SHA-256 hash = 32. */
|
|
6
|
+
const EXPECTED_BYTES = {
|
|
7
|
+
k: { length: 33, label: 'secp256k1 compressed public key (33 bytes)' },
|
|
8
|
+
x: { length: 32, label: 'SHA-256 hash (32 bytes)' },
|
|
9
|
+
};
|
|
10
|
+
export function registerCreateCommand(program, ops, globals) {
|
|
11
|
+
program
|
|
12
|
+
.command('create')
|
|
13
|
+
.description('Create an identifier and initial DID document')
|
|
14
|
+
.requiredOption('-t, --type <type>', 'Identifier type <k|x>', 'k')
|
|
15
|
+
.requiredOption('-n, --network <network>', 'Identifier bitcoin network <bitcoin|testnet3|testnet4|signet|mutinynet|regtest>')
|
|
16
|
+
.requiredOption('-b, --bytes <bytes>', 'Genesis bytes as a hex string. ' +
|
|
17
|
+
'If type=k, MUST be secp256k1 public key. ' +
|
|
18
|
+
'If type=x, MUST be SHA-256 hash of a genesis document')
|
|
19
|
+
.action(async (options) => {
|
|
20
|
+
const parsed = validateCreateOptions(options);
|
|
21
|
+
const idType = parsed.type === 'k' ? IdentifierTypes.KEY : IdentifierTypes.EXTERNAL;
|
|
22
|
+
const genesisBytes = Buffer.from(parsed.bytes, 'hex');
|
|
23
|
+
const data = ops.create(genesisBytes, { idType, network: parsed.network });
|
|
24
|
+
const result = { action: 'create', data };
|
|
25
|
+
console.log(formatResult(result, globals()));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function validateCreateOptions(options) {
|
|
29
|
+
if (!['k', 'x'].includes(options.type)) {
|
|
30
|
+
throw new CLIError('Invalid type. Must be "k" or "x".', 'INVALID_ARGUMENT_ERROR', options);
|
|
31
|
+
}
|
|
32
|
+
if (!SUPPORTED_NETWORKS.includes(options.network)) {
|
|
33
|
+
throw new CLIError('Invalid network. Must be one of "bitcoin", "testnet3", "testnet4", "signet", "mutinynet", or "regtest".', 'INVALID_ARGUMENT_ERROR', options);
|
|
34
|
+
}
|
|
35
|
+
const buf = Buffer.from(options.bytes, 'hex');
|
|
36
|
+
if (buf.length === 0) {
|
|
37
|
+
throw new CLIError('Invalid bytes. Must be a non-empty hex string.', 'INVALID_ARGUMENT_ERROR', options);
|
|
38
|
+
}
|
|
39
|
+
const expected = EXPECTED_BYTES[options.type];
|
|
40
|
+
if (buf.length !== expected.length) {
|
|
41
|
+
throw new CLIError(`Invalid bytes length for type="${options.type}": expected ${expected.label}, got ${buf.length} bytes.`, 'INVALID_ARGUMENT_ERROR', options);
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
type: options.type,
|
|
45
|
+
network: options.network,
|
|
46
|
+
bytes: options.bytes,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../../../../src/commands/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAKL,kBAAkB,GACnB,MAAM,aAAa,CAAC;AAErB,8FAA8F;AAC9F,MAAM,cAAc,GAAyD;IAC3E,CAAC,EAAG,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,4CAA4C,EAAE;IACvE,CAAC,EAAG,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,yBAAyB,EAAE;CACrD,CAAC;AAEF,MAAM,UAAU,qBAAqB,CACnC,OAAiB,EACjB,GAA0B,EAC1B,OAA6B;IAE7B,OAAO;SACJ,OAAO,CAAC,QAAQ,CAAC;SACjB,WAAW,CAAC,+CAA+C,CAAC;SAC5D,cAAc,CAAC,mBAAmB,EAAE,uBAAuB,EAAE,GAAG,CAAC;SACjE,cAAc,CACb,yBAAyB,EACzB,iFAAiF,CAClF;SACA,cAAc,CACb,qBAAqB,EACrB,iCAAiC;QACjC,2CAA2C;QAC3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AQ,CAChB,gDAAgD,EAChD,wBAAwB,EACxB,OAAO,CACR,CAAC;IACJ,CAAC;IACD,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,CAAC,IAAiB,CAAC,CAAC;IAC3D,IAAI,GAAG,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;QACnC,MAAM,IAAI,QAAQ,CAChB,kCAAkC,OAAO,CAAC,IAAI,eAAe,QAAQ,CAAC,KAAK,SAAS,GAAG,CAAC,MAAM,SAAS,EACvG,wBAAwB,EACxB,OAAO,CACR,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAM,OAAO,CAAC,IAAiB;QACnC,OAAO,EAAG,OAAO,CAAC,OAAwB;QAC1C,KAAK,EAAK,OAAO,CAAC,KAAK;KACxB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { CLIError } from '../error.js';
|
|
2
|
+
import { formatResult } from '../output.js';
|
|
3
|
+
/** The JSON Patch that marks a DID document as permanently deactivated. */
|
|
4
|
+
const DEACTIVATION_PATCH = [{ op: 'add', path: '/deactivated', value: true }];
|
|
5
|
+
export function registerDeactivateCommand(program, ops, globals) {
|
|
6
|
+
program
|
|
7
|
+
.command('deactivate')
|
|
8
|
+
.alias('delete')
|
|
9
|
+
.description('Deactivate the did:btcr2 identifier permanently. This is irreversible.')
|
|
10
|
+
.requiredOption('-s, --source-document <json>', 'Current DID document as JSON string', parseJsonArg('--source-document'))
|
|
11
|
+
.requiredOption('--source-version-id <number>', 'Current version ID of the DID document')
|
|
12
|
+
.requiredOption('-m, --verification-method-id <id>', 'DID document verification method ID used to sign the deactivation')
|
|
13
|
+
.requiredOption('-b, --beacon-id <json>', 'Beacon ID as a JSON string', parseJsonArg('--beacon-id'))
|
|
14
|
+
.action(async (options) => {
|
|
15
|
+
const parsed = {
|
|
16
|
+
sourceDocument: options.sourceDocument,
|
|
17
|
+
patches: DEACTIVATION_PATCH,
|
|
18
|
+
sourceVersionId: Number(options.sourceVersionId),
|
|
19
|
+
verificationMethodId: options.verificationMethodId,
|
|
20
|
+
beaconId: options.beaconId,
|
|
21
|
+
};
|
|
22
|
+
const data = await ops.update(parsed);
|
|
23
|
+
const result = { action: 'deactivate', data };
|
|
24
|
+
console.log(formatResult(result, globals()));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns a commander argParser that validates JSON.
|
|
29
|
+
* Errors at parse time with a clear flag reference.
|
|
30
|
+
*/
|
|
31
|
+
function parseJsonArg(flagName) {
|
|
32
|
+
return (value) => {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(value);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
throw new CLIError(`Invalid JSON for ${flagName}. Must be a valid JSON string.`, 'INVALID_ARGUMENT_ERROR', { flagName, value });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=deactivate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deactivate.js","sourceRoot":"","sources":["../../../../src/commands/deactivate.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,2EAA2E;AAC3E,MAAM,kBAAkB,GAAG,CAAC,EAAE,EAAE,EAAE,KAAc,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAEvF,MAAM,UAAU,yBAAyB,CACvC,OAAiB,EACjB,GAA0B,EAC1B,OAA6B;IAE7B,OAAO;SACJ,OAAO,CAAC,YAAY,CAAC;SACrB,KAAK,CAAC,QAAQ,CAAC;SACf,WAAW,CAAC,wEAAwE,CAAC;SACrF,cAAc,CACb,8BAA8B,EAC9B,qCAAqC,EACrC,YAAY,CAAC,mBAAmB,CAAC,CAClC;SACA,cAAc,CACb,8BAA8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,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Identifier } from '@did-btcr2/method';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { CLIError } from '../error.js';
|
|
4
|
+
import { formatResult } from '../output.js';
|
|
5
|
+
export function registerResolveCommand(program, ops, globals) {
|
|
6
|
+
program
|
|
7
|
+
.command('resolve')
|
|
8
|
+
.alias('read')
|
|
9
|
+
.description('Resolve the DID document of the identifier.')
|
|
10
|
+
.requiredOption('-i, --identifier <identifier>', 'did:btcr2 identifier')
|
|
11
|
+
.option('-r, --resolution-options <json>', 'JSON string containing resolution options')
|
|
12
|
+
.option('-p, --resolution-options-path <path>', 'Path to a JSON file containing resolution options')
|
|
13
|
+
.action(async (options) => {
|
|
14
|
+
const parsed = await validateResolveOptions(options);
|
|
15
|
+
const data = await ops.resolve(parsed.identifier, parsed.options);
|
|
16
|
+
const result = { action: 'resolve', data };
|
|
17
|
+
console.log(formatResult(result, globals()));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async function validateResolveOptions(options) {
|
|
21
|
+
validateIdentifier(options.identifier, options);
|
|
22
|
+
const resolutionOptions = await parseResolutionOptions(options);
|
|
23
|
+
return {
|
|
24
|
+
identifier: options.identifier,
|
|
25
|
+
...(resolutionOptions && { options: resolutionOptions }),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function validateIdentifier(identifier, data) {
|
|
29
|
+
try {
|
|
30
|
+
Identifier.decode(identifier);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
throw new CLIError('Invalid identifier. Must be a valid did:btcr2 identifier.', 'INVALID_ARGUMENT_ERROR', data);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function parseJson(value, errorMessage, data) {
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(value);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
throw new CLIError(errorMessage, 'INVALID_ARGUMENT_ERROR', data);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async function parseResolutionOptions(options) {
|
|
45
|
+
if (options.resolutionOptions) {
|
|
46
|
+
return parseJson(options.resolutionOptions, 'Invalid resolution options. Must be a valid JSON string.', options);
|
|
47
|
+
}
|
|
48
|
+
if (options.resolutionOptionsPath) {
|
|
49
|
+
try {
|
|
50
|
+
const data = await readFile(options.resolutionOptionsPath, 'utf-8');
|
|
51
|
+
return JSON.parse(data);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new CLIError('Invalid resolution options path. Must be a valid path to a JSON file.', 'INVALID_ARGUMENT_ERROR', options);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve.js","sourceRoot":"","sources":["../../../../src/commands/resolve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,MAAM,UAAU,sBAAsB,CACpC,OAAiB,EACjB,GAA0B,EAC1B,OAA6B;IAE7B,OAAO;SACJ,OAAO,CAAC,SAAS,CAAC;SAClB,KAAK,CAAC,MAAM,CAAC;SACb,WAAW,CAAC,6CAA6C,CAAC;SAC1D,cAAc,CAAC,+BAA+B,EAAE,sBAAsB,CAAC;SACvE,MAAM,CAAC,iCAAiC,EAAE,2CAA2C,CAAC;SACtF,MAAM,CAAC,sCAAsC,EAAE,mDAAmD,CAAC;SACnG,MAAM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