-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
357 lines (304 loc) · 9.72 KB
/
Copy pathindex.js
File metadata and controls
357 lines (304 loc) · 9.72 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const express = require('express');
const crypto = require('crypto');
const { config, verifyConfig } = require('./config');
const { makeRpcCallBTC } = require('./utils/bitcoin-rpc');
const { execThunderCli } = require('./utils/thunder-cli');
const { execBitnamesCli } = require('./utils/bitnames-cli');
const app = express();
const SERVER_FEE_SATS = 1000
// Bitcoin-patched RPC interaction
// RPC to get Bitcoin balance
async function getBalanceBTC() {
try {
const info = await makeRpcCallBTC(config, 'getbalance');
console.debug("getbalance response: ", info);
return {
info
};
} catch (error) {
console.error('Failed to get BTC balance:', error);
throw error;
}
}
// RPC to get Bitcoin address
async function getAddressBTC() {
try {
const info = await makeRpcCallBTC(config, 'getnewaddress');
console.debug("getnewaddress response: ", info);
return {
info
};
} catch (error) {
console.error('Failed to get BTC address:', error);
throw error;
}
}
// RPC to send coins to Bitcoin address
async function sendToAddressBTC(address, amount) {
try {
const info = await makeRpcCallBTC(config, 'sendtoaddress', [address, amount]);
console.debug("sendtoaddress response: ", info);
return {
info
};
} catch (error) {
console.error('Failed to send to BTC address:', error);
throw error;
}
}
async function validateAddressBTC(address) {
try {
const info = await makeRpcCallBTC(config, 'validateaddress', [address]);
return { info };
} catch (error) {
console.error('Failed to validate BTC address:', error);
throw error;
}
}
// Thunder interaction
async function getAddressThunder() {
try {
console.log("Getting Thunder address via CLI");
const info = await execThunderCli(config, 'get-new-address');
return { info };
} catch (error) {
console.error('Failed to get Thunder address:', error);
throw error;
}
}
async function verifyThunderPayment(txid, address, amount) {
try {
// TODO
return true;
} catch (error) {
console.error('Payment could not be verified', error);
throw error;
}
}
// BitNames interaction
async function getAddressBitNames() {
try {
console.log("Getting BitNames address via CLI");
const info = await execBitnamesCli(config, 'get-new-address');
return { info };
} catch (error) {
console.error('Failed to get BitNames address:', error);
throw error;
}
}
async function verifyBitNamesPayment(txid, address, amount) {
try {
// TODO
return true;
} catch (error) {
console.error('Payment could not be verified', error);
throw error;
}
}
// The server
// Store withdrawal requests
const withdrawalRequests = [];
// Middleware for parsing JSON bodies
app.use(express.json());
// Basic route
app.get('/', (req, res) => {
res.json({ message: 'Server is running' });
});
// Get BTC balance endpoint
app.get('/balance', async (req, res) => {
try {
const balance = await getBalanceBTC();
res.json({
status: 'success',
data: balance
});
} catch (error) {
res.status(500).json({
status: 'error',
message: 'Failed to get balance',
error: error.message
});
}
});
// Handle withdrawal requests
app.post('/withdraw', async (req, res) => {
const { withdrawal_destination, withdrawal_amount, layer_2_chain_name } = req.body;
const missing_fields = [];
// Validate required fields
if (!withdrawal_destination) {
missing_fields.push('withdrawal_destination');
}
if (!withdrawal_amount) {
missing_fields.push('withdrawal_amount');
}
if (!layer_2_chain_name) {
missing_fields.push('layer_2_chain_name');
}
if (missing_fields.length > 0) {
return res.status(400).json({
error: 'Missing required fields: ' + missing_fields.join(', ')
});
}
// Validate mainchain destination and layer2 name are strings
if (typeof withdrawal_destination !== 'string' ||
typeof layer_2_chain_name !== 'string') {
return res.status(400).json({
error: 'withdrawal_destination and layer_2_chain_name must be strings'
});
}
// Validate withdrawal_amount is an integer
const amount = Number.parseFloat(withdrawal_amount);
if (Number.isNaN(amount)) {
return res.status(400).json({
error: 'withdrawal_amount must be a number'
});
}
if (amount <= 0) {
return res.status(400).json({
error: 'withdrawal_amount must be positive'
});
}
// max withdrawal amount is 10% of our mainchain balance
const balance = await getBalanceBTC();
const maxWithdrawalAmount = balance.info * 0.1;
if (amount > maxWithdrawalAmount) {
return res.status(400).json({
error: `withdrawal amount is above max: ${maxWithdrawalAmount.toString()}`
});
}
// validate withdrawal destination address
const addressValidation = await validateAddressBTC(withdrawal_destination);
if (!addressValidation.info.isvalid) {
const error = addressValidation.info.error ? `Invalid L1 BTC address: ${addressValidation.info.error}` : "Invalid L1 BTC address";
return res.status(400).json({ error });
}
// Check L2 chain name
if (layer_2_chain_name != "Thunder" && layer_2_chain_name != "BitNames") {
return res.status(400).json({
error: 'Invalid Layer 2 name'
});
}
// Generate a new L2 address for this payment
var server_l2_address = "";
if (layer_2_chain_name == "Thunder") {
server_l2_address = await getAddressThunder();
}
else
if (layer_2_chain_name == "BitNames") {
server_l2_address = await getAddressBitNames();
}
if (server_l2_address == "") {
return res.status(400).json({
error: 'Failed to generate L2 address'
});
}
// Generate a new bitcoin address for this request
const server_l1_address = await getAddressBTC();
// Create withdrawal request
const withdrawalRequest = {
withdrawal_destination,
withdrawal_amount: amount,
layer_2_chain_name,
server_l1_address,
server_l2_address,
server_fee_sats: SERVER_FEE_SATS,
timestamp: new Date().toISOString()
};
// Create hash of the request
const requestHash = crypto
.createHash('sha256')
.update(JSON.stringify(withdrawalRequest))
.digest('hex');
// Add hash to request object
withdrawalRequest.hash = requestHash;
// Track new withdrawal request
console.log("pushing new withdrawal request: ", withdrawalRequest);
withdrawalRequests.push(withdrawalRequest);
res.json({
status: 'success',
message: 'Withdrawal request received and stored',
data: withdrawalRequest,
});
});
// Handle paid notifications
app.post('/paid', async (req, res) => {
const { hash, txid } = req.body;
// Validate required fields
if (!hash || !txid) {
return res.status(400).json({
error: 'Both hash and txid are required'
});
}
// Validate types
if (typeof hash !== 'string' || typeof txid !== 'string') {
return res.status(400).json({
error: 'Both hash and txid must be strings'
});
}
// Find the withdrawal request with matching hash
const request = withdrawalRequests.find(req => req.hash === hash);
if (!request) {
return res.status(404).json({
error: 'No withdrawal request found with the provided hash'
});
}
// Verify L2 payment
if (request.layer_2_chain_name == "Thunder") {
if (!verifyThunderPayment(txid, request.server_l2_address, request.amount)) {
res.json({
status: 'error',
message: "Payment not verified",
data: request
});
}
}
else
if (request.layer_2_chain_name == "BitNames") {
if (!verifyBitNamesPayment(txid, request.server_l2_address, request.amount)) {
res.json({
status: 'error',
message: "Payment not verified",
data: request
});
}
}
else {
res.json({
status: 'error',
message: "Invalid L2 chain name",
data: request
});
}
// Update the request with txid of L2 payment and paid timestamp
request.txid = txid;
request.paid_at = new Date().toISOString();
// Send BTC to requester and complete the invoice
const payout_txid = await sendToAddressBTC(request.withdrawal_destination, request.withdrawal_amount);
res.json({
status: 'success',
message: payout_txid,
data: request
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});
// Verify CLI tools before starting server
async function startServer() {
try {
await verifyConfig(config);
// Start server
const PORT = process.env.PORT || 3333;
const HOST = process.env.HOST || 'localhost';
app.listen(PORT, HOST, () => {
console.log(`Server is running on ${HOST}:${PORT}`);
});
} catch (error) {
console.error('Failed to start server:', error.message);
process.exit(1);
}
}
// Replace the existing server start code at the bottom with:
startServer();