Skip to content

Commit

Permalink
Add remaining new NWC functions
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Sep 21, 2023
1 parent 5919e9e commit 1f6d576
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions crates/nostr/src/nips/nip47.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,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,
Expand All @@ -141,10 +150,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,
}
Expand All @@ -156,8 +171,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(),
}
}
Expand All @@ -170,6 +188,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 {
Expand All @@ -192,6 +239,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 {
Expand Down Expand Up @@ -233,6 +317,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)
Expand All @@ -241,6 +329,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,
};

Expand Down Expand Up @@ -273,6 +369,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 {
Expand All @@ -291,6 +396,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
Expand Down Expand Up @@ -323,10 +437,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),
}
Expand All @@ -338,8 +458,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),
}
}
Expand Down Expand Up @@ -386,6 +509,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)
Expand All @@ -394,6 +521,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)
Expand Down

0 comments on commit 1f6d576

Please sign in to comment.