forked from akordavid373/sealed-auction-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
304 lines (255 loc) · 8.29 KB
/
server.js
File metadata and controls
304 lines (255 loc) · 8.29 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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const helmet = require('helmet');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const { v4: uuidv4 } = require('uuid');
const { Server, Keypair, TransactionBuilder, Networks, BASE_FEE, Asset } = require('stellar-sdk');
const { StellarSealedBidAuction } = require('./contracts/StellarSealedBidAuction');
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
// Security middleware
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
app.use(limiter);
// In-memory storage (in production, use a proper database)
const auctions = new Map();
const bids = new Map();
const users = new Map();
// Auction class
class Auction {
constructor(id, title, description, startingBid, endTime, creator) {
this.id = id;
this.title = title;
this.description = description;
this.startingBid = startingBid;
this.currentHighestBid = startingBid;
this.endTime = endTime;
this.creator = creator;
this.status = 'active';
this.bids = [];
this.winner = null;
this.winningBid = null;
this.createdAt = new Date();
}
addBid(bid) {
this.bids.push(bid);
if (bid.amount > this.currentHighestBid) {
this.currentHighestBid = bid.amount;
}
}
close() {
this.status = 'closed';
if (this.bids.length > 0) {
const winningBid = this.bids.reduce((prev, current) =>
prev.amount > current.amount ? prev : current
);
this.winner = winningBid.bidderId;
this.winningBid = winningBid;
}
}
}
// Bid class
class Bid {
constructor(id, auctionId, bidderId, amount, encryptedBid) {
this.id = id;
this.auctionId = auctionId;
this.bidderId = bidderId;
this.amount = amount;
this.encryptedBid = encryptedBid;
this.timestamp = new Date();
this.revealed = false;
}
}
// User class
class User {
constructor(id, username, hashedPassword) {
this.id = id;
this.username = username;
this.hashedPassword = hashedPassword;
this.createdAt = new Date();
}
}
// Helper functions
function generateAuctionId() {
return uuidv4();
}
function encryptBid(bidAmount, secretKey) {
const algorithm = 'aes-256-cbc';
const key = crypto.scryptSync(secretKey, 'salt', 32);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(bidAmount.toString(), 'utf8', 'hex');
encrypted += cipher.final('hex');
return {
encrypted,
iv: iv.toString('hex')
};
}
function decryptBid(encryptedData, secretKey) {
const algorithm = 'aes-256-cbc';
const key = crypto.scryptSync(secretKey, 'salt', 32);
const iv = Buffer.from(encryptedData.iv, 'hex');
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(encryptedData.encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return parseFloat(decrypted);
}
// Routes
app.get('/api/auctions', (req, res) => {
const auctionList = Array.from(auctions.values()).map(auction => ({
id: auction.id,
title: auction.title,
description: auction.description,
startingBid: auction.startingBid,
currentHighestBid: auction.currentHighestBid,
endTime: auction.endTime,
status: auction.status,
bidCount: auction.bids.length,
creator: auction.creator
}));
res.json(auctionList);
});
app.post('/api/auctions', async (req, res) => {
try {
const { title, description, startingBid, endTime, userId } = req.body;
if (!title || !description || !startingBid || !endTime || !userId) {
return res.status(400).json({ error: 'Missing required fields' });
}
const auctionId = generateAuctionId();
const auction = new Auction(auctionId, title, description, startingBid, new Date(endTime), userId);
auctions.set(auctionId, auction);
io.emit('auctionCreated', auction);
res.status(201).json(auction);
} catch (error) {
res.status(500).json({ error: 'Failed to create auction' });
}
});
app.get('/api/auctions/:id', (req, res) => {
const auction = auctions.get(req.params.id);
if (!auction) {
return res.status(404).json({ error: 'Auction not found' });
}
res.json(auction);
});
app.post('/api/bids', async (req, res) => {
try {
const { auctionId, bidderId, amount, secretKey } = req.body;
if (!auctionId || !bidderId || amount === undefined || !secretKey) {
return res.status(400).json({ error: 'Missing required fields' });
}
const auction = auctions.get(auctionId);
if (!auction) {
return res.status(404).json({ error: 'Auction not found' });
}
if (auction.status !== 'active') {
return res.status(400).json({ error: 'Auction is not active' });
}
if (amount <= auction.startingBid) {
return res.status(400).json({ error: 'Bid must be higher than starting bid' });
}
const encryptedBid = encryptBid(amount, secretKey);
const bidId = uuidv4();
const bid = new Bid(bidId, auctionId, bidderId, amount, encryptedBid);
bids.set(bidId, bid);
auction.addBid(bid);
io.emit('bidPlaced', { auctionId, bidCount: auction.bids.length });
res.status(201).json({ message: 'Bid placed successfully', bidId });
} catch (error) {
res.status(500).json({ error: 'Failed to place bid' });
}
});
app.post('/api/auctions/:id/close', (req, res) => {
try {
const auction = auctions.get(req.params.id);
if (!auction) {
return res.status(404).json({ error: 'Auction not found' });
}
if (auction.status !== 'active') {
return res.status(400).json({ error: 'Auction is already closed' });
}
auction.close();
io.emit('auctionClosed', auction);
res.json(auction);
} catch (error) {
res.status(500).json({ error: 'Failed to close auction' });
}
});
app.post('/api/users/register', async (req, res) => {
try {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password required' });
}
const existingUser = Array.from(users.values()).find(u => u.username === username);
if (existingUser) {
return res.status(400).json({ error: 'Username already exists' });
}
const hashedPassword = await bcrypt.hash(password, 10);
const userId = uuidv4();
const user = new User(userId, username, hashedPassword);
users.set(userId, user);
res.status(201).json({ userId, username });
} catch (error) {
res.status(500).json({ error: 'Failed to register user' });
}
});
app.post('/api/users/login', async (req, res) => {
try {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password required' });
}
const user = Array.from(users.values()).find(u => u.username === username);
if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const isValid = await bcrypt.compare(password, user.hashedPassword);
if (!isValid) {
return res.status(401).json({ error: 'Invalid credentials' });
}
res.json({ userId: user.id, username: user.username });
} catch (error) {
res.status(500).json({ error: 'Failed to login' });
}
});
// Socket.io connections
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
socket.on('joinAuction', (auctionId) => {
socket.join(auctionId);
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
});
});
// Auto-close expired auctions
setInterval(() => {
const now = new Date();
for (const [id, auction] of auctions) {
if (auction.status === 'active' && new Date(auction.endTime) <= now) {
auction.close();
io.emit('auctionClosed', auction);
}
}
}, 60000); // Check every minute
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Sealed-Bid Auction server running on port ${PORT}`);
});