-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate user-provided
AuthSignature
(#537)
- Loading branch information
Showing
24 changed files
with
276 additions
and
126 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
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,16 @@ | ||
import { ethers } from 'ethers'; | ||
import { z } from 'zod'; | ||
|
||
export const ETH_ADDRESS_REGEXP = new RegExp('^0x[a-fA-F0-9]{40}$'); | ||
|
||
const isAddress = (address: string) => { | ||
try { | ||
return ethers.utils.getAddress(address); | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export const EthAddressSchema = z.string() | ||
.regex(ETH_ADDRESS_REGEXP) | ||
.refine(isAddress, { message: 'Invalid Ethereum address' }); |
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,16 @@ | ||
import {describe, expect, it} from 'vitest'; | ||
|
||
import { EthAddressSchema } from '../src'; | ||
|
||
|
||
describe('ethereum address schema', () => { | ||
it('should accept valid ethereum address', () => { | ||
const validAddress = '0x1234567890123456789012345678901234567890'; | ||
EthAddressSchema.parse(validAddress); | ||
}); | ||
|
||
it('should reject invalid ethereum address', () => { | ||
const invalidAddress = '0x123456789012345678901234567890123456789'; | ||
expect(() => EthAddressSchema.parse(invalidAddress)).toThrow(); | ||
}); | ||
}); |
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
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 { EthAddressSchema } from '@nucypher/shared'; | ||
import { z } from 'zod'; | ||
|
||
import { EIP4361_AUTH_METHOD, EIP712_AUTH_METHOD } from './auth-provider'; | ||
import { EIP4361TypedDataSchema, EIP712TypedDataSchema } from './providers'; | ||
|
||
|
||
export const authSignatureSchema = z.object({ | ||
signature: z.string(), | ||
address: EthAddressSchema, | ||
scheme: z.enum([EIP712_AUTH_METHOD, EIP4361_AUTH_METHOD]), | ||
typedData: z.union([ | ||
EIP4361TypedDataSchema, | ||
// TODO(#536): Remove post EIP712 deprecation | ||
EIP712TypedDataSchema, | ||
]), | ||
}); | ||
|
||
export type AuthSignature = z.infer<typeof authSignatureSchema>; |
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,16 +1,19 @@ | ||
import {ethers} from "ethers"; | ||
import { ethers } from 'ethers'; | ||
|
||
import { EIP4361AuthProvider, EIP4361AuthProviderParams } from './providers/eip4361'; | ||
import { EIP712AuthProvider } from './providers/eip712'; | ||
import { AuthProviders, EIP4361_AUTH_METHOD, EIP712_AUTH_METHOD } from './types'; | ||
import { AuthProviders, EIP4361_AUTH_METHOD, EIP712_AUTH_METHOD } from './auth-provider'; | ||
import { | ||
EIP4361AuthProvider, | ||
EIP4361AuthProviderParams, | ||
EIP712AuthProvider, | ||
} from './providers'; | ||
|
||
export const makeAuthProviders = ( | ||
provider: ethers.providers.Provider, | ||
signer?: ethers.Signer, | ||
siweDefaultParams?: EIP4361AuthProviderParams | ||
siweDefaultParams?: EIP4361AuthProviderParams, | ||
): AuthProviders => { | ||
return { | ||
[EIP712_AUTH_METHOD]: signer ? new EIP712AuthProvider(provider, signer) : undefined, | ||
[EIP4361_AUTH_METHOD]: signer ? new EIP4361AuthProvider(provider, signer, siweDefaultParams) : undefined | ||
[EIP4361_AUTH_METHOD]: signer ? new EIP4361AuthProvider(provider, signer, siweDefaultParams) : undefined, | ||
} as AuthProviders; | ||
}; |
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,4 @@ | ||
export * from './providers/eip4361'; | ||
export * from './providers/eip712'; | ||
export * from './types'; | ||
export * from './providers'; | ||
export * from './helper'; | ||
export * from './auth-sig'; | ||
export * from './auth-provider'; |
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
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,25 +1,2 @@ | ||
import { EIP4361_AUTH_METHOD, EIP712_AUTH_METHOD } from '../types'; | ||
|
||
import { EIP4361AuthProvider, EIP4361TypedData } from './eip4361'; | ||
import { EIP712AuthProvider, EIP712TypedData } from './eip712'; | ||
|
||
export interface AuthSignatureProvider { | ||
getOrCreateAuthSignature(): Promise<AuthSignature>; | ||
} | ||
|
||
export type AuthProviders = { | ||
[EIP712_AUTH_METHOD]?: EIP712AuthProvider; | ||
[EIP4361_AUTH_METHOD]?: EIP4361AuthProvider; | ||
// Fallback to satisfy type checking | ||
[key: string]: AuthProvider | undefined; | ||
}; | ||
|
||
export interface AuthSignature { | ||
signature: string; | ||
address: string; | ||
scheme: 'EIP712' | 'EIP4361'; | ||
typedData: EIP712TypedData | EIP4361TypedData; | ||
} | ||
|
||
// Add other providers here | ||
export type AuthProvider = AuthSignatureProvider; | ||
export * from './eip712'; | ||
export * from './eip4361'; |
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.