open-standards/openpay/crates/op-bin/src/lib.rs
Transaction Science 5ae7e4ef86 ingest wave E: 3 crates · op-bin + smart-byte-tendermint + eoc-safety
OpenPay (1 crate, 1801 LOC, 57 tests)
  op-bin              BIN/IIN database + range matching + card-network
                      classification. 8-digit normalized prefix Bin +
                      BinRange. Sorted-vec interval tree with O(log N) point
                      lookup + overlap rejection. CardNetwork classify()
                      with structural rules (Visa 4, MC 51-55/2221-2720,
                      Amex 34/37, Discover, JCB, Diners, UnionPay, RuPay,
                      Mir, Troy, Maestro, Elo). Static prefix tables +
                      build_default_tree(). ISO/IEC 7812-1 Annex B Luhn
                      (validate / is_valid / check_digit). Card-type +
                      issuer-country + Durbin regulated-debit. PCI-safe:
                      8-digit cap, no full-PAN APIs.

Smart Byte (1 crate, ~1500 LOC, 20 tests)
  smart-byte-tendermint  BFT consensus. Step enum + Round + propose/prevote/
                         precommit/commit FSM with voting-power tallies.
                         ValidatorSet with 2/3+1 quorum, 1/3+1 trust
                         threshold, round-robin proposer. Block / Header /
                         Commit / CommitSig with Blake3 hashing. Light client
                         (sequential + skipping). ABCI Application trait +
                         EchoApp. DuplicateVoteEvidence + LightClientAttackEvidence.
                         IBC ClientState + ConsensusState + light-client
                         update.

EOC (1 crate, 2084 LOC, 23 tests)
  eoc-safety          AI safety primitives. RegexSet over 16 prompt-injection
                      signatures. 13-family jailbreak detector (DAN/DUDE/
                      Grandma/STAN/AIM/dev-mode/etc.). Presidio-style PII
                      redactor with Luhn-validated CC. Toxicity / bias /
                      NSFW classifier traits + heuristic baselines (5-axis
                      bias, 4-category NSFW with hard-reject for minors).
                      Constitutional AI critique→revise loop. Composable
                      InputGuard / OutputGuard pipelines. JSON-Schema-subset
                      output validator. Token-bucket rate limiter +
                      abuse-window detection. Red-team replay harness.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-24 16:52:07 -04:00

77 lines
3 KiB
Rust

//! # `op-bin` — BIN/IIN database and card-network classification
//!
//! Maps the first 6-8 digits of a Primary Account Number (PAN) —
//! the **Bank Identification Number** (BIN), also called the
//! **Issuer Identification Number** (IIN) per ISO/IEC 7812 — to
//! the issuing card network, card type, country, and Durbin
//! regulated-debit status.
//!
//! ## ISO/IEC 7812
//!
//! As of 2022, ISO/IEC 7812 mandates **eight-digit IINs** for
//! newly-allocated ranges. Most legacy ranges remain six digits;
//! networks routinely subdivide a 6-digit prefix into 8-digit
//! sub-ranges. This crate represents every range as a half-open
//! interval `[low, high)` over the **first-8-digit prefix** of a
//! PAN, so the lookup path is uniform regardless of whether the
//! issuer was allocated a 6-, 7-, or 8-digit IIN.
//!
//! ## What the crate ships
//!
//! - [`Bin`] — a validated 6-to-8-digit numeric prefix.
//! - [`BinRange`] — half-open prefix interval annotated with
//! network, card type, issuer country, and Durbin flag.
//! - [`RangeTree`] — sorted interval-tree lookup; `O(log N)`
//! point queries over a few thousand ranges.
//! - [`CardNetwork`] + [`classify`](network::classify) — given a
//! `Bin`, classify the network by structural prefix rules from
//! `network_ranges` (Visa = `4xxxxx`, Mastercard 51-55 / 2221-2720,
//! Amex 34/37, Discover, JCB, Diners, UnionPay, RuPay, Maestro,
//! Elo, Mir, Troy).
//! - [`luhn::is_valid`] / [`luhn::check_digit`] — ISO/IEC 7812-1
//! Annex B check-digit computation.
//! - [`CardType`] — credit / debit / prepaid / charge.
//! - [`IssuerCountry`] — ISO 3166-1 alpha-2 wrapper.
//! - [`durbin::is_regulated`] — Federal Reserve Regulation II
//! merchant-side classification helper.
//!
//! ## PCI scope
//!
//! `op-bin` is deliberately **only** about the BIN. It never
//! takes, stores, or returns a full PAN. The API surface accepts
//! at most 8 digits anywhere a `Bin` is constructed; downstream
//! callers in the OpenPay reference stack hand us
//! `pan[..pan.len().min(8)]` and discard the remainder before the
//! BIN lookup runs.
//!
//! ## What the crate does NOT do
//!
//! - **No network lookups.** The static tables in
//! [`network_ranges`] are structural prefix rules drawn from
//! public ISO/IEC 7812 documentation and network developer
//! docs; they are not a substitute for a commercial BIN feed
//! (e.g. Bin Codes, BinList, FreeBINChecker) and they do not
//! resolve individual issuing banks.
//! - **No PAN handling.** See PCI note above.
//! - **No I/O.** All tables are compiled in; operators ship
//! updates by recompiling.
#![forbid(unsafe_code)]
#![warn(missing_docs)]
pub mod bin;
pub mod card_type;
pub mod durbin;
pub mod error;
pub mod issuer_country;
pub mod luhn;
pub mod network;
pub mod network_ranges;
pub mod range_tree;
pub use bin::{Bin, BinRange};
pub use card_type::CardType;
pub use error::{Error, Result};
pub use issuer_country::IssuerCountry;
pub use network::{classify, CardNetwork};
pub use range_tree::RangeTree;