Skip to content

Commit

Permalink
Merge pull request #129 from credebl/endorser-request-and-submit
Browse files Browse the repository at this point in the history
feat: schema endorsement request and sign transaction
  • Loading branch information
nishad-ayanworks authored Oct 7, 2023
2 parents d7c49ec + b612161 commit af1c1d0
Show file tree
Hide file tree
Showing 14 changed files with 799 additions and 308 deletions.
10 changes: 10 additions & 0 deletions apps/agent-service/src/agent-service.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,14 @@ export class AgentServiceController {
async getProofFormData(payload: { url: string, apiKey: string }): Promise<object> {
return this.agentServiceService.getProofFormData(payload.url, payload.apiKey);
}

@MessagePattern({ cmd: 'agent-schema-endorsement-request' })
async schemaEndorsementRequest(payload: { url: string, apiKey: string, requestSchemaPayload:object }): Promise<object> {
return this.agentServiceService.schemaEndorsementRequest(payload.url, payload.apiKey, payload.requestSchemaPayload);
}

@MessagePattern({ cmd: 'agent-sign-transaction' })
async signTransaction(payload: { url: string, apiKey: string, signEndorsementPayload:string }): Promise<object> {
return this.agentServiceService.signTransaction(payload.url, payload.apiKey, payload.signEndorsementPayload);
}
}
26 changes: 26 additions & 0 deletions apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,5 +889,31 @@ export class AgentServiceService {
}
}

async schemaEndorsementRequest(url: string, apiKey: string, requestSchemaPayload:object): Promise<object> {
try {
const schemaRequest = await this.commonService
.httpPost(url, requestSchemaPayload, { headers: { 'x-api-key': apiKey } })
.then(async response => response);
return schemaRequest;
} catch (error) {
this.logger.error(`Error in schema endorsement request in agent service : ${JSON.stringify(error)}`);
throw new RpcException(error.response ? error.response : error);
}
}

async signTransaction(url: string, apiKey: string, signEndorsementPayload: string): Promise<object> {
try {

const signEndorsementTransaction = await this.commonService
.httpPost(url, signEndorsementPayload, { headers: { 'x-api-key': apiKey } })
.then(async response => response);

return signEndorsementTransaction;
} catch (error) {
this.logger.error(`Error in sign transaction in agent service : ${JSON.stringify(error)}`);
throw new RpcException(error.response ? error.response : error);
}
}

}

36 changes: 0 additions & 36 deletions apps/api-gateway/src/ecosystem/dtos/create-organization-dto.ts

This file was deleted.

47 changes: 47 additions & 0 deletions apps/api-gateway/src/ecosystem/dtos/request-schema-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ApiExtraModels, ApiProperty } from '@nestjs/swagger';
import { IsArray, IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
@ApiExtraModels()

class AttributeValue {

@IsString()
@IsNotEmpty({ message: 'attributeName is required.' })
attributeName: string;

@IsString()
@IsNotEmpty({ message: 'schemaDataType is required.' })
schemaDataType: string;

@IsString()
@IsNotEmpty({ message: 'displayName is required.' })
displayName: string;
}

export class RequestSchemaDto {
@ApiProperty()
@IsString({ message: 'name must be in string format.' })
name: string;

@ApiProperty()
@IsInt({ message: 'version must be in number format.' })
version: number;

@ApiProperty({
'example': [
{
attributeName: 'name',
schemaDataType: 'string',
displayName: 'Name'
}
]
})
@IsArray({ message: 'attributes must be an array' })
@IsNotEmpty({ message: 'please provide valid attributes' })
attributes: AttributeValue[];

@ApiProperty()
@IsBoolean({ message: 'endorse must be a boolean.' })
@IsOptional()
endorse?: boolean;

}
30 changes: 29 additions & 1 deletion apps/api-gateway/src/ecosystem/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EcosystemService } from './ecosystem.service';
import { Post, Get } from '@nestjs/common';
import { Body } from '@nestjs/common';
import { Res } from '@nestjs/common';
import { CreateEcosystemDto } from './dtos/create-ecosystem-dto';
import { RequestSchemaDto } from './dtos/request-schema-dto';
import IResponseType from '@credebl/common/interfaces/response.interface';
import { HttpStatus } from '@nestjs/common';
import { Response } from 'express';
Expand All @@ -27,6 +27,7 @@ import { EcosystemRolesGuard } from '../authz/guards/ecosystem-roles.guard';
import { EcosystemsRoles, Roles } from '../authz/decorators/roles.decorator';
import { OrgRolesGuard } from '../authz/guards/org-roles.guard';
import { OrgRoles } from 'libs/org-roles/enums';
import { CreateEcosystemDto } from './dtos/create-ecosystem-dto';


@UseFilters(CustomExceptionFilter)
Expand Down Expand Up @@ -168,6 +169,33 @@ export class EcosystemController {
return res.status(HttpStatus.CREATED).json(finalResponse);
}

