# vardhin/torrentchain architecture

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

- Interactive diagram: https://gitdiagram.com/vardhin/torrentchain
- Repository: https://github.com/vardhin/torrentchain

## Overview

##### Project Type and Purpose
- This is a full-stack, decentralized file-sharing and versioning system (“Torrent Chain”) combining:
- A Svelte-based SPA frontend
- Multiple backend microservices (Node.js and Go)
- IPFS/IPNS as the storage layer
- A local Ethereum testnet (Ganache) + smart contracts for reputation/version control
- The goal is to manage and version torrent files in a purely decentralized fashion, with reputation anchored on-chain.

##### File Structure Analysis
- Top-level directories:
- frontend/ — Svelte UI (routes, components, styling)
- main_backend/ — Node.js service for encryption, WebTorrent, upload management
- ipfs-go-server/ — Go service exposing IPFS/IPNS operations via HTTP
- blockchain/ — Express server + Truffle smart contract for IPNSRegistry on Ganache
- Each directory contains its own package/dependency manifest and entrypoint:
- frontend: package.json, svelte.config.js, vite.config.js
- main_backend: index.js, encryption.js, torrentService.js
- ipfs-go-server: cmd/main.go, internal/handlers, internal/ipfs, models, storage
- blockchain: server.js, migrations/, contracts/, truffle-config.js
- External dependencies:
- IPFS daemon (must be running)
- Ganache local Ethereum node
- MetaMask in the browser for signing transactions

