Skip to content

Commit

Permalink
feat: PRO-2395 sequelize integration to backend and property name upd…
Browse files Browse the repository at this point in the history
…ates for APIKey entity references in backend and frontend components
  • Loading branch information
kanthgithub committed Jun 7, 2024
1 parent df5476a commit ce1a9a2
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 171 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# build
/build/

# misc
.DS_Store
.env.production

# debug
npm-debug.log*

.nyc_output
coverage

.env
config.json
database.sqlite

package-lock.json
pnpm-lock.yaml

# Ponder
/indexer/.ponder
/indexer/generated
yarn.lock
1 change: 1 addition & 0 deletions admin_frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock
26 changes: 13 additions & 13 deletions admin_frontend/src/components/ApiKeys.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ const ApiKeysPage = () => {
JSON.stringify(customErc20Paymaster)
).toString("base64");
const requestData = {
API_KEY: apiKey,
PRIVATE_KEY: privateKey,
SUPPORTED_NETWORKS:
apiKey: apiKey,
privateKey: privateKey,
supportedNetworks:
Buffer.from(JSON.stringify(supportedNetworks)).toString("base64") ??
"",
ERC20_PAYMASTERS: base64Erc20 ?? "",
erc20Paymasters: base64Erc20 ?? "",
};
const data = await fetch(
`${process.env.REACT_APP_SERVER_URL}${ENDPOINTS["saveKey"]}`,
Expand Down Expand Up @@ -280,12 +280,12 @@ const ApiKeysPage = () => {
</TableCell>
</TableRow>
{keys.map((row, index) => (
<TableRow key={row.API_KEY}>
<TableCell>{row.WALLET_ADDRESS}</TableCell>
<TableCell>{row.API_KEY}</TableCell>
<TableRow key={row.apiKey}>
<TableCell>{row.walletAddress}</TableCell>
<TableCell>{row.apiKey}</TableCell>
<TableCell>
<div className="flex flex-row">
<div>{showPassword ? row.PRIVATE_KEY : "*****"} </div>
<div>{showPassword ? row.privateKey : "*****"} </div>
<div>
<InputAdornment position="end">
<IconButton
Expand All @@ -302,16 +302,16 @@ const ApiKeysPage = () => {
</TableCell>
<TableCell>
<Button
disabled={row.SUPPORTED_NETWORKS === ""}
onClick={() => handleViewOpen(row.SUPPORTED_NETWORKS)}
disabled={row.supportedNetworks === ""}
onClick={() => handleViewOpen(row.supportedNetworks)}
>
View
</Button>
</TableCell>
<TableCell>
<Button
disabled={row.ERC20_PAYMASTERS === ""}
onClick={() => handleViewERC20Open(row.ERC20_PAYMASTERS)}
disabled={row.erc20Paymasters === ""}
onClick={() => handleViewERC20Open(row.erc20Paymasters)}
>
View
</Button>
Expand All @@ -324,7 +324,7 @@ const ApiKeysPage = () => {
startIcon={<RemoveCircleIcon />}
variant="contained"
onClick={() => {
handleDelete(row.API_KEY);
handleDelete(row.apiKey);
}}
>
Delete Row
Expand Down
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ pnpm-lock.yaml

# Ponder
/indexer/.ponder
/indexer/generated
/indexer/generated

yarn.lock
86 changes: 86 additions & 0 deletions backend/src/models/APIKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Sequelize, DataTypes, Model } from 'sequelize';

export class APIKey extends Model {
declare apiKey: string;
declare walletAddress: string;
declare privateKey: string;
declare supportedNetworks: string | null;
declare erc20Paymasters: string | null;
declare multiTokenPaymasters: string | null;
declare multiTokenOracles: string | null;
declare sponsorName: string | null;
declare logoUrl: string | null;
declare transactionLimit: number;
declare noOfTransactionsInAMonth: number | null;
declare indexerEndpoint: string | null;
}

export function initializeAPIKeyModel(sequelize: Sequelize) {
APIKey.init({
apiKey: {
type: DataTypes.TEXT,
allowNull: false,
primaryKey: true,
field: 'API_KEY'
},
walletAddress: {
type: DataTypes.TEXT,
allowNull: false,
field: 'WALLET_ADDRESS'
},
privateKey: {
type: DataTypes.STRING,
allowNull: false,
field: 'PRIVATE_KEY'
},
supportedNetworks: {
type: DataTypes.STRING,
allowNull: true,
field: 'SUPPORTED_NETWORKS'
},
erc20Paymasters: {
type: DataTypes.STRING,
allowNull: true,
field: 'ERC20_PAYMASTERS'
},
multiTokenPaymasters: {
type: DataTypes.STRING,
allowNull: true,
field: 'MULTI_TOKEN_PAYMASTERS'
},
multiTokenOracles: {
type: DataTypes.STRING,
allowNull: true,
field: 'MULTI_TOKEN_ORACLES'
},
sponsorName: {
type: DataTypes.STRING,
allowNull: true,
field: 'SPONSOR_NAME'
},
logoUrl: {
type: DataTypes.STRING,
allowNull: true,
field: 'LOGO_URL'
},
transactionLimit: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'TRANSACTION_LIMIT'
},
noOfTransactionsInAMonth: {
type: DataTypes.INTEGER,
allowNull: true,
field: 'NO_OF_TRANSACTIONS_IN_A_MONTH'
},
indexerEndpoint: {
type: DataTypes.STRING,
allowNull: true,
field: 'INDEXER_ENDPOINT'
},
}, {
tableName: 'api_keys',
sequelize, // passing the `sequelize` instance is required
timestamps: false, // this will deactivate the `createdAt` and `updatedAt` columns
});
}
26 changes: 26 additions & 0 deletions backend/src/plugins/sequelizePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fp from "fastify-plugin";
import { FastifyPluginAsync } from "fastify";
import { Sequelize } from 'sequelize';
import sqlite3 from 'sqlite3';

const sequelizePlugin: FastifyPluginAsync = async (server) => {
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: './database.sqlite',
dialectModule: sqlite3
});

server.decorate('sequelize', sequelize);

server.addHook('onClose', (instance, done) => {
instance.sequelize.close().then(() => done(), done);
});
};

declare module "fastify" {
interface FastifyInstance {
sequelize: Sequelize;
}
}

export default fp(sequelizePlugin);
25 changes: 25 additions & 0 deletions backend/src/repository/APIKeyRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Sequelize } from 'sequelize';
import { APIKey } from '../models/APIKey';

export class APIKeyRepository {
private sequelize: Sequelize;

constructor(sequelize: Sequelize) {
this.sequelize = sequelize;
}

async findAll(): Promise<APIKey[]> {
const result = await this.sequelize.models.APIKey.findAll();
return result.map(apiKey => apiKey.get() as APIKey);
}

async findOneByApiKey(apiKey: string): Promise<APIKey | null> {
const result = await this.sequelize.models.APIKey.findOne({ where: { apiKey: apiKey } });
return result ? result.get() as APIKey : null;
}

async findOneByWalletAddress(walletAddress: string): Promise<APIKey | null> {
const result = await this.sequelize.models.APIKey.findOne({ where: { walletAddress: walletAddress } });
return result ? result.get() as APIKey : null;
}
}
Loading

0 comments on commit ce1a9a2

Please sign in to comment.