Skip to content

Commit

Permalink
update next example to normalize address
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomas committed Nov 20, 2024
1 parent 5e60da7 commit ff5fd76
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 77 deletions.
19 changes: 17 additions & 2 deletions dapps/appkit-siwe/next/app/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getCsrfToken, getSession, signIn, signOut } from 'next-auth/react';

import { cookieStorage, createStorage } from 'wagmi';
import { arbitrum, mainnet, sepolia, optimism, AppKitNetwork } from '@reown/appkit/networks'
import { CaipNetwork } from '@reown/appkit';
import { getAddress } from 'viem';

// Get projectId from https://cloud.reown.com
export const projectId = process.env.NEXT_PUBLIC_PROJECT_ID;
Expand All @@ -34,6 +34,21 @@ export const wagmiAdapter = new WagmiAdapter({
ssr: true
});

// Normalize the address (checksum)
const normalizeAddress = (address: string): string => {
try {
const splitAddress = address.split(':');
const extractedAddress = splitAddress[splitAddress.length - 1];
const checksumAddress = getAddress(extractedAddress);
splitAddress[splitAddress.length - 1] = checksumAddress;
const normalizedAddress = splitAddress.join(':');

return normalizedAddress;
} catch (error) {
return address;
}
}

export const siweConfig = createSIWEConfig({
getMessageParams: async () => ({
domain: typeof window !== 'undefined' ? window.location.host : '',
Expand All @@ -42,7 +57,7 @@ export const siweConfig = createSIWEConfig({
statement: 'Please sign with your account',
}),
createMessage: ({ address, ...args }: SIWECreateMessageArgs) =>
formatMessage(args, address),
formatMessage(args, normalizeAddress(address)),
getNonce: async () => {
const nonce = await getCsrfToken();
if (!nonce) {
Expand Down
6 changes: 3 additions & 3 deletions dapps/appkit-siwe/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
},
"dependencies": {
"@tanstack/react-query": "^5.53.1",
"@reown/appkit-siwe": "^1.1.2",
"@reown/appkit": "^1.1.2",
"@reown/appkit-adapter-wagmi": "^1.1.2",
"@reown/appkit-siwe": "1.4.1",
"@reown/appkit": "1.4.1",
"@reown/appkit-adapter-wagmi": "1.4.1",
"next": "14.2.7",
"next-auth": "^4.24.7",
"react": "^18",
Expand Down
Loading

0 comments on commit ff5fd76

Please sign in to comment.