From 881c83cfffbe32a25858df03ea67ecd06c52f160 Mon Sep 17 00:00:00 2001 From: Pratap2018 Date: Mon, 30 Dec 2024 17:49:00 +0530 Subject: [PATCH] fix-did --- src/did/dto/create-did.dto.ts | 6 ++- src/did/services/did.service.ts | 3 ++ src/utils/customDecorator/did.decorator.ts | 43 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/did/dto/create-did.dto.ts b/src/did/dto/create-did.dto.ts index 58b161ff..38462074 100644 --- a/src/did/dto/create-did.dto.ts +++ b/src/did/dto/create-did.dto.ts @@ -14,7 +14,10 @@ import { } from 'class-validator'; import { RegistrationStatus } from '../schemas/did.schema'; import { DidDoc } from '../dto/update-did.dto'; -import { IsDid } from 'src/utils/customDecorator/did.decorator'; +import { + IsDid, + IsMethodSpecificId, +} from 'src/utils/customDecorator/did.decorator'; import { ValidatePublicKeyMultibase } from 'src/utils/customDecorator/pubKeyMultibase.decorator'; import { IVerificationRelationships, IKeyType } from 'hs-ssi-sdk'; import { IsKeyTypeArrayOrSingle } from 'src/utils/customDecorator/keyType.decorator'; @@ -105,6 +108,7 @@ export class CreateDidDto { @IsString() @MinLength(32) @MaxLength(48) + @IsMethodSpecificId() @ApiProperty({ name: 'methodSpecificId', description: 'MethodSpecificId to be added in did', diff --git a/src/did/services/did.service.ts b/src/did/services/did.service.ts index 195fd676..bcb464b2 100644 --- a/src/did/services/did.service.ts +++ b/src/did/services/did.service.ts @@ -873,6 +873,9 @@ export class DidService { 'resolveDid() method: before calling hypersignDid.generate', 'DidService', ); + console.log(methodSpecificId); + console.log(publicKeyMultibase); + resolvedDid = await hypersignDid.generate({ methodSpecificId, publicKeyMultibase, diff --git a/src/utils/customDecorator/did.decorator.ts b/src/utils/customDecorator/did.decorator.ts index c3252ed7..eeb4f001 100644 --- a/src/utils/customDecorator/did.decorator.ts +++ b/src/utils/customDecorator/did.decorator.ts @@ -36,3 +36,46 @@ export const IsDid = (): PropertyDecorator => { }, ); }; + +export const IsMethodSpecificId = (): PropertyDecorator => { + return applyDecorators( + SetMetadata('isMethodSpecificId', true), + (target: object, propertyKey: string | symbol) => { + let original = target[propertyKey]; + const descriptor: PropertyDescriptor = { + get: () => original, + set: (val: any) => { + if (val.trim() === '') { + throw new BadRequestException([ + `${propertyKey.toString()} cannot be empty`, + ]); + } + + const did = val; + if (did.includes('did:hid:')) { + throw new BadRequestException([ + `Invalid ${propertyKey.toString()}`, + ]); + } + if (did.includes('hid')) { + throw new BadRequestException([ + `Invalid ${propertyKey.toString()}`, + ]); + } + if (did.includes(':')) { + throw new BadRequestException([ + `Invalid ${propertyKey.toString()}`, + ]); + } + if (did.includes('.')) { + throw new BadRequestException([ + `Invalid ${propertyKey.toString()}`, + ]); + } + original = val; + }, + }; + Object.defineProperty(target, propertyKey, descriptor); + }, + ); +};