Skip to content

feat: implement secure multi-merchant HODL invoice escrow and automated settlement system#217

Open
GautamBytes wants to merge 7 commits intoshopstr-eng:mainfrom
GautamBytes:feat/nwc-hold-invoices
Open

feat: implement secure multi-merchant HODL invoice escrow and automated settlement system#217
GautamBytes wants to merge 7 commits intoshopstr-eng:mainfrom
GautamBytes:feat/nwc-hold-invoices

Conversation

@GautamBytes
Copy link
Contributor

Secure Multi-Merchant HODL Escrow System

🚀Summary

This PR implements a robust, trust-minimized HODL Invoice system for Shopstr, enabling secure escrow-style payments for physical goods. It replaces the legacy "Direct Zap" flow with a conditional "Handshake" protocol (Order Request → HODL Invoice → Automated Settlement) using NIP-17 DMs and NIP-47 (NWC). This architecture ensures funds are only moved after inventory is verified by the merchant's automated agent.

Key Features

Secure HODL Handshake

  • Implements the full NIP-17 handshake where buyer funds are locked in their own wallet via Alby's makeHoldInvoice.
  • Funds are only settled once the merchant's backend bot confirms stock and verifies shipping details.

Multi-Merchant Cart Support

  • Logic automatically groups products by merchant during checkout.
  • Sends individual encrypted DMs to each seller and manages multiple simultaneous HODL payment loops in a single UI session.

Automated Settlement Bot

  • A serverless-compatible background worker polls pending invoice states and settles funds once the "held" status is confirmed.
  • Built-in network resilience: Uses a Retry Queue (failed_settlements table) to handle temporary relay or API downtime.

Transactional Integrity & Security

  • Uses PostgreSQL FOR UPDATE row-level locking for inventory reservations to prevent double-booking.
  • All merchant credentials (NWC strings and bot keys) are AES-encrypted server-side.
  • Ephemeral bot keys are wiped from React memory immediately after syncing to the server.

🛠 Architectural Changes

  • Database: Added hodl_invoices, inventory_reservations, and failed_settlements tables. Automatic schema migration is handled in db-service.ts.
  • SDK: Upgraded to Alby SDK v7.0.0 to support HODL methods.
  • APIs: Introduced:
    • /api/inventory/check (bulk pre-flight check)
    • /api/merchant/save-connection (secure key sync)
    • /api/settlement/run (CRON heartbeat)

@vercel
Copy link

vercel bot commented Dec 25, 2025

Someone is attempting to deploy a commit to the shopstr-eng Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments