How 2046/onekey works
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).