forked from wevm/wagmi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: add siwe guide * chore: changeset
- Loading branch information
Showing
37 changed files
with
775 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'wagmi-private': patch | ||
'wagmi': patch | ||
--- | ||
|
||
add siwe guide |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
NEXT_IRON_PASSWORD= | ||
NEXT_PUBLIC_ALCHEMY_ID= | ||
NEXT_PUBLIC_ETHERSCAN_API_KEY= | ||
NEXT_PUBLIC_INFURA_ID= |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { PreviewWrapper } from './PreviewWrapper' |
5 changes: 2 additions & 3 deletions
5
docs/components/ConnectWallet.tsx → docs/components/guides/ConnectWallet.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as React from 'react' | ||
import { Box, Button, Skeleton, Stack } from 'degen' | ||
import { chain, useAccount, useNetwork } from 'wagmi' | ||
|
||
import { PreviewWrapper } from '../core' | ||
import { Account, SignInWithEthereumButton, WalletSelector } from '../web3' | ||
import { formatAddress } from '../../lib/address' | ||
|
||
export const SignInWithEthereum = () => { | ||
const [state, setState] = React.useState<{ | ||
address?: string | ||
loading?: boolean | ||
}>({}) | ||
const [{ data: accountData }] = useAccount() | ||
const [{ data: networkData }] = useNetwork() | ||
|
||
React.useEffect(() => { | ||
const handler = async () => { | ||
try { | ||
const res = await fetch('/api/me') | ||
const json = await res.json() | ||
setState((x) => ({ ...x, address: json.address })) | ||
} finally { | ||
setState((x) => ({ ...x, loading: false })) | ||
} | ||
} | ||
;(async () => await handler())() | ||
|
||
window.addEventListener('focus', handler) | ||
return () => window.removeEventListener('focus', handler) | ||
}, []) | ||
|
||
const signedInContent = state.address ? ( | ||
<Stack direction="horizontal" align="center" justify="center"> | ||
<Box fontSize="large">Signed in as {formatAddress(state.address)}</Box> | ||
<Button | ||
size="small" | ||
variant="tertiary" | ||
onClick={async () => { | ||
await fetch('/api/logout') | ||
setState({}) | ||
}} | ||
> | ||
Sign Out | ||
</Button> | ||
</Stack> | ||
) : null | ||
|
||
if (accountData) | ||
return ( | ||
<PreviewWrapper> | ||
<Stack space="6"> | ||
<Account /> | ||
|
||
{state.address ? ( | ||
signedInContent | ||
) : ( | ||
<Skeleton loading={state.loading} width="full" radius="2xLarge"> | ||
<SignInWithEthereumButton | ||
address={accountData.address} | ||
chainId={networkData.chain?.id ?? chain.mainnet.id} | ||
onSuccess={({ address }) => | ||
setState((x) => ({ ...x, address })) | ||
} | ||
/> | ||
</Skeleton> | ||
)} | ||
</Stack> | ||
</PreviewWrapper> | ||
) | ||
|
||
return ( | ||
<PreviewWrapper> | ||
<Stack space="6"> | ||
<WalletSelector /> | ||
{signedInContent} | ||
</Stack> | ||
</PreviewWrapper> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { Account } from './Account' | ||
export { ConnectWallet } from './ConnectWallet' | ||
export { PreviewWrapper } from './PreviewWrapper' | ||
export { SignInWithEthereum } from './SignInWithEthereum' | ||
export { SignMessage } from './SignMessage' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as React from 'react' | ||
import { Button, IconEth, Stack, Text } from 'degen' | ||
import { SiweMessage } from 'siwe' | ||
import { useSignMessage } from 'wagmi' | ||
|
||
type Props = { | ||
address: string | ||
chainId: number | ||
onSuccess?(data: { address: string }): void | ||
} | ||
|
||
export const SignInWithEthereumButton = ({ | ||
address, | ||
chainId, | ||
onSuccess, | ||
}: Props) => { | ||
const [state, setState] = React.useState<{ | ||
error?: Error | ||
loading?: boolean | ||
}>({}) | ||
const [, signMessage] = useSignMessage() | ||
|
||
const handleSignIn = React.useCallback(async () => { | ||
try { | ||
setState((x) => ({ ...x, error: undefined, loading: true })) | ||
const nonceRes = await fetch('/api/nonce') | ||
const message = new SiweMessage({ | ||
domain: window.location.host, | ||
address, | ||
statement: 'Sign in with Ethereum to the app.', | ||
uri: window.location.origin, | ||
version: '1', | ||
chainId, | ||
nonce: await nonceRes.text(), | ||
}) | ||
|
||
const signRes = await signMessage({ message: message.prepareMessage() }) | ||
if (signRes.error) throw signRes.error | ||
|
||
const signature = signRes.data | ||
const verifyRes = await fetch('/api/verify', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ message, signature }), | ||
}) | ||
if (!verifyRes.ok) throw new Error('Error verifying message') | ||
|
||
setState((x) => ({ ...x, loading: false })) | ||
onSuccess && onSuccess({ address }) | ||
} catch (error) { | ||
setState((x) => ({ ...x, error: error as Error, loading: false })) | ||
} | ||
}, [address, chainId, signMessage, onSuccess]) | ||
|
||
return ( | ||
<Stack space="4"> | ||
<Button | ||
prefix={!state.loading && <IconEth />} | ||
width="full" | ||
loading={state.loading} | ||
disabled={state.loading} | ||
center | ||
onClick={handleSignIn} | ||
> | ||
{state.loading ? 'Check Wallet' : 'Sign-In with Ethereum'} | ||
</Button> | ||
|
||
{state.error && ( | ||
<Text color="red">{state.error?.message ?? 'Failed to sign in'}</Text> | ||
)} | ||
</Stack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { Account } from './Account' | ||
export { WalletSelector } from './WalletSelector' | ||
export { SignInWithEthereumButton } from './SignInWithEthereumButton' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const formatAddress = (address: string) => | ||
`${address.slice(0, 6)}…${address.slice(38, 42)}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IronSessionOptions } from 'iron-session' | ||
|
||
export const ironOptions: IronSessionOptions = { | ||
cookieName: 'siwe', | ||
password: process.env.NEXT_IRON_PASSWORD as string, | ||
cookieOptions: { | ||
secure: process.env.NODE_ENV === 'production', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { withIronSessionApiRoute } from 'iron-session/next' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
import { ironOptions } from '../../lib/iron' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const { method } = req | ||
switch (method) { | ||
case 'GET': | ||
req.session.destroy() | ||
res.send({ ok: true }) | ||
break | ||
default: | ||
res.setHeader('Allow', ['GET']) | ||
res.status(405).end(`Method ${method} Not Allowed`) | ||
} | ||
} | ||
|
||
export default withIronSessionApiRoute(handler, ironOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { withIronSessionApiRoute } from 'iron-session/next' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
import { ironOptions } from '../../lib/iron' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const { method } = req | ||
switch (method) { | ||
case 'GET': | ||
res.send({ address: req.session.siwe?.address }) | ||
break | ||
default: | ||
res.setHeader('Allow', ['GET']) | ||
res.status(405).end(`Method ${method} Not Allowed`) | ||
} | ||
} | ||
|
||
export default withIronSessionApiRoute(handler, ironOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { generateNonce } from 'siwe' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const { method } = req | ||
switch (method) { | ||
case 'GET': | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.send(generateNonce()) | ||
break | ||
default: | ||
res.setHeader('Allow', ['GET']) | ||
res.status(405).end(`Method ${method} Not Allowed`) | ||
} | ||
} | ||
|
||
export default handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { withIronSessionApiRoute } from 'iron-session/next' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { SiweMessage } from 'siwe' | ||
|
||
import { ironOptions } from '../../lib/iron' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const { method } = req | ||
switch (method) { | ||
case 'POST': | ||
try { | ||
const { message, signature } = req.body | ||
const siweMessage = new SiweMessage(message) | ||
const fields = await siweMessage.validate(signature) | ||
req.session.siwe = fields | ||
await req.session.save() | ||
|
||
res.json({ ok: true }) | ||
} catch (_error) { | ||
res.json({ ok: false }) | ||
} | ||
break | ||
default: | ||
res.setHeader('Allow', ['POST']) | ||
res.status(405).end(`Method ${method} Not Allowed`) | ||
} | ||
} | ||
|
||
export default withIronSessionApiRoute(handler, ironOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.