|
| 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