Skip to content
Snippets Groups Projects
Commit 7c96c9c3 authored by Berend Sliedrecht's avatar Berend Sliedrecht
Browse files

feat(ssi): schemas module


Signed-off-by: default avatarBerend Sliedrecht <berend@animo.id>
parent 887661b7
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !15. Comments created here will be created in the context of that merge request.
......@@ -21,6 +21,7 @@
"test:e2e": "jest --config ./test/jest.config.js"
},
"dependencies": {
"@aries-framework/anoncreds": "0.4.2",
"@aries-framework/core": "0.4.2",
"@aries-framework/tenants": "^0.4.2",
"@elastic/ecs-winston-format": "^1.5.0",
......
import type { BaseEventInput } from './baseEvents.js';
import type { AnonCredsSchema } from '@aries-framework/anoncreds';
import { BaseEvent } from './baseEvents.js';
export type EventAnonCredsSchemasGetAllInput = BaseEventInput;
export class EventAnonCredsSchemasGetAll extends BaseEvent<
Array<AnonCredsSchema>
> {
public static token = 'anoncreds.schemas.getAll';
public get instance() {
return this.data;
}
public static fromEvent(e: EventAnonCredsSchemasGetAll) {
return new EventAnonCredsSchemasGetAll(
e.data,
e.tenantId,
e.id,
e.type,
e.timestamp,
);
}
}
export type EventAnonCredsSchemasGetByIdInput = BaseEventInput<{
schemaId: string;
}>;
export class EventAnonCredsSchemasGetById extends BaseEvent<AnonCredsSchema | null> {
public static token = 'anoncreds.schemas.getById';
public get instance() {
return this.data;
}
public static fromEvent(e: EventAnonCredsSchemasGetById) {
return new EventAnonCredsSchemasGetById(
e.data,
e.tenantId,
e.id,
e.type,
e.timestamp,
);
}
}
export type EventAnonCredsSchemasRegisterInput = BaseEventInput<{
issuerDid: string;
name: string;
version: string;
attributeNames: Array<string>;
}>;
export class EventAnonCredsSchemasRegister extends BaseEvent<AnonCredsSchema> {
public static token = 'anoncreds.schemas.register';
public get instance() {
return this.data;
}
public static fromEvent(e: EventAnonCredsSchemasRegister) {
return new EventAnonCredsSchemasRegister(
e.data,
e.tenantId,
e.id,
e.type,
e.timestamp,
);
}
}
......@@ -7,3 +7,4 @@ export * from './logging/logAxiosError.js';
export * from './events/connectionEvents.js';
export * from './events/didEvents.js';
export * from './events/tenantEvents.js';
export * from './events/schemaEvents.js';
import type { AnonCredsSchema } from '@aries-framework/anoncreds';
import { Test } from '@nestjs/testing';
import { mockConfigModule } from '../../../config/__tests__/mockConfig.js';
import { AgentModule } from '../../agent.module.js';
import { SchemasController } from '../schemas.controller.js';
import { SchemasService } from '../schemas.service.js';
describe('ConnectionsController', () => {
let schemasController: SchemasController;
let schemasService: SchemasService;
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
imports: [mockConfigModule(), AgentModule],
controllers: [SchemasController],
providers: [SchemasService],
}).compile();
schemasService = moduleRef.get(SchemasService);
schemasController = moduleRef.get(SchemasController);
});
describe('get all', () => {
it('should get all the registered schemas of the agent', async () => {
const result: Array<AnonCredsSchema> = [];
jest.spyOn(schemasService, 'getAll').mockResolvedValue(result);
const event = await schemasController.getAll({
tenantId: 'some-id',
});
expect(event.data).toStrictEqual(result);
});
});
describe('get by id', () => {
it('should get a schema by id', async () => {
const result: AnonCredsSchema | null = null;
jest.spyOn(schemasService, 'getById').mockResolvedValue(result);
const event = await schemasController.getById({
schemaId: 'id',
tenantId: 'some-id',
});
expect(event.data).toStrictEqual(result);
});
});
describe('register schema', () => {
it('should register a schema on a ledger', async () => {
const result: AnonCredsSchema = {
name: 'schema-name',
version: '1.0',
issuerId: 'did:indy:123',
attrNames: ['name', 'age'],
};
jest.spyOn(schemasService, 'register').mockResolvedValue(result);
const event = await schemasController.register({
tenantId: 'some-id',
version: '1.0',
name: 'schema-name',
issuerDid: 'did:indy:123',
attributeNames: ['name', 'age'],
});
expect(event.data).toStrictEqual(result);
});
});
});
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import {
EventAnonCredsSchemasGetAll,
EventAnonCredsSchemasGetAllInput,
EventAnonCredsSchemasGetById,
EventAnonCredsSchemasGetByIdInput,
EventAnonCredsSchemasRegister,
EventAnonCredsSchemasRegisterInput,
} from '@ocm/shared';
import { SchemasService } from './schemas.service.js';
@Controller('schemas')
export class SchemasController {
public constructor(private schemasService: SchemasService) {}
@MessagePattern(EventAnonCredsSchemasGetAll.token)
public async getAll(
options: EventAnonCredsSchemasGetAllInput,
): Promise<EventAnonCredsSchemasGetAll> {
return new EventAnonCredsSchemasGetAll(
await this.schemasService.getAll(options),
options.tenantId,
);
}
@MessagePattern(EventAnonCredsSchemasGetById.token)
public async getById(
options: EventAnonCredsSchemasGetByIdInput,
): Promise<EventAnonCredsSchemasGetById> {
return new EventAnonCredsSchemasGetById(
await this.schemasService.getById(options),
options.tenantId,
);
}
@MessagePattern(EventAnonCredsSchemasRegister.token)
public async register(
options: EventAnonCredsSchemasRegisterInput,
): Promise<EventAnonCredsSchemasRegister> {
return new EventAnonCredsSchemasRegister(
await this.schemasService.register(options),
options.tenantId,
);
}
}
import { Module } from '@nestjs/common';
import { AgentModule } from '../agent.module.js';
import { SchemasController } from './schemas.controller.js';
import { SchemasService } from './schemas.service.js';
@Module({
imports: [AgentModule],
providers: [SchemasService],
controllers: [SchemasController],
})
export class SchemasModule {}
import type { AnonCredsSchema } from '@aries-framework/anoncreds';
import type { IndyVdrRegisterSchemaOptions } from '@aries-framework/indy-vdr';
import type {
EventAnonCredsSchemasGetAllInput,
EventAnonCredsSchemasGetByIdInput,
EventAnonCredsSchemasRegisterInput,
} from '@ocm/shared';
import { Injectable } from '@nestjs/common';
import { WithTenantService } from '../withTenantService.js';
@Injectable()
export class SchemasService {
public withTenantService: WithTenantService;
public constructor(withTenantService: WithTenantService) {
this.withTenantService = withTenantService;
}
public async getAll({
tenantId,
}: EventAnonCredsSchemasGetAllInput): Promise<Array<AnonCredsSchema>> {
return this.withTenantService.invoke(tenantId, async (t) =>
(await t.modules.anoncreds.getCreatedSchemas({})).map((r) => r.schema),
);
}
public async getById({
tenantId,
schemaId,
}: EventAnonCredsSchemasGetByIdInput): Promise<AnonCredsSchema | null> {
return this.withTenantService.invoke(tenantId, async (t) => {
const { schema } = await t.modules.anoncreds.getSchema(schemaId);
return schema ?? null;
});
}
public async register({
tenantId,
name,
version,
issuerDid,
attributeNames,
}: EventAnonCredsSchemasRegisterInput): Promise<AnonCredsSchema> {
return this.withTenantService.invoke(tenantId, async (t) => {
const { schemaState } =
await t.modules.anoncreds.registerSchema<IndyVdrRegisterSchemaOptions>({
schema: {
version,
name,
issuerId: issuerDid,
attrNames: attributeNames,
},
options: {
endorserMode: 'external',
endorserDid: issuerDid,
},
});
if (schemaState.state !== 'finished') {
throw new Error(
`Error registering schema: ${
schemaState.state === 'failed' ? schemaState.reason : 'Not Finished'
}`,
);
}
return schemaState.schema;
});
}
}
......@@ -682,6 +682,9 @@ importers:
 
apps/shared:
dependencies:
'@aries-framework/anoncreds':
specifier: 0.4.2
version: 0.4.2(expo@49.0.18)(react-native@0.72.7)
'@aries-framework/core':
specifier: 0.4.2
version: 0.4.2(expo@49.0.18)(react-native@0.72.7)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment