|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// Mock x402 paywall server for the x402buyer demo. |
| 4 | +// |
| 5 | +// Endpoints: |
| 6 | +// |
| 7 | +// GET /api/premium-data |
| 8 | +// - No X-Payment header → 402 with payment terms |
| 9 | +// - X-Payment header present → 200 with premium data |
| 10 | +// |
| 11 | +// Payment terms are binary-encoded in the response body: |
| 12 | +// |
| 13 | +// [amount:8 LE][recipient_len:4 LE][recipient][memo_len:4 LE][memo] |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/binary" |
| 18 | + "fmt" |
| 19 | + "log" |
| 20 | + "math/rand/v2" |
| 21 | + "net/http" |
| 22 | + "os" |
| 23 | + "os/signal" |
| 24 | + "syscall" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + paymentAmount int64 = 100_000 // 0.10 currency units (100,000 microcents) |
| 30 | + paymentRecipient string = "paywall-provider" |
| 31 | + paymentMemo string = "premium-data-access" |
| 32 | +) |
| 33 | + |
| 34 | +func main() { |
| 35 | + addr := ":8402" |
| 36 | + if envAddr := os.Getenv("PAYWALL_ADDR"); envAddr != "" { |
| 37 | + addr = envAddr |
| 38 | + } |
| 39 | + |
| 40 | + mux := http.NewServeMux() |
| 41 | + mux.HandleFunc("/api/premium-data", handlePremiumData) |
| 42 | + mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { |
| 43 | + w.WriteHeader(http.StatusOK) |
| 44 | + fmt.Fprint(w, "ok") |
| 45 | + }) |
| 46 | + |
| 47 | + srv := &http.Server{ |
| 48 | + Addr: addr, |
| 49 | + Handler: mux, |
| 50 | + ReadHeaderTimeout: 5 * time.Second, |
| 51 | + } |
| 52 | + |
| 53 | + // Graceful shutdown on SIGINT/SIGTERM. |
| 54 | + done := make(chan os.Signal, 1) |
| 55 | + signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) |
| 56 | + |
| 57 | + go func() { |
| 58 | + log.Printf("[paywall] Listening on %s", addr) |
| 59 | + if err := srv.ListenAndServe(); err != http.ErrServerClosed { |
| 60 | + log.Fatalf("[paywall] Server error: %v", err) |
| 61 | + } |
| 62 | + }() |
| 63 | + |
| 64 | + <-done |
| 65 | + log.Println("[paywall] Shutting down...") |
| 66 | + srv.Close() |
| 67 | +} |
| 68 | + |
| 69 | +func handlePremiumData(w http.ResponseWriter, r *http.Request) { |
| 70 | + payment := r.Header.Get("X-Payment") |
| 71 | + |
| 72 | + if payment == "" { |
| 73 | + // No payment — return 402 with payment terms. |
| 74 | + log.Printf("[paywall] 402 → %s (no payment header)", r.RemoteAddr) |
| 75 | + w.Header().Set("Content-Type", "application/octet-stream") |
| 76 | + w.WriteHeader(http.StatusPaymentRequired) |
| 77 | + w.Write(encodePaymentTerms(paymentAmount, paymentRecipient, paymentMemo)) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + // Payment present — return premium data. |
| 82 | + log.Printf("[paywall] 200 → %s (payment received, %d bytes)", r.RemoteAddr, len(payment)) |
| 83 | + w.Header().Set("Content-Type", "application/octet-stream") |
| 84 | + w.WriteHeader(http.StatusOK) |
| 85 | + |
| 86 | + // Simulate premium data: a randomized market insight. |
| 87 | + insights := []string{ |
| 88 | + "BTC dominance rising to 58.3%, altcoin rotation expected", |
| 89 | + "ETH gas fees averaging 12 gwei, L2 adoption accelerating", |
| 90 | + "DeFi TVL crossed $95B, new ATH since 2022", |
| 91 | + "Stablecoin supply expanding, USDC market cap up 15% MoM", |
| 92 | + "On-chain whale activity: 3 wallets accumulated 12,000 BTC this week", |
| 93 | + } |
| 94 | + insight := insights[rand.IntN(len(insights))] |
| 95 | + fmt.Fprintf(w, `{"insight":"%s","timestamp":%d,"tier":"premium"}`, insight, time.Now().Unix()) |
| 96 | +} |
| 97 | + |
| 98 | +// encodePaymentTerms encodes payment parameters for the 402 response body. |
| 99 | +// Format: [amount:8 LE][recipient_len:4 LE][recipient][memo_len:4 LE][memo] |
| 100 | +func encodePaymentTerms(amount int64, recipient, memo string) []byte { |
| 101 | + size := 8 + 4 + len(recipient) + 4 + len(memo) |
| 102 | + buf := make([]byte, size) |
| 103 | + off := 0 |
| 104 | + binary.LittleEndian.PutUint64(buf[off:], uint64(amount)) |
| 105 | + off += 8 |
| 106 | + binary.LittleEndian.PutUint32(buf[off:], uint32(len(recipient))) |
| 107 | + off += 4 |
| 108 | + copy(buf[off:], recipient) |
| 109 | + off += len(recipient) |
| 110 | + binary.LittleEndian.PutUint32(buf[off:], uint32(len(memo))) |
| 111 | + off += 4 |
| 112 | + copy(buf[off:], memo) |
| 113 | + return buf |
| 114 | +} |
0 commit comments