Skip to content

Commit

Permalink
Added KMS (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
vignesha22 authored Sep 20, 2023
1 parent 329fa25 commit 607cb3e
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 132 deletions.
5 changes: 2 additions & 3 deletions backend/demo.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ LOG_LEVEL=debug

API_HOST=127.0.0.1
API_PORT=5050
API_KEY=dev

PAYMASTER_PRIVATE_KEY=0x
STACKUP_API_KEY=
STACKUP_API_KEY=
SUPPORTED_NETWORKS=
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arka",
"version": "1.0.0",
"version": "1.1.1",
"description": "This project was bootstrapped with Fastify-CLI.",
"type": "module",
"directories": {
Expand Down Expand Up @@ -28,6 +28,8 @@
"dependencies": {
"@account-abstraction/contracts": "0.6.0",
"@account-abstraction/utils": "0.5.0",
"@aws-sdk/client-secrets-manager": "3.410.0",
"@fastify/cors": "^8.4.0",
"@sinclair/typebox": "0.23.5",
"ajv": "8.11.2",
"dotenv": "16.0.3",
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 @@ -5,4 +5,5 @@ export default {
UNSUPPORTED_NETWORK_TOKEN: 'Unsupported network/token',
EMPTY_BODY: 'Empty Body received',
SOMETHING_WENT_WRONG: 'Something went wrong',
INVALID_MODE: 'Invalid mode selected'
}
4 changes: 4 additions & 0 deletions backend/src/constants/ReturnCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
SUCCESS: 200,
FAILURE: 400,
}
15 changes: 6 additions & 9 deletions backend/src/paymaster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ interface stackupPaymasterResponse {
}

export class Paymaster {
private relayerKey: string;
private stackupEndpoint: string | null;

constructor(
relayerKey: string,
stackupApiKey: string,
) {
this.relayerKey = relayerKey;
this.stackupEndpoint = stackupApiKey ? `https://api.stackup.sh/v1/paymaster/${stackupApiKey}` : null;
}

Expand Down Expand Up @@ -58,11 +55,11 @@ export class Paymaster {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async sign(userOp: any, validUntil: string, validAfter: string, entryPoint: string, paymasterAddress: string, bundlerRpc: string) {
async sign(userOp: any, validUntil: string, validAfter: string, entryPoint: 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(this.relayerKey, provider)
const signer = new Wallet(relayerKey, provider)
userOp.paymasterAndData = await this.getPaymasterAndData(userOp, validUntil, validAfter, paymasterContract, signer);
const response = await provider.send('eth_estimateUserOperationGas', [userOp, entryPoint]);
userOp.verificationGasLimit = response.verificationGasLimit;
Expand Down Expand Up @@ -140,11 +137,11 @@ export class Paymaster {
}
}

async whitelistAddresses(address: string[], paymasterAddress: string, bundlerRpc: string) {
async whitelistAddresses(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(this.relayerKey, provider)
const signer = new Wallet(relayerKey, provider)
const encodedData = paymasterContract.interface.encodeFunctionData('addBatchToWhitelist', [address]);
const tx = await signer.sendTransaction({ to: paymasterAddress, data: encodedData });
await tx.wait();
Expand All @@ -162,11 +159,11 @@ export class Paymaster {
return paymasterContract.check(sponsorAddress, accountAddress);
}

async deposit(amount: string, paymasterAddress: string, bundlerRpc: string) {
async deposit(amount: 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(this.relayerKey, provider)
const signer = new Wallet(relayerKey, provider)
const balance = await signer.getBalance();
if (ethers.utils.parseEther(amount.toString()).gte(balance))
throw new Error(`${signer.address} Balance is less than the amount to be deposited`)
Expand Down
4 changes: 0 additions & 4 deletions backend/src/plugins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ const ConfigSchema = Type.Strict(
LOG_LEVEL: Type.String(),
API_HOST: Type.String(),
API_PORT: Type.String(),
PAYMASTER_PRIVATE_KEY: Type.String(),
STACKUP_API_KEY: Type.String() || undefined,
API_KEY: Type.String(),
SUPPORTED_NETWORKS: Type.String() || undefined,
})
);
Expand Down Expand Up @@ -47,8 +45,6 @@ const configPlugin: FastifyPluginAsync = async (server) => {
LOG_LEVEL: process.env.LOG_LEVEL ?? '',
API_PORT: process.env.API_PORT ?? '',
API_HOST: process.env.API_HOST ?? '',
API_KEY: process.env.API_KEY ?? '',
PAYMASTER_PRIVATE_KEY: process.env.PAYMASTER_PRIVATE_KEY ?? '',
STACKUP_API_KEY: process.env.STACKUP_API_KEY ?? '',
SUPPORTED_NETWORKS: process.env.SUPPORTED_NETWORKS ?? '',
}
Expand Down
Loading

0 comments on commit 607cb3e

Please sign in to comment.