Skip to content

Commit

Permalink
abstract base controllers and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AvbrehtLuka committed Aug 20, 2024
1 parent 4d2995f commit d5c5c47
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 603 deletions.
30 changes: 23 additions & 7 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@ export interface IConfig {
port: number;
// comma separated list of API keys (API_KEYS)
api_keys: string[];
// RPC endpoint (RPC)
rpcETH: string;
rpcFLR: string;
rpcSGB: string;
// // RPC endpoint (RPC)
// rpcETH: string;
// rpcFLR: string;
// rpcSGB: string;
getRPCSource: IGetRPCSource;
isTestnet: boolean;
}

export type EVMTransactionSources = "ETH" | "FLR" | "SGB";
export type IGetRPCSource = (source: EVMTransactionSources) => string;

function getRPCSource(source: EVMTransactionSources, config: IConfig): string {
switch (source) {
case "ETH":
return process.env.RPC_ETH || "https://flare-api.flare.network/ext/C/rpc";
case "FLR":
return process.env.RPC_FLR || "https://flare-api.flare.network/ext/C/rpc";
case "SGB":
return process.env.RPC_SGB || "https://flare-api.flare.network/ext/C/rpc";
}
}

export default () => {
const api_keys = process.env.API_KEYS?.split(",") || [""];
const isTestnet = process.env.TESTNET == "true";
const config: IConfig = {
port: parseInt(process.env.PORT || "3000"),
api_keys,
rpcFLR: process.env.RPC_FLR || "https://flare-api.flare.network/ext/C/rpc",
rpcETH: process.env.RPC_ETH || "https://flare-api.flare.network/ext/C/rpc",
rpcSGB: process.env.RPC_SGB || "https://flare-api.flare.network/ext/C/rpc",
getRPCSource: (source: EVMTransactionSources) => getRPCSource(source, config),
isTestnet,
};
return config;
};
73 changes: 7 additions & 66 deletions src/controller/eth/eth-evm-transaction-verifier.controller.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,12 @@
///////////////////////////////////////////////////////////////
// THIS IS GENERATED CODE. DO NOT CHANGE THIS FILE MANUALLY .//
///////////////////////////////////////////////////////////////

import { Body, Controller, HttpCode, Post, UseGuards } from "@nestjs/common";
import { ApiSecurity, ApiTags } from "@nestjs/swagger";
import { ApiKeyAuthGuard } from "../../auth/apikey.guard";

import { ApiTags } from "@nestjs/swagger";
import { ETHEVMTransactionVerifierService } from "../../service/eth/eth-evm-transaction-verifier.service";
import { AttestationResponseDTO_EVMTransaction_Response, EVMTransaction_RequestNoMic } from "../../dto/EVMTransaction.dto";
import { EncodedRequest, MicResponse, EncodedRequestResponse } from "../../dto/generic.dto";
import { AttestationResponseDTO_EVMTransaction_ResponseEncoded } from "../../dto/fdcTransactions.dto";
import { EVMTransactionVerifierControllerBase } from "../evm-transaction-verifier-base.controller";
import { Controller } from "@nestjs/common";

