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
- 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, project structure, package relationships, and import hierarchy rules.4---5
6# OneKey Architecture Overview
7
8## Platform Structure
9- **`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 application
13- **`apps/web-embed/`** - Embeddable wallet components
14
15## Core Packages
16- **`packages/core/`** - Blockchain protocol implementations, cryptography, hardware wallet communication
17- **`packages/kit/`** - Application logic, state management, API integrations
18- **`packages/kit-bg/`** - Background services and workers
19- **`packages/components/`** - Tamagui-based cross-platform UI components
20- **`packages/shared/`** - Platform abstractions, utilities, build configurations
21- **`packages/qr-wallet-sdk/`** - Air-gapped wallet QR communication
22
23## Key Architectural Patterns
24- **Multi-chain support**: 40+ blockchains with pluggable chain implementations
25- **Cross-platform UI**: Tamagui for universal components with platform-specific adaptations
26- **Platform-specific files**: Use `.native.ts`, `.desktop.ts`, `.web.ts`, `.ext.ts` suffixes
27- **Hardware wallet integration**: Custom `@onekeyfe/hd-*` SDK packages
28- **State management**: Jotai for atomic state management
29
30## Code Organization
31
32### File Naming Conventions
33- 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`
37
38### Import Patterns
39- Use workspace references: `@onekeyhq/components`, `@onekeyhq/core`, `@onekeyhq/kit`
40- Platform detection via `@onekeyhq/shared/src/platformEnv`
41- Conditional imports based on platform capabilities
42
43### Import Hierarchy Rules - STRICTLY ENFORCED
44
45**CRITICAL**: Violating these rules WILL break the build and cause circular dependencies.
46
47**HIERARCHY (NEVER violate this order):**
48- `@onekeyhq/shared` - **FORBIDDEN** to import from any other OneKey packages
49- `@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 packages
53
54**BEFORE ADDING ANY IMPORT:**
551. Verify the import respects the hierarchy above
562. Check if the import creates a circular dependency
573. If unsure, find an alternative approach that respects the hierarchy
58
59**COMMON VIOLATIONS TO AVOID:**
60- ❌ Importing from `@onekeyhq/kit` in `@onekeyhq/components`
61- ❌ Importing from `@onekeyhq/components` in `@onekeyhq/kit-bg`
62- ❌ Importing from `@onekeyhq/kit` in `@onekeyhq/core`
63- ❌ Any "upward" imports in the hierarchy
64
65### Component Structure
66- UI components in `packages/components/src/`
67- Business logic in `packages/kit/src/`
68- Chain-specific code in `packages/core/src/chains/`
69
70## Deep Analysis & Architecture Consistency Framework
71
72### Pre-Modification Analysis Protocol
73
74**MANDATORY ANALYSIS STEPS** (Execute BEFORE any code changes):
75
761. **Scope Impact Assessment**
77 - Identify ALL packages/apps affected by the change
78 - Map dependencies that will be impacted (use `yarn why <package>` if needed)
79 - Evaluate cross-platform implications (desktop/mobile/web/extension)
80 - Assess backward compatibility requirements
81
822. **Pattern Consistency Verification**
83 - Examine existing similar implementations in the codebase
84 - Identify established patterns and conventions used
85 - Verify new code follows identical patterns
86 - Check naming conventions align with existing code
87
883. **Architecture Integrity Check**
89 - Validate against monorepo import hierarchy rules
90 - Ensure separation of concerns is maintained
91 - Verify platform-specific code uses correct file extensions
92 - Check that business logic stays in appropriate packages
93
944. **Performance Impact Evaluation**
95 - Consider bundle size implications (especially for web/extension)
96 - Evaluate runtime performance effects
97 - Assess memory usage implications
98 - Consider impact on application startup time
99
100### Code Pattern Recognition Framework
101
102**WHEN ADDING NEW FUNCTIONALITY:**
1031. **Find Similar Examples**: Search codebase for similar implementations
1042. **Extract Patterns**: Identify common approaches, naming, structure
1053. **Follow Conventions**: Mirror existing patterns exactly
1064. **Validate Consistency**: Ensure new code looks like existing code
107
108**WHEN MODIFYING EXISTING CODE:**
1091. **Understand Context**: Read surrounding code and imports
1102. **Preserve Patterns**: Maintain existing architectural decisions
1113. **Consistent Style**: Match existing code style and structure
1124. **Validate Integration**: Ensure changes integrate seamlessly
113
114### Architecture Validation Checklist
115
116**BEFORE COMMITTING ANY CHANGES:**
117- [ ] Import hierarchy rules respected (no upward imports)
118- [ ] Platform-specific files use correct extensions
119- [ ] Security patterns maintained (especially for crypto operations)
120- [ ] Error handling follows established patterns
121- [ ] State management patterns consistently applied
122- [ ] UI component patterns followed (Tamagui usage)
123- [ ] Translation patterns properly implemented
124- [ ] Testing patterns maintained and extended