-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
blockchainTriggerFilter & deploy url check #93
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) ? 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; | ||
} |
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result와 triggerValue 가 같으면 not from blockChain인가요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반대로 적은 것 같네요 수정하였습니다!