import { SiweMessage } from 'siwe';
import { signMessage } from 'wagmi/actions';
async function authenticate(address: string, chainId: number) {
// 1. Get nonce
const nonceRes = await fetch('/api/signIn');
const { nonce } = await nonceRes.json();
// 2. Create SIWE message
const message = new SiweMessage({
domain: window.location.host,
address,
statement: 'Sign this message to authenticate with SHIELD.',
uri: window.location.origin,
version: '1',
chainId,
nonce,
});
// 3. Sign message
const messageString = message.prepareMessage();
const signature = await signMessage({ message: messageString });
// 4. Verify
const verifyRes = await fetch('/api/verify-siwe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: messageString, signature }),
});
return await verifyRes.json();
}