From c4dcc1602705a21356a6a16db4d34202f94f9c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFs=20Mansot?= <26844641+devfull@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:09:06 +0200 Subject: [PATCH] add `SecureService` --- src/common/config/variables/Variables.ts | 12 +++- .../common/SecureService/SecureService.ts | 59 +++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 src/services/common/SecureService/SecureService.ts diff --git a/src/common/config/variables/Variables.ts b/src/common/config/variables/Variables.ts index d865df6e..14e5e0a6 100644 --- a/src/common/config/variables/Variables.ts +++ b/src/common/config/variables/Variables.ts @@ -73,11 +73,16 @@ export class BackendVariables { @IsNotEmpty() public readonly MAILCHIMP_API_KEY!: string; + @IsNotEmpty() + public readonly SECURE_API_KEY!: string; + + @IsNotEmpty() + public readonly SECURE_API_BASE_URL!: string; + @IsNotEmpty() public readonly ENV!: string; public constructor() { - dotenv.config(); this.DATABASE_PORT = process.env["DATABASE_PORT"]!; this.DATABASE_HOST = process.env["DATABASE_HOST"]!; @@ -102,10 +107,11 @@ export class BackendVariables { this.ACCESS_TOKEN_SECRET = process.env["ACCESS_TOKEN_SECRET"]!; this.REFRESH_TOKEN_SECRET = process.env["REFRESH_TOKEN_SECRET"]!; this.MAILCHIMP_API_KEY = process.env["MAILCHIMP_API_KEY"]!; + this.SECURE_API_KEY = process.env["SECURE_API_KEY"]!; + this.SECURE_API_BASE_URL = process.env["SECURE_API_BASE_URL"]!; this.ENV = process.env["ENV"]!; - } - public async validate(groups?: string[]) { + public async validate(groups?: string[]) { const validationOptions = groups ? { groups } : undefined; try { diff --git a/src/services/common/SecureService/SecureService.ts b/src/services/common/SecureService/SecureService.ts new file mode 100644 index 00000000..6ff422df --- /dev/null +++ b/src/services/common/SecureService/SecureService.ts @@ -0,0 +1,59 @@ +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; +import { BackendVariables } from "@Common/config/variables/Variables"; + +@Service() +export default class SecureService extends BaseService { + constructor(protected variables: BackendVariables) { + super(); + } + + /** + * @description : anchor a sequence of hashes + * @throws {Error} If secure job cannot be created + */ + public async anchor(hash_sources: string[]) { + const url = new URL(this.variables.SECURE_API_BASE_URL.concat("/flows/v2/anchor")); + + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + apiKey: this.variables.SECURE_API_KEY, + }, + body: JSON.stringify({ + hash_sources, + callback_url: "", + callback_config: {}, + }), + }); + + return await response.json(); + } + + /** + * @description : verify if a sequence of hashes is anchored + * @throws {Error} If secure job cannot be found + */ + public async verify(hash_sources: string[]) { + const params = new URLSearchParams(); + + hash_sources.forEach((hash) => { + params.append("hash_sources", hash); + }); + + const url = new URL(this.variables.SECURE_API_BASE_URL.concat("/flows/v2/verify?").concat(params.toString())); + + const response = await fetch(url, { + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + apiKey: this.variables.SECURE_API_KEY, + }, + }); + + return await response.json(); + } +}