@ApiTags("EVMTransaction")
@ApiTags("EVMTransaction", "ETH")
@Controller("eth/EVMTransaction")
@UseGuards(ApiKeyAuthGuard)
@ApiSecurity("X-API-KEY")
export class ETHEVMTransactionVerifierController {
constructor(private readonly verifierService: ETHEVMTransactionVerifierService) {}

/**
*
* Tries to verify encoded attestation request without checking message integrity code, and if successful it returns response.
* @param verifierBody
* @deprecated
* @returns
*/
@HttpCode(200)
@Post()
async verify(@Body() body: EncodedRequest): Promise<AttestationResponseDTO_EVMTransaction_Response> {
return this.verifierService.verifyEncodedRequest(body.abiEncodedRequest!);
}

@HttpCode(200)
@Post("verifyFDC")
async verifyFDC(@Body() body: EncodedRequest): Promise<AttestationResponseDTO_EVMTransaction_ResponseEncoded> {
return this.verifierService.verifyEncodedRequestFDC(body.abiEncodedRequest!);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code, and if successful it returns response.
* @param prepareResponseBody
* @returns
*/
@HttpCode(200)
@Post("prepareResponse")
async prepareResponse(@Body() body: EVMTransaction_RequestNoMic): Promise<AttestationResponseDTO_EVMTransaction_Response> {
return this.verifierService.prepareResponse(body);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code, and if successful, it returns the correct message integrity code.
* @param body
*/
@HttpCode(200)
@Post("mic")
async mic(@Body() body: EVMTransaction_RequestNoMic): Promise<MicResponse> {
return this.verifierService.mic(body);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code.
* If successful, it returns the encoding of the attestation request with the correct message integrity code, which can be directly submitted to the State Connector contract.
* @param body
*/
@HttpCode(200)
@Post("prepareRequest")
async prepareRequest(@Body() body: EVMTransaction_RequestNoMic): Promise<EncodedRequestResponse> {
return this.verifierService.prepareRequest(body);
export class ETHEVMTransactionVerifierController extends EVMTransactionVerifierControllerBase {
constructor(protected readonly verifierService: ETHEVMTransactionVerifierService) {
super();
}
}
70 changes: 70 additions & 0 deletions src/controller/evm-transaction-verifier-base.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Body, HttpCode, Post, UseGuards } from "@nestjs/common";
import { ApiSecurity, ApiTags } from "@nestjs/swagger";
import { ApiKeyAuthGuard } from "../auth/apikey.guard";
import { AttestationResponseDTO_EVMTransaction_Response, EVMTransaction_RequestNoMic } from "../dto/EVMTransaction.dto";
import { AttestationResponseDTO_EVMTransaction_ResponseEncoded } from "../dto/fdcTransactions.dto";
import { EncodedRequest, EncodedRequestResponse, MicResponse } from "../dto/generic.dto";
import { EVMTransactionVerifierServiceBase } from "../service/evm-transaction-verification-base.service";

@ApiTags("EVMTransaction")
@UseGuards(ApiKeyAuthGuard)
@ApiSecurity("X-API-KEY")
export abstract class EVMTransactionVerifierControllerBase {
protected readonly verifierService: EVMTransactionVerifierServiceBase;

/**
*
* Tries to verify encoded attestation request without checking message integrity code, and if successful it returns response in json form.
* @param body
* @deprecated
* @returns json of AttestationResponse
*/
@HttpCode(200)
@Post()
async verify(@Body() body: EncodedRequest): Promise<AttestationResponseDTO_EVMTransaction_Response> {
return this.verifierService.verifyEncodedRequest(body.abiEncodedRequest!);
}

/**
* Tries to verify encoded attestation request without checking message integrity code, and if successful it returns response in abi encoded form.
* @param body
* @returns abi encoded AttestationResponse
*/
@HttpCode(200)
@Post("verifyFDC")
async verifyFDC(@Body() body: EncodedRequest): Promise<AttestationResponseDTO_EVMTransaction_ResponseEncoded> {
return this.verifierService.verifyEncodedRequestFDC(body.abiEncodedRequest!);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code, and if successful it returns response.
* @param prepareResponseBody
* @returns
*/
@HttpCode(200)
@Post("prepareResponse")
async prepareResponse(@Body() body: EVMTransaction_RequestNoMic): Promise<AttestationResponseDTO_EVMTransaction_Response> {
return this.verifierService.prepareResponse(body);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code, and if successful, it returns the correct message integrity code.
* @param body
*/
@HttpCode(200)
@Post("mic")
async mic(@Body() body: EVMTransaction_RequestNoMic): Promise<MicResponse> {
return this.verifierService.mic(body);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code.
* If successful, it returns the encoding of the attestation request with the correct message integrity code, which can be directly submitted to the State Connector contract.
* @param body
*/
@HttpCode(200)
@Post("prepareRequest")
async prepareRequest(@Body() body: EVMTransaction_RequestNoMic): Promise<EncodedRequestResponse> {
return this.verifierService.prepareRequest(body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { ApiKeyStrategy } from "../../auth/apikey.strategy";
import { AuthModule } from "../../auth/auth.module";
import { AuthService } from "../../auth/auth.service";
import configuration from "../../config/configuration";
import { ETHEVMTransactionVerifierService } from "../../service/eth/eth-evm-transaction-verifier.service";
import { ETHEVMTransactionVerifierController } from "./eth-evm-transaction-verifier.controller";
import { FLREVMTransactionVerifierController } from "./flr-evm-transaction-verifier.controller";
import { FLREVMTransactionVerifierService } from "../../service/flr/flr-evm-transaction-verifier.service";

const EXAMPLE_ENCODED_REQUEST =
"0x45564d5472616e73616374696f6e000000000000000000000000000000000000455448000000000000000000000000000000000000000000000000000000000087e8d91c3d07630fe2bf9b85c36d85cfe2d03ba5e63ac054c22ccb0110bebd310000000000000000000000000000000000000000000000000000000000000020e1ad057e71ac82cd2eaaee0dc8700a2c1b6cff4f295a7674b9e97a5f8dd9b51c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000";
"0x45564d5472616e73616374696f6e000000000000000000000000000000000000464c520000000000000000000000000000000000000000000000000000000000b046e06fc597c21b0d329ce4c1df55bef28d2fd1f261d6ee35f05f21790e51e60000000000000000000000000000000000000000000000000000000000000020e1ad057e71ac82cd2eaaee0dc8700a2c1b6cff4f295a7674b9e97a5f8dd9b51c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000";

const EXAMPLE_REQUEST_NOMIC = {
attestationType: "0x45564d5472616e73616374696f6e000000000000000000000000000000000000",
sourceId: "0x4554480000000000000000000000000000000000000000000000000000000000",
sourceId: "0x464c520000000000000000000000000000000000000000000000000000000000",
requestBody: {
transactionHash: "0xe1ad057e71ac82cd2eaaee0dc8700a2c1b6cff4f295a7674b9e97a5f8dd9b51c",
requiredConfirmations: "1",
Expand All @@ -26,7 +26,7 @@ const EXAMPLE_RESPONSE = {
status: "VALID",
response: {
attestationType: "0x45564d5472616e73616374696f6e000000000000000000000000000000000000",
sourceId: "0x4554480000000000000000000000000000000000000000000000000000000000",
sourceId: "0x464c520000000000000000000000000000000000000000000000000000000000",
votingRound: "0",
lowestUsedTimestamp: "1696806248",
requestBody: {
Expand Down Expand Up @@ -62,10 +62,10 @@ const EXAMPLE_RESPONSE = {
},
};

const EXPECTED_MIC = "0x87e8d91c3d07630fe2bf9b85c36d85cfe2d03ba5e63ac054c22ccb0110bebd31";
const EXPECTED_MIC = "0xb046e06fc597c21b0d329ce4c1df55bef28d2fd1f261d6ee35f05f21790e51e6";

describe("AppController", () => {
let appController: ETHEVMTransactionVerifierController;
describe("AppController for FLR (Must have FLR mainnet RPC configure in .env)", () => {
let appController: FLREVMTransactionVerifierController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
Expand All @@ -76,11 +76,11 @@ describe("AppController", () => {
}),
AuthModule,
],
controllers: [ETHEVMTransactionVerifierController],
providers: [ApiKeyStrategy, AuthService, ETHEVMTransactionVerifierService],
controllers: [FLREVMTransactionVerifierController],
providers: [ApiKeyStrategy, AuthService, FLREVMTransactionVerifierService],
}).compile();

appController = app.get<ETHEVMTransactionVerifierController>(ETHEVMTransactionVerifierController);
appController = app.get<FLREVMTransactionVerifierController>(FLREVMTransactionVerifierController);
});

describe("root", () => {
Expand All @@ -91,15 +91,18 @@ describe("AppController", () => {
expect(actualRes.status).toEqual("VALID");
expect(JSON.parse(JSON.stringify(actualRes.response))).toStrictEqual(EXAMPLE_RESPONSE.response);
});

it("should prepare response", async () => {
const actualRes = await appController.prepareResponse(EXAMPLE_REQUEST_NOMIC);
expect(actualRes.status).toEqual("VALID");
expect(JSON.parse(JSON.stringify(actualRes.response))).toStrictEqual(EXAMPLE_RESPONSE.response);
});

it("should obtain 'mic'", async () => {
const actualMic = await appController.mic(EXAMPLE_REQUEST_NOMIC);
expect(actualMic.messageIntegrityCode).toStrictEqual(EXPECTED_MIC);
});

it("should prepare request", async () => {
const actualRequest = await appController.prepareRequest(EXAMPLE_REQUEST_NOMIC);
expect(actualRequest.abiEncodedRequest).toStrictEqual(EXAMPLE_ENCODED_REQUEST);
Expand Down
72 changes: 7 additions & 65 deletions src/controller/flr/flr-evm-transaction-verifier.controller.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,12 @@
///////////////////////////////////////////////////////////////
// THIS IS GENERATED CODE. DO NOT CHANGE THIS FILE MANUALLY .//
///////////////////////////////////////////////////////////////

import { Body, Controller, HttpCode, Post, UseGuards } from "@nestjs/common";
import { ApiSecurity, ApiTags } from "@nestjs/swagger";
import { ApiKeyAuthGuard } from "../../auth/apikey.guard";

import { Controller } from "@nestjs/common";
import { ApiTags } from "@nestjs/swagger";
import { FLREVMTransactionVerifierService } from "../../service/flr/flr-evm-transaction-verifier.service";
import { AttestationResponseDTO_EVMTransaction_Response, EVMTransaction_RequestNoMic } from "../../dto/EVMTransaction.dto";
import { EncodedRequest, MicResponse, EncodedRequestResponse } from "../../dto/generic.dto";
import { AttestationResponseDTO_EVMTransaction_ResponseEncoded } from "../../dto/fdcTransactions.dto";
import { EVMTransactionVerifierControllerBase } from "../evm-transaction-verifier-base.controller";

@ApiTags("EVMTransaction")
@ApiTags("EVMTransaction", "FLR")
@Controller("flr/EVMTransaction")
@UseGuards(ApiKeyAuthGuard)
@ApiSecurity("X-API-KEY")
export class FLREVMTransactionVerifierController {
constructor(private readonly verifierService: FLREVMTransactionVerifierService) {}

/**
*
* Tries to verify encoded attestation request without checking message integrity code, and if successful it returns response.
* @param verifierBody
* @returns
*/
@HttpCode(200)
@Post()
async verify(@Body() body: EncodedRequest): Promise<AttestationResponseDTO_EVMTransaction_Response> {
return this.verifierService.verifyEncodedRequest(body.abiEncodedRequest!);
}

@HttpCode(200)
@Post("verifyFDC")
async verifyFDC(@Body() body: EncodedRequest): Promise<AttestationResponseDTO_EVMTransaction_ResponseEncoded> {
return this.verifierService.verifyEncodedRequestFDC(body.abiEncodedRequest!);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code, and if successful it returns response.
* @param prepareResponseBody
* @returns
*/
@HttpCode(200)
@Post("prepareResponse")
async prepareResponse(@Body() body: EVMTransaction_RequestNoMic): Promise<AttestationResponseDTO_EVMTransaction_Response> {
return this.verifierService.prepareResponse(body);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code, and if successful, it returns the correct message integrity code.
* @param body
*/
@HttpCode(200)
@Post("mic")
async mic(@Body() body: EVMTransaction_RequestNoMic): Promise<MicResponse> {
return this.verifierService.mic(body);
}

/**
* Tries to verify attestation request (given in JSON) without checking message integrity code.
* If successful, it returns the encoding of the attestation request with the correct message integrity code, which can be directly submitted to the State Connector contract.
* @param body
*/
@HttpCode(200)
@Post("prepareRequest")
async prepareRequest(@Body() body: EVMTransaction_RequestNoMic): Promise<EncodedRequestResponse> {
return this.verifierService.prepareRequest(body);
export class FLREVMTransactionVerifierController extends EVMTransactionVerifierControllerBase {
constructor(protected readonly verifierService: FLREVMTransactionVerifierService) {
super();
}
}
Loading

0 comments on commit d5c5c47

Please sign in to comment.