Skip to content

Commit 5df28a2

Browse files
committed
adding credentials to all write endpoints
1 parent 63a6aac commit 5df28a2

File tree

28 files changed

+176
-132
lines changed

28 files changed

+176
-132
lines changed

src/server/middleware/rate-limit.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { env } from "../../shared/utils/env";
44
import { redis } from "../../shared/utils/redis/redis";
55
import { createCustomError } from "./error";
66
import { OPENAPI_ROUTES } from "./open-api";
7+
import { getTransactionCredentials } from "../../shared/lib/transaction/transaction-credentials";
8+
import { toClientId } from "../../shared/utils/sdk";
79

810
const SKIP_RATELIMIT_PATHS = ["/", ...OPENAPI_ROUTES];
911

@@ -13,10 +15,11 @@ export function withRateLimit(server: FastifyInstance) {
1315
return;
1416
}
1517

16-
const epochTimeInMinutes = Math.floor(new Date().getTime() / (1000 * 60));
17-
const key = `rate-limit:global:${epochTimeInMinutes}`;
18-
const count = await redis.incr(key);
19-
redis.expire(key, 2 * 60);
18+
const epochMinutes = Math.floor(new Date().getTime() / (1000 * 60));
19+
20+
const globalRateLimitKey = `rate-limit:global:${epochMinutes}`;
21+
const count = await redis.incr(globalRateLimitKey);
22+
redis.expire(globalRateLimitKey, 2 * 60);
2023

2124
if (count > env.GLOBAL_RATE_LIMIT_PER_MIN) {
2225
throw createCustomError(
@@ -25,5 +28,21 @@ export function withRateLimit(server: FastifyInstance) {
2528
"TOO_MANY_REQUESTS",
2629
);
2730
}
31+
32+
// Lite mode enforces a rate limit per team.
33+
if (env.ENGINE_MODE === "lite") {
34+
const { clientId } = getTransactionCredentials(request);
35+
const clientRateLimitKey = `rate-limit:client-id:${clientId}`;
36+
const count = await redis.incr(clientRateLimitKey);
37+
redis.expire(globalRateLimitKey, 2 * 60);
38+
39+
if (count > env.LITE_CLIENT_RATE_LIMIT_PER_MIN) {
40+
throw createCustomError(
41+
`${env.LITE_CLIENT_RATE_LIMIT_PER_MIN} requests/minute rate limit exceeded. Upgrade to Engine Standard to get a dedicated Engine without rate limits.`,
42+
StatusCodes.TOO_MANY_REQUESTS,
43+
"TOO_MANY_REQUESTS",
44+
);
45+
}
46+
}
2847
});
2948
}

src/server/routes/backend-wallet/sign-message.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getChain } from "../../../shared/utils/chain";
1212
import { createCustomError } from "../../middleware/error";
1313
import { standardResponseSchema } from "../../schemas/shared-api-schemas";
1414
import { walletHeaderSchema } from "../../schemas/wallet";
15+
import { getTransactionCredentials } from "../../../shared/lib/transaction/transaction-credentials";
1516

