react-native-nitro-ark 0.0.38 → 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,563 +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>> syncArk() override
156
- {
157
- return Promise<void>::async([]()
158
- {
159
- try {
160
- bark_cxx::sync_ark();
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<double>> onchainBalance() override
200
- {
201
- return Promise<double>::async([]()
202
- {
203
- try {
204
- return static_cast<double>(bark_cxx::onchain_balance());
205
- } catch (const rust::Error &e) {
206
- throw std::runtime_error(e.what());
207
- } });
208
- }
209
-
210
- std::shared_ptr<Promise<double>> offchainBalance() override
211
- {
212
- return Promise<double>::async([]()
213
- {
214
- try {
215
- return static_cast<double>(bark_cxx::offchain_balance());
216
- } catch (const rust::Error &e) {
217
- throw std::runtime_error(e.what());
218
- } });
219
- }
220
-
221
- std::shared_ptr<Promise<std::string>>
222
- getOnchainAddress() override
223
- {
224
- return Promise<std::string>::async([]()
225
- {
226
- try {
227
- rust::String address_rs = bark_cxx::get_onchain_address();
228
- return std::string(address_rs.data(), address_rs.length());
229
- } catch (const rust::Error &e) {
230
- throw std::runtime_error(e.what());
231
- } });
232
- }
233
-
234
- std::shared_ptr<Promise<std::string>>
235
- getOnchainUtxos(bool no_sync) override
236
- {
237
- return Promise<std::string>::async([no_sync]()
238
- {
239
- try {
240
- rust::String json_rs = bark_cxx::get_onchain_utxos(no_sync);
241
- return std::string(json_rs.data(), json_rs.length());
242
- } catch (const rust::Error &e) {
243
- throw std::runtime_error(e.what());
244
- } });
245
- }
246
-
247
- std::shared_ptr<Promise<std::string>>
248
- getVtxoPubkey(std::optional<double> index) override
249
- {
250
- return Promise<std::string>::async([index]()
251
- {
252
- try {
253
- rust::String pubkey_rs;
254
- if (index.has_value()) {
255
- uint32_t index_val = static_cast<uint32_t>(index.value());
256
- pubkey_rs = bark_cxx::get_vtxo_pubkey(&index_val);
257
- } else {
258
- pubkey_rs = bark_cxx::get_vtxo_pubkey(nullptr);
259
- }
260
- return std::string(pubkey_rs.data(), pubkey_rs.length());
261
- } catch (const rust::Error &e) {
262
- throw std::runtime_error(e.what());
263
- } });
264
- }
265
-
266
- std::shared_ptr<Promise<std::string>> getVtxos(bool no_sync) override
267
- {
268
- return Promise<std::string>::async([no_sync]()
269
- {
270
- try {
271
- rust::String json_rs = bark_cxx::get_vtxos(no_sync);
272
- return std::string(json_rs.data(), json_rs.length());
273
- } catch (const rust::Error &e) {
274
- throw std::runtime_error(e.what());
275
- } });
276
- }
277
-
278
- // --- Onchain Operations ---
279
-
280
- std::shared_ptr<Promise<OnchainPaymentResult>>
281
- sendOnchain(const std::string &destination, double amountSat) override
282
- {
283
- return Promise<OnchainPaymentResult>::async([destination, amountSat]()
284
- {
285
- try {
286
- bark_cxx::OnchainPaymentResult rust_result = bark_cxx::send_onchain(destination, static_cast<uint64_t>(amountSat));
287
-
288
- OnchainPaymentResult result;
289
- result.txid = std::string(rust_result.txid.data(), rust_result.txid.length());
290
- result.amount_sat = static_cast<double>(rust_result.amount_sat);
291
- result.destination_address = std::string(rust_result.destination_address.data(), rust_result.destination_address.length());
292
- result.payment_type = convertPaymentType(rust_result.payment_type);
293
-
294
- return result;
295
- } catch (const rust::Error &e) {
296
- throw std::runtime_error(e.what());
297
- } });
298
- }
299
-
300
- std::shared_ptr<Promise<std::string>>
301
- drainOnchain(const std::string &destination, bool no_sync) override
302
- {
303
- return Promise<std::string>::async(
304
- [destination, no_sync]()
305
- {
306
- try
307
- {
308
- rust::String txid_rs = bark_cxx::drain_onchain(destination, no_sync);
309
- return std::string(txid_rs.data(), txid_rs.length());
310
- }
311
- catch (const rust::Error &e)
312
- {
313
- throw std::runtime_error(e.what());
314
- }
315
- });
316
- }
317
-
318
- std::shared_ptr<Promise<std::string>>
319
- sendManyOnchain(const std::vector<BarkSendManyOutput> &outputs,
320
- bool no_sync) override
321
- {
322
- return Promise<std::string>::async([outputs, no_sync]()
323
- {
324
- try {
325
- rust::Vec<bark_cxx::SendManyOutput> cxx_outputs;
326
- for (const auto &output : outputs) {
327
- cxx_outputs.push_back({rust::String(output.destination), static_cast<uint64_t>(output.amountSat)});
328
- }
329
- rust::String txid_rs = bark_cxx::send_many_onchain(std::move(cxx_outputs), no_sync);
330
- return std::string(txid_rs.data(), txid_rs.length());
331
- } catch (const rust::Error &e) {
332
- throw std::runtime_error(e.what());
333
- } });
334
- }
335
-
336
- // --- Ark & Lightning Payments ---
337
-
338
- std::shared_ptr<Promise<std::string>> boardAmount(double amountSat) override
339
- {
340
- return Promise<std::string>::async([amountSat]()
341
- {
342
- try {
343
- rust::String status_rs = bark_cxx::board_amount(static_cast<uint64_t>(amountSat));
344
- return std::string(status_rs.data(), status_rs.length());
345
- } catch (const rust::Error &e) {
346
- throw std::runtime_error(e.what());
347
- } });
348
- }
349
-
350
- std::shared_ptr<Promise<std::string>> boardAll() override
351
- {
352
- return Promise<std::string>::async([]()
353
- {
354
- try {
355
- rust::String status_rs = bark_cxx::board_all();
356
- return std::string(status_rs.data(), status_rs.length());
357
- } catch (const rust::Error &e) {
358
- throw std::runtime_error(e.what());
359
- } });
360
- }
361
-
362
- std::shared_ptr<Promise<ArkoorPaymentResult>>
363
- sendArkoorPayment(const std::string &destination, double amountSat) override
364
- {
365
- return Promise<ArkoorPaymentResult>::async([destination, amountSat]()
366
- {
367
- try {
368
- bark_cxx::ArkoorPaymentResult rust_result = bark_cxx::send_arkoor_payment(destination, static_cast<uint64_t>(amountSat));
369
-
370
- ArkoorPaymentResult result;
371
- result.amount_sat = static_cast<double>(rust_result.amount_sat);
372
- result.destination_pubkey = std::string(rust_result.destination_pubkey.data(), rust_result.destination_pubkey.length());
373
- result.payment_type = convertPaymentType(rust_result.payment_type);
374
-
375
- std::vector<BarkVtxo> vtxos;
376
- for (const auto& rust_vtxo : rust_result.vtxos) {
377
- BarkVtxo vtxo;
378
- vtxo.amount = static_cast<double>(rust_vtxo.amount);
379
- vtxo.expiry_height = static_cast<double>(rust_vtxo.expiry_height);
380
- vtxo.exit_delta = static_cast<double>(rust_vtxo.exit_delta);
381
- vtxo.anchor_point = std::string(rust_vtxo.anchor_point.data(), rust_vtxo.anchor_point.length());
382
- vtxos.push_back(vtxo);
383
- }
384
- result.vtxos = vtxos;
385
-
386
- return result;
387
- } catch (const rust::Error &e) {
388
- throw std::runtime_error(e.what());
389
- } });
390
- }
391
-
392
- std::shared_ptr<Promise<Bolt11PaymentResult>>
393
- sendBolt11Payment(const std::string &destination, std::optional<double> amountSat) override
394
- {
395
- return Promise<Bolt11PaymentResult>::async([destination, amountSat]()
396
- {
397
- try {
398
- bark_cxx::Bolt11PaymentResult rust_result;
399
- if (amountSat.has_value()) {
400
- uint64_t amountSat_val = static_cast<uint64_t>(amountSat.value());
401
- rust_result = bark_cxx::send_bolt11_payment(destination, &amountSat_val);
402
- } else {
403
- rust_result = bark_cxx::send_bolt11_payment(destination, nullptr);
404
- }
405
-
406
- Bolt11PaymentResult result;
407
- result.bolt11_invoice = std::string(rust_result.bolt11_invoice.data(), rust_result.bolt11_invoice.length());
408
- result.preimage = std::string(rust_result.preimage.data(), rust_result.preimage.length());
409
- result.payment_type = convertPaymentType(rust_result.payment_type);
410
-
411
- return result;
412
- } catch (const rust::Error &e) {
413
- throw std::runtime_error(e.what());
414
- } });
415
- }
416
-
417
- std::shared_ptr<Promise<LnurlPaymentResult>>
418
- sendLnaddr(const std::string &addr, double amountSat, const std::string &comment) override
419
- {
420
- return Promise<LnurlPaymentResult>::async([addr, amountSat, comment]()
421
- {
422
- try {
423
- bark_cxx::LnurlPaymentResult rust_result = bark_cxx::send_lnaddr(addr, static_cast<uint64_t>(amountSat), comment);
424
-
425
- LnurlPaymentResult result;
426
- result.lnurl = std::string(rust_result.lnurl.data(), rust_result.lnurl.length());
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<std::string>>
438
- sendRoundOnchain(const std::string &destination, double amountSat,
439
- bool no_sync) override
440
- {
441
- return Promise<std::string>::async(
442
- [destination, amountSat, no_sync]()
443
- {
444
- try
445
- {
446
- rust::String status_rs = bark_cxx::send_round_onchain(destination, static_cast<uint64_t>(amountSat), no_sync);
447
- return std::string(status_rs.data(), status_rs.length());
448
- }
449
- catch (const rust::Error &e)
450
- {
451
- throw std::runtime_error(e.what());
452
- }
453
- });
454
- }
455
-
456
- // --- Lightning Invoicing ---
457
-
458
- std::shared_ptr<Promise<std::string>>
459
- bolt11Invoice(double amountMsat) override
460
- {
461
- return Promise<std::string>::async([amountMsat]()
462
- {
463
- try {
464
- rust::String invoice_rs = bark_cxx::bolt11_invoice(static_cast<uint64_t>(amountMsat));
465
- return std::string(invoice_rs.data(), invoice_rs.length());
466
- } catch (const rust::Error &e) {
467
- throw std::runtime_error(e.what());
468
- } });
469
- }
470
-
471
- std::shared_ptr<Promise<void>>
472
- claimBolt11Payment(const std::string &bolt11) override
473
- {
474
- return Promise<void>::async([bolt11]()
475
- {
476
- try {
477
- bark_cxx::claim_bolt11_payment(bolt11);
478
- } catch (const rust::Error &e) {
479
- throw std::runtime_error(e.what());
480
- } });
481
- }
482
-
483
- // --- Offboarding / Exiting ---
484
-
485
- std::shared_ptr<Promise<std::string>>
486
- offboardSpecific(const std::vector<std::string> &vtxoIds,
487
- const std::string &destinationAddress) override
488
- {
489
- return Promise<std::string>::async(
490
- [vtxoIds, destinationAddress]()
491
- {
492
- try
493
- {
494
- rust::Vec<rust::String> rust_vtxo_ids;
495
- for (const auto &id : vtxoIds)
496
- {
497
- rust_vtxo_ids.push_back(rust::String(id));
498
- }
499
- rust::String status_rs = bark_cxx::offboard_specific(std::move(rust_vtxo_ids), destinationAddress);
500
- return std::string(status_rs.data(), status_rs.length());
501
- }
502
- catch (const rust::Error &e)
503
- {
504
- throw std::runtime_error(e.what());
505
- }
506
- });
507
- }
508
-
509
- std::shared_ptr<Promise<std::string>>
510
- offboardAll(const std::string &destinationAddress) override
511
- {
512
- return Promise<std::string>::async([destinationAddress]()
513
- {
514
- try {
515
- rust::String status_rs = bark_cxx::offboard_all(destinationAddress);
516
- return std::string(status_rs.data(), status_rs.length());
517
- } catch (const rust::Error &e) {
518
- throw std::runtime_error(e.what());
519
- } });
520
- }
521
-
522
- std::shared_ptr<Promise<std::string>> exitStartSpecific(
523
- const std::vector<std::string> &vtxoIds) override
524
- {
525
- return Promise<std::string>::async([vtxoIds]()
526
- {
527
- try {
528
- rust::Vec<rust::String> rust_vtxo_ids;
529
- for (const auto &id : vtxoIds)
530
- {
531
- rust_vtxo_ids.push_back(rust::String(id));
532
- }
533
- rust::String status_rs = bark_cxx::start_exit_for_vtxos(std::move(rust_vtxo_ids));
534
- return std::string(status_rs.data(), status_rs.length());
535
- } catch (const rust::Error &e) {
536
- throw std::runtime_error(e.what());
537
- } });
538
- }
539
-
540
- std::shared_ptr<Promise<void>>
541
- startExitForEntireWallet() override
542
- {
543
- return Promise<void>::async([]()
544
- {
545
- try {
546
- bark_cxx::start_exit_for_entire_wallet();
547
- } catch (const rust::Error &e) {
548
- throw std::runtime_error(e.what());
549
- } });
550
- }
551
-
552
- std::shared_ptr<Promise<std::string>>
553
- exitProgressOnce() override
554
- {
555
- return Promise<std::string>::async([]()
556
- {
557
- try {
558
- rust::String status_rs = bark_cxx::exit_progress_once();
559
- return std::string(status_rs.data(), status_rs.length());
560
- } catch (const rust::Error &e) {
561
- throw std::runtime_error(e.what());
562
- } });
563
- }
564
-
565
- private:
566
- // Tag for logging/debugging within Nitro
567
- static constexpr auto TAG = "NitroArk";
568
- };
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
+ };
569
605
 
570
606
  } // namespace margelo::nitro::nitroark