Skip to content

Commit

Permalink
added remove Whitelist API
Browse files Browse the repository at this point in the history
  • Loading branch information
vignesha22 committed Feb 26, 2024
1 parent 75bc1a0 commit 8a574cc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
23 changes: 23 additions & 0 deletions backend/src/paymaster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ export class Paymaster {
}
}

async removeWhitelistAddress(address: string[], paymasterAddress: string, bundlerRpc: string, relayerKey: string) {
try {
const provider = new providers.JsonRpcProvider(bundlerRpc);
const paymasterContract = new ethers.Contract(paymasterAddress, abi, provider);
const signer = new Wallet(relayerKey, provider)
for (let i = 0; i < address.length; i++) {
const isAdded = await paymasterContract.check(signer.address, address[i]);
if (!isAdded) {
throw new Error(`${address[i]} is not whitelisted`)
}
}
const encodedData = paymasterContract.interface.encodeFunctionData('removeBatchToWhitelist', [address]);
const tx = await signer.sendTransaction({ to: paymasterAddress, data: encodedData });
await tx.wait();
return {
message: `Successfully removed whitelisted addresses with transaction Hash ${tx.hash}`
};
} catch (err: any) {
if (err.message.includes('is not whitelisted')) throw new Error(err);
throw new Error('Error while submitting transaction');
}
}

async checkWhitelistAddress(accountAddress: string, paymasterAddress: string, bundlerRpc: string, relayerKey: string) {
try {
const provider = new providers.JsonRpcProvider(bundlerRpc);
Expand Down
56 changes: 56 additions & 0 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,62 @@ const routes: FastifyPluginAsync = async (server) => {
}
)

server.post("/removeWhitelist", async function (request, reply) {
try {
const body: any = request.body;
const query: any = request.query;
const address = body.params[0];
const chainId = query['chainId'] ?? body.params[1];
const api_key = query['apiKey'] ?? body.params[2];
if (!api_key)
return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY })
let privateKey = '';
let supportedNetworks;
if (!unsafeMode) {
const AWSresponse = await client.send(
new GetSecretValueCommand({
SecretId: prefixSecretId + api_key,
})
);
const secrets = JSON.parse(AWSresponse.SecretString ?? '{}');
if (!secrets['PRIVATE_KEY']) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY })
privateKey = secrets['PRIVATE_KEY'];
supportedNetworks = secrets['SUPPORTED_NETWORKS'];
} else {
const record: any = await getSQLdata(api_key);
console.log(record);
if (!record) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY })
privateKey = decode(record['PRIVATE_KEY']);
supportedNetworks = record['SUPPORTED_NETWORKS'];
}
if (!privateKey) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY })
if (
!Array.isArray(address) ||
address.length > 10 ||
!chainId ||
isNaN(chainId)
) {
return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_DATA });
}
if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) {
return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.UNSUPPORTED_NETWORK });
}
const networkConfig = getNetworkConfig(chainId, supportedNetworks ?? '');
if (!networkConfig) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.UNSUPPORTED_NETWORK });
const validAddresses = address.every(ethers.utils.isAddress);
if (!validAddresses) return reply.code(ReturnCode.FAILURE).send({ error: "Invalid Address passed" });
const result = await paymaster.removeWhitelistAddress(address, networkConfig.contracts.etherspotPaymasterAddress, networkConfig.bundler, privateKey);
if (body.jsonrpc)
return reply.code(ReturnCode.SUCCESS).send({ jsonrpc: body.jsonrpc, id: body.id, result, error: null })
return reply.code(ReturnCode.SUCCESS).send(result);
} catch (err: any) {
request.log.error(err);
if (err.name == "ResourceNotFoundException")
return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY });
return reply.code(ReturnCode.FAILURE).send({ error: err.message ?? ErrorMessage.SOMETHING_WENT_WRONG })
}
})

server.post(
"/checkWhitelist",
async function (request, reply) {
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ const Dashboard = ({ logInType }) => {
fetchData(user.address);
}
setIsLoading(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [user, isLoading]);
}, [user]);

useEffect(() => {
getPaymasterBalance(chainId)
Expand Down

0 comments on commit 8a574cc

Please sign in to comment.