From daffcd6f436c4d09fe1ff7fb1d6fbdf4598b2740 Mon Sep 17 00:00:00 2001 From: Danial Date: Wed, 7 Aug 2024 15:01:26 +0900 Subject: [PATCH] Check if the prefix of the shop id is available --- packages/contracts/src/utils/ContractUtils.ts | 4 ++-- packages/contracts/test/03-Shop.test.ts | 2 +- packages/contracts/test/04-Ledger.test.ts | 16 ++++++++-------- packages/contracts/test/06-ShopId.test.ts | 2 +- .../contracts/test/08-Ledger-Provider.test.ts | 2 +- packages/relay/config/config.yaml | 1 + packages/relay/config/config_test.yaml | 1 + packages/relay/src/common/Config.ts | 5 +++++ packages/relay/src/routers/PaymentRouter.ts | 6 +++++- packages/relay/src/routers/ShopRouter.ts | 18 +++++++++++++++--- packages/relay/src/utils/ContractUtils.ts | 4 ++-- packages/relay/src/utils/Errors.ts | 1 + packages/relay/test/DelegatorApproval.test.ts | 2 +- packages/relay/test/Endpoints.test.ts | 2 +- packages/relay/test/Payment.test.ts | 8 ++++---- packages/relay/test/Shop.test.ts | 2 +- packages/relay/test/ShopWithdraw.test.ts | 2 +- packages/relay/test/TempararyAccount.test.ts | 2 +- 18 files changed, 52 insertions(+), 28 deletions(-) diff --git a/packages/contracts/src/utils/ContractUtils.ts b/packages/contracts/src/utils/ContractUtils.ts index 17b0f585..ca7e2f05 100644 --- a/packages/contracts/src/utils/ContractUtils.ts +++ b/packages/contracts/src/utils/ContractUtils.ts @@ -24,8 +24,8 @@ import { verifyMessage } from "@ethersproject/wallet"; import * as hre from "hardhat"; export enum LoyaltyNetworkID { - LYT, - ACC, + ACC_TESTNET = 1, + ACC_MAINNET, } export class ContractUtils { diff --git a/packages/contracts/test/03-Shop.test.ts b/packages/contracts/test/03-Shop.test.ts index 5c3af579..b07272a6 100644 --- a/packages/contracts/test/03-Shop.test.ts +++ b/packages/contracts/test/03-Shop.test.ts @@ -103,7 +103,7 @@ describe("Test for Shop", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/contracts/test/04-Ledger.test.ts b/packages/contracts/test/04-Ledger.test.ts index d8285dfd..f07a3854 100644 --- a/packages/contracts/test/04-Ledger.test.ts +++ b/packages/contracts/test/04-Ledger.test.ts @@ -237,7 +237,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -1255,7 +1255,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -1425,7 +1425,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -1603,7 +1603,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -1890,7 +1890,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -2197,7 +2197,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -2469,7 +2469,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -2683,7 +2683,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/contracts/test/06-ShopId.test.ts b/packages/contracts/test/06-ShopId.test.ts index b257741d..6fec4aa8 100644 --- a/packages/contracts/test/06-ShopId.test.ts +++ b/packages/contracts/test/06-ShopId.test.ts @@ -5,7 +5,7 @@ import { ContractUtils, LoyaltyNetworkID } from "../src/utils/ContractUtils"; describe("Test for ShopId", () => { it("Shop ID", async () => { - const id = ContractUtils.getShopId("0xeDBFECF2D2D30fDd7b6D1D0975D679976954fF25", LoyaltyNetworkID.LYT); + const id = ContractUtils.getShopId("0xeDBFECF2D2D30fDd7b6D1D0975D679976954fF25", LoyaltyNetworkID.ACC_TESTNET); console.log(id); }); }); diff --git a/packages/contracts/test/08-Ledger-Provider.test.ts b/packages/contracts/test/08-Ledger-Provider.test.ts index c46dd513..d407f8f1 100644 --- a/packages/contracts/test/08-Ledger-Provider.test.ts +++ b/packages/contracts/test/08-Ledger-Provider.test.ts @@ -156,7 +156,7 @@ describe("Test for Ledger", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/relay/config/config.yaml b/packages/relay/config/config.yaml index a3fa3c26..d4586e3e 100644 --- a/packages/relay/config/config.yaml +++ b/packages/relay/config/config.yaml @@ -95,6 +95,7 @@ relay: encryptKey: "${RELAY_ENCRYPT_KEY}" testMode: ${RELAY_TEST_MODE} bridgeActiveStatus: true + allowedShopIdPrefix: "0x0001" contracts: sideChain: diff --git a/packages/relay/config/config_test.yaml b/packages/relay/config/config_test.yaml index 80e26ced..e84c4dff 100644 --- a/packages/relay/config/config_test.yaml +++ b/packages/relay/config/config_test.yaml @@ -95,6 +95,7 @@ relay: encryptKey: "${RELAY_ENCRYPT_KEY}" testMode: ${RELAY_TEST_MODE} bridgeActiveStatus: true + allowedShopIdPrefix: "0x0001" contracts: sideChain: diff --git a/packages/relay/src/common/Config.ts b/packages/relay/src/common/Config.ts index 4612cad5..ae617925 100644 --- a/packages/relay/src/common/Config.ts +++ b/packages/relay/src/common/Config.ts @@ -210,6 +210,7 @@ export class RelayConfig implements IRelayConfig { public encryptKey: string; public testMode: boolean; public bridgeActiveStatus: boolean; + public allowedShopIdPrefix: string; constructor() { const defaults = RelayConfig.defaultValue(); @@ -226,6 +227,7 @@ export class RelayConfig implements IRelayConfig { this.encryptKey = defaults.encryptKey; this.testMode = defaults.testMode; this.bridgeActiveStatus = defaults.bridgeActiveStatus; + this.allowedShopIdPrefix = defaults.allowedShopIdPrefix; } public static defaultValue(): IRelayConfig { @@ -248,6 +250,7 @@ export class RelayConfig implements IRelayConfig { encryptKey: "", testMode: false, bridgeActiveStatus: true, + allowedShopIdPrefix: "0x0001", }; } @@ -265,6 +268,7 @@ export class RelayConfig implements IRelayConfig { if (config.testMode !== undefined) this.testMode = config.testMode.toString().toLowerCase() === "true"; if (config.bridgeActiveStatus !== undefined) this.bridgeActiveStatus = config.bridgeActiveStatus.toString().toLowerCase() === "true"; + if (config.allowedShopIdPrefix !== undefined) this.allowedShopIdPrefix = config.allowedShopIdPrefix; } } @@ -488,6 +492,7 @@ export interface IRelayConfig { encryptKey: string; testMode: boolean; bridgeActiveStatus: boolean; + allowedShopIdPrefix: string; } export interface IContractsConfig { diff --git a/packages/relay/src/routers/PaymentRouter.ts b/packages/relay/src/routers/PaymentRouter.ts index 7456bdcf..196ca7aa 100644 --- a/packages/relay/src/routers/PaymentRouter.ts +++ b/packages/relay/src/routers/PaymentRouter.ts @@ -436,6 +436,11 @@ export class PaymentRouter { return res.status(200).json(ResponseMessage.getErrorMessage("2001", { validation: errors.array() })); } + const shopId: string = String(req.body.shopId).trim(); + if (shopId.substring(0, 6) !== this.config.relay.allowedShopIdPrefix) { + return res.status(200).json(ResponseMessage.getErrorMessage("3072")); + } + try { let accessKey = req.get("Authorization"); if (accessKey === undefined) accessKey = String(req.body.accessKey).trim(); @@ -458,7 +463,6 @@ export class PaymentRouter { const purchaseId: string = String(req.body.purchaseId).trim(); const amount: BigNumber = BigNumber.from(req.body.amount); const currency: string = String(req.body.currency).trim(); - const shopId: string = String(req.body.shopId).trim(); const feeRate = await this.contractManager.sideLedgerContract.getPaymentFee(); const rate = await this.contractManager.sideCurrencyRateContract.get(currency.toLowerCase()); diff --git a/packages/relay/src/routers/ShopRouter.ts b/packages/relay/src/routers/ShopRouter.ts index 7d435130..698ab414 100644 --- a/packages/relay/src/routers/ShopRouter.ts +++ b/packages/relay/src/routers/ShopRouter.ts @@ -427,9 +427,13 @@ export class ShopRouter { return res.status(200).json(ResponseMessage.getErrorMessage("2001", { validation: errors.array() })); } + const shopId = String(req.body.shopId).trim(); + if (shopId.substring(0, 6) !== this.config.relay.allowedShopIdPrefix) { + return res.status(200).json(ResponseMessage.getErrorMessage("3072")); + } + const signerItem = await this.getRelaySigner(); try { - const shopId: string = String(req.body.shopId).trim(); const name: string = String(req.body.name).trim(); const currency: string = String(req.body.currency).trim().toLowerCase(); const account: string = String(req.body.account).trim(); @@ -554,6 +558,11 @@ export class ShopRouter { return res.status(200).json(ResponseMessage.getErrorMessage("2001", { validation: errors.array() })); } + const shopId: string = String(req.body.shopId).trim(); + if (shopId.substring(0, 6) !== this.config.relay.allowedShopIdPrefix) { + return res.status(200).json(ResponseMessage.getErrorMessage("3072")); + } + try { let accessKey = req.get("Authorization"); if (accessKey === undefined) accessKey = String(req.body.accessKey).trim(); @@ -561,7 +570,6 @@ export class ShopRouter { return res.json(ResponseMessage.getErrorMessage("2002")); } - const shopId: string = String(req.body.shopId).trim(); const name: string = String(req.body.name).trim(); const currency: string = String(req.body.currency).trim().toLowerCase(); @@ -821,6 +829,11 @@ export class ShopRouter { return res.status(200).json(ResponseMessage.getErrorMessage("2001", { validation: errors.array() })); } + const shopId: string = String(req.body.shopId).trim(); + if (shopId.substring(0, 6) !== this.config.relay.allowedShopIdPrefix) { + return res.status(200).json(ResponseMessage.getErrorMessage("3072")); + } + const signerItem = await this.getRelaySigner(); try { let accessKey = req.get("Authorization"); @@ -829,7 +842,6 @@ export class ShopRouter { return res.json(ResponseMessage.getErrorMessage("2002")); } - const shopId: string = String(req.body.shopId).trim(); const status: number = Number(String(req.body.status).trim()); const shopInfo = await this.contractManager.sideShopContract.shopOf(shopId); if (shopInfo.status !== 0) { diff --git a/packages/relay/src/utils/ContractUtils.ts b/packages/relay/src/utils/ContractUtils.ts index 88978440..8e4f98a8 100644 --- a/packages/relay/src/utils/ContractUtils.ts +++ b/packages/relay/src/utils/ContractUtils.ts @@ -26,8 +26,8 @@ import * as crypto from "crypto"; import * as hre from "hardhat"; export enum LoyaltyNetworkID { - LYT, - PNB, + ACC_TESTNET = 1, + ACC_MAINNET, } export class ContractUtils { public static findLog(receipt: ContractReceipt, iface: Interface, eventName: string): Log | undefined { diff --git a/packages/relay/src/utils/Errors.ts b/packages/relay/src/utils/Errors.ts index db86479b..f99f4874 100644 --- a/packages/relay/src/utils/Errors.ts +++ b/packages/relay/src/utils/Errors.ts @@ -77,6 +77,7 @@ export class ResponseMessage { ["2033", "The task ID is not exist"], ["2040", "The status code for this task cannot be approved"], ["3001", "Bridge functionality is not yet available"], + ["3072", "This is not a permitted shop ID"], ["4000", "Denied by user"], ["5000", "Smart Contract Error"], ["6000", "Server Error"], diff --git a/packages/relay/test/DelegatorApproval.test.ts b/packages/relay/test/DelegatorApproval.test.ts index d9fc0077..fb31e2bb 100644 --- a/packages/relay/test/DelegatorApproval.test.ts +++ b/packages/relay/test/DelegatorApproval.test.ts @@ -115,7 +115,7 @@ describe("Test of Delegator", function () { context("Test delegator approval", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/relay/test/Endpoints.test.ts b/packages/relay/test/Endpoints.test.ts index 66dea7af..0b3608e8 100644 --- a/packages/relay/test/Endpoints.test.ts +++ b/packages/relay/test/Endpoints.test.ts @@ -140,7 +140,7 @@ describe("Test of Server", function () { context("Test token & point relay endpoints", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/relay/test/Payment.test.ts b/packages/relay/test/Payment.test.ts index 0eed4d26..b9c6ef14 100644 --- a/packages/relay/test/Payment.test.ts +++ b/packages/relay/test/Payment.test.ts @@ -156,7 +156,7 @@ describe("Test of Server", function () { context("Test point relay endpoints", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -640,7 +640,7 @@ describe("Test of Server", function () { context("Test point relay endpoints - Cancel Confirm", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -1046,7 +1046,7 @@ describe("Test of Server", function () { context("Test point relay endpoints - Cancel Deny", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); @@ -1513,7 +1513,7 @@ describe("Test of Server", function () { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/relay/test/Shop.test.ts b/packages/relay/test/Shop.test.ts index 61daac7f..8e277622 100644 --- a/packages/relay/test/Shop.test.ts +++ b/packages/relay/test/Shop.test.ts @@ -114,7 +114,7 @@ describe("Test for Shop", function () { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/relay/test/ShopWithdraw.test.ts b/packages/relay/test/ShopWithdraw.test.ts index d54d325d..c969e11f 100644 --- a/packages/relay/test/ShopWithdraw.test.ts +++ b/packages/relay/test/ShopWithdraw.test.ts @@ -208,7 +208,7 @@ describe("Test for Shop", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } }); diff --git a/packages/relay/test/TempararyAccount.test.ts b/packages/relay/test/TempararyAccount.test.ts index 0f6c9441..a1303c60 100644 --- a/packages/relay/test/TempararyAccount.test.ts +++ b/packages/relay/test/TempararyAccount.test.ts @@ -156,7 +156,7 @@ describe("Test of Server", function () { context("Test point relay endpoints", () => { before("Set Shop ID", async () => { for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); + elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.ACC_TESTNET); } });