Skip to content

Commit

Permalink
Identity-service:Interface implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwin275 committed Dec 13, 2024
1 parent 1c4cacf commit aa5b102
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 67 deletions.
6 changes: 3 additions & 3 deletions services/identity-service/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { AuthGuard } from './auth/auth.guard';
import { TerminusModule } from '@nestjs/terminus';
import { PrismaHealthIndicator } from './utils/prisma.health';
import { VaultHealthIndicator } from './utils/vault.health';
import { AnchorCordService } from './utils/cord.service';

import { AnchorCordService } from './did/implementations/anchor-cord.service';
import { BlockchainAnchorFactory } from './did/factories/blockchain-anchor.factory';
@Module({
imports: [
DidModule,
Expand All @@ -27,7 +27,7 @@ import { AnchorCordService } from './utils/cord.service';
],
controllers: [AppController, DidController],
providers: [
PrismaService, DidService, VaultService,AnchorCordService,
PrismaService, DidService, VaultService,BlockchainAnchorFactory,AnchorCordService,
{
provide: APP_GUARD,
useClass: AuthGuard,
Expand Down
4 changes: 2 additions & 2 deletions services/identity-service/src/did/did.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { InternalServerErrorException } from '@nestjs/common';
import { DidService } from './did.service';
import { PrismaService } from '../utils/prisma.service';
import { VaultService } from '../utils/vault.service';
import { AnchorCordService } from 'src/utils/cord.service';
import { ConfigService } from '@nestjs/config';

describe('DidController', () => {
Expand Down Expand Up @@ -39,7 +38,8 @@ describe('DidController', () => {
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DidController],
providers: [DidService, PrismaService, ConfigService, VaultService,AnchorCordService]
// providers: [DidService, PrismaService, ConfigService, VaultService,AnchorCordService]
providers: [DidService, PrismaService, ConfigService, VaultService]
}).compile();

controller = module.get<DidController>(DidController);
Expand Down
7 changes: 4 additions & 3 deletions services/identity-service/src/did/did.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import { PrismaService } from 'src/utils/prisma.service';
import { DidController } from './did.controller';
import { DidService } from './did.service';
import { VaultService } from '../utils/vault.service';
import { AnchorCordService } from 'src/utils/cord.service';

import { BlockchainAnchorFactory } from './factories/blockchain-anchor.factory';
import { AnchorCordService } from './implementations/anchor-cord.service';
@Module({
imports: [HttpModule],
controllers: [DidController],
providers: [
DidService,
PrismaService,
VaultService,
AnchorCordService
AnchorCordService,
BlockchainAnchorFactory
],
})
export class DidModule {}
5 changes: 3 additions & 2 deletions services/identity-service/src/did/did.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { DidService } from './did.service';
import { PrismaService } from '../utils/prisma.service';
import { VaultService } from '../utils/vault.service';
import { AnchorCordService } from 'src/utils/cord.service';
// import { AnchorCordService } from 'src/utils/cord.service';
import { GenerateDidDTO, VerificationKeyType } from './dtos/GenerateDidRequest.dto';
import { ConfigService } from '@nestjs/config';

Expand Down Expand Up @@ -33,7 +33,8 @@ describe('DidService', () => {

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [DidService, PrismaService, VaultService, ConfigService,AnchorCordService],
// providers: [DidService, PrismaService, VaultService, ConfigService,AnchorCordService],
providers: [DidService, PrismaService, VaultService, ConfigService],
}).compile();

service = module.get<DidService>(DidService);
Expand Down
61 changes: 48 additions & 13 deletions services/identity-service/src/did/did.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { VaultService } from '../utils/vault.service';
import { Identity } from '@prisma/client';
import { RSAKeyPair } from 'crypto-ld';
import { GenerateDidDTO } from './dtos/GenerateDidRequest.dto';
import { AnchorCordService } from 'src/utils/cord.service';
// import { AnchorCordService } from 'src/utils/cord.service';
import { BlockchainAnchorFactory } from './factories/blockchain-anchor.factory';

const { DIDDocument } = require('did-resolver');
type DIDDocument = typeof DIDDocument;
Expand All @@ -22,7 +23,7 @@ export class DidService {
webDidPrefix: string;
signingAlgorithm: string;
didResolver: any;
constructor(private prisma: PrismaService, private vault: VaultService , private anchorcord:AnchorCordService) {
constructor(private prisma: PrismaService, private vault: VaultService , private blockchainFactory:BlockchainAnchorFactory) {
let baseUrl: string = process.env.WEB_DID_BASE_URL;
this.webDidPrefix = this.getDidPrefixForBaseUrl(baseUrl);
this.signingAlgorithm = process.env.SIGNING_ALGORITHM;
Expand Down Expand Up @@ -103,18 +104,22 @@ export class DidService {
let document: DIDDocument;
let privateKeys: object;
let blockchainStatus: boolean = false;

if (this.shouldAnchorToCord()) {

// Check if anchoring to blockchain is enabled and get the method
const method = this.shouldAnchorToBlockchain();

if (method) {
try {
if (doc.method !== 'cord') {
throw new BadRequestException('Invalid method: only "cord" is allowed for anchoring to Cord.');
}
const response = await this.anchorcord.anchorDid(doc);
didUri = response.document.uri;
document = response.document;

// store mnemonic and delegate keys in to vault
privateKeys = {
// Get the appropriate service from the factory
const anchorService = this.blockchainFactory.getAnchorService(method);
// Use the service to anchor the DID
const response = await anchorService.anchorDid(doc);

didUri = response.document.uri;
document = response.document;

// store mnemonic and delegate keys in to vault
privateKeys = {
"mnemonic":response.mnemonic,
"delegateKeys":response.delegateKeys
};
Expand Down Expand Up @@ -250,4 +255,34 @@ export class DidService {
process.env.ANCHOR_TO_CORD.toLowerCase().trim() === 'true'
);
}

/**
* Determines if anchoring to a blockchain is enabled based on environment variables.
* Checks for specific blockchain configurations and returns the appropriate method.
* @returns The blockchain method (e.g., 'cord', 'solana') if anchoring is enabled; otherwise, null.
*/
private shouldAnchorToBlockchain(): string | null {
// Check if the environment variable ANCHOR_TO_CORD is set to 'true' for the CORD blockchain
if (
process.env.ANCHOR_TO_CORD &&
process.env.ANCHOR_TO_CORD.toLowerCase().trim() === 'true'
) {
return 'cord'; // Return 'cord' as the service method if CORD anchoring is enabled
}

// Add additional checks here for other blockchains, e.g.,Solana, Ethereum, Polkadot
/*
if (
process.env.ANCHOR_TO_SOLANA &&
process.env.ANCHOR_TO_SOLANA.toLowerCase().trim() === 'true'
) {
return 'solana'; // Return 'solana' if solana anchoring is enabled
}
*/

return null; // Return null if no blockchain anchoring is required
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Injectable, BadRequestException } from '@nestjs/common';
import { AnchorCordService } from '../implementations/anchor-cord.service';
import { BlockchainAnchor } from '../interfaces/blockchain_anchor.interface';

/**
* Factory class to dynamically resolve the appropriate BlockchainAnchor service.
* It uses the specified method to determine which implementation to return.
*/
@Injectable()
export class BlockchainAnchorFactory {
/**
* Constructor for the BlockchainAnchorFactory.
* @param cordService - An instance of AnchorCordService, which handles CORD-specific anchoring logic.
*/
constructor(private readonly cordService: AnchorCordService) {}

/**
* Resolves the appropriate BlockchainAnchor service based on the provided method.
* @param method - The blockchain method (e.g., 'cord').
* @returns The service instance corresponding to the specified method or null if no method is provided.
* @throws
*/
getAnchorService(method?: string): BlockchainAnchor | null {
// If no method is specified, return null to indicate no anchoring is required
if (!method) {
return null;
}

// Determine the appropriate service implementation based on the method
switch (method) {
case 'cord':
// Return the CORD-specific implementation
return this.cordService;
default:
throw new BadRequestException(`Unsupported blockchain method: ${method}`);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable, Logger, InternalServerErrorException } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { BlockchainAnchor } from '../interfaces/blockchain_anchor.interface';
import {BadRequestException} from '@nestjs/common';
@Injectable()
export class AnchorCordService implements BlockchainAnchor {
private readonly logger = new Logger(AnchorCordService.name);

constructor(private readonly httpService: HttpService) {}

async anchorDid(body: any): Promise<{ document: any; mnemonic: string; delegateKeys: object }> {
try {
if (body.method !== 'cord') {
throw new BadRequestException('Invalid method: only "cord" is allowed for anchoring to Cord.');
}
const response = await this.httpService.axiosRef.post(
`${process.env.ISSUER_AGENT_BASE_URL}/did/create/`,
body,
);
return response.data.result;
} catch (err) {
this.logger.error('Error anchoring DID to CORD blockchain', err);
throw new InternalServerErrorException('Failed to anchor DID to CORD blockchain');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface BlockchainAnchor {
/**
* Anchors a DID document to the blockchain.
* @param body The request payload for anchoring.
* @returns The anchored DID document or related data.
*/
anchorDid(body: any): Promise<any>;
}

40 changes: 0 additions & 40 deletions services/identity-service/src/utils/cord.service.ts

This file was deleted.

7 changes: 5 additions & 2 deletions services/identity-service/src/vc/vc.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { VaultService } from 'src/utils/vault.service';
import { PrismaService } from 'src/utils/prisma.service';
import { VcController } from './vc.controller';
import VcService from './vc.service';
import { AnchorCordService } from 'src/utils/cord.service';
import { BlockchainAnchorFactory } from 'src/did/factories/blockchain-anchor.factory';
import { AnchorCordService } from 'src/did/implementations/anchor-cord.service';
// import { AnchorCordService } from 'src/utils/cord.service';

@Module({
imports: [HttpModule],
controllers: [VcController],
providers: [VcService, PrismaService, DidService, VaultService,AnchorCordService],
providers: [VcService, PrismaService, DidService, VaultService,BlockchainAnchorFactory,AnchorCordService],
// providers: [VcService, PrismaService, DidService, VaultService,AnchorCordService],
})
export class VcModule {}
5 changes: 3 additions & 2 deletions services/identity-service/src/vc/vc.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import VcService from './vc.service';
import { PrismaService } from '../utils/prisma.service';
import { DidService } from '../did/did.service';
import { VaultService } from '../utils/vault.service';
import { AnchorCordService } from 'src/utils/cord.service';
// import { AnchorCordService } from 'src/utils/cord.service';
describe('DidService', () => {
let service: VcService;
let didService: DidService;
Expand All @@ -17,7 +17,8 @@ describe('DidService', () => {

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [VcService, PrismaService, DidService, VaultService,AnchorCordService],
providers: [VcService, PrismaService, DidService, VaultService],
// providers: [VcService, PrismaService, DidService, VaultService,AnchorCordService],
}).compile();

service = module.get<VcService>(VcService);
Expand Down

0 comments on commit aa5b102

Please sign in to comment.