Node.js/TypeScript library to extract and validate bank proof data (chave + PIN) from manual input or PDF/Image with OCR fallback. Extensible via providers, stateless, and ready for backend use.
- Manual validation and PDF/Image extraction with OCR fallback
- Provider-based parsing and validation (
validate(chave, pin)) - Normalized result:
{ status: "CONFIRMED" | "INVALID" | "ERROR", provider, message? } - Timeout/error handling; no sensitive data persisted
npm install bankproofRequires Node.js >= 18. OCR needs Tesseract runtime (tesseract.js expects eng.traineddata available on the host).
import { BankProof, baiProvider, createConsoleLogger } from "bankproof";
import { readFileSync } from "fs";
const logger = createConsoleLogger("info");
// Manual input
const result = await BankProof.validate({ chave: "414979709", pin: "86612413" }, baiProvider, { logger });
// PDF / Image
const file = readFileSync("receipt.pdf");
const pdfResult = await BankProof.extractAndValidate(file, baiProvider, { logger });BankProof.validate({ chave, pin }, provider, options?)BankProof.extractAndValidate(file, provider, options?)
options:
timeoutMs?: numbersignal?: AbortSignallanguage?: string(OCR)logger?: Loggerextract?: { pdfText?: (buf: Buffer) => Promise<string>; ocrText?: (buf: Buffer, language?: string) => Promise<string>; }
interface Provider {
name: string;
patterns: { chave?: RegExp; pin?: RegExp; [key: string]: RegExp | undefined };
validate(
chave: string,
pin: string,
context?: { timeoutMs?: number; signal?: AbortSignal; logger?: Logger }
): Promise<{
status: "CONFIRMED" | "INVALID" | "ERROR";
provider: string;
message?: string;
raw?: unknown;
}>;
}Included: baiProvider (encodes inputs, posts to the official endpoint with timeout/error handling).
import { Provider } from "bankproof";
import { request } from "undici";
export const myBank: Provider = {
name: "MYBANK",
patterns: {
chave: /\b\d{10}\b/,
pin: /\b\d{6}\b/
},
async validate(chave, pin, ctx) {
const payload = { chave, pin };
try {
const { body } = await request("https://mybank/validate", {
method: "POST",
body: JSON.stringify(payload),
headers: { "Content-Type": "application/json" },
bodyTimeout: ctx?.timeoutMs
});
const res = await body.json();
const ok = res?.status === "ok";
return { status: ok ? "CONFIRMED" : "INVALID", provider: "MYBANK", raw: res };
} catch (error) {
return { status: "ERROR", provider: "MYBANK", message: "TEMPORARY_FAILURE", raw: error };
}
}
};Register the provider and pass it to BankProof.validate or BankProof.extractAndValidate.
- Server:
examples/server.ts(Express + Multer). Choose provider via request (defaults to BAI). - Frontend:
examples/public/index.html— simple page (English) to test manual input and PDF/Image upload.
Run locally:
npx ts-node examples/server.ts
# open http://localhost:3001- Keep core stateless and provider-agnostic.
- Add providers by implementing the contract; register in the demo only if you want it available there.
- Use the logger interface; avoid raw console logs in core/providers.
- Add tests (Vitest) for parsing and provider behaviors; mock external calls.
- In-memory only; nothing persisted.
- Configure
timeoutMsto avoid hanging calls. - Mask sensitive values in logs; use logger levels appropriately.