Skip to content

Commit

Permalink
solana ledger sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
shan57blocks committed Nov 12, 2024
1 parent 2f8c02b commit 2c7ee00
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 250 deletions.
18 changes: 18 additions & 0 deletions packages/huma-shared/src/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,25 @@ const verifySignature = async (
},
)

const verifySolanaTx = async (
message: string,
serializedTx: number[],
chainId: number,
isDev: boolean = false,
): Promise<null> =>
requestPost<null>(
`${configUtil.getAuthServiceUrl(
chainId,
isDev,
)}/verify-signature?chainId=${chainId}`,
{
message,
serializedTx,
},
)

export const AuthService = {
createSession,
verifySignature,
verifySolanaTx,
}
250 changes: 0 additions & 250 deletions packages/huma-web-shared/src/hooks/useAuthErrorHandling.ts

This file was deleted.

89 changes: 89 additions & 0 deletions packages/huma-web-shared/src/hooks/useAuthErrorHandling/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { CHAIN_TYPE } from '@huma-finance/shared'
import axios, { HttpStatusCode } from 'axios'
import { useCallback, useState } from 'react'
import { useAuthErrorHandingEvm } from './useAuthErrorHandingEvm'
import { useAuthErrorHandingSolana } from './useAuthErrorHandingSolana'

export type ErrorType = 'NotSignedIn' | 'UserRejected' | 'Other'

export type AuthState = {
isWalletOwnershipVerificationRequired: boolean
isWalletOwnershipVerified: boolean
errorType?: ErrorType
error: unknown
setError: React.Dispatch<React.SetStateAction<unknown>>
reset: () => void
}

export const useAuthErrorHandling = (
isDev: boolean,
chainType: CHAIN_TYPE = CHAIN_TYPE.EVM,
): AuthState => {
const [error, setError] = useState<unknown>(null)
const [isVerified, setIsVerified] = useState<boolean>(false)
const [errorType, setErrorType] = useState<ErrorType | undefined>()
const [isVerificationRequired, setIsVerificationRequired] =
useState<boolean>(false)

const handleVerificationCompletion = useCallback(() => {
setIsVerified(true)
}, [])

const getErrorInfo = useCallback((error: any) => {
const isUnauthorizedError =
axios.isAxiosError(error) &&
error.response?.status === HttpStatusCode.Unauthorized &&
[
'IdTokenNotFoundException',
'InvalidIdTokenException',
'WalletMismatchException',
].includes(error.response?.data?.detail?.type)

const isWalletNotCreatedError = error === 'WalletNotCreatedException'
const isWalletNotSignInError = error === 'WalletNotSignInException'

return {
isUnauthorizedError,
isWalletNotCreatedError,
isWalletNotSignInError,
}
}, [])

useAuthErrorHandingEvm(
chainType,
isDev,
error,
getErrorInfo,
setError,
setErrorType,
setIsVerificationRequired,
handleVerificationCompletion,
)
useAuthErrorHandingSolana(
chainType,
isDev,
error,
getErrorInfo,
setError,
setErrorType,
setIsVerificationRequired,
handleVerificationCompletion,
)

const reset = useCallback(() => {
setIsVerificationRequired(false)
setIsVerified(false)
setError(null)
setErrorType(undefined)
}, [])

return {
isWalletOwnershipVerificationRequired: isVerificationRequired,
isWalletOwnershipVerified: isVerified,
errorType,
error,
setError,
reset,
}
}
Loading

0 comments on commit 2c7ee00

Please sign in to comment.