Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilise Eddsa & Public Keys - Part 1 #791

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
39 changes: 28 additions & 11 deletions examples/bri-3/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from '@prisma/client';
import { PrismaClient, PublicKeyType } from '@prisma/client';
import { type } from 'os';

const prisma = new PrismaClient();

Expand All @@ -23,17 +24,33 @@ async function main() {

await prisma.bpiSubject.create({
data: {
name: 'BpiAdmin',
description: 'Internal Bpi Subject of this Bpi',
publicKey: '0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe',
// private key 0x0fbdb56ab0fecb2f406fa807d9e6558baedacc1c15c0e2703b77d4c08441e4fe, used for testing purposes only
loginNonce: '',
roles: {
connect: {
id: internalBpiSubjectRole.id
},
name: 'BpiAdmin',
description: 'Internal Bpi Subject of this Bpi',
publicKeys: {
createMany: {
data: [
{
type: PublicKeyType.ECDSA,
value:
'0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe',
},
{
type: PublicKeyType.EDDSA,
value:
'0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe',
},
],
},
}
},

// private key 0x0fbdb56ab0fecb2f406fa807d9e6558baedacc1c15c0e2703b77d4c08441e4fe, used for testing purposes only
loginNonce: '',
roles: {
connect: {
id: internalBpiSubjectRole.id,
},
},
},
});
}
main()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class CreateBpiMessageCommandHandler
this.authAgent.throwIfSignatureVerificationFails(
command.content,
command.signature,
fromBpiSubject.publicKey,
fromBpiSubject.publicKeys[0].value,
);

const newBpiMessageCandidate = this.agent.createNewBpiMessage(
Expand All @@ -47,7 +47,7 @@ export class CreateBpiMessageCommandHandler
);

