# stk2chain/stk2eth architecture

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

- Interactive diagram: https://gitdiagram.com/stk2chain/stk2eth
- Repository: https://github.com/stk2chain/stk2eth

## Overview

##### Project type and purpose
- This is a multi‐component, blockchain‐and‐telecom system (“STK2ETH”) that lets a Java Card SIM Toolkit Applet relay Ethereum transactions over USSD (no internet).
- Key subsystems:
- Java Card Applet (eSTK) running on the SIM
- USSD-to-Ethereum Gateway (“ussdgeth”) written in Rust
- Ethereum Account Abstraction smart contracts (Solidity, foundry)
- CLI and test tools (ethclient, ussdclient, Python tests)

##### File structure
- Top‐level modules:
- contracts/ – Solidity smart contracts, scripts, tests, Foundry config
- ethclient/ – Rust Ethereum JSON-RPC client CLI
- ussdclient/ – Rust USSD client CLI (for testing)
- ussdgeth/ – Rust USSD-ETH gateway service
- tests/ – end‐to‐end Python test of STK flow
- doc/ – formal SPECs for applet, gateway, contracts
- ussdgeth/src: layered architecture
- controller/ – HTTP or USSD inbound routing
- ussdframework/ – session, request/response parsing, service logic
- data/menu.json – menu definitions

##### README for additional insights
- System Schematic image links outline three zones: eSIM Applet, USSD Gateway (4337#), on-chain AA contracts.
- Specs describe protocol messages, encoding, and flows.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Device Tier
    subgraph "Device Tier"
        SIMApplet["SIM Toolkit Applet"]:::device
        PhoneUI["Mobile Phone UI / STK Launcher"]:::device
    end

    %% Transport Tier
    subgraph "Transport Tier"
        MNO["MNO USSD Infrastructure"]:::transport
    end

    %% Backend Tier
    subgraph "Backend Tier"
        subgraph "USSD-ETH Gateway Service"
            GatewayLib["Gateway Entrypoint (lib.rs)"]:::backend
            Controller["Controller Module"]:::backend
            subgraph "USSD Framework"
                USSDMenu["ussd_menu.rs"]:::backend
                USSDReq["ussd_request.rs"]:::backend
                USSDResp["ussd_response.rs"]:::backend
                USSDScreens["ussd_screens.rs"]:::backend
                USSDSvc["ussd_service.rs"]:::backend
                USSDSess["ussd_session.rs"]:::backend
            end
            DataStore["Menu Data Store (menu.json)"]:::backend
        end
        EthClientCLI["Ethereum JSON-RPC Client CLI"]:::cli
        USSDClientCLI["USSD Client CLI"]:::cli
        subgraph "Ethereum Network"
            Node["Ethereum Node"]:::blockchain
            subgraph "AA Smart Contracts"
                ContractsSrc["contracts/src/"]:::blockchain
                ContractsTest["contracts/test/"]:::blockchain
                ContractsScript["contracts/script/"]:::blockchain
                ContractsConfig["contracts/foundry.toml"]:::blockchain
            end
        end
        Tests["Test Harness (tests/test_stk_flow.py)"]:::tests
        subgraph "Documentation"
            DocOverall["doc/SPEC.md"]:::docs
            DocApplet["doc/specs/applet.md"]:::docs
            DocContracts["doc/specs/contracts.md"]:::docs
            DocGateway["doc/specs/gateway.md"]:::docs
            DocContrib["doc/contributing/commit-message.md"]:::docs
        end
    end

    %% Data Flows
    PhoneUI -->|"Trigger USSD"| SIMApplet
    SIMApplet -->|"USSD SEND PDU"| MNO
    MNO -->|"USSD PDU"| GatewayLib
    GatewayLib --> Controller
    Controller --> USSDMenu
    Controller --> USSDReq
    Controller --> USSDResp
    Controller --> USSDScreens
    Controller --> USSDSvc
    Controller --> USSDSess
    USSDSvc --> DataStore
    USSDSvc -->|"JSON-RPC submitTransaction"| EthClientCLI
    EthClientCLI --> Node
    Node --> ContractsSrc
    ContractsSrc --> ContractsTest
    ContractsSrc --> ContractsScript
    ContractsSrc --> ContractsConfig
    Node -->|"Event/Receipt"| EthClientCLI
    EthClientCLI --> USSDSvc
    USSDSvc --> MNO
    MNO --> SIMApplet

    %% CLI and Tests
    Tests --> USSDClientCLI
    Tests --> EthClientCLI

    %% Documentation Links
    DocOverall --> SIMApplet
    DocOverall --> GatewayLib
    DocOverall --> ContractsSrc

    %% Click Events
    click SIMApplet "https://github.com/stk2chain/stk2eth/blob/develop/doc/specs/applet.md"
    click GatewayLib "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/lib.rs"
    click Controller "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/controller/mod.rs"
    click USSDMenu "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/ussdframework/ussd_menu.rs"
    click USSDReq "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/ussdframework/ussd_request.rs"
    click USSDResp "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/ussdframework/ussd_response.rs"
    click USSDScreens "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/ussdframework/ussd_screens.rs"
    click USSDSvc "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/ussdframework/ussd_service.rs"
    click USSDSess "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/ussdframework/ussd_session.rs"
    click DataStore "https://github.com/stk2chain/stk2eth/blob/develop/ussdgeth/src/data/menu.json"
    click ContractsSrc "https://github.com/stk2chain/stk2eth/tree/develop/contracts/src/"
    click ContractsTest "https://github.com/stk2chain/stk2eth/tree/develop/contracts/test/"
    click ContractsScript "https://github.com/stk2chain/stk2eth/tree/develop/contracts/script/"
    click ContractsConfig "https://github.com/stk2chain/stk2eth/blob/develop/contracts/foundry.toml"
    click EthClientCLI "https://github.com/stk2chain/stk2eth/blob/develop/ethclient/src/main.rs"
    click USSDClientCLI "https://github.com/stk2chain/stk2eth/blob/develop/ussdclient/src/main.rs"
    click Tests "https://github.com/stk2chain/stk2eth/blob/develop/tests/test_stk_flow.py"
    click DocOverall "https://github.com/stk2chain/stk2eth/blob/develop/doc/SPEC.md"
    click DocContracts "https://github.com/stk2chain/stk2eth/blob/develop/doc/specs/contracts.md"
    click DocGateway "https://github.com/stk2chain/stk2eth/blob/develop/doc/specs/gateway.md"
    click DocContrib "https://github.com/stk2chain/stk2eth/blob/develop/doc/contributing/commit-message.md"

    %% Styles
    classDef device fill:#E0F7FA,stroke:#006064,color:#006064
    classDef transport fill:#FFF3E0,stroke:#E65100,color:#E65100
    classDef backend fill:#E8F5E9,stroke:#2E7D32,color:#2E7D32
    classDef blockchain fill:#F3E5F5,stroke:#6A1B9A,color:#6A1B9A
    classDef cli fill:#FFFDE7,stroke:#F9A825,color:#F9A825
    classDef tests fill:#ECEFF1,stroke:#455A64,color:#455A64
    classDef docs fill:#F1F8E9,stroke:#558B2F,color:#558B2F
```
