Skip to content

aaravjj2/HashPledge

Repository files navigation

HashPledge

PoW-Aligned Bitcoin Preconfirmation Receipts with Cryptographic Commitments

HashPledge provides verifiable preconfirmation receipts for Bitcoin transactions before they are mined. A miner service produces a signed secp256k1 commitment to include a specific transaction within a validity window, anchored to observed chain state. The system demonstrates both success (fulfilled) and failure (violated + slashing) paths end-to-end on Bitcoin regtest.


Quick Start

Prerequisites

  • Docker & Docker Compose (v2+)
  • bash, curl, jq (for demo scripts)

One Command Start

make up

This starts:

Service URL
Bitcoin Core (regtest) localhost:18443
Backend API (FastAPI) http://localhost:8000
Frontend UI (React) http://localhost:5173
API Docs (Swagger) http://localhost:8000/docs

Run the Demo

# Run both success and failure scenarios
make demo

# Or individually
make demo-success
make demo-failure

Stop Everything

make down

What It Does

Core Features

MILESTONE 1: Equivocation Detection
Detects when a miner issues multiple conflicting receipts (double-pledging). Uses UTXO fingerprinting (SHA256 of sorted input outpoints) to identify conflicts. Both conflicting receipts are marked EQUIVOCATED and bonds are slashed.

MILESTONE 2: Reorg Handling
Detects when a receipt's anchor block is orphaned due to chain reorganization. Validates anchor via getblockhash(anchor_height) comparison. If the anchor is reorged out, the receipt is marked ANCHOR_REORGED and the bond is neutralized (released, not slashed) — because this isn't the miner's fault.

MILESTONE 3: Bond Capacity
Models finite bonding capacity (e.g., 100k sats from Lightning channels or fidelity bonds). Tracks available = capacity - reserved - slashed. When capacity is exhausted, new receipt requests are refused with HTTP 409 INSUFFICIENT_BOND (machine-readable error).

Demo Scenarios

5 Interactive Scenarios:

  1. Success: Receipt issued, TX mined in window → FULFILLED, bond released
  2. Failure: Receipt issued, TX not broadcast → VIOLATED, bond slashed
  3. Equivocation: Two receipts for conflicting TXs → EQUIVOCATED, both bonds slashed
  4. Reorg: Anchor block invalidated → ANCHOR_REORGED, bond neutralized
  5. Capacity: Issue receipts until exhaustion → HTTP 409 refusal

Success Path

  1. User creates a transaction intent (unsigned raw tx)
  2. Miner issues a preconfirmation receipt — a signed commitment to include the tx within N blocks
  3. Miner broadcasts the tx; blocks are mined
  4. Receipt verification confirms FULFILLED — bond is released

Failure Path

  1. User creates an intent
  2. Miner issues a receipt but does not broadcast (simulated noncompliance)
  3. Blocks are mined past the validity window
  4. Receipt verification detects VIOLATED — miner bond is slashed

Repository Structure

├── docker-compose.yml          # Full stack orchestration
├── Makefile                    # Build/run/demo/test commands
├── scripts/                    # Deterministic demo scripts
│   ├── demo_success.sh
│   ├── demo_failure.sh
│   └── demo_all.sh
├── docs/                       # Protocol & architecture docs
│   ├── PROTOCOL.md
│   ├── THREAT_MODEL.md
│   ├── ARCHITECTURE.md
│   └── DEVPOST.md
├── backend/                    # FastAPI Python backend
│   ├── Dockerfile
│   ├── pyproject.toml
│   └── app/
│       ├── main.py
│       ├── core/               # Config, logging
│       ├── db/                 # SQLite models & session
│       ├── bitcoin/            # RPC client & wallet mgmt
│       ├── preconfirm/         # Crypto, encoding, service
│       └── api/                # Route modules
├── frontend/                   # Vite + React + TypeScript
│   ├── Dockerfile
│   └── src/
│       ├── api/                # Typed fetch wrappers
│       ├── components/         # UI panels
│       └── models/             # TypeScript types
└── tests/                      # Backend pytest suite

Key Commands

Command Description
make up Build and start all services
make down Stop and remove all containers + volumes
make demo Run success + failure demo scripts
make test Run ALL tests (TypeScript → Backend with Dockerized bitcoind → Playwright E2E)
make lint Run ruff + black + mypy
make logs Tail all service logs
make proof MILESTONE=<name> Generate proof pack with test results and evidence

How to Verify

Test Everything

# Runs full constitutional test suite:
# 1. TypeScript compilation (tsc --noEmit)
# 2. Backend tests with Dockerized bitcoind (pytest)
# 3. Frontend E2E tests via Playwright MCP wrapper
make test

Expected output: tsc --noEmit → 0 errors, pytest → failed=0 skipped=0, Playwright → failed=0 skipped=0 retries=0

Generate Proof Pack

# Creates artifacts/proofpacks/<milestone>_YYYYMMDD_HHMMSS/ with:
# - Test results (JUnit XML + Playwright JSON + HTML reports)
# - Screenshots, videos, traces
# - MANIFEST.md with test statistics table
# - Integration harness evidence (bitcoind logs)
make proof MILESTONE=integration-zero-skip

Constitutional Enforcement

# Run enforcement gates (automatically run by make test):
bash scripts/ci/enforce_constitution.sh

Enforces 6 constitutional rules:

  1. Zero Skipped Tests - All tests run with Dockerized dependencies
  2. Zero Retries - Playwright retries disabled
  3. Data-TestID Only - No text-based selectors
  4. Playwright-Only Frontend Testing - No vitest/jest in gates
  5. Determinism Proven - Fixed clock produces identical outputs
  6. E2E on Vite Preview Only - Tests run against production builds

Receipt Verification (CLI)

# Save a receipt to a file, then verify offline:
cd backend
python -m app.verify --receipt path/to/receipt.json

Troubleshooting

Issue Fix
Services won't start Run make down then make up
Bitcoin RPC not ready Wait 10s; backend retries automatically
Port conflict Change ports in docker-compose.yml
Demo script fails Ensure jq is installed: apt install jq

Tech Stack

  • Bitcoin Core 24 — regtest mode, JSON-RPC
  • FastAPI (Python 3.11) — async API server
  • SQLite — persistence for receipts, intents, miner ledger
  • secp256k1 — ECDSA signatures via coincurve
  • React 18 + TypeScript + Vite — frontend UI
  • Docker Compose — full orchestration

License

MIT — Hackathon prototype. Not for production use with real funds.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors