-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from ainize-team/feat/woojae/blockchainFilter
blockchainTriggerFilter & deploy url check
- Loading branch information
Showing
5 changed files
with
66 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) ? next(): res.send("not from blockChain"); | ||
} | ||
/** | ||
* 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |