OneKey Architecture Overview
Platform Structure
apps/desktop/ - Electron desktop app (Windows, macOS, Linux)
apps/mobile/ - React Native mobile app (iOS, Android)
apps/ext/ - Browser extension (Chrome, Firefox, Edge, Brave)
apps/web/ - Progressive web application
apps/web-embed/ - Embeddable wallet components
Core Packages
packages/core/ - Blockchain protocol implementations, cryptography, hardware wallet communication
packages/kit/ - Application logic, state management, API integrations
packages/kit-bg/ - Background services and workers
packages/components/ - Tamagui-based cross-platform UI components
packages/shared/ - Platform abstractions, utilities, build configurations
packages/qr-wallet-sdk/ - Air-gapped wallet QR communication
Key Architectural Patterns
- Multi-chain support: 40+ blockchains with pluggable chain implementations
- Cross-platform UI: Tamagui for universal components with platform-specific adaptations
- Platform-specific files: Use
.native.ts, .desktop.ts, .web.ts, .ext.ts suffixes
- Hardware wallet integration: Custom
@onekeyfe/hd-* SDK packages
- State management: Jotai for atomic state management
Code Organization
File Naming Conventions
- Platform-specific implementations use suffixes:
.native.ts, .web.ts, .desktop.ts, .ext.ts
- Component files use PascalCase:
ComponentName.tsx
- Hook files use camelCase with
use prefix: useHookName.ts
- Utility files use camelCase:
utilityName.ts
Import Patterns
- Use workspace references:
@onekeyhq/components, @onekeyhq/core, @onekeyhq/kit
- Platform detection via
@onekeyhq/shared/src/platformEnv
- Conditional imports based on platform capabilities
Import Hierarchy Rules - STRICTLY ENFORCED
CRITICAL: Violating these rules WILL break the build and cause circular dependencies.
HIERARCHY (NEVER violate this order):
@onekeyhq/shared - FORBIDDEN to import from any other OneKey packages
@onekeyhq/components - ONLY allowed to import from shared
@onekeyhq/kit-bg - ONLY allowed to import from shared and core (NEVER components or kit)
@onekeyhq/kit - Can import from shared, components, and kit-bg
- Apps (desktop/mobile/ext/web) - Can import from all packages
BEFORE ADDING ANY IMPORT:
- Verify the import respects the hierarchy above
- Check if the import creates a circular dependency
- Run
yarn tsc:only to validate no circular dependency introduced
- If unsure, find an alternative approach that respects the hierarchy
COMMON VIOLATIONS TO AVOID:
- ❌ Importing from
@onekeyhq/kit in @onekeyhq/components
- ❌ Importing from
@onekeyhq/components in @onekeyhq/kit-bg
- ❌ Importing from
@onekeyhq/kit in @onekeyhq/core
- ❌ Any "upward" imports in the hierarchy
Component Structure
- UI components in
packages/components/src/
- Business logic in
packages/kit/src/
- Chain-specific code in
packages/core/src/chains/
Deep Analysis & Architecture Consistency Framework
Pre-Modification Analysis Protocol
MANDATORY ANALYSIS STEPS (Execute BEFORE any code changes):
Scope Impact Assessment
- Identify ALL packages/apps affected by the change
- Map dependencies that will be impacted (use
yarn why <package> if needed)
- Evaluate cross-platform implications (desktop/mobile/web/extension)
- Assess backward compatibility requirements
Pattern Consistency Verification
- Examine existing similar implementations in the codebase
- Identify established patterns and conventions used
- Verify new code follows identical patterns
- Check naming conventions align with existing code
Architecture Integrity Check
- Validate against monorepo import hierarchy rules
- Ensure separation of concerns is maintained
- Verify platform-specific code uses correct file extensions
- Check that business logic stays in appropriate packages
Performance Impact Evaluation
- Consider bundle size implications (especially for web/extension)
- Evaluate runtime performance effects
- Assess memory usage implications
- Consider impact on application startup time
Code Pattern Recognition Framework
WHEN ADDING NEW FUNCTIONALITY:
- Find Similar Examples: Search codebase for similar implementations
- Extract Patterns: Identify common approaches, naming, structure
- Follow Conventions: Mirror existing patterns exactly
- Validate Consistency: Ensure new code looks like existing code
WHEN MODIFYING EXISTING CODE:
- Understand Context: Read surrounding code and imports
- Preserve Patterns: Maintain existing architectural decisions
- Consistent Style: Match existing code style and structure
- Validate Integration: Ensure changes integrate seamlessly
Architecture Validation Checklist
BEFORE COMMITTING ANY CHANGES:
1---2name: 1k-architecture3description: OneKey monorepo architecture and code organization. Use when understanding project structure, package relationships, import rules, or component organization. Triggers on architecture, structure, packages, imports, hierarchy, dependencies, monorepo, organization.4---56# OneKey Architecture Overview78## Platform Structure9- **`apps/desktop/`** - Electron desktop app (Windows, macOS, Linux)10- **`apps/mobile/`** - React Native mobile app (iOS, Android)11- **`apps/ext/`** - Browser extension (Chrome, Firefox, Edge, Brave)12- **`apps/web/`** - Progressive web application13- **`apps/web-embed/`** - Embeddable wallet components1415## Core Packages16- **`packages/core/`** - Blockchain protocol implementations, cryptography, hardware wallet communication17- **`packages/kit/`** - Application logic, state management, API integrations18- **`packages/kit-bg/`** - Background services and workers19- **`packages/components/`** - Tamagui-based cross-platform UI components20- **`packages/shared/`** - Platform abstractions, utilities, build configurations21- **`packages/qr-wallet-sdk/`** - Air-gapped wallet QR communication2223## Key Architectural Patterns24- **Multi-chain support**: 40+ blockchains with pluggable chain implementations25- **Cross-platform UI**: Tamagui for universal components with platform-specific adaptations26- **Platform-specific files**: Use `.native.ts`, `.desktop.ts`, `.web.ts`, `.ext.ts` suffixes27- **Hardware wallet integration**: Custom `@onekeyfe/hd-*` SDK packages28- **State management**: Jotai for atomic state management2930## Code Organization3132### File Naming Conventions33- Platform-specific implementations use suffixes: `.native.ts`, `.web.ts`, `.desktop.ts`, `.ext.ts`34- Component files use PascalCase: `ComponentName.tsx`35- Hook files use camelCase with `use` prefix: `useHookName.ts`36- Utility files use camelCase: `utilityName.ts`3738### Import Patterns39- Use workspace references: `@onekeyhq/components`, `@onekeyhq/core`, `@onekeyhq/kit`40- Platform detection via `@onekeyhq/shared/src/platformEnv`41- Conditional imports based on platform capabilities4243### Import Hierarchy Rules - STRICTLY ENFORCED4445**CRITICAL**: Violating these rules WILL break the build and cause circular dependencies.4647**HIERARCHY (NEVER violate this order):**48- `@onekeyhq/shared` - **FORBIDDEN** to import from any other OneKey packages49- `@onekeyhq/components` - **ONLY** allowed to import from `shared`50- `@onekeyhq/kit-bg` - **ONLY** allowed to import from `shared` and `core` (NEVER `components` or `kit`)51- `@onekeyhq/kit` - Can import from `shared`, `components`, and `kit-bg`52- Apps (desktop/mobile/ext/web) - Can import from all packages5354**BEFORE ADDING ANY IMPORT:**551. Verify the import respects the hierarchy above562. Check if the import creates a circular dependency573. Run `yarn tsc:only` to validate no circular dependency introduced584. If unsure, find an alternative approach that respects the hierarchy5960**COMMON VIOLATIONS TO AVOID:**61- ❌ Importing from `@onekeyhq/kit` in `@onekeyhq/components`62- ❌ Importing from `@onekeyhq/components` in `@onekeyhq/kit-bg`63- ❌ Importing from `@onekeyhq/kit` in `@onekeyhq/core`64- ❌ Any "upward" imports in the hierarchy6566### Component Structure67- UI components in `packages/components/src/`68- Business logic in `packages/kit/src/`69- Chain-specific code in `packages/core/src/chains/`7071## Deep Analysis & Architecture Consistency Framework7273### Pre-Modification Analysis Protocol7475**MANDATORY ANALYSIS STEPS** (Execute BEFORE any code changes):76771. **Scope Impact Assessment**78 - Identify ALL packages/apps affected by the change79 - Map dependencies that will be impacted (use `yarn why <package>` if needed)80 - Evaluate cross-platform implications (desktop/mobile/web/extension)81 - Assess backward compatibility requirements82832. **Pattern Consistency Verification**84 - Examine existing similar implementations in the codebase85 - Identify established patterns and conventions used86 - Verify new code follows identical patterns87 - Check naming conventions align with existing code88893. **Architecture Integrity Check**90 - Validate against monorepo import hierarchy rules91 - Ensure separation of concerns is maintained92 - Verify platform-specific code uses correct file extensions93 - Check that business logic stays in appropriate packages94954. **Performance Impact Evaluation**96 - Consider bundle size implications (especially for web/extension)97 - Evaluate runtime performance effects98 - Assess memory usage implications99 - Consider impact on application startup time100101### Code Pattern Recognition Framework102103**WHEN ADDING NEW FUNCTIONALITY:**1041. **Find Similar Examples**: Search codebase for similar implementations1052. **Extract Patterns**: Identify common approaches, naming, structure1063. **Follow Conventions**: Mirror existing patterns exactly1074. **Validate Consistency**: Ensure new code looks like existing code108109**WHEN MODIFYING EXISTING CODE:**1101. **Understand Context**: Read surrounding code and imports1112. **Preserve Patterns**: Maintain existing architectural decisions1123. **Consistent Style**: Match existing code style and structure1134. **Validate Integration**: Ensure changes integrate seamlessly114115### Architecture Validation Checklist116117**BEFORE COMMITTING ANY CHANGES:**118- [ ] Import hierarchy rules respected (no upward imports)119- [ ] Platform-specific files use correct extensions120- [ ] Security patterns maintained (especially for crypto operations)121- [ ] Error handling follows established patterns122- [ ] State management patterns consistently applied123- [ ] UI component patterns followed (Tamagui usage)124- [ ] Translation patterns properly implemented125- [ ] Testing patterns maintained and extended