Skip to content

authorization list support on send-transaction #837

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

Merged
merged 4 commits into from
Mar 18, 2025
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
7 changes: 7 additions & 0 deletions src/server/middleware/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export const badChainError = (chain: string | number): CustomError =>
"INVALID_CHAIN",
);

export const badBigIntError = (variableName: string): CustomError =>
createCustomError(
`Invalid BigInt: ${variableName}`,
StatusCodes.BAD_REQUEST,
"INVALID_BIGINT",
);

const flipObject = (data: object) =>
Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key]));

Expand Down
9 changes: 8 additions & 1 deletion src/server/routes/backend-wallet/send-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
} from "../../schemas/wallet";
import { getChainIdFromChain } from "../../utils/chain";
import { parseTransactionOverrides } from "../../utils/transaction-overrides";
import {
authorizationListSchema,
toParsedAuthorization,
} from "../../schemas/transaction/authorization";

const requestBodySchema = Type.Object({
toAddress: Type.Optional(AddressSchema),
Expand All @@ -26,6 +30,7 @@ const requestBodySchema = Type.Object({
value: Type.String({
examples: ["10000000"],
}),
authorizationList: authorizationListSchema,
...txOverridesSchema.properties,
});

Expand Down Expand Up @@ -65,7 +70,8 @@ export async function sendTransaction(fastify: FastifyInstance) {
},
handler: async (request, reply) => {
const { chain } = request.params;
const { toAddress, data, value, txOverrides } = request.body;
const { toAddress, data, value, txOverrides, authorizationList } =
request.body;
const { simulateTx } = request.query;
const {
"x-backend-wallet-address": fromAddress,
Expand Down Expand Up @@ -110,6 +116,7 @@ export async function sendTransaction(fastify: FastifyInstance) {
data: data as Hex,
transactionMode: transactionMode,
value: BigInt(value),
authorizationList: authorizationList?.map(toParsedAuthorization),
...parseTransactionOverrides(txOverrides),
},
shouldSimulate: simulateTx,
Expand Down
30 changes: 30 additions & 0 deletions src/server/schemas/transaction/authorization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type Static, Type } from "@sinclair/typebox";
import { AddressSchema } from "../address";
import { requiredAddress } from "../wallet";
import { requiredBigInt } from "../../../shared/utils/primitive-types";

export const authorizationSchema = Type.Object({
address: AddressSchema,
chainId: Type.Integer(),
nonce: Type.String(),
r: Type.String(),
s: Type.String(),
yParity: Type.Number(),
});

export const authorizationListSchema = Type.Optional(
Type.Array(authorizationSchema),
);

export const toParsedAuthorization = (
authorization: Static<typeof authorizationSchema>,
) => {
return {
address: requiredAddress(authorization.address, "[Authorization List]"),
chainId: authorization.chainId,
nonce: requiredBigInt(authorization.nonce, "[Authorization List] -> nonce"),
r: requiredBigInt(authorization.r, "[Authorization List] -> r"),
s: requiredBigInt(authorization.s, "[Authorization List] -> s"),
yParity: authorization.yParity,
};
};
3 changes: 1 addition & 2 deletions src/server/utils/transaction-overrides.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Static } from "@sinclair/typebox";
import { maybeBigInt } from "../../shared/utils/primitive-types";
import type { InsertedTransaction } from "../../shared/utils/transaction/types";
import type {
txOverridesSchema,
txOverridesWithValueSchema,
Expand All @@ -10,7 +9,7 @@ export const parseTransactionOverrides = (
overrides:
| Static<typeof txOverridesSchema>["txOverrides"]
| Static<typeof txOverridesWithValueSchema>["txOverrides"],
): Partial<InsertedTransaction> => {
) => {
if (!overrides) {
return {};
}
Expand Down
9 changes: 9 additions & 0 deletions src/shared/utils/primitive-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { Address } from "thirdweb";
import { checksumAddress } from "thirdweb/utils";
import { badBigIntError } from "../../server/middleware/error";

export const maybeBigInt = (val?: string) => (val ? BigInt(val) : undefined);
export const maybeInt = (val?: string) =>
val ? Number.parseInt(val) : undefined;

export function requiredBigInt(val: string, variableName: string) {
try {
return BigInt(val);
} catch {
throw badBigIntError(variableName);
}
}

// These overloads hint TS at the response type (ex: Address if `val` is Address).
export function normalizeAddress(val: Address): Address;
export function normalizeAddress(val?: Address): Address | undefined;
Expand Down
9 changes: 8 additions & 1 deletion src/shared/utils/transaction/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { Address, Hex, toSerializableTransaction } from "thirdweb";
import {
signAuthorization,
SignedAuthorization,
type Address,
type Hex,
type toSerializableTransaction,
} from "thirdweb";
import type { TransactionType } from "viem";

// TODO: Replace with thirdweb SDK exported type when available.
Expand Down Expand Up @@ -30,6 +36,7 @@ export type InsertedTransaction = {
value?: bigint;

data?: Hex;
authorizationList?: SignedAuthorization[];
functionName?: string;
functionArgs?: unknown[];

Expand Down
Loading