@Post('/:orgId/transaction/schema')
@ApiOperation({ summary: 'Request new schema', description: 'Request new schema' })
@ApiResponse({ status: 201, description: 'Success', type: ApiResponseDto })
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
async requestSchemaTransaction(@Body() requestSchemaPayload: RequestSchemaDto, @Param('orgId') orgId: number, @Res() res: Response): Promise<Response> {
await this.ecosystemService.schemaEndorsementRequest(requestSchemaPayload, orgId);
const finalResponse: IResponseType = {
statusCode: HttpStatus.CREATED,
message: ResponseMessages.ecosystem.success.schemaRequest
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}

@Post('transaction/sign/:endorsementId')
@ApiOperation({ summary: 'Sign transaction', description: 'Sign transaction' })
@ApiResponse({ status: 201, description: 'Success', type: ApiResponseDto })
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
async SignEndorsementRequests(@Param('endorsementId') endorsementId: string, @Res() res: Response): Promise<Response> {
await this.ecosystemService.signTransaction(endorsementId);
const finalResponse: IResponseType = {
statusCode: HttpStatus.CREATED,
message: ResponseMessages.ecosystem.success.sign
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}

/**
*
Expand Down
11 changes: 11 additions & 0 deletions apps/api-gateway/src/ecosystem/ecosystem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BulkEcosystemInvitationDto } from './dtos/send-invitation.dto';
import { AcceptRejectEcosystemInvitationDto } from './dtos/accept-reject-ecosysteminvitation-dto';
import { GetAllEcosystemInvitationsDto } from './dtos/get-all-sent-invitations.dto';
import { GetAllSentEcosystemInvitationsDto } from './dtos/get-all-sent-ecosystemInvitations-dto';
import { RequestSchemaDto } from './dtos/request-schema-dto';


@Injectable()
Expand Down Expand Up @@ -99,4 +100,14 @@ export class EcosystemService extends BaseService {
}


async schemaEndorsementRequest(requestSchemaPayload: RequestSchemaDto, orgId: number): Promise<object> {
const payload = { requestSchemaPayload, orgId};
return this.sendNats(this.serviceProxy, 'schema-endorsement-request', payload);
}


async signTransaction(endorsementId:string): Promise<object> {
const payload = { endorsementId };
return this.sendNats(this.serviceProxy, 'sign-endorsement-transaction', payload);
}
}
7 changes: 7 additions & 0 deletions apps/ecosystem/enums/ecosystem.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ export enum EcosystemInvitationStatus {
ACCEPTED = 'accepted',
REJECTED = 'rejected',
PENDING = 'pending'
}

export enum endorsementTransactionStatus {
REQUESTED = 'Requested',
SIGNED = 'Signed',
DECLINED = 'Declined',
SUBMITED = 'Submited'
}
51 changes: 51 additions & 0 deletions apps/ecosystem/interfaces/ecosystem.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export interface RequestSchemaEndorsement {
orgId: number
name: string;
version: number;
attributes: IAttributeValue[];
endorse?: boolean;
}

export interface IAttributeValue {
attributeName: string;
schemaDataType: string;
displayName: string
}

export interface SchemaTransactionPayload {
endorserDid: string;
endorse: boolean;
attributes: string[];
version: string;
name: string;
issuerId: string;
}

export interface SchemaMessage {
message?: {
jobId: string;
schemaState: {
state: string;
action: string;
schemaId: string;
schema: Record<string, unknown>;
schemaRequest: string;
};
registrationMetadata: Record<string, unknown>;
schemaMetadata: Record<string, unknown>;
};
}

export interface SchemaTransactionResponse {
endorserDid: string;
authorDid: string;
requestPayload: string;
status: string;
ecosystemOrgId: string;
}

export interface SignedTransactionMessage {
message?: {
signedTransaction: string;
};
}
24 changes: 24 additions & 0 deletions apps/ecosystem/src/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Body } from '@nestjs/common';
import { BulkSendInvitationDto } from '../dtos/send-invitation.dto';
import { AcceptRejectEcosystemInvitationDto } from '../dtos/accept-reject-ecosysteminvitation.dto';
import { FetchInvitationsPayload } from '../interfaces/invitations.interface';
import { RequestSchemaEndorsement } from '../interfaces/ecosystem.interfaces';

@Controller()
export class EcosystemController {
Expand Down Expand Up @@ -105,4 +106,27 @@ export class EcosystemController {
);
}

/**
*
* @param payload
* @returns Schema endorsement request
*/
@MessagePattern({ cmd: 'schema-endorsement-request' })
async schemaEndorsementRequest(
@Body() payload: { requestSchemaPayload: RequestSchemaEndorsement; orgId: number }
): Promise<object> {
return this.ecosystemService.requestSchemaEndorsement(payload.requestSchemaPayload, payload.orgId);
}

/**
*
* @param payload
* @returns sign endorsement request
*/
@MessagePattern({ cmd: 'sign-endorsement-transaction' })
async signTransaction(
@Body() payload: { endorsementId: string }
): Promise<object> {
return this.ecosystemService.signTransaction(payload.endorsementId);
}
}
Loading

0 comments on commit af1c1d0

Please sign in to comment.