react-native-nitro-ark 0.0.39 → 0.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cpp/NitroArk.hpp CHANGED
@@ -8,593 +8,599 @@
8
8
  #include <string>
9
9
  #include <vector>
10
10
 
11
- namespace margelo::nitro::nitroark
12
- {
13
-
14
- using namespace margelo::nitro;
15
- // Helper function to convert rust cxx payment type to nitrogen payment type
16
- inline PaymentTypes convertPaymentType(bark_cxx::PaymentTypes type)
17
- {
18
- switch (type)
19
- {
20
- case bark_cxx::PaymentTypes::Bolt11:
21
- return PaymentTypes::BOLT11;
22
- case bark_cxx::PaymentTypes::Lnurl:
23
- return PaymentTypes::LNURL;
24
- case bark_cxx::PaymentTypes::Arkoor:
25
- return PaymentTypes::ARKOOR;
26
- case bark_cxx::PaymentTypes::Onchain:
27
- return PaymentTypes::ONCHAIN;
28
- default:
29
- throw std::runtime_error("Invalid payment type");
30
- }
31
- }
32
-
33
- class NitroArk : public HybridNitroArkSpec
34
- {
35
- public:
36
- NitroArk() : HybridObject(TAG)
37
- {
38
- // Initialize the Rust logger once when a NitroArk object is created.
39
- bark_cxx::init_logger();
40
- }
41
-
42
- // --- Management ---
43
-
44
- std::shared_ptr<Promise<std::string>> createMnemonic() override
45
- {
46
- return Promise<std::string>::async([]()
47
- {
48
- try {
49
- rust::String mnemonic_rs = bark_cxx::create_mnemonic();
50
- return std::string(mnemonic_rs.data(), mnemonic_rs.length());
51
- } catch (const rust::Error &e) {
52
- throw std::runtime_error(e.what());
53
- } });
54
- }
55
-
56
- std::shared_ptr<Promise<void>>
57
- loadWallet(const std::string &datadir,
58
- const BarkCreateOpts &opts) override
59
- {
60
- return Promise<void>::async([datadir, opts]()
61
- {
62
- try {
63
- bark_cxx::ConfigOpts config_opts;
64
- if (opts.config.has_value()) {
65
- config_opts.asp = opts.config->asp.value_or("");
66
- config_opts.esplora = opts.config->esplora.value_or("");
67
- config_opts.bitcoind = opts.config->bitcoind.value_or("");
68
- config_opts.bitcoind_cookie = opts.config->bitcoind_cookie.value_or("");
69
- config_opts.bitcoind_user = opts.config->bitcoind_user.value_or("");
70
- config_opts.bitcoind_pass = opts.config->bitcoind_pass.value_or("");
71
- config_opts.vtxo_refresh_expiry_threshold = static_cast<uint32_t>(opts.config->vtxo_refresh_expiry_threshold.value_or(0));
72
- config_opts.fallback_fee_rate = static_cast<uint64_t>(opts.config->fallback_fee_rate.value_or(0));
73
- }
74
-
75
- bark_cxx::CreateOpts create_opts;
76
- create_opts.regtest = opts.regtest.value_or(false);
77
- create_opts.signet = opts.signet.value_or(false);
78
- create_opts.bitcoin = opts.bitcoin.value_or(true);
79
- create_opts.mnemonic = opts.mnemonic;
80
- uint32_t birthday_height_val;
81
- if (opts.birthday_height.has_value()) {
82
- birthday_height_val = static_cast<uint32_t>(opts.birthday_height.value());
83
- create_opts.birthday_height = &birthday_height_val;
84
- } else {
85
- create_opts.birthday_height = nullptr;
86
- }
87
- create_opts.config = config_opts;
88
-
89
- bark_cxx::load_wallet(datadir, create_opts);
90
- } catch (const rust::Error &e) {
91
- throw std::runtime_error(e.what());
92
- } });
93
- }
94
-
95
- std::shared_ptr<Promise<void>> closeWallet() override
96
- {
97
- return Promise<void>::async([]()
98
- {
99
- try {
100
- bark_cxx::close_wallet();
101
- } catch (const rust::Error &e) {
102
- throw std::runtime_error(e.what());
103
- } });
104
- }
105
-
106
- std::shared_ptr<Promise<bool>> isWalletLoaded() override
107
- {
108
- return Promise<bool>::async([]()
109
- { return bark_cxx::is_wallet_loaded(); });
110
- }
111
-
112
- std::shared_ptr<Promise<void>>
113
- persistConfig(const BarkConfigOpts &opts) override
114
- {
115
- return Promise<void>::async([opts]()
116
- {
117
- try {
118
- bark_cxx::ConfigOpts config_opts;
119
- config_opts.asp = opts.asp.value_or("");
120
- config_opts.esplora = opts.esplora.value_or("");
121
- config_opts.bitcoind = opts.bitcoind.value_or("");
122
- config_opts.bitcoind_cookie = opts.bitcoind_cookie.value_or("");
123
- config_opts.bitcoind_user = opts.bitcoind_user.value_or("");
124
- config_opts.bitcoind_pass = opts.bitcoind_pass.value_or("");
125
- config_opts.vtxo_refresh_expiry_threshold = static_cast<uint32_t>(opts.vtxo_refresh_expiry_threshold.value_or(0));
126
- config_opts.fallback_fee_rate = static_cast<uint64_t>(opts.fallback_fee_rate.value_or(0));
127
- bark_cxx::persist_config(config_opts);
128
- } catch (const rust::Error &e) {
129
- throw std::runtime_error(e.what());
130
- } });
131
- }
132
-
133
- std::shared_ptr<Promise<void>> maintenance() override
134
- {
135
- return Promise<void>::async([]()
136
- {
137
- try {
138
- bark_cxx::maintenance();
139
- } catch (const rust::Error &e) {
140
- throw std::runtime_error(e.what());
141
- } });
142
- }
143
-
144
- std::shared_ptr<Promise<void>> sync() override
145
- {
146
- return Promise<void>::async([]()
147
- {
148
- try {
149
- bark_cxx::sync();
150
- } catch (const rust::Error &e) {
151
- throw std::runtime_error(e.what());
152
- } });
153
- }
154
-
155
- std::shared_ptr<Promise<void>> syncExits() override
156
- {
157
- return Promise<void>::async([]()
158
- {
159
- try {
160
- bark_cxx::sync_exits();
161
- } catch (const rust::Error &e) {
162
- throw std::runtime_error(e.what());
163
- } });
164
- }
165
-
166
- std::shared_ptr<Promise<void>> syncRounds() override
167
- {
168
- return Promise<void>::async([]()
169
- {
170
- try {
171
- bark_cxx::sync_rounds();
172
- } catch (const rust::Error &e) {
173
- throw std::runtime_error(e.what());
174
- } });
175
- }
176
-
177
- // --- Wallet Info ---
178
-
179
- std::shared_ptr<Promise<BarkArkInfo>> getArkInfo() override
180
- {
181
- return Promise<BarkArkInfo>::async([]()
182
- {
183
- try {
184
- bark_cxx::CxxArkInfo rust_info = bark_cxx::get_ark_info();
185
- BarkArkInfo info;
186
- info.network = std::string(rust_info.network.data(), rust_info.network.length());
187
- info.asp_pubkey = std::string(rust_info.asp_pubkey.data(), rust_info.asp_pubkey.length());
188
- info.round_interval_secs = static_cast<double>(rust_info.round_interval_secs);
189
- info.vtxo_exit_delta = static_cast<double>(rust_info.vtxo_exit_delta);
190
- info.vtxo_expiry_delta = static_cast<double>(rust_info.vtxo_expiry_delta);
191
- info.htlc_expiry_delta = static_cast<double>(rust_info.htlc_expiry_delta);
192
- info.max_vtxo_amount_sat = static_cast<double>(rust_info.max_vtxo_amount_sat);
193
- return info;
194
- } catch (const rust::Error &e) {
195
- throw std::runtime_error(e.what());
196
- } });
197
- }
198
-
199
- std::shared_ptr<Promise<OffchainBalanceResult>> offchainBalance() override
200
- {
201
- return Promise<OffchainBalanceResult>::async([]()
202
- {
203
- try {
204
- bark_cxx::OffchainBalance rust_balance = bark_cxx::offchain_balance();
205
- OffchainBalanceResult balance;
206
- balance.spendable = static_cast<double>(rust_balance.spendable);
207
- balance.pending_lightning_send = static_cast<double>(rust_balance.pending_lightning_send);
208
- balance.pending_exit = static_cast<double>(rust_balance.pending_exit);
209
- return balance;
210
- } catch (const rust::Error &e) {
211
- throw std::runtime_error(e.what());
212
- } });
213
- }
214
-
215
- std::shared_ptr<Promise<std::string>>
216
- deriveStoreNextKeypair() override
217
- {
218
- return Promise<std::string>::async([]()
219
- {
220
- try {
221
- rust::String keypair_rs = bark_cxx::derive_store_next_keypair();
222
- return std::string(keypair_rs.data(), keypair_rs.length());
223
- } catch (const rust::Error &e) {
224
- throw std::runtime_error(e.what());
225
- } });
226
- }
227
-
228
- std::shared_ptr<Promise<std::string>>
229
- peakKeyPair(double index) override
230
- {
231
- return Promise<std::string>::async([index]()
232
- {
233
- try {
234
- uint32_t index_val = static_cast<uint32_t>(index);
235
- rust::String keypair_rs = bark_cxx::peak_keypair(index_val);
236
- return std::string(keypair_rs.data(), keypair_rs.length());
237
- } catch (const rust::Error &e) {
238
- throw std::runtime_error(e.what());
239
- } });
240
- }
241
-
242
- std::shared_ptr<Promise<std::vector<BarkVtxo>>> getVtxos() override
243
- {
244
- return Promise<std::vector<BarkVtxo>>::async([]()
245
- {
246
- try {
247
- rust::Vec<bark_cxx::BarkVtxo> rust_vtxos = bark_cxx::get_vtxos();
248
- std::vector<BarkVtxo> vtxos;
249
- for (const auto& rust_vtxo : rust_vtxos) {
250
- BarkVtxo vtxo;
251
- vtxo.amount = static_cast<double>(rust_vtxo.amount);
252
- vtxo.expiry_height = static_cast<double>(rust_vtxo.expiry_height);
253
- vtxo.exit_delta = static_cast<double>(rust_vtxo.exit_delta);
254
- vtxo.anchor_point = std::string(rust_vtxo.anchor_point.data(), rust_vtxo.anchor_point.length());
255
- vtxos.push_back(vtxo);
256
- }
257
- return vtxos;
258
- } catch (const rust::Error &e) {
259
- throw std::runtime_error(e.what());
260
- } });
261
- }
262
-
263
- // --- Onchain Operations ---
264
-
265
- std::shared_ptr<Promise<OnchainBalanceResult>> onchainBalance() override
266
- {
267
- return Promise<OnchainBalanceResult>::async([]()
268
- {
269
- try {
270
- bark_cxx::OnChainBalance rust_balance = bark_cxx::onchain_balance();
271
- OnchainBalanceResult balance;
272
- balance.immature = static_cast<double>(rust_balance.immature);
273
- balance.trusted_pending = static_cast<double>(rust_balance.trusted_pending);
274
- balance.untrusted_pending = static_cast<double>(rust_balance.untrusted_pending);
275
- balance.confirmed = static_cast<double>(rust_balance.confirmed);
276
- return balance;
277
- } catch (const rust::Error &e) {
278
- throw std::runtime_error(e.what());
279
- } });
280
- }
281
-
282
- std::shared_ptr<Promise<void>> onchainSync() override
283
- {
284
- return Promise<void>::async([]()
285
- {
286
- try {
287
- bark_cxx::onchain_sync();
288
- } catch (const rust::Error &e) {
289
- throw std::runtime_error(e.what());
290
- } });
291
- }
292
-
293
- std::shared_ptr<Promise<std::string>> onchainListUnspent() override
294
- {
295
- return Promise<std::string>::async([]()
296
- {
297
- try {
298
- rust::String json_rs = bark_cxx::onchain_list_unspent();
299
- return std::string(json_rs.data(), json_rs.length());
300
- } catch (const rust::Error &e) {
301
- throw std::runtime_error(e.what());
302
- } });
303
- }
304
-
305
- std::shared_ptr<Promise<std::string>> onchainUtxos() override
306
- {
307
- return Promise<std::string>::async([]()
308
- {
309
- try {
310
- rust::String json_rs = bark_cxx::onchain_utxos();
311
- return std::string(json_rs.data(), json_rs.length());
312
- } catch (const rust::Error &e) {
313
- throw std::runtime_error(e.what());
314
- } });
315
- }
316
-
317
- std::shared_ptr<Promise<std::string>>
318
- onchainAddress() override
319
- {
320
- return Promise<std::string>::async([]()
321
- {
322
- try {
323
- rust::String address_rs = bark_cxx::onchain_address();
324
- return std::string(address_rs.data(), address_rs.length());
325
- } catch (const rust::Error &e) {
326
- throw std::runtime_error(e.what());
327
- } });
328
- }
329
-
330
- std::shared_ptr<Promise<OnchainPaymentResult>>
331
- onchainSend(const std::string &destination, double amountSat, std::optional<double> feeRate) override
332
- {
333
- return Promise<OnchainPaymentResult>::async([destination, amountSat, feeRate]()
334
- {
335
- try {
336
- uint64_t feeRate_val;
337
- bark_cxx::OnchainPaymentResult rust_result;
338
- if (feeRate.has_value()) {
339
- feeRate_val = static_cast<uint64_t>(feeRate.value());
340
- rust_result = bark_cxx::onchain_send(destination, static_cast<uint64_t>(amountSat), &feeRate_val);
341
- } else {
342
- rust_result = bark_cxx::onchain_send(destination, static_cast<uint64_t>(amountSat), nullptr);
343
- }
344
-
345
- OnchainPaymentResult result;
346
- result.txid = std::string(rust_result.txid.data(), rust_result.txid.length());
347
- result.amount_sat = static_cast<double>(rust_result.amount_sat);
348
- result.destination_address = std::string(rust_result.destination_address.data(), rust_result.destination_address.length());
349
- result.payment_type = convertPaymentType(rust_result.payment_type);
350
-
351
- return result;
352
- } catch (const rust::Error &e) {
353
- throw std::runtime_error(e.what());
354
- } });
355
- }
356
-
357
- std::shared_ptr<Promise<std::string>>
358
- onchainDrain(const std::string &destination, std::optional<double> feeRate) override
359
- {
360
- return Promise<std::string>::async(
361
- [destination, feeRate]()
362
- {
363
- try
364
- {
365
- uint64_t feeRate_val;
366
- rust::String txid_rs;
367
- if (feeRate.has_value())
368
- {
369
- feeRate_val = static_cast<uint64_t>(feeRate.value());
370
- txid_rs = bark_cxx::onchain_drain(destination, &feeRate_val);
371
- }
372
- else
373
- {
374
- txid_rs = bark_cxx::onchain_drain(destination, nullptr);
375
- }
376
- return std::string(txid_rs.data(), txid_rs.length());
377
- }
378
- catch (const rust::Error &e)
379
- {
380
- throw std::runtime_error(e.what());
381
- }
382
- });
383
- }
384
-
385
- std::shared_ptr<Promise<std::string>>
386
- onchainSendMany(const std::vector<BarkSendManyOutput> &outputs,
387
- std::optional<double> feeRate) override
388
- {
389
- return Promise<std::string>::async([outputs, feeRate]()
390
- {
391
- try {
392
- rust::Vec<bark_cxx::SendManyOutput> cxx_outputs;
393
- for (const auto &output : outputs) {
394
- cxx_outputs.push_back({rust::String(output.destination), static_cast<uint64_t>(output.amountSat)});
395
- }
396
- uint64_t feeRate_val;
397
- rust::String txid_rs;
398
- if (feeRate.has_value()) {
399
- feeRate_val = static_cast<uint64_t>(feeRate.value());
400
- txid_rs = bark_cxx::onchain_send_many(std::move(cxx_outputs), &feeRate_val);
401
- } else {
402
- txid_rs = bark_cxx::onchain_send_many(std::move(cxx_outputs), nullptr);
403
- }
404
- return std::string(txid_rs.data(), txid_rs.length());
405
- } catch (const rust::Error &e) {
406
- throw std::runtime_error(e.what());
407
- } });
408
- }
409
-
410
- // --- Lightning Operations ---
411
-
412
- std::shared_ptr<Promise<LightningPaymentResult>>
413
- sendLightningPayment(const std::string &destination, std::optional<double> amountSat) override
414
- {
415
- return Promise<LightningPaymentResult>::async([destination, amountSat]()
416
- {
417
- try {
418
- bark_cxx::Bolt11PaymentResult rust_result;
419
- if (amountSat.has_value()) {
420
- uint64_t amountSat_val = static_cast<uint64_t>(amountSat.value());
421
- rust_result = bark_cxx::send_lightning_payment(destination, &amountSat_val);
422
- } else {
423
- rust_result = bark_cxx::send_lightning_payment(destination, nullptr);
424
- }
425
-
426
- LightningPaymentResult result;
427
- result.bolt11_invoice = std::string(rust_result.bolt11_invoice.data(), rust_result.bolt11_invoice.length());
428
- result.preimage = std::string(rust_result.preimage.data(), rust_result.preimage.length());
429
- result.payment_type = convertPaymentType(rust_result.payment_type);
430
-
431
- return result;
432
- } catch (const rust::Error &e) {
433
- throw std::runtime_error(e.what());
434
- } });
435
- }
436
-
437
- std::shared_ptr<Promise<LnurlPaymentResult>>
438
- sendLnaddr(const std::string &addr, double amountSat, const std::string &comment) override
439
- {
440
- return Promise<LnurlPaymentResult>::async([addr, amountSat, comment]()
441
- {
442
- try {
443
- bark_cxx::LnurlPaymentResult rust_result = bark_cxx::send_lnaddr(addr, static_cast<uint64_t>(amountSat), comment);
444
-
445
- LnurlPaymentResult result;
446
- result.lnurl = std::string(rust_result.lnurl.data(), rust_result.lnurl.length());
447
- result.bolt11_invoice = std::string(rust_result.bolt11_invoice.data(), rust_result.bolt11_invoice.length());
448
- result.preimage = std::string(rust_result.preimage.data(), rust_result.preimage.length());
449
- result.payment_type = convertPaymentType(rust_result.payment_type);
450
-
451
- return result;
452
- } catch (const rust::Error &e) {
453
- throw std::runtime_error(e.what());
454
- } });
455
- }
456
-
457
- std::shared_ptr<Promise<std::string>>
458
- bolt11Invoice(double amountMsat) override
459
- {
460
- return Promise<std::string>::async([amountMsat]()
461
- {
462
- try {
463
- rust::String invoice_rs = bark_cxx::bolt11_invoice(static_cast<uint64_t>(amountMsat));
464
- return std::string(invoice_rs.data(), invoice_rs.length());
465
- } catch (const rust::Error &e) {
466
- throw std::runtime_error(e.what());
467
- } });
468
- }
469
-
470
- std::shared_ptr<Promise<void>>
471
- finishLightningReceive(const std::string &bolt11) override
472
- {
473
- return Promise<void>::async([bolt11]()
474
- {
475
- try {
476
- bark_cxx::finish_lightning_receive(bolt11);
477
- } catch (const rust::Error &e) {
478
- throw std::runtime_error(e.what());
479
- } });
480
- }
481
-
482
- // --- Ark Operations ---
483
-
484
- std::shared_ptr<Promise<std::string>> boardAmount(double amountSat) override
485
- {
486
- return Promise<std::string>::async([amountSat]()
487
- {
488
- try {
489
- rust::String status_rs = bark_cxx::board_amount(static_cast<uint64_t>(amountSat));
490
- return std::string(status_rs.data(), status_rs.length());
491
- } catch (const rust::Error &e) {
492
- throw std::runtime_error(e.what());
493
- } });
494
- }
495
-
496
- std::shared_ptr<Promise<std::string>> boardAll() override
497
- {
498
- return Promise<std::string>::async([]()
499
- {
500
- try {
501
- rust::String status_rs = bark_cxx::board_all();
502
- return std::string(status_rs.data(), status_rs.length());
503
- } catch (const rust::Error &e) {
504
- throw std::runtime_error(e.what());
505
- } });
506
- }
507
-
508
- std::shared_ptr<Promise<ArkoorPaymentResult>>
509
- sendArkoorPayment(const std::string &destination, double amountSat) override
510
- {
511
- return Promise<ArkoorPaymentResult>::async([destination, amountSat]()
512
- {
513
- try {
514
- bark_cxx::ArkoorPaymentResult rust_result = bark_cxx::send_arkoor_payment(destination, static_cast<uint64_t>(amountSat));
515
-
516
- ArkoorPaymentResult result;
517
- result.amount_sat = static_cast<double>(rust_result.amount_sat);
518
- result.destination_pubkey = std::string(rust_result.destination_pubkey.data(), rust_result.destination_pubkey.length());
519
- result.payment_type = convertPaymentType(rust_result.payment_type);
520
-
521
- std::vector<BarkVtxo> vtxos;
522
- for (const auto& rust_vtxo : rust_result.vtxos) {
523
- BarkVtxo vtxo;
524
- vtxo.amount = static_cast<double>(rust_vtxo.amount);
525
- vtxo.expiry_height = static_cast<double>(rust_vtxo.expiry_height);
526
- vtxo.exit_delta = static_cast<double>(rust_vtxo.exit_delta);
527
- vtxo.anchor_point = std::string(rust_vtxo.anchor_point.data(), rust_vtxo.anchor_point.length());
528
- vtxos.push_back(vtxo);
529
- }
530
- result.vtxos = vtxos;
531
-
532
- return result;
533
- } catch (const rust::Error &e) {
534
- throw std::runtime_error(e.what());
535
- } });
536
- }
537
-
538
- std::shared_ptr<Promise<std::string>>
539
- sendRoundOnchainPayment(const std::string &destination, double amountSat) override
540
- {
541
- return Promise<std::string>::async(
542
- [destination, amountSat]()
543
- {
544
- try
545
- {
546
- rust::String status_rs = bark_cxx::send_round_onchain_payment(destination, static_cast<uint64_t>(amountSat));
547
- return std::string(status_rs.data(), status_rs.length());
548
- }
549
- catch (const rust::Error &e)
550
- {
551
- throw std::runtime_error(e.what());
552
- }
553
- });
554
- }
555
-
556
- // --- Offboarding / Exiting ---
557
-
558
- std::shared_ptr<Promise<std::string>>
559
- offboardSpecific(const std::vector<std::string> &vtxoIds,
560
- const std::string &destinationAddress) override
561
- {
562
- return Promise<std::string>::async(
563
- [vtxoIds, destinationAddress]()
564
- {
565
- try
566
- {
567
- rust::Vec<rust::String> rust_vtxo_ids;
568
- for (const auto &id : vtxoIds)
569
- {
570
- rust_vtxo_ids.push_back(rust::String(id));
571
- }
572
- rust::String status_rs = bark_cxx::offboard_specific(std::move(rust_vtxo_ids), destinationAddress);
573
- return std::string(status_rs.data(), status_rs.length());
574
- }
575
- catch (const rust::Error &e)
576
- {
577
- throw std::runtime_error(e.what());
578
- }
579
- });
580
- }
581
-
582
- std::shared_ptr<Promise<std::string>>
583
- offboardAll(const std::string &destinationAddress) override
584
- {
585
- return Promise<std::string>::async([destinationAddress]()
586
- {
587
- try {
588
- rust::String status_rs = bark_cxx::offboard_all(destinationAddress);
589
- return std::string(status_rs.data(), status_rs.length());
590
- } catch (const rust::Error &e) {
591
- throw std::runtime_error(e.what());
592
- } });
593
- }
594
-
595
- private:
596
- // Tag for logging/debugging within Nitro
597
- static constexpr auto TAG = "NitroArk";
598
- };
11
+ namespace margelo::nitro::nitroark {
12
+
13
+ using namespace margelo::nitro;
14
+ // Helper function to convert rust cxx payment type to nitrogen payment type
15
+ inline PaymentTypes convertPaymentType(bark_cxx::PaymentTypes type) {
16
+ switch (type) {
17
+ case bark_cxx::PaymentTypes::Bolt11:
18
+ return PaymentTypes::BOLT11;
19
+ case bark_cxx::PaymentTypes::Lnurl:
20
+ return PaymentTypes::LNURL;
21
+ case bark_cxx::PaymentTypes::Arkoor:
22
+ return PaymentTypes::ARKOOR;
23
+ case bark_cxx::PaymentTypes::Onchain:
24
+ return PaymentTypes::ONCHAIN;
25
+ default:
26
+ throw std::runtime_error("Invalid payment type");
27
+ }
28
+ }
29
+
30
+ class NitroArk : public HybridNitroArkSpec {
31
+ public:
32
+ NitroArk() : HybridObject(TAG) {
33
+ // Initialize the Rust logger once when a NitroArk object is created.
34
+ bark_cxx::init_logger();
35
+ }
36
+
37
+ // --- Management ---
38
+
39
+ std::shared_ptr<Promise<std::string>> createMnemonic() override {
40
+ return Promise<std::string>::async([]() {
41
+ try {
42
+ rust::String mnemonic_rs = bark_cxx::create_mnemonic();
43
+ return std::string(mnemonic_rs.data(), mnemonic_rs.length());
44
+ } catch (const rust::Error &e) {
45
+ throw std::runtime_error(e.what());
46
+ }
47
+ });
48
+ }
49
+
50
+ std::shared_ptr<Promise<void>>
51
+ loadWallet(const std::string &datadir, const BarkCreateOpts &opts) override {
52
+ return Promise<void>::async([datadir, opts]() {
53
+ try {
54
+ bark_cxx::ConfigOpts config_opts;
55
+ if (opts.config.has_value()) {
56
+ config_opts.asp = opts.config->asp.value_or("");
57
+ config_opts.esplora = opts.config->esplora.value_or("");
58
+ config_opts.bitcoind = opts.config->bitcoind.value_or("");
59
+ config_opts.bitcoind_cookie =
60
+ opts.config->bitcoind_cookie.value_or("");
61
+ config_opts.bitcoind_user = opts.config->bitcoind_user.value_or("");
62
+ config_opts.bitcoind_pass = opts.config->bitcoind_pass.value_or("");
63
+ config_opts.vtxo_refresh_expiry_threshold = static_cast<uint32_t>(
64
+ opts.config->vtxo_refresh_expiry_threshold.value_or(0));
65
+ config_opts.fallback_fee_rate =
66
+ static_cast<uint64_t>(opts.config->fallback_fee_rate.value_or(0));
67
+ }
68
+
69
+ bark_cxx::CreateOpts create_opts;
70
+ create_opts.regtest = opts.regtest.value_or(false);
71
+ create_opts.signet = opts.signet.value_or(false);
72
+ create_opts.bitcoin = opts.bitcoin.value_or(true);
73
+ create_opts.mnemonic = opts.mnemonic;
74
+ uint32_t birthday_height_val;
75
+ if (opts.birthday_height.has_value()) {
76
+ birthday_height_val =
77
+ static_cast<uint32_t>(opts.birthday_height.value());
78
+ create_opts.birthday_height = &birthday_height_val;
79
+ } else {
80
+ create_opts.birthday_height = nullptr;
81
+ }
82
+ create_opts.config = config_opts;
83
+
84
+ bark_cxx::load_wallet(datadir, create_opts);
85
+ } catch (const rust::Error &e) {
86
+ throw std::runtime_error(e.what());
87
+ }
88
+ });
89
+ }
90
+
91
+ std::shared_ptr<Promise<void>> closeWallet() override {
92
+ return Promise<void>::async([]() {
93
+ try {
94
+ bark_cxx::close_wallet();
95
+ } catch (const rust::Error &e) {
96
+ throw std::runtime_error(e.what());
97
+ }
98
+ });
99
+ }
100
+
101
+ std::shared_ptr<Promise<bool>> isWalletLoaded() override {
102
+ return Promise<bool>::async([]() { return bark_cxx::is_wallet_loaded(); });
103
+ }
104
+
105
+ std::shared_ptr<Promise<void>>
106
+ persistConfig(const BarkConfigOpts &opts) override {
107
+ return Promise<void>::async([opts]() {
108
+ try {
109
+ bark_cxx::ConfigOpts config_opts;
110
+ config_opts.asp = opts.asp.value_or("");
111
+ config_opts.esplora = opts.esplora.value_or("");
112
+ config_opts.bitcoind = opts.bitcoind.value_or("");
113
+ config_opts.bitcoind_cookie = opts.bitcoind_cookie.value_or("");
114
+ config_opts.bitcoind_user = opts.bitcoind_user.value_or("");
115
+ config_opts.bitcoind_pass = opts.bitcoind_pass.value_or("");
116
+ config_opts.vtxo_refresh_expiry_threshold = static_cast<uint32_t>(
117
+ opts.vtxo_refresh_expiry_threshold.value_or(0));
118
+ config_opts.fallback_fee_rate =
119
+ static_cast<uint64_t>(opts.fallback_fee_rate.value_or(0));
120
+ bark_cxx::persist_config(config_opts);
121
+ } catch (const rust::Error &e) {
122
+ throw std::runtime_error(e.what());
123
+ }
124
+ });
125
+ }
126
+
127
+ std::shared_ptr<Promise<void>> maintenance() override {
128
+ return Promise<void>::async([]() {
129
+ try {
130
+ bark_cxx::maintenance();
131
+ } catch (const rust::Error &e) {
132
+ throw std::runtime_error(e.what());
133
+ }
134
+ });
135
+ }
136
+
137
+ std::shared_ptr<Promise<void>> sync() override {
138
+ return Promise<void>::async([]() {
139
+ try {
140
+ bark_cxx::sync();
141
+ } catch (const rust::Error &e) {
142
+ throw std::runtime_error(e.what());
143
+ }
144
+ });
145
+ }
146
+
147
+ std::shared_ptr<Promise<void>> syncExits() override {
148
+ return Promise<void>::async([]() {
149
+ try {
150
+ bark_cxx::sync_exits();
151
+ } catch (const rust::Error &e) {
152
+ throw std::runtime_error(e.what());
153
+ }
154
+ });
155
+ }
156
+
157
+ std::shared_ptr<Promise<void>> syncRounds() override {
158
+ return Promise<void>::async([]() {
159
+ try {
160
+ bark_cxx::sync_rounds();
161
+ } catch (const rust::Error &e) {
162
+ throw std::runtime_error(e.what());
163
+ }
164
+ });
165
+ }
166
+
167
+ // --- Wallet Info ---
168
+
169
+ std::shared_ptr<Promise<BarkArkInfo>> getArkInfo() override {
170
+ return Promise<BarkArkInfo>::async([]() {
171
+ try {
172
+ bark_cxx::CxxArkInfo rust_info = bark_cxx::get_ark_info();
173
+ BarkArkInfo info;
174
+ info.network =
175
+ std::string(rust_info.network.data(), rust_info.network.length());
176
+ info.asp_pubkey = std::string(rust_info.asp_pubkey.data(),
177
+ rust_info.asp_pubkey.length());
178
+ info.round_interval_secs =
179
+ static_cast<double>(rust_info.round_interval_secs);
180
+ info.vtxo_exit_delta = static_cast<double>(rust_info.vtxo_exit_delta);
181
+ info.vtxo_expiry_delta =
182
+ static_cast<double>(rust_info.vtxo_expiry_delta);
183
+ info.htlc_expiry_delta =
184
+ static_cast<double>(rust_info.htlc_expiry_delta);
185
+ info.max_vtxo_amount_sat =
186
+ static_cast<double>(rust_info.max_vtxo_amount_sat);
187
+ return info;
188
+ } catch (const rust::Error &e) {
189
+ throw std::runtime_error(e.what());
190
+ }
191
+ });
192
+ }
193
+
194
+ std::shared_ptr<Promise<OffchainBalanceResult>> offchainBalance() override {
195
+ return Promise<OffchainBalanceResult>::async([]() {
196
+ try {
197
+ bark_cxx::OffchainBalance rust_balance = bark_cxx::offchain_balance();
198
+ OffchainBalanceResult balance;
199
+ balance.spendable = static_cast<double>(rust_balance.spendable);
200
+ balance.pending_lightning_send =
201
+ static_cast<double>(rust_balance.pending_lightning_send);
202
+ balance.pending_exit = static_cast<double>(rust_balance.pending_exit);
203
+ return balance;
204
+ } catch (const rust::Error &e) {
205
+ throw std::runtime_error(e.what());
206
+ }
207
+ });
208
+ }
209
+
210
+ std::shared_ptr<Promise<std::string>> deriveStoreNextKeypair() override {
211
+ return Promise<std::string>::async([]() {
212
+ try {
213
+ rust::String keypair_rs = bark_cxx::derive_store_next_keypair();
214
+ return std::string(keypair_rs.data(), keypair_rs.length());
215
+ } catch (const rust::Error &e) {
216
+ throw std::runtime_error(e.what());
217
+ }
218
+ });
219
+ }
220
+
221
+ std::shared_ptr<Promise<std::string>> peakKeyPair(double index) override {
222
+ return Promise<std::string>::async([index]() {
223
+ try {
224
+ uint32_t index_val = static_cast<uint32_t>(index);
225
+ rust::String keypair_rs = bark_cxx::peak_keypair(index_val);
226
+ return std::string(keypair_rs.data(), keypair_rs.length());
227
+ } catch (const rust::Error &e) {
228
+ throw std::runtime_error(e.what());
229
+ }
230
+ });
231
+ }
232
+
233
+ std::shared_ptr<Promise<NewAddressResult>> newAddress() override {
234
+ return Promise<NewAddressResult>::async([]() {
235
+ try {
236
+ bark_cxx::NewAddressResult address_rs = bark_cxx::new_address();
237
+ NewAddressResult address;
238
+ address.user_pubkey = std::string(address_rs.user_pubkey.data(),
239
+ address_rs.user_pubkey.length());
240
+ address.ark_id =
241
+ std::string(address_rs.ark_id.data(), address_rs.ark_id.length());
242
+ address.address =
243
+ std::string(address_rs.address.data(), address_rs.address.length());
244
+ return address;
245
+
246
+ } catch (const rust::Error &e) {
247
+ throw std::runtime_error(e.what());
248
+ }
249
+ });
250
+ }
251
+
252
+ std::shared_ptr<Promise<std::vector<BarkVtxo>>> getVtxos() override {
253
+ return Promise<std::vector<BarkVtxo>>::async([]() {
254
+ try {
255
+ rust::Vec<bark_cxx::BarkVtxo> rust_vtxos = bark_cxx::get_vtxos();
256
+ std::vector<BarkVtxo> vtxos;
257
+ for (const auto &rust_vtxo : rust_vtxos) {
258
+ BarkVtxo vtxo;
259
+ vtxo.amount = static_cast<double>(rust_vtxo.amount);
260
+ vtxo.expiry_height = static_cast<double>(rust_vtxo.expiry_height);
261
+ vtxo.exit_delta = static_cast<double>(rust_vtxo.exit_delta);
262
+ vtxo.anchor_point = std::string(rust_vtxo.anchor_point.data(),
263
+ rust_vtxo.anchor_point.length());
264
+ vtxos.push_back(vtxo);
265
+ }
266
+ return vtxos;
267
+ } catch (const rust::Error &e) {
268
+ throw std::runtime_error(e.what());
269
+ }
270
+ });
271
+ }
272
+
273
+ // --- Onchain Operations ---
274
+
275
+ std::shared_ptr<Promise<OnchainBalanceResult>> onchainBalance() override {
276
+ return Promise<OnchainBalanceResult>::async([]() {
277
+ try {
278
+ bark_cxx::OnChainBalance rust_balance = bark_cxx::onchain_balance();
279
+ OnchainBalanceResult balance;
280
+ balance.immature = static_cast<double>(rust_balance.immature);
281
+ balance.trusted_pending =
282
+ static_cast<double>(rust_balance.trusted_pending);
283
+ balance.untrusted_pending =
284
+ static_cast<double>(rust_balance.untrusted_pending);
285
+ balance.confirmed = static_cast<double>(rust_balance.confirmed);
286
+ return balance;
287
+ } catch (const rust::Error &e) {
288
+ throw std::runtime_error(e.what());
289
+ }
290
+ });
291
+ }
292
+
293
+ std::shared_ptr<Promise<void>> onchainSync() override {
294
+ return Promise<void>::async([]() {
295
+ try {
296
+ bark_cxx::onchain_sync();
297
+ } catch (const rust::Error &e) {
298
+ throw std::runtime_error(e.what());
299
+ }
300
+ });
301
+ }
302
+
303
+ std::shared_ptr<Promise<std::string>> onchainListUnspent() override {
304
+ return Promise<std::string>::async([]() {
305
+ try {
306
+ rust::String json_rs = bark_cxx::onchain_list_unspent();
307
+ return std::string(json_rs.data(), json_rs.length());
308
+ } catch (const rust::Error &e) {
309
+ throw std::runtime_error(e.what());
310
+ }
311
+ });
312
+ }
313
+
314
+ std::shared_ptr<Promise<std::string>> onchainUtxos() override {
315
+ return Promise<std::string>::async([]() {
316
+ try {
317
+ rust::String json_rs = bark_cxx::onchain_utxos();
318
+ return std::string(json_rs.data(), json_rs.length());
319
+ } catch (const rust::Error &e) {
320
+ throw std::runtime_error(e.what());
321
+ }
322
+ });
323
+ }
324
+
325
+ std::shared_ptr<Promise<std::string>> onchainAddress() override {
326
+ return Promise<std::string>::async([]() {
327
+ try {
328
+ rust::String address_rs = bark_cxx::onchain_address();
329
+ return std::string(address_rs.data(), address_rs.length());
330
+ } catch (const rust::Error &e) {
331
+ throw std::runtime_error(e.what());
332
+ }
333
+ });
334
+ }
335
+
336
+ std::shared_ptr<Promise<OnchainPaymentResult>>
337
+ onchainSend(const std::string &destination, double amountSat,
338
+ std::optional<double> feeRate) override {
339
+ return Promise<OnchainPaymentResult>::async(
340
+ [destination, amountSat, feeRate]() {
341
+ try {
342
+ uint64_t feeRate_val;
343
+ bark_cxx::OnchainPaymentResult rust_result;
344
+ if (feeRate.has_value()) {
345
+ feeRate_val = static_cast<uint64_t>(feeRate.value());
346
+ rust_result = bark_cxx::onchain_send(
347
+ destination, static_cast<uint64_t>(amountSat), &feeRate_val);
348
+ } else {
349
+ rust_result = bark_cxx::onchain_send(
350
+ destination, static_cast<uint64_t>(amountSat), nullptr);
351
+ }
352
+
353
+ OnchainPaymentResult result;
354
+ result.txid =
355
+ std::string(rust_result.txid.data(), rust_result.txid.length());
356
+ result.amount_sat = static_cast<double>(rust_result.amount_sat);
357
+ result.destination_address =
358
+ std::string(rust_result.destination_address.data(),
359
+ rust_result.destination_address.length());
360
+ result.payment_type = convertPaymentType(rust_result.payment_type);
361
+
362
+ return result;
363
+ } catch (const rust::Error &e) {
364
+ throw std::runtime_error(e.what());
365
+ }
366
+ });
367
+ }
368
+
369
+ std::shared_ptr<Promise<std::string>>
370
+ onchainDrain(const std::string &destination,
371
+ std::optional<double> feeRate) override {
372
+ return Promise<std::string>::async([destination, feeRate]() {
373
+ try {
374
+ uint64_t feeRate_val;
375
+ rust::String txid_rs;
376
+ if (feeRate.has_value()) {
377
+ feeRate_val = static_cast<uint64_t>(feeRate.value());
378
+ txid_rs = bark_cxx::onchain_drain(destination, &feeRate_val);
379
+ } else {
380
+ txid_rs = bark_cxx::onchain_drain(destination, nullptr);
381
+ }
382
+ return std::string(txid_rs.data(), txid_rs.length());
383
+ } catch (const rust::Error &e) {
384
+ throw std::runtime_error(e.what());
385
+ }
386
+ });
387
+ }
388
+
389
+ std::shared_ptr<Promise<std::string>>
390
+ onchainSendMany(const std::vector<BarkSendManyOutput> &outputs,
391
+ std::optional<double> feeRate) override {
392
+ return Promise<std::string>::async([outputs, feeRate]() {
393
+ try {
394
+ rust::Vec<bark_cxx::SendManyOutput> cxx_outputs;
395
+ for (const auto &output : outputs) {
396
+ cxx_outputs.push_back({rust::String(output.destination),
397
+ static_cast<uint64_t>(output.amountSat)});
398
+ }
399
+ uint64_t feeRate_val;
400
+ rust::String txid_rs;
401
+ if (feeRate.has_value()) {
402
+ feeRate_val = static_cast<uint64_t>(feeRate.value());
403
+ txid_rs =
404
+ bark_cxx::onchain_send_many(std::move(cxx_outputs), &feeRate_val);
405
+ } else {
406
+ txid_rs =
407
+ bark_cxx::onchain_send_many(std::move(cxx_outputs), nullptr);
408
+ }
409
+ return std::string(txid_rs.data(), txid_rs.length());
410
+ } catch (const rust::Error &e) {
411
+ throw std::runtime_error(e.what());
412
+ }
413
+ });
414
+ }
415
+
416
+ // --- Lightning Operations ---
417
+
418
+ std::shared_ptr<Promise<LightningPaymentResult>>
419
+ sendLightningPayment(const std::string &destination,
420
+ std::optional<double> amountSat) override {
421
+ return Promise<LightningPaymentResult>::async([destination, amountSat]() {
422
+ try {
423
+ bark_cxx::Bolt11PaymentResult rust_result;
424
+ if (amountSat.has_value()) {
425
+ uint64_t amountSat_val = static_cast<uint64_t>(amountSat.value());
426
+ rust_result =
427
+ bark_cxx::send_lightning_payment(destination, &amountSat_val);
428
+ } else {
429
+ rust_result = bark_cxx::send_lightning_payment(destination, nullptr);
430
+ }
431
+
432
+ LightningPaymentResult result;
433
+ result.bolt11_invoice =
434
+ std::string(rust_result.bolt11_invoice.data(),
435
+ rust_result.bolt11_invoice.length());
436
+ result.preimage = std::string(rust_result.preimage.data(),
437
+ rust_result.preimage.length());
438
+ result.payment_type = convertPaymentType(rust_result.payment_type);
439
+
440
+ return result;
441
+ } catch (const rust::Error &e) {
442
+ throw std::runtime_error(e.what());
443
+ }
444
+ });
445
+ }
446
+
447
+ std::shared_ptr<Promise<LnurlPaymentResult>>
448
+ sendLnaddr(const std::string &addr, double amountSat,
449
+ const std::string &comment) override {
450
+ return Promise<LnurlPaymentResult>::async([addr, amountSat, comment]() {
451
+ try {
452
+ bark_cxx::LnurlPaymentResult rust_result = bark_cxx::send_lnaddr(
453
+ addr, static_cast<uint64_t>(amountSat), comment);
454
+
455
+ LnurlPaymentResult result;
456
+ result.lnurl =
457
+ std::string(rust_result.lnurl.data(), rust_result.lnurl.length());
458
+ result.bolt11_invoice =
459
+ std::string(rust_result.bolt11_invoice.data(),
460
+ rust_result.bolt11_invoice.length());
461
+ result.preimage = std::string(rust_result.preimage.data(),
462
+ rust_result.preimage.length());
463
+ result.payment_type = convertPaymentType(rust_result.payment_type);
464
+
465
+ return result;
466
+ } catch (const rust::Error &e) {
467
+ throw std::runtime_error(e.what());
468
+ }
469
+ });
470
+ }
471
+
472
+ std::shared_ptr<Promise<std::string>>
473
+ bolt11Invoice(double amountMsat) override {
474
+ return Promise<std::string>::async([amountMsat]() {
475
+ try {
476
+ rust::String invoice_rs =
477
+ bark_cxx::bolt11_invoice(static_cast<uint64_t>(amountMsat));
478
+ return std::string(invoice_rs.data(), invoice_rs.length());
479
+ } catch (const rust::Error &e) {
480
+ throw std::runtime_error(e.what());
481
+ }
482
+ });
483
+ }
484
+
485
+ std::shared_ptr<Promise<void>>
486
+ finishLightningReceive(const std::string &bolt11) override {
487
+ return Promise<void>::async([bolt11]() {
488
+ try {
489
+ bark_cxx::finish_lightning_receive(bolt11);
490
+ } catch (const rust::Error &e) {
491
+ throw std::runtime_error(e.what());
492
+ }
493
+ });
494
+ }
495
+
496
+ // --- Ark Operations ---
497
+
498
+ std::shared_ptr<Promise<std::string>> boardAmount(double amountSat) override {
499
+ return Promise<std::string>::async([amountSat]() {
500
+ try {
501
+ rust::String status_rs =
502
+ bark_cxx::board_amount(static_cast<uint64_t>(amountSat));
503
+ return std::string(status_rs.data(), status_rs.length());
504
+ } catch (const rust::Error &e) {
505
+ throw std::runtime_error(e.what());
506
+ }
507
+ });
508
+ }
509
+
510
+ std::shared_ptr<Promise<std::string>> boardAll() override {
511
+ return Promise<std::string>::async([]() {
512
+ try {
513
+ rust::String status_rs = bark_cxx::board_all();
514
+ return std::string(status_rs.data(), status_rs.length());
515
+ } catch (const rust::Error &e) {
516
+ throw std::runtime_error(e.what());
517
+ }
518
+ });
519
+ }
520
+
521
+ std::shared_ptr<Promise<ArkoorPaymentResult>>
522
+ sendArkoorPayment(const std::string &destination, double amountSat) override {
523
+ return Promise<ArkoorPaymentResult>::async([destination, amountSat]() {
524
+ try {
525
+ bark_cxx::ArkoorPaymentResult rust_result =
526
+ bark_cxx::send_arkoor_payment(destination,
527
+ static_cast<uint64_t>(amountSat));
528
+
529
+ ArkoorPaymentResult result;
530
+ result.amount_sat = static_cast<double>(rust_result.amount_sat);
531
+ result.destination_pubkey =
532
+ std::string(rust_result.destination_pubkey.data(),
533
+ rust_result.destination_pubkey.length());
534
+ result.payment_type = convertPaymentType(rust_result.payment_type);
535
+
536
+ std::vector<BarkVtxo> vtxos;
537
+ for (const auto &rust_vtxo : rust_result.vtxos) {
538
+ BarkVtxo vtxo;
539
+ vtxo.amount = static_cast<double>(rust_vtxo.amount);
540
+ vtxo.expiry_height = static_cast<double>(rust_vtxo.expiry_height);
541
+ vtxo.exit_delta = static_cast<double>(rust_vtxo.exit_delta);
542
+ vtxo.anchor_point = std::string(rust_vtxo.anchor_point.data(),
543
+ rust_vtxo.anchor_point.length());
544
+ vtxos.push_back(vtxo);
545
+ }
546
+ result.vtxos = vtxos;
547
+
548
+ return result;
549
+ } catch (const rust::Error &e) {
550
+ throw std::runtime_error(e.what());
551
+ }
552
+ });
553
+ }
554
+
555
+ std::shared_ptr<Promise<std::string>>
556
+ sendRoundOnchainPayment(const std::string &destination,
557
+ double amountSat) override {
558
+ return Promise<std::string>::async([destination, amountSat]() {
559
+ try {
560
+ rust::String status_rs = bark_cxx::send_round_onchain_payment(
561
+ destination, static_cast<uint64_t>(amountSat));
562
+ return std::string(status_rs.data(), status_rs.length());
563
+ } catch (const rust::Error &e) {
564
+ throw std::runtime_error(e.what());
565
+ }
566
+ });
567
+ }
568
+
569
+ // --- Offboarding / Exiting ---
570
+
571
+ std::shared_ptr<Promise<std::string>>
572
+ offboardSpecific(const std::vector<std::string> &vtxoIds,
573
+ const std::string &destinationAddress) override {
574
+ return Promise<std::string>::async([vtxoIds, destinationAddress]() {
575
+ try {
576
+ rust::Vec<rust::String> rust_vtxo_ids;
577
+ for (const auto &id : vtxoIds) {
578
+ rust_vtxo_ids.push_back(rust::String(id));
579
+ }
580
+ rust::String status_rs = bark_cxx::offboard_specific(
581
+ std::move(rust_vtxo_ids), destinationAddress);
582
+ return std::string(status_rs.data(), status_rs.length());
583
+ } catch (const rust::Error &e) {
584
+ throw std::runtime_error(e.what());
585
+ }
586
+ });
587
+ }
588
+
589
+ std::shared_ptr<Promise<std::string>>
590
+ offboardAll(const std::string &destinationAddress) override {
591
+ return Promise<std::string>::async([destinationAddress]() {
592
+ try {
593
+ rust::String status_rs = bark_cxx::offboard_all(destinationAddress);
594
+ return std::string(status_rs.data(), status_rs.length());
595
+ } catch (const rust::Error &e) {
596
+ throw std::runtime_error(e.what());
597
+ }
598
+ });
599
+ }
600
+
601
+ private:
602
+ // Tag for logging/debugging within Nitro
603
+ static constexpr auto TAG = "NitroArk";
604
+ };
599
605
 
600
606
  } // namespace margelo::nitro::nitroark