Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/gl-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ hex = "0.4"
thiserror = "2.0.11"
tokio = "1.43.0"
vls-core.workspace = true
serde_json = "^1.0"

[badges]
maintenance = { status = "actively-developed" }
9 changes: 8 additions & 1 deletion libs/gl-cli/src/bin/glcli.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use clap::Parser;
use gl_cli::{run, Cli};
use serde_json::json;

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

match run(cli).await {
Ok(()) => (),
Err(e) => {
println!("{}", e);
if json_print {
let j = json!({"error": e.to_string()});
println!("{}", serde_json::to_string_pretty(&j).unwrap());
} else {
println!("{}", e);
}
}
}
}
7 changes: 7 additions & 0 deletions libs/gl-cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub enum Error {

#[error(transparent)]
UtilError(#[from] util::UtilsError),

#[error("Failed to serialize response: {0}")]
JsonResponseError(String),
}

impl Error {
Expand All @@ -31,6 +34,10 @@ impl Error {
pub fn credentials_not_found(e: impl std::fmt::Display) -> Error {
Error::CredentialsNotFoundError(e.to_string())
}

pub fn failed_response_serialization(e: impl std::fmt::Display) -> Error {
Error::JsonResponseError(e.to_string())
}
}

// -- Disable hints for now as it would require to get rid of thiserror. Might
Expand Down
296 changes: 296 additions & 0 deletions libs/gl-cli/src/json_hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
use gl_client::pb::cln;
use serde_json::json;

pub trait ToJsonHex {
fn to_json_hex(&self) -> serde_json::Value;
}

impl ToJsonHex for cln::GetinfoResponse {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"id": hex::encode(&self.id),
"color": hex::encode(&self.color),
"num_peers": self.num_peers,
"num_pending_channels": self.num_pending_channels,
"num_active_channels": self.num_active_channels,
"num_inactive_channels": self.num_inactive_channels,
"address": self.address.clone(),
"binding": self.binding.clone(),
"version": self.version.clone(),
"blockheight": self.blockheight,
"network": self.network,
"lightning_dir": self.lightning_dir.clone(),
"fees_collected_msat": self.fees_collected_msat.clone().map_or(0, |amt| amt.msat),
});
if let Some(alias) = &self.alias {
j["alias"] = json!(alias);
}
if let Some(feat) = &self.our_features {
j["our_features"] = json!({
"init": hex::encode(&feat.init),
"node": hex::encode(&feat.node),
"channel": hex::encode(&feat.channel),
"invoice": hex::encode(&feat.invoice),
});
}
if let Some(warn_bsync) = &self.warning_bitcoind_sync {
j["warning_bitcoind_sync"] = json!(warn_bsync);
}
if let Some(warn_lsync) = &self.warning_lightningd_sync {
j["warning_lightningd_sync"] = json!(warn_lsync);
}
j
}
}

impl ToJsonHex for cln::InvoiceResponse {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"bolt11": self.bolt11.clone(),
"payment_hash": hex::encode(&self.payment_hash),
"payment_secret": hex::encode(&self.payment_secret),
"expires_at": self.expires_at,
});
if let Some(x) = self.created_index {
j["created_index"] = json!(x);
}
if let Some(x) = &self.warning_capacity {
j["warning_capacity"] = json!(x);
}
if let Some(x) = &self.warning_offline {
j["warning_offline"] = json!(x);
}
if let Some(x) = &self.warning_deadends {
j["warning_deadends"] = json!(x);
}
if let Some(x) = &self.warning_private_unused {
j["warning_private_unused"] = json!(x);
}
if let Some(x) = &self.warning_mpp {
j["warning_mpp"] = json!(x);
}
j
}
}

impl ToJsonHex for cln::PayResponse {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"status": self.status,
"payment_preimage": hex::encode(&self.payment_preimage),
"payment_hash": hex::encode(&self.payment_hash),
"created_at": self.created_at,
"parts": self.parts,
"amount_msat": self.amount_msat.clone().map_or(0, |amt| amt.msat),
"amount_sent_msat": self.amount_sent_msat.clone().map_or(0, |amt| amt.msat),
});
if let Some(x) = &self.destination {
j["destination"] = json!(hex::encode(x));
}
if let Some(x) = &self.warning_partial_completion {
j["warning_partial_completion"] = json!(x);
}
j
}
}

impl ToJsonHex for cln::ListpaysPays {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"payment_hash": hex::encode(&self.payment_hash),
"status": self.status,
"created_at": self.created_at,
});
if let Some(x) = &self.destination {
j["destination"] = json!(hex::encode(x));
}
if let Some(x) = self.completed_at {
j["completed_at"] = json!(x);
}
if let Some(x) = &self.label {
j["label"] = json!(x);
}
if let Some(x) = &self.bolt11 {
j["bolt11"] = json!(x);
}
if let Some(x) = &self.description {
j["description"] = json!(x);
}
if let Some(x) = &self.bolt12 {
j["bolt12"] = json!(x);
}
if let Some(x) = &self.amount_msat {
j["amount_msat"] = x.msat.into();
}
if let Some(x) = &self.amount_sent_msat {
j["amount_sent_msat"] = x.msat.into();
}
if let Some(x) = &self.preimage {
j["preimage"] = json!(hex::encode(x));
}
if let Some(x) = self.number_of_parts {
j["number_of_parts"] = json!(x);
}
if let Some(x) = &self.erroronion {
j["erroronion"] = json!(hex::encode(x));
}
j
}
}

