Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: EIP-4361 support #2032

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/rainbowkit-siwe-next-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@
"@rainbow-me/rainbowkit": "2.0.x || 2.1.x",
"next-auth": ">=4.21.0 <5",
"react": ">=18",
"siwe": "^2.1.4"
"viem": ">=2.12.0"
},
"devDependencies": {
"@rainbow-me/rainbowkit": "workspace:*",
"ethers": "^5.6.8",
"siwe": "^2.1.4"
"@rainbow-me/rainbowkit": "workspace:*"
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import {
} from '@rainbow-me/rainbowkit';
import { getCsrfToken, signIn, signOut, useSession } from 'next-auth/react';
import React, { ReactNode, useMemo } from 'react';
import { SiweMessage } from 'siwe';
import type { Address } from 'viem';
import {
type CreateSiweMessageParameters,
createSiweMessage,
parseSiweMessage,
} from 'viem/siwe';

type UnconfigurableMessageOptions = {
address: string;
address: Address;
chainId: number;
nonce: string;
};

type ConfigurableMessageOptions = Partial<
Omit<SiweMessage, keyof UnconfigurableMessageOptions>
> & {
[_Key in keyof UnconfigurableMessageOptions]?: never;
};
type ConfigurableMessageOptions = Omit<
CreateSiweMessageParameters,
keyof UnconfigurableMessageOptions
>;

export type GetSiweMessageOptions = () => ConfigurableMessageOptions;
export type GetSiweMessageOptions = () => Partial<ConfigurableMessageOptions>;

interface RainbowKitSiweNextAuthProviderProps {
enabled?: boolean;
Expand Down Expand Up @@ -49,18 +53,25 @@ export function RainbowKitSiweNextAuthProvider({
nonce,
};

return new SiweMessage({
...defaultConfigurableOptions,
const { domain, uri, version, statement, ...restSiweOptions } =
getSiweMessageOptions?.() ?? {};

return createSiweMessage({
// Use provided SIWE message options, fallback to defaults if undefined
domain: domain ?? defaultConfigurableOptions.domain,
uri: uri ?? defaultConfigurableOptions.uri,
version: version ?? defaultConfigurableOptions.version,
statement: statement ?? defaultConfigurableOptions.statement,

// Spread custom SIWE message options provided by the consumer
...getSiweMessageOptions?.(),
...restSiweOptions,

// Spread unconfigurable options last so they can't be overridden
...unconfigurableOptions,
});
},

getMessageBody: ({ message }) => message.prepareMessage(),
getMessageBody: ({ message }) => message,

getNonce: async () => {
const nonce = await getCsrfToken();
Expand All @@ -74,7 +85,9 @@ export function RainbowKitSiweNextAuthProvider({

verify: async ({ message, signature }) => {
const response = await signIn('credentials', {
message: JSON.stringify(message),
// Consumers will receive a stringified JSON, which they can parse afterwards.
// This approach is taken to prevent breaking changes for previous versions.
message: JSON.stringify(parseSiweMessage(message)),
redirect: false,
signature,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useMemo,
useState,
} from 'react';
import type { Address } from 'viem';
import { Config, useAccount, useAccountEffect } from 'wagmi';

export type AuthenticationStatus =
Expand All @@ -17,7 +18,7 @@ export interface AuthenticationAdapter<Message> {
getNonce: () => Promise<string>;
createMessage: (args: {
nonce: string;
address: string;
address: Address;
chainId: number;
}) => Message;
getMessageBody: (args: { message: Message }) => string;
Expand Down
61 changes: 50 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions site/data/en-US/docs/authentication.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ If you haven't already, first set up your [Next.js](https://nextjs.org) project

#### Install

Install the `@rainbow-me/rainbowkit-siwe-next-auth` package and its peer dependency, [ethers](https://docs.ethers.org/v5/).
Install the `@rainbow-me/rainbowkit-siwe-next-auth` package.

```bash
npm install @rainbow-me/rainbowkit-siwe-next-auth siwe@^2 ethers@^5
npm install @rainbow-me/rainbowkit-siwe-next-auth
```

> Note: `siwe` requires the [ethers](https://docs.ethers.org/v5/) peer dependency, while [wagmi](https://wagmi.sh/) now relies on the alternative [viem](https://viem.sh).

#### Set up the provider

In your `App` component, import `RainbowKitSiweNextAuthProvider`.
Expand Down