Skip to content
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
16 changes: 14 additions & 2 deletions routes/auth/mintAndFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export async function mintNextAndAddAuthMethodsHandler(
console.info("PKP address extracted", { pkpEthAddress });

// Send gas funding transaction using optimistic nonce management
// This returns immediately after transaction submission
const gasFundingTxn = await executeTransactionWithRetry(
signer,
async (nonce: number) => {
Expand All @@ -71,7 +70,20 @@ export async function mintNextAndAddAuthMethodsHandler(
fundingAmount: gasToFund.toString(),
});

// Return immediately - confirmations happen in background
// Wait for gas funding transaction to complete before returning
console.info("Waiting for gas funding transaction to be mined...");
const gasFundingReceipt = await signer.provider.waitForTransaction(gasFundingTxn.hash!);

if (gasFundingReceipt.status !== 1) {
throw new Error("Gas funding transaction failed");
}

console.info("Gas funding transaction completed successfully", {
gasFundingTxHash: gasFundingTxn.hash,
blockNumber: gasFundingReceipt.blockNumber,
});

// Return after both PKP mint and gas funding are complete
return res.status(200).json({
requestId: mintTxHash,
});
Expand Down
86 changes: 86 additions & 0 deletions tests/routes/auth/mintAndFetchWaitForCompletion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import request from "supertest";
import express from "express";
import { mintNextAndAddAuthMethodsHandler } from "../../../routes/auth/mintAndFetch";
import cors from "cors";

describe("mintNextAndAddAuthMethodsHandler - Wait for Completion", () => {
let app: express.Application;

beforeEach(() => {
app = express();
app.use(express.json());
app.use(cors());
app.post("/mint-next-and-add-auth-methods", mintNextAndAddAuthMethodsHandler);
});

it("should wait for both PKP mint and gas funding to complete before returning", async () => {
// Create a test request body
const requestBody = {
keyType: "2",
permittedAuthMethodTypes: ["1"],
permittedAuthMethodIds: ["0x1234567890123456789012345678901234567890"],
permittedAuthMethodPubkeys: ["0x"],
permittedAuthMethodScopes: [["1"]],
addPkpEthAddressAsPermittedAddress: true,
sendPkpToItself: true,
};

// Record start time
const startTime = Date.now();

const response = await request(app)
.post("/mint-next-and-add-auth-methods")
.set("api-key", "test-key")
.send(requestBody)
.expect("Content-Type", /json/);

// Record end time
const endTime = Date.now();
const duration = endTime - startTime;

// The response should only come after both transactions are complete
// This should take longer than just the PKP mint alone (which was the previous behavior)
// A successful test would show the response contains a requestId and took a reasonable amount of time

console.log(`Mint request took ${duration}ms to complete`);

if (response.status === 200) {
expect(response.body).toHaveProperty("requestId");
expect(response.body.requestId).toMatch(/^0x[a-fA-F0-9]{64}$/);

// The duration should be long enough to include both transactions
// In practice, this should be at least a few seconds on a real network
console.log("✅ Mint and funding completed successfully");
console.log(`Transaction hash: ${response.body.requestId}`);
} else {
// If it fails, that's also expected in test environment
console.log("Expected failure in test environment:", response.body.error);
expect(response.status).toBe(500);
}
}, 60000); // 60 second timeout since we're potentially waiting for real transactions

it("should handle errors gracefully and still return after attempting both operations", async () => {
// Test with invalid parameters to trigger an error
const requestBody = {
keyType: "invalid",
permittedAuthMethodTypes: [],
permittedAuthMethodIds: [],
permittedAuthMethodPubkeys: [],
permittedAuthMethodScopes: [],
addPkpEthAddressAsPermittedAddress: false,
sendPkpToItself: false,
};

const response = await request(app)
.post("/mint-next-and-add-auth-methods")
.set("api-key", "test-key")
.send(requestBody)
.expect("Content-Type", /json/)
.expect(500);

expect(response.body).toHaveProperty("error");
expect(response.body.error).toContain("Unable to mint PKP");

console.log("✅ Error handling works correctly");
});
});