mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-04-22 05:37:56 +00:00
7b95b8a409
This commit reorganizes the codebase to eliminate duplication between web and mobile frontends by moving all commonly used files to the shared folder. Changes: - Moved lib files to shared/lib/: * wallet.ts, staking.ts, tiki.ts, identity.ts * multisig.ts, usdt.ts, scores.ts, citizenship-workflow.ts - Moved utils to shared/utils/: * auth.ts, dex.ts * Created format.ts (extracted formatNumber from web utils) - Created shared/theme/: * colors.ts (Kurdistan and App color definitions) - Updated web configuration: * Added @pezkuwi/* path aliases in tsconfig.json and vite.config.ts * Updated all imports to use @pezkuwi/lib/*, @pezkuwi/utils/*, @pezkuwi/theme/* * Removed duplicate files from web/src/lib and web/src/utils - Updated mobile configuration: * Added @pezkuwi/* path aliases in tsconfig.json * Updated theme/colors.ts to re-export from shared * Mobile already uses relative imports to shared (no changes needed) Architecture Benefits: - Single source of truth for common code - No duplication between frontends - Easier maintenance and consistency - Clear separation of shared vs platform-specific code Web-specific files kept: - web/src/lib/supabase.ts - web/src/lib/utils.ts (cn function for Tailwind, re-exports formatNumber from shared) All imports updated and tested. Both web and mobile now use the centralized shared folder.
161 lines
4.4 KiB
TypeScript
161 lines
4.4 KiB
TypeScript
import React, { createContext, useContext, useState, useEffect } from 'react';
|
|
import { useWallet } from './WalletContext';
|
|
import {
|
|
IdentityProfile,
|
|
KYCData,
|
|
Badge,
|
|
Role,
|
|
calculateReputationScore,
|
|
generateZKProof,
|
|
DEFAULT_BADGES,
|
|
ROLES
|
|
} from '@pezkuwi/lib/identity';
|
|
|
|
interface IdentityContextType {
|
|
profile: IdentityProfile | null;
|
|
isVerifying: boolean;
|
|
startKYC: (data: KYCData) => Promise<void>;
|
|
updatePrivacySettings: (settings: any) => void;
|
|
addBadge: (badge: Badge) => void;
|
|
assignRole: (role: Role) => void;
|
|
refreshReputation: () => void;
|
|
}
|
|
|
|
const IdentityContext = createContext<IdentityContextType | undefined>(undefined);
|
|
|
|
export function IdentityProvider({ children }: { children: React.ReactNode }) {
|
|
const { account } = useWallet();
|
|
const [profile, setProfile] = useState<IdentityProfile | null>(null);
|
|
const [isVerifying, setIsVerifying] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (account) {
|
|
// Load or create profile for connected wallet
|
|
const storedProfile = localStorage.getItem(`identity_${account}`);
|
|
if (storedProfile) {
|
|
setProfile(JSON.parse(storedProfile));
|
|
} else {
|
|
// Create new profile
|
|
const newProfile: IdentityProfile = {
|
|
address: account,
|
|
verificationLevel: 'none',
|
|
kycStatus: 'none',
|
|
reputationScore: 0,
|
|
badges: [],
|
|
roles: [],
|
|
privacySettings: {
|
|
showRealName: false,
|
|
showEmail: false,
|
|
showCountry: true,
|
|
useZKProof: true
|
|
}
|
|
};
|
|
setProfile(newProfile);
|
|
localStorage.setItem(`identity_${account}`, JSON.stringify(newProfile));
|
|
}
|
|
} else {
|
|
setProfile(null);
|
|
}
|
|
}, [account]);
|
|
|
|
const startKYC = async (data: KYCData) => {
|
|
if (!profile) return;
|
|
|
|
setIsVerifying(true);
|
|
try {
|
|
// Simulate KYC verification process
|
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
|
|
const zkProof = generateZKProof(data);
|
|
|
|
const updatedProfile: IdentityProfile = {
|
|
...profile,
|
|
kycStatus: 'approved',
|
|
verificationLevel: data.documentType ? 'verified' : 'basic',
|
|
verificationDate: new Date(),
|
|
badges: [...profile.badges, ...DEFAULT_BADGES],
|
|
roles: [ROLES.verified_user as Role],
|
|
reputationScore: calculateReputationScore(
|
|
[],
|
|
data.documentType ? 'verified' : 'basic',
|
|
[...profile.badges, ...DEFAULT_BADGES]
|
|
)
|
|
};
|
|
|
|
setProfile(updatedProfile);
|
|
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
|
|
} catch (error) {
|
|
console.error('KYC verification failed:', error);
|
|
} finally {
|
|
setIsVerifying(false);
|
|
}
|
|
};
|
|
|
|
const updatePrivacySettings = (settings: any) => {
|
|
if (!profile) return;
|
|
|
|
const updatedProfile = {
|
|
...profile,
|
|
privacySettings: { ...profile.privacySettings, ...settings }
|
|
};
|
|
|
|
setProfile(updatedProfile);
|
|
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
|
|
};
|
|
|
|
const addBadge = (badge: Badge) => {
|
|
if (!profile) return;
|
|
|
|
const updatedProfile = {
|
|
...profile,
|
|
badges: [...profile.badges, badge]
|
|
};
|
|
|
|
setProfile(updatedProfile);
|
|
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
|
|
};
|
|
|
|
const assignRole = (role: Role) => {
|
|
if (!profile) return;
|
|
|
|
const updatedProfile = {
|
|
...profile,
|
|
roles: [...profile.roles, role]
|
|
};
|
|
|
|
setProfile(updatedProfile);
|
|
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
|
|
};
|
|
|
|
const refreshReputation = () => {
|
|
if (!profile) return;
|
|
|
|
const newScore = calculateReputationScore([], profile.verificationLevel, profile.badges);
|
|
const updatedProfile = { ...profile, reputationScore: newScore };
|
|
|
|
setProfile(updatedProfile);
|
|
localStorage.setItem(`identity_${profile.address}`, JSON.stringify(updatedProfile));
|
|
};
|
|
|
|
return (
|
|
<IdentityContext.Provider value={{
|
|
profile,
|
|
isVerifying,
|
|
startKYC,
|
|
updatePrivacySettings,
|
|
addBadge,
|
|
assignRole,
|
|
refreshReputation
|
|
}}>
|
|
{children}
|
|
</IdentityContext.Provider>
|
|
);
|
|
}
|
|
|
|
export const useIdentity = () => {
|
|
const context = useContext(IdentityContext);
|
|
if (!context) {
|
|
throw new Error('useIdentity must be used within IdentityProvider');
|
|
}
|
|
return context;
|
|
}; |