Skip to content

Commit

Permalink
feat: add challenge validation (#251)
Browse files Browse the repository at this point in the history
* feat: add challenge validation
* fix: rola challenge validation case insensitive
  • Loading branch information
dawidsowardx authored Sep 17, 2024
1 parent 7bf404b commit 5eab5c7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/dapp-toolkit/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './typed-error'
export * from './unwrap-observable'
export * from './validate-wallet-response'
export * from './generate-rola-challenge'
export * from './validate-rola-challenge'
export * from './parse-signed-challenge'
32 changes: 32 additions & 0 deletions packages/dapp-toolkit/src/helpers/validate-rola-challenge.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest'
import { validateRolaChallenge } from './validate-rola-challenge'

describe('validateRolaChallenge', () => {
const validTestVectors = [
'3f30f8d67ca69af8b646170d6ddd0a16cb501dcb7d457d0b49ef78a5d1b4beac',
'0a6a5b8beac0e56b8613f1b1a08223b3986aa28b9acc81d16493c75a428f436e',
'2455077b5bb93e1d5c9816513c3385b88293d91b9d44ed6bd652764834eb997a',
'2455077b5BB93e1d5c9816513c3385b88293d91b9d44ed6BD652764834eb997a',
]

const invalidTestVectors = [
'abc',
'',
undefined,
null,
{},
'3f30f8d67ca69af8b646170d6ddd0a16cb501dcb7d457d0b49ef78a5d1b4beacz',
]

it('should return true for valid challenge', () => {
validTestVectors.forEach((challenge) => {
expect(validateRolaChallenge(challenge)).toBe(true)
})
})

it('should return false for invalid challenge', () => {
invalidTestVectors.forEach((challenge) => {
expect(validateRolaChallenge(challenge)).toBe(false)
})
})
})
2 changes: 2 additions & 0 deletions packages/dapp-toolkit/src/helpers/validate-rola-challenge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const validateRolaChallenge = (challenge?: unknown) =>
typeof challenge === 'string' && /^[0-9a-f]{64}$/i.test(challenge);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
switchMap,
tap,
} from 'rxjs'
import type { Logger } from '../../helpers'
import { validateRolaChallenge, type Logger } from '../../helpers'
import { TransactionStatus } from '../gateway'
import { Result, ResultAsync, err, ok, okAsync } from 'neverthrow'
import type {
Expand Down Expand Up @@ -181,6 +181,10 @@ export const WalletRequestModule = (input: {

return ResultAsync.fromPromise(challengeGeneratorFn(), () =>
SdkError('ChallengeGeneratorError', '', 'failed to generate challenge'),
).andThen((challenge) =>
validateRolaChallenge(challenge)
? ok(challenge)
: err(SdkError('ChallengeValidationError', '', 'challenge is invalid')),
)
}

Expand Down

0 comments on commit 5eab5c7

Please sign in to comment.