-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Credential-service:Interface Implemented
- Loading branch information
Showing
5 changed files
with
190 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
services/credentials-service/src/credentials/factories/blockchain-anchor.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
services/credentials-service/src/credentials/implementations/anchor-cord.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Injectable, Logger, InternalServerErrorException, BadRequestException } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BlockchainAnchor } from '../interfaces/blockchain_anchor.interface'; | ||
import { AxiosResponse } from '@nestjs/terminus/dist/health-indicator/http/axios.interfaces'; | ||
import { IssueCredentialDTO } from '../dto/issue-credential.dto'; | ||
import { JwtCredentialSubject } from 'src/app.interface'; | ||
import { W3CCredential, Verifiable } from 'vc.types'; | ||
|
||
@Injectable() | ||
export class AnchorCordService implements BlockchainAnchor { | ||
|
||
private readonly logger = new Logger(AnchorCordService.name); | ||
|
||
|
||
constructor(private readonly httpService: HttpService) { | ||
|
||
} | ||
|
||
async anchorCredential(issueRequest: IssueCredentialDTO): Promise<any> { | ||
try { | ||
const credInReq = issueRequest.credential; | ||
if (!issueRequest.credentialSchemaId) { | ||
|
||
this.logger.error('Credential SchemaId Schema ID is required for anchoring but is missing'); | ||
throw new BadRequestException('Cord Schema ID is missing'); | ||
} | ||
|
||
this.logger.debug('url', process.env.ISSUER_AGENT_BASE_URL); | ||
this.logger.debug('Anchoring unsigned credential to Cord blockchain with schema ID:', issueRequest.credentialSchemaId); | ||
const credentialPayload = { | ||
...credInReq, | ||
schemaId: issueRequest.credentialSchemaId, | ||
} | ||
let anchorHttpResponse: AxiosResponse = | ||
await this.httpService.axiosRef.post( | ||
`${process.env.ISSUER_AGENT_BASE_URL}/cred`, | ||
{ | ||
credential: credentialPayload, | ||
} | ||
); | ||
|
||
this.logger.debug('Credential successfully anchored'); | ||
let anchoredResult = anchorHttpResponse.data.result; | ||
this.logger.debug('Credential successfully anchored to Cord:', anchoredResult); | ||
const { | ||
id, issuer, issuanceDate, validUntil: expirationDate, credentialSubject, proof, | ||
} = anchoredResult.vc; | ||
|
||
const anchoredCredentialData = { | ||
id, | ||
type: issueRequest.credential.type, | ||
issuer, | ||
issuanceDate, | ||
expirationDate, | ||
subject: credentialSubject, | ||
subjectId: (credentialSubject as JwtCredentialSubject).id, | ||
proof, | ||
credential_schema: issueRequest.credentialSchemaId, | ||
signed: anchoredResult.vc as object, | ||
tags: issueRequest.tags, | ||
blockchainStatus: "ANCHORED", | ||
|
||
}; | ||
return anchoredCredentialData; | ||
} catch (err) { | ||
this.logger.error('Error anchoring credential:', err); | ||
|
||
throw new InternalServerErrorException(`Error anchoring credential : ${err.response.data.details}`); | ||
} | ||
} | ||
|
||
|
||
async verifyCredential( | ||
credToVerify: Verifiable<W3CCredential> | ||
): Promise<any> { | ||
try { | ||
this.logger.debug(`${process.env.VERIFICATION_MIDDLEWARE_BASE_URL}/credentials/verify}`) | ||
const response = await this.httpService.axiosRef.post( | ||
`${process.env.VERIFICATION_MIDDLEWARE_BASE_URL}/credentials/verify`, | ||
credToVerify | ||
); | ||
|
||
if (response.status !== 200) { | ||
this.logger.error('Cord verification failed:', response.data); | ||
throw new InternalServerErrorException('Cord verification failed'); | ||
} | ||
|
||
return response.data; | ||
} catch (err) { | ||
this.logger.error('Error calling Cord verification API:', err); | ||
throw new InternalServerErrorException( | ||
'Error verifying credential on Cord' | ||
); | ||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
services/credentials-service/src/credentials/interfaces/blockchain_anchor.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface BlockchainAnchor { | ||
/** | ||
* Anchors a Scheam to the blockchain. | ||
* @param body The request payload for anchoring. | ||
* @returns The anchored Schema or related data. | ||
*/ | ||
anchorCredential(body: any): Promise<any>; | ||
verifyCredential(body: any): Promise<any>; | ||
} |