Middleware Recipes

Reference snippets for implementing BB-402 server-side verification. Adapt these to your framework of choice.

Nonce Management

import crypto from 'crypto';

const nonces = new Map<string, number>();
const NONCE_TTL_MS = 60_000;

function generateNonce(): string {
  const nonce = crypto.randomBytes(16).toString('hex');
  nonces.set(nonce, Date.now());
  return nonce;
}

function validateNonce(nonce: string): boolean {
  const created = nonces.get(nonce);
  if (!created || Date.now() - created > NONCE_TTL_MS) {
    nonces.delete(nonce);
    return false;
  }
  nonces.delete(nonce); // single-use
  return true;
}

Signature Verification (Cosmos ADR-036)

Signature Verification (EVM EIP-191)

Ownership Check (BitBadges API)

Proof Decoding

402 Challenge Response

When no proof header is present, return a 402 with this shape:

The message field is a JSON string that the client signs and sends back.

Amount Patterns

Goal

mustOwnAmounts

Must own at least 1

{ start: '1', end: '1' }

Must NOT own (ban list)

{ start: '0', end: '0' }

Client: Handling a 402

Dependencies

Last updated