-
Notifications
You must be signed in to change notification settings - Fork 40
signing struc #189
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
Merged
Merged
signing struc #189
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,249 @@ | ||
| # Wallet Authentication System | ||
|
|
||
| ## Overview | ||
|
|
||
| The Wallet Authentication System provides automatic sign-in verification after wallet connection, ensuring users verify wallet ownership before accessing sensitive information. | ||
|
|
||
| ## Features | ||
|
|
||
| - β **Automatic Popup**: Appears automatically after wallet connection | ||
| - β **Wallet Signature Verification**: Uses typed data signing for secure verification | ||
| - β **Session Persistence**: Authentication persists for the current session | ||
| - β **Auto Reset on Disconnect**: Clears authentication when wallet disconnects | ||
| - β **Theme Support**: Fully integrated with dark/light mode | ||
| - β **Mobile Responsive**: Works seamlessly across all devices | ||
|
|
||
| ## How It Works | ||
|
|
||
| ### 1. User Flow | ||
|
|
||
| 1. User connects wallet via Starknetkit modal | ||
| 2. **Automatic popup appears** prompting user to sign in | ||
| 3. User clicks "Sign Message" to verify wallet ownership | ||
| 4. Wallet prompts user to sign a message (no gas fees) | ||
| 5. Upon successful signature, user is authenticated | ||
| 6. Authentication persists for the session | ||
| 7. When wallet disconnects, authentication resets | ||
|
|
||
| ### 2. Architecture | ||
|
|
||
| #### Components | ||
|
|
||
| - **`WalletAuthProvider`**: Context provider managing authentication state | ||
| - **`WalletSignInModal`**: The popup modal for signature verification | ||
| - **`ProtectedContent`**: Wrapper component for sensitive content | ||
| - **`useWalletAuth`**: Hook to access authentication state and functions | ||
| - **`useRequireAuth`**: Hook to require authentication in components | ||
|
|
||
| #### File Structure | ||
|
|
||
| ``` | ||
| frontend/ | ||
| βββ app/ | ||
| β βββ context/ | ||
| β β βββ wallet-auth-context.tsx # Authentication context | ||
| β βββ components/ | ||
| β β βββ modals/ | ||
| β β βββ WalletSignInModal.tsx # Sign-in modal component | ||
| β βββ layout.tsx # Provider integration | ||
| βββ components/ | ||
| βββ shared/ | ||
| βββ ProtectedContent.tsx # Protected content wrapper | ||
| βββ WalletConnected.tsx # Updated with auth clearing | ||
| ``` | ||
|
|
||
| ## Usage Examples | ||
|
|
||
| ### 1. Protecting Sensitive Content | ||
|
|
||
| Use the `ProtectedContent` component to wrap any sensitive information: | ||
|
|
||
| ```tsx | ||
| import ProtectedContent from '@/components/shared/ProtectedContent' | ||
|
|
||
| function UserSettings() { | ||
| return ( | ||
| <ProtectedContent fallbackMessage="Sign in to view your settings"> | ||
| <div> | ||
| <h2>Private Settings</h2> | ||
| <p>Your sensitive data here...</p> | ||
| </div> | ||
| </ProtectedContent> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Checking Authentication Status | ||
|
|
||
| Use the `useWalletAuth` hook to check authentication status: | ||
|
|
||
| ```tsx | ||
| import { useWalletAuth } from '@/app/context/wallet-auth-context' | ||
|
|
||
| function MyComponent() { | ||
| const { isAuthenticated } = useWalletAuth() | ||
|
|
||
| return ( | ||
| <div> | ||
| {isAuthenticated ? ( | ||
| <div>Welcome! You are authenticated.</div> | ||
| ) : ( | ||
| <div>Please authenticate to continue.</div> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ### 3. Requiring Authentication Before Action | ||
|
|
||
| Use the `useRequireAuth` hook to require authentication before performing actions: | ||
|
|
||
| ```tsx | ||
| import { useRequireAuth } from '@/components/shared/ProtectedContent' | ||
|
|
||
| function TransactionComponent() { | ||
| const { requireAuth } = useRequireAuth() | ||
|
|
||
| const handleTransaction = () => { | ||
| // Check if user is authenticated before proceeding | ||
| if (!requireAuth()) { | ||
| return // Will show auth modal | ||
| } | ||
|
|
||
| // Proceed with transaction | ||
| executeSensitiveOperation() | ||
| } | ||
|
|
||
| return <button onClick={handleTransaction}>Execute Transaction</button> | ||
| } | ||
| ``` | ||
|
|
||
| ### 4. Manually Triggering Auth Modal | ||
|
|
||
| ```tsx | ||
| import { useWalletAuth } from '@/app/context/wallet-auth-context' | ||
|
|
||
| function CustomComponent() { | ||
| const { setIsAuthModalOpen } = useWalletAuth() | ||
|
|
||
| return <button onClick={() => setIsAuthModalOpen(true)}>Verify Wallet</button> | ||
| } | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### `useWalletAuth()` Hook | ||
|
|
||
| Returns an object with: | ||
|
|
||
| - **`isAuthenticated`** (boolean): Whether the user is authenticated | ||
| - **`isAuthModalOpen`** (boolean): Whether the auth modal is open | ||
| - **`setIsAuthModalOpen`** (function): Function to open/close the modal | ||
| - **`authenticate`** (function): Function to mark user as authenticated | ||
| - **`logout`** (function): Function to disconnect wallet and clear auth | ||
| - **`needsAuthentication`** (boolean): Whether user needs to authenticate | ||
|
|
||
| ### `useRequireAuth()` Hook | ||
|
|
||
| Returns an object with: | ||
|
|
||
| - **`isAuthenticated`** (boolean): Whether the user is authenticated | ||
| - **`requireAuth`** (function): Function that returns true if authenticated, opens modal if not | ||
|
|
||
| ### `ProtectedContent` Component | ||
|
|
||
| Props: | ||
|
|
||
| - **`children`** (ReactNode): The sensitive content to protect | ||
| - **`fallbackMessage`** (string, optional): Custom message when not authenticated | ||
|
|
||
| ## Technical Details | ||
|
|
||
| ### Signature Process | ||
|
|
||
| The system uses Starknet typed data signing: | ||
|
|
||
| 1. Creates a typed data structure with: | ||
|
|
||
| - Domain: Spherre app identification | ||
| - Message: Sign-in text with wallet address and timestamp | ||
| - Types: StarkNet domain and message types | ||
|
|
||
| 2. Requests signature from user's wallet | ||
| 3. Validates signature exists (signature verification is done client-side) | ||
| 4. Marks user as authenticated in session storage | ||
|
|
||
| ### Session Storage | ||
|
|
||
| Authentication state is stored per wallet address: | ||
|
|
||
| - Key format: `wallet_auth_{address}` | ||
| - Storage: `sessionStorage` (cleared when browser tab closes) | ||
| - Automatic cleanup on disconnect | ||
|
|
||
| ### Auto-Trigger Logic | ||
|
|
||
| The modal automatically appears when: | ||
|
|
||
| 1. Wallet address is detected (connected) | ||
| 2. User is not already authenticated for that address | ||
| 3. Small 500ms delay after wallet connection for smooth UX | ||
|
|
||
| ## Security Considerations | ||
|
|
||
| β **Secure**: Uses cryptographic signature verification | ||
| β **Gas-free**: No blockchain transactions involved | ||
| β **Session-only**: Auth clears when session ends | ||
| β **Per-wallet**: Each wallet requires separate authentication | ||
| β **No private keys**: Never exposes or stores private keys | ||
|
|
||
| ## Testing Checklist | ||
|
|
||
| - [ ] Connect wallet β Modal appears automatically | ||
| - [ ] Sign message β Authentication succeeds | ||
| - [ ] Reject signature β Error message shown with retry option | ||
| - [ ] Authenticated β ProtectedContent renders children | ||
| - [ ] Not authenticated β ProtectedContent shows fallback | ||
| - [ ] Disconnect wallet β Authentication resets | ||
| - [ ] Reconnect same wallet β Modal appears again (new session) | ||
| - [ ] Dark/light mode β Modal theme updates correctly | ||
| - [ ] Mobile view β Modal is responsive and usable | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Modal doesn't appear after connection | ||
|
|
||
| - Check that `WalletAuthProvider` is in the provider tree | ||
| - Check browser console for errors | ||
| - Ensure `StarknetProvider` is above `WalletAuthProvider` | ||
|
|
||
| ### Authentication doesn't persist | ||
|
|
||
| - Check that sessionStorage is enabled in browser | ||
| - Verify wallet address is being captured correctly | ||
| - Check for console errors in `wallet-auth-context.tsx` | ||
|
|
||
| ### Modal appears too quickly after connection | ||
|
|
||
| - Adjust the 500ms delay in `wallet-auth-context.tsx` line 48 | ||
| - Increase timeout for slower wallet connection modals | ||
|
|
||
| ## Future Enhancements | ||
|
|
||
| Potential improvements: | ||
|
|
||
| - [ ] Backend signature verification | ||
| - [ ] JWT token generation for API authentication | ||
| - [ ] Remember device option (localStorage with expiry) | ||
| - [ ] Biometric authentication fallback | ||
| - [ ] Multi-signature support for shared accounts | ||
| - [ ] Rate limiting for signature requests | ||
|
|
||
| ## Support | ||
|
|
||
| For issues or questions: | ||
|
|
||
| - Check the troubleshooting section above | ||
| - Review the code comments in context files | ||
| - Test with a fresh browser session (incognito mode) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify backend verification status.
Line 236 lists "Backend signature verification" and "JWT token generation" as future enhancements, but
WALLET_AUTH_BACKEND_INTEGRATION.mddocuments that the backend already validates signatures via/api/v1/auth/signinand issues JWT tokens. Update this section to reflect the current implementation state or move fully-implemented features to a separate "Implemented Features" section.π€ Prompt for AI Agents