# getalby/hub architecture

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

- Interactive diagram: https://gitdiagram.com/getalby/hub
- Repository: https://github.com/getalby/hub

## Overview

Key Components to Include:

##### Core Services Layer
- "LNClient Interface" which abstracts different wallet implementations
- "Transactions Service" for managing payment records
- "Event Publisher" system for internal communication

##### Wallet Implementations
- LDK (Lightning Development Kit)
- LND
- Phoenixd
- Cashu
- Breez
- Greenlight

##### Frontend Layer
- Dual-mode architecture:
- HTTP Web App mode
- Desktop (Wails) mode
- React components and routing
- Authentication flow (JWT for HTTP mode)

##### Communication Layer
- NIP-47 Protocol handlers
- Nostr relay communication
- Internal API endpoints
- WebSocket connections for real-time updates

##### Storage Layer
- SQLite database
- Encrypted storage for sensitive data
- Channel backup system

##### External Integrations
- Alby OAuth service
- Lightning Network
- Nostr network
- External apps using NWC

- Blue: Core services
- Green: Wallet implementations
- Orange: Frontend components
- Purple: Communication layers
- Gray: Storage components
- Yellow: External integrations

##### Specific flows
- Payment flow
- Authentication flow
- Event propagation
- App connection flow

##### Event Flow
- LNClient -> Event Publisher -> Transaction Service -> NIP-47 Notifier -> Connected Apps

##### Authentication Chain
- Frontend -> Auth Service -> JWT/Wails -> Protected Services

##### Payment Flow
- NIP-47 Request -> Handler -> Transaction Service -> LNClient -> Lightning Network

##### Demonstrate the relationship between NIP-47 and internal API

- Component types
- Different connection types
- Important event types
- Version information

This architecture should clearly show how Alby Hub functions as a self-sovereign, self-custodial, single-user NWC-first application while maintaining security and providing flexibility in deployment options.

## Diagram (Mermaid)

```mermaid
graph TB
    title[Alby Hub System Architecture]:::title

    subgraph Core["Core Services Layer"]
        Service["Main Service"]:::core
        LNClientInterface["LNClient Interface"]:::core
        TransactionService["Transactions Service"]:::core
        EventPublisher["Event Publisher"]:::core
    end

    subgraph Wallets["Wallet Implementations"]
        LDK["LDK"]:::wallet
        LND["LND"]:::wallet
        Phoenixd["Phoenixd"]:::wallet
        Cashu["Cashu"]:::wallet
        Breez["Breez"]:::wallet
        Greenlight["Greenlight"]:::wallet
    end

    subgraph Frontend["Frontend Layer"]
        HTTPMode["HTTP Web App"]:::frontend
        WailsMode["Desktop Mode"]:::frontend
        ReactComponents["React Components"]:::frontend
        Routes["Frontend Routes"]:::frontend
    end

    subgraph Comm["Communication Layer"]
        NIP47["NIP-47 Protocol"]:::comm
        NostrRelay["Nostr Relay"]:::comm
        APIEndpoints["API Endpoints"]:::comm
        WebSocket["WebSocket"]:::comm
    end

    subgraph Storage["Storage Layer"]
        SQLite[(SQLite DB)]:::storage
        EncryptedStorage["Encrypted Storage"]:::storage
        ChannelBackup["Channel Backup"]:::storage
    end

    subgraph External["External Integrations"]
        AlbyOAuth["Alby OAuth"]:::external
        LightningNetwork["Lightning Network"]:::external
        NostrNetwork["Nostr Network"]:::external
        NWCApps["External NWC Apps"]:::external
    end

    %% Core Relationships
    Service --> LNClientInterface
    Service --> TransactionService
    Service --> EventPublisher
    
    %% Wallet Implementation Relationships
    LNClientInterface --> LDK
    LNClientInterface --> LND
    LNClientInterface --> Phoenixd
    LNClientInterface --> Cashu
    LNClientInterface --> Breez
    LNClientInterface --> Greenlight

    %% Frontend Relationships
    HTTPMode --> APIEndpoints
    WailsMode --> Service
    ReactComponents --> Routes
    Routes --> APIEndpoints

    %% Communication Flow
    NIP47 --> Service
    Service --> NostrRelay
    WebSocket --> EventPublisher
    APIEndpoints --> Service

    %% Storage Relationships
    Service --> SQLite
    Service --> EncryptedStorage
    Service --> ChannelBackup

    %% External Integration Relationships
    Service --> AlbyOAuth
    Service --> LightningNetwork
    Service --> NostrNetwork
    NWCApps --> NIP47

    %% Styling
    classDef title fill:#f9f,stroke:#333,stroke-width:4px
    classDef core fill:#3498db,stroke:#2980b9,color:#fff
    classDef wallet fill:#2ecc71,stroke:#27ae60,color:#fff
    classDef frontend fill:#e67e22,stroke:#d35400,color:#fff
    classDef comm fill:#9b59b6,stroke:#8e44ad,color:#fff
    classDef storage fill:#95a5a6,stroke:#7f8c8d,color:#fff
    classDef external fill:#f1c40f,stroke:#f39c12,color:#000

    %% Click Events
    click Service "https://github.com/getAlby/hub/blob/master/service/service.go"
    click LNClientInterface "https://github.com/getAlby/hub/blob/master/lnclient/models.go"
    click TransactionService "https://github.com/getAlby/hub/blob/master/transactions/transactions_service.go"
    click EventPublisher "https://github.com/getAlby/hub/blob/master/events/events.go"
    click LDK "https://github.com/getAlby/hub/blob/master/lnclient/ldk/ldk.go"
    click LND "https://github.com/getAlby/hub/blob/master/lnclient/lnd/lnd.go"
    click Phoenixd "https://github.com/getAlby/hub/blob/master/lnclient/phoenixd/phoenixd.go"
    click Cashu "https://github.com/getAlby/hub/blob/master/lnclient/cashu/cashu.go"
    click Breez "https://github.com/getAlby/hub/blob/master/lnclient/breez/breez.go"
    click Greenlight "https://github.com/getAlby/hub/blob/master/lnclient/greenlight/greenlight.go"
    click HTTPMode "https://github.com/getAlby/hub/blob/master/cmd/http/main.go"
    click WailsMode "https://github.com/getAlby/hub/blob/master/wails/wails_app.go"
    click ReactComponents "https://github.com/getAlby/hub/tree/master/frontend/src/components"
    click Routes "https://github.com/getAlby/hub/blob/master/frontend/src/routes.tsx"
    click NIP47 "https://github.com/getAlby/hub/tree/master/nip47"
    click NostrRelay "https://github.com/getAlby/hub/tree/master/nostr"
    click APIEndpoints "https://github.com/getAlby/hub/blob/master/api/api.go"
    click SQLite "https://github.com/getAlby/hub/blob/master/db/db.go"
    click EncryptedStorage "https://github.com/getAlby/hub/blob/master/config/aesgcm.go"
    click AlbyOAuth "https://github.com/getAlby/hub/blob/master/alby/alby_oauth_service.go"
```
