Skip to content

Commit c9e6900

Browse files
Merge pull request #2 from blackcoffeexbt/feature/subscription-construction
Added NostrRequestObject
2 parents 3f225b6 + d361b6a commit c9e6900

File tree

5 files changed

+259
-5
lines changed

5 files changed

+259
-5
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
This example sketch shows how to use the NostrRequestObject to construct a request for events
3+
*
4+
*/
5+
#include <Arduino.h>
6+
#include "WiFiClientSecure.h"
7+
#include "time.h"
8+
#include <NostrEvent.h>
9+
#include <NostrRelayManager.h>
10+
11+
const char* ssid = "wubwub"; // wifi SSID here
12+
const char* password = "blob19750405blob"; // wifi password here
13+
14+
NostrEvent nostr;
15+
NostrRelayManager nostrRelayManager;
16+
NostrQueueProcessor nostrQueue;
17+
18+
bool hasSentEvent = false;
19+
20+
// NTP server to request epoch time
21+
const char* ntpServer = "pool.ntp.org";
22+
const long gmtOffset_sec = 0;
23+
const int daylightOffset_sec = 3600;
24+
25+
char const *nsecHex = "<SENDER PRIVATE KEY IN HEX FORMAT>"; // sender private key in hex e.g. bdd19cecdXXXXXXXXXXXXXXXXXXXXXXXXXX
26+
char const *npubHex = "<SENDER PUBLIC KEY IN HEX FORMAT>"; // sender public key in hex e.g. d0bfc94bd4324f7df2a7601c4177209828047c4d3904d64009a3c67fb5d5e7ca
27+
// BC
28+
char const *testRecipientPubKeyHex = "<RECIPIENT PUB KEY IN HEX FORMAT>"; // e.g. // sender public key 683211bd155c7b764e4b99ba263a151d81209be7a566a2bb1971dc1bbd3b715e
29+
30+
unsigned long getUnixTimestamp() {
31+
time_t now;
32+
struct tm timeinfo;
33+
if(!getLocalTime(&timeinfo)){
34+
Serial.println("Failed to obtain time");
35+
return 0;
36+
} else {
37+
Serial.println("Got timestamp of " + String(now));
38+
}
39+
time(&now);
40+
return now;
41+
}
42+
43+
void okEvent(const std::string& key, const char* payload) {
44+
Serial.println("OK event");
45+
Serial.println("payload is: ");
46+
Serial.println(payload);
47+
}
48+
49+
void nip01Event(const std::string& key, const char* payload) {
50+
Serial.println("NIP01 event");
51+
Serial.println("payload is: ");
52+
Serial.println(payload);
53+
}
54+
55+
void nip04Event(const std::string& key, const char* payload) {
56+
Serial.println("NIP04 event");
57+
String dmMessage = nostr.decryptDm(nsecHex, payload);
58+
Serial.println("message is: ");
59+
Serial.println(dmMessage);
60+
}
61+
62+
void setup() {
63+
Serial.begin(115200);
64+
65+
WiFi.begin(ssid, password);
66+
67+
while (WiFi.status() != WL_CONNECTED) {
68+
delay(500);
69+
Serial.print(".");
70+
}
71+
Serial.println("");
72+
Serial.println("WiFi connected");
73+
Serial.println("IP address: ");
74+
Serial.println(WiFi.localIP());
75+
76+
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
77+
78+
long timestamp = getUnixTimestamp();
79+
80+
const char *const relays[] = {
81+
"relay.damus.io",
82+
"nostr.mom",
83+
"relay.nostr.bg"
84+
};
85+
int relayCount = sizeof(relays) / sizeof(relays[0]);
86+
87+
nostr.setLogging(false);
88+
nostrRelayManager.setRelays(relays, relayCount);
89+
nostrRelayManager.setMinRelaysAndTimeout(2,10000);
90+
91+
// Set some event specific callbacks here
92+
nostrRelayManager.setEventCallback("ok", okEvent);
93+
nostrRelayManager.setEventCallback("nip01", nip01Event);
94+
nostrRelayManager.setEventCallback("nip04", nip04Event);
95+
96+
nostrRelayManager.connect();
97+
98+
NostrRequestOptions* eventRequestOptions = new NostrRequestOptions();
99+
100+
// // Populate ids
101+
// // String ids[] = {"id1", "id2", "id3"};
102+
// // eventRequestOptions->ids = ids;
103+
// // eventRequestOptions->ids_count = sizeof(ids) / sizeof(ids[0]);
104+
105+
// // Populate authors
106+
String authors[] = {"d0bfc94bd4324f7df2a7601c4177209828047c4d3904d64009a3c67fb5d5e7ca", "91c9a5e1a9744114c6fe2d61ae4de82629eaaa0fb52f48288093c7e7e036f832", "c1fc7771f5fa418fd3ac49221a18f19b42ccb7a663da8f04cbbf6c08c80d20b1"};
107+
eventRequestOptions->authors = authors;
108+
eventRequestOptions->authors_count = sizeof(authors) / sizeof(authors[0]);
109+
110+
// Populate kinds
111+
int kinds[] = {1};
112+
eventRequestOptions->kinds = kinds;
113+
eventRequestOptions->kinds_count = sizeof(kinds) / sizeof(kinds[0]);
114+
115+
// // Populate #e
116+
// // String e[] = {"1", "4"};
117+
// // eventRequestOptions->e = e;
118+
// // eventRequestOptions->e_count = sizeof(e) / sizeof(e[0]);
119+
120+
// // Populate #p
121+
// // String p[] = {"91c9a5e1a9744114c6fe2d61ae4de82629eaaa0fb52f48288093c7e7e036f832"};
122+
// // eventRequestOptions->p = p;
123+
// // eventRequestOptions->p_count = sizeof(p) / sizeof(p[0]);
124+
125+
// // Populate other fields
126+
// // eventRequestOptions->since = 1609459200;
127+
// // eventRequestOptions->until = 1640995200;
128+
eventRequestOptions->limit = 5;
129+
130+
nostrRelayManager.requestEvents(eventRequestOptions);
131+
132+
delete eventRequestOptions;
133+
}
134+
135+
void loop() {
136+
nostrRelayManager.loop();
137+
nostrRelayManager.broadcastEvents();
138+
}

