OfflinePay
A simulator of an offline payment network. A signed, encrypted payment hops across nearby phones to one with internet, and the backend settles it exactly once. Educational only: no real money, banks or UPI.
Extends an educational demo by another developer (perryvegehan/UPI_Without_Internet). The signed packets, replay and failure handling, dashboard, ML preview and tests are my work; the repository documents the split.
- virtual phones relaying packets by gossip
- 5
- delivery outcomes: settled, rejected, duplicate, invalid
- 4
- held-out recall of the advisory risk model, on synthetic data only
- 28.6%
- Java 17
- Spring Boot 3.3
- Spring Data JPA
- H2
- JavaScript
- scikit-learn
- JUnit 5
- Docker

The problem
If a sender is offline, the payment has to be carried by untrusted phones and may arrive late, repeatedly, or through several bridges at the same moment. The backend must keep it confidential, check it really came from the sender, reject stale or forged copies, and never debit twice.
Pipeline
- Offline sender
- Encrypt and sign
- Gossip across phones
- Bridge uploads
- Verify, then claim the hash
- Settle in one transaction
How it works
Packets use hybrid encryption (RSA-OAEP with AES-256-GCM) and a mandatory Ed25519 sender signature, so relays only ever see opaque ciphertext and any bit flip fails authentication.
Five in-process virtual phones form a mesh. Each gossip round copies packets between connected phones; only the bridge has internet by default.
The backend decrypts, verifies the signature, checks a freshness window (24 hours old, 5 minutes ahead), then claims a SHA-256 hash of the ciphertext backed by a unique database index.
Debit, credit and ledger row commit in one transaction with optimistic locking. Insufficient funds produce a REJECTED ledger row with balances unchanged.
A small logistic-regression model, trained on synthetic data, shows an advisory fraud-risk preview. It has no path into settlement.
The decision
- I chose
- Verify the sender's signature before claiming the packet's duplicate key.
- Instead of
- Claiming the key first and verifying afterwards.
- Because
- Otherwise a forged copy that arrives first could claim the hash and block the real payment from ever settling.
What broke, and the fix
If a settlement failed to commit, its duplicate claim would make every redelivery look like a duplicate.
A failed settlement releases its claim, so a redelivered copy can settle.
Several bridges can upload the same payment at the same moment.
Settlement is serialized and accounts use optimistic locking. Concurrent copies settle exactly once.
The risk model is easy to over-read.
It is labelled advisory, trained on synthetic data, and its 28.6% held-out recall is shown in the UI.
What I would do next
- Give each visitor their own sandbox; today the hosted demo is one shared instance.
- Persist keys and the ledger, with an explicit recovery policy.
- Move the gossip mesh onto a real transport, and train the risk model on real evaluation data.
- Use database-level locking, so more than one instance can settle safely.