Skip to content

Commit 0989e4f

Browse files
authored
fix: Add validation on chain ID + slug (#739)
* fix: Add validation on chain ID + slug * undo * merge chain files
1 parent d8951fa commit 0989e4f

File tree

12 files changed

+152
-61
lines changed

12 files changed

+152
-61
lines changed

src/server/routes/contract/subscriptions/addContractSubscription.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getChain } from "../../../../utils/chain";
1313
import { thirdwebClient } from "../../../../utils/sdk";
1414
import { createCustomError } from "../../../middleware/error";
1515
import { AddressSchema } from "../../../schemas/address";
16+
import { chainIdOrSlugSchema } from "../../../schemas/chain";
1617
import {
1718
contractSubscriptionSchema,
1819
toContractSubscriptionSchema,
@@ -22,9 +23,7 @@ import { getChainIdFromChain } from "../../../utils/chain";
2223
import { isValidHttpUrl } from "../../../utils/validator";
2324

2425
const bodySchema = Type.Object({
25-
chain: Type.String({
26-
description: "The chain for the contract.",
27-
}),
26+
chain: chainIdOrSlugSchema,
2827
contractAddress: {
2928
...AddressSchema,
3029
description: "The address for the contract.",

src/server/routes/contract/subscriptions/getContractIndexedBlockRange.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { getContractEventLogsIndexedBlockRange } from "../../../../db/contractEventLogs/getContractEventLogs";
55
import { createCustomError } from "../../../middleware/error";
66
import { AddressSchema } from "../../../schemas/address";
7+
import { chainIdOrSlugSchema } from "../../../schemas/chain";
78
import {
89
contractParamSchema,
910
standardResponseSchema,
@@ -12,7 +13,7 @@ import { getChainIdFromChain } from "../../../utils/chain";
1213

1314
const responseSchema = Type.Object({
1415
result: Type.Object({
15-
chain: Type.String(),
16+
chain: chainIdOrSlugSchema,
1617
contractAddress: AddressSchema,
1718
fromBlock: Type.Number(),
1819
toBlock: Type.Number(),

src/server/routes/relayer/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { prisma } from "../../../db/client";
55
import { AddressSchema } from "../../schemas/address";
6+
import { chainIdOrSlugSchema } from "../../schemas/chain";
67
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
78
import { getChainIdFromChain } from "../../utils/chain";
89

910
const requestBodySchema = Type.Object({
1011
name: Type.Optional(Type.String()),
11-
chain: Type.String(),
12+
chain: chainIdOrSlugSchema,
1213
backendWalletAddress: {
1314
...AddressSchema,
1415
description:

src/server/routes/relayer/update.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { prisma } from "../../../db/client";
55
import { AddressSchema } from "../../schemas/address";
6+
import { chainIdOrSlugSchema } from "../../schemas/chain";
67
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
78
import { getChainIdFromChain } from "../../utils/chain";
89

910
const requestBodySchema = Type.Object({
1011
id: Type.String(),
1112
name: Type.Optional(Type.String()),
12-
chain: Type.Optional(Type.String()),
13+
chain: Type.Optional(chainIdOrSlugSchema),
1314
backendWalletAddress: Type.Optional(AddressSchema),
1415
allowedContracts: Type.Optional(Type.Array(Type.String())),
1516
allowedForwarders: Type.Optional(Type.Array(Type.String())),

src/server/routes/transaction/blockchain/getLogs.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ import { getChain } from "../../../../utils/chain";
1717
import { thirdwebClient } from "../../../../utils/sdk";
1818
import { createCustomError } from "../../../middleware/error";
1919
import { AddressSchema, TransactionHashSchema } from "../../../schemas/address";
20+
import { chainIdOrSlugSchema } from "../../../schemas/chain";
2021
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
2122
import { getChainIdFromChain } from "../../../utils/chain";
2223

2324
// INPUT
2425
const requestQuerystringSchema = Type.Object({
25-
chain: Type.String({
26-
examples: ["80002"],
27-
description: "Chain ID or name",
28-
}),
26+
chain: chainIdOrSlugSchema,
2927
queueId: Type.Optional(
3028
Type.String({
3129
description: "The queue ID for a mined transaction.",

src/server/routes/transaction/blockchain/getReceipt.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@ import {
1717
} from "../../../../utils/sdk";
1818
import { createCustomError } from "../../../middleware/error";
1919
import { AddressSchema, TransactionHashSchema } from "../../../schemas/address";
20+
import { chainIdOrSlugSchema } from "../../../schemas/chain";
2021
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
2122
import { getChainIdFromChain } from "../../../utils/chain";
2223

2324
// INPUT
2425
const requestSchema = Type.Object({
26+
chain: chainIdOrSlugSchema,
2527
transactionHash: {
2628
...TransactionHashSchema,
2729
description: "Transaction hash",
2830
},
29-
chain: Type.String({
30-
examples: ["80002"],
31-
description: "Chain ID or name",
32-
}),
3331
});
3432

3533
// OUTPUT

src/server/routes/transaction/blockchain/getUserOpReceipt.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ import { StatusCodes } from "http-status-codes";
44
import { env } from "../../../../utils/env";
55
import { createCustomError } from "../../../middleware/error";
66
import { TransactionHashSchema } from "../../../schemas/address";
7+
import { chainIdOrSlugSchema } from "../../../schemas/chain";
78
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
89
import { getChainIdFromChain } from "../../../utils/chain";
910

1011
// INPUT
1112
const requestSchema = Type.Object({
13+
chain: chainIdOrSlugSchema,
1214
userOpHash: {
1315
...TransactionHashSchema,
1416
description: "User operation hash",
1517
},
16-
chain: Type.String({
17-
examples: ["80002"],
18-
description: "Chain ID or name",
19-
}),
2018
});
2119

2220
// OUTPUT

src/server/schemas/chain/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Type } from "@sinclair/typebox";
22

3+
export const chainIdOrSlugSchema = Type.RegExp(/^[\w-]{1,50}$/, {
4+
description: `A chain ID ("137") or slug ("polygon-amoy-testnet"). Chain ID is preferred.`,
5+
examples: ["80002"],
6+
});
7+
38
export const chainRequestQuerystringSchema = Type.Object({
4-
chain: Type.String({
5-
description: "Chain name or ID",
6-
examples: ["1", "ethereum"],
7-
}),
9+
chain: chainIdOrSlugSchema,
810
});
911

1012
export const chainResponseSchema = Type.Partial(

src/server/schemas/prebuilts/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Type } from "@sinclair/typebox";
22
import { constants } from "ethers";
33
import { AddressSchema } from "../address";
4+
import { chainIdOrSlugSchema } from "../chain";
45

56
const MAX_BPS = 10000;
67

@@ -87,10 +88,7 @@ export const commonTrustedForwarderSchema = Type.Object({
8788
});
8889

8990
export const prebuiltDeployContractParamSchema = Type.Object({
90-
chain: Type.String({
91-
examples: ["80002"],
92-
description: "Chain ID or name",
93-
}),
91+
chain: chainIdOrSlugSchema,
9492
});
9593

9694
export const prebuiltDeployResponseSchema = Type.Object({

src/server/schemas/sharedApiSchemas.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { RouteGenericInterface } from "fastify";
44
import type { FastifySchema } from "fastify/types/schema";
55
import { StatusCodes } from "http-status-codes";
66
import { AddressSchema } from "./address";
7+
import { chainIdOrSlugSchema } from "./chain";
78

89
export const baseReplyErrorSchema = Type.Object({
910
message: Type.Optional(Type.String()),
@@ -17,10 +18,7 @@ export const baseReplyErrorSchema = Type.Object({
1718
* Basic schema for Request Parameters
1819
*/
1920
export const contractParamSchema = Type.Object({
20-
chain: Type.String({
21-
examples: ["80002"],
22-
description: "Chain ID or name",
23-
}),
21+
chain: chainIdOrSlugSchema,
2422
contractAddress: {
2523
...AddressSchema,
2624
description: "Contract address",
@@ -37,21 +35,15 @@ export const requestQuerystringSchema = Type.Object({
3735
});
3836

3937
export const prebuiltDeployParamSchema = Type.Object({
40-
chain: Type.String({
41-
examples: ["80002"],
42-
description: "Chain ID or name",
43-
}),
38+
chain: chainIdOrSlugSchema,
4439
contractType: Type.String({
4540
examples: Object.keys(PREBUILT_CONTRACTS_MAP),
4641
description: "Contract type to deploy",
4742
}),
4843
});
4944

5045
export const publishedDeployParamSchema = Type.Object({
51-
chain: Type.String({
52-
examples: ["80002"],
53-
description: "Chain ID or name",
54-
}),
46+
chain: chainIdOrSlugSchema,
5547
publisher: Type.String({
5648
examples: ["deployer.thirdweb.eth"],
5749
description: "Address or ENS of the publisher of the contract",
@@ -176,10 +168,7 @@ transactionWritesResponseSchema.example = {
176168
* Basic schema for ERC721 Contract Request Parameters
177169
*/
178170
export const erc20ContractParamSchema = Type.Object({
179-
chain: Type.String({
180-
examples: ["80002"],
181-
description: "Chain ID or name",
182-
}),
171+
chain: chainIdOrSlugSchema,
183172
contractAddress: {
184173
...AddressSchema,
185174
description: "ERC20 contract address",
@@ -190,10 +179,7 @@ export const erc20ContractParamSchema = Type.Object({
190179
* Basic schema for ERC721 Contract Request Parameters
191180
*/
192181
export const erc1155ContractParamSchema = Type.Object({
193-
chain: Type.String({
194-
examples: ["80002"],
195-
description: "Chain ID or name",
196-
}),
182+
chain: chainIdOrSlugSchema,
197183
contractAddress: {
198184
...AddressSchema,
199185
description: "ERC1155 contract address",
@@ -204,10 +190,7 @@ export const erc1155ContractParamSchema = Type.Object({
204190
* Basic schema for ERC721 Contract Request Parameters
205191
*/
206192
export const erc721ContractParamSchema = Type.Object({
207-
chain: Type.String({
208-
examples: ["80002"],
209-
description: "Chain ID or name",
210-
}),
193+
chain: chainIdOrSlugSchema,
211194
contractAddress: {
212195
...AddressSchema,
213196
description: "ERC721 contract address",
@@ -234,10 +217,7 @@ export enum Status {
234217
}
235218

236219
export const marketplaceV3ContractParamSchema = Type.Object({
237-
chain: Type.String({
238-
examples: ["80002"],
239-
description: "Chain ID or name",
240-
}),
220+
chain: chainIdOrSlugSchema,
241221
contractAddress: {
242222
...AddressSchema,
243223
description: "Contract address",

src/server/schemas/wallet/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Type } from "@sinclair/typebox";
2-
import { type Address, getAddress } from "thirdweb";
2+
import { getAddress, type Address } from "thirdweb";
33
import { env } from "../../../utils/env";
44
import { badAddressError } from "../../middleware/error";
55
import { AddressSchema } from "../address";
6+
import { chainIdOrSlugSchema } from "../chain";
67

78
export const walletHeaderSchema = Type.Object({
89
"x-backend-wallet-address": {
@@ -66,10 +67,7 @@ export function requiredAddress(
6667
}
6768

6869
export const walletChainParamSchema = Type.Object({
69-
chain: Type.String({
70-
examples: ["80002"],
71-
description: "Chain ID",
72-
}),
70+
chain: chainIdOrSlugSchema,
7371
});
7472

7573
export const walletWithAddressParamSchema = Type.Object({

0 commit comments

Comments
 (0)