Skip to content

Commit

Permalink
add payment metadata functionality to sdk-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored Jan 19, 2024
1 parent 75781e2 commit d7705b3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
13 changes: 12 additions & 1 deletion libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::db::SqliteStorage;
use super::error::{PersistError, PersistResult};
use crate::lnurl::pay::model::SuccessActionProcessed;
use crate::{ensure_sdk, models::*};
use anyhow::anyhow;
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use rusqlite::Row;
use rusqlite::{named_params, params, OptionalExtension};
Expand Down Expand Up @@ -112,14 +113,24 @@ impl SqliteStorage {
) -> PersistResult<()> {
ensure_sdk!(
new_metadata.len() <= METADATA_MAX_LEN,
PersistError::Generic(anyhow::anyhow!(
PersistError::Generic(anyhow!(
"Max metadata size ({} characters) has been exceeded",
METADATA_MAX_LEN
))
);

let _ = serde_json::from_str::<Map<String, Value>>(&new_metadata)?;

// Check if the payment exists
let payment_exists = self
.get_connection()?
.prepare("SELECT 1 FROM payments WHERE id = ?1;")?
.exists(params![payment_hash])?;

if !payment_exists {
return Err(PersistError::Generic(anyhow!("Payment not found")));
}

self.get_connection()?.execute(
"
INSERT OR REPLACE INTO sync.payments_metadata(
Expand Down
34 changes: 31 additions & 3 deletions tools/sdk-cli/src/command_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fs;
use std::sync::Arc;

use anyhow::{anyhow, Error, Result};
use anyhow::{anyhow, Context, Error, Result};
use breez_sdk_core::InputType::{LnUrlAuth, LnUrlPay, LnUrlWithdraw};
use breez_sdk_core::{
parse, BreezEvent, BreezServices, BuyBitcoinRequest, CheckMessageRequest, EventListener,
GreenlightCredentials, ListPaymentsRequest, LnUrlPayRequest, LnUrlWithdrawRequest,
PrepareRedeemOnchainFundsRequest, PrepareRefundRequest, ReceiveOnchainRequest,
MetadataFilter, PrepareRedeemOnchainFundsRequest, PrepareRefundRequest, ReceiveOnchainRequest,
ReceivePaymentRequest, RedeemOnchainFundsRequest, RefundRequest, ReportIssueRequest,
ReportPaymentFailureDetails, ReverseSwapFeesRequest, SendOnchainRequest, SendPaymentRequest,
SendSpontaneousPaymentRequest, SignMessageRequest, StaticBackupRequest,
Expand Down Expand Up @@ -215,11 +215,31 @@ pub(crate) async fn handle_command(
include_failures,
limit,
offset,
metadata_filters: metadata_filters_raw,
} => {
let metadata_filters = match metadata_filters_raw {
Some(raw_filters) => {
let mut filters = vec![];

for filter in raw_filters.iter() {
let (json_path, json_value) =
filter.split_once(':').context("Invalid metadata filter")?;

filters.push(MetadataFilter {
json_path: json_path.to_string(),
json_value: json_value.to_string(),
});
}

Some(filters)
}
None => None,
};

let payments = sdk()?
.list_payments(ListPaymentsRequest {
filters: None,
metadata_filters: None,
metadata_filters,
from_timestamp,
to_timestamp,
include_failures: Some(include_failures),
Expand All @@ -229,6 +249,14 @@ pub(crate) async fn handle_command(
.await?;
serde_json::to_string_pretty(&payments).map_err(|e| e.into())
}
Commands::SetPaymentMetadata {
payment_hash,
metadata,
} => {
sdk()?.set_payment_metadata(payment_hash, metadata).await?;

Ok("Payment metadata was set successfully".to_string())
}
Commands::PaymentByHash { hash } => {
let payment = sdk()?.payment_by_hash(hash).await?;
serde_json::to_string_pretty(&payment).map_err(|e| e.into())
Expand Down
10 changes: 10 additions & 0 deletions tools/sdk-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ pub(crate) enum Commands {
/// Optional offset in payments
#[clap(short = 'o', long = "offset")]
offset: Option<u32>,

/// Optional metadata filter, in the form of json_path:json_value
#[clap(short = 'm', long = "metadata", num_args = 1..)]
metadata_filters: Option<Vec<String>>,
},

/// Set the metadata for a given payment
SetPaymentMetadata {
payment_hash: String,
metadata: String,
},

/// Retrieve a payment by its hash
Expand Down

0 comments on commit d7705b3

Please sign in to comment.