Skip to content

Commit

Permalink
Merge pull request #50 from lacchain/PD-Type3
Browse files Browse the repository at this point in the history
feat: add support for type-3 member data in public directory
  • Loading branch information
eum602 authored Nov 1, 2023
2 parents 738f579 + b843944 commit 2d32ee3
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.0.8

* Adds 'Type-3' data for Public Directory

## 0.0.1

### Additions and Improvements
Expand Down
29 changes: 29 additions & 0 deletions docs/api/Committee-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ data='{"validDays":'$validDays', "expires":'$expires',"identificationData":'$ide
curl -X 'POST' ${add_member_to_public_directory_url} -H 'accept: application/json' -F caCert=@$path_to_public_cacrt -F data=$data
```

#### Onboarding a Type-3 member into the Public Directory

* You must have:
1. The DID (Decentralized Identifier)
2. New Member's Metadata according to type-3 data type
```sh
api_url=http://localhost:3002 # Set LACChain Trust url
```
```sh
validDays=500
expires=false
# metadata
did="did:lac1:1iT58jc7tEP4Y8zbf9Ma8dpdiGKZjzYmjtmFEZZ8Dias9rbBpAYS39dAekAZupNDdgGb"
legalName="Ministry Of Country CHL"
countryCode="CHL" #urn:iso:std:iso:3166
url="https://lacnet.lacchain.net"
identificationData='{"id":'\"$did\"',"legalName":'\"$legalName\"',"countryCode": '\"$countryCode\"', "url": '\"$url\"'}'
# process
add_type3_member_to_public_directory_url="$api_url"/api/v1/public-directory/add-type3-member
data='{"validDays":'$validDays', "expires":'$expires',"identificationData":'$identificationData'}'
curl -X 'POST' ${add_type3_member_to_public_directory_url} -H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d "$data"
```
##### requirements
* You must have:
1. The Certificate Authority
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lacchain-trust",
"version": "0.0.7",
"version": "0.0.8",
"description": "Rest api for Lacchain Trust built on top of Lacchain",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
35 changes: 34 additions & 1 deletion src/controllers/lac1/public.directory.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { Service } from 'typedi';
import { LacchainPublicDirectory } from '../../services/public-directory/lacchain.public.directory';
import { ErrorsMessages } from '../../constants/errorMessages';
import { IManager } from 'src/interfaces/manager/manager';
import { PublicDirectoryType1MemberDTO } from '@dto/public.directory/public.directoryDTO';
import {
PublicDirectoryType1MemberDTO,
PublicDirectoryType3MemberDTO
} from '../../dto/public.directory/public.directoryDTO';

@JsonController('/public-directory')
@Service()
Expand Down Expand Up @@ -71,6 +74,36 @@ export class PublicDirectoryController {
);
}
}

/**
* Type3 member add all base identification data
* @param {PublicDirectoryType1MemberDTO} data
* @example - POST
{
"validDays": 1000,
"expires": true,
"identificationData": {
"id": "did:lac1:1iT5QGEpfYcG6....2yfGHwM48pNDHJoqF92NGzzpo",
"legalName": "Racsel - American Electronic Health Cooperation Network",
"domainName": "racsel.org"
}
}
*/
@Post('/add-type3-member')
async addType3Member(
@Body({ validate: true }) data: PublicDirectoryType3MemberDTO
): Promise<any> {
try {
return this.publicDirectory.addType3Member(data);
} catch (error: any) {
if (error.detail ?? error.message) {
throw new BadRequestError(error.detail ?? error.message);
}
throw new InternalServerError(
error.detail ?? error.message ?? ErrorsMessages.INTERNAL_SERVER_ERROR
);
}
}
@Get('/get-manager')
async getManager(): Promise<IManager> {
try {
Expand Down
22 changes: 22 additions & 0 deletions src/dto/public.directory/public.directoryDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export class Type2MemberDataValidator extends BaseTypeMemberDataValidator {
certificateAuthority!: string;
}

export class Type3MemberDataValidator extends BaseTypeMemberDataValidator {
@ValidateNested()
@IsDefined()
@Type(() => Base2IdentificationDataValidator)
identificationData!: Base2IdentificationDataValidator;
}

export class Base1PublicDirectoryMemberValidator {
@Min(1)
@IsPositive({
Expand Down Expand Up @@ -118,6 +125,13 @@ export class PublicDirectoryType2MemberDTO extends Base1PublicDirectoryMemberVal
identificationData!: Base2IdentificationDataValidator;
}

export class PublicDirectoryType3MemberDTO extends Base1PublicDirectoryMemberValidator {
@ValidateNested()
@IsDefined()
@Type(() => Base2IdentificationDataValidator)
identificationData!: Base2IdentificationDataValidator;
}

// eslint-disable-next-line max-len
export class PublicDirectoryType1MemberValidator extends Base1PublicDirectoryMemberValidator {
@ValidateNested()
Expand All @@ -133,3 +147,11 @@ export class PublicDirectoryType2MemberValidator extends Base1PublicDirectoryMem
@Type(() => Type2MemberDataValidator)
memberData!: Type2MemberDataValidator;
}

// eslint-disable-next-line max-len
export class PublicDirectoryType3MemberValidator extends Base1PublicDirectoryMemberValidator {
@ValidateNested()
@IsDefined()
@Type(() => Type3MemberDataValidator)
memberData!: Type3MemberDataValidator;
}
31 changes: 30 additions & 1 deletion src/services/public-directory/lacchain.identity.structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
Base2IdentificationDataValidator,
PublicDirectoryType1MemberValidator,
Type1MemberDataValidator,
Base1IdentificationDataValidator
Base1IdentificationDataValidator,
PublicDirectoryType3MemberValidator,
Type3MemberDataValidator
} from '../../dto/public.directory/public.directoryDTO';
import { validateOrReject } from 'class-validator';
import { BadRequestError } from 'routing-controllers';
Expand Down Expand Up @@ -48,6 +50,21 @@ export class IdentityValidator {
}
}

async validateType3PublicDirectoryMember(
publicDirectoryMemberV: PublicDirectoryType3MemberValidator
) {
const v = new PublicDirectoryType3MemberValidator();
v.validDays = publicDirectoryMemberV.validDays;
v.expires = publicDirectoryMemberV.expires;
v.chainOfTrustAddress = publicDirectoryMemberV.chainOfTrustAddress;
v.memberData = publicDirectoryMemberV.memberData;
try {
await validateOrReject(v);
} catch (err: any) {
throw new BadRequestError(err);
}
}

async validateType1MemberData(memberData: Type1MemberDataValidator) {
const v = new Type1MemberDataValidator();
v.type = memberData.type;
Expand All @@ -73,6 +90,18 @@ export class IdentityValidator {
}
}

async validateType3MemberData(memberData: Type3MemberDataValidator) {
const v = new Type3MemberDataValidator();
v.type = memberData.type;
v.version = memberData.version;
v.identificationData = memberData.identificationData;
try {
await validateOrReject(v);
} catch (err: any) {
throw new BadRequestError(err);
}
}

async validateBase1IdentificationData(
identificationData: Base1IdentificationDataValidator
) {
Expand Down
45 changes: 42 additions & 3 deletions src/services/public-directory/lacchain.public.directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
PublicDirectoryType2MemberValidator,
Type1MemberDataValidator,
PublicDirectoryType1MemberDTO,
PublicDirectoryType1MemberValidator
PublicDirectoryType1MemberValidator,
PublicDirectoryType3MemberDTO,
Type3MemberDataValidator,
PublicDirectoryType3MemberValidator
} from '../../dto/public.directory/public.directoryDTO';

import { IdentityValidator } from './lacchain.identity.structure';
Expand All @@ -39,6 +42,8 @@ export class LacchainPublicDirectory {
private memberDataType1Name = 'Type-1';
private memberDataType2EncodingVersion = '1.0.0';
private memberDataType2Name = 'Type-2';
private memberDataType3EncodingVersion = '1.0.0';
private memberDataType3Name = 'Type-3';
constructor() {
this.publicDirectory = new PublicDirectoryBase(
resolvePublicDirectoryAddress(),
Expand All @@ -63,8 +68,6 @@ export class LacchainPublicDirectory {
}
await this._type1ValidateAndFillAdditionalParams(publicDirectoryMember);
return this.addMember(publicDirectoryMember);

// return { Ok: true };
}

async rawAddType2Member(
Expand Down Expand Up @@ -99,6 +102,23 @@ export class LacchainPublicDirectory {
await this._type2ValidateAndFillAdditionalParams(publicDirectoryMember);
return this.addMember(publicDirectoryMember);
}

async addType3Member(type3Data: PublicDirectoryType3MemberDTO): Promise<any> {
const memberData = new Type3MemberDataValidator();
memberData.identificationData = type3Data.identificationData;
const publicDirectoryMember = new PublicDirectoryType3MemberValidator();
publicDirectoryMember.validDays = type3Data.validDays;
publicDirectoryMember.expires = type3Data.expires;
publicDirectoryMember.chainOfTrustAddress = type3Data.chainOfTrustAddress;
publicDirectoryMember.memberData = memberData;

if (!publicDirectoryMember.memberData) {
throw new BadRequestError(ErrorsMessages.BAD_REQUEST_ERROR);
}
await this._type3ValidateAndFillAdditionalParams(publicDirectoryMember);
return this.addMember(publicDirectoryMember);
}

private _validatex509Cert(x509CA: X509Certificate) {
if (!x509CA.ca) {
throw new BadRequestError(ErrorsMessages.NOT_CA_CERTIFICATE_ERROR);
Expand Down Expand Up @@ -149,6 +169,25 @@ export class LacchainPublicDirectory {
);
}

private async _type3ValidateAndFillAdditionalParams(
publicDirectoryMemberV: PublicDirectoryType3MemberValidator
) {
if (publicDirectoryMemberV.memberData) {
publicDirectoryMemberV.memberData.version =
this.memberDataType3EncodingVersion;
publicDirectoryMemberV.memberData.type = this.memberDataType3Name;
}
await this.identityValidator.validateType3PublicDirectoryMember(
publicDirectoryMemberV
);
await this.identityValidator.validateType3MemberData(
publicDirectoryMemberV.memberData
);
await this.identityValidator.validateBase2IdentificationData(
publicDirectoryMemberV.memberData.identificationData
);
}

async addMember(
publicDirectoryMember:
| Type1PublicDirectoryMember
Expand Down

0 comments on commit 2d32ee3

Please sign in to comment.