Skip to content

Commit 74770cc

Browse files
authored
Support EIP-2612 and auth trusted forwarders (#304)
* Add allowedForwarders, forwarder verification, etc. * Add support for EIP-2612 * Add relayer creation * Reject transactions with value > 0
1 parent bde0b2d commit 74770cc

File tree

6 files changed

+272
-73
lines changed

6 files changed

+272
-73
lines changed

src/constants/relayer.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
export const ForwarderAbi = [
2+
{ inputs: [], stateMutability: "nonpayable", type: "constructor" },
3+
{
4+
inputs: [
5+
{
6+
components: [
7+
{ internalType: "address", name: "from", type: "address" },
8+
{ internalType: "address", name: "to", type: "address" },
9+
{ internalType: "uint256", name: "value", type: "uint256" },
10+
{ internalType: "uint256", name: "gas", type: "uint256" },
11+
{ internalType: "uint256", name: "nonce", type: "uint256" },
12+
{ internalType: "bytes", name: "data", type: "bytes" },
13+
],
14+
internalType: "struct MinimalForwarder.ForwardRequest",
15+
name: "req",
16+
type: "tuple",
17+
},
18+
{ internalType: "bytes", name: "signature", type: "bytes" },
19+
],
20+
name: "execute",
21+
outputs: [
22+
{ internalType: "bool", name: "", type: "bool" },
23+
{ internalType: "bytes", name: "", type: "bytes" },
24+
],
25+
stateMutability: "payable",
26+
type: "function",
27+
},
28+
{
29+
inputs: [{ internalType: "address", name: "from", type: "address" }],
30+
name: "getNonce",
31+
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
32+
stateMutability: "view",
33+
type: "function",
34+
},
35+
{
36+
inputs: [
37+
{
38+
components: [
39+
{ internalType: "address", name: "from", type: "address" },
40+
{ internalType: "address", name: "to", type: "address" },
41+
{ internalType: "uint256", name: "value", type: "uint256" },
42+
{ internalType: "uint256", name: "gas", type: "uint256" },
43+
{ internalType: "uint256", name: "nonce", type: "uint256" },
44+
{ internalType: "bytes", name: "data", type: "bytes" },
45+
],
46+
internalType: "struct MinimalForwarder.ForwardRequest",
47+
name: "req",
48+
type: "tuple",
49+
},
50+
{ internalType: "bytes", name: "signature", type: "bytes" },
51+
],
52+
name: "verify",
53+
outputs: [{ internalType: "bool", name: "", type: "bool" }],
54+
stateMutability: "view",
55+
type: "function",
56+
},
57+
];
58+
59+
export const ERC2771ContextAbi = [
60+
{
61+
inputs: [
62+
{
63+
internalType: "address[]",
64+
name: "trustedForwarder",
65+
type: "address[]",
66+
},
67+
],
68+
stateMutability: "nonpayable",
69+
type: "constructor",
70+
},
71+
{
72+
inputs: [
73+
{
74+
internalType: "address",
75+
name: "forwarder",
76+
type: "address",
77+
},
78+
],
79+
name: "isTrustedForwarder",
80+
outputs: [
81+
{
82+
internalType: "bool",
83+
name: "",
84+
type: "bool",
85+
},
86+
],
87+
stateMutability: "view",
88+
type: "function",
89+
},
90+
];
91+
92+
export const ERC20PermitAbi = [
93+
{
94+
inputs: [
95+
{
96+
internalType: "address",
97+
name: "owner",
98+
type: "address",
99+
},
100+
{
101+
internalType: "address",
102+
name: "spender",
103+
type: "address",
104+
},
105+
{
106+
internalType: "uint256",
107+
name: "value",
108+
type: "uint256",
109+
},
110+
{
111+
internalType: "uint256",
112+
name: "deadline",
113+
type: "uint256",
114+
},
115+
{
116+
internalType: "uint8",
117+
name: "v",
118+
type: "uint8",
119+
},
120+
{
121+
internalType: "bytes32",
122+
name: "r",
123+
type: "bytes32",
124+
},
125+
{
126+
internalType: "bytes32",
127+
name: "s",
128+
type: "bytes32",
129+
},
130+
],
131+
name: "permit",
132+
outputs: [],
133+
stateMutability: "nonpayable",
134+
type: "function",
135+
},
136+
];
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "relayers" ADD COLUMN "allowedForwarders" TEXT;

src/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ model Relayers {
168168
chainId String @map("chainId")
169169
backendWalletAddress String @map("backendWalletAddress")
170170
allowedContracts String? @map("allowedContracts")
171+
allowedForwarders String? @map("allowedForwarders")
171172
172173
@@map("relayers")
173174
}

src/server/routes/relayer/create.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const BodySchema = Type.Object({
1111
"The address of the backend wallet to use for relaying transactions.",
1212
}),
1313
allowedContracts: Type.Optional(Type.Array(Type.String())),
14+
allowedForwarders: Type.Optional(Type.Array(Type.String())),
1415
});
1516

1617
const ReplySchema = Type.Object({
@@ -37,14 +38,26 @@ export async function createRelayer(fastify: FastifyInstance) {
3738
},
3839
},
3940
handler: async (req, res) => {
40-
const { chain, backendWalletAddress, allowedContracts } = req.body;
41+
const {
42+
chain,
43+
backendWalletAddress,
44+
allowedContracts,
45+
allowedForwarders,
46+
} = req.body;
4147

4248
const relayer = await prisma.relayers.create({
4349
data: {
4450
chainId: getChainIdFromChain(chain).toString(),
4551
backendWalletAddress,
4652
allowedContracts: allowedContracts
47-
? JSON.stringify(allowedContracts)
53+
? JSON.stringify(
54+
allowedContracts.map((address) => address.toLowerCase()),
55+
)
56+
: null,
57+
allowedForwarders: allowedForwarders
58+
? JSON.stringify(
59+
allowedForwarders.map((address) => address.toLowerCase()),
60+
)
4861
: null,
4962
},
5063
});

src/server/routes/relayer/getAll.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const ReplySchema = Type.Object({
1111
chainId: Type.String(),
1212
backendWalletAddress: Type.String(),
1313
allowedContracts: Type.Union([Type.Array(Type.String()), Type.Null()]),
14+
allowedForwarders: Type.Union([Type.Array(Type.String()), Type.Null()]),
1415
}),
1516
),
1617
});
@@ -37,6 +38,9 @@ export async function getAllRelayers(fastify: FastifyInstance) {
3738
allowedContracts: relayer.allowedContracts
3839
? JSON.parse(relayer.allowedContracts)
3940
: null,
41+
allowedForwarders: relayer.allowedForwarders
42+
? JSON.parse(relayer.allowedForwarders)
43+
: null,
4044
})),
4145
});
4246
},

0 commit comments

Comments
 (0)