# outblock/flow-swift architecture

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

- Interactive diagram: https://gitdiagram.com/outblock/flow-swift
- Repository: https://github.com/outblock/flow-swift

## Overview

##### Main Components
- SDK Core Module: At the heart is the Flow.swift file that likely acts as the orchestrator coordinating interactions and exposing the public API.
- Models: Components under Sources/Models encapsulate all the domain entities such as accounts, blocks, transactions, events, scripts, etc.
- Network Layer:
- HTTP Communications: Located in Sources/Network/HTTP (e.g., FlowHTTPClient, AccessEndpoint) and FlowTransport.
- gRPC Support: Although the gRPC implementation is in a separate repository (flow-swift-gRPC), this core SDK configures a gRPC access node as an alternative.
- UserAgent configuration is also part of network setup.
- Transaction and Build Module:
- Transaction building functionality is in Sources/Build (e.g., TransactionBuild.swift) and likely invoked via helper methods in Flow.swift.
- This module supports building transactions (with Cadence script injection, proposal keys, authorizers, etc.) as outlined in the README.
- Cadence Module:
- Located in Sources/Cadence, this component handles the integration of Cadence scripts (the DSL for Flow blockchain) and common Cadence utilities.
- Codeable and Serialization:
- Sources/Codeable handles encoding/decoding of arguments and conversion of transaction data to a suitable format.
- Error Handling:
- Sources/Error defines custom error types used throughout the SDK for handling failure conditions (e.g., FVMError, FlowError).
- Logging:
- Sources/Log contains a logging component to capture SDK events and debugging information.
- Extensions and Utilities:
- Sources/Extension provides helpful language extensions that support async network calls (e.g., URLSession+Async), data conversion (Data, Array, Double, String extensions) and provide additional utility functions.
- RLP Encoding:
- Sources/RLP provides support for RLP (Recursive Length Prefix) encoding, which may be used for transaction signing or other data encoding purposes.
- Testing Suite:
- The Tests directory covers unit tests for the various functionalities (argument encoding/decoding, network operations, RLP, FlowAccess interactions on different networks). This confirms the modular nature of the architecture and the separation of concerns.

##### Relationships and Interactions
- The Flow SDK Core (Flow.swift) acts as the central point that coordinates between the network layer (HTTP/gRPC) and the business logic layers (transaction building, account creation, execution of Cadence scripts).
- The Models are used throughout to represent state from the blockchain; these models pass between layers.
- Transactions are built by the Build module, then passed to a signing routine (potentially using one of the provided FlowSigner implementations) which then are sent through the Network layer.
- The Codeable and RLP modules assist in serialization and encoding tasks during both querying and transaction processing.
- Error handling and logging are cross-cutting concerns that feed into debugging and application reliability.
- The Extensions module ensures that network calls and asynchronous operations are handled in an idiomatic Swift manner.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The library cleanly separates domain models, network communication, transaction construction, encoding utilities, and error handling.
- Modular Design: Each major functionality is located in its own directory (e.g., Models, Network, Cadence, Build), making the system easy to extend or modify.
- Protocol-Oriented and Interface Abstraction: For instance, the FlowSigner protocol abstracts the signing process, allowing multiple implementations (in-memory, HSM, different algorithms).
- Layered Communication: SDK users interact with high-level methods (like flow.getLatestBlock, flow.buildTransaction) while the underlying layers manage data serialization and network communication.
- Test-Driven: Having an extensive test suite (located in Tests) indicates a focus on reliability and maintainability.

