bark-cli 0.1.0-beta.9

CLI for the bitcoin Ark protocol built by Second
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#[macro_use] extern crate anyhow;

mod dev;
mod exit;
mod lightning;
mod onchain;
mod round;

use std::cmp::Ordering;
use std::{env, process};
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::Context;
use bark::movement::PaymentMethod;
use bark_cli::VERSION_DIRTY;
use bitcoin::{Amount};
use clap::builder::BoolishValueParser;
use clap::Parser;
use ::lightning::offers::offer::Offer;
use lightning_invoice::Bolt11Invoice;
use lnurl::lightning_address::LightningAddress;
use log::{debug, info, warn};

use ark::VtxoId;
use ark::lightning::PaymentHash;
use bark::Wallet;
use bark::onchain::ChainSync;
use bark::pid_lock::PidLock;
use bark::vtxo::{VtxoFilter, VtxoStateKind};
use bark_json::{cli as json};
use bark_json::primitives::WalletVtxoInfo;

use bark_cli::wallet::{CreateOpts, create_wallet, open_wallet};
use bark_cli::log::init_logging;
use bark_cli::util::output_json;


fn default_datadir() -> String {
	home::home_dir().or_else(|| {
		env::current_dir().ok()
	}).unwrap_or_else(|| {
		"./".into()
	}).join(".bark").display().to_string()
}

/// The full version string we show in our binary.
/// (BARK_VERSION and GIT_HASH are set in build.rs)
const FULL_VERSION: &str = concat!(env!("BARK_VERSION"), " (", env!("GIT_HASH"), ")");

#[derive(Parser)]
#[command(name = "bark", author = "Team Second <hello@second.tech>", version = FULL_VERSION, about)]
struct Cli {
	/// Enable verbose logging
	#[arg(
		long,
		short = 'v',
		env = "BARK_VERBOSE",
		global = true,
		value_parser = BoolishValueParser::new(),
	)]
	verbose: bool,
	/// Disable all terminal logging
	#[arg(
		long,
		short = 'q',
		env = "BARK_QUIET",
		global = true,
		value_parser = BoolishValueParser::new(),
	)]
	quiet: bool,

	/// The datadir of the bark wallet
	#[arg(long, env = "BARK_DATADIR", global = true, default_value_t = default_datadir())]
	datadir: String,

	#[command(subcommand)]
	command: Command,
}


#[derive(clap::Args)]
#[group(required = true, multiple = false)]
struct AddressLookupFilter {
	/// The Ark address to look up
	#[arg(long)]
	address: Option<ark::Address>,
	/// Address pubkey index to look up
	#[arg(long)]
	index: Option<u32>,
}

#[derive(clap::Subcommand)]
enum AddressCommand {
	/// Look up receives for an Ark address
	Lookup {
		#[clap(flatten)]
		filter: AddressLookupFilter,
	},
}

#[derive(clap::Subcommand)]
enum Command {
	/// Create a new wallet
	///
	/// Configuration will pass in default values when --signet is used, but will
	/// require full configuration for regtest
	#[command()]
	Create(CreateOpts),

	/// Print the configuration of your bark wallet
	#[command()]
	Config,

	/// Prints information related to the Ark Server
	#[command()]
	ArkInfo,

	/// Get an address to receive VTXOs
	#[command()]
	Address {
		/// Address pubkey index to peek
		#[arg(long)]
		index: Option<u32>,

		#[command(subcommand)]
		subcommand: Option<AddressCommand>,
	},

	/// Get the wallet balance
	#[command()]
	Balance {
		/// Skip syncing before computing balance
		#[arg(long)]
		no_sync: bool,
	},

	/// List the wallet's VTXOs
	#[command()]
	Vtxos {
		/// Skip syncing before fetching VTXOs
		#[arg(long)]
		no_sync: bool,
		/// Returns all VTXOs regardless of their state
		#[arg(long)]
		all: bool,
	},

