diff --git a/src/ainize.ts b/src/ainize.ts index 1749b60..d99bff0 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -49,6 +49,7 @@ export default class Ainize { async loginWithSigner() { const signer = new AinWalletSigner; this.ain.setSigner(signer); + await this.handler.connect(); console.log('login success! address: ', await this.ain.getAddress()); } diff --git a/src/middlewares/middleware.ts b/src/middlewares/middleware.ts index 1b36249..825c0df 100644 --- a/src/middlewares/middleware.ts +++ b/src/middlewares/middleware.ts @@ -21,19 +21,25 @@ export default class Middleware { */ blockchainTriggerFilter = async (req: Request, res: Response, next: NextFunction) => { //check if request is from blockchain trigger - const { triggerPath, triggerValue, txHash } = extractTriggerDataFromRequest(req); - if(!triggerPath || !triggerValue || !txHash) { - res.send("Not from blockchain"); + try { + const { triggerPath, triggerValue, txHash } = extractTriggerDataFromRequest(req); + if(!triggerPath || !triggerValue || !txHash) { + throw new Error("Not from blockchain"); + } + const result = await this.ain.getValue(triggerPath); + + // If request is first request, set cache + if (this.cache.get(txHash) && this.cache.get(txHash) !== "error") { + res.send("Duplicated"); + return; + } + this.cache.set(txHash, "in_progress", 500); + _.isEqual(result, triggerValue) ? next(): res.send("Not from blockchain"); + } catch (e) { + console.log("Filtering Error ", e) + res.send(e); return; } - 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; - } - this.cache.set(txHash, "in_progress", 500); - _.isEqual(result, triggerValue) ? next(): res.send("Not from blockchain"); } /** * DEPRECATED