Skip to content

Commit

Permalink
modify: proof value 즉시 생성 후 vc에 삽입
Browse files Browse the repository at this point in the history
getProofValue 메서드 삭제 및 API 통신 단축
  • Loading branch information
Lee-clipse committed May 7, 2024
1 parent f9dc2ad commit 7ddb755
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 41 deletions.
1 change: 0 additions & 1 deletion BE/holder/src/config/.test.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
API_CREATE_USER_VC=http://localhost:8082/api/issuer/create-vc
API_GET_PROOF_VALUE=http://localhost:8082/api/issuer/generate-proof-value
MAIL_PASSWORD=pwdpwd
MAIL_USER=[email protected]
JWT_SECRET=secrete
31 changes: 17 additions & 14 deletions BE/holder/src/holder/holder-api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@ export class HolderAPIController {
private readonly customLoggerService: CustomLoggerService,
) {}

@Post('/reg-did')
@ApiOperation({
summary: 'Holder DID 생성 및 적재',
})
async registerDID() {
try {
// DID를 Near 네트워크에 적재
return await this.holderAPIService.loadDID();
} catch (error) {
this.customLoggerService.error('/reg-did', 'Holder DID 생성 실패', {});
throw new CustomErrorException('Register DID Failed', 500);
}
}

@Post('/create-vc')
@ApiOperation({
summary: '사용자 VC 생성',
})
async createUserVC(@Body() dto: UserVCDto) {
try {
const { issuerPubKey, vc } = await this.holderAPIService.createUserVC(
dto,
);
const { proofValue, message } =
await this.holderAPIService.getProofValue();
const rawVC = JSON.parse(vc);

const newProof = { ...rawVC.proof, proofValue };
const newVC = { ...rawVC, proof: newProof };

// DID를 Near 네트워크에 적재
// await this.holderAPIService.loadDID();

const { issuerPubKey, vc, message } =
await this.holderAPIService.createUserVC(dto);
return {
statusCode: 200,
data: { issuerPubKey, vc: JSON.stringify(newVC), message },
data: { issuerPubKey, vc, message },
};
} catch (error) {
this.customLoggerService.error('/create-vc', '사용자 VC 생성 실패', dto);
Expand Down
20 changes: 2 additions & 18 deletions BE/holder/src/holder/holder-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class HolderAPIService {
) {}

CREATE_USER_VC = this.configService.get<string>('API_CREATE_USER_VC');
GET_PROOF_VALUE = this.configService.get<string>('API_GET_PROOF_VALUE');

/*
@ Use: Holder Controller - createUserVC()
Expand All @@ -31,7 +30,7 @@ export class HolderAPIService {
*/
async createUserVC(
dto: UserVCDto,
): Promise<{ issuerPubKey: string; vc: string }> {
): Promise<{ issuerPubKey: string; vc: string; message: string }> {
return lastValueFrom(
this.httpService
.post(this.CREATE_USER_VC, {
Expand All @@ -41,19 +40,6 @@ export class HolderAPIService {
);
}

/*
@ Use: Holder Controller - createUserVC()
@ Intend: VC에 담을 proof value를 Issuer에게 생성 요청
* API Call: Issuer - generateProofValue()
*/
async getProofValue(): Promise<{ proofValue: string; message: string }> {
return lastValueFrom(
this.httpService
.post(this.GET_PROOF_VALUE)
.pipe(map((response) => response?.data)),
);
}

/*
@ Use: Holder Controller - sendEmailCode()
@ Intend: 사용자 이메일로 인증코드를 발송하여 2차 인증
Expand Down Expand Up @@ -88,11 +74,9 @@ export class HolderAPIService {
*/
async loadDID() {
const contract = await connectToNEARContract();

await (contract as NEARContract).reg_did_using_account({
is_issuer: true,
is_issuer: false,
});

return true;
}
}
5 changes: 3 additions & 2 deletions BE/issuer/src/issuer/issuer-api.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Controller, Get, Query, Post, Body } from '@nestjs/common';
import { IssuerAPIService } from './issuer-api.service';
import { ApiTags, ApiOperation, ApiQuery } from '@nestjs/swagger';
Expand All @@ -22,13 +23,13 @@ export class IssuerAPIController {
// 적재된 DID인지 확인: 아니라면 throw
// await this.checkIsLoadedDID(dto.holderPubKey);

const { vc } = this.issuerAPIService.createUserVC(dto);
const { vc, message } = await this.issuerAPIService.createUserVC(dto);
const vcString = JSON.stringify(vc);
const issuerPubKey = this.issuerAPIService.getIssuerPubKey();

try {
await this.issuerAPIService.loadKeyChain(issuerPubKey, vcString);
return { issuerPubKey, vc: vcString };
return { issuerPubKey, vc: vcString, message };
} catch (error) {
this.customLoggerService.error('/create-vc', '사용자 VC 생성 실패', {
...dto,
Expand Down
10 changes: 7 additions & 3 deletions BE/issuer/src/issuer/issuer-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ export class IssuerAPIService {
@ Use: Issuer Controller - createUserVC()
@ Intend: did 규격에 맞게 VC object 생성
*/
createUserVC(dto: UserVCDto) {
async createUserVC(dto: UserVCDto) {
const { holderPubKey } = dto;
const uuid = uuidv4();
const vc = createVC(uuid, holderPubKey);
return { uuid, vc };

// proof value가 삽입된 VC 생성
const { proofValue, message } = await this.generateProofValue();

const vc = createVC(uuid, holderPubKey, proofValue);
return { uuid, vc, message };
}

/*
Expand Down
10 changes: 7 additions & 3 deletions BE/issuer/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ export async function connectToNEARContract(): Promise<Contract> {
return contract;
}

export function createVC(uuid: string, holderPubKey: string) {
export function createVC(
uuid: string,
holderPubKey: string,
proofValue: string,
) {
const timeStamp = getTimeStamp();
// proofValue 는 현재 하드코딩 됨
// - issuer 필드와 함께, 연산결과로 수정되어야 함
// - 참고: https://cyphr.me/ed25519_tool/ed.html
return {
context: ['https://www.w3.org/ns/credentials/v2'],
id: `url:uuid:${uuid}`,
credential_type: ['VerifiableCredential', 'MajorCredential'],
credential_type: ['VerifiableCredential'],
// 완성본 issuer
issuer: 'did:near:pnu.testnet',
validFrom: timeStamp,
Expand All @@ -70,7 +74,7 @@ export function createVC(uuid: string, holderPubKey: string) {
created: timeStamp,
verificationMethod: 'CircRefVCSignatureVerificationMethod',
proofPurpose: 'assertionMethod',
proofValue: '',
proofValue,
},
};
}
Expand Down

0 comments on commit 7ddb755

Please sign in to comment.