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

Substitute sender address in the calldata when received as a query parameter in /paymentTxParams #5

Merged
merged 5 commits 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
28 changes: 14 additions & 14 deletions src/helpers/formatTxDataResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ import { ethers } from "ethers";
// TODO: Justin, can we separate helpers related to slice unto their own folder?
type Props = {
txData: any;
buyerAddress?: string;
senderAddress?: string;
}

export async function formatTxDataResponse({ txData, buyerAddress }: Props) {
const { requiresBuyerAddress, contractAbi, placeholderBuyerAddress } = txData;
export async function formatTxDataResponse({ txData, senderAddress }: Props) {
const { requiresSenderAddress, contractAbi, placeholderSenderAddress } = txData;

let finalTxData = txData;
if (requiresBuyerAddress && contractAbi && placeholderBuyerAddress && buyerAddress) {
if (requiresSenderAddress && contractAbi && placeholderSenderAddress && senderAddress) {
// substitute all instances of the placeholder buyer address with the given buyer address
finalTxData = {
...txData,
paymentTx: {
...txData.paymentTx,
data: await substituteBuyerAddress({ contractAbi, data: txData.paymentTx.data, placeholderBuyerAddress, buyerAddress })
data: await substituteSenderAddress({ contractAbi, data: txData.paymentTx.data, placeholderSenderAddress, senderAddress })
}
}
}

// omit the requiresBuyerAddress, contractAbi, and placeholderBuyerAddress fields
// omit the requiresSenderAddress, contractAbi, and placeholderSenderAddress fields
// from the response
const txDataReturned = {
...finalTxData,
requiresBuyerAddress: undefined,
requiresSenderAddress: undefined,
contractAbi: undefined,
placeholderBuyerAddress: undefined,
placeholderSenderAddress: undefined,
}

return txDataReturned
}

type SubstituteBuyerAddressProps = {
type SubstituteSenderAddressProps = {
contractAbi: string;
data: string;
placeholderBuyerAddress: string;
buyerAddress: string;
placeholderSenderAddress: string;
senderAddress: string;
}

async function substituteBuyerAddress({ contractAbi, data, placeholderBuyerAddress, buyerAddress }: SubstituteBuyerAddressProps) {
async function substituteSenderAddress({ contractAbi, data, placeholderSenderAddress, senderAddress }: SubstituteSenderAddressProps) {
// Create an interface from the ABI
const iface = new ethers.utils.Interface(contractAbi);

Expand All @@ -57,9 +57,9 @@ async function substituteBuyerAddress({ contractAbi, data, placeholderBuyerAddre
}

const isAddress = ethers.utils.isAddress(param)
const isEqualAddress = isAddress && param.toLowerCase() === placeholderBuyerAddress.toLowerCase();
const isEqualAddress = isAddress && param.toLowerCase() === placeholderSenderAddress.toLowerCase();

return isEqualAddress ? buyerAddress : param;
return isEqualAddress ? senderAddress : param;
});
};

Expand Down
8 changes: 4 additions & 4 deletions src/helpers/formatTxMessageResponse.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

type Props = {
txMessage: any;
buyerAddress?: string;
senderAddress?: string;
}

export function formatTxMessageResponse({ txMessage, buyerAddress }: Props) {
if (!buyerAddress) return txMessage;
export function formatTxMessageResponse({ txMessage, senderAddress }: Props) {
if (!senderAddress) return txMessage;

const updatedTxMessage = {
...txMessage,
message: {
...txMessage.message,
message: {
...txMessage.message.message,
from: buyerAddress,
from: senderAddress,
},
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/services/paymentTxOrMsgService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createPaymentTxOrMsg = async (payload: Payload) => {
dappName: payload.dappName,
payloadType: payload.payloadType,
additionalPayload: payload.additionalPayload,
rpcProxySubmissionParams: payload.rpcProxySubmissionParams,
rpcProxySubmissionParams: JSON.stringify(payload.rpcProxySubmissionParams), // we stringify ths object to store it in the database to preserve field order
};

let txParams: Prisma.JsonValue;
Expand All @@ -47,7 +47,9 @@ export const createPaymentTxOrMsg = async (payload: Payload) => {
paymentTx: payload.paymentTx,
};
} else if (isEip712Payload(payload)) {
txParams = { message: payload.message };
// noop, rpcProxySubmissionParams contains the message needed to be signed and submitted
// TODO (Justin): Make this work for Slice
txParams = {};
} else {
throw new Error('Invalid payload type');
}
Expand Down Expand Up @@ -75,9 +77,10 @@ export const getPaymentTxOrMsg = async (uuid: string) => {
}

// remove the txParams prop and flatten
const { txParams, ...rest } = paymentTxOrMsg;
const { txParams, rpcProxySubmissionParams, ...rest } = paymentTxOrMsg;
return {
...rest,
rpcProxySubmissionParams: typeof rpcProxySubmissionParams === 'string' ? JSON.parse(rpcProxySubmissionParams) : undefined,
...(txParams as Record<string, unknown>),
};
};
Expand Down
Loading