Skip to content
Merged
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
42 changes: 26 additions & 16 deletions src/server/routes/backend-wallet/sign-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import {
maybeBigInt,
maybeInt,
} from "../../../shared/utils/primitive-types";
import { toTransactionType } from "../../../shared/utils/sdk";
import { thirdwebClient } from "../../../shared/utils/sdk";
import { createCustomError } from "../../middleware/error";
import { standardResponseSchema } from "../../schemas/shared-api-schemas";
import { walletHeaderSchema } from "../../schemas/wallet";
import type { Hex } from "thirdweb";
import {
prepareTransaction,
toSerializableTransaction,
type Hex,
} from "thirdweb";
import { getChain } from "../../../shared/utils/chain";

const requestBodySchema = Type.Object({
transaction: Type.Object({
Expand All @@ -21,7 +26,7 @@ const requestBodySchema = Type.Object({
gasPrice: Type.Optional(Type.String()),
data: Type.Optional(Type.String()),
value: Type.Optional(Type.String()),
chainId: Type.Optional(Type.Integer()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this gonna break existing users calling without a chain id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, open to any way to support pre-eip155 signatures as well. I couldn't find a way with the current SDK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh signing a tx without a chain id is kinda sus. And I guess it will only break ppl when they upgrade?

Maybe we make it really clear in the release notes

chainId: Type.Integer(),
type: Type.Optional(Type.Integer()),
accessList: Type.Optional(Type.Any()),
maxFeePerGas: Type.Optional(Type.String()),
Expand Down Expand Up @@ -54,14 +59,17 @@ export async function signTransaction(fastify: FastifyInstance) {
},
},
handler: async (request, reply) => {
const { transaction } = request.body;
const { "x-backend-wallet-address": walletAddress } =
request.headers as Static<typeof walletHeaderSchema>;

const { chainId, nonce, ...transaction } = request.body.transaction;
const chain = await getChain(chainId);

const account = await getAccount({
chainId: 1,
chainId,
from: getChecksumAddress(walletAddress),
});

if (!account.signTransaction) {
throw createCustomError(
'This backend wallet does not support "signTransaction".',
Expand All @@ -70,23 +78,25 @@ export async function signTransaction(fastify: FastifyInstance) {
);
}

const serializableTransaction = {
chainId: transaction.chainId,
to: getChecksumAddress(transaction.to),
nonce: maybeInt(transaction.nonce),
gas: maybeBigInt(transaction.gasLimit),
gasPrice: maybeBigInt(transaction.gasPrice),
// const prepareTransactionOptions: StaticPrepareTransactionOptions
const prepareTransactionOptions = {
...transaction,
data: transaction.data as Hex | undefined,
client: thirdwebClient,
nonce: maybeInt(nonce),
chain,
value: maybeBigInt(transaction.value),
type: transaction.type
? toTransactionType(transaction.type)
: undefined,
accessList: transaction.accessList,
gas: maybeBigInt(transaction.gasLimit),
gasPrice: maybeBigInt(transaction.gasPrice),
maxFeePerGas: maybeBigInt(transaction.maxFeePerGas),
maxPriorityFeePerGas: maybeBigInt(transaction.maxPriorityFeePerGas),
ccipReadEnabled: transaction.ccipReadEnabled,
};

const preparedTransaction = prepareTransaction(prepareTransactionOptions);
const serializableTransaction = await toSerializableTransaction({
transaction: preparedTransaction,
});

const signature = await account.signTransaction(serializableTransaction);

reply.status(StatusCodes.OK).send({
Expand Down
Loading