How outblock/flow-swift works
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.