impl ToJsonHex for cln::ListpaysResponse {
fn to_json_hex(&self) -> serde_json::Value {
json!({
"pays": json!(self.pays.iter().map(|x| x.to_json_hex()).collect::<Vec<_>>())
})
}
}

impl ToJsonHex for cln::ConnectAddress {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"item_type": self.item_type,
});
if let Some(x) = &self.socket {
j["socket"] = json!(x);
}
if let Some(x) = &self.address {
j["address"] = json!(x);
}
if let Some(x) = &self.port {
j["port"] = json!(x);
}
j
}
}

impl ToJsonHex for cln::ConnectResponse {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"id": hex::encode(&self.id),
"features": hex::encode(&self.features),
"direction": self.direction,
});
if let Some(x) = &self.address {
j["address"] = x.to_json_hex();
}
j
}
}

impl ToJsonHex for cln::StopResponse {
fn to_json_hex(&self) -> serde_json::Value {
json!({})
}
}

impl ToJsonHex for cln::CloseResponse {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"item_type": self.item_type,
});
if let Some(x) = &self.tx {
j["tx"] = json!(hex::encode(x));
}
if let Some(x) = &self.txid {
j["txid"] = json!(hex::encode(x));
}

j
}
}

impl ToJsonHex for cln::FundchannelResponse {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"tx": hex::encode(&self.tx),
"txid": hex::encode(&self.txid),
"outnum": self.outnum,
"channel_id": hex::encode(&self.channel_id),
});
if let Some(x) = &self.close_to {
j["close_to"] = json!(hex::encode(x));
}
if let Some(x) = &self.mindepth {
j["mindepth"] = json!(x);
}

j
}
}

impl ToJsonHex for cln::WithdrawResponse {
fn to_json_hex(&self) -> serde_json::Value {
json!({
"tx": hex::encode(&self.tx),
"txid": hex::encode(&self.txid),
"psbt": self.psbt.clone(),
})
}
}

impl ToJsonHex for cln::ListfundsOutputs {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"txid": hex::encode(&self.txid),
"scriptpubkey": hex::encode(&self.scriptpubkey),
"output": self.output,
"amount_msat": self.amount_msat.clone().map_or(0, |amt| amt.msat),
"status": self.status,
"reserved": self.reserved
});
if let Some(x) = &self.address {
j["address"] = json!(x);
}
if let Some(x) = &self.redeemscript {
j["redeemscript"] = json!(hex::encode(x));
}
if let Some(x) = self.blockheight {
j["blockheight"] = json!(x);
}
j
}
}

impl ToJsonHex for cln::ListfundsChannels {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({
"peer_id": hex::encode(&self.peer_id),
"our_amount_msat": self.our_amount_msat.clone().map_or(0, |amt| amt.msat),
"amount_msat": self.amount_msat.clone().map_or(0, |amt| amt.msat),
"funding_txid": hex::encode(&self.funding_txid),
"funding_output": self.funding_output,
"connected": self.connected,
"state": self.state,
});
if let Some(x) = &self.channel_id {
j["channel_id"] = json!(hex::encode(x));
}
if let Some(x) = &self.short_channel_id {
j["short_channel_id"] = json!(x);
}
j
}
}

impl ToJsonHex for cln::ListfundsResponse {
fn to_json_hex(&self) -> serde_json::Value {
json!({
"outputs": json!(self.outputs.iter().map(|x| x.to_json_hex()).collect::<Vec<_>>()),
"channels": json!(self.channels.iter().map(|x| x.to_json_hex()).collect::<Vec<_>>())
})
}
}

impl ToJsonHex for cln::NewaddrResponse {
fn to_json_hex(&self) -> serde_json::Value {
let mut j = json!({});
if let Some(x) = &self.p2tr {
j["p2tr"] = json!(x);
}
if let Some(x) = &self.bech32 {
j["bech32"] = json!(x);
}
j
}
}
7 changes: 7 additions & 0 deletions libs/gl-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{Parser, Subcommand};
use gl_client::bitcoin::Network;
use std::{path::PathBuf, str::FromStr};
mod error;
mod json_hex;
pub mod model;
mod node;
mod scheduler;
Expand All @@ -20,6 +21,9 @@ pub struct Cli {
network: Network,
#[arg(long, short, global = true, help_heading = "Global options")]
verbose: bool,
/// Produce json outputs.
#[arg(long, short, global = true, help_heading = "Global options")]
pub json: bool,
#[command(subcommand)]
cmd: Commands,
}
Expand Down Expand Up @@ -57,6 +61,7 @@ pub async fn run(cli: Cli) -> Result<()> {
scheduler::Config {
data_dir,
network: cli.network,
print_json: cli.json,
},
)
.await?
Expand All @@ -68,6 +73,7 @@ pub async fn run(cli: Cli) -> Result<()> {
signer::Config {
data_dir,
network: cli.network,
print_json: cli.json,
},
)
.await?
Expand All @@ -78,6 +84,7 @@ pub async fn run(cli: Cli) -> Result<()> {
node::Config {
data_dir,
network: cli.network,
print_json: cli.json,
},
)
.await?
Expand Down
Loading
Loading