##### Relevant Technologies and Frameworks
- Swift and Swift Package Manager as the primary language and dependency management system.
- gRPC and HTTP client implementations for network communications.
- Usage of Swift’s Codable for JSON handling and structured data representation.
- Possibly integration with SwiftLint for code quality enforcement (referenced in the swiftlint.yml).
- GitHub workflows for CI/CD processes are present in the .github/workflows directory.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% SDK Core
    SDK_Core["SDK Core (Flow Orchestrator)"]:::core

    %% Models Subgraph
    subgraph "Models Layer"
        Flow_Account["Flow Account"]:::models
        Flow_Block["Flow Block"]:::models
        Flow_Transaction["Flow Transaction"]:::models
    end

    %% Network Layer Subgraph
    subgraph "Network Layer"
        HTTP_Client["HTTP Client"]:::network
        Access_Endpoint["AccessEndpoint"]:::network
        HTTP_Model["HTTP Model"]:::network
        Flow_Transport["Flow Transport"]:::network
        User_Agent["UserAgent"]:::network
        GRPC["gRPC Access (Alternate)"]:::network
    end

    %% Transaction Builder
    Transaction_Builder["Transaction Builder"]:::transaction

    %% Cadence Module
    Cadence_Module["Cadence Module"]:::cadence

    %% Codeable & Serialization Subgraph
    subgraph "Codeable & Serialization"
        FlowArgument_Decode["FlowArgument+Decode"]:::codeable
        FlowArgument_Encode["FlowArgument+Encode"]:::codeable
    end

    %% Error Handling Subgraph
    subgraph "Error Handling"
        FVM_Error["FVMError"]:::error
        Flow_Error["FlowError"]:::error
    end

    %% Logging Module
    Logging_Module["Logging Module"]:::logging

    %% Extensions & Utilities Subgraph
    subgraph "Extensions & Utilities"
        Array_Ext["Array Extension"]:::extensions
        Data_Ext["Data Extension"]:::extensions
        Double_Ext["Double Extension"]:::extensions
        Publisher_Async["Publisher+Async"]:::extensions
        String_Ext["String Extension"]:::extensions
        URLSession_Async["URLSession+Async"]:::extensions
    end

    %% RLP Encoding Module
    RLP_Module["RLP Encoding"]:::rlp

    %% Testing Suite
    Testing_Suite["Testing Suite"]:::testing

    %% Relationships
    SDK_Core -->|"usesModels"| Flow_Account
    SDK_Core -->|"usesModels"| Flow_Block
    SDK_Core -->|"usesModels"| Flow_Transaction

    SDK_Core -->|"buildsTx"| Transaction_Builder
    Transaction_Builder -->|"submitsTx"| HTTP_Client

    SDK_Core -->|"executesCadence"| Cadence_Module

    SDK_Core -->|"serializesData"| FlowArgument_Decode
    SDK_Core -->|"serializesData"| FlowArgument_Encode

    SDK_Core -->|"handlesErrors"| FVM_Error
    SDK_Core -->|"handlesErrors"| Flow_Error

    SDK_Core -->|"logsEvents"| Logging_Module

    SDK_Core -->|"utilizes"| Array_Ext
    SDK_Core -->|"utilizes"| Data_Ext
    SDK_Core -->|"utilizes"| Double_Ext
    SDK_Core -->|"utilizes"| Publisher_Async
    SDK_Core -->|"utilizes"| String_Ext
    SDK_Core -->|"utilizes"| URLSession_Async

    SDK_Core -->|"encodesRLP"| RLP_Module

    SDK_Core -->|"testedBy"| Testing_Suite

    %% Network internal connections
    HTTP_Client -->|"processesData"| Flow_Transaction
    Access_Endpoint -->|"defines"| HTTP_Model
    Access_Endpoint -->|"communicates"| Flow_Transport
    User_Agent -->|"configures"| HTTP_Client
    SDK_Core -->|"optionalGRPC"| GRPC

    %% Class Definitions
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef models fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef network fill:#FFD700,stroke:#000,stroke-width:1px;
    classDef transaction fill:#87CEEB,stroke:#000,stroke-width:1px;
    classDef cadence fill:#DDA0DD,stroke:#000,stroke-width:1px;
    classDef codeable fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef error fill:#F08080,stroke:#000,stroke-width:1px;
    classDef logging fill:#FFA500,stroke:#000,stroke-width:1px;
    classDef extensions fill:#E0FFFF,stroke:#000,stroke-width:1px;
    classDef rlp fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef testing fill:#98FB98,stroke:#000,stroke-width:1px;

    %% Click Events
    click SDK_Core "https://github.com/outblock/flow-swift/blob/main/Sources/Flow.swift"
    click Flow_Account "https://github.com/outblock/flow-swift/blob/main/Sources/Models/FlowAccount.swift"
    click Flow_Block "https://github.com/outblock/flow-swift/blob/main/Sources/Models/FlowBlock.swift"
    click Flow_Transaction "https://github.com/outblock/flow-swift/blob/main/Sources/Models/FlowTransaction.swift"
    click HTTP_Client "https://github.com/outblock/flow-swift/blob/main/Sources/Network/HTTP/FlowHTTPClient.swift"
    click Access_Endpoint "https://github.com/outblock/flow-swift/blob/main/Sources/Network/HTTP/AccessEndpoint.swift"
    click HTTP_Model "https://github.com/outblock/flow-swift/blob/main/Sources/Network/HTTP/FlowHTTPModel.swift"
    click Flow_Transport "https://github.com/outblock/flow-swift/blob/main/Sources/Network/FlowTransport.swift"
    click User_Agent "https://github.com/outblock/flow-swift/blob/main/Sources/Network/UserAgent.swift"
    click Transaction_Builder "https://github.com/outblock/flow-swift/blob/main/Sources/Build/TransactionBuild.swift"
    click Cadence_Module "https://github.com/outblock/flow-swift/blob/main/Sources/Cadence/CommonCadence.swift"
    click FlowArgument_Decode "https://github.com/outblock/flow-swift/blob/main/Sources/Codeable/FlowArgument+Decode.swift"
    click FlowArgument_Encode "https://github.com/outblock/flow-swift/blob/main/Sources/Codeable/FlowArgument+Encode.swift"
    click FVM_Error "https://github.com/outblock/flow-swift/blob/main/Sources/Error/FVMError.swift"
    click Flow_Error "https://github.com/outblock/flow-swift/blob/main/Sources/Error/FlowError.swift"
    click Logging_Module "https://github.com/outblock/flow-swift/blob/main/Sources/Log/FlowLogger.swift"
    click Array_Ext "https://github.com/outblock/flow-swift/blob/main/Sources/Extension/Array.swift"
    click Data_Ext "https://github.com/outblock/flow-swift/blob/main/Sources/Extension/Data.swift"
    click Double_Ext "https://github.com/outblock/flow-swift/blob/main/Sources/Extension/Double.swift"
    click Publisher_Async "https://github.com/outblock/flow-swift/blob/main/Sources/Extension/Publisher+Async.swift"
    click String_Ext "https://github.com/outblock/flow-swift/blob/main/Sources/Extension/String.swift"
    click URLSession_Async "https://github.com/outblock/flow-swift/blob/main/Sources/Extension/URLSession+Async.swift"
    click RLP_Module "https://github.com/outblock/flow-swift/blob/main/Sources/RLP/RLP.swift"
    click Testing_Suite "https://github.com/outblock/flow-swift/tree/main/Tests"
```