await this.messagingAgent.publishMessage(
toBpiSubject.publicKey,
toBpiSubject.publicKeys[0].value,
this.messagingAgent.serializeBpiMessage(newBpiMessage),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ProcessInboundMessageCommandHandler
const isSignatureValid = this.authAgent.verifySignatureAgainstPublicKey(
command.content,
command.signature,
fromBpiSubject.publicKey,
fromBpiSubject.publicKeys[0].value,
);

if (!isSignatureValid) {
Expand All @@ -55,7 +55,7 @@ export class ProcessInboundMessageCommandHandler
);

await this.messagingAgent.publishMessage(
toBpiSubject.publicKey,
toBpiSubject.publicKeys[0].value,
this.messagingAgent.serializeBpiMessage(newBpiMessage),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from '../api/err.messages';
import { BpiSubjectStorageAgent } from './bpiSubjectsStorage.agent';
import { BpiSubjectRoleName } from '../models/bpiSubjectRole';
import { PublicKeyDto } from '../api/dtos/request/publicKey.dto';
import { PublicKey, PublicKeyType } from '../models/publicKey';

// Agent methods have extremely declarative names and perform a single task
@Injectable()
Expand All @@ -27,12 +29,36 @@ export class BpiSubjectAgent {
public async createNewExternalBpiSubject(
name: string,
description: string,
publicKey: string,
publicKeys: PublicKeyDto[],
): Promise<BpiSubject> {
const externalRole = await this.storageAgent.getBpiSubjectRoleByName(
BpiSubjectRoleName.EXTERNAL_BPI_SUBJECT,
);
return new BpiSubject(v4(), name, description, publicKey, [externalRole]);

const bpiSubjectId = v4();

const publicKeyCandidates = publicKeys.map((key) => {
let publicKeyType;

switch (key.type.toLowerCase()) {
case 'ecdsa':
publicKeyType = PublicKeyType.ECDSA;
break;
case 'eddsa':
publicKeyType = PublicKeyType.EDDSA;
break;
default:
}
return new PublicKey(v4(), publicKeyType, key.value, bpiSubjectId);
});

return new BpiSubject(
bpiSubjectId,
name,
description,
publicKeyCandidates,
[externalRole],
);
}

public async fetchUpdateCandidateAndThrowIfUpdateValidationFails(
Expand Down Expand Up @@ -64,11 +90,9 @@ export class BpiSubjectAgent {
bpiSubjectToUpdate: BpiSubject,
name: string,
description: string,
publicKey: string,
) {
bpiSubjectToUpdate.updateName(name);
bpiSubjectToUpdate.updateDescription(description);
bpiSubjectToUpdate.updatePublicKey(publicKey);
}

public async fetchDeleteCandidateAndThrowIfDeleteValidationFails(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ export class BpiSubjectStorageAgent extends PrismaService {
};
}),
},
publicKey: {
connect: bpiSubject.publicKeys.map((pk) => {
return {
id: pk.id,
};
}),
publicKeys: {
createMany: { data: bpiSubject.publicKeys },
},
},
});
Expand All @@ -131,7 +127,7 @@ export class BpiSubjectStorageAgent extends PrismaService {
};
}),
},
publicKey: {
publicKeys: {
connect: bpiSubject.publicKeys.map((pk) => {
return {
id: pk.id,
Expand All @@ -152,14 +148,14 @@ export class BpiSubjectStorageAgent extends PrismaService {
async getBpiSubjectByPublicKey(publicKeyValue: string): Promise<BpiSubject> {
const bpiSubjectModel = await this.prisma.bpiSubject.findFirst({
where: {
publicKey: {
publicKeys: {
some: {
value: publicKeyValue,
},
},
},
include: {
publicKey: true,
publicKeys: true,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { CreateBpiSubjectDto } from './createBpiSubject.dto';
describe('CreateBpiSubjectDto', () => {
it('should return error in case name not provided.', async () => {
// Arrange
const dto = { desc: 'this is a description', publicKey: '2323' };
const dto = {
desc: 'this is a description',
publicKeys: [{ type: 'ecdsa', value: '214324' }],
};
const createBpiSubjectDto = plainToInstance(CreateBpiSubjectDto, dto);

// Act
Expand All @@ -22,7 +25,10 @@ describe('CreateBpiSubjectDto', () => {

it('should return error in case desc not provided.', async () => {
// Arrange
const dto = { name: 'test', publicKey: '2323' };
const dto = {
name: 'test',
publicKeys: [{ type: 'ecdsa', value: '214324' }],
};
const createBpiSubjectDto = plainToInstance(CreateBpiSubjectDto, dto);

// Act
Expand All @@ -46,9 +52,9 @@ describe('CreateBpiSubjectDto', () => {

// Assert
expect(errors.length).toBe(1);
expect(errors[0].property).toEqual('publicKey');
expect(errors[0].property).toEqual('publicKeys');
expect(errors[0].constraints?.isNotEmpty).toContain(
'publicKey ' + SHOULD_NOT_BE_EMPTY_VALIDATION_MESSAGE,
'publicKeys ' + SHOULD_NOT_BE_EMPTY_VALIDATION_MESSAGE,
);
});

Expand All @@ -57,7 +63,7 @@ describe('CreateBpiSubjectDto', () => {
const dto = {
name: 'test',
desc: 'this is a description',
publicKey: '2323',
publicKeys: [{ type: 'ecdsa', value: '214324' }],
};
const createBpiSubjectDto = plainToInstance(CreateBpiSubjectDto, dto);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IsNotEmpty } from 'class-validator';
import { PublicKeyDto } from './publicKey.dto';

export class CreateBpiSubjectDto {
@IsNotEmpty()
Expand All @@ -8,5 +9,5 @@ export class CreateBpiSubjectDto {
desc: string;

@IsNotEmpty()
publicKey: string;
publicKeys: PublicKeyDto[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IsNotEmpty } from 'class-validator';
export class PublicKeyDto {
@IsNotEmpty()
type: string;

@IsNotEmpty()
value: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { UpdateBpiSubjectDto } from './updateBpiSubject.dto';
describe('UpdateBpiSubjectDto', () => {
it('should return error in case name not provided.', async () => {
// Arrange
const dto = { desc: 'this is a description', publicKey: '2323' };
const dto = {
desc: 'this is a description',
publicKeys: [{ type: 'ecdsa', value: '214324' }],
};
const updateBpiSubjectDto = plainToInstance(UpdateBpiSubjectDto, dto);

// Act
Expand All @@ -22,7 +25,10 @@ describe('UpdateBpiSubjectDto', () => {

it('should return error in case desc not provided.', async () => {
// Arrange
const dto = { name: 'test', publicKey: '2323' };
const dto = {
name: 'test',
publicKeys: [{ type: 'ecdsa', value: '214324' }],
};
const updateBpiSubjectDto = plainToInstance(UpdateBpiSubjectDto, dto);

// Act
Expand All @@ -46,9 +52,9 @@ describe('UpdateBpiSubjectDto', () => {

// Assert
expect(errors.length).toBe(1);
expect(errors[0].property).toEqual('publicKey');
expect(errors[0].property).toEqual('publicKeys');
expect(errors[0].constraints?.isNotEmpty).toContain(
'publicKey ' + SHOULD_NOT_BE_EMPTY_VALIDATION_MESSAGE,
'publicKeys ' + SHOULD_NOT_BE_EMPTY_VALIDATION_MESSAGE,
);
});

Expand All @@ -57,7 +63,7 @@ describe('UpdateBpiSubjectDto', () => {
const dto = {
name: 'test',
desc: 'this is a description',
publicKey: '2323',
publicKeys: [{ type: 'ecdsa', value: '214324' }],
};
const updateBpiSubjectDto = plainToInstance(UpdateBpiSubjectDto, dto);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IsNotEmpty } from 'class-validator';
import { PublicKeyDto } from './publicKey.dto';

export class UpdateBpiSubjectDto {
@IsNotEmpty()
Expand All @@ -8,5 +9,5 @@ export class UpdateBpiSubjectDto {
desc: string;

@IsNotEmpty()
publicKey: string;
publicKeys: PublicKeyDto[];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AutoMap } from '@automapper/classes';
import { BpiSubjectRoleDto } from './bpiSubjectRole.dto';
import { PublicKeyDto } from '../request/publicKey.dto';

export class BpiSubjectDto {
@AutoMap()
Expand All @@ -12,7 +13,7 @@ export class BpiSubjectDto {
description: string;

@AutoMap()
publicKey: string;
publicKeys: PublicKeyDto[];

@AutoMap(() => [BpiSubjectRoleDto])
roles: BpiSubjectRoleDto[];
Expand Down
Loading
Loading