Skip to content

Commit fbd6173

Browse files
committed
added identity module
1 parent 8f33f88 commit fbd6173

8 files changed

+144
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ lerna-debug.log*
3636
!.vscode/launch.json
3737
!.vscode/extensions.json
3838
.env
39+
.env.archive
3940
secret.json
4041
pdfs
4142
data

src/app.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SchemaModule } from './schema/schema.module';
99
import { TemplateModule } from './template/template.module';
1010
import { CertificateModule } from './certificate/certificate.module';
1111
import { PrismaModule } from 'src/prisma/prisma.module';
12+
import { IdentityModule } from './identity/identity.module';
1213
import configuration from './config/configuration';
1314

1415
@Module({
@@ -23,6 +24,7 @@ import configuration from './config/configuration';
2324
TemplateModule,
2425
CertificateModule,
2526
PrismaModule,
27+
IdentityModule,
2628
],
2729
controllers: [AppController, SchemaController],
2830
providers: [AppService],

src/identity/config.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"content": [
3+
{
4+
"alsoKnownAs": [
5+
"C4GT",
6+
"https://www.codeforgovtech.in/"
7+
],
8+
"services": [
9+
{
10+
"id": "C4GT",
11+
"type": "IdentityHub",
12+
"serviceEndpoint": {
13+
"@context": "schema.c4gt.acknowledgment",
14+
"@type": "UserServiceEndpoint",
15+
"instance": [
16+
"https://www.codeforgovtech.in"
17+
]
18+
}
19+
}
20+
],
21+
"method": "C4GT"
22+
}
23+
]
24+
}
25+
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { IdentityController } from './identity.controller';
3+
4+
describe('IdentityController', () => {
5+
let controller: IdentityController;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
controllers: [IdentityController],
10+
}).compile();
11+
12+
controller = module.get<IdentityController>(IdentityController);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(controller).toBeDefined();
17+
});
18+
});

src/identity/identity.controller.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Controller, Post, Get, Body, Param } from '@nestjs/common';
2+
import { IdentityService } from './identity.service';
3+
4+
@Controller('identity')
5+
export class IdentityController {
6+
constructor(private readonly identityService: IdentityService) {}
7+
8+
@Post('generate')
9+
async generateIdentity() {
10+
return this.identityService.generateIdentity();
11+
}
12+
13+
@Get('resolve/:did')
14+
async resolveIdentity(@Param('did') did: string) {
15+
return this.identityService.resolveIdentity(did);
16+
}
17+
}

src/identity/identity.module.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from '@nestjs/common';
2+
import { IdentityService } from './identity.service';
3+
import { IdentityController } from './identity.controller';
4+
import { HttpModule } from '@nestjs/axios';
5+
6+
@Module({
7+
imports: [HttpModule],
8+
providers: [IdentityService],
9+
controllers: [IdentityController]
10+
})
11+
export class IdentityModule {}

src/identity/identity.service.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { IdentityService } from './identity.service';
3+
4+
describe('IdentityService', () => {
5+
let service: IdentityService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [IdentityService],
10+
}).compile();
11+
12+
service = module.get<IdentityService>(IdentityService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});

src/identity/identity.service.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { HttpService } from '@nestjs/axios';
3+
import { ConfigService } from '@nestjs/config';
4+
import axios from 'axios';
5+
import * as fs from 'fs';
6+
import * as path from 'path';
7+
8+
interface RCWIdentityConfig {
9+
baseUrl: string;
10+
}
11+
12+
@Injectable()
13+
export class IdentityService {
14+
private readonly config;
15+
16+
constructor(
17+
private readonly configService: ConfigService,
18+
private readonly httpService: HttpService
19+
) {
20+
this.config = this.configService.get<RCWIdentityConfig>('identityService');
21+
}
22+
23+
async generateIdentity() {
24+
try {
25+
const configPath = path.resolve(__dirname, '../config.json');
26+
const requestBody = JSON.parse(fs.readFileSync(configPath, 'utf8'));
27+
28+
const generateUrl = `${this.config.baseUrl}/did/generate`;
29+
30+
const response = await this.httpService.axiosRef.post(generateUrl, requestBody, {
31+
headers: { 'Content-Type': 'application/json' },
32+
});
33+
return response.data;
34+
} catch (error) {
35+
console.error('Error generating identity:', error.message);
36+
throw error.response?.data || error.message;
37+
}
38+
}
39+
40+
async resolveIdentity(did: string) {
41+
try {
42+
const url = `${this.config.baseUrl}/did/resolve/${did}`;
43+
const response = await this.httpService.axiosRef.get(url, {
44+
headers: { 'Content-Type': 'application/json' },
45+
});
46+
return response.data;
47+
} catch (error) {
48+
console.error('Error resolving identity:', error.message);
49+
throw error.response?.data || error.message;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)