Skip to content

Commit 8308320

Browse files
committed
add factory address validation, chainId to cache key
1 parent 6b930e2 commit 8308320

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

src/server/routes/backend-wallet/sendTransaction.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4+
import { getAddress } from "thirdweb";
45
import { queueTxRaw } from "../../../db/transactions/queueTxRaw";
56
import { redis } from "../../../utils/redis/redis";
67
import {
@@ -78,14 +79,19 @@ export async function sendTransaction(fastify: FastifyInstance) {
7879
const chainId = await getChainIdFromChain(chain);
7980

8081
if (accountAddress && factoryAddress) {
81-
// Note: This is a temporary solution to cache the deployed address's factory for 7 days.
82-
// This is needed due to a potential race condition of submitting a transaction immediately after creating an account that is not yet mined onchain
83-
await redis.set(
84-
`account-factory:${accountAddress.toLowerCase()}`,
85-
factoryAddress,
86-
"EX",
87-
7 * 24 * 60 * 60,
88-
);
82+
try {
83+
const validatedFactoryAddress = getAddress(factoryAddress);
84+
// Note: This is a temporary solution to cache the deployed address's factory for 7 days.
85+
// This is needed due to a potential race condition of submitting a transaction immediately after creating an account that is not yet mined onchain
86+
await redis.set(
87+
`account-factory:${chainId}:${validatedFactoryAddress.toLowerCase()}`,
88+
factoryAddress,
89+
"EX",
90+
7 * 24 * 60 * 60,
91+
);
92+
} catch {
93+
// incorrect factory address provided, ignore for backwards compatibility
94+
}
8995
}
9096

9197
let queueId: string;

src/server/routes/contract/extensions/accountFactory/write/createAccount.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ export const createAccount = async (fastify: FastifyInstance) => {
9595

9696
// Note: This is a temporary solution to cache the deployed address's factory for 7 days.
9797
// This is needed due to a potential race condition of submitting a transaction immediately after creating an account that is not yet mined onchain
98-
await redis.set(`account-factory:${deployedAddress.toLowerCase()}`, contractAddress, 'EX', 7 * 24 * 60 * 60);
98+
await redis.set(
99+
`account-factory:${chainId}:${deployedAddress.toLowerCase()}`,
100+
contractAddress,
101+
"EX",
102+
7 * 24 * 60 * 60,
103+
);
99104

100105
reply.status(StatusCodes.OK).send({
101106
result: {

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4+
import { getAddress } from "thirdweb";
45
import { queueTx } from "../../../../db/transactions/queueTx";
56
import { getContract } from "../../../../utils/cache/getContract";
67
import { redis } from "../../../../utils/redis/redis";
@@ -70,18 +71,24 @@ export async function writeToContract(fastify: FastifyInstance) {
7071
"x-account-factory-address": factoryAddress,
7172
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7273

74+
const chainId = await getChainIdFromChain(chain);
75+
7376
if (accountAddress && factoryAddress) {
74-
// Note: This is a temporary solution to cache the deployed address's factory for 7 days.
75-
// This is needed due to a potential race condition of submitting a transaction immediately after creating an account that is not yet mined onchain
76-
await redis.set(
77-
`account-factory:${accountAddress.toLowerCase()}`,
78-
factoryAddress,
79-
"EX",
80-
7 * 24 * 60 * 60,
81-
);
77+
try {
78+
const validatedFactoryAddress = getAddress(factoryAddress);
79+
// Note: This is a temporary solution to cache the deployed address's factory for 7 days.
80+
// This is needed due to a potential race condition of submitting a transaction immediately after creating an account that is not yet mined onchain
81+
await redis.set(
82+
`account-factory:${chainId}:${validatedFactoryAddress.toLowerCase()}`,
83+
factoryAddress,
84+
"EX",
85+
7 * 24 * 60 * 60,
86+
);
87+
} catch {
88+
// incorrect factory address provided, ignore for backwards compatibility
89+
}
8290
}
8391

84-
const chainId = await getChainIdFromChain(chain);
8592
const contract = await getContract({
8693
chainId,
8794
contractAddress,

src/server/utils/wallets/getSmartWallet.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export const getSmartWallet = async ({
2020
// Note: This is a temporary solution to use cached deployed address's factory address from create-account
2121
// This is needed due to a potential race condition of submitting a transaction immediately after creating an account that is not yet mined onchain
2222
factoryAddress =
23-
(await redis.get(`account-factory:${accountAddress.toLowerCase()}`)) ||
24-
"";
23+
(await redis.get(
24+
`account-factory:${chainId}:${accountAddress.toLowerCase()}`,
25+
)) || "";
2526
} catch {}
2627

2728
if (!factoryAddress) {

0 commit comments

Comments
 (0)