Skip to content

Commit

Permalink
fix: PRO-2522 SponsorshipPolicy Creation and PaymasterData Generation (
Browse files Browse the repository at this point in the history
…#116)

* fix: PRO-2522 SponsorshipPolicy Creation  - Subset check on DefaultNetworkConfig in absence of SupportedNetworks value for APIKey

* fix: PRO-2544 bump up the package version

* feat: PRO-2544 error message for apikey not found in database
  • Loading branch information
kanthgithub authored Jul 25, 2024
1 parent 65eea10 commit 05bc1f7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion admin_frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "admin_frontend",
"version": "1.2.9",
"version": "1.3.0",
"private": true,
"dependencies": {
"@emotion/react": "11.11.3",
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arka",
"version": "1.2.9",
"version": "1.3.0",
"description": "ARKA - (Albanian for Cashier's case) is the first open source Paymaster as a service software",
"type": "module",
"directories": {
Expand Down
1 change: 1 addition & 0 deletions backend/src/constants/ErrorMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default {
INVALID_SPONSORSHIP_POLICY: 'Invalid sponsorship policy data',
INVALID_SPONSORSHIP_POLICY_ID: 'Invalid sponsorship policy id',
INVALID_API_KEY: 'Invalid Api Key',
API_KEY_NOT_CONFIGURED_IN_DATABASE: 'Api Key not configured in database',
UNSUPPORTED_NETWORK: 'Unsupported network',
UNSUPPORTED_NETWORK_TOKEN: 'Unsupported network/token',
EMPTY_BODY: 'Empty Body received',
Expand Down
9 changes: 3 additions & 6 deletions backend/src/routes/paymaster-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ const paymasterRoutes: FastifyPluginAsync = async (server) => {
const apiKeyEntity = await server.apiKeyRepository.findOneByApiKey(api_key);

if (!apiKeyEntity) {
server.log.error("Invalid Api Key provided")
return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY })
server.log.error("APIKey not configured in database")
return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.API_KEY_NOT_CONFIGURED_IN_DATABASE })
}

if (apiKeyEntity.erc20Paymasters) {
Expand Down Expand Up @@ -197,7 +197,7 @@ const paymasterRoutes: FastifyPluginAsync = async (server) => {

// get wallet_address from api_key
const apiKeyData = await server.apiKeyRepository.findOneByApiKey(api_key);
if (!apiKeyData) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY });
if (!apiKeyData) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.API_KEY_NOT_CONFIGURED_IN_DATABASE });

// get sponsorshipPolicy for the user from walletAddress and entrypoint version
const sponsorshipPolicy: SponsorshipPolicy | null = await server.sponsorshipPolicyRepository.findOneByWalletAddressAndSupportedEPVersion(apiKeyData?.walletAddress, getEPVersion(epVersion));
Expand All @@ -215,9 +215,6 @@ const paymasterRoutes: FastifyPluginAsync = async (server) => {
const supportedNetworks: number[] | undefined | null = sponsorshipPolicy.enabledChains;
if (!supportedNetworks || !supportedNetworks.includes(chainId.chainId)) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.UNSUPPORTED_NETWORK });

// is chainId exists in supportedNetworks
if (!supportedNetworks.includes(chainId.chainId)) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.UNSUPPORTED_NETWORK });

if (txnMode) {
const signerAddress = await signer.getAddress();
const IndexerData = await getIndexerData(signerAddress, userOp.sender, date.getMonth(), date.getFullYear(), noOfTxns, indexerEndpoint);
Expand Down
16 changes: 10 additions & 6 deletions backend/src/routes/sponsorship-policy-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ErrorMessage, { generateErrorMessage } from "../constants/ErrorMessage.js
import ReturnCode from "../constants/ReturnCode.js";
import { SponsorshipPolicyDto, getEPVersion } from "../types/sponsorship-policy-dto.js";
import { SponsorshipPolicy } from "../models/sponsorship-policy.js";
import { getChainIdsFromSupportedNetworks } from "../utils/common.js";
import { getChainIdsFromDefaultSupportedNetworks, getChainIdsFromSupportedNetworks } from "../utils/common.js";

interface RouteParams {
id?: string;
Expand Down Expand Up @@ -262,21 +262,25 @@ const sponsorshipPolicyRoutes: FastifyPluginAsync = async (server) => {

// apiKey has supportedNetworks and validate if the enabledChains array in SponsorshipPolicyDto is a subset of supportedNetworks
const supportedNetworks = apiKey.supportedNetworks;
if (!supportedNetworks) {

// get supportedNetworks from defaultConfig
const supportedChains: number[] = supportedNetworks ? getChainIdsFromSupportedNetworks(supportedNetworks as string) : getChainIdsFromDefaultSupportedNetworks();

if (!supportedChains || supportedChains.length === 0) {
return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.UNSUPPORTED_NETWORK });
}
const apiKeySupportedChains: number[] = getChainIdsFromSupportedNetworks(supportedNetworks as string);

const sponsorshipPolicySupportedChains = sponsorshipPolicyDto.enabledChains;

if (sponsorshipPolicySupportedChains && sponsorshipPolicySupportedChains.length > 0) {
if (!sponsorshipPolicyDto.isApplicableToAllNetworks && sponsorshipPolicySupportedChains && sponsorshipPolicySupportedChains.length > 0) {

if (!sponsorshipPolicySupportedChains.every((chainId: number) => sponsorshipPolicySupportedChains.includes(chainId))) {
if (!sponsorshipPolicySupportedChains.every((chainId: number) => supportedChains.includes(chainId))) {

//generate a comma separate string of sponsorshipPolicySupportedChains
const sponsorshipPolicySupportedChainsCSV: string = sponsorshipPolicySupportedChains.join(',');

//generate a comma separated string of apiKeySupportedChains
const apiKeySupportedChainsCSV: string = apiKeySupportedChains.join(',');
const apiKeySupportedChainsCSV: string = supportedChains.join(',');
const errorMessage: string = generateErrorMessage(ErrorMessage.SPONSORSHIP_POLICY_CHAINS_NOT_IN_SUBSET_OF_APIKEY_SUPPORTED_CHAINS, { sponsorshipPolicyChains: sponsorshipPolicySupportedChainsCSV, apiKeyChains: apiKeySupportedChainsCSV });

return reply.code(ReturnCode.FAILURE).send({ error: errorMessage });
Expand Down
4 changes: 4 additions & 0 deletions backend/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function getNetworkConfig(key: any, supportedNetworks: any, entryPoint: s
return SupportedNetworks.find((chain) => chain.chainId == key && chain.entryPoint == entryPoint);
}

export function getChainIdsFromDefaultSupportedNetworks() {
return SupportedNetworks.map((chain) => chain.chainId);
}

export function decodeSupportedNetworks(supportedNetworksForDecode: string) {
const buffer = Buffer.from(supportedNetworksForDecode, "base64");
return JSON.parse(buffer.toString());
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arka_frontend",
"version": "1.2.9",
"version": "1.3.0",
"private": true,
"dependencies": {
"@babel/plugin-proposal-private-property-in-object": "7.21.11",
Expand Down

0 comments on commit 05bc1f7

Please sign in to comment.