-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaywall.go
More file actions
328 lines (304 loc) · 10.4 KB
/
paywall.go
File metadata and controls
328 lines (304 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Package paywall implements a Bitcoin payment system for protecting web content
package paywall
import (
"context"
"crypto/rand"
"embed"
"encoding/hex"
"fmt"
"html/template"
"log"
"os"
"time"
"github.com/opd-ai/paywall/wallet"
)
// TemplateFS embeds the payment page HTML template
//
//go:embed templates/payment.html
var TemplateFS embed.FS
// QrcodeJS embeds the QR code generation JavaScript library
//
//go:embed static/qrcode.min.js
var QrcodeJs embed.FS
// Config defines the configuration options for initializing a Paywall
// All fields are required unless otherwise noted
type Config struct {
// PriceInBTC is the amount in Bitcoin required for access
PriceInBTC float64
// PriceInXMR is the amount in Monero required for access
PriceInXMR float64
// PaymentTimeout is the duration after which pending payments expire
PaymentTimeout time.Duration
// MinConfirmations is the required number of blockchain confirmations
MinConfirmations int
// TestNet determines whether to use Bitcoin testnet (true) or mainnet (false)
TestNet bool
// Store implements the payment persistence interface
Store PaymentStore
// XMRUser is the monero-rpc username
XMRUser string
// XMRPassword is the monero-rpc password
XMRPassword string
// XMRRPC is the monero-rpc URL
XMRRPC string
}
// Paywall manages Bitcoin payment processing and verification
// It generates payment addresses, tracks payment status, and validates transactions
// Related types: Config, Payment, PaymentStore, wallet.HDWallet
type Paywall struct {
// HDWallets generates unique Bitcoin or XMR addresses for payments
HDWallets map[wallet.WalletType]wallet.HDWallet
// Store persists payment information
Store PaymentStore
// prices is the required payment amount in crypto per wallet
prices map[wallet.WalletType]float64
// paymentTimeout is how long payments can remain pending
paymentTimeout time.Duration
// minConfirmations is required blockchain confirmations
minConfirmations int
// template is the parsed payment page HTML template
template *template.Template
// monitor is the blockchain monitoring service
monitor *CryptoChainMonitor
// ctx is the context for monitoring goroutine
ctx context.Context
// cancel is the context cancellation function
cancel context.CancelFunc
}
// NewPaywall creates and initializes a new Paywall instance
// Parameters:
// - config: Configuration options for the paywall
//
// Returns:
// - *Paywall: Initialized paywall instance
// - error: If initialization fails
//
// Errors:
// - If random seed generation fails
// - If HD wallet creation fails
// - If template parsing fails
//
// Related types: Config, Paywall
func NewPaywall(config Config) (*Paywall, error) {
// validate payment timeout
if config.PaymentTimeout <= 0 {
return nil, fmt.Errorf("payment timeout must be positive")
}
// validate payment amounts
if config.PriceInBTC <= 0 {
return nil, fmt.Errorf("PriceInBTC must be positive, got: %f", config.PriceInBTC)
}
// Validate XMR price if XMR configuration is provided
if (config.XMRUser != "" || config.XMRPassword != "" || config.XMRRPC != "") && config.PriceInXMR <= 0 {
return nil, fmt.Errorf("PriceInXMR must be positive, got: %f", config.PriceInXMR)
}
// Generate random seed for HD wallet
seed := make([]byte, 32)
if _, err := rand.Read(seed); err != nil {
return nil, fmt.Errorf("generate seed: %w", err)
}
hdWallet, err := wallet.NewBTCHDWallet(seed, config.TestNet, config.MinConfirmations)
if err != nil {
return nil, fmt.Errorf("create wallet: %w", err)
}
if config.XMRUser == "" {
config.XMRUser = os.Getenv("XMR_WALLET_USER")
}
// Use secure environment variable handling
if config.XMRPassword == "" {
pass, exists := os.LookupEnv("XMR_WALLET_PASS")
if !exists {
return nil, fmt.Errorf("XMR wallet password not provided")
}
config.XMRPassword = pass
}
if config.XMRRPC == "" {
config.XMRRPC = "http://127.0.0.1:18081"
}
// Add credential validation
if config.XMRUser != "" && len(config.XMRUser) < 3 {
return nil, fmt.Errorf("XMR RPC username must be at least 3 characters")
}
if config.XMRPassword != "" && len(config.XMRPassword) < 8 {
return nil, fmt.Errorf("XMR RPC password must be at least 8 characters")
}
xmrHdWallet, err := wallet.NewMoneroWallet(wallet.MoneroConfig{
RPCUser: config.XMRUser,
RPCURL: config.XMRRPC,
RPCPassword: config.XMRPassword,
}, config.MinConfirmations)
if err != nil {
log.Printf("WARNING: XMR wallet configuration was provided but wallet creation failed: %v", err)
log.Printf("Continuing with Bitcoin-only support. Please check your Monero RPC configuration.")
}
tmpl, err := template.ParseFS(TemplateFS, "templates/payment.html")
if err != nil {
return nil, fmt.Errorf("parse template: %w", err)
}
if config.MinConfirmations < 1 {
config.MinConfirmations = 1
}
// Validate payment amounts are positive
if config.PriceInBTC <= 0 {
return nil, fmt.Errorf("PriceInBTC must be positive, got: %f", config.PriceInBTC)
}
hdWallets := make(map[wallet.WalletType]wallet.HDWallet)
hdWallets[wallet.WalletType(hdWallet.Currency())] = hdWallet
if xmrHdWallet != nil {
hdWallets[wallet.WalletType(xmrHdWallet.Currency())] = xmrHdWallet
}
prices := make(map[wallet.WalletType]float64)
prices[wallet.WalletType(hdWallet.Currency())] = config.PriceInBTC
if xmrHdWallet != nil {
prices[wallet.WalletType(xmrHdWallet.Currency())] = config.PriceInXMR
}
// Create context with cancellation
pctx, pcancel := context.WithCancel(context.Background())
p := &Paywall{
HDWallets: hdWallets,
Store: config.Store,
prices: prices,
paymentTimeout: config.PaymentTimeout,
minConfirmations: config.MinConfirmations,
template: tmpl,
ctx: pctx,
cancel: pcancel,
}
// Initialize monitor
monitor := &CryptoChainMonitor{
paywall: p,
client: make(map[wallet.WalletType]CryptoClient),
}
monitor.client[wallet.Bitcoin] = hdWallets[wallet.Bitcoin]
if xmrHdWallet != nil {
monitor.client[wallet.Monero] = hdWallets[wallet.Monero]
}
p.monitor = monitor
p.monitor.Start(pctx)
return p, nil
}
func (p *Paywall) Close() {
p.cancel()
p.monitor.Close()
}
func (p *Paywall) btcWalletAddress() (string, error) {
return p.HDWallets[wallet.Bitcoin].GetAddress()
}
func (p *Paywall) xmrWalletAddress() (string, error) {
if _, ok := p.HDWallets[wallet.Monero]; !ok {
log.Printf("Warning: XMR wallet is not in use, your privacy is sub-optimal")
return "", nil
}
xmrAddress, err := p.HDWallets[wallet.Monero].GetAddress()
if err != nil {
return "", fmt.Errorf("failed to get XMR address: %w", err)
}
return xmrAddress, nil
}
func (p *Paywall) addressMap() (map[wallet.WalletType]string, error) {
btcAddress, err := p.btcWalletAddress()
if err != nil {
return nil, err
}
xmrAddress, err := p.xmrWalletAddress()
if err != nil {
return nil, err
}
addresses := make(map[wallet.WalletType]string)
addresses[wallet.Bitcoin] = btcAddress
if xmrAddress != "" {
addresses[wallet.Monero] = xmrAddress
}
return addresses, nil
}
// CreatePayment generates a new payment with addresses for all enabled cryptocurrencies
//
// Returns:
// - *Payment: New payment record with generated addresses and amounts
// - error: If address generation fails or random ID generation fails
//
// The method creates a unique payment with:
// - Cryptographically secure random payment ID
// - Bitcoin address (if enabled)
// - Monero address (if enabled)
// - Configured payment amounts for each currency
// - Expiration time based on paymentTimeout
// - Initial status of StatusPending
//
// Error handling:
// - Returns error if random ID generation fails
// - Returns error if any wallet address generation fails
// - Validates payment amounts against dust limits
//
// Related types: Payment, wallet.HDWallet, PaymentStatus
func (p *Paywall) CreatePayment() (*Payment, error) {
// Generate cryptographically secure payment ID
idBytes := make([]byte, 16)
if _, err := rand.Read(idBytes); err != nil {
return nil, fmt.Errorf("generate payment ID: %w", err)
}
paymentID := hex.EncodeToString(idBytes)
// Create payment record
payment := &Payment{
ID: paymentID,
Addresses: make(map[wallet.WalletType]string),
Amounts: make(map[wallet.WalletType]float64),
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(p.paymentTimeout),
Status: StatusPending,
Confirmations: 0,
}
// Generate addresses for all enabled wallets
// Track which wallets had addresses generated for rollback on failure
var generatedWallets []wallet.WalletType
for walletType, hdWallet := range p.HDWallets {
address, err := hdWallet.DeriveNextAddress()
if err != nil {
// Rollback any previously generated addresses
p.rollbackAddressGeneration(generatedWallets)
return nil, fmt.Errorf("generate %s address: %w", walletType, err)
}
payment.Addresses[walletType] = address
payment.Amounts[walletType] = p.prices[walletType]
generatedWallets = append(generatedWallets, walletType)
}
// Validate payment has at least one enabled currency
if len(payment.Addresses) == 0 {
return nil, fmt.Errorf("no wallets enabled for payment")
}
// Store the payment
if err := p.Store.CreatePayment(payment); err != nil {
// Rollback address generation on storage failure
p.rollbackAddressGeneration(generatedWallets)
return nil, fmt.Errorf("store payment: %w", err)
}
return payment, nil
}
// rollbackAddressGeneration decrements the address index for wallets that had addresses generated
// This is used to maintain atomic payment creation by rolling back on failures
func (p *Paywall) rollbackAddressGeneration(walletTypes []wallet.WalletType) {
for _, walletType := range walletTypes {
if hdWallet, exists := p.HDWallets[walletType]; exists {
// Call rollback method on each wallet that had an address generated
switch w := hdWallet.(type) {
case *wallet.BTCHDWallet:
w.RollbackLastAddress()
case *wallet.MoneroHDWallet:
w.RollbackLastAddress()
}
}
}
}
// generatePaymentID creates a random 16-byte hex-encoded payment identifier
// Returns:
// - string: A 32-character hexadecimal string
// - error: If random generation fails
//
// This is an internal helper function that uses crypto/rand for secure randomness
func generatePaymentID() (string, error) {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return "", fmt.Errorf("failed to generate secure random payment ID: %w", err)
}
return hex.EncodeToString(b), nil
}