Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually substitute the sender address in the message/call data #6

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/helpers/formatTxMessageResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ export function formatTxMessageResponse({ txMessage, senderAddress }: Props) {

const updatedTxMessage = {
...txMessage,
message: {
...txMessage.message,
rpcProxySubmissionParams: {
...txMessage.rpcProxySubmissionParams,
message: {
...txMessage.message.message,
from: senderAddress,
...txMessage.rpcProxySubmissionParams.message,
message: {
...txMessage.rpcProxySubmissionParams.message.message,
from: senderAddress,
}
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/paymentTxParams/[uuid].ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(400).json({ message: 'Invalid sender address' });
}

const paymentTxOrMsg = await getPaymentTxOrMsg(uuid as string);
const paymentTxOrMsg = await getPaymentTxOrMsg(uuid as string, senderAddress as string);
res.status(200).json(paymentTxOrMsg);
} catch (error) {
console.error('Error retrieving payment transaction:', error);
Expand Down
30 changes: 26 additions & 4 deletions src/services/paymentTxOrMsgService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
isEip712Payload,
} from '@/types/paymentTx';
import { Prisma } from '@prisma/client';
import { formatTxMessageResponse } from '@/helpers/formatTxMessageResponse';
import { formatTxDataResponse } from '@/helpers/formatTxDataResponse';

/**
* Helper for formatting the payload for storage in the database
Expand All @@ -27,7 +29,7 @@ export const createPaymentTxOrMsg = async (payload: Payload) => {
dappName: payload.dappName,
payloadType: payload.payloadType,
additionalPayload: payload.additionalPayload,
rpcProxySubmissionParams: JSON.stringify(payload.rpcProxySubmissionParams), // we stringify ths object to store it in the database to preserve field order
rpcProxySubmissionParams: payload.rpcProxySubmissionParams,
};

let txParams: Prisma.JsonValue;
Expand Down Expand Up @@ -65,7 +67,7 @@ export const createPaymentTxOrMsg = async (payload: Payload) => {
/**
* Get a payment transaction or message by uuid. Flattens the txParams object
*/
export const getPaymentTxOrMsg = async (uuid: string) => {
export const getPaymentTxOrMsg = async (uuid: string, senderAddress?: string) => {
const prisma = getPrismaClient();

const paymentTxOrMsg = await prisma.contactlessPaymentTxOrMsg.findUnique({
Expand All @@ -77,10 +79,30 @@ export const getPaymentTxOrMsg = async (uuid: string) => {
}

// remove the txParams prop and flatten
const { txParams, rpcProxySubmissionParams, ...rest } = paymentTxOrMsg;
const { txParams, ...rest } = paymentTxOrMsg;

if (paymentTxOrMsg.payloadType === 'eip712') {
return formatTxMessageResponse({
txMessage: {
...rest,
...(txParams as Record<string, unknown>),
},
senderAddress,
});
}

if (paymentTxOrMsg.payloadType === 'contractCall') {
return formatTxDataResponse({
txData: {
...rest,
...(txParams as Record<string, unknown>)
},
senderAddress,
});
}

return {
...rest,
rpcProxySubmissionParams: typeof rpcProxySubmissionParams === 'string' ? JSON.parse(rpcProxySubmissionParams) : undefined,
...(txParams as Record<string, unknown>),
};
};
Expand Down
Loading