forked from RadiantRanger42/MuonPaySDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.js
More file actions
320 lines (271 loc) · 8.59 KB
/
Copy pathdoc.js
File metadata and controls
320 lines (271 loc) · 8.59 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
import getAddressUTXOs from "../../utils/getUtxos.js";
import broadcastTransaction from "../../utils/broadcast_trans.js";
import validateRadiantAddress from "../../utils/addr_validity.js";
import generateAddresses from "../../utils/gen_new_addr.js";
import getConfirmedBal from "../../utils/get_confimed_bal.js";
import Secrets from "../../config/secrets.js";
import pkg from "@chainbow/radiantjs";
import db from "../../config/databse.js";
import bitcoin from "bitcoinjs-lib";
import getTransactionHistory from "../../utils/get_trans_history.js";
import WebSocket from "ws";
import bitcoin from "bitcoinjs-lib";
import pkg from "@chainbow/radiantjs";
import Secrets from "../config/secrets.js";
const { Transaction, UnspentOutput } = pkg;
export const transaction = async (req, res) => {
try {
const {
myAddress,
toAddress,
amount,
donation,
donationAmount,
donationAddress,
} = req.body;
if (!myAddress || !toAddress) {
return res.status(400).json({
success: false,
message: "Sender and receiver addresses are required",
});
}
const addressIsValid = validateRadiantAddress(toAddress);
if (!addressIsValid) {
return res.status(400).json({
success: false,
message: "Invalid recipient Radiant address",
});
}
const utxos = await getAddressUTXOs(myAddress);
if (!utxos || utxos.length === 0) {
return res.status(400).json({
success: false,
message: "No available UTXOs for the given address",
});
}
const totalAvailable = utxos.reduce((sum, utxo) => sum + utxo.value, 0);
let tx = new Transaction().from(utxos).to(toAddress, amount);
const feeRate = 2000;
// Calculate Transaction Size
const transactionSize =
tx.toBuffer().length >= 200 ? tx.toBuffer().length : 230;
// Calculate Fee
const calculatedFee = transactionSize * feeRate;
// Set Fee
tx = tx.fee(calculatedFee);
const neededAmount =
parseFloat(amount) +
(donation && donationAmount > 0 ? parseFloat(donationAmount) : 0) +
calculatedFee;
if (neededAmount > totalAvailable) {
return res.status(400).json({
success: false,
message: "Insufficient funds for the transaction",
});
}
if (donation && donationAmount > 0) {
if (donation === "networkMiners") {
const newFee = calculatedFee + donationAmount; // Add the donation amount to the current fee
tx = tx.fee(newFee);
} else {
const donationAddressIsValid = validateRadiantAddress(donationAddress);
if (!donationAddressIsValid) {
return res.status(400).json({
success: false,
message: "Invalid donation address",
});
}
tx = tx.to(donationAddress, donationAmount);
}
}
tx = tx.change(myAddress).sign(Secrets.PRIVATE_KEY);
const rawTxHex = tx.toString();
const txId = await broadcastTransaction(rawTxHex);
return res.status(200).json({
success: true,
message: "Transaction broadcasted successfully",
data: { txId },
});
} catch (error) {
console.error({ error });
return res.status(500).json({
success: false,
message: "Internal Server Error",
data: error.message || "An unknown error occurred",
});
}
};
export const getTotalBalance = async (req, res) => {
try {
const { address } = req.body;
if (!address) {
return res.status(400).json({
success: false,
message: "My Radiant Address is required",
});
}
const utxos = await getConfirmedBal(address);
const confimredBal = utxos.result.confirmed;
return res.status(200).json({
success: true,
message: "Balance Retrieved",
data: confimredBal,
});
} catch (error) {
console.error({ error });
return res.status(500).json({
success: false,
message: "Internal Server Error",
data: error.message || "An unknown error occurred",
});
}
};
export const genAddress = async (req, res) => {
try {
const { userid } = req.body;
// Validate input
if (!userid) {
return res.status(422).json({
success: false,
message: "User ID is required",
});
}
const time = Date.now();
const seedPhrase = Secrets.SEED_PHRASE;
const addressIndex = Math.floor(Math.random() * 8999 + 100000);
const { address, privateKey } = await generateAddresses(
seedPhrase,
addressIndex
);
// Insert into the database securely using placeholders
const query =
"INSERT INTO address_used (userid, time, address, p_key, used, cleared) VALUES (?, ?, ?, ?, ?, ?)";
const values = [userid, time, address, privateKey, false, false]; // Using false for the 'used' column
// Execute the query
const [result] = await db.query(query, values);
// Return success response
return res.status(201).json({
success: true,
message: "Address generated successfully",
data: {
addressId: result.insertId, // Return the inserted row's ID
userid,
address,
time,
},
});
} catch (error) {
console.error("Error in genAddress:", error);
return res.status(500).json({
success: false,
message: "Internal Server Error",
data: error.message || "An unknown error occurred",
});
}
};
export const checkDeposit = async (req, res) => {
try {
const { address } = req.body;
if (!address) {
return res.status(422).json({
success: false,
message: "Address is required",
});
}
// Fetch row from the database with address and used = false
const [rows] = await db.query(
"SELECT * FROM address_used WHERE address = ? AND used = ?",
[address, false]
);
if (rows.length === 0) {
return res.status(404).json({
success: false,
message: "Address not found or already used",
});
}
const row = rows[0];
const script = bitcoin.address.toOutputScript(address);
const scriptHash = bitcoin.crypto.sha256(script).reverse().toString("hex");
// Fetch deposit history using scripthash
const depositHistory = await getTransactionHistory(scriptHash);
if (depositHistory && depositHistory.length > 0) {
// Update the database to mark this address as used
await db.query("UPDATE address_used SET used = ? WHERE id = ?", [
true,
row.id,
]);
return res.status(200).json({
success: true,
message: "Deposit found and address marked as used",
data: { depositHistory },
});
} else {
return res.status(200).json({
success: true,
message: "No deposits found for this address",
data: [],
});
}
} catch (error) {
console.error("Error in checkDeposit:", error);
return res.status(500).json({
success: false,
message: "Internal Server Error",
data: error.message || "An unknown error occurred",
});
}
};
async function getAddressUTXOs(address) {
const url = Secrets?.ELECTRUMX_URL;
const ws = new WebSocket(url);
return new Promise((resolve, reject) => {
ws.on("open", () => {
try {
const script = bitcoin.address.toOutputScript(address);
const hash = bitcoin.crypto.sha256(script).reverse().toString("hex");
const request = JSON.stringify({
jsonrpc: "2.0",
method: "blockchain.scripthash.listunspent",
params: [hash],
id: 1,
});
ws.send(request);
} catch (error) {
reject(`Error preparing request: ${error.message}`);
ws.close();
}
});
ws.on("message", (data) => {
try {
const response = JSON.parse(data);
if (response.result) {
// Transform UTXOs into the required format
const utxos = response.result.map((utxo) => {
return {
txid: utxo.tx_hash,
vout: utxo.tx_pos,
satoshis: utxo.value,
scriptPubKey: bitcoin.address
.toOutputScript(address)
.toString("hex"), // Optional, ensure scriptPubKey matches
};
});
resolve(utxos);
} else {
reject(`Error from server: ${response.error}`);
}
} catch (error) {
reject(`Error parsing response: ${error.message}`);
} finally {
ws.close();
}
});
ws.on("error", (err) => {
reject(`WebSocket Error: ${err.message}`);
});
ws.on("close", () => {
console.log("WebSocket connection closed");
});
});
}
export default getAddressUTXOs;