Skip to content

Commit

Permalink
Check if the prefix of the shop id is available
Browse files Browse the repository at this point in the history
  • Loading branch information
danial303065 authored and MichaelKim20 committed Aug 7, 2024
1 parent f87530f commit daffcd6
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/contracts/src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/test/03-Shop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
16 changes: 8 additions & 8 deletions packages/contracts/test/04-Ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/test/06-ShopId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion packages/contracts/test/08-Ledger-Provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
1 change: 1 addition & 0 deletions packages/relay/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ relay:
encryptKey: "${RELAY_ENCRYPT_KEY}"
testMode: ${RELAY_TEST_MODE}
bridgeActiveStatus: true
allowedShopIdPrefix: "0x0001"

contracts:
sideChain:
Expand Down
1 change: 1 addition & 0 deletions packages/relay/config/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ relay:
encryptKey: "${RELAY_ENCRYPT_KEY}"
testMode: ${RELAY_TEST_MODE}
bridgeActiveStatus: true
allowedShopIdPrefix: "0x0001"

contracts:
sideChain:
Expand Down
5 changes: 5 additions & 0 deletions packages/relay/src/common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -248,6 +250,7 @@ export class RelayConfig implements IRelayConfig {
encryptKey: "",
testMode: false,
bridgeActiveStatus: true,
allowedShopIdPrefix: "0x0001",
};
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -488,6 +492,7 @@ export interface IRelayConfig {
encryptKey: string;
testMode: boolean;
bridgeActiveStatus: boolean;
allowedShopIdPrefix: string;
}

export interface IContractsConfig {
Expand Down
6 changes: 5 additions & 1 deletion packages/relay/src/routers/PaymentRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
Expand Down
18 changes: 15 additions & 3 deletions packages/relay/src/routers/ShopRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -554,14 +558,18 @@ 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();
if (accessKey !== this.config.relay.accessKey) {
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();

Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/relay/src/utils/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/test/DelegatorApproval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/relay/test/Endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
8 changes: 4 additions & 4 deletions packages/relay/test/Payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/relay/test/Shop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/relay/test/ShopWithdraw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/relay/test/TempararyAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down

0 comments on commit daffcd6

Please sign in to comment.