From d473ddb8235ec7cfc82439e311c9bd212a986134 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Wed, 16 Apr 2025 21:22:18 +0530 Subject: [PATCH] chore: remove zerokit and use light by default --- src/app/layout.tsx | 28 ++- src/components/RLNImplementationToggle.tsx | 40 ---- src/components/RLNStatusIndicator.tsx | 9 +- .../MembershipTab/MembershipRegistration.tsx | 18 +- src/contexts/index.ts | 2 +- src/contexts/rln/RLNContext.tsx | 78 ++----- src/contexts/rln/RLNImplementationContext.tsx | 30 --- src/contexts/rln/implementations/factory.tsx | 11 - src/contexts/rln/implementations/index.ts | 3 - src/contexts/rln/implementations/standard.tsx | 206 ------------------ src/contexts/rln/index.ts | 3 +- .../rln/{implementations => }/light.tsx | 2 +- .../rln/{implementations => }/types.ts | 0 13 files changed, 41 insertions(+), 389 deletions(-) delete mode 100644 src/components/RLNImplementationToggle.tsx delete mode 100644 src/contexts/rln/RLNImplementationContext.tsx delete mode 100644 src/contexts/rln/implementations/factory.tsx delete mode 100644 src/contexts/rln/implementations/index.ts delete mode 100644 src/contexts/rln/implementations/standard.tsx rename src/contexts/rln/{implementations => }/light.tsx (99%) rename src/contexts/rln/{implementations => }/types.ts (100%) diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 77076e5..cb65a98 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -7,7 +7,7 @@ import { Toaster } from "../components/ui/toaster"; import "@fontsource-variable/inter"; import "@fontsource-variable/jetbrains-mono"; -import { WalletProvider, RLNImplementationProvider, KeystoreProvider, RLNProvider } from "../contexts/index"; +import { WalletProvider, KeystoreProvider, RLNProvider } from "../contexts/index"; import { Header } from "../components/Header"; import { AppStateProvider } from "../contexts/AppStateContext"; import { Footer } from "@/components/Footer"; @@ -51,20 +51,18 @@ export default function RootLayout({ children }: RootLayoutProps) { > - - - -
-
-
- {children} -
-
-
- -
-
-
+ + +
+
+
+ {children} +
+
+
+ +
+
diff --git a/src/components/RLNImplementationToggle.tsx b/src/components/RLNImplementationToggle.tsx deleted file mode 100644 index b086da3..0000000 --- a/src/components/RLNImplementationToggle.tsx +++ /dev/null @@ -1,40 +0,0 @@ -"use client"; - -import { useRLNImplementation } from '../contexts'; -import React from 'react'; -import { ToggleGroup, ToggleGroupItem } from "./ui/toggle-group"; - -export function RLNImplementationToggle() { - const { implementation, setImplementation } = useRLNImplementation(); - - return ( -
- - - { - if (value) setImplementation(value as 'light' | 'standard'); - }} - className="w-full max-w-md" - > - - Light - - - Standard - - - -

- {implementation === 'light' - ? 'Light implementation, without Zerokit. Instant initialisation.' - : 'Standard implementation, with Zerokit. Initialisation takes 10-15 seconds for WASM module.' - } -

-
- ); -} \ No newline at end of file diff --git a/src/components/RLNStatusIndicator.tsx b/src/components/RLNStatusIndicator.tsx index 2f476ec..62f99a7 100644 --- a/src/components/RLNStatusIndicator.tsx +++ b/src/components/RLNStatusIndicator.tsx @@ -2,12 +2,10 @@ import React from 'react'; import { useRLN } from '../contexts'; -import { useRLNImplementation } from '../contexts'; import { useWallet } from '../contexts'; export function RLNStatusIndicator() { const { isInitialized, isStarted, isLoading, error } = useRLN(); - const { implementation } = useRLNImplementation(); const { isConnected, chainId } = useWallet(); // Debug logging @@ -17,8 +15,7 @@ export function RLNStatusIndicator() { isInitialized, isStarted, isLoading, - error, - implementation + error }); const getStatusColor = () => { @@ -37,10 +34,10 @@ export function RLNStatusIndicator() { const getStatusText = () => { if (error) return 'Error'; - if (isLoading) return `Initializing ${implementation} RLN...`; + if (isLoading) return 'Initializing RLN...'; if (!isConnected) return 'Connect Wallet'; if (chainId !== 59141) return 'Switch to Linea Sepolia'; - if (isInitialized && isStarted) return `RLN Active`; + if (isInitialized && isStarted) return 'RLN Active'; return 'RLN Inactive'; }; diff --git a/src/components/Tabs/MembershipTab/MembershipRegistration.tsx b/src/components/Tabs/MembershipTab/MembershipRegistration.tsx index 6b45f6b..a564d5b 100644 --- a/src/components/Tabs/MembershipTab/MembershipRegistration.tsx +++ b/src/components/Tabs/MembershipTab/MembershipRegistration.tsx @@ -1,13 +1,10 @@ "use client"; import React, { useState, useEffect } from 'react'; -import { RLNImplementationToggle } from '../../RLNImplementationToggle'; import { RLNStatusIndicator } from '../../RLNStatusIndicator'; import { KeystoreEntity } from '@waku/rln'; import { useRLN } from '../../../contexts/rln/RLNContext'; import { useWallet } from '../../../contexts/wallet'; -import { useRLNImplementation } from '../../../contexts'; -import { RLNInitButton } from '../../RLNinitButton'; import { TerminalWindow } from '../../ui/terminal-window'; import { Slider } from '../../ui/slider'; import { Button } from '../../ui/button'; @@ -17,7 +14,6 @@ import { toast } from 'sonner'; export function MembershipRegistration() { const { registerMembership, isInitialized, isStarted, rateMinLimit, rateMaxLimit, error, isLoading } = useRLN(); const { isConnected, chainId } = useWallet(); - const { implementation } = useRLNImplementation(); const [rateLimit, setRateLimit] = useState(rateMinLimit); const [isRegistering, setIsRegistering] = useState(false); @@ -110,10 +106,6 @@ export function MembershipRegistration() {
-
- -
- {/* Network Warning */} {isConnected && !isLineaSepolia && (
@@ -125,7 +117,7 @@ export function MembershipRegistration() { )} {/* Informational Box */} -
+
{">"}

@@ -160,12 +152,6 @@ export function MembershipRegistration() {

- {implementation === 'standard' && !isInitialized && !isStarted && ( -
- -
- )} - {!isConnected ? (
ℹ️ @@ -174,7 +160,7 @@ export function MembershipRegistration() { ) : !isInitialized || !isStarted ? (
ℹ️ - {implementation === 'light' && isLoading ? 'Initializing Light RLN...' : membershipRegistration.initializePrompt} + {isLoading ? 'Initializing RLN...' : membershipRegistration.initializePrompt}
) : (
diff --git a/src/contexts/index.ts b/src/contexts/index.ts index 06d17fc..cd3ac0c 100644 --- a/src/contexts/index.ts +++ b/src/contexts/index.ts @@ -1,3 +1,3 @@ export { WalletProvider, useWallet } from './wallet'; export { KeystoreProvider, useKeystore } from './keystore'; -export { RLNImplementationProvider, useRLNImplementation, type RLNImplementationType, RLNProvider, useRLN } from './rln'; +export { RLNProvider, useRLN } from './rln'; diff --git a/src/contexts/rln/RLNContext.tsx b/src/contexts/rln/RLNContext.tsx index 6f41a4e..1921dd6 100644 --- a/src/contexts/rln/RLNContext.tsx +++ b/src/contexts/rln/RLNContext.tsx @@ -2,8 +2,6 @@ import { createContext, useContext, useState, useEffect, ReactNode, useCallback } from 'react'; import { KeystoreEntity, MembershipInfo, RLNCredentialsManager } from '@waku/rln'; -import { createRLNImplementation } from './implementations'; -import { useRLNImplementation } from './RLNImplementationContext'; import { ethers } from 'ethers'; import { useKeystore } from '../keystore'; import { ERC20_ABI, LINEA_SEPOLIA_CONFIG, ensureLineaSepoliaNetwork } from '../../utils/network'; @@ -40,7 +38,6 @@ interface RLNContextType { const RLNContext = createContext(undefined); export function RLNProvider({ children }: { children: ReactNode }) { - const { implementation } = useRLNImplementation(); const [rln, setRln] = useState(null); const [isInitialized, setIsInitialized] = useState(false); const [isStarted, setIsStarted] = useState(false); @@ -96,20 +93,6 @@ export function RLNProvider({ children }: { children: ReactNode }) { }; }, []); - // Reset RLN state when implementation changes - useEffect(() => { - console.log('Implementation changed, resetting state:', { - oldRln: !!rln, - oldIsInitialized: isInitialized, - oldIsStarted: isStarted, - newImplementation: implementation - }); - setRln(null); - setIsInitialized(false); - setIsStarted(false); - setError(null); - }, [implementation]); - const initializeRLN = useCallback(async () => { console.log("InitializeRLN called. Connected:", isConnected, "Signer available:", !!signer); @@ -117,19 +100,16 @@ export function RLNProvider({ children }: { children: ReactNode }) { setError(null); setIsLoading(true); - let rlnInstance = rln; + const rlnInstance = rln; if (!rlnInstance) { - console.log(`Creating RLN ${implementation} instance...`); - + console.log("Creating RLN light instance..."); try { - rlnInstance = await createRLNImplementation(implementation); - console.log("RLN instance created successfully:", !!rlnInstance); setRln(rlnInstance); - setIsInitialized(true); - console.log("isInitialized set to true"); + setIsStarted(true); + console.log("isInitialized and isStarted set to true"); } catch (createErr) { console.error("Error creating RLN instance:", createErr); throw createErr; @@ -137,39 +117,22 @@ export function RLNProvider({ children }: { children: ReactNode }) { } else { console.log("RLN instance already exists, skipping creation"); } - - if (isConnected && signer && rlnInstance && !isStarted) { - console.log("Starting RLN with signer..."); - try { - await rlnInstance.start({ signer }); - - setIsStarted(true); - console.log("RLN started successfully, isStarted set to true"); - try { - const minLimit = await rlnInstance.contract?.getMinRateLimit(); - const maxLimit = await rlnInstance.contract?.getMaxRateLimit(); - if (minLimit !== undefined && maxLimit !== undefined) { - setRateMinLimit(minLimit); - setRateMaxLimit(maxLimit); - console.log("Rate limits fetched:", { min: minLimit, max: maxLimit }); - } else { - throw new Error("Rate limits not available"); - } - } catch (limitErr) { - console.warn("Could not fetch rate limits:", limitErr); + // Fetch rate limits if available + if (rlnInstance && rlnInstance.contract) { + try { + const minLimit = await rlnInstance.contract.getMinRateLimit(); + const maxLimit = await rlnInstance.contract.getMaxRateLimit(); + if (minLimit !== undefined && maxLimit !== undefined) { + setRateMinLimit(minLimit); + setRateMaxLimit(maxLimit); + console.log("Rate limits fetched:", { min: minLimit, max: maxLimit }); + } else { + throw new Error("Rate limits not available"); } - } catch (startErr) { - console.error("Error starting RLN:", startErr); - throw startErr; + } catch (limitErr) { + console.warn("Could not fetch rate limits:", limitErr); } - } else { - console.log("Skipping RLN start because:", { - isConnected, - hasSigner: !!signer, - hasRln: !!rlnInstance, - isAlreadyStarted: isStarted - }); } } catch (err) { console.error('Error in initializeRLN:', err); @@ -177,23 +140,22 @@ export function RLNProvider({ children }: { children: ReactNode }) { } finally { setIsLoading(false); } - }, [isConnected, signer, implementation, rln, isStarted]); + }, [isConnected, signer, rln, isStarted]); // Auto-initialize effect for Light implementation useEffect(() => { console.log('Auto-init check:', { - implementation, isConnected, hasSigner: !!signer, isInitialized, isStarted, isLoading }); - if (implementation === 'light' && isConnected && signer && !isInitialized && !isStarted && !isLoading) { + if (isConnected && signer && !isInitialized && !isStarted && !isLoading) { console.log('Auto-initializing Light RLN implementation...'); initializeRLN(); } - }, [implementation, isConnected, signer, isInitialized, isStarted, isLoading, initializeRLN]); + }, [isConnected, signer, isInitialized, isStarted, isLoading, initializeRLN]); const getCurrentRateLimit = async (): Promise => { try { diff --git a/src/contexts/rln/RLNImplementationContext.tsx b/src/contexts/rln/RLNImplementationContext.tsx deleted file mode 100644 index 222181f..0000000 --- a/src/contexts/rln/RLNImplementationContext.tsx +++ /dev/null @@ -1,30 +0,0 @@ -"use client"; - -import { createContext, useContext, useState, ReactNode } from 'react'; - -export type RLNImplementationType = 'standard' | 'light'; - -interface RLNImplementationContextType { - implementation: RLNImplementationType; - setImplementation: (implementation: RLNImplementationType) => void; -} - -const RLNImplementationContext = createContext(undefined); - -export function RLNImplementationProvider({ children }: { children: ReactNode }) { - const [implementation, setImplementation] = useState('light'); - - return ( - - {children} - - ); -} - -export function useRLNImplementation() { - const context = useContext(RLNImplementationContext); - if (context === undefined) { - throw new Error('useRLNImplementation must be used within a RLNImplementationProvider'); - } - return context; -} \ No newline at end of file diff --git a/src/contexts/rln/implementations/factory.tsx b/src/contexts/rln/implementations/factory.tsx deleted file mode 100644 index 7865eda..0000000 --- a/src/contexts/rln/implementations/factory.tsx +++ /dev/null @@ -1,11 +0,0 @@ -"use client"; - -import { createRLN, RLNCredentialsManager } from '@waku/rln'; - -export async function createRLNImplementation(type: 'standard' | 'light' = 'light') { - if (type === 'standard') { - return await createRLN(); - } else { - return new RLNCredentialsManager; - } -} \ No newline at end of file diff --git a/src/contexts/rln/implementations/index.ts b/src/contexts/rln/implementations/index.ts deleted file mode 100644 index a2dc555..0000000 --- a/src/contexts/rln/implementations/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { RLNProvider as StandardRLNProvider, useRLN as useStandardRLN } from './standard'; -export { RLNProvider as LightRLNProvider, useRLN as useLightRLN } from './light'; -export { createRLNImplementation } from './factory'; \ No newline at end of file diff --git a/src/contexts/rln/implementations/standard.tsx b/src/contexts/rln/implementations/standard.tsx deleted file mode 100644 index 70367e4..0000000 --- a/src/contexts/rln/implementations/standard.tsx +++ /dev/null @@ -1,206 +0,0 @@ -"use client"; - -import { createContext, useContext, useState, ReactNode } from 'react'; -import { createRLN, LINEA_CONTRACT, RLNInstance } from '@waku/rln'; -import { ethers } from 'ethers'; -import { ensureLineaSepoliaNetwork, ERC20_ABI, SIGNATURE_MESSAGE } from '../../../utils/network'; -import { RLNContextType } from './types'; -import { useWallet } from '@/contexts'; - - - -const RLNContext = createContext(undefined); - -export function RLNProvider({ children }: { children: ReactNode }) { - const { isConnected, signer } = useWallet(); - const [rln, setRln] = useState(null); - const [isInitialized, setIsInitialized] = useState(false); - const [isStarted, setIsStarted] = useState(false); - const [error, setError] = useState(null); - const [rateMinLimit, setRateMinLimit] = useState(0); - const [rateMaxLimit, setRateMaxLimit] = useState(0); - - const initializeRLN = async () => { - console.log("InitializeRLN called. Connected:", isConnected, "Signer available:", !!signer); - - try { - setError(null); - - if (!rln) { - console.log("Creating RLN instance..."); - - try { - const rlnInstance = await createRLN(); - - console.log("RLN instance created successfully:", !!rlnInstance); - setRln(rlnInstance); - - setIsInitialized(true); - console.log("isInitialized set to true"); - } catch (createErr) { - console.error("Error creating RLN instance:", createErr); - throw createErr; - } - } else { - console.log("RLN instance already exists, skipping creation"); - } - - if (isConnected && signer && rln && !isStarted) { - console.log("Starting RLN with signer..."); - try { - await rln.start({ signer }); - - setIsStarted(true); - console.log("RLN started successfully, isStarted set to true"); - - const minRate = await rln.contract?.getMinRateLimit(); - const maxRate = await rln.contract?.getMaxRateLimit(); - setRateMinLimit(minRate || 0); - setRateMaxLimit(maxRate || 0); - console.log("Min rate:", minRate); - console.log("Max rate:", maxRate); - } catch (startErr) { - console.error("Error starting RLN:", startErr); - throw startErr; - } - } else { - console.log("Skipping RLN start because:", { - isConnected, - hasSigner: !!signer, - hasRln: !!rln, - isAlreadyStarted: isStarted - }); - } - } catch (err) { - console.error('Error in initializeRLN:', err); - setError(err instanceof Error ? err.message : 'Failed to initialize RLN'); - } - }; - - const registerMembership = async (rateLimit: number) => { - console.log("registerMembership called with rate limit:", rateLimit); - - if (!rln || !isStarted) { - return { success: false, error: 'RLN not initialized or not started' }; - } - - if (!signer) { - return { success: false, error: 'No signer available' }; - } - - try { - // Validate rate limit - if (rateLimit < rateMinLimit || rateLimit > rateMaxLimit) { - return { - success: false, - error: `Rate limit must be between ${rateMinLimit} and ${rateMaxLimit}` - }; - } - rln.contract?.setRateLimit(rateLimit); - - // Ensure we're on the correct network - const isOnLineaSepolia = await ensureLineaSepoliaNetwork(signer); - if (!isOnLineaSepolia) { - console.warn("Could not switch to Linea Sepolia network. Registration may fail."); - } - - // Get user address and contract address - const userAddress = await signer.getAddress(); - - if (!rln.contract || !rln.contract.address) { - return { success: false, error: "RLN contract address not available. Cannot proceed with registration." }; - } - - const contractAddress = rln.contract.address; - const tokenAddress = LINEA_CONTRACT.address; - - // Create token contract instance - const tokenContract = new ethers.Contract( - tokenAddress, - ERC20_ABI, - signer - ); - - // Check token balance - const tokenBalance = await tokenContract.balanceOf(userAddress); - if (tokenBalance.isZero()) { - return { success: false, error: "You need tokens to register a membership. Your token balance is zero." }; - } - - // Check and approve token allowance if needed - const currentAllowance = await tokenContract.allowance(userAddress, contractAddress); - if (currentAllowance.eq(0)) { - console.log("Requesting token approval..."); - - // Approve a large amount (max uint256) - const maxUint256 = ethers.constants.MaxUint256; - - try { - const approveTx = await tokenContract.approve(contractAddress, maxUint256); - console.log("Approval transaction submitted:", approveTx.hash); - - // Wait for the transaction to be mined - await approveTx.wait(1); - console.log("Token approval confirmed"); - } catch (approvalErr) { - console.error("Error during token approval:", approvalErr); - return { - success: false, - error: `Failed to approve token: ${approvalErr instanceof Error ? approvalErr.message : String(approvalErr)}` - }; - } - } else { - console.log("Token allowance already sufficient"); - } - - // Generate signature for identity - const message = `${SIGNATURE_MESSAGE} ${Date.now()}`; - const signature = await signer.signMessage(message); - - const _credentials = await rln.registerMembership({signature: signature}); - if (!_credentials) { - throw new Error("Failed to register membership: No credentials returned"); - } - if (!_credentials.identity) { - throw new Error("Failed to register membership: Missing identity information"); - } - if (!_credentials.membership) { - throw new Error("Failed to register membership: Missing membership information"); - } - - return { success: true, credentials: _credentials }; - } catch (err) { - let errorMsg = "Failed to register membership"; - if (err instanceof Error) { - errorMsg = err.message; - } - - return { success: false, error: errorMsg }; - } - }; - - return ( - - {children} - - ); -} - -export function useRLN() { - const context = useContext(RLNContext); - if (context === undefined) { - throw new Error('useRLN must be used within a RLNProvider'); - } - return context; -} \ No newline at end of file diff --git a/src/contexts/rln/index.ts b/src/contexts/rln/index.ts index f59277d..2aae22e 100644 --- a/src/contexts/rln/index.ts +++ b/src/contexts/rln/index.ts @@ -1,2 +1 @@ -export { RLNProvider, useRLN } from './RLNContext'; -export { RLNImplementationProvider, useRLNImplementation, type RLNImplementationType } from './RLNImplementationContext'; \ No newline at end of file +export { RLNProvider, useRLN } from './RLNContext'; \ No newline at end of file diff --git a/src/contexts/rln/implementations/light.tsx b/src/contexts/rln/light.tsx similarity index 99% rename from src/contexts/rln/implementations/light.tsx rename to src/contexts/rln/light.tsx index 88a8c94..75000cf 100644 --- a/src/contexts/rln/implementations/light.tsx +++ b/src/contexts/rln/light.tsx @@ -3,7 +3,7 @@ import { createContext, useContext, useState, useEffect, ReactNode, useCallback } from 'react'; import { RLNCredentialsManager } from '@waku/rln'; import { ethers } from 'ethers'; -import { ensureLineaSepoliaNetwork, ERC20_ABI, SIGNATURE_MESSAGE } from '../../../utils/network'; +import { ensureLineaSepoliaNetwork, ERC20_ABI, SIGNATURE_MESSAGE } from '../../utils/network'; import { useWallet } from '@/contexts'; import { RLNContextType } from './types'; diff --git a/src/contexts/rln/implementations/types.ts b/src/contexts/rln/types.ts similarity index 100% rename from src/contexts/rln/implementations/types.ts rename to src/contexts/rln/types.ts