From a9fb37ba4f954ef5239053d04954e606b78cd9c0 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Thu, 4 Jan 2024 15:09:14 +0900 Subject: [PATCH 1/3] blockchainFilter --- src/ainize.ts | 1 + src/internal.ts | 23 +++--------------- src/middlewares/middleware.ts | 44 +++++++++++++--------------------- src/utils/extractor.ts | 45 +++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 48 deletions(-) create mode 100644 src/utils/extractor.ts diff --git a/src/ainize.ts b/src/ainize.ts index 4bfc00f..5b739b3 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -83,6 +83,7 @@ export default class Ainize { if (!serviceUrl) { serviceUrl = `https://${serviceName}.ainetwork.xyz`; } + serviceUrl = serviceUrl.replace(/\/$/, ''); const servicePath = Path.app(serviceName).status(); await this.handler.subscribe(servicePath, resolve); await this.appController.createApp({ appName: serviceName, serviceUrl, billingConfig }); diff --git a/src/internal.ts b/src/internal.ts index cd96ea0..57b676e 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -4,6 +4,7 @@ import { getChangeBalanceOp, getResponseOp, getWriteHistoryOp } from "./utils/op import { HISTORY_TYPE, RESPONSE_STATUS, deposit, request, response } from "./types/type"; import { buildTxBody } from "./utils/builder"; import AinModule from "./ain"; +import { extractDataFromDepositRequest, extractDataFromServiceRequest } from "./utils/extractor"; export default class Internal { private ain = AinModule.getInstance(); @@ -35,28 +36,10 @@ export default class Internal { } getDataFromServiceRequest(req: Request) { - if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[4] || !req.body.value) { - throw new Error("Not from service request"); - } - const requestData: request = { - appName: req.body.valuePath[1], - requesterAddress: req.body.auth.addr, - requestKey: req.body.valuePath[4], - requestData: req.body.value, - } - return requestData; + return extractDataFromServiceRequest(req); } private getDataFromDepositRequest(req: Request) { - if(!req.body.valuePath[1] || !req.body.valuePath[4] || !req.body.value) { - throw new Error("Not from deposit request"); - } - const depositData: deposit = { - transferKey: req.body.valuePath[4], - transferValue: req.body.value, - appName: req.body.valuePath[1], - requesterAddress: req.body.auth.addr, - } - return depositData; + return extractDataFromDepositRequest(req); } } \ No newline at end of file diff --git a/src/middlewares/middleware.ts b/src/middlewares/middleware.ts index e619353..f096085 100644 --- a/src/middlewares/middleware.ts +++ b/src/middlewares/middleware.ts @@ -1,51 +1,39 @@ import { Request, Response, NextFunction } from "express"; import NodeCache = require("node-cache"); +import { extractTriggerDataFromRequest } from "../utils/extractor"; +import AinModule from "../ain"; +import _ from "lodash"; export default class Middleware { cache: NodeCache; - constructor(cache: NodeCache,) { + private ain = AinModule.getInstance(); + constructor(cache: NodeCache) { this.cache = cache; } /** * Middleware for AI Network trigger call. It will filter duplicated request triggered by same transaction. - * It will pass request which is not from AI Network trigger. + * It will reject requests which is not from AI Network trigger. * @param {Request} request - Request data * @param {Res} response - Response data * @param {NextFunction} next - Next function * @returns Null if if request is duplicated. */ - triggerDuplicateFilter = (req: Request, res: Response, next: NextFunction) => { - if (req.body.transaction.hash === undefined){ - next(); - } - const txHash = req.body.transaction.hash; + blockchainTriggerFilter = async (req: Request, res: Response, next: NextFunction) => { + //check if request is from blockchain trigger + const { triggerPath, triggerValue, txHash } = extractTriggerDataFromRequest(req); + const result = await this.ain.getValue(triggerPath); + // if request is first reque st, set cache if (this.cache.get(txHash) && this.cache.get(txHash) !== "error") { res.send("duplicated"); return; } - // if request is first request, set cache this.cache.set(txHash, "in_progress", 500); - next(); + _.isEqual(result, triggerValue) ? res.send("not from blockChain") : next(); } - /** - * Middleware for AI Network trigger call. It will filter duplicated request triggered by same transaction. - * It will pass request which is not from AI Network trigger. - * You can set filter inside specific api. - * @param {Request} request - Request data - * @param {Res} response - Response data - * @returns Null if if request is duplicated. + /** + * DEPRECATED + * use blockchainTriggerFilter instead */ - triggerFilter = (req: Request, res: Response) => { - if (req.body.fid || req.body.transaction){ - res.send("not from trigger"); - return; - } - const txHash = req.body.transaction.hash; - if (this.cache.get(txHash) && this.cache.get(txHash) !== "error") { - res.send("duplicated"); - return; - } - this.cache.set(txHash, "in_progress", 500); - } + triggerDuplicateFilter = this.blockchainTriggerFilter; } \ No newline at end of file diff --git a/src/utils/extractor.ts b/src/utils/extractor.ts new file mode 100644 index 0000000..47be1f4 --- /dev/null +++ b/src/utils/extractor.ts @@ -0,0 +1,45 @@ +import { Request } from "express"; +import { deposit, request } from "../types/type"; + +export const extractDataFromServiceRequest = (req:Request) => { + if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[4] || !req.body.value) { + throw new Error("Not from service request"); + } + const requestData: request = { + appName: req.body.valuePath[1], + requesterAddress: req.body.auth.addr, + requestKey: req.body.valuePath[4], + requestData: req.body.value, + } + return requestData; +} + +export const extractTriggerDataFromRequest = (req:Request) => { + if(req?.body?.valuePath === undefined) { + throw new Error("Not from trigger request"); + } + let path = ''; + req.body.valuePath.forEach((value:string) => { + path += value + '/'; + } + ); + const triggerData = { + triggerPath: path, + triggerValue: req.body.value, + txHash: req.body.txhash, + } + return triggerData +} + +export const extractDataFromDepositRequest = (req:Request) => { + if(!req.body.valuePath[1] || !req.body.valuePath[4] || !req.body.value) { + throw new Error("Not from deposit request"); + } + const depositData: deposit = { + transferKey: req.body.valuePath[4], + transferValue: req.body.value, + appName: req.body.valuePath[1], + requesterAddress: req.body.auth.addr, + } + return depositData; +} \ No newline at end of file From 1383ce3720dc924cff32d171ce6316a4461aafea Mon Sep 17 00:00:00 2001 From: akaster99 Date: Thu, 4 Jan 2024 15:36:14 +0900 Subject: [PATCH 2/3] fix: add await --- src/controllers/serviceController.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/serviceController.ts b/src/controllers/serviceController.ts index f5bbfbe..302c1ce 100644 --- a/src/controllers/serviceController.ts +++ b/src/controllers/serviceController.ts @@ -48,7 +48,7 @@ export default class ServiceController { } async chargeCredit(serviceName: string, amount: number): Promise { - this.checkRunning(serviceName); + await this.checkRunning(serviceName); const transferKey = Date.now(); const userAddress = this.ain.getAddress(); const depositAddress = await this.getDepositAddress(serviceName); @@ -78,7 +78,7 @@ export default class ServiceController { } async request(serviceName: string, requestData: any) : Promise { - this.checkRunning(serviceName); + await this.checkRunning(serviceName); const result = await new Promise(async (resolve, reject) => { const requestKey = Date.now(); const requesterAddress = this.ain.getAddress(); From 850695787293ede0cba140129d4f6aa5dd605cbe Mon Sep 17 00:00:00 2001 From: akaster99 Date: Mon, 22 Jan 2024 13:54:01 +0900 Subject: [PATCH 3/3] fix: if --- src/middlewares/middleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middlewares/middleware.ts b/src/middlewares/middleware.ts index f096085..f954533 100644 --- a/src/middlewares/middleware.ts +++ b/src/middlewares/middleware.ts @@ -29,7 +29,7 @@ export default class Middleware { return; } this.cache.set(txHash, "in_progress", 500); - _.isEqual(result, triggerValue) ? res.send("not from blockChain") : next(); + _.isEqual(result, triggerValue) ? next(): res.send("not from blockChain"); } /** * DEPRECATED