-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclementine-cli.js
executable file
·588 lines (522 loc) · 17.3 KB
/
clementine-cli.js
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env node
const { Command } = require("commander");
const axios = require("axios");
const { ethers } = require("ethers");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const { default: bs58 } = require("bs58");
const program = new Command();
let config = {
bitcoinRpcUrl: "",
bitcoinRpcUser: "",
bitcoinRpcPassword: "",
citreaRpcUrl: "https://rpc.testnet.citrea.xyz/",
citreaPrivateKey: "",
operatorEndpoints: ["https://api.testnet.citrea.xyz/withdrawals"],
citreaExplorerUrl: "https://explorer.testnet.citrea.xyz/",
bitcoinExplorerUrl: "https://mempool.space/testnet4/",
};
const configFilePath = path.join(__dirname, "config.json");
if (fs.existsSync(configFilePath)) {
const fileConfig = JSON.parse(fs.readFileSync(configFilePath, "utf-8"));
config = { ...config, ...fileConfig };
}
// Command-line options for configuration
program
.option("--bitcoin-rpc-url <url>", "Bitcoin RPC URL")
.option("--bitcoin-rpc-user <user>", "Bitcoin RPC User")
.option("--bitcoin-rpc-password <password>", "Bitcoin RPC Password")
.option("--citrea-rpc-url <url>", "Citrea RPC URL")
.option("--citrea-private-key <key>", "Citrea Private Key");
// Function to convert a private key to WIF, ref: https://en.bitcoin.it/wiki/Wallet_import_format
function privateKeyToWIF(privateKeyHex, compressed = true, testnet = true) {
// Convert the hex string to a Buffer
let privateKeyBuffer = Buffer.from(privateKeyHex, "hex");
// Add the 0x80 prefix for mainnet or 0xef for testnet
const prefix = testnet ? Buffer.from([0xef]) : Buffer.from([0x80]);
let extendedKey = Buffer.concat([prefix, privateKeyBuffer]);
// Add the 0x01 suffix if the key will correspond to a compressed public key
if (compressed) {
extendedKey = Buffer.concat([extendedKey, Buffer.from([0x01])]);
}
// Perform SHA-256 hash twice
const firstSHA = crypto.createHash("sha256").update(extendedKey).digest();
const secondSHA = crypto.createHash("sha256").update(firstSHA).digest();
// Take the first 4 bytes of the second SHA-256 hash for the checksum
/** @type {Buffer} */
const checksum = secondSHA.subarray(0, 4);
// Add the checksum to the end of the extended key
const finalKey = Buffer.concat([extendedKey, checksum]);
// Convert to Base58Check encoding
const WIF = bs58.encode(finalKey);
return WIF;
}
// Function to create a random 32-byte private key
function createRandomPrivKey() {
// Generate a random 32-byte Buffer
const randomKey = crypto.randomBytes(32);
// Convert the Buffer to a hex string
const privateKeyHex = randomKey.toString("hex");
return privateKeyHex;
}
// Function to make Bitcoin RPC calls
const makeBitcoinRpcCall = async (options, method, params = []) => {
try {
const response = await axios.post(
options.bitcoinRpcUrl,
{
jsonrpc: "2.0",
id: 1,
method: method,
params: params,
},
{
auth: {
username: options.bitcoinRpcUser,
password: options.bitcoinRpcPassword,
},
}
);
if (response.data.error) {
throw new Error(response.data.error?.message || "Unknown error");
}
return response.data.result;
} catch (error) {
console.error(`Bitcoin RPC error: ${error.message}`);
throw error;
}
};
const createDustUtxoFromWallet = async (options, address) => {
const rawtx = await makeBitcoinRpcCall(options, "createrawtransaction", [
[],
{ [address]: 0.00000546 },
]);
const fundedtx = (
await makeBitcoinRpcCall(options, "fundrawtransaction", [
rawtx,
{ changePosition: 1 },
])
).hex;
const signedtx = (
await makeBitcoinRpcCall(options, "signrawtransactionwithwallet", [
fundedtx,
])
).hex;
const txid = (
await makeBitcoinRpcCall(options, "decoderawtransaction", [signedtx])
).txid;
const vout = 0;
await makeBitcoinRpcCall(options, "testmempoolaccept", [[signedtx]]);
await makeBitcoinRpcCall(options, "sendrawtransaction", [signedtx]);
return { txid, vout };
};
const createBurnTx = async (wallet, { txid, vout }) => {
if (!txid) {
throw new Error("txid is required to create a burn transaction");
}
if (vout !== 0) {
throw new Error("vout must be 0 for the burn transaction");
}
const reversed_txid = txid.match(/.{2}/g).reverse().join("");
const data = new ethers.Interface([
"function withdraw(bytes32 txId, bytes4 outputId)",
]).encodeFunctionData("withdraw", ["0x" + reversed_txid, "0x00000000"]);
const tx = {
to: "0x3100000000000000000000000000000000000002",
value: ethers.parseEther("10"),
data: data,
};
const populatedtx = await wallet.populateTransaction(tx);
return populatedtx;
};
const sendAnyoneCanPaySignature = async (
options,
{ dustUtxoDetails, withdrawalAddress, amount }
) => {
const operatorEndpoints = options.operatorEndpoints;
const rawtx = await makeBitcoinRpcCall(options, "createpsbt", [
[{ txid: dustUtxoDetails.txid, vout: dustUtxoDetails.vout }],
{ [withdrawalAddress]: amount },
]);
const signedtxrequest = await makeBitcoinRpcCall(
options,
"descriptorprocesspsbt",
[rawtx, [dustUtxoDetails.descriptor], "SINGLE|ANYONECANPAY"]
);
if (!signedtxrequest.complete) {
if (
signedtxrequest.errors[0].error === "Input not found or already spent"
) {
console.log(
"Input not found or already spent, Withdrawal completed. Exiting..."
);
return;
}
throw new Error("Bitcoin Transaction signing failed");
}
const signedtx = signedtxrequest.hex;
// Extract the txinwitness
const txinwitness = (
await makeBitcoinRpcCall(options, "decoderawtransaction", [signedtx])
).vin[0].txinwitness[0].slice(0, -2);
const inputAddressScriptPubKey = (
await makeBitcoinRpcCall(options, "getaddressinfo", [
dustUtxoDetails.address,
])
).scriptPubKey;
const withdrawalAddressScriptPubKey = (
await makeBitcoinRpcCall(options, "getaddressinfo", [withdrawalAddress])
).scriptPubKey;
const payload = {
idx: dustUtxoDetails.withdrawalIdx,
user_sig: txinwitness,
input_utxo: {
outpoint: `${dustUtxoDetails.txid}:0`,
txout: {
script_pubkey: inputAddressScriptPubKey,
value: 546,
},
},
output_txout: {
script_pubkey: withdrawalAddressScriptPubKey,
value: parseInt(amount * 1e8),
},
};
// console.log("Payload:", payload);
// make that at least one of the requests is successful
let success = false;
let paymentTxid = null;
// TODO: Promise.allSettled
await Promise.all(
operatorEndpoints.map((endpoint) => {
return axios
.post(endpoint, payload)
.then((response) => {
// console.log("Response:", response);
if (response.status === 200) {
success = true;
paymentTxid = response.data;
}
})
.catch((error) => {
const errMsg =
error.response.data?.error || error?.message || "Unknown error";
throw new Error(errMsg);
});
})
);
if (success) {
return paymentTxid;
}
};
const withdraw = async (
config,
backupFilePath,
withdrawalAddress,
minWithdrawalAmount,
precision
) => {
let backupData;
try {
backupData = JSON.parse(fs.readFileSync(backupFilePath, "utf-8"));
} catch (error) {
throw new Error(`Error reading backup data from file: ${error.message}`);
}
// console.log("Backup data:", backupData);
if (!backupData.descriptor || !backupData.address) {
throw new Error("Invalid backup data.");
}
if (!backupData.txid) {
try {
console.log("Creating dust UTXO...");
const { txid, vout } = await createDustUtxoFromWallet(
config,
backupData.address
);
backupData = { ...backupData, txid, vout };
fs.writeFileSync(backupFilePath, JSON.stringify(backupData));
} catch (error) {
console.error(`\nError creating dust UTXO`);
console.error(`You need to have some funds in your wallet.`);
const balance = await makeBitcoinRpcCall(config, "getbalance");
console.log(`\nYour wallet balance: ${balance}\n`);
throw new Error(`Error creating dust UTXO: ${error.message}`);
}
}
let outspend;
try {
outspend = await axios.get(
`${config.bitcoinExplorerUrl}api/tx/${backupData.txid}/outspend/${backupData.vout}`
);
outspend = outspend.data;
} catch (error) {
throw new Error(`Error getting outspend data: ${error.message}`);
}
if (outspend.txid) {
console.log("Withdrawal completed.");
console.log(
`You can view the transaction on Bitcoin Explorer: ${config.bitcoinExplorerUrl}tx/${outspend.txid}`
);
return;
}
if (!backupData.burnTxHash) {
console.log("Withdrawing 10 cBTC...");
let wallet;
let provider;
try {
provider = new ethers.JsonRpcProvider(config.citreaRpcUrl);
wallet = new ethers.Wallet(config.citreaPrivateKey, provider);
} catch (error) {
throw new Error(`Error connecting to Citrea: ${error.message}`);
}
let tx;
try {
tx = await createBurnTx(wallet, backupData);
} catch (error) {
if (error.message.startsWith("insufficient funds")) {
console.error(
"\nError creating withdraw transaction: Insufficient funds."
);
console.error(
"You need to have at least 10 cBTC in your Citrea wallet.\n"
);
try {
const balance = await provider.getBalance(wallet.address);
console.log(`Your Citrea balance: ${ethers.formatEther(balance)} cBTC\n`);
} catch (error) {
console.error(`Error getting wallet balance: ${error.message}\n`);
}
}
throw new Error(`Error creating withdraw transaction: ${error.message}`);
}
let txhash;
try {
const signature = await wallet.signTransaction(tx);
txhash = ethers.keccak256(signature);
} catch (error) {
throw new Error(`Error signing transaction: ${error.message}`);
}
backupData = { ...backupData, burnTxHash: txhash };
// write the hash to the file
fs.writeFileSync(backupFilePath, JSON.stringify(backupData));
let signedTx;
try {
// Sign and send the transaction
signedTx = await wallet.sendTransaction(tx);
} catch (error) {
fs.writeFileSync(
backupFilePath,
JSON.stringify({ ...backupData, burnTxHash: null })
);
throw new Error(`Error sending transaction: ${error.message}`);
}
try {
const receipt = await signedTx.wait();
const logs = receipt.logs;
const logData = logs[0].data;
const withdrawalIdx = parseInt(
logData.slice(logData.length - 128, logData.length - 64),
16
);
backupData = { ...backupData, withdrawalIdx, receipt };
fs.writeFileSync(backupFilePath, JSON.stringify(backupData));
} catch (error) {
throw new Error(`Error getting receipt: ${error.message}`);
}
// sleep 1 second
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(
`\nWithdrawal tx on Citrea completed, see the tx on ${config.citreaExplorerUrl}tx/${txhash}\n`
);
}
if (!backupData.withdrawalIdx) {
if (!backupData.burnTxHash) {
throw new Error("Burn transaction hash is required.");
}
const provider = new ethers.JsonRpcProvider(config.citreaRpcUrl);
const receipt = await provider.getTransactionReceipt(backupData.burnTxHash);
const logs = receipt.logs;
const logData = logs[0].data;
const withdrawalIdx = parseInt(
logData.slice(logData.length - 128, logData.length - 64),
16
);
backupData = { ...backupData, withdrawalIdx };
fs.writeFileSync(backupFilePath, JSON.stringify(backupData));
}
if (!withdrawalAddress) {
throw new Error("Withdrawal address is required.");
}
if (!minWithdrawalAmount) {
throw new Error("Minimum withdrawal amount is required.");
}
try {
minWithdrawalAmount = parseFloat(minWithdrawalAmount);
} catch (error) {
throw new Error("Invalid minimum withdrawal amount.");
}
if (!precision) {
throw new Error("Precision is required.");
}
try {
precision = parseFloat(precision);
} catch (error) {
throw new Error("Invalid precision.");
}
// amounts is from 10 to minWithdrawalAmount in steps of precision
for (let amount = 10; amount >= minWithdrawalAmount; amount -= precision) {
amount = amount.toFixed(8);
console.log(`Trying to withdraw ${amount} BTC...`);
try {
const paymentTxid = await sendAnyoneCanPaySignature(config, {
dustUtxoDetails: backupData,
withdrawalAddress,
amount,
});
console.log(
`Withdrawal successful. You will receive ${amount} BTC soon.`
);
console.log(`Here are the payment txids:`);
// iterate over paymentTxid.withdrawal_operator_payments
for (const payment of paymentTxid.withdrawal_operator_payments) {
console.log(`${config.bitcoinExplorerUrl}tx/${payment.txid}`);
}
// console.log(paymentTxid);
// console.log(
// `Payment txid for ${amount} cBTC: ${JSON.stringify(paymentTxid)}`
// );
return paymentTxid;
} catch (error) {
console.log(`Error sending withdrawal signature: ${error.message}`);
// sleep for 1 second
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
throw new Error("Withdrawal failed.");
};
program
.command("withdraw")
.description("Withdraw funds from Citrea to Bitcoin")
.option(
"-a, --withdrawal-address <address>",
"Specify the withdrawal address on Bitcoin",
)
.option(
"-m, --min-withdrawal-amount <amount>",
"Specify the minimum withdrawal amount",
"9.99"
)
.option("-p, --precision <number>", "Specify the precision", "0.001")
.option(
"-b, --backup-folder-path <path>",
"Specify the backup folder path",
"backups/"
)
.action(async (options) => {
const {
withdrawalAddress,
minWithdrawalAmount,
precision,
backupFolderPath,
} = options;
config = { ...config, ...program.opts() };
// Ensure required options are provided
if (
!config.bitcoinRpcUrl ||
!config.bitcoinRpcUser ||
!config.bitcoinRpcPassword ||
!config.citreaRpcUrl ||
!config.citreaPrivateKey ||
!withdrawalAddress
) {
console.error(
"Error: --bitcoin-rpc-url, --bitcoin-rpc-user, --bitcoin-rpc-password, --citrea-rpc-url, and --citrea-private-key are required for this command."
);
process.exit(1);
}
// Step 1: Generate a new private key
const privateKey = createRandomPrivKey();
// Convert the private key to WIF format
const WIF = privateKeyToWIF(privateKey, true, true);
// Call getdescriptorinfo to get the address
const descriptor = `tr(${WIF})`;
const descriptor_results = await makeBitcoinRpcCall(
config,
"getdescriptorinfo",
[descriptor]
);
const addressArray = await makeBitcoinRpcCall(config, "deriveaddresses", [
descriptor_results.descriptor,
]);
const address = addressArray[0];
const backupFilePath = path.join(backupFolderPath, `${address}.json`);
console.log(
`\nTo be able to resume your withdrawal process, use this command:\n./clementine-cli.js resumewithdraw --backup-file-path ${backupFilePath}\n./clementine-cli.js resumewithdraw --help\nfor more information.\n\n`
);
fs.writeFileSync(
backupFilePath,
JSON.stringify({ descriptor, address, withdrawalAddress })
);
try {
await withdraw(
config,
backupFilePath,
withdrawalAddress,
minWithdrawalAmount,
precision
);
} catch (error) {
console.log(`Error in withdrawal process: ${error.message}`);
console.log(
"\nResume the withdrawal process using the following command:"
);
console.log(
`./clementine-cli.js resumewithdraw --backup-file-path ${backupFilePath}\n`
);
}
});
program
.command("resumewithdraw")
.description("Resume withdrawal process from a backup file")
.option("-b, --backup-file-path <path>", "Backup file path", "")
.option(
"-m, --min-withdrawal-amount <amount>",
"Specify the minimum withdrawal amount",
"9.99"
)
.option("-p, --precision <number>", "Specify the precision", "0.001")
.action(async (options) => {
const { minWithdrawalAmount, precision, backupFilePath } = options;
config = { ...config, ...program.opts() };
// Ensure required options are provided
if (
!config.bitcoinRpcUrl ||
!config.bitcoinRpcUser ||
!config.bitcoinRpcPassword ||
!config.citreaRpcUrl ||
!config.citreaPrivateKey
) {
console.error(
"Error: --bitcoin-rpc-url, --bitcoin-rpc-user, --bitcoin-rpc-password, --citrea-rpc-url, and --citrea-private-key are required for this command."
);
process.exit(1);
}
// read the backup file
const backupData = JSON.parse(fs.readFileSync(backupFilePath, "utf-8"));
if (!backupData.withdrawalAddress) {
throw new Error("Withdrawal address is not set.");
}
try {
await withdraw(
config,
backupFilePath,
backupData.withdrawalAddress,
minWithdrawalAmount,
precision
);
} catch (error) {
console.log(`Error in withdrawal process: ${error.message}`);
}
});
program.parse(process.argv);