import { createWalletClient, custom, publicActions } from "viem";
import { optimism } from "viem/chains";
import { ID_REGISTRY_ABI, ID_REGISTRY_ADDRESS } from "./constants";
const wallet = createWalletClient({
chain: optimism,
transport: custom(window.ethereum),
}).extend(publicActions);
const nonce = await wallet.readContract({
address: ID_REGISTRY_ADDRESS,
abi: ID_REGISTRY_ABI,
functionName: "nonces",
args: [userAddress],
});
const now = Math.floor(Date.now() / 1000);
const deadline = now + 3600; // 1 hour from now
const domain = {
name: "Farcaster IdRegistry",
version: "1",
chainId: 10,
verifyingContract: ID_REGISTRY_ADDRESS,
};
const types = {
Transfer: [
{ name: "fid", type: "uint256" },
{ name: "to", type: "address" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
],
};
const message = {
fid: BigInt(fid),
to: userAddress,
nonce: BigInt(nonce),
deadline: BigInt(deadline),
};
const signature = await wallet.signTypedData({
account: userAddress,
domain,
types,
primaryType: "Transfer",
message,
});
console.log("Signature:", signature);