Skip to content

Commit 5342f6a

Browse files
committed
feat: adjust keypair generation and error handling in KeygenService
- Added OnApplicationBootstrap lifecycle method to check for existing keypair and generate a new one if not present. - Enhanced generateKey method to handle keypair creation errors more effectively.
1 parent 817a9ee commit 5342f6a

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

libs/contract/constants/errors/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,9 @@ export const ERRORS = {
443443
message: 'LIMITED and EXPIRED statuses are not allowed to be set manually.',
444444
httpCode: 400,
445445
},
446+
KEYPAIR_CREATION_ERROR: {
447+
code: 'A090',
448+
message: 'Keypair creation error',
449+
httpCode: 500,
450+
},
446451
} as const;

src/modules/keygen/commands/get-node-jwt/get-node-jwt.handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export class GetNodeJwtHandler
4242
expiresIn: '9999d',
4343
});
4444

45+
// TODO: remove this after testing
46+
this.logger.log(`Node JWT: ${token}, signed with private key: \n ${privKey}`);
47+
4548
try {
4649
return {
4750
isOk: true,

src/modules/keygen/keygen.service.ts

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { generateKeyPair } from 'crypto';
22
import { promisify } from 'util';
33

4-
import { Injectable, Logger } from '@nestjs/common';
4+
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
55

66
import { ICommandResponse } from '@common/types/command-response.type';
77
import { ERRORS } from '@libs/contracts/constants/errors';
@@ -12,11 +12,31 @@ import { KeygenEntity } from './entities/keygen.entity';
1212
const generateKeyPairAsync = promisify(generateKeyPair);
1313

1414
@Injectable()
15-
export class KeygenService {
15+
export class KeygenService implements OnApplicationBootstrap {
1616
private readonly logger = new Logger(KeygenService.name);
1717

1818
constructor(private readonly keygenRepository: KeygenRepository) {}
1919

20+
async onApplicationBootstrap() {
21+
const pubKey = await this.keygenRepository.findFirstByCriteria({});
22+
23+
if (pubKey) {
24+
this.logger.log('Keypair already exists, skipping...');
25+
return;
26+
}
27+
28+
const result = await this.generateKey();
29+
30+
if (!result.isOk) {
31+
this.logger.error('Failed to generate keypair, please restart application.');
32+
return;
33+
}
34+
35+
this.logger.log('Keypair generated successfully.');
36+
37+
return;
38+
}
39+
2040
public async generateKey(): Promise<ICommandResponse<KeygenEntity>> {
2141
try {
2242
const pubKey = await this.keygenRepository.findFirstByCriteria({});
@@ -28,6 +48,30 @@ export class KeygenService {
2848
};
2949
}
3050

51+
const newEntity = await this.createKeypair();
52+
53+
if (!newEntity) {
54+
return {
55+
isOk: false,
56+
...ERRORS.KEYPAIR_CREATION_ERROR,
57+
};
58+
}
59+
60+
return {
61+
isOk: true,
62+
response: newEntity,
63+
};
64+
} catch (error) {
65+
this.logger.error(error);
66+
return {
67+
isOk: false,
68+
...ERRORS.GET_PUBLIC_KEY_ERROR,
69+
};
70+
}
71+
}
72+
73+
private async createKeypair(): Promise<KeygenEntity | null> {
74+
try {
3175
const { privateKey, publicKey } = await generateKeyPairAsync('rsa', {
3276
modulusLength: 2048,
3377
publicKeyEncoding: {
@@ -47,16 +91,10 @@ export class KeygenService {
4791

4892
const newEntity = await this.keygenRepository.create(keygenEntity);
4993

50-
return {
51-
isOk: true,
52-
response: newEntity,
53-
};
94+
return newEntity;
5495
} catch (error) {
5596
this.logger.error(error);
56-
return {
57-
isOk: false,
58-
...ERRORS.GET_PUBLIC_KEY_ERROR,
59-
};
97+
return null;
6098
}
6199
}
62100
}

0 commit comments

Comments
 (0)