Skip to content

Commit

Permalink
Add validate fund txn request body
Browse files Browse the repository at this point in the history
  • Loading branch information
abhidtu2014 committed Mar 20, 2024
1 parent 3fbb9ef commit 4bc8706
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "rm -rf dist && swc ./src -d ./dist --ignore \"**/*.test.ts,**/__test__/**\"",
"lint": "eslint \"src/**/*.{json,js,jsx,ts,tsx}\" && tsc --noemit",
"build": "tsc --noemit && rm -rf dist && swc ./src -d ./dist --ignore \"**/*.test.ts,**/__test__/**\"",
"lint": "eslint \"src/**/*.{json,js,jsx,ts,tsx}\"",
"prettier": "prettier \"src/**/*.{json,js,jsx,ts,tsx}\" --write",
"test": "jest --clearCache --coverage"
},
Expand Down
2 changes: 1 addition & 1 deletion backend/src/handlers/discord-signed-digest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const signDiscordMessage = async (
console.error('Error generating signed discord digest', err)
return {
statusCode: 500,
body: JSON.stringify({ error: 'Error generating signed discord digest' }),
body: JSON.stringify({ error: 'Internal server error' }),
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions backend/src/handlers/fund-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const fundTransaction = async (
): Promise<APIGatewayProxyResult> => {
try {
const requestBody = JSON.parse(event.body!) as FundTransactionRequest

validateFundTransactions(requestBody.transactions)
const transactions = deserializeTransactions(requestBody.transactions)
const isTransactionsValid = await checkTransactions(transactions)

Expand All @@ -40,7 +40,23 @@ export const fundTransaction = async (
console.error('Error fully signing transactions', err)
return {
statusCode: 500,
body: JSON.stringify({ error: 'Error fully signing transactions' }),
body: JSON.stringify({ error: 'Internal server error' }),
}
}
}

function validateFundTransactions(transactions: unknown) {
if (!Array.isArray(transactions) || transactions.length === 0) {
return {
statusCode: 400,
body: JSON.stringify({ error: 'Must provide transactions' }),
}
}

if (transactions.length >= 10) {
return {
statusCode: 400,
body: JSON.stringify({ error: 'Too many transactions' }),
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions backend/src/utils/fundTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import { getSecret } from './index'
const SET_COMPUTE_UNIT_LIMIT_DISCRIMINANT = 2

export function deserializeTransactions(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
transactions: any,
transactions: unknown,
): VersionedTransaction[] {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return transactions.map((serializedTx: any) => {
return VersionedTransaction.deserialize(Buffer.from(serializedTx))
})
return (transactions as Uint8Array[]).map((serializedTx) =>
VersionedTransaction.deserialize(Buffer.from(serializedTx)),
)
} catch (err) {
console.error('Failed to deserialize transactions', err)
throw err
Expand Down

0 comments on commit 4bc8706

Please sign in to comment.