Building a Scratch-Card PIN Scanner in Zimbabwe
Resumo
Pixel shares a DEV Community article about building a camera-based scanner for Zimbabwean airtime scratch cards. It explains why common checksum methods cannot validate Econet’s random 17-digit PINs and why verification must happen on the network, offering practical lessons for OCR and app design.
DEV Community
I built a scratch-card scanner. The hard part wasn't reading the digits.
In Zimbabwe you buy airtime on a scratch card. You scratch off the foil, and underneath is a 17-digit PIN. Then you open your dialer and type 121 followed by all seventeen digits, followed by #. If you mistype one digit, the network rejects it and you try again. If you mistype it twice you start to wonder whether you're reading a 5 or a 6 through a scratched-up panel in bad light. People do this several times a week. So: point a camera at it, read the digits, build the string. A weekend project. It was not a weekend project, and almost none of the difficulty was in the OCR. First instinct: validate the PIN Before you send a PIN anywhere, you'd like to know it's plausible. Credit card numbers have the Luhn check digit. ISBNs have a weighted modulus. IMEIs use Luhn too. If airtime PINs carried anything similar, a misread would be caught instantly on the device, before a wasted attempt. I had four real Econet PINs, from four real cards I'd bought. I tested: Luhn, both digit orders Simple digit sum mod 7, 9, 10, 11 Mod 97, the IBAN scheme, on the first 15 digits against the last 2 Weighted schemes — alternating weights, positional weights, a few dozen combinations Nothing. Not a single scheme produced a consistent remainder across all four. Then I looked at what else is printed on the card. Econet cards carry three numbers: the 17-digit PIN under the panel, an 11-digit serial, and a 9-digit batch number. The serials across my four cards were sequential. The PINs were not correlated with them in any way I could find. That's the answer, and in hindsight it's obvious. The serial is the card's identity, printed openly, and it increments. The PIN is a secret that has to be unguessable — so it's random, and validity is a server-side lookup against a table. There is nothing in those 17 digits for a client to verify. Which means the app cannot tell a good PIN from a bad one. It can only try to read correctly in the first place. That one finding set the shape of everything else. OCR has no error correction Here's the thing that took me a while to internalise. A QR code can't be read wrong. It carries Reed–Solomon error correction, so a scanner either decodes the correct payload or fails outright. At the highest correction level roughly 30% of the symbol can be destroyed and you still get the right answer back. There is no state where a QR scanner confidently hands you the wrong string. OCR has no equivalent. A text recogniser handed a blurry 8 will cheerfully return 0 or B, with no signal that anything went wrong. The confidence scores, where the model exposes them at all, are not calibrated for "is this digit right". So for digits that are money, a single read is not evidence. It's a guess with good odds. Voting across frames The fix is to read the same card several times and require agreement. final top = eligible.first;
_votes[top.pin] = (_votes[top.pin] ?? 0) + 1;
final leader = _votes.entries.reduce((a, b) => a.value >= b.value ? a : b);
if (leader.value >= _votesToAccept) {
await _accept();
}
Three agreeing frames before the PIN is accepted. On a clean card they agree immediately and it feels instant. On a worn one it takes a second or two, and that second is the whole point. Two refinements worth mentioning. One vote per frame. Early on I counted every candidate the extractor found, which meant the 11-digit serial number printed next to the panel accumulated votes alongside the PIN. On a hard-to-read card the serial could reach the threshold first. Now only the top-ranked candidate in each frame gets a vote, and anything whose length the carrier doesn't issue can't vote at all. Early accept, strictly. Three votes earn their keep on a worn card where frames disagree. On a clean card the third frame confirms something nothing ever disputed — a third of the scan spent re-reading a settled question. So two agreeing frames is enough, but only when there has been exactly one distinct reading in the entire scan and its length is ...

Powered by Readybot.io