# moneybutton/bsv architecture

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

- Interactive diagram: https://gitdiagram.com/moneybutton/bsv
- Repository: https://github.com/moneybutton/bsv

## Overview

##### Main Components

a) Core Components:
- Cryptography Module
- Key Management (key-pair.js, priv-key.js, pub-key.js)
- Encryption/Decryption (aes.js, aescbc.js)
- Hashing (hash.js, hash-cache.js)
- ECDSA Operations (ecdsa.js, ecies.js)

b) Bitcoin Protocol Components:
- Transaction Management
- Transaction Building (tx-builder.js)
- Transaction Verification (tx-verifier.js)
- Input/Output Handling (tx-in.js, tx-out.js)
- Block Management
- Block Handling (block.js)
- Block Headers (block-header.js)
- Merkle Trees (merkle.js)

c) Address Management:
- Address Generation
- Network-specific Implementations (mainnet/testnet)

d) BIP Implementation Layer:
- BIP32 (Hierarchical Deterministic Wallets)
- BIP39 (Mnemonic Codes)

e) Worker System:
- Web Worker Interface
- Worker Results Handler

**┌─────────────────────────────────────────────┐**
- Application Layer              │
- (External Applications Using the Library)  │
- ┘
- ↕
**┌─────────────────────────────────────────────┐**
- Public API Layer                │
- (index.js main entry point)          │
- ┘
- ↕
**┌─────────────────────────────────────────────┐**
- Core Functionality Layer            │
- ┬─────────────┬────────────────┤
- Crypto     │  Protocol   │   Address      │
- Operations │  Handling   │   Management   │
- ┴─────────────┴────────────────┘
- ↕
**┌─────────────────────────────────────────────┐**
- Utility Layer                    │
- (Base58, Buffer Ops, Random, Constants)    │
- ┘

**Main Thread ←→ Worker Interface ←→ Worker Pool**

**Network Layer**
- Mainnet Implementation
- Testnet Implementation

##### Implementation Details

- Use rectangles for modules
- Use hexagons for cryptographic operations

##### Additional Elements

- Configuration management showing how environment variables affect the system
- Test coverage indicators for each component
- External dependency relationships (minimal as per project goals)
- BIP implementation compliance

## Diagram (Mermaid)

```mermaid
%%{init: {
  'theme': 'base',
  'themeVariables': {
    'primaryColor': '#2b2b2b',
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#7C0000',
    'lineColor': '#F8B229',
    'secondaryColor': '#006100',
    'tertiaryColor': '#0000ff'
  }
}}%%

flowchart TB
    subgraph Application["Application Layer"]
        ExternalApp["External Applications"]
    end

    subgraph API["Public API Layer"]
        PublicAPI["Library Entry Point"]
    end

    subgraph Core["Core Functionality Layer"]
        subgraph Crypto["Cryptographic Operations"]
            direction TB
            KeyMgmt["Key Management"]:::crypto
            click KeyMgmt "https://github.com/moneybutton/bsv/blob/master/lib/key-pair.js"
            Encryption["Encryption/Decryption"]:::crypto
            click Encryption "https://github.com/moneybutton/bsv/blob/master/lib/aes.js"
            Hashing["Hashing"]:::crypto
            click Hashing "https://github.com/moneybutton/bsv/blob/master/lib/hash.js"
            ECDSA["ECDSA Operations"]:::crypto
            click ECDSA "https://github.com/moneybutton/bsv/blob/master/lib/ecdsa.js"
        end

        subgraph Protocol["Protocol Handling"]
            direction TB
            TxMgmt["Transaction Management"]:::protocol
            click TxMgmt "https://github.com/moneybutton/bsv/blob/master/lib/tx-builder.js"
            BlockMgmt["Block Management"]:::protocol
            click BlockMgmt "https://github.com/moneybutton/bsv/blob/master/lib/block.js"
            MerkleOps["Merkle Trees"]:::protocol
            click MerkleOps "https://github.com/moneybutton/bsv/blob/master/lib/merkle.js"
        end

        subgraph Addr["Address Management"]
            direction TB
            MainNet["Mainnet Implementation"]:::address
            click MainNet "https://github.com/moneybutton/bsv/blob/master/lib/mainnet-address.js"
            TestNet["Testnet Implementation"]:::address
            click TestNet "https://github.com/moneybutton/bsv/blob/master/lib/testnet-address.js"
        end

        subgraph BIP["BIP Implementation"]
            direction TB
            BIP32["BIP32 HD Wallets"]:::bip
            click BIP32 "https://github.com/moneybutton/bsv/blob/master/lib/bip-32.js"
            BIP39["BIP39 Mnemonic Codes"]:::bip
            click BIP39 "https://github.com/moneybutton/bsv/blob/master/lib/bip-39.js"
        end
    end

    subgraph Workers["Worker System"]
        WorkerInterface["Worker Interface"]:::worker
        click WorkerInterface "https://github.com/moneybutton/bsv/blob/master/lib/workers.js"
        WorkerResults["Results Handler"]:::worker
        click WorkerResults "https://github.com/moneybutton/bsv/blob/master/lib/workers-result.js"
    end

    subgraph Util["Utility Layer"]
        Base58["Base58"]:::util
        click Base58 "https://github.com/moneybutton/bsv/blob/master/lib/base-58.js"
        Random["Random"]:::util
        click Random "https://github.com/moneybutton/bsv/blob/master/lib/random.js"
        Constants["Constants"]:::util
        click Constants "https://github.com/moneybutton/bsv/blob/master/lib/constants.js"
    end

    Application --> API
    API --> Core
    API --> Workers
    Core --> Util
    Workers --> Core

    %% Component Relationships
    KeyMgmt --> ECDSA
    Encryption --> KeyMgmt
    TxMgmt --> BlockMgmt
    BlockMgmt --> MerkleOps
    MainNet --> KeyMgmt
    TestNet --> KeyMgmt
    BIP32 --> KeyMgmt
    BIP39 --> BIP32

    %% Styling
    classDef crypto fill:#0000ff,color:#fff
    classDef protocol fill:#006100,color:#fff
    classDef address fill:#7C0000,color:#fff
    classDef bip fill:#9932CC,color:#fff
    classDef worker fill:#FF8C00,color:#fff
    classDef util fill:#4B0082,color:#fff

    %% Legend
    subgraph Legend
        L1["Cryptographic Operations"]:::crypto
        L2["Protocol Components"]:::protocol
        L3["Address Management"]:::address
        L4["BIP Implementation"]:::bip
        L5["Worker System"]:::worker
        L6["Utilities"]:::util
    end
```
