Nostrduino
Loading...
Searching...
No Matches
NWC.h
Go to the documentation of this file.
1#ifndef _NOSTR_NWC_H
2#define _NOSTR_NWC_H
3#include <functional>
4#include <memory>
5#include <vector>
6
7#include "ArduinoJson.h"
8#include "Nip47.h"
9#include "NostrEvent.h"
10#include "NostrPool.h"
11#include "NostrString.h"
12#include "NostrTransport.h"
13#include "NostrUtils.h"
14
15#define NWC_INFO_KIND "13194"
16#define NWC_RESPONSE_KIND "23195"
17#define NWC_NIP04_NOTIFICATION_KIND "23196"
18#define NWC_NIP44_NOTIFICATION_KIND "23197"
19
20#define NWC_DEFAULT_TIMEOUT (60 * 10) // 10 minutes in seconds
21#define NWC_INFINITE_TIMEOUT (0xFFFFFFFF) // Max unsigned int, ~136 years
22
23namespace nostr {
24
42
44 public:
45 virtual ~NWCResponseCallbackBase() = default;
46 virtual void call(Nip47 *nip47, SignedNostrEvent *ev) {}
47 std::function<void(NostrString, NostrString)> onErr = nullptr;
48 unsigned long long timestampSeconds;
52 unsigned int n = 1;
53 bool requiresItemId = false;
54 bool strictItemIds = false;
55 std::vector<NostrString> expectedItemIds;
56 std::vector<NostrString> seenItemIds;
57 std::vector<NostrString> seenEventIds;
59};
60
61template <typename T> class NWCResponseCallback : public NWCResponseCallbackBase {
62 public:
63 void call(Nip47 *nip47, SignedNostrEvent *ev) override {
65 nip47->parseResponse(ev, resp);
67 if (this->onErr != nullptr) {
68 this->onErr(resp.errorCode, resp.errorMessage);
69 }
70 } else {
71 if (this->onRes != nullptr) {
72 this->onRes(resp.result);
73 }
74 }
75 }
76 std::function<void(T)> onRes = nullptr;
77};
78
82class NWC {
83 public:
84 ~NWC();
100 NWC(Transport *transport, NostrString nwcUrl, const NostrLimits &limits = NostrLimits(),
102
106 void loop();
107
111 void close();
112
116 bool isReady() const { return encryptionReady; }
117
119 bool isValid() const { return validConnection; }
120
124 Nip47Encryption getEncryption() const { return nip47.getEncryption(); }
125
133 void payInvoice(NostrString invoice, MilliSats amount = MILLISATS_UNSPECIFIED, std::function<void(PayInvoiceResponse)> onRes = nullptr,
134 std::function<void(NostrString, NostrString)> onErr = nullptr);
135
142 void multiPayInvoice(std::initializer_list<Invoice> invoices, std::function<void(MultiPayInvoiceResponse)> onRes = nullptr, std::function<void(NostrString, NostrString)> onErr = nullptr);
143 void multiPayInvoice(const std::vector<Invoice> &invoices, std::function<void(MultiPayInvoiceResponse)> onRes = nullptr,
144 std::function<void(NostrString, NostrString)> onErr = nullptr);
145
155 void payKeySend(NostrString pubkey, MilliSats amount, NostrString preimage = "", std::initializer_list<TLVRecords> tlv = {}, std::function<void(PayKeySendResponse)> onRes = nullptr,
156 std::function<void(NostrString, NostrString)> onErr = nullptr);
157 void payKeySend(NostrString pubkey, MilliSats amount, NostrString preimage, const std::vector<TLVRecords> &tlv,
158 std::function<void(PayKeySendResponse)> onRes = nullptr,
159 std::function<void(NostrString, NostrString)> onErr = nullptr);
160
167 void multiPayKeySend(std::initializer_list<KeySend> keySends, std::function<void(MultiPayKeySendResponse)> onRes = nullptr, std::function<void(NostrString, NostrString)> onErr = nullptr);
168 void multiPayKeySend(const std::vector<KeySend> &keySends, std::function<void(MultiPayKeySendResponse)> onRes = nullptr,
169 std::function<void(NostrString, NostrString)> onErr = nullptr);
170
180 void makeInvoice(MilliSats amount, NostrString description = "", NostrString descriptionHash = "", unsigned long expiry = 0, std::function<void(MakeInvoiceResponse)> onRes = nullptr,
181 std::function<void(NostrString, NostrString)> onErr = nullptr);
182
189 void lookUpPaymentHash(NostrString paymentHash, std::function<void(LookUpInvoiceResponse)> onRes = nullptr, std::function<void(NostrString, NostrString)> onErr = nullptr);
190
197 void lookUpInvoice(NostrString invoice, std::function<void(LookUpInvoiceResponse)> onRes = nullptr, std::function<void(NostrString, NostrString)> onErr = nullptr);
198
210 void listTransactions(unsigned long from = 0, unsigned long until = 0, int limit = 0, int offset = 0, bool unpaid = false, NostrString type = "",
211 std::function<void(ListTransactionsResponse)> onRes = nullptr, std::function<void(NostrString, NostrString)> onErr = nullptr);
212
218 void getBalance(std::function<void(GetBalanceResponse)> onRes = nullptr, std::function<void(NostrString, NostrString)> onErr = nullptr);
219
225 void getInfo(std::function<void(GetInfoResponse)> onRes = nullptr, std::function<void(NostrString, NostrString)> onErr = nullptr);
226
232 void subscribeNotifications(std::function<void(NotificationResponse)> onRes, std::function<void(NostrString, NostrString)> onErr);
233
236 if (this->pool != nullptr) this->pool->setSeenTracker(callback);
237 }
238
239 private:
240 struct PendingRequest {
241 std::function<void()> send;
242 std::function<void(NostrString, NostrString)> onErr;
243 };
244
245 Transport *transport;
246 NostrLimits limits;
247 NWCEncryptionPolicy encryptionPolicy;
248 NostrString sendEvent(SignedNostrEvent *ev,
249 std::function<void(NostrString, NostrString)> onErr);
250 void discoverEncryption();
251 void finishEncryptionDiscovery();
252 void selectEncryption(Nip47Encryption encryption, bool includeEncryptionTag);
253 void rejectUnsupportedEncryption();
254 void failEncryption(NostrString errorCode, NostrString errorMessage);
255 bool deferUntilEncryptionReady(std::function<void()> send,
256 std::function<void(NostrString, NostrString)> onErr);
257 bool flushPendingRequests();
258 bool rejectIfCallbackQueueFull(std::function<void(NostrString, NostrString)> onErr);
259 void finishInfoSubscription();
260 void performClose();
261 void removeCallback(const std::shared_ptr<NWCResponseCallbackBase> &callback);
262 std::unique_ptr<NostrPool> pool;
263 NWCData nwc;
264 Nip47 nip47;
265 NostrString accountPubKey;
266 NostrString infoSubId;
267 bool encryptionReady = false;
268 bool encryptionFailed = false;
269 unsigned int notificationKind = 0;
270 NostrString encryptionErrorCode;
271 NostrString encryptionErrorMessage;
272 std::vector<PendingRequest> pendingRequests;
273 std::vector<std::shared_ptr<NWCResponseCallbackBase>> callbacks;
274 unsigned int poolDispatchDepth = 0;
275 bool deferredClose = false;
276 bool closed = false;
277 bool validConnection = false;
278 unsigned long long discoveryStartedAt = 0;
279 bool sawInfoEvent = false;
280 bool sawNip44 = false;
281 bool sawNip04 = false;
282 bool sawLegacyWithoutTag = false;
283};
284}
285
286#endif
#define NWC_DEFAULT_TIMEOUT
Definition NWC.h:20
long int NostrString_length(const std::string &str)
Definition NostrString.cpp:180
#define NostrString
Definition NostrString.h:10
std::vector< NostrString > expectedItemIds
Definition NWC.h:55
NostrString subId
Definition NWC.h:51
virtual void call(Nip47 *nip47, SignedNostrEvent *ev)
Definition NWC.h:46
unsigned int n
Definition NWC.h:52
unsigned int timeoutSeconds
Definition NWC.h:49
NostrString eventId
Definition NWC.h:50
std::vector< NostrString > seenEventIds
Definition NWC.h:57
std::function< void(NostrString, NostrString)> onErr
Definition NWC.h:47
std::vector< NostrString > seenItemIds
Definition NWC.h:56
unsigned long long timestampSeconds
Definition NWC.h:48
bool accept(SignedNostrEvent *event)
Definition NWC.cpp:28
bool requiresItemId
Definition NWC.h:53
bool strictItemIds
Definition NWC.h:54
virtual ~NWCResponseCallbackBase()=default
Definition NWC.h:61
void call(Nip47 *nip47, SignedNostrEvent *ev) override
Definition NWC.h:63
std::function< void(T)> onRes
Definition NWC.h:76
Definition NWC.h:82
Nip47Encryption getEncryption() const
Definition NWC.h:124
void payInvoice(NostrString invoice, MilliSats amount=MILLISATS_UNSPECIFIED, std::function< void(PayInvoiceResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:371
void multiPayKeySend(std::initializer_list< KeySend > keySends, std::function< void(MultiPayKeySendResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:476
void lookUpInvoice(NostrString invoice, std::function< void(LookUpInvoiceResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:564
bool isReady() const
Definition NWC.h:116
void getBalance(std::function< void(GetBalanceResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:599
void payKeySend(NostrString pubkey, MilliSats amount, NostrString preimage="", std::initializer_list< TLVRecords > tlv={}, std::function< void(PayKeySendResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:443
void setSeenTracker(NostrSeenCallback callback)
Definition NWC.h:235
void listTransactions(unsigned long from=0, unsigned long until=0, int limit=0, int offset=0, bool unpaid=false, NostrString type="", std::function< void(ListTransactionsResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:580
void lookUpPaymentHash(NostrString paymentHash, std::function< void(LookUpInvoiceResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:548
void loop()
Definition NWC.cpp:240
void makeInvoice(MilliSats amount, NostrString description="", NostrString descriptionHash="", unsigned long expiry=0, std::function< void(MakeInvoiceResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:521
void close()
Definition NWC.cpp:216
~NWC()
Definition NWC.cpp:45
void multiPayInvoice(std::initializer_list< Invoice > invoices, std::function< void(MultiPayInvoiceResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:395
void getInfo(std::function< void(GetInfoResponse)> onRes=nullptr, std::function< void(NostrString, NostrString)> onErr=nullptr)
Definition NWC.cpp:615
void subscribeNotifications(std::function< void(NotificationResponse)> onRes, std::function< void(NostrString, NostrString)> onErr)
Definition NWC.cpp:335
bool isValid() const
Definition NWC.h:119
Definition Nip47.h:44
NostrString errorMessage
Definition Nip47.h:50
T result
Definition Nip47.h:52
NostrString errorCode
Definition Nip47.h:49
Definition Nip47.h:176
void parseResponse(SignedNostrEvent *response, Nip47Response< PayInvoiceResponse > &out)
Definition Nip47.cpp:323
Nip47Encryption getEncryption() const
Definition Nip47.h:181
Definition NostrEvent.h:130
Definition NostrTransport.h:21
Definition CryptoLock.h:6
std::function< bool(const SignedNostrEvent &)> NostrSeenCallback
Definition NostrPool.h:35
NWCEncryptionPolicy
Definition NWC.h:38
struct nostr::s_GetBalanceResponse GetBalanceResponse
struct nostr::s_ListTransactionsResponse ListTransactionsResponse
struct nostr::s_PayKeySendResponse PayKeySendResponse
struct nostr::s_MakeInvoiceResponse MakeInvoiceResponse
struct nostr::s_LookUpInvoiceResponse LookUpInvoiceResponse
Nip47Encryption
Definition Nip47.h:15
int64_t MilliSats
Definition NostrAmount.h:26
struct nostr::s_NWCData NWCData
struct nostr::s_NotificationResponse NotificationResponse
struct nostr::s_GetInfoResponse GetInfoResponse
struct nostr::s_MultiPayKeySendResponse MultiPayKeySendResponse
Definition NostrLimits.h:9
Definition Nip47.h:55