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

refactor(siwe): ox #3151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 27 additions & 1 deletion pnpm-lock.yaml

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

26 changes: 0 additions & 26 deletions src/errors/siwe.test.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/errors/siwe.ts

This file was deleted.

16 changes: 10 additions & 6 deletions src/siwe/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// biome-ignore lint/performance/noBarrelFile: entrypoint module
export {
type Message as SiweMessage,
generateNonce as generateSiweNonce,
InvalidMessageFieldError as SiweInvalidMessageFieldError,
} from 'ox/Siwe'

export {
verifySiweMessage,
type VerifySiweMessageParameters,
Expand All @@ -13,7 +19,6 @@ export {
type CreateSiweMessageErrorType,
} from '../utils/siwe/createSiweMessage.js'

export { generateSiweNonce } from '../utils/siwe/generateSiweNonce.js'
export { parseSiweMessage } from '../utils/siwe/parseSiweMessage.js'

export {
Expand All @@ -22,9 +27,8 @@ export {
type ValidateSiweMessageReturnType,
} from '../utils/siwe/validateSiweMessage.js'

export type { SiweMessage } from '../utils/siwe/types.js'
import type { Siwe } from 'ox'

export {
type SiweInvalidMessageFieldErrorType,
SiweInvalidMessageFieldError,
} from '../errors/siwe.js'
export type SiweInvalidMessageFieldErrorType = Siwe.InvalidMessageFieldError & {
name: 'SiweInvalidMessageFieldError'
}
59 changes: 20 additions & 39 deletions src/utils/siwe/createSiweMessage.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Siwe } from 'ox'
import { expect, test, vi } from 'vitest'

import { mainnet } from '../../chains/definitions/mainnet.js'
import { createSiweMessage } from './createSiweMessage.js'
import type { SiweMessage } from './types.js'

const message = {
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
Expand All @@ -11,7 +11,7 @@ const message = {
nonce: 'foobarbaz',
uri: 'https://example.com/path',
version: '1',
} satisfies SiweMessage
} satisfies Siwe.Message

test('default', () => {
vi.useFakeTimers()
Expand Down Expand Up @@ -249,72 +249,61 @@ test('behavior: invalid address', () => {
expect(() =>
createSiweMessage({ ...message, address: '0xfoobarbaz' }),
).toThrowErrorMatchingInlineSnapshot(`
[InvalidAddressError: Address "0xfoobarbaz" is invalid.
[Address.InvalidAddressError: Address "0xfoobarbaz" is invalid.

- Address must be a hex value of 20 bytes (40 hex characters).
- Address must match its checksum counterpart.

Version: [email protected]]
Details: Address is not a 20 byte (40 hexadecimal character) value.]
`)
})

test('behavior: invalid chainId', () => {
expect(() =>
createSiweMessage({ ...message, chainId: 1.1 }),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "chainId".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "chainId".

- Chain ID must be a EIP-155 chain ID.
- See https://eips.ethereum.org/EIPS/eip-155

Provided value: 1.1

Version: [email protected]]
Provided value: 1.1]
`)
})

test('behavior: invalid domain', () => {
expect(() =>
createSiweMessage({ ...message, domain: '#foo' }),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "domain".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "domain".

- Domain must be an RFC 3986 authority.
- See https://www.rfc-editor.org/rfc/rfc3986

Provided value: #foo

Version: [email protected]]
Provided value: #foo]
`)
})

test('behavior: invalid nonce', () => {
expect(() =>
createSiweMessage({ ...message, nonce: '#foo' }),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "nonce".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "nonce".

- Nonce must be at least 8 characters.
- Nonce must be alphanumeric.

Provided value: #foo

Version: [email protected]]
Provided value: #foo]
`)
})

test('behavior: invalid uri', () => {
expect(() =>
createSiweMessage({ ...message, uri: '#foo' }),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "uri".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "uri".

- URI must be a RFC 3986 URI referring to the resource that is the subject of the signing.
- See https://www.rfc-editor.org/rfc/rfc3986

Provided value: #foo

Version: [email protected]]
Provided value: #foo]
`)
})

Expand All @@ -323,43 +312,37 @@ test('behavior: invalid version', () => {
// @ts-expect-error
createSiweMessage({ ...message, version: '2' }),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "version".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "version".

- Version must be '1'.

Provided value: 2

Version: [email protected]]
Provided value: 2]
`)
})

test('behavior: invalid scheme', () => {
expect(() =>
createSiweMessage({ ...message, scheme: 'foo_bar' }),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "scheme".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "scheme".

- Scheme must be an RFC 3986 URI scheme.
- See https://www.rfc-editor.org/rfc/rfc3986#section-3.1

Provided value: foo_bar

Version: [email protected]]
Provided value: foo_bar]
`)
})

test('behavior: invalid statement', () => {
expect(() =>
createSiweMessage({ ...message, statement: 'foo\nbar' }),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "statement".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "statement".

- Statement must not include '\\n'.

Provided value: foo
bar

Version: [email protected]]
bar]
`)
})

Expand All @@ -370,14 +353,12 @@ test('behavior: invalid resources', () => {
resources: ['https://example.com', 'foo'],
}),
).toThrowErrorMatchingInlineSnapshot(`
[SiweInvalidMessageFieldError: Invalid Sign-In with Ethereum message field "resources".
[Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "resources".

- Every resource must be a RFC 3986 URI.
- See https://www.rfc-editor.org/rfc/rfc3986

Provided value: foo

Version: [email protected]]
Provided value: https://example.com]
`)
})

Expand Down
Loading
Loading