src/NostrRelayManager.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <NostrRelayManager.h>
77
#include <NostrEvent.h>
88
#include <WebSocketsClient.h>
9+
#include "ArduinoJson.h"
910

1011
/**
1112
* @brief Construct a new Nostr Relay Manager:: Nostr Relay Manager object
@@ -60,13 +61,17 @@ bool NostrRelayManager::hasEnqueuedMessages() {
6061
}
6162

6263
/**
63-
* @brief Subscribe to a relay event. Currently a proxy for broadcast
64-
* TODO: Add parameters for easier subscriptions
64+
* @brief Queue an event request message to be sent to relays
6565
*
66-
* @param subscriptionJson
66+
* @param options A NostrRequestOptions object with arguments for the requested event(s)
6767
*/
68-
void NostrRelayManager::subscribe(String subscriptionJson) {
69-
broadcastEvent(subscriptionJson);
68+
void NostrRelayManager::requestEvents(const NostrRequestOptions* options) {
69+
// now put the json within a REQ to be sent to the relays
70+
String serialisedJson = "[\"REQ\", \"" + getNewSubscriptionId() + "\"," + options->toJson() + "]";
71+
Serial.println(F("REQ looks like this"));
72+
Serial.println(serialisedJson);
73+
74+
enqueueMessage(serialisedJson.c_str());
7075
}
7176

7277
/**

src/NostrRelayManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <WebSocketsClient.h>
1111
#include <NostrQueueProcessor.h>
1212
#include <NostrEvent.h>
13+
#include "NostrRequestOptions.h"
1314
#include <string>
1415
#include <map>
1516
#include <functional>
@@ -25,6 +26,7 @@ class NostrRelayManager
2526
void setMinRelaysAndTimeout(int minRelays, unsigned long minRelaysTimeout);
2627

2728
void subscribe(String subscriptionJson);
29+
void requestEvents(const NostrRequestOptions* options);
2830
void setEventCallback(const std::string& key, EventCallbackFn callback);
2931

3032
void performEventAction(const std::string& key, const char *payload);

src/NostrRequestOptions.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "NostrRequestOptions.h"
2+
3+
NostrRequestOptions::NostrRequestOptions()
4+
: ids(nullptr),
5+
ids_count(0),
6+
authors(nullptr),
7+
authors_count(0),
8+
kinds(nullptr),
9+
kinds_count(0),
10+
e(nullptr),
11+
e_count(0),
12+
p(nullptr),
13+
p_count(0),
14+
since(-1),
15+
until(-1),
16+
limit(-1) {}
17+
18+
/**
19+
* @brief serialise a NostrRequestOptions object to a JSON string
20+
*
21+
* @return String
22+
*/
23+
String NostrRequestOptions::toJson() const {
24+
StaticJsonDocument<1024> json;
25+
26+
if (ids) {
27+
JsonArray idsArray = json.createNestedArray("ids");
28+
for (int i = 0; i < ids_count; i++) {
29+
idsArray.add(ids[i]);
30+
}
31+
}
32+
33+
if (authors) {
34+
JsonArray authorsArray = json.createNestedArray("authors");
35+
for (int i = 0; i < authors_count; i++) {
36+
authorsArray.add(authors[i]);
37+
}
38+
}
39+
40+
if (kinds) {
41+
JsonArray kindsArray = json.createNestedArray("kinds");
42+
for (int i = 0; i < kinds_count; i++) {
43+
kindsArray.add(kinds[i]);
44+
}
45+
}
46+
47+
if (e) {
48+
JsonArray eArray = json.createNestedArray("#e");
49+
for (int i = 0; i < e_count; i++) {
50+
eArray.add(e[i]);
51+
}
52+
}
53+
54+
if (p) {
55+
JsonArray pArray = json.createNestedArray("#p");
56+
for (int i = 0; i < p_count; i++) {
57+
pArray.add(p[i]);
58+
}
59+
}
60+
61+
if (since != -1) {
62+
json["since"] = since;
63+
}
64+
if (until != -1) {
65+
json["until"] = until;
66+
}
67+
if (limit != -1) {
68+
json["limit"] = limit;
69+
}
70+
71+
String jsonString;
72+
serializeJson(json, jsonString);
73+
74+
return jsonString;
75+
}

src/NostrRequestOptions.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef NOSTR_REQUEST_OPTIONS_H
2+
#define NOSTR_REQUEST_OPTIONS_H
3+
4+
#include <Arduino.h>
5+
#include <ArduinoJson.h>
6+
7+
class NostrRequestOptions {
8+
public:
9+
String* ids;
10+
int ids_count;
11+
12+
String* authors;
13+
int authors_count;
14+
15+
int* kinds;
16+
int kinds_count;
17+
18+
String* e;
19+
int e_count;
20+
21+
String* p;
22+
int p_count;
23+
24+
long since;
25+
long until;
26+
int limit;
27+
28+
String toJson() const;
29+
30+
// Default constructor
31+
NostrRequestOptions();
32+
};
33+
34+
#endif

0 commit comments

Comments
 (0)