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

add helpers for siwe #2123

Merged
merged 1 commit into from
Apr 8, 2024
Merged
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
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"eslint:all",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:@typescript-eslint/strict-type-checked",
"prettier",
"plugin:prettier/recommended"
"prettier"
],
"parserOptions": {
"project": ["tsconfig.json"]
Expand Down
24 changes: 10 additions & 14 deletions apps/laboratory/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,19 +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 { isValidEip191Signature, isValidEip1271Signature } from '@walletconnect/utils'
import {
type SIWESession,
verifySignature,
getChainIdFromMessage,
getAddressFromMessage
} from '@web3modal/siwe'

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

const ETH_ADDRESS_PATTERN = /0x[a-fA-F0-9]{40}/u
const ETH_CHAIN_ID_IN_SIWE_PATTERN = /Chain ID: (?<temp1>\d+)/u

/*
* 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 @@ -49,14 +49,10 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
throw new Error('SiweMessage is undefined')
}
const { message, signature } = credentials
const address = message.match(ETH_ADDRESS_PATTERN)?.[0] || ''
const chainId = `eip155:${
credentials.message.match(ETH_CHAIN_ID_IN_SIWE_PATTERN)?.[1] || 1
}`
let isValid = isValidEip191Signature(address, message, signature)
if (!isValid) {
isValid = await isValidEip1271Signature(address, message, signature, chainId, projectId)
}
const address = getAddressFromMessage(message)
const chainId = getChainIdFromMessage(message)

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

if (isValid) {
return {
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

33 changes: 33 additions & 0 deletions packages/siwe/core/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { isValidEip191Signature, isValidEip1271Signature } from '@walletconnect/utils'

const ETH_ADDRESS_PATTERN = /0x[a-fA-F0-9]{40}/u
const ETH_CHAIN_ID_IN_SIWE_PATTERN = /Chain ID: (?<temp1>\d+)/u

export function getAddressFromMessage(message: string) {
return message.match(ETH_ADDRESS_PATTERN)?.[0] || ''
}

export function getChainIdFromMessage(message: string) {
return `eip155:${message.match(ETH_CHAIN_ID_IN_SIWE_PATTERN)?.[1] || 1}`
}

export async function verifySignature({
address,
message,
signature,
chainId,
projectId
}: {
address: string
message: string
signature: string
chainId: string
projectId: string
}) {
let isValid = isValidEip191Signature(address, message, signature)
if (!isValid) {
isValid = await isValidEip1271Signature(address, message, signature, chainId, projectId)
}

return isValid
}
5 changes: 5 additions & 0 deletions packages/siwe/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type {
SIWEClientMethods
} from '../core/utils/TypeUtils.js'
import { Web3ModalSIWEClient } from '../src/client.js'
export {
getAddressFromMessage,
getChainIdFromMessage,
verifySignature
} from '../core/helpers/index.js'
export { SIWEController, type SIWEControllerClient } from '../core/controller/SIWEController.js'

export type {
Expand Down
1 change: 1 addition & 0 deletions packages/siwe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@web3modal/core": "4.1.5",
"@web3modal/scaffold-utils": "4.1.5",
"@walletconnect/utils": "2.12.0",
"valtio": "1.11.2",
"lit": "3.1.0"
},
Expand Down