Skip to content

Commit

Permalink
feat: Sign 2.5 (#2033)
Browse files Browse the repository at this point in the history
Co-authored-by: Gancho Radkov <[email protected]>
Co-authored-by: Glitch <[email protected]>
Co-authored-by: Glitch <[email protected]>
Co-authored-by: Sven <[email protected]>
Co-authored-by: tomiir <[email protected]>
Co-authored-by: Enes <[email protected]>
  • Loading branch information
7 people authored May 7, 2024
1 parent 5f7ba24 commit a9c53c6
Show file tree
Hide file tree
Showing 33 changed files with 1,459 additions and 172 deletions.
2 changes: 1 addition & 1 deletion apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"tailwind-merge": "2.2.1",
"vaul": "0.9.0",
"viem": "2.7.19",
"wagmi": "2.5.7",
"wagmi": "2.5.19",
"zustand": "4.5.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions apps/laboratory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
"next": "14.0.4",
"next-auth": "4.24.5",
"react-icons": "4.12.0",
"siwe": "2.1.4",
"@walletconnect/utils": "2.12.0",
"valtio": "1.11.2",
"viem": "2.9.3",
"wagmi": "2.5.7"
"wagmi": "2.5.19"
},
"devDependencies": {
"@aws-sdk/client-cloudwatch": "3.509.0",
Expand Down
32 changes: 13 additions & 19 deletions apps/laboratory/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { SIWESession } from '@web3modal/siwe'
import type { NextApiRequest, NextApiResponse } from 'next'
import nextAuth from 'next-auth'
import credentialsProvider from 'next-auth/providers/credentials'
import { getCsrfToken } from 'next-auth/react'
import { SiweMessage } from 'siwe'
import { ethers } from 'ethers'
import {
type SIWESession,
verifySignature,
getChainIdFromMessage,
getAddressFromMessage
} from '@web3modal/siwe'

declare module 'next-auth' {
interface Session extends SIWESession {
address: string
chainId: number
}
}

/*
* For more information on each option (and a full list of options) go to
* https://next-auth.js.org/configuration/options
Expand Down Expand Up @@ -47,22 +48,15 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
if (!credentials?.message) {
throw new Error('SiweMessage is undefined')
}
const siwe = new SiweMessage(credentials.message)
const provider = new ethers.JsonRpcProvider(
`https://rpc.walletconnect.com/v1?chainId=eip155:${siwe.chainId}&projectId=${projectId}`
)
const nonce = await getCsrfToken({ req: { headers: req.headers } })
const result = await siwe.verify(
{
signature: credentials?.signature || '',
nonce
},
{ provider }
)
const { message, signature } = credentials
const address = getAddressFromMessage(message)
const chainId = getChainIdFromMessage(message)

const isValid = await verifySignature({ address, message, signature, chainId, projectId })

if (result.success) {
if (isValid) {
return {
id: `eip155:${siwe.chainId}:${siwe.address}`
id: `${chainId}:${address}`
}
}

Expand Down
1 change: 0 additions & 1 deletion apps/laboratory/src/pages/library/wagmi-siwe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const wagmiConfig = defaultWagmiConfig({
metadata: ConstantsUtil.Metadata,
ssr: true
})

const modal = createWeb3Modal({
wagmiConfig,
projectId: ConstantsUtil.ProjectId,
Expand Down
33 changes: 19 additions & 14 deletions apps/laboratory/src/utils/SiweUtils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { SiweMessage } from 'siwe'
import { getCsrfToken, signIn, signOut, getSession } from 'next-auth/react'
import type { SIWEVerifyMessageArgs, SIWECreateMessageArgs, SIWESession } from '@web3modal/siwe'
import { createSIWEConfig } from '@web3modal/siwe'
import { createSIWEConfig, formatMessage } from '@web3modal/siwe'
import { WagmiConstantsUtil } from '../utils/WagmiConstants'

export const siweConfig = createSIWEConfig({
createMessage: ({ nonce, address, chainId }: SIWECreateMessageArgs) =>
new SiweMessage({
version: '1',
domain: window.location.host,
uri: window.location.origin,
address,
chainId,
nonce,
// Human-readable ASCII assertion that the user will sign, and it must not contain `\n`.
statement: 'Sign in With Ethereum.'
}).prepareMessage(),
// We don't require any async action to populate params but other apps might
// eslint-disable-next-line @typescript-eslint/require-await
getMessageParams: async () => ({
domain: window.location.host,
uri: window.location.origin,
chains: WagmiConstantsUtil.chains.map(chain => chain.id),
statement: 'Please sign with your account'
}),
createMessage: ({ address, ...args }: SIWECreateMessageArgs) => formatMessage(args, address),
getNonce: async () => {
const nonce = await getCsrfToken()
if (!nonce) {
Expand All @@ -33,8 +31,15 @@ export const siweConfig = createSIWEConfig({

return { address, chainId }
},
verifyMessage: async ({ message, signature }: SIWEVerifyMessageArgs) => {
verifyMessage: async ({ message, signature, cacao }: SIWEVerifyMessageArgs) => {
try {
/*
* Signed Cacao (CAIP-74) will be available for further validations if the wallet supports caip-222 signing
* When personal_sign fallback is used, cacao will be undefined
*/
if (cacao) {
// Do something
}
const success = await signIn('credentials', {
message,
redirect: false,
Expand Down
3 changes: 0 additions & 3 deletions apps/laboratory/tests/shared/pages/ModalPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ export class ModalPage {
context: BrowserContext,
mailsacApiKey: string
): Promise<void> {
await this.load()

this.emailAddress = emailAddress

const email = new Email(mailsacApiKey)
Expand Down Expand Up @@ -113,7 +111,6 @@ export class ModalPage {
}

async loginWithEmail(email: string) {
await this.page.goto(this.url)
// Connect Button doesn't have a proper `disabled` attribute so we need to wait for the button to change the text
await this.page
.getByTestId('connect-button')
Expand Down
3 changes: 1 addition & 2 deletions apps/laboratory/tests/smart-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ testModalSmartAccount(
await walletModalPage.openSettings()
await walletModalPage.togglePreferredAccountType()
await walletModalPage.disconnect()
await walletModalPage.page.waitForTimeout(500)

await walletModalPage.emailFlow(
email.getEmailAddressToUse(parallelIndex, NOT_ENABLED_DOMAIN),
Expand All @@ -89,8 +90,6 @@ testModalSmartAccount(
await walletModalPage.page.waitForTimeout(1500)
await walletModalPage.openAccount()
await walletModalPage.openSettings()
await walletModalPage.switchNetwork('Sepolia')
await walletModalValidator.expectSwitchedNetwork('Sepolia')
await walletModalValidator.expectTogglePreferredTypeVisible(false)
await walletModalPage.closeModal()

Expand Down
4 changes: 2 additions & 2 deletions examples/html-wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"build:examples": "vite build"
},
"dependencies": {
"@wagmi/connectors": "4.1.25",
"@wagmi/core": "2.6.16",
"@web3modal/wagmi": "4.1.12-910a844.0",
"@wagmi/connectors": "4.1.14",
"@wagmi/core": "2.6.5",
"react": "18.2.0",
"react-dom": "18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/next-wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"viem": "2.7.13",
"wagmi": "2.5.7"
"wagmi": "2.5.19"
},
"devDependencies": {
"@types/node": "20.11.5",
Expand Down
2 changes: 1 addition & 1 deletion examples/react-wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react-dom": "18.2.0",
"vite": "5.0.12",
"viem": "2.7.13",
"wagmi": "2.5.7"
"wagmi": "2.5.19"
},
"devDependencies": {
"@vitejs/plugin-react": "4.2.1",
Expand Down
6 changes: 3 additions & 3 deletions examples/vue-wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"build:examples": "vite build"
},
"dependencies": {
"@web3modal/wagmi": "4.1.12-910a844.0",
"@wagmi/connectors": "4.1.14",
"@wagmi/core": "2.6.5"
"@wagmi/connectors": "4.1.25",
"@wagmi/core": "2.6.16",
"@web3modal/wagmi": "4.1.12-910a844.0"
},
"devDependencies": {
"@vitejs/plugin-vue": "5.0.2"
Expand Down
Loading

0 comments on commit a9c53c6

Please sign in to comment.