Skip to content

Commit

Permalink
chore: upgrade wagmi / viem to latest version and add siwe verify end…
Browse files Browse the repository at this point in the history
…point

chore: tweak

chore: prettify

revert: removing getSiweMessageOptions

chore: tweak
  • Loading branch information
magiziz committed May 28, 2024
1 parent 9433904 commit 571de08
Show file tree
Hide file tree
Showing 28 changed files with 190 additions and 332 deletions.
8 changes: 5 additions & 3 deletions examples/with-create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"react-scripts": "5.0.1",
"react": "^18.3.0",
"typescript": "5.4.2",
"util": "0.12.5",
"viem": "2.12.1",
"util": "0.12.4",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
"@tanstack/react-query": "^5.28.4",
"web-vitals": "^2.1.4",
"buffer": "npm:buffer@6.0.3"
},
"scripts": {
"dev": "GENERATE_SOURCEMAP=false react-scripts start",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-app-i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"next-intl": "^3.9.4",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-custom-button/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-mint-nft/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-rainbow-button/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-siwe-iron-session/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
13 changes: 7 additions & 6 deletions examples/with-next-siwe-iron-session/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const { wallets } = getDefaultWallets();

const config = getDefaultConfig({
export const wagmiConfig = getDefaultConfig({
appName: 'RainbowKit demo',
projectId: 'YOUR_PROJECT_ID',
wallets: [
Expand Down Expand Up @@ -97,12 +97,13 @@ export default function App({ Component, pageProps }: AppProps) {

createMessage: ({ nonce, address, chainId }) => {
return createSiweMessage({
address,
chainId,
domain: window.location.host,
nonce,
address,
statement: 'Sign in with Ethereum to the app.',
uri: window.location.origin,
version: '1'
version: '1',
chainId,
nonce
});
},

Expand Down Expand Up @@ -138,7 +139,7 @@ export default function App({ Component, pageProps }: AppProps) {
}, []);

return (
<WagmiProvider config={config}>
<WagmiProvider config={wagmiConfig}>
<QueryClientProvider client={queryClient}>
<RainbowKitAuthenticationProvider
adapter={authAdapter}
Expand Down
24 changes: 15 additions & 9 deletions examples/with-next-siwe-iron-session/pages/api/verify.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { withIronSessionApiRoute } from 'iron-session/next';
import { NextApiRequest, NextApiResponse } from 'next';
import { ironOptions } from './../../lib/iron';
import { createSiweMessage } from 'viem/siwe';
import { publicActions } from 'viem';
import { wagmiConfig } from '../_app';
import { parseSiweMessage } from 'viem/siwe';

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const { method } = req;
switch (method) {
case 'POST':
try {
const { message, signature } = req.body;
const siweMessage = createSiweMessage(message);
// TODO: remove @ts-ignore
// @ts-ignore
const { success, error, data } = await siweMessage.verify({
const { message, signature, address } = req.body;

const publicClient = wagmiConfig.getClient().extend(publicActions);

const valid = await publicClient.verifyMessage({
address,
message,
signature,
});

if (!success) throw error;
if (!valid) {
throw new Error('Invalid message');
}

if (data.nonce !== req.session.nonce)
if (parseSiweMessage(message).nonce !== req.session.nonce)
return res.status(422).json({ message: 'Invalid nonce.' });

req.session.siwe = data;
req.session.siwe = { address };
await req.session.save();
res.json({ ok: true });
} catch (_error) {
Expand Down
4 changes: 2 additions & 2 deletions examples/with-next-siwe-iron-session/types/iron-session.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'iron-session';
import { SiweMessage } from 'siwe';
import { Address } from 'viem';

declare module 'iron-session' {
interface IronSessionData {
nonce?: string;
siwe?: SiweMessage;
siwe?: { address: Address };
}
}
2 changes: 1 addition & 1 deletion examples/with-next-siwe-next-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"next-auth": "4.24.5",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
12 changes: 9 additions & 3 deletions examples/with-next-siwe-next-auth/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const { wallets } = getDefaultWallets();

const config = getDefaultConfig({
export const wagmiConfig = getDefaultConfig({
appName: 'RainbowKit demo',
projectId: 'YOUR_PROJECT_ID',
wallets: [
Expand All @@ -53,6 +53,10 @@ const config = getDefaultConfig({
ssr: true,
});

const getSiweMessageOptions: GetSiweMessageOptions = () => ({
statement: 'Sign in to the RainbowKit + SIWE example app',
});

const queryClient = new QueryClient();

export default function App({
Expand All @@ -63,9 +67,11 @@ export default function App({
}>) {
return (
<SessionProvider refetchInterval={0} session={pageProps.session}>
<WagmiProvider config={config}>
<WagmiProvider config={wagmiConfig}>
<QueryClientProvider client={queryClient}>
<RainbowKitSiweNextAuthProvider>
<RainbowKitSiweNextAuthProvider
getSiweMessageOptions={getSiweMessageOptions}
>
<RainbowKitProvider>
<Component {...pageProps} />
</RainbowKitProvider>
Expand Down
42 changes: 25 additions & 17 deletions examples/with-next-siwe-next-auth/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,57 @@ import { NextApiRequest, NextApiResponse } from 'next';
import NextAuth, { NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { getCsrfToken } from 'next-auth/react';
import { createSiweMessage } from 'viem/siwe';
import { wagmiConfig } from '../../_app';
import { ByteArray, Hex, Signature, publicActions } from 'viem';
import { parseSiweMessage } from 'viem/siwe';

export function getAuthOptions(req: IncomingMessage): NextAuthOptions {
const providers = [
CredentialsProvider({
async authorize(credentials) {
try {
const siwe = createSiweMessage(
JSON.parse(credentials?.message || '{}')
);
const message = credentials?.message ?? '';

const { domain, nonce, address } = parseSiweMessage(message);

if (!address) return null;

const nextAuthUrl =
process.env.NEXTAUTH_URL ||
(process.env.VERCEL_URL
? `https://${process.env.VERCEL_URL}`
: null);

if (!nextAuthUrl) {
return null;
}

const nextAuthHost = new URL(nextAuthUrl).host;
// TODO: remove @ts-ignore
// @ts-ignore
if (siwe.domain !== nextAuthHost) {

if (domain !== nextAuthHost) {
return null;
}

if (
// TODO: remove @ts-ignore
// @ts-ignore
siwe.nonce !==
(await getCsrfToken({ req: { headers: req.headers } }))
nonce !== (await getCsrfToken({ req: { headers: req.headers } }))
) {
return null;
}

// TODO: remove @ts-ignore
// @ts-ignore
await siwe.verify({ signature: credentials?.signature || '' });
const publicClient = wagmiConfig.getClient().extend(publicActions);

const valid = await publicClient.verifyMessage({
address,
message,
signature: credentials?.signature as Hex | ByteArray | Signature,
});

if (!valid) {
throw new Error('Invalid message');
}

return {
// TODO: remove @ts-ignore
// @ts-ignore
id: siwe.address,
id: address,
};
} catch (e) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-wallet-button/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@remix-run/serve": "^2.9.2",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/with-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@rainbow-me/rainbowkit": "workspace:*",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7",
"@tanstack/react-query": "^5.28.4"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"react-dom": "^18.3.0",
"recursive-readdir-files": "^2.3.1",
"typescript": "5.4.2",
"viem": "2.12.1",
"viem": "2.13.1",
"vitest": "^0.33.0",
"wagmi": "^2.9.7",
"dotenv": "^16.4.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/create-rainbowkit/generated-test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-rainbowkit/templates/next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"next": "^14.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"next-auth": "4.24.5",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"viem": "2.12.1",
"viem": "2.13.1",
"wagmi": "^2.9.7"
},
"scripts": {
Expand Down

0 comments on commit 571de08

Please sign in to comment.