-
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.
Schema-service:Interface Implemented
- Loading branch information
Showing
10 changed files
with
152 additions
and
82 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
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/credential-schema/src/schema/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}`); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
services/credential-schema/src/schema/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,37 @@ | ||
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 anchorSchema(body: any): Promise<any> { | ||
try { | ||
const response = await this.httpService.axiosRef.post( | ||
`${process.env.ISSUER_AGENT_BASE_URL}/schema`, | ||
body, | ||
); | ||
return response.data.result; | ||
} catch (err) { | ||
const errorDetails = { | ||
message: err.message, | ||
status: err.response?.status, | ||
statusText: err.response?.statusText, | ||
data: err.response?.data, | ||
headers: err.response?.headers, | ||
request: err.config, | ||
}; | ||
|
||
this.logger.error( | ||
'Error anchoring schema to Cord blockchain', | ||
errorDetails, | ||
); | ||
throw new InternalServerErrorException( | ||
'Failed to anchor schema to Cord blockchain', | ||
); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
services/credential-schema/src/schema/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. | ||
*/ | ||
anchorSchema(body: any): Promise<any>; | ||
} | ||
|
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
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