# kumavis/metamask-extension architecture

> Architecture diagram of the GitHub repository kumavis/metamask-extension, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kumavis/metamask-extension
- Repository: https://github.com/kumavis/metamask-extension

## Overview

##### Main Components Overview
The system should be divided into these major components:

a) Browser Extension Components:
- MetaMask UI (Frontend)
- MetaMask Background (Core Logic)
- ContentScript (Bridge)
- Inpage Provider (Web3 Interface)

b) External Components:
- Web Dapps
- Ethereum Network
- External Services (Infura)

##### Component Details and Relationships

A. UI Layer (ui/app/):
- Major UI components:
- Account Management
- Transaction Processing
- Network Selection
- Token Management
- State management flow with Redux

B. Background Process (app/scripts/):
- MetaMask Controller
- Various Controllers:
- Transaction Controller
- Network Controller
- Account Controller
- Token Controller
- Data flow between controllers

C. Bridge Layer:
- ContentScript
- Port Communication
- Message Passing

D. Provider Layer:
- Web3 Provider Engine
- RPC Connections
- Account Management

##### Data Flow Representation

- User interactions → UI
- UI → Background Process
- Background Process → Ethereum Network
- Dapp → MetaMask
- State Updates → UI Components

##### Key Processes to Highlight

Flows for:
- Transaction Processing
- Account Management
- Network Switching
- Token Operations
- State Management

##### Technical Stack Indicators

Technology labels:
- React/Redux
- Web3.js
- JavaScript
- Browser APIs
- Ethereum RPC

##### Security Boundaries

Security domains:
- Isolated Extension Background
- Content Security Boundaries
- Secure Storage
- External Communication

##### Storage Systems

Storage components:
- Extension Storage
- State Management
- Vault Storage
- Network Cache

##### External Services Layer

- Component types
- Data flow directions
- Security boundaries
- State management
- Communication channels

Keep the abstraction level high enough to be understandable but detailed enough to show the extension's complexity and security model. Focus on showing how the extension maintains security while bridging web applications with the Ethereum blockchain.

This architecture should reflect MetaMask's role as a secure bridge between web applications and the Ethereum blockchain, emphasizing its key responsibilities in transaction signing, account management, and network interaction.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and Classes
    classDef ui fill:#3498db,stroke:#2980b9,color:white
    classDef core fill:#2ecc71,stroke:#27ae60,color:white
    classDef bridge fill:#f1c40f,stroke:#f39c12,color:black
    classDef external fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef storage fill:#9b59b6,stroke:#8e44ad,color:white

    %% External Layer
    subgraph External
        DAPP["Web Dapps"]:::external
        ETH["Ethereum Network"]:::external
        INFURA["Infura Services"]:::external
    end

    %% UI Layer
    subgraph UserInterface
        UI["MetaMask UI"]:::ui
        ACCT_UI["Account Management"]:::ui
        TX_UI["Transaction Processing"]:::ui
        NET_UI["Network Selection"]:::ui
        TOKEN_UI["Token Management"]:::ui
    end

    %% Core Layer
    subgraph CoreLogic
        MC["MetaMask Controller"]:::core
        TX_CTRL["Transaction Controller"]:::core
        NET_CTRL["Network Controller"]:::core
        ACCT_CTRL["Account Controller"]:::core
        TOKEN_CTRL["Token Controller"]:::core
    end

    %% Bridge Layer
    subgraph Bridge
        CS["ContentScript"]:::bridge
        PORT["Port Communication"]:::bridge
        INPAGE["Inpage Provider"]:::bridge
    end

    %% Storage Layer
    subgraph Storage
        VAULT["Vault Storage"]:::storage
        STATE["State Management"]:::storage
        CACHE["Network Cache"]:::storage
    end

    %% Relationships
    DAPP --> INPAGE
    INPAGE --> CS
    CS --> PORT
    PORT --> MC
    
    UI --> MC
    MC --> TX_CTRL & NET_CTRL & ACCT_CTRL & TOKEN_CTRL
    
    TX_CTRL --> ETH
    NET_CTRL --> INFURA
    
    ACCT_UI --> UI
    TX_UI --> UI
    NET_UI --> UI
    TOKEN_UI --> UI
    
    MC --> STATE
    MC --> VAULT
    NET_CTRL --> CACHE

    %% Click Events
    click UI "https://github.com/kumavis/metamask-extension/tree/develop/ui/app/"
    click MC "https://github.com/kumavis/metamask-extension/blob/develop/app/scripts/metamask-controller.js"
    click CS "https://github.com/kumavis/metamask-extension/blob/develop/app/scripts/contentscript.js"
    click INPAGE "https://github.com/kumavis/metamask-extension/blob/develop/app/scripts/inpage.js"
    click TX_CTRL "https://github.com/kumavis/metamask-extension/blob/develop/app/scripts/controllers/transactions/index.js"
    click NET_CTRL "https://github.com/kumavis/metamask-extension/blob/develop/app/scripts/controllers/network/index.js"
    click ACCT_UI "https://github.com/kumavis/metamask-extension/blob/develop/ui/app/components/account-dropdowns.js"
    click TOKEN_UI "https://github.com/kumavis/metamask-extension/blob/develop/ui/app/components/token-list.js"
    click TX_UI "https://github.com/kumavis/metamask-extension/blob/develop/ui/app/components/tx-view.js"
    click NET_UI "https://github.com/kumavis/metamask-extension/blob/develop/ui/app/components/network.js"

    %% Legend
    subgraph Legend
        L1["UI Components"]:::ui
        L2["Core Components"]:::core
        L3["Bridge Components"]:::bridge
        L4["External Services"]:::external
        L5["Storage"]:::storage
    end
```