1617
const requestBodySchema = Type.Object({
1718
message: Type.String(),
@@ -46,6 +47,7 @@ export async function signMessageRoute(fastify: FastifyInstance) {
4647
const { message, isBytes, chainId } = request.body;
4748
const { "x-backend-wallet-address": walletAddress } =
4849
request.headers as Static<typeof walletHeaderSchema>;
50+
const credentials = getTransactionCredentials(request);
4951

5052
if (isBytes && !isHex(message)) {
5153
throw createCustomError(
@@ -71,6 +73,7 @@ export async function signMessageRoute(fastify: FastifyInstance) {
7173
const { account } = await walletDetailsToAccount({
7274
walletDetails,
7375
chain,
76+
credentials,
7477
});
7578

7679
const messageToSign = isBytes ? { raw: message as Hex } : message;

src/server/routes/contract/write/write.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { sanitizeAbi, sanitizeFunctionName } from "../../../utils/abi";
2323
import { getChainIdFromChain } from "../../../utils/chain";
2424
import { parseTransactionOverrides } from "../../../utils/transaction-overrides";
25+
import { getTransactionCredentials } from "../../../../shared/lib/transaction/transaction-credentials";
2526

2627
// INPUT
2728
const writeRequestBodySchema = Type.Object({
@@ -78,6 +79,7 @@ export async function writeToContract(fastify: FastifyInstance) {
7879
"x-account-factory-address": accountFactoryAddress,
7980
"x-account-salt": accountSalt,
8081
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
82+
const credentials = getTransactionCredentials(request);
8183

8284
const chainId = await getChainIdFromChain(chain);
8385
const contract = await getContractV5({
@@ -129,6 +131,7 @@ export async function writeToContract(fastify: FastifyInstance) {
129131
txOverrides,
130132
idempotencyKey,
131133
shouldSimulate: simulateTx,
134+
credentials,
132135
});
133136

134137
reply.status(StatusCodes.OK).send({

src/server/routes/deploy/prebuilt.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { txOverridesWithValueSchema } from "../../schemas/tx-overrides";
1414
import { walletWithAAHeaderSchema } from "../../schemas/wallet";
1515
import { getChainIdFromChain } from "../../utils/chain";
16+
import { getTransactionCredentials } from "../../../shared/lib/transaction/transaction-credentials";
1617

1718
// INPUTS
1819
const requestSchema = prebuiltDeployParamSchema;
@@ -83,6 +84,7 @@ export async function deployPrebuilt(fastify: FastifyInstance) {
8384
"x-account-address": accountAddress,
8485
"x-idempotency-key": idempotencyKey,
8586
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
87+
const credentials = getTransactionCredentials(request);
8688

8789
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
8890
const tx = await sdk.deployer.deployBuiltInContract.prepare(
@@ -103,6 +105,7 @@ export async function deployPrebuilt(fastify: FastifyInstance) {
103105
extension: "deploy-prebuilt",
104106
idempotencyKey,
105107
txOverrides,
108+
credentials,
106109
});
107110

108111
reply.status(StatusCodes.OK).send({

src/server/routes/deploy/prebuilts/edition-drop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
2020
import { txOverridesWithValueSchema } from "../../../schemas/tx-overrides";
2121
import { walletWithAAHeaderSchema } from "../../../schemas/wallet";
2222
import { getChainIdFromChain } from "../../../utils/chain";
23+
import { getTransactionCredentials } from "../../../../shared/lib/transaction/transaction-credentials";
2324

2425
// INPUTS
2526
const requestSchema = prebuiltDeployContractParamSchema;
@@ -87,6 +88,7 @@ export async function deployPrebuiltEditionDrop(fastify: FastifyInstance) {
8788
"x-account-address": accountAddress,
8889
"x-idempotency-key": idempotencyKey,
8990
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
91+
const credentials = getTransactionCredentials(request);
9092

9193
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
9294
const tx = await sdk.deployer.deployBuiltInContract.prepare(
@@ -109,6 +111,7 @@ export async function deployPrebuiltEditionDrop(fastify: FastifyInstance) {
109111
deployedContractType: "edition-drop",
110112
idempotencyKey,
111113
txOverrides,
114+
credentials,
112115
});
113116

114117
reply.status(StatusCodes.OK).send({

src/server/routes/deploy/prebuilts/edition.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
1919
import { txOverridesWithValueSchema } from "../../../schemas/tx-overrides";
2020
import { walletWithAAHeaderSchema } from "../../../schemas/wallet";
2121
import { getChainIdFromChain } from "../../../utils/chain";
22+
import { getTransactionCredentials } from "../../../../shared/lib/transaction/transaction-credentials";
2223

2324
// INPUTS
2425
const requestSchema = prebuiltDeployContractParamSchema;
@@ -87,6 +88,7 @@ export async function deployPrebuiltEdition(fastify: FastifyInstance) {
8788
"x-account-address": accountAddress,
8889
"x-idempotency-key": idempotencyKey,
8990
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
91+
const credentials = getTransactionCredentials(request);
9092

9193
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
9294
const tx = await sdk.deployer.deployBuiltInContract.prepare(
@@ -109,6 +111,7 @@ export async function deployPrebuiltEdition(fastify: FastifyInstance) {
109111
deployedContractType: "edition",
110112
idempotencyKey,
111113
txOverrides,
114+
credentials,
112115
});
113116

114117
reply.status(StatusCodes.OK).send({

src/server/routes/deploy/prebuilts/marketplace-v3.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
1616
import { txOverridesWithValueSchema } from "../../../schemas/tx-overrides";
1717
import { walletWithAAHeaderSchema } from "../../../schemas/wallet";
1818
import { getChainIdFromChain } from "../../../utils/chain";
19+
import { getTransactionCredentials } from "../../../../shared/lib/transaction/transaction-credentials";
1920

2021
// INPUTS
2122
const requestSchema = prebuiltDeployContractParamSchema;
@@ -78,6 +79,7 @@ export async function deployPrebuiltMarketplaceV3(fastify: FastifyInstance) {
7879
"x-account-address": accountAddress,
7980
"x-idempotency-key": idempotencyKey,
8081
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
82+
const credentials = getTransactionCredentials(request);
8183

8284
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
8385
const tx = await sdk.deployer.deployBuiltInContract.prepare(
@@ -100,6 +102,7 @@ export async function deployPrebuiltMarketplaceV3(fastify: FastifyInstance) {
100102
deployedContractType: "marketplace-v3",
101103
idempotencyKey,
102104
txOverrides,
105+
credentials,
103106
});
104107

105108
reply.status(StatusCodes.OK).send({

src/server/routes/deploy/prebuilts/multiwrap.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
1717
import { txOverridesWithValueSchema } from "../../../schemas/tx-overrides";
1818
import { walletWithAAHeaderSchema } from "../../../schemas/wallet";
1919
import { getChainIdFromChain } from "../../../utils/chain";
20+
import { getTransactionCredentials } from "../../../../shared/lib/transaction/transaction-credentials";
2021

2122
// INPUTS
2223
const requestSchema = prebuiltDeployContractParamSchema;
@@ -81,6 +82,7 @@ export async function deployPrebuiltMultiwrap(fastify: FastifyInstance) {
8182
"x-account-address": accountAddress,
8283
"x-idempotency-key": idempotencyKey,
8384
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
85+
const credentials = getTransactionCredentials(request);
8486

8587
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
8688
const tx = await sdk.deployer.deployBuiltInContract.prepare(
@@ -103,6 +105,7 @@ export async function deployPrebuiltMultiwrap(fastify: FastifyInstance) {
103105
deployedContractType: "multiwrap",
104106
idempotencyKey,
105107
txOverrides,
108+
credentials,
106109
});
107110

108111
reply.status(StatusCodes.OK).send({

src/server/routes/deploy/prebuilts/nft-collection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
1919
import { txOverridesWithValueSchema } from "../../../schemas/tx-overrides";
2020
import { walletWithAAHeaderSchema } from "../../../schemas/wallet";
2121
import { getChainIdFromChain } from "../../../utils/chain";
22+
import { getTransactionCredentials } from "../../../../shared/lib/transaction/transaction-credentials";
2223

2324
// INPUTS
2425
const requestSchema = prebuiltDeployContractParamSchema;
@@ -84,6 +85,7 @@ export async function deployPrebuiltNFTCollection(fastify: FastifyInstance) {
8485
"x-account-address": accountAddress,
8586
"x-idempotency-key": idempotencyKey,
8687
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
88+
const credentials = getTransactionCredentials(request);
8789

8890
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
8991
const tx = await sdk.deployer.deployBuiltInContract.prepare(
@@ -106,6 +108,7 @@ export async function deployPrebuiltNFTCollection(fastify: FastifyInstance) {
106108
deployedContractType: "nft-collection",
107109
idempotencyKey,
108110
txOverrides,
111+
credentials,
109112
});
110113

111114
reply.status(StatusCodes.OK).send({

src/server/routes/deploy/prebuilts/nft-drop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
2020
import { txOverridesWithValueSchema } from "../../../schemas/tx-overrides";
2121
import { walletWithAAHeaderSchema } from "../../../schemas/wallet";
2222
import { getChainIdFromChain } from "../../../utils/chain";
23+
import { getTransactionCredentials } from "../../../../shared/lib/transaction/transaction-credentials";
2324

2425
// INPUTS
2526
const requestSchema = prebuiltDeployContractParamSchema;
@@ -87,6 +88,7 @@ export async function deployPrebuiltNFTDrop(fastify: FastifyInstance) {
8788
"x-account-address": accountAddress,
8889
"x-idempotency-key": idempotencyKey,
8990
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
91+
const credentials = getTransactionCredentials(request);
9092

9193
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
9294
const tx = await sdk.deployer.deployBuiltInContract.prepare(
@@ -109,6 +111,7 @@ export async function deployPrebuiltNFTDrop(fastify: FastifyInstance) {
109111
deployedContractType: "nft-drop",
110112
idempotencyKey,
111113
txOverrides,
114+
credentials,
112115
});
113116

114117
reply.status(StatusCodes.OK).send({

0 commit comments

Comments
 (0)