TypeScript Types Reference
Midnight Auth is written in TypeScript and includes comprehensive type definitions. This page provides a complete reference of all exported types.
Importing Types
All types are exported from the main package:
import type {
// Core Types
WalletState,
BalanceBreakdown,
Session,
// API Types
MidnightWalletAPI,
MidnightProvider,
SignDataResult,
TransactionResult,
// Context & Provider Types
MidnightAuthContextValue,
MidnightAuthProviderProps,
// Component Props
MidnightConnectButtonProps,
MidnightWalletInfoProps,
MidnightSessionTimerProps,
ProtectedRouteProps
} from '@uppzen/midnight-auth'
Core Types
WalletState
Complete wallet state returned by the Midnight provider.
interface WalletState {
// Address fields
address?: string
shieldAddress?: string
addressLegacy?: string
legacyAddress?: string
// Balance
balance?: string
balances?: BalanceBreakdown
// Provider info
provider?: string
providerName?: string
// Cryptographic keys
coinPublicKey?: string
coinPublicKeyLegacy?: string
encryptionPublicKey?: string
encryptionPublicKeyLegacy?: string
// Versioning
apiVersion?: string
// Extensible
[key: string]: any
}
BalanceBreakdown
Detailed balance information.
interface BalanceBreakdown {
unshielded: string
shielded: string
total: string
}
Session
Session tracking information.
interface Session {
address: string
connectedAt: number
expiresAt?: number
metadata?: Record<string, any>
}
API Types
MidnightWalletAPI
Wallet API interface from the provider.
interface MidnightWalletAPI {
state: () => Promise<WalletState>
balances?: () => Promise<{ unshielded: bigint; shielded: bigint }>
getBalance?: () => Promise<string>
signData?: (address: string, payload: string) => Promise<SignDataResult>
submitTx?: (tx: any) => Promise<TransactionResult>
[key: string]: any
}
SignDataResult
Result from signing data.
interface SignDataResult {
signature: string
key: string
}
TransactionResult
Result from submitting a transaction.
interface TransactionResult {
txHash: string
success: boolean
}
Component Props
MidnightConnectButtonProps
interface MidnightConnectButtonProps {
className?: string
connectText?: string
connectingText?: string
disconnectText?: string
onConnectSuccess?: () => void
onDisconnectSuccess?: () => void
variant?: "default" | "outline" | "ghost"
size?: "sm" | "md" | "lg"
}
MidnightWalletInfoProps
interface MidnightWalletInfoProps {
className?: string
showBalance?: boolean
showProvider?: boolean
addressFormat?: "short" | "full"
variant?: "card" | "compact" | "inline"
}
MidnightSessionTimerProps
interface MidnightSessionTimerProps {
className?: string
showRefreshButton?: boolean
autoRefreshThreshold?: number
variant?: "default" | "compact" | "minimal"
}
ProtectedRouteProps
interface ProtectedRouteProps {
children: ReactNode
fallback?: ReactNode
}
Provider & Context Types
MidnightAuthProviderProps
interface MidnightAuthProviderProps {
children: React.ReactNode
sessionTimeout?: number
autoConnect?: boolean
onConnect?: (walletState: WalletState) => void
onDisconnect?: () => void
onError?: (error: Error) => void
}
MidnightAuthContextValue
interface MidnightAuthContextValue {
// Connection state
isConnected: boolean
isConnecting: boolean
walletState: WalletState | null
error: string | null
// Session state
session: Session | null
// Methods
connect: () => Promise<void>
disconnect: () => void
clearError: () => void
// Session methods
refreshSession: () => Promise<void>
updateSessionMetadata: (metadata: Record<string, any>) => void
// Wallet API access
getWalletAPI: () => any | null
// Transaction methods
signData?: (address: string, payload: string) => Promise<SignDataResult>
submitTransaction?: (tx: any) => Promise<TransactionResult>
}
Using Types in Your Project
Type-safe Component
import { useMidnightWallet } from '@uppzen/midnight-auth'
import type { WalletState } from '@uppzen/midnight-auth'
export default function WalletDisplay() {
const { walletState } = useMidnightWallet()
const displayWallet = (state: WalletState | null) => {
if (!state) return 'No wallet connected'
return state.address
}
return <div>{displayWallet(walletState)}</div>
}
Type-safe Callbacks
import { MidnightAuthProvider } from '@uppzen/midnight-auth'
import type { WalletState } from '@uppzen/midnight-auth'
const handleConnect = (walletState: WalletState) => {
console.log('Connected to:', walletState.address)
}
export default function App({ children }: { children: React.ReactNode }) {
return (
<MidnightAuthProvider onConnect={handleConnect}>
{children}
</MidnightAuthProvider>
)
}
Type-safe Transaction Handling
import { useMidnightWallet } from '@uppzen/midnight-auth'
import type { TransactionResult } from '@uppzen/midnight-auth'
export default function TransactionHandler() {
const { submitTransaction } = useMidnightWallet()
const handleTransaction = async (): Promise<TransactionResult | null> => {
if (!submitTransaction) return null
try {
const result = await submitTransaction({ /* tx data */ })
return result
} catch (error) {
console.error(error)
return null
}
}
return <button onClick={handleTransaction}>Submit</button>
}
Type Guards
Create type guards for runtime type checking:
import type { WalletState, BalanceBreakdown } from '@uppzen/midnight-auth'
function hasBalances(state: WalletState): state is WalletState & { balances: BalanceBreakdown } {
return state.balances !== undefined
}
// Usage
if (hasBalances(walletState)) {
console.log(walletState.balances.total) // Type-safe!
}
Next Steps
- Check the API Reference for detailed documentation
- Browse Examples for real-world usage patterns
- Learn about Hooks for programmatic access