# 2046/onekey architecture

> Architecture diagram of the GitHub repository 2046/onekey, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/2046/onekey
- Repository: https://github.com/2046/onekey

## Overview

##### Main Components
- CLI Layer:
- Located in the “src/cli” directory.
- Handles command-line arguments, interprets file paths, passwords, and tool flags.
- Core files: main.ts, crypto.ts, tasks.ts, typing.ts, utils.ts.

- Library/Services Layer:
- Found in “src/lib”. This contains the core logic needed to download, install, and unpack packages:
▪ Commands for installation and activation (active.ts, install.ts).
▪ Download functionality (donwload.ts – note the slight typo in file name).
▪ Unpack modules under “src/lib/unpack” for handling different installation file types (brew.ts, dmg.ts, mas.ts, pkg.ts, zip.ts).
▪ Command line tools helper functions (commandlinetools.ts).
▪ Utility functions for constants, shell commands, and package parsing in “src/lib/utils”.

- Test Suites:
- Located in the “test” directory.
- Verifies CLI and library functionalities using specs for crypto, tasks, utils, download, and install operations.

- Configuration & Assets:
- The project configuration (package.json, tsconfig.json, etc.) and assets (such as the bundled mas-cli package).
- Example configuration files (under examples) that illustrate valid application “pack” files.

##### Relationships and Data Flow
- The CLI module is the entry point. It parses user inputs (configuration file path, secret key, and optional encryption/decryption flags) and then invokes functions from the library layer.
- The Library layer then:
- Reads and parses the YAML configuration (.pack files) to determine which operations to perform.
- Routes the task to the appropriate service module (download, install, unpack, and possibly activate).
- Uses dedicated “unpack” modules for handling different file types (dmg, pkg, zip, brew, mas).
- Leverages utility functions (for shell execution, constants, package utilities) to support the installation operations.
- Test components ensure that each of these layers works correctly.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clear division between the CLI (user interface) and backend library functions. The CLI handles input parsing, while the library modules encapsulate business logic (downloading, installing, and unpacking).
- Modular Decomposition: Each file in “src/lib/unpack” is responsible for a specific package type. This modular approach promotes extensibility, allowing new package types to be added without impacting other components.
- Utility/Helper Layer: The utility functions enclosed in “src/lib/utils” centralize common operations, reducing redundancy and keeping the code DRY (Don't Repeat Yourself).

##### Technologies and Frameworks
- Node.js with TypeScript as indicated by tsconfig files.
- Shell utilities and file system operations invoked in installation routines.
- External services (download URLs, signing/activation mechanisms) as defined in the configuration file and referenced from macOS.
- Integration with encryption via AES-256-cbc algorithm (supporting secure configuration files).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CLI Layer"
        CLI["CLI Module"]:::cli
    end

    subgraph "Library Services"
        DS["Download Service"]:::service
        IS["Installation Service"]:::service
        AS["Activation/Sync Service"]:::service
        subgraph "Unpack Services"
            dmg["dmg Module"]:::unpack
            pkg["pkg Module"]:::unpack
            zip["zip Module"]:::unpack
            brew["brew Module"]:::unpack
            mas["mas Module"]:::unpack
        end
        UF["Utility Functions"]:::utility
    end

    subgraph "External & Testing"
        TEST["Test Suites"]:::test
        CONFIG["Configuration & Example Assets"]:::config
    end

    CLI -->|"invokes"| DS
    CLI -->|"invokes"| IS
    CLI -->|"invokes"| AS
    CLI -->|"uses"| UF

    DS -->|"downloads"| UF
    DS -->|"passes file to"| IS

    IS -->|"triggers"| dmg
    IS -->|"triggers"| pkg
    IS -->|"triggers"| zip
    IS -->|"triggers"| brew
    IS -->|"triggers"| mas

    AS -->|"activates"| IS

    TEST -->|"verifies"| CLI
    TEST -->|"verifies"| DS
    TEST -->|"verifies"| IS
    CONFIG -->|"drives"| CLI

    click CLI "https://github.com/2046/onekey/blob/main/src/cli/main.ts"
    click DS "https://github.com/2046/onekey/blob/main/src/lib/donwload.ts"
    click IS "https://github.com/2046/onekey/blob/main/src/lib/install.ts"
    click AS "https://github.com/2046/onekey/blob/main/src/lib/active.ts"
    click dmg "https://github.com/2046/onekey/blob/main/src/lib/unpack/dmg.ts"
    click pkg "https://github.com/2046/onekey/blob/main/src/lib/unpack/pkg.ts"
    click zip "https://github.com/2046/onekey/blob/main/src/lib/unpack/zip.ts"
    click brew "https://github.com/2046/onekey/blob/main/src/lib/unpack/brew.ts"
    click mas "https://github.com/2046/onekey/blob/main/src/lib/unpack/mas.ts"
    click UF "https://github.com/2046/onekey/blob/main/src/lib/commandlinetools.ts"
    click TEST "https://github.com/2046/onekey/tree/main/test"
    click CONFIG "https://github.com/2046/onekey/tree/main/examples"

    classDef cli fill:#f9c2ff,stroke:#333,stroke-width:2px;
    classDef service fill:#c2e0ff,stroke:#333,stroke-width:2px;
    classDef unpack fill:#c2ffc2,stroke:#333,stroke-width:2px;
    classDef utility fill:#fff4c2,stroke:#333,stroke-width:2px;
    classDef test fill:#ffd9c2,stroke:#333,stroke-width:2px;
    classDef config fill:#d9ffd9,stroke:#333,stroke-width:2px;
```