	/// List the wallet's payments
	///
	/// By default will fetch the 10 first items
	#[command(alias="movements")]
	History {
		/// Skip syncing wallet
		#[arg(long)]
		no_sync: bool,
	},

	/// Refresh expiring VTXOs
	///
	/// By default the wallet's configured threshold is used
	#[command()]
	Refresh {
		/// The ID of a VTXO to be refreshed, can be specified multiple times.
		#[arg(long = "vtxo", value_name = "VTXO_ID")]
		vtxos: Option<Vec<String>>,
		/// Refresh VTXOs that expire within this amount of blocks
		#[arg(long)]
		threshold_blocks: Option<u32>,
		/// Refresh VTXOs that expire within this number of hours
		#[arg(long)]
		threshold_hours: Option<u32>,
		/// Force refresh all VTXOs regardless of expiry height
		#[arg(long)]
		all: bool,
		/// Force refresh all VTXOs that have some counterparty risk,
		/// regardless of expiry height
		#[arg(long)]
		counterparty: bool,
		/// Perform the refresh in delegated (non-interactive) mode
		#[arg(long)]
		delegated: bool,
		/// Skip syncing wallet
		#[arg(long)]
		no_sync: bool,
	},

	/// Board from the onchain wallet into the Ark
	#[command()]
	Board {
		/// Optional amount of on-chain funds to board.
		///
		/// Provided value must match format `<amount> <unit>`, where unit can be any amount denomination. Example: `250000 sats`.
		///
		/// Either this or --all should be provided.
		amount: Option<Amount>,
		/// Whether or not all funds in on-chain wallet should be boarded
		#[arg(long)]
		all: bool,
		/// Skip syncing wallet before board
		#[arg(long)]
		no_sync: bool,
	},

	/// Send money using Ark
	#[command()]
	Send {
		/// The destination can be an Ark address, a BOLT11-invoice, LNURL or a lightning address
		destination: String,
		/// The amount to send (optional for bolt11)
		///
		/// Provided value must match format `<amount> <unit>`, where unit can be any amount denomination. Example: `250000 sats`.
		amount: Option<Amount>,
		/// An optional comment
		comment: Option<String>,
		/// Skip syncing wallet
		#[arg(long)]
		no_sync: bool,
		/// Wait for the payment to be completed
		#[arg(long)]
		wait: bool
	},

	/// Send money from your vtxo's to an onchain address
	/// This method requires to wait for a round
	#[command()]
	SendOnchain {
		/// The bitcoin address to which money will be sent
		destination: String,
		/// Amount to send.
		///
		/// Provided value must match format `<amount> <unit>`, where unit can be any amount denomination. Example: `250000 sats`.
		amount: Amount,
		/// Skip syncing wallet
		#[arg(long)]
		no_sync: bool,
	},

	/// Turn VTXOs into UTXOs
	/// This command sends
	#[command()]
	Offboard {
		/// Optional address to receive offboarded VTXOs. If no address is provided, one will be
		/// generated from the onchain wallet
		#[arg(long)]
		address: Option<String>,
		/// Optional ID of a VTXO to offboard, this can be specified multiple times.
		/// Either this or --all should be provided
		#[arg(long = "vtxo", value_name = "VTXO_ID")]
		vtxos: Option<Vec<String>>,
		/// Whether or not all VTXOs should be offboarded. Either this or --vtxos should be provided
		#[arg(long)]
		all: bool,
		/// Skip syncing wallet
		#[arg(long)]
		no_sync: bool,
	},

	/// Use the built-in onchain wallet
	#[command(subcommand)]
	Onchain(onchain::OnchainCommand),

	/// Perform a unilateral exit from the Ark
	#[command(subcommand)]
	Exit(exit::ExitCommand),

	/// Perform any lightning-related command
	#[command(subcommand, visible_alias = "ln")]
	Lightning(lightning::LightningCommand),

	/// round-related commands
	#[command(subcommand)]
	Round(round::RoundCommand),

