import type { Wallet } from "@crossmint/wallets-sdk";
import { EVMWallet } from "@crossmint/wallets-sdk";
import type { Hex } from "viem";
export function createX402Signer(wallet: Wallet<any>) {
const evm = EVMWallet.from(wallet);
console.log("Creating x402 account:", {
walletAddress: wallet.address,
evmAddress: evm.address,
addressType: typeof evm.address
});
// Create viem Account-compatible object
// x402 expects an Account with address, type, and signTypedData method
const account: any = {
address: evm.address as `0x${string}`,
type: "local",
source: "custom",
// signTypedData method required by x402 for payment signatures
signTypedData: async (params: any) => {
console.log("signTypedData called for address:", evm.address);
const { domain, message, primaryType, types } = params;
console.log("Full EIP-712 payload being signed:");
console.log(" Domain:", JSON.stringify(domain, null, 2));
console.log(" Message:", JSON.stringify(message, null, 2));
console.log(" PrimaryType:", primaryType);
console.log(" Types:", JSON.stringify(types, null, 2));
console.log("Key fields:");
console.log(" - Payer (from):", evm.address);
console.log(" - Recipient (to):", message?.to || message?.recipient || message?.payTo || 'MISSING');
console.log(" - Verifying contract:", domain?.verifyingContract);
// Sign with Crossmint wallet
console.log("Calling Crossmint signTypedData...");
const sig = await evm.signTypedData({
domain,
message,
primaryType,
types,
chain: evm.chain as any
} as any);
console.log("Signature received from Crossmint");
console.log("Signature details:", {
signatureLength: sig.signature.length,
signatureStart: sig.signature.substring(0, 66),
isERC6492: sig.signature.endsWith("6492649264926492649264926492649264926492649264926492649264926492")
});
const processed = processSignature(sig.signature as string);
console.log("Processed signature ready for x402 facilitator");
return processed;
}
};
console.log("x402 account created with address:", account.address);
return account;
}