Skip to content

Commit 90d6f19

Browse files
committed
Fix validateRequest
1 parent 4a70f8a commit 90d6f19

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

bun.lockb

19.4 KB
Binary file not shown.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"dotenv": "^16.4.7",
2525
"eslint": "^9.16.0",
2626
"jest": "^29.7.0",
27-
"prettier": "^3.4.1",
27+
"next": "^15.0.3",
28+
"prettier": "^3.4.2",
2829
"ts-jest": "^29.2.5",
2930
"typescript": "^5.7.2",
3031
"viem": "^2.21.53"

packages/agent-sdk/src/evm/index.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export interface BaseResponse {
4040
json(data: unknown, init?: { status?: number }): unknown;
4141
}
4242

43-
export function createResponse(
44-
responseData: unknown,
43+
export function fallbackResponder(
44+
responseData: object,
4545
init?: { status?: number },
4646
): BaseResponse {
4747
return {
@@ -60,25 +60,23 @@ export async function validateRequest<
6060
req: TRequest,
6161
// TODO(bh2smith): Use Bitte Wallet's safeSaltNonce as Default.
6262
safeSaltNonce: string,
63+
responder?: (data: object, init?: { status?: number }) => TResponse,
6364
): Promise<TResponse | null> {
65+
const createResponse = responder ? responder : fallbackResponder;
6466
const metadataHeader = req.headers.get("mb-metadata");
6567
const metadata = JSON.parse(metadataHeader ?? "{}");
6668
const { accountId, evmAddress } = metadata;
6769
if (!accountId || !evmAddress) {
68-
return createResponse(
69-
{ error: "Missing accountId or evmAddress in metadata" },
70-
{ status: 400 },
71-
) as TResponse;
70+
const error = "Missing accountId or evmAddress in metadata";
71+
console.error(error);
72+
return createResponse({ error }, { status: 400 }) as TResponse;
7273
}
7374

7475
const derivedSafeAddress = await getAdapterAddress(accountId, safeSaltNonce);
7576
if (derivedSafeAddress !== getAddress(evmAddress)) {
76-
return createResponse(
77-
{
78-
error: `Invalid safeAddress in metadata: ${derivedSafeAddress} !== ${evmAddress}`,
79-
},
80-
{ status: 401 },
81-
) as TResponse;
77+
const error = `Invalid safeAddress in metadata: ${derivedSafeAddress} !== ${evmAddress}`;
78+
console.error(error);
79+
return createResponse({ error }, { status: 401 }) as TResponse;
8280
}
8381
return null;
8482
}

packages/agent-sdk/tests/evm/index.spec.ts

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { signRequestFor, createResponse, validateRequest } from "../../src/evm";
1+
import {
2+
signRequestFor,
3+
fallbackResponder,
4+
validateRequest,
5+
} from "../../src/evm";
26
import { zeroAddress } from "viem";
7+
import { NextRequest, NextResponse } from "next/server";
38
import type { BaseRequest } from "../../src/evm";
49

510
// Mock external dependencies
@@ -64,11 +69,11 @@ describe("evm/index", () => {
6469
});
6570
});
6671

67-
describe("createResponse", () => {
72+
describe("fallbackResponder", () => {
6873
it("creates a response with default status", () => {
6974
const responseData = { message: "Success" };
7075

71-
const response = createResponse(responseData);
76+
const response = fallbackResponder(responseData);
7277

7378
expect(response.json({}, {})).toEqual({
7479
data: responseData,
@@ -78,7 +83,7 @@ describe("evm/index", () => {
7883
it("creates a response with specified status", () => {
7984
const responseData = { message: "Error" };
8085

81-
const response = createResponse(responseData, { status: 400 });
86+
const response = fallbackResponder(responseData, { status: 400 });
8287
expect(response.json({}, {})).toEqual({
8388
data: responseData,
8489
status: 400,
@@ -161,4 +166,48 @@ describe("evm/index", () => {
161166
});
162167
});
163168
});
169+
170+
describe("validateNextRequest", () => {
171+
it("should validate a real request", async () => {
172+
const request = new NextRequest(
173+
new Request("https://example.com", {
174+
method: "POST",
175+
headers: new Headers({
176+
"mb-metadata": JSON.stringify({
177+
accountId: "max-normal.near",
178+
evmAddress: zeroAddress,
179+
}),
180+
}),
181+
body: JSON.stringify({ test: "data" }),
182+
}),
183+
);
184+
185+
const result = await validateNextRequest(request, "0");
186+
// Get the response data
187+
const responseData = await result?.json();
188+
189+
// Assert the status and response data separately
190+
expect(result?.status).toBe(401);
191+
expect(responseData).toEqual({
192+
error: `Invalid safeAddress in metadata: 0x123 !== ${zeroAddress}`,
193+
});
194+
});
195+
});
164196
});
197+
198+
// TODO: Use in Next Agents.
199+
export async function validateNextRequest(
200+
req: NextRequest,
201+
safeSaltNonce?: string,
202+
): Promise<NextResponse | null> {
203+
const result = await validateRequest<NextRequest, NextResponse>(
204+
req,
205+
safeSaltNonce || "0",
206+
(data: unknown, init?: { status?: number }) =>
207+
NextResponse.json(data, init),
208+
);
209+
210+
console.log("validateNextRequest result:", result); // Add this line for debugging
211+
212+
return result;
213+
}

0 commit comments

Comments
 (0)