|
| 1 | +# BTC Integration for Mintlayer P2P Swap Board |
| 2 | + |
| 3 | +This document describes the Bitcoin (BTC) integration added to the swap board, enabling atomic swaps between Mintlayer tokens and native Bitcoin. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The BTC integration allows users to: |
| 8 | +- Create offers involving BTC (BTC → ML tokens or ML tokens → BTC) |
| 9 | +- Accept BTC offers by providing BTC credentials |
| 10 | +- Create BTC HTLCs for atomic swaps |
| 11 | +- Claim and refund BTC HTLCs |
| 12 | +- Track BTC transactions on blockchain explorers |
| 13 | + |
| 14 | +## Architecture |
| 15 | + |
| 16 | +### Wallet-Centric Design |
| 17 | +- **Web App**: Builds requests and manages UI/coordination |
| 18 | +- **Wallet Extension**: Handles all BTC cryptographic operations |
| 19 | +- **No Private Keys**: Web app never handles BTC private keys |
| 20 | + |
| 21 | +### Key Components |
| 22 | + |
| 23 | +#### 1. Database Schema (`prisma/schema.prisma`) |
| 24 | +**Offer Model Additions:** |
| 25 | +- `creatorBTCAddress`: Creator's BTC address |
| 26 | +- `creatorBTCPublicKey`: Creator's BTC public key for HTLC creation |
| 27 | + |
| 28 | +**Swap Model Additions:** |
| 29 | +- `takerBTCAddress`: Taker's BTC address |
| 30 | +- `takerBTCPublicKey`: Taker's BTC public key for HTLC creation |
| 31 | +- `btcHtlcAddress`: Generated BTC HTLC contract address |
| 32 | +- `btcRedeemScript`: BTC HTLC redeem script |
| 33 | +- `btcHtlcTxId`: BTC HTLC funding transaction ID |
| 34 | +- `btcHtlcTxHex`: BTC HTLC signed transaction hex |
| 35 | +- `btcClaimTxId`: BTC claim transaction ID |
| 36 | +- `btcClaimTxHex`: BTC claim signed transaction hex |
| 37 | +- `btcRefundTxId`: BTC refund transaction ID |
| 38 | +- `btcRefundTxHex`: BTC refund signed transaction hex |
| 39 | + |
| 40 | +#### 2. Type Definitions (`src/types/`) |
| 41 | +- `btc-wallet.ts`: Wallet interface definitions |
| 42 | +- `swap.ts`: Updated with BTC fields and new status types |
| 43 | + |
| 44 | +#### 3. BTC Utilities (`src/lib/btc-request-builder.ts`) |
| 45 | +- Amount conversion (BTC ↔ satoshis) |
| 46 | +- Address and public key validation |
| 47 | +- HTLC request builders |
| 48 | +- Explorer URL generators |
| 49 | + |
| 50 | +#### 4. API Endpoints |
| 51 | +- **POST /api/offers**: Validates BTC credentials for BTC offers |
| 52 | +- **POST /api/swaps**: Handles BTC credentials during offer acceptance |
| 53 | +- **POST /api/swaps/[id]**: Updates swaps with BTC transaction data |
| 54 | + |
| 55 | +#### 5. Frontend Components |
| 56 | +- **Create Offer**: Requests BTC credentials when BTC is involved |
| 57 | +- **Offers List**: Handles BTC credential exchange during acceptance |
| 58 | +- **Swap Detail**: Full BTC HTLC management interface |
| 59 | + |
| 60 | +## Swap Flow |
| 61 | + |
| 62 | +### ML → BTC Swap |
| 63 | +1. **Creator creates offer**: Provides BTC address + public key |
| 64 | +2. **Taker accepts**: Provides their BTC address + public key |
| 65 | +3. **Creator creates ML HTLC**: Standard Mintlayer HTLC |
| 66 | +4. **Taker creates BTC HTLC**: Using creator's public key as recipient |
| 67 | +5. **Creator claims BTC**: Uses secret to spend BTC HTLC |
| 68 | +6. **Taker claims ML**: Uses revealed secret to claim ML HTLC |
| 69 | + |
| 70 | +### BTC → ML Swap |
| 71 | +1. **Creator creates offer**: Provides BTC address + public key |
| 72 | +2. **Taker accepts**: Provides their BTC address + public key |
| 73 | +3. **Creator creates BTC HTLC**: Using taker's public key as recipient |
| 74 | +4. **Taker creates ML HTLC**: Standard Mintlayer HTLC |
| 75 | +5. **Taker claims BTC**: Uses secret to spend BTC HTLC |
| 76 | +6. **Creator claims ML**: Uses revealed secret to claim ML HTLC |
| 77 | + |
| 78 | +## Status Tracking |
| 79 | + |
| 80 | +New swap statuses: |
| 81 | +- `btc_htlc_created`: BTC HTLC has been created |
| 82 | +- `both_htlcs_created`: Both ML and BTC HTLCs exist |
| 83 | +- `btc_refunded`: BTC side was refunded |
| 84 | + |
| 85 | +## Security Considerations |
| 86 | + |
| 87 | +### Public Key Exchange |
| 88 | +- Public keys are required for HTLC script generation |
| 89 | +- Keys are stored in database (consider privacy implications) |
| 90 | +- Keys are visible to counterparty (required for HTLC creation) |
| 91 | + |
| 92 | +### Timelock Coordination |
| 93 | +- BTC timelock should be shorter than ML timelock |
| 94 | +- Ensures proper claim ordering for security |
| 95 | + |
| 96 | +### Atomic Guarantees |
| 97 | +- Same secret hash used for both chains |
| 98 | +- Standard HTLC atomic swap properties maintained |
| 99 | +- Manual refund available after timelock expiry |
| 100 | + |
| 101 | +## Testing |
| 102 | + |
| 103 | +Run the integration test: |
| 104 | +```bash |
| 105 | +node test-btc-integration.js |
| 106 | +``` |
| 107 | + |
| 108 | +## Development Status |
| 109 | + |
| 110 | +### ✅ Completed |
| 111 | +- Database schema with BTC fields |
| 112 | +- Type definitions and interfaces |
| 113 | +- BTC utility functions |
| 114 | +- API endpoint updates |
| 115 | +- Frontend BTC integration |
| 116 | +- Status tracking and UI |
| 117 | +- BTC wallet method implementations |
| 118 | +- BTC HTLC script generation |
| 119 | +- BTC transaction building and signing |
| 120 | +- BTC network integration |
| 121 | +- Secret extraction from BTC claims |
| 122 | + |
| 123 | +### 🧪 Testing Needed |
| 124 | +- End-to-end BTC swap testing |
| 125 | +- Error handling and edge cases |
| 126 | +- Network compatibility (testnet/mainnet) |
| 127 | +- Performance optimization |
| 128 | + |
| 129 | +## Support |
| 130 | + |
| 131 | +For questions about the BTC integration: |
| 132 | +- Check the test file for usage examples |
| 133 | +- Review the utility functions in `btc-request-builder.ts` |
| 134 | +- Examine the swap detail page for UI implementation |
| 135 | +- Test with the provided mock data structures |
0 commit comments