Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d897be2
feat: add identity init to connector cli
sebbi08 Jan 10, 2025
a92a402
feat: add identity init to connector cli
sebbi08 Jan 10, 2025
984e297
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 10, 2025
3b38657
test: add test for identity init
sebbi08 Jan 10, 2025
174deb9
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 14, 2025
5911663
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 14, 2025
f8d5d20
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 15, 2025
ccb92e0
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 15, 2025
e79aba9
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 16, 2025
0c20118
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 24, 2025
e8cd502
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 27, 2025
f5b3a79
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 27, 2025
c127c3f
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 28, 2025
094f6af
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 28, 2025
c66ccb9
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 29, 2025
67c8288
Merge branch 'main' into feature/add_identity_init_cli_command
mergify[bot] Jan 29, 2025
631db13
Merge branch 'main' into feature/add_identity_init_cli_command
sebbi08 Jul 3, 2025
e6d2bf7
improve identity detection logic
sebbi08 Jul 3, 2025
bf475a2
chore: potential test fix
sebbi08 Jul 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cli/BaseCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApplicationError } from "@js-soft/ts-utils";
import _ from "lodash";
import yargs from "yargs";
import { ConnectorRuntime } from "../ConnectorRuntime";
import { ConnectorRuntimeConfig } from "../ConnectorRuntimeConfig";
Expand Down Expand Up @@ -60,11 +61,11 @@ export abstract class BaseCommand {
}
}

protected async createRuntime(): Promise<void> {
protected async createRuntime(allowIdentityCreation = false): Promise<void> {
if (this.#cliRuntime) return;
if (!this.#connectorConfig) throw new Error("Connector config not initialized");

this.#cliRuntime = await ConnectorRuntime.create(this.#connectorConfig);
this.#cliRuntime = await ConnectorRuntime.create(_.merge(this.#connectorConfig, { transportLibrary: { allowIdentityCreation } }));
await this.#cliRuntime.start();
}

Expand Down
30 changes: 30 additions & 0 deletions src/cli/commands/identity/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { CoreError } from "@nmshd/core-types";
import { CommandModule } from "yargs";
import { BaseCommand, ConfigFileOptions, configOptionBuilder } from "../../BaseCommand";

export const identityInitHandler = async ({ config }: ConfigFileOptions): Promise<void> => {
await new IdentityInit().run(config);
};
export const yargsIdentityInitCommand: CommandModule<{}, ConfigFileOptions> = {
command: "init",
describe: "initialize the identity",
handler: identityInitHandler,
builder: configOptionBuilder
};

export default class IdentityInit extends BaseCommand {
protected async runInternal(): Promise<void> {
try {
await this.createRuntime(false);
this.log.log("Identity already exists not creating a new one.");
} catch (error: unknown) {
if (error instanceof CoreError && error.code === "error.transport.general.noIdentityFound") {
await this.createRuntime(true);
const address = (await this.cliRuntime.getServices().transportServices.account.getIdentityInfo()).value.address;
this.log.log(`Identity with address (${address}) created successfully.`);
} else {
throw error;
}
}
}
}
1 change: 1 addition & 0 deletions src/cli/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./identity/init";
export * from "./identity/status";
export * from "./identityDeletion/cancel";
export * from "./identityDeletion/init";
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { startConnectorCommand, yargsIdentityDeletionCancelCommand, yargsIdentityDeletionInitCommand, yargsIdentityStatusCommand } from "./cli/commands";
import { startConnectorCommand, yargsIdentityDeletionCancelCommand, yargsIdentityDeletionInitCommand, yargsIdentityInitCommand, yargsIdentityStatusCommand } from "./cli/commands";

yargs(hideBin(process.argv))
.command({
command: "identity [command]",
describe: "Identity related commands",
builder: (yargs) => {
return yargs.command(yargsIdentityStatusCommand);
return yargs.command(yargsIdentityStatusCommand).command(yargsIdentityInitCommand);
},
handler: () => {
yargs.showHelp("log");
Expand Down
33 changes: 33 additions & 0 deletions test/modules/cli/identity/IdentityInit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { identityInitHandler } from "../../../../dist/cli/commands";
import { resetDB, setupEnvironment } from "../setup";

describe("identity init", () => {
const identityCreatedPattern = /Identity with address did:e:((([A-Za-z0-9]+(-[A-Za-z0-9]+)*)\.)+[a-z]{2,}|localhost):dids:[0-9a-f]{22} created successfully\./;

beforeAll(() => {
setupEnvironment();
});

afterAll(async () => {
await resetDB();
});

afterEach(() => {
jest.resetAllMocks();
});

test("identity should be created", async () => {
const consoleSpy = jest.spyOn(console, "log");
await identityInitHandler({ config: undefined });
expect(consoleSpy.mock.lastCall![0]).toMatch(identityCreatedPattern);
expect(consoleSpy).toHaveBeenCalledTimes(1);
});

test("identity should not be created when one already exists", async () => {
const consoleSpy = jest.spyOn(console, "log");
await identityInitHandler({ config: undefined });
await identityInitHandler({ config: undefined });
expect(consoleSpy).toHaveBeenCalledWith("Identity already created!");
expect(consoleSpy).toHaveBeenCalledTimes(2);
});
});
5 changes: 3 additions & 2 deletions test/modules/cli/identity/identityStatus.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { sleep } from "@js-soft/ts-utils";
import { identityDeletionInitHandler, identityStatusHandler } from "../../../../dist/cli/commands";
import { identityDeletionInitHandler, identityInitHandler, identityStatusHandler } from "../../../../dist/cli/commands";
import { resetDB, setupEnvironment } from "../setup";

describe("Identity status", () => {
const identityStatusPattern = /Identity Address: did:e:((([A-Za-z0-9]+(-[A-Za-z0-9]+)*)\.)+[a-z]{2,}|localhost):dids:[0-9a-f]{22}/;

beforeAll(() => {
beforeAll(async () => {
setupEnvironment();
await identityInitHandler({});
});

afterAll(async () => {
Expand Down
5 changes: 3 additions & 2 deletions test/modules/cli/identityDeletion/identityDeletion.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { identityDeletionCancelHandler, identityDeletionInitHandler } from "../../../../dist/cli/commands";
import { identityDeletionCancelHandler, identityDeletionInitHandler, identityInitHandler } from "../../../../dist/cli/commands";
import { resetDB, setupEnvironment } from "../setup";

describe("Identity deletion", () => {
beforeAll(() => {
beforeAll(async () => {
setupEnvironment();
await identityInitHandler({});
});

afterAll(async () => {
Expand Down
Loading