Live Website & Interledger Studio: codesphere-u.github.io/flowledger | Docs: Getting Started · Rails, ISO 20022 & Aegis G6
![]()
![]()
![]()
FlowLedger is a Pan-African & Global Interledger Engine, Multi-Rail Payment Orchestrator, and Immutable Double-Entry Reconciliation System built in Python. It models mobile money and card collection workflows (Safaricom M-Pesa Daraja 2.0, MTN MoMo, Airtel Money, Wave, Orange Money), cross-border FX corridors, ISO 20022 messaging (pacs.008, pacs.002, camt.053), offline-first cryptographic vouchers, SQLite WAL persistence, and RFC 6962 Merkle audit trees across 12 currencies with strict mathematical accounting invariants.
-
Immutable Double-Entry Accounting (
flowledger/ledger/)- Five canonical account classes (
ASSET,LIABILITY,REVENUE,EXPENSE,EQUITY) tracked strictly in integer minor units — zero IEEE-754 floating-point rounding bugs. - Every state-changing financial event records a balanced
JournalEntrycontaining two or moreLedgerPostinglegs where$\sum \text{Debits} \equiv \sum \text{Credits}$ is enforced prior to commit. - Tamper-evident cryptographic audit chain: every entry computes
entry_hash = sha256(prev_hash + canonical_payload)starting fromGENESIS_HASH("0" * 64).
- Five canonical account classes (
-
Deterministic Payment State Machine (
flowledger/payments/state_machine.py)- Formal lifecycle graph:
INITIATED$\rightarrow$ PENDING_PROVIDER$\rightarrow$ AUTHORIZED$\rightarrow$ SETTLED|FAILED|REVERSED|DISPUTED. - Safely handles out-of-order webhooks (e.g.,
SETTLEDcallback arriving beforeAUTHORIZEDcallback automatically records the implicit authorization leg and absorbs the delayedAUTHORIZEDwebhook as an idempotent no-op). - Prevents illegal transitions such as
FAILED$\rightarrow$ SETTLEDunless an explicit reconciliation exception override is authorized.
- Formal lifecycle graph:
-
Idempotency & Request Fingerprinting (
flowledger/payments/idempotency.py)- Binds each
idempotency_keyto a canonical SHA-256 request payload digest. - Duplicate retries with an identical payload deterministically replay the cached response; reusing an
idempotency_keywith a mutated payload raisesIdempotencyConflictError(HTTP 409 Conflict).
- Binds each
-
Cryptographic Webhook Verification & Sandbox Providers (
flowledger/providers/)- Constant-time HMAC-SHA256 verification (
X-Signature,X-Timestamp) with configurable replay-window enforcement and optional nonce deduplication. - Includes
MtnMomoSandboxAdapterandAirtelMoneySandboxAdapterwith realistic fee tiers, exponential backoff with deterministic jitter, and a Failure Injection Simulator (DUPLICATE_WEBHOOK,OUT_OF_ORDER_WEBHOOK,PROVIDER_TIMEOUT_RETRY,FEE_MISMATCH,MISSING_IN_LEDGER,MISSING_AT_PROVIDER,AMOUNT_MISMATCH,CUSTOMER_REVERSAL).
- Constant-time HMAC-SHA256 verification (
-
Automated 3-Way Reconciliation Engine (
flowledger/reconciliation/engine.py)- Continuously reconciles (1) Internal Payment Intent Orders, (2) Double-Entry Ledger Postings, and (3) External Provider Settlement Statements (CSV/JSON).
- Classifies every transaction into
MATCHED,MISSING_IN_LEDGER,MISSING_AT_PROVIDER,AMOUNT_MISMATCH,FEE_DISCREPANCY, orSTALE_UNSETTLED, and generates balanced Adjustment Journal Proposals.
- Pan-African rails: Safaricom M-Pesa Daraja (STK Push, C2B, B2C) payload builders/parsers; Wave, Orange Money, MTN GH/RW, Tigo TZ profiles; new currencies
GHS ZAR TZS RWF XOF XAF USDC EURC. - ISO 20022
pacs.008/pacs.002generate + parse alongside CAMT.053. - Cross-border FX: rate-locked, single-use quotes posted as balanced journals with spread revenue.
- Offline vouchers: HMAC-signed, replay- and nonce-protected, bulk settlement queue.
- Compliance: fuzzy sanctions/PEP screening and structuring/velocity detection.
- Durability & audit: SQLite WAL store with hash-verified restore; RFC 6962 Merkle inclusion proofs.
- MCP: 12 tools for AI agents.
Provider modules build and parse payloads offline; they do not call live APIs. See Limitations for exactly what is and isn't production-ready.
flowchart LR
Client["Merchant / API Client"] -->|"Idempotency-Key + Payload"| API["FastAPI / Orchestrator"]
API -->|"SHA-256 Fingerprint"| Idem["Idempotency Store"]
API -->|"Validate Transition"| SM["Payment State Machine"]
SM -->|"Balanced Postings"| Ledger["Immutable Double-Entry Ledger"]
Ledger -->|"sha256(prev_hash + payload)"| Chain["Cryptographic Audit Chain"]
Providers["MTN MoMo / Airtel Sandbox"] -->|"X-Signature + X-Timestamp"| WH["HMAC Webhook Verifier"]
WH --> SM
Providers -->|"Settlement CSV / JSON"| Recon["3-Way Reconciliation Engine"]
Ledger --> Recon
API --> Recon
Recon -->|"Discrepancy Detection"| Proposals["Balanced Adjustment Proposals"]
pip install "git+https://github.com/codesphere-u/flowledger.git@v2.0.0"
# PyPI release (pip install flowledger) is configured; see .github/workflows/publish.yml
python -c "import flowledger; print(flowledger.__version__)"uv run --with fastapi --with httpx --with pydantic python -m flowledger.cli demouv run --with fastapi --with httpx --with pydantic python -m flowledger.cli verify-invariantsuv run --with fastapi --with uvicorn --with httpx --with pydantic python -m flowledger.cli serve --port 8095Open http://127.0.0.1:8095 to inspect live double-entry accounts, inject webhook anomalies, and run 3-way reconciliation in real time.
uv run --with pytest --with fastapi --with httpx --with pydantic pytest -v- Architecture & Data Flow (v2.0)
- v2.0 Architecture Additions
- ADR-001: Integer Minor Units & Double-Entry Invariants
- ADR-002: Cross-Border FX Corridors via Two-Journal Settlement
- ADR-003: Offline Cryptographic Vouchers & Replay-Safe Settlement Queue
- ADR-004: ISO 20022 Messaging Subsystem and SQLite WAL Ledger Durability
- Testing & Verification Guide (94 Tests)
- Operational Scope & Known Limitations
- Security Policy
Released under the MIT License.