##### Key Architecture Insights from README
- Tech Stack: Svelte (frontend) | Node.js + Go (backends) | IPFS & IPNS (storage) | WebTorrent (P2P transfer) | Ethereum/Ganache (blockchain)
- Interaction flow:
1. User signs transactions (MetaMask) → blockchain express server → deploy/invoke IPNSRegistry contract
2. Files uploaded/encrypted → main_backend service → IPFS daemon (via ipfs-go-server) + torrent creation/seeding (WebTorrent)
3. Frontend requests content / metadata from backends and renders dynamically

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        FE_SRC["frontend/src"]:::frontend
        FE_APP["app.html"]:::frontend
        FE_ROUTES["routes/"]:::frontend
        FE_SVELTECFG["svelte.config.js"]:::frontend
        FE_VITECFG["vite.config.js"]:::frontend
        FE_PKG["package.json"]:::frontend
    end

    %% Blockchain Layer
    subgraph "Blockchain Layer"
        direction TB
        BC_SRVR["server.js"]:::blockchain
        BC_PKG["package.json"]:::blockchain
        BC_PKGLOCK["package-lock.json"]:::blockchain
        BC_PUBLIC["public/index.html"]:::blockchain
        BC_CONTRACT["IPNSRegistry.sol"]:::blockchain
        BC_BUILD["build/contracts/IPNSRegistry.json"]:::blockchain
        BC_MIGR["2_deploy_ipns_registry.js"]:::blockchain
        BC_CONFIG["truffle-config.js"]:::blockchain
        BC_TEST["test/"]:::blockchain
    end

    %% Main Backend Layer
    subgraph "Main Backend (Node.js)"
        direction TB
        MB_INDEX["index.js"]:::backend
        MB_ENC["encryption.js"]:::backend
        MB_TOR["torrentService.js"]:::backend
        MB_UPLOADS["uploads/"]:::backend
        MB_TORRENTS["torrents/"]:::backend
        MB_PKG["package.json"]:::backend
        MB_PKGLOCK["package-lock.json"]:::backend
    end

    %% IPFS Layer
    subgraph "IPFS Layer (Go)"
        direction TB
        IPFS_CMD["cmd/main.go"]:::ipfs
        IPFS_CLIENT["internal/ipfs/client.go"]:::ipfs
        IPNS_CLIENT["internal/ipfs/ipns.go"]:::ipfs
        IPFS_HANDLERS["internal/handlers/handlers.go"]:::ipfs
        IPFS_STORAGE["internal/storage/storage.go"]:::ipfs
        IPFS_MODELS["internal/models/table.go"]:::ipfs
        IPFS_UTILS["pkg/utils/utils.go"]:::ipfs
        IPFS_TABLES["tables_registry.json"]:::ipfs
        IPFS_GOMOD["go.mod"]:::ipfs
        IPFS_GO_SUM["go.sum"]:::ipfs
    end

    %% External & Infrastructure
    MetaMask["MetaMask (Browser)"]:::external
    Ganache["Ganache Ethereum Node"]:::external
    IPFS_DAEMON["IPFS Daemon"]:::external
    WebTorrentNet["WebTorrent Network"]:::p2p

    %% Relationships
    FE_SRC -->|"serves SPA"| FE_APP
    FE_APP -->|"loads routes"| FE_ROUTES
    FE_SRC -->|"configures"| FE_SVELTECFG
    FE_SRC -->|"configures"| FE_VITECFG
    FE_SRC -->|"defines deps"| FE_PKG

    FE_ROUTES -->|"+layout/+page"| FE_SRC

    FE_SRC -->|"calls REST|WS"| MB_INDEX
    FE_SRC -->|"invokes JSON-RPC REST"| BC_SRVR
    FE_SRC -->|"requests tx signature"| MetaMask

    MetaMask -->|"signs tx"| BC_SRVR

    BC_SRVR -->|"Web3 RPC"| Ganache
    BC_SRVR -->|"deploy/invoke"| BC_CONTRACT

    MB_INDEX -->|"calls encryption"| MB_ENC
    MB_INDEX -->|"calls torrentService"| MB_TOR
    MB_INDEX -->|"stores uploads"| MB_UPLOADS
    MB_INDEX -->|"stores torrents"| MB_TORRENTS
    MB_INDEX -->|"calls IPFS API"| IPFS_CMD
    MB_INDEX -->|"streams P2P"| WebTorrentNet

    IPFS_CMD -->|"uses client|ipns"| IPFS_CLIENT
    IPFS_CMD -->|"uses ipns client"| IPNS_CLIENT
    IPFS_CMD -->|"HTTP handlers"| IPFS_HANDLERS
    IPFS_CMD -->|"storage"| IPFS_STORAGE
    IPFS_CMD -->|"models"| IPFS_MODELS
    IPFS_CMD -->|"utils"| IPFS_UTILS
    IPFS_CMD -->|"config"| IPFS_TABLES
    IPFS_CMD -->|"build info"| IPFS_GOMOD
    IPFS_CMD -->|"checksum"| IPFS_GO_SUM

    IPFS_HANDLERS -->|"calls"| IPFS_DAEMON
    MB_INDEX -->|"calls IPFS HTTP"| IPFS_HANDLERS

    %% Click Events Frontend
    click FE_SRC "https://github.com/vardhin/torrentchain/tree/main/frontend/src"
    click FE_APP "https://github.com/vardhin/torrentchain/blob/main/frontend/src/app.html"
    click FE_ROUTES "https://github.com/vardhin/torrentchain/tree/main/frontend/src/routes"
    click FE_SVELTECFG "https://github.com/vardhin/torrentchain/blob/main/frontend/svelte.config.js"
    click FE_VITECFG "https://github.com/vardhin/torrentchain/blob/main/frontend/vite.config.js"
    click FE_PKG "https://github.com/vardhin/torrentchain/blob/main/frontend/package.json"

    %% Click Events Blockchain
    click BC_SRVR "https://github.com/vardhin/torrentchain/blob/main/blockchain/server.js"
    click BC_PKG "https://github.com/vardhin/torrentchain/blob/main/blockchain/package.json"
    click BC_PKGLOCK "https://github.com/vardhin/torrentchain/blob/main/blockchain/package-lock.json"
    click BC_PUBLIC "https://github.com/vardhin/torrentchain/blob/main/blockchain/public/index.html"
    click BC_CONTRACT "https://github.com/vardhin/torrentchain/blob/main/blockchain/contracts/IPNSRegistry.sol"
    click BC_BUILD "https://github.com/vardhin/torrentchain/blob/main/blockchain/build/contracts/IPNSRegistry.json"
    click BC_MIGR "https://github.com/vardhin/torrentchain/blob/main/blockchain/migrations/2_deploy_ipns_registry.js"
    click BC_CONFIG "https://github.com/vardhin/torrentchain/blob/main/blockchain/truffle-config.js"
    click BC_TEST "https://github.com/vardhin/torrentchain/tree/main/blockchain/test"

    %% Click Events Main Backend
    click MB_INDEX "https://github.com/vardhin/torrentchain/blob/main/main_backend/index.js"
    click MB_ENC "https://github.com/vardhin/torrentchain/blob/main/main_backend/encryption.js"
    click MB_TOR "https://github.com/vardhin/torrentchain/blob/main/main_backend/torrentService.js"
    click MB_UPLOADS "https://github.com/vardhin/torrentchain/tree/main/main_backend/uploads/"
    click MB_TORRENTS "https://github.com/vardhin/torrentchain/tree/main/main_backend/torrents/"
    click MB_PKG "https://github.com/vardhin/torrentchain/blob/main/main_backend/package.json"
    click MB_PKGLOCK "https://github.com/vardhin/torrentchain/blob/main/main_backend/package-lock.json"

    %% Click Events IPFS
    click IPFS_CMD "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/cmd/main.go"
    click IPFS_CLIENT "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/internal/ipfs/client.go"
    click IPNS_CLIENT "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/internal/ipfs/ipns.go"
    click IPFS_HANDLERS "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/internal/handlers/handlers.go"
    click IPFS_STORAGE "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/internal/storage/storage.go"
    click IPFS_MODELS "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/internal/models/table.go"
    click IPFS_UTILS "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/pkg/utils/utils.go"
    click IPFS_TABLES "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/tables_registry.json"
    click IPFS_GOMOD "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/go.mod"
    click IPFS_GO_SUM "https://github.com/vardhin/torrentchain/blob/main/ipfs-go-server/go.sum"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef blockchain fill:#90EE90,stroke:#000,stroke-width:1px
    classDef backend fill:#DDA0DD,stroke:#000,stroke-width:1px
    classDef ipfs fill:#FFA500,stroke:#000,stroke-width:1px
    classDef p2p fill:#D3D3D3,stroke:#000,stroke-width:1px
    classDef external fill:#FFFACD,stroke:#000,stroke-width:1px
```
