Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rust-nostr/nostr
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e686558d2a5b80bf040afe5f863c849fc108989c
Choose a base ref
..
head repository: rust-nostr/nostr
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f221dfc36602a3b31af90d96a19307fc6e00aab4
Choose a head ref
Showing with 138 additions and 0 deletions.
  1. +138 −0 crates/nostr/src/nips/nip47.rs
138 changes: 138 additions & 0 deletions crates/nostr/src/nips/nip47.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
//! <https://github.com/nostr-protocol/nips/blob/master/47.md>
use alloc::string::String;
use alloc::vec::Vec;
use alloc::{borrow::Cow, string::ToString};
use core::fmt;
use core::str::FromStr;
@@ -125,12 +126,21 @@ pub enum Method {
/// Pay Invoice
#[serde(rename = "pay_invoice")]
PayInvoice,
/// Pay Keysend
#[serde(rename = "pay_keysend")]
PayKeysend,
/// Make Invoice
#[serde(rename = "make_invoice")]
MakeInvoice,
/// Lookup Invoice
#[serde(rename = "lookup_invoice")]
LookupInvoice,
/// List Invoices
#[serde(rename = "list_invoices")]
ListInvoices,
/// List Payments
#[serde(rename = "list_payments")]
ListPayments,
/// Get Balance
#[serde(rename = "get_balance")]
GetBalance,
@@ -141,10 +151,16 @@ pub enum Method {
pub enum RequestParams {
/// Pay Invoice
PayInvoice(PayInvoiceRequestParams),
/// Pay Keysend
PayKeysend(PayKeysendRequestParams),
/// Make Invoice
MakeInvoice(MakeInvoiceRequestParams),
/// Lookup Invoice
LookupInvoice(LookupInvoiceRequestParams),
/// List Invoices
ListInvoices(ListInvoicesRequestParams),
/// List Payments
ListPayments(ListPaymentsRequestParams),
/// Get Balance
GetBalance,
}
@@ -156,8 +172,11 @@ impl Serialize for RequestParams {
{
match self {
RequestParams::PayInvoice(p) => p.serialize(serializer),
RequestParams::PayKeysend(p) => p.serialize(serializer),
RequestParams::MakeInvoice(p) => p.serialize(serializer),
RequestParams::LookupInvoice(p) => p.serialize(serializer),
RequestParams::ListInvoices(p) => p.serialize(serializer),
RequestParams::ListPayments(p) => p.serialize(serializer),
RequestParams::GetBalance => serializer.serialize_none(),
}
}
@@ -170,6 +189,35 @@ pub struct PayInvoiceRequestParams {
pub invoice: String,
}

/// TLVs to be added to the keysend payment
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct KeysendTLVRecord {
/// TLV type
#[serde(rename = "type")]
pub type_: u64,
/// TLV value
pub value: String,
}

/// Pay Invoice Request Params
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PayKeysendRequestParams {
/// Amount in millisatoshis
pub amount: i64,
/// Receiver's node id
pub pubkey: String,
/// Optional message
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
/// Optional preimage
#[serde(skip_serializing_if = "Option::is_none")]
pub preimage: Option<String>,
/// Optional TLVs to be added to the keysend payment
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub tlv_records: Vec<KeysendTLVRecord>,
}

/// Make Invoice Request Params
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MakeInvoiceRequestParams {
@@ -179,6 +227,8 @@ pub struct MakeInvoiceRequestParams {
pub description: Option<String>,
/// Invoice description hash
pub description_hash: Option<String>,
/// Preimage to be used for the invoice
pub preimage: Option<String>,
/// Invoice expiry in seconds
pub expiry: Option<i64>,
}
@@ -192,6 +242,43 @@ pub struct LookupInvoiceRequestParams {
pub bolt11: Option<String>,
}

/// List Invoice Request Params
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ListInvoicesRequestParams {
/// Starting timestamp in seconds since epoch
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<u64>,
/// Ending timestamp in seconds since epoch
#[serde(skip_serializing_if = "Option::is_none")]
pub until: Option<u64>,
/// Number of invoices to return
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,
/// Offset of the first invoice to return
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<u64>,
/// If true, include unpaid invoices
#[serde(skip_serializing_if = "Option::is_none")]
pub unpaid: Option<bool>,
}

/// List Payments Request Params
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ListPaymentsRequestParams {
/// Starting timestamp in seconds since epoch
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<u64>,
/// Ending timestamp in seconds since epoch
#[serde(skip_serializing_if = "Option::is_none")]
pub until: Option<u64>,
/// Number of invoices to return
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,
/// Offset of the first invoice to return
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<u64>,
}

/// NIP47 Request
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct Request {
@@ -233,6 +320,10 @@ impl Request {
let params: PayInvoiceRequestParams = serde_json::from_value(template.params)?;
RequestParams::PayInvoice(params)
}
Method::PayKeysend => {
let params: PayKeysendRequestParams = serde_json::from_value(template.params)?;
RequestParams::PayKeysend(params)
}
Method::MakeInvoice => {
let params: MakeInvoiceRequestParams = serde_json::from_value(template.params)?;
RequestParams::MakeInvoice(params)
@@ -241,6 +332,14 @@ impl Request {
let params: LookupInvoiceRequestParams = serde_json::from_value(template.params)?;
RequestParams::LookupInvoice(params)
}
Method::ListInvoices => {
let params: ListInvoicesRequestParams = serde_json::from_value(template.params)?;
RequestParams::ListInvoices(params)
}
Method::ListPayments => {
let params: ListPaymentsRequestParams = serde_json::from_value(template.params)?;
RequestParams::ListPayments(params)
}
Method::GetBalance => RequestParams::GetBalance,
};

@@ -273,6 +372,15 @@ pub struct PayInvoiceResponseResult {
pub preimage: String,
}

/// NIP47 Response Result
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct PayKeysendResponseResult {
/// Response preimage
pub preimage: String,
/// Payment hash
pub payment_hash: String,
}

/// NIP47 Response Result
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct MakeInvoiceResponseResult {
@@ -291,6 +399,15 @@ pub struct LookupInvoiceResponseResult {
pub paid: bool,
}

/// NIP47 Response Result
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ListPaymentResponseResult {
/// Bolt11 invoice
pub invoice: String,
/// Preimage for the payment
pub preimage: Option<String>,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
/// Budget renewal type
@@ -323,10 +440,16 @@ pub struct GetBalanceResponseResult {
pub enum ResponseResult {
/// Pay Invoice
PayInvoice(PayInvoiceResponseResult),
/// Pay Keysend
PayKeysend(PayKeysendResponseResult),
/// Make Invoice
MakeInvoice(MakeInvoiceResponseResult),
/// Lookup Invoice
LookupInvoice(LookupInvoiceResponseResult),
/// List Invoices
ListInvoices(Vec<LookupInvoiceResponseResult>),
/// List Payments
ListPayments(Vec<ListPaymentResponseResult>),
/// Get Balance
GetBalance(GetBalanceResponseResult),
}
@@ -338,8 +461,11 @@ impl Serialize for ResponseResult {
{
match self {
ResponseResult::PayInvoice(p) => p.serialize(serializer),
ResponseResult::PayKeysend(p) => p.serialize(serializer),
ResponseResult::MakeInvoice(p) => p.serialize(serializer),
ResponseResult::LookupInvoice(p) => p.serialize(serializer),
ResponseResult::ListInvoices(p) => p.serialize(serializer),
ResponseResult::ListPayments(p) => p.serialize(serializer),
ResponseResult::GetBalance(p) => p.serialize(serializer),
}
}
@@ -386,6 +512,10 @@ impl Response {
let result: PayInvoiceResponseResult = serde_json::from_value(result)?;
ResponseResult::PayInvoice(result)
}
Method::PayKeysend => {
let result: PayKeysendResponseResult = serde_json::from_value(result)?;
ResponseResult::PayKeysend(result)
}
Method::MakeInvoice => {
let result: MakeInvoiceResponseResult = serde_json::from_value(result)?;
ResponseResult::MakeInvoice(result)
@@ -394,6 +524,14 @@ impl Response {
let result: LookupInvoiceResponseResult = serde_json::from_value(result)?;
ResponseResult::LookupInvoice(result)
}
Method::ListInvoices => {
let result: Vec<LookupInvoiceResponseResult> = serde_json::from_value(result)?;
ResponseResult::ListInvoices(result)
}
Method::ListPayments => {
let result: Vec<ListPaymentResponseResult> = serde_json::from_value(result)?;
ResponseResult::ListPayments(result)
}
Method::GetBalance => {
let result: GetBalanceResponseResult = serde_json::from_value(result)?;
ResponseResult::GetBalance(result)