	/// Run wallet maintenence
	///
	/// This includes onchain sync, offchain sync, registering boards with the server,
	/// syncing Lightning VTXOs, syncing exits, and refreshing soon-to-expire VTXOs
	#[command()]
	Maintain {
		/// Perform the maintenance in delegated (non-interactive) mode
		#[arg(long)]
		delegated: bool,
	},

	/// developer commands
	#[command(subcommand)]
	Dev(dev::DevCommand),
}


async fn inner_main(cli: Cli) -> anyhow::Result<()> {
	let datadir = PathBuf::from_str(&cli.datadir).unwrap();

	init_logging(cli.verbose, cli.quiet, &datadir);

	info!("Using bark datadir at {}", datadir.display());
	let _pid_lock = PidLock::acquire(&datadir)?;

	if env!("BARK_VERSION") == VERSION_DIRTY {
		warn!("You're running a custom build of bark, which might cause unexpected issues. \
			Consider building at one of the tagged versions or using the release builds.");
	}

	// Handle create command differently.
	if let Command::Create(opts) = cli.command {
		create_wallet(&datadir, opts).await?;
		return Ok(())
	}

	if let Command::Dev(cmd) = cli.command {
		return dev::execute_dev_command(cmd, datadir).await;
	}

	let (mut wallet, mut onchain) = open_wallet(&datadir).await
			.context("error opening wallet")?
			.context("No wallet found")?;

	let net = wallet.network().await?;

	match cli.command {
		Command::Create { .. } | Command::Dev(_) => unreachable!("handled earlier"),
		Command::Config => {
			output_json(&wallet.config())
		},
		Command::ArkInfo => {
			if let Some(info) = wallet.ark_info().await? {
				output_json(&bark_json::cli::ArkInfo::from(info));
			} else {
				warn!("Could not connect with Ark server.")
			}
		},
		Command::Address { index, subcommand } => {
			match subcommand {
				Some(AddressCommand::Lookup { filter: AddressLookupFilter { address, index } }) => {
					let address = if let Some(address) = address {
						address
					} else if let Some(index) = index {
						wallet.peek_address(index).await?
					} else {
						unreachable!("clap requires --address or --index");
					};
					let pm = PaymentMethod::Ark(address);
					let movements = wallet.history_by_payment_method(&pm).await?.into_iter()
						.map(json::Movement::from)
						.collect::<Vec<_>>();
					output_json(&movements);
				},
				None => {
					if let Some(index) = index {
						println!("{}", wallet.peek_address(index).await?)
					} else {
						println!("{}", wallet.new_address().await?)
					}
				},
			}
		},
		Command::Balance { no_sync } => {
			if !no_sync {
				info!("Syncing wallet...");
				wallet.sync().await;
			}

			let balance = wallet.balance().await?;
			output_json(&json::Balance::from(balance));
		},
		Command::Vtxos { all, no_sync } => {
			if !no_sync {
				info!("Syncing wallet...");
				wallet.sync().await;
			}

			let mut vtxos = if all {
				wallet.all_vtxos().await?
			} else {
				wallet.vtxos().await?
			};

			vtxos.sort_by(|a, b| {
				match (a.state.kind(), b.state.kind()) {
					(VtxoStateKind::Spent, b) if b != VtxoStateKind::Spent => Ordering::Less,
					(VtxoStateKind::Spendable, a) if a != VtxoStateKind::Spendable => Ordering::Greater,
					_ => a.expiry_height().cmp(&b.expiry_height()),
				}
			});

			output_json(&vtxos.into_iter().map(WalletVtxoInfo::from).collect::<Vec<_>>());
		},
		Command::History { no_sync } => {
			if !no_sync {
				info!("Syncing wallet...");
				wallet.sync().await;
			}

			let mut movements = wallet.history().await?.into_iter()
				.map(json::Movement::try_from)
				.collect::<Result<Vec<_>, _>>()?;

			// Movements are ordered from newest to oldest, so we reverse them to ensure the last
			// item in the terminal is the newest.
			movements.reverse();

			output_json(&movements);
		},
		Command::Refresh {
			vtxos, threshold_blocks, threshold_hours, counterparty, all, delegated, no_sync,
		} => {
			if !no_sync {
				info!("Syncing wallet...");
				wallet.sync().await;
			}

			let vtxos = match (threshold_blocks, threshold_hours, counterparty, all, vtxos) {
				(None, None, false, false, None) => wallet.get_expiring_vtxos(wallet.config().vtxo_refresh_expiry_threshold).await?,
				(Some(b), None, false, false, None) => wallet.get_expiring_vtxos(b).await?,
				(None, Some(h), false, false, None) => wallet.get_expiring_vtxos(h*6).await?,
				(None, None, true, false, None) => {
					let filter = VtxoFilter::new(&wallet).counterparty();
					wallet.spendable_vtxos_with(&filter).await?
				},
				(None, None, false, true, None) => wallet.spendable_vtxos().await?,
				(None, None, false, false, Some(vs)) => {
					let mut vtxos = vec![];
					for s in vs {
						let id = VtxoId::from_str(&s)?;
						vtxos.push(wallet.get_vtxo_by_id(id).await?);
					}
					vtxos
				}
				_ => bail!("please provide either threshold vtxo, threshold_blocks, threshold_hours, counterparty or all"),
			};

			info!("Refreshing {} vtxos...", vtxos.len());
			if delegated {
				if let Some(res) = wallet.refresh_vtxos_delegated(vtxos).await? {
					output_json(&json::RoundStateInfo {
						round_state_id: res.id().0,
					});
				} else {
					info!("No round happened");
				}
			} else {
				if let Some(res) = wallet.refresh_vtxos(vtxos).await? {
					output_json(&json::RoundStatus::from(res));
				} else {
					info!("No round happened");
				}
			}
		},
		Command::Board { amount, all, no_sync } => {
			if !no_sync {
				info!("Syncing onchain wallet...");
				if let Err(e) = onchain.sync(&wallet.chain).await {
					warn!("Sync error: {}", e)
				}
			}
			let board = match (amount, all) {
				(Some(a), false) => {
					info!("Boarding {}...", a);
					wallet.board_amount(&mut onchain, a).await?

				},
				(None, true) => {
					info!("Boarding total balance...");
					wallet.board_all(&mut onchain).await?
				},
				_ => bail!("please provide either an amount or --all"),
			};
			output_json(&json::PendingBoardInfo::from(board));
		},
		Command::Send { destination, amount, comment, no_sync, wait } => {
			if !no_sync {
				info!("Syncing wallet...");
				wallet.sync().await;
			}

			if let Ok(addr) = ark::Address::from_str(&destination) {
				let amount = amount.context("amount missing")?;
				if comment.is_some() {
					bail!("comment not supported for Ark address");
				}

				info!("Sending arkoor payment of {} to address {}", amount, addr);
				wallet.send_arkoor_payment(&addr, amount).await?;
				info!("Payment sent successfully!");
			} else if let Ok(inv) = Bolt11Invoice::from_str(&destination) {
				if comment.is_some() {
					bail!("comment is not supported for BOLT-11 invoices");
				}
				let ln_send = wallet.pay_lightning_invoice(inv, amount).await?;
				wait_for_lightning_send(&wallet, ln_send.invoice.payment_hash(), wait).await;
			} else if let Ok(offer) = Offer::from_str(&destination) {
				if comment.is_some() {
					bail!("comment is not supported for BOLT-12 offers");
				}
				let ln_send = wallet.pay_lightning_offer(offer, amount).await?;
				wait_for_lightning_send(&wallet, ln_send.invoice.payment_hash(), wait).await;
			} else if let Ok(addr) = LightningAddress::from_str(&destination) {
				let amount = amount.context("amount is required for Lightning addresses")?;
				let ln_send = wallet.pay_lightning_address(&addr, amount, comment).await?;
				wait_for_lightning_send(&wallet, ln_send.invoice.payment_hash(), wait).await;
			} else {
				bail!("Argument is not a valid destination. Supported are: \
					VTXO pubkeys, bolt11 invoices, bolt12 offers and lightning addresses",
				);
			}
		},
		Command::SendOnchain { destination, amount, no_sync } => {
			if let Ok(addr) = bitcoin::Address::from_str(&destination) {
				let addr = addr.require_network(net).with_context(|| {
					format!("address is not valid for configured network {}", net)
				})?;

				if !no_sync {
					info!("Syncing wallet...");
					wallet.sync().await;
				}

				info!("Sending on-chain payment of {} to {}", amount, addr);
				let offboard_txid = wallet.send_onchain(addr, amount).await?;
				output_json(&json::OffboardResult { offboard_txid });
			} else {
				bail!("Invalid destination");
			}
		},
		Command::Offboard { address, vtxos , all, no_sync } => {
			let address = if let Some(address) = address {
				let address = bitcoin::Address::from_str(&address)?
					.require_network(net)
					.with_context(|| {
						format!("address is not valid for configured network {}", net)
					})?;

				debug!("Sending to on-chain address {}", address);

				address
			} else {
				onchain.address().await?
			};

			let offboard_txid = if let Some(vtxos) = vtxos {
				let vtxos = vtxos
					.into_iter()
					.map(|vtxo| {
						VtxoId::from_str(&vtxo).with_context(|| format!("invalid vtxoid: {}", vtxo))
					})
					.collect::<anyhow::Result<Vec<_>>>()?;

				if !no_sync {
					info!("Syncing wallet...");
					wallet.sync().await;
				}

				info!("Offboarding {} vtxos...", vtxos.len());
				wallet.offboard_vtxos(vtxos, address).await?
			} else if all {
				if !no_sync {
					info!("Syncing wallet...");
					wallet.sync().await;
				}
				info!("Offboarding all off-chain funds...");
				wallet.offboard_all(address).await?
			} else {
				bail!("Either --vtxos or --all argument must be provided to offboard");
			};
			output_json(&json::OffboardResult { offboard_txid });
		},
		Command::Onchain(onchain_command) => {
			onchain::execute_onchain_command(onchain_command, &mut wallet, &mut onchain).await?;
		},
		Command::Exit(cmd) => {
			exit::execute_exit_command(cmd, &mut wallet, &mut onchain).await?;
		},
		Command::Lightning(cmd) => {
			lightning::execute_lightning_command(cmd, &mut wallet).await?;
		},
		Command::Round(cmd) => {
			round::execute_round_command(cmd, &mut wallet).await?;
		},
		Command::Maintain { delegated } => {
			if delegated {
				wallet.maintenance_with_onchain_delegated(&mut onchain).await?;
			} else {
				wallet.maintenance_with_onchain(&mut onchain).await?;
			}
		},
	}
	Ok(())
}


async fn wait_for_lightning_send(wallet: &Wallet, payment_hash: PaymentHash, wait: bool) {
	if wait {
		match wallet.check_lightning_payment(payment_hash, true).await {
			Ok(Some(_)) => info!("Payment sent: hash = {}", payment_hash),
			Err(err) => warn!("Error waiting for payment: {:?}", err),
			Ok(None) => info!("Payment failed: hash = {}", payment_hash),
		}
	} else {
		info!("Payment initiated but not completed (yet).");
	}

}

#[tokio::main]
async fn main() {
	let cli = Cli::parse();
	let verbose = cli.verbose;

	if let Err(e) = inner_main(cli).await {
		eprintln!("An error occurred: {}", e);

		// this is taken from anyhow code because it's not exposed
		if let Some(cause) = e.source() {
			eprintln!("Caused by:");
			for error in anyhow::Chain::new(cause) {
				eprintln!("	{}", error);
			}
		}

		if verbose {
			eprintln!();
			eprintln!("Stack backtrace:");
			eprintln!("{}", e.backtrace());
		}
		process::exit(1);
	}
}