@@ -76,7 +76,7 @@ use crate::{
76
76
use crate :: { nostr:: NostrManager , utils:: sleep} ;
77
77
use :: nostr:: nips:: nip57;
78
78
use :: nostr:: prelude:: ZapRequestData ;
79
- use :: nostr:: { Event , EventId , JsonUtil , Kind , Metadata } ;
79
+ use :: nostr:: { EventId , JsonUtil , Kind } ;
80
80
use async_lock:: RwLock ;
81
81
use bdk_chain:: ConfirmationTime ;
82
82
use bip39:: Mnemonic ;
@@ -103,7 +103,7 @@ use moksha_core::token::TokenV3;
103
103
use nostr_sdk:: { Client , RelayPoolNotification } ;
104
104
use reqwest:: multipart:: { Form , Part } ;
105
105
use serde:: { Deserialize , Serialize } ;
106
- use serde_json:: { json , Value } ;
106
+ use serde_json:: Value ;
107
107
use std:: sync:: Arc ;
108
108
#[ cfg( not( target_arch = "wasm32" ) ) ]
109
109
use std:: time:: Instant ;
@@ -114,7 +114,6 @@ use uuid::Uuid;
114
114
use crate :: labels:: LabelItem ;
115
115
use crate :: nostr:: NostrKeySource ;
116
116
use crate :: storage:: { persist_payment_info, SUBSCRIPTION_TIMESTAMP } ;
117
- use crate :: utils:: parse_profile_metadata;
118
117
#[ cfg( test) ]
119
118
use mockall:: { automock, predicate:: * } ;
120
119
@@ -790,6 +789,7 @@ impl<S: MutinyStorage> MutinyWalletBuilder<S> {
790
789
self . xprivkey ,
791
790
self . nostr_key_source ,
792
791
self . storage . clone ( ) ,
792
+ config. primal_url . clone ( ) ,
793
793
logger. clone ( ) ,
794
794
stop. clone ( ) ,
795
795
) ?) ;
@@ -1733,24 +1733,6 @@ impl<S: MutinyStorage> MutinyWallet<S> {
1733
1733
Err ( MutinyError :: NostrError )
1734
1734
}
1735
1735
1736
- /// Makes a request to the primal api
1737
- async fn primal_request (
1738
- client : & reqwest:: Client ,
1739
- url : & str ,
1740
- body : Value ,
1741
- ) -> Result < Vec < Value > , MutinyError > {
1742
- client
1743
- . post ( url)
1744
- . header ( "Content-Type" , "application/json" )
1745
- . body ( body. to_string ( ) )
1746
- . send ( )
1747
- . await
1748
- . map_err ( |_| MutinyError :: NostrError ) ?
1749
- . json ( )
1750
- . await
1751
- . map_err ( |_| MutinyError :: NostrError )
1752
- }
1753
-
1754
1736
/// Syncs all of our nostr data from the configured primal instance
1755
1737
pub async fn sync_nostr ( & self ) -> Result < ( ) , MutinyError > {
1756
1738
let contacts_fut = self . sync_nostr_contacts ( self . nostr . public_key ) ;
@@ -1766,24 +1748,12 @@ impl<S: MutinyStorage> MutinyWallet<S> {
1766
1748
1767
1749
/// Fetches our latest nostr profile from primal and saves to storage
1768
1750
async fn sync_nostr_profile ( & self ) -> Result < ( ) , MutinyError > {
1769
- let url = self
1770
- . config
1771
- . primal_url
1772
- . as_deref ( )
1773
- . unwrap_or ( "https://primal-cache.mutinywallet.com/api" ) ;
1774
- let client = reqwest:: Client :: new ( ) ;
1775
-
1776
- let body = json ! ( [ "user_profile" , { "pubkey" : self . nostr. public_key } ] ) ;
1777
- let data: Vec < Value > = Self :: primal_request ( & client, url, body) . await ?;
1778
-
1779
- if let Some ( json) = data. first ( ) . cloned ( ) {
1780
- let event: Event = serde_json:: from_value ( json) . map_err ( |_| MutinyError :: NostrError ) ?;
1781
- if event. kind != Kind :: Metadata {
1782
- return Ok ( ( ) ) ;
1783
- }
1784
-
1785
- let metadata: Metadata =
1786
- serde_json:: from_str ( & event. content ) . map_err ( |_| MutinyError :: NostrError ) ?;
1751
+ if let Some ( metadata) = self
1752
+ . nostr
1753
+ . primal_client
1754
+ . get_user_profile ( self . nostr . public_key )
1755
+ . await ?
1756
+ {
1787
1757
self . storage . set_nostr_profile ( metadata) ?;
1788
1758
}
1789
1759
@@ -1792,35 +1762,28 @@ impl<S: MutinyStorage> MutinyWallet<S> {
1792
1762
1793
1763
/// Get contacts from the given npub and sync them to the wallet
1794
1764
pub async fn sync_nostr_contacts ( & self , npub : :: nostr:: PublicKey ) -> Result < ( ) , MutinyError > {
1795
- let url = self
1796
- . config
1797
- . primal_url
1798
- . as_deref ( )
1799
- . unwrap_or ( "https://primal-cache.mutinywallet.com/api" ) ;
1800
- let client = reqwest:: Client :: new ( ) ;
1801
-
1802
- let body = json ! ( [ "contact_list" , { "pubkey" : npub } ] ) ;
1803
- let data: Vec < Value > = Self :: primal_request ( & client, url, body) . await ?;
1804
- let mut metadata = parse_profile_metadata ( data) ;
1765
+ let mut metadata = self . nostr . primal_client . get_nostr_contacts ( npub) . await ?;
1805
1766
1806
1767
let contacts = self . storage . get_contacts ( ) ?;
1807
1768
1808
1769
// get contacts that weren't in our npub contacts list
1809
- let missing_pks: Vec < String > = contacts
1770
+ let missing_pks: Vec < :: nostr :: PublicKey > = contacts
1810
1771
. iter ( )
1811
1772
. filter_map ( |( _, c) | {
1812
1773
if c. npub . is_some_and ( |n| metadata. get ( & n) . is_none ( ) ) {
1813
- c. npub . map ( |n| n . to_hex ( ) )
1774
+ c. npub
1814
1775
} else {
1815
1776
None
1816
1777
}
1817
1778
} )
1818
1779
. collect ( ) ;
1819
1780
1820
1781
if !missing_pks. is_empty ( ) {
1821
- let body = json ! ( [ "user_infos" , { "pubkeys" : missing_pks } ] ) ;
1822
- let data: Vec < Value > = Self :: primal_request ( & client, url, body) . await ?;
1823
- let missing_metadata = parse_profile_metadata ( data) ;
1782
+ let missing_metadata = self
1783
+ . nostr
1784
+ . primal_client
1785
+ . get_user_profiles ( missing_pks)
1786
+ . await ?;
1824
1787
metadata. extend ( missing_metadata) ;
1825
1788
}
1826
1789
@@ -1872,60 +1835,33 @@ impl<S: MutinyStorage> MutinyWallet<S> {
1872
1835
until : Option < u64 > ,
1873
1836
since : Option < u64 > ,
1874
1837
) -> Result < Vec < DirectMessage > , MutinyError > {
1875
- let url = self
1876
- . config
1877
- . primal_url
1878
- . as_deref ( )
1879
- . unwrap_or ( "https://primal-cache.mutinywallet.com/api" ) ;
1880
- let client = reqwest:: Client :: new ( ) ;
1838
+ let events = self
1839
+ . nostr
1840
+ . primal_client
1841
+ . get_dm_conversation ( npub, self . nostr . public_key , limit, until, since)
1842
+ . await ?;
1881
1843
1882
- // api is a little weird, has sender and receiver but still gives full conversation
1883
- let sender = npub. to_hex ( ) ;
1884
- let receiver = self . nostr . public_key . to_hex ( ) ;
1885
- let body = match ( until, since) {
1886
- ( Some ( until) , Some ( since) ) => {
1887
- json ! ( [ "get_directmsgs" , { "sender" : sender, "receiver" : receiver, "limit" : limit, "until" : until, "since" : since } ] )
1888
- }
1889
- ( None , Some ( since) ) => {
1890
- json ! ( [ "get_directmsgs" , { "sender" : sender, "receiver" : receiver, "limit" : limit, "since" : since } ] )
1891
- }
1892
- ( Some ( until) , None ) => {
1893
- json ! ( [ "get_directmsgs" , { "sender" : sender, "receiver" : receiver, "limit" : limit, "until" : until } ] )
1894
- }
1895
- ( None , None ) => {
1896
- json ! ( [ "get_directmsgs" , { "sender" : sender, "receiver" : receiver, "limit" : limit, "since" : 0 } ] )
1844
+ let mut messages = Vec :: with_capacity ( events. len ( ) ) ;
1845
+ for event in events {
1846
+ if event. verify ( ) . is_err ( ) {
1847
+ continue ;
1897
1848
}
1898
- } ;
1899
- let data: Vec < Value > = Self :: primal_request ( & client, url, body) . await ?;
1900
-
1901
- let mut messages = Vec :: with_capacity ( data. len ( ) ) ;
1902
- for d in data {
1903
- let event = Event :: from_value ( d)
1904
- . ok ( )
1905
- . filter ( |e| e. kind == Kind :: EncryptedDirectMessage ) ;
1906
1849
1907
- if let Some ( event) = event {
1908
- // verify signature
1909
- if event. verify ( ) . is_err ( ) {
1910
- continue ;
1911
- }
1912
-
1913
- // if decryption fails, skip this message, just a bad dm
1914
- if let Ok ( message) = self . nostr . decrypt_dm ( npub, & event. content ) . await {
1915
- let to = if event. pubkey == npub {
1916
- self . nostr . public_key
1917
- } else {
1918
- npub
1919
- } ;
1920
- let dm = DirectMessage {
1921
- from : event. pubkey ,
1922
- to,
1923
- message,
1924
- date : event. created_at . as_u64 ( ) ,
1925
- event_id : event. id ,
1926
- } ;
1927
- messages. push ( dm) ;
1928
- }
1850
+ // if decryption fails, skip this message, just a bad dm
1851
+ if let Ok ( message) = self . nostr . decrypt_dm ( npub, & event. content ) . await {
1852
+ let to = if event. pubkey == npub {
1853
+ self . nostr . public_key
1854
+ } else {
1855
+ npub
1856
+ } ;
1857
+ let dm = DirectMessage {
1858
+ from : event. pubkey ,
1859
+ to,
1860
+ message,
1861
+ date : event. created_at . as_u64 ( ) ,
1862
+ event_id : event. id ,
1863
+ } ;
1864
+ messages. push ( dm) ;
1929
1865
}
1930
1866
}
1931
1867
0 commit comments