Skip to content

Commit e7f4c88

Browse files
committed
nostr: small adj. to NIP-47 ListTransactionsRequestParams and LookupInvoiceResponseResult structs
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent a8a9e55 commit e7f4c88

File tree

5 files changed

+94
-47
lines changed

5 files changed

+94
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
### Changed
2929

3030
* nostr: rename NIP-51 `EventBuilder` set constructors and `Kind` variants ([Yuki Kishimoto])
31+
* nostr: small adj. to NIP-47 `ListTransactionsRequestParams` and `LookupInvoiceResponseResult` structs ([Yuki Kishimoto])
3132
* pool: use per-purpose dedicated relay channels ([Yuki Kishimoto])
3233
* ffi(sdk): convert `RelayPool::handle_notifications` method to async/future ([Yuki Kishimoto])
3334

bindings/nostr-ffi/src/nips/nip47.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use nostr::{JsonUtil, Url};
1010
use uniffi::{Enum, Object, Record};
1111

1212
use crate::error::Result;
13-
use crate::{PublicKey, SecretKey};
13+
use crate::{JsonValue, PublicKey, SecretKey, Timestamp};
1414

1515
/// NIP47 Response Error codes
1616
#[derive(Enum)]
@@ -480,9 +480,9 @@ impl From<LookupInvoiceRequestParams> for nip47::LookupInvoiceRequestParams {
480480
#[derive(Record)]
481481
pub struct ListTransactionsRequestParams {
482482
/// Starting timestamp in seconds since epoch
483-
pub from: Option<u64>,
483+
pub from: Option<Arc<Timestamp>>,
484484
/// Ending timestamp in seconds since epoch
485-
pub until: Option<u64>,
485+
pub until: Option<Arc<Timestamp>>,
486486
/// Number of invoices to return
487487
pub limit: Option<u64>,
488488
/// Offset of the first invoice to return
@@ -496,8 +496,8 @@ pub struct ListTransactionsRequestParams {
496496
impl From<nip47::ListTransactionsRequestParams> for ListTransactionsRequestParams {
497497
fn from(value: nip47::ListTransactionsRequestParams) -> Self {
498498
Self {
499-
from: value.from,
500-
until: value.until,
499+
from: value.from.map(|t| Arc::new(t.into())),
500+
until: value.until.map(|t| Arc::new(t.into())),
501501
limit: value.limit,
502502
offset: value.offset,
503503
unpaid: value.unpaid,
@@ -509,8 +509,8 @@ impl From<nip47::ListTransactionsRequestParams> for ListTransactionsRequestParam
509509
impl From<ListTransactionsRequestParams> for nip47::ListTransactionsRequestParams {
510510
fn from(value: ListTransactionsRequestParams) -> Self {
511511
Self {
512-
from: value.from,
513-
until: value.until,
512+
from: value.from.map(|t| **t),
513+
until: value.until.map(|t| **t),
514514
limit: value.limit,
515515
offset: value.offset,
516516
unpaid: value.unpaid,
@@ -651,13 +651,13 @@ pub struct LookupInvoiceResponseResult {
651651
/// Fees paid in millisatoshis
652652
pub fees_paid: u64,
653653
/// Creation timestamp in seconds since epoch
654-
pub created_at: u64,
654+
pub created_at: Arc<Timestamp>,
655655
/// Expiration timestamp in seconds since epoch
656-
pub expires_at: u64,
656+
pub expires_at: Arc<Timestamp>,
657657
/// Settled timestamp in seconds since epoch
658-
pub settled_at: Option<u64>,
658+
pub settled_at: Option<Arc<Timestamp>>,
659659
/// Optional metadata about the payment
660-
pub metadata: String,
660+
pub metadata: Option<JsonValue>,
661661
}
662662

663663
impl From<nip47::LookupInvoiceResponseResult> for LookupInvoiceResponseResult {
@@ -671,10 +671,10 @@ impl From<nip47::LookupInvoiceResponseResult> for LookupInvoiceResponseResult {
671671
payment_hash: value.payment_hash,
672672
amount: value.amount,
673673
fees_paid: value.fees_paid,
674-
created_at: value.created_at,
675-
expires_at: value.expires_at,
676-
settled_at: value.settled_at,
677-
metadata: value.metadata.to_string(),
674+
created_at: Arc::new(value.created_at.into()),
675+
expires_at: Arc::new(value.expires_at.into()),
676+
settled_at: value.settled_at.map(|t| Arc::new(t.into())),
677+
metadata: value.metadata.and_then(|m| m.try_into().ok()),
678678
}
679679
}
680680
}
@@ -690,10 +690,10 @@ impl From<LookupInvoiceResponseResult> for nip47::LookupInvoiceResponseResult {
690690
payment_hash: value.payment_hash,
691691
amount: value.amount,
692692
fees_paid: value.fees_paid,
693-
created_at: value.created_at,
694-
expires_at: value.expires_at,
695-
settled_at: value.settled_at,
696-
metadata: value.metadata.into(),
693+
created_at: **value.created_at,
694+
expires_at: **value.expires_at,
695+
settled_at: value.settled_at.map(|t| **t),
696+
metadata: value.metadata.and_then(|m| m.try_into().ok()),
697697
}
698698
}
699699
}

bindings/nostr-js/src/nips/nip47.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use wasm_bindgen::prelude::*;
1010

1111
use crate::error::{into_err, Result};
1212
use crate::key::{JsPublicKey, JsSecretKey};
13+
use crate::types::time::JsTimestamp;
1314

1415
/// NIP47 Response Error codes
1516
#[wasm_bindgen(js_name = Nip47ErrorCode)]
@@ -327,9 +328,9 @@ impl From<JsLookupInvoiceRequestParams> for LookupInvoiceRequestParams {
327328
#[wasm_bindgen(js_name = ListTransactionsRequestParams)]
328329
pub struct JsListTransactionsRequestParams {
329330
/// Starting timestamp in seconds since epoch
330-
pub from: Option<u64>,
331+
pub from: Option<JsTimestamp>,
331332
/// Ending timestamp in seconds since epoch
332-
pub until: Option<u64>,
333+
pub until: Option<JsTimestamp>,
333334
/// Number of invoices to return
334335
pub limit: Option<u64>,
335336
/// Offset of the first invoice to return
@@ -343,8 +344,8 @@ pub struct JsListTransactionsRequestParams {
343344
impl From<ListTransactionsRequestParams> for JsListTransactionsRequestParams {
344345
fn from(value: ListTransactionsRequestParams) -> Self {
345346
Self {
346-
from: value.from,
347-
until: value.until,
347+
from: value.from.map(|t| t.into()),
348+
until: value.until.map(|t| t.into()),
348349
limit: value.limit,
349350
offset: value.offset,
350351
unpaid: value.unpaid,
@@ -356,8 +357,8 @@ impl From<ListTransactionsRequestParams> for JsListTransactionsRequestParams {
356357
impl From<JsListTransactionsRequestParams> for ListTransactionsRequestParams {
357358
fn from(value: JsListTransactionsRequestParams) -> Self {
358359
Self {
359-
from: value.from,
360-
until: value.until,
360+
from: value.from.map(|t| *t),
361+
until: value.until.map(|t| *t),
361362
limit: value.limit,
362363
offset: value.offset,
363364
unpaid: value.unpaid,
@@ -464,14 +465,14 @@ pub struct JsLookupInvoiceResponseResult {
464465
/// Fees paid in millisatoshis
465466
pub fees_paid: u64,
466467
/// Creation timestamp in seconds since epoch
467-
pub created_at: u64,
468+
pub created_at: JsTimestamp,
468469
/// Expiration timestamp in seconds since epoch
469-
pub expires_at: u64,
470+
pub expires_at: JsTimestamp,
470471
/// Settled timestamp in seconds since epoch
471-
pub settled_at: Option<u64>,
472-
/// Optional metadata about the payment
473-
#[wasm_bindgen(getter_with_clone)]
474-
pub metadata: String,
472+
pub settled_at: Option<JsTimestamp>,
473+
// /// Optional metadata about the payment
474+
// #[wasm_bindgen(getter_with_clone)]
475+
// pub metadata: String, // TODO: this is not a string
475476
}
476477

477478
impl From<LookupInvoiceResponseResult> for JsLookupInvoiceResponseResult {
@@ -485,10 +486,10 @@ impl From<LookupInvoiceResponseResult> for JsLookupInvoiceResponseResult {
485486
payment_hash: value.payment_hash,
486487
amount: value.amount,
487488
fees_paid: value.fees_paid,
488-
created_at: value.created_at,
489-
expires_at: value.expires_at,
490-
settled_at: value.settled_at,
491-
metadata: value.metadata.to_string(),
489+
created_at: value.created_at.into(),
490+
expires_at: value.expires_at.into(),
491+
settled_at: value.settled_at.map(|t| t.into()),
492+
// metadata: value.metadata.to_string(),
492493
}
493494
}
494495
}
@@ -504,10 +505,10 @@ impl From<JsLookupInvoiceResponseResult> for LookupInvoiceResponseResult {
504505
payment_hash: value.payment_hash,
505506
amount: value.amount,
506507
fees_paid: value.fees_paid,
507-
created_at: value.created_at,
508-
expires_at: value.expires_at,
509-
settled_at: value.settled_at,
510-
metadata: value.metadata.into(),
508+
created_at: *value.created_at,
509+
expires_at: *value.expires_at,
510+
settled_at: value.settled_at.map(|t| *t),
511+
metadata: None,
511512
}
512513
}
513514
}

bindings/nostr-js/src/types/time.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::ops::Deref;
77
use nostr::prelude::*;
88
use wasm_bindgen::prelude::*;
99

10+
#[derive(Clone, Copy)]
1011
#[wasm_bindgen(js_name = Timestamp)]
1112
pub struct JsTimestamp {
1213
inner: Timestamp,

crates/nostr/src/nips/nip47.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use serde_json::Value;
1818
use super::nip04;
1919
use crate::types::url::form_urlencoded::byte_serialize;
2020
use crate::types::url::{ParseError, Url};
21-
use crate::{key, Event, JsonUtil, PublicKey, SecretKey};
21+
use crate::{key, Event, JsonUtil, PublicKey, SecretKey, Timestamp};
2222
#[cfg(feature = "std")]
2323
use crate::{EventBuilder, Keys, Kind, Tag};
2424

@@ -358,14 +358,14 @@ pub enum TransactionType {
358358
}
359359

360360
/// List Transactions Request Params
361-
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
361+
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
362362
pub struct ListTransactionsRequestParams {
363363
/// Starting timestamp in seconds since epoch
364364
#[serde(skip_serializing_if = "Option::is_none")]
365-
pub from: Option<u64>,
365+
pub from: Option<Timestamp>,
366366
/// Ending timestamp in seconds since epoch
367367
#[serde(skip_serializing_if = "Option::is_none")]
368-
pub until: Option<u64>,
368+
pub until: Option<Timestamp>,
369369
/// Number of invoices to return
370370
#[serde(skip_serializing_if = "Option::is_none")]
371371
pub limit: Option<u64>,
@@ -584,14 +584,15 @@ pub struct LookupInvoiceResponseResult {
584584
/// Fees paid in millisatoshis
585585
pub fees_paid: u64,
586586
/// Creation timestamp in seconds since epoch
587-
pub created_at: u64,
587+
pub created_at: Timestamp,
588588
/// Expiration timestamp in seconds since epoch
589-
pub expires_at: u64,
589+
pub expires_at: Timestamp,
590590
/// Settled timestamp in seconds since epoch
591591
#[serde(skip_serializing_if = "Option::is_none")]
592-
pub settled_at: Option<u64>,
592+
pub settled_at: Option<Timestamp>,
593593
/// Optional metadata about the payment
594-
pub metadata: Value,
594+
#[serde(skip_serializing_if = "Option::is_none")]
595+
pub metadata: Option<Value>,
595596
}
596597

597598
/// NIP47 `get_balance` Response Result
@@ -1070,4 +1071,47 @@ mod tests {
10701071
panic!("Invalid request params");
10711072
}
10721073
}
1074+
1075+
#[test]
1076+
fn test_parse_list_transactions_result() {
1077+
let json = r#"{
1078+
"result_type": "list_transactions",
1079+
"result": {
1080+
"transactions": [
1081+
{
1082+
"type": "incoming",
1083+
"invoice": "abcd",
1084+
"description": "string",
1085+
"payment_hash": "",
1086+
"amount": 123,
1087+
"fees_paid": 1,
1088+
"created_at": 123456,
1089+
"expires_at": 1234567
1090+
}
1091+
]
1092+
}
1093+
}"#;
1094+
let result = Response::from_json(json).unwrap();
1095+
assert_eq!(result.result_type, Method::ListTransactions);
1096+
assert!(result.error.is_none());
1097+
assert_eq!(
1098+
result.result,
1099+
Some(ResponseResult::ListTransactions(vec![
1100+
LookupInvoiceResponseResult {
1101+
transaction_type: Some(TransactionType::Incoming),
1102+
invoice: Some(String::from("abcd")),
1103+
description: Some(String::from("string")),
1104+
amount: 123,
1105+
fees_paid: 1,
1106+
created_at: Timestamp::from(123456),
1107+
expires_at: Timestamp::from(1234567),
1108+
description_hash: None,
1109+
payment_hash: String::new(),
1110+
metadata: None,
1111+
settled_at: None,
1112+
preimage: None
1113+
}
1114+
]))
1115+
)
1116+
}
10731117
}

0 commit comments

Comments
 (0)