Skip to content

Commit

Permalink
cleanup and format
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Jul 30, 2024
1 parent 07f0aa3 commit f58db50
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 77 deletions.
2 changes: 1 addition & 1 deletion examples/ESP32TestNWC/ESP32TestNWC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void testNWC() {
[](String err, String errMsg) { Serial.println("[!] Error: " + err + " " + errMsg); });

transport->getInvoiceFromLNAddr(PAYOUT_ADDRESS, PAYOUT_AMOUNT_MSAT, "Arduino NWC test", [&](String invoice) {
if(NostrString_equals(invoice,"")){
if (NostrString_equals(invoice, "")) {
Serial.println("[!] Error: Could not get invoice from LN address");
}
Serial.println("[!] Paying " + String(PAYOUT_AMOUNT_MSAT) + " msats to " + PAYOUT_ADDRESS + " invoice: " + invoice);
Expand Down
18 changes: 8 additions & 10 deletions examples/ESP32TestNip01/ESP32TestNip01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Arduino.h>


#include "ArduinoJson.h"
#include "NostrEvent.h"
#include "NostrPool.h"
Expand All @@ -13,7 +12,6 @@
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""


#define RELAY "wss://nostr.rblb.it:7777"
#define PRIVKEY "1558dadfae151555818a6aa6cf046ca3dfbb196c419efc18482479a74b74009a"

Expand Down Expand Up @@ -65,27 +63,27 @@ void loop() {
nostr::Transport *transport;

void testNIP01() {
try{
try {
String relay = RELAY;
String privKey = PRIVKEY;
transport = nostr::esp32::ESP32Platform::getTransport();

// We need a NostrPool instance that will handle all the communication
nostr::NostrPool *pool = new nostr::NostrPool(transport);
pools.push_back(pool); // NB. we are adding it to this vector since we need to call
// pool->loop() in the main loop to make it work properly
// pool->loop() in the main loop to make it work properly

// Lets subscribe to the relay
String subId = pool->subscribeMany(
{relay},
{
{// we set the filters here (see
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// You can add another filter here
},
[&](const String &subId, nostr::SignedNostrEvent *event) {
Expand Down
22 changes: 9 additions & 13 deletions examples/ESP32TestNip01Filter/ESP32TestNip01Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "Utils.h"
#include "time.h"


#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""

Expand Down Expand Up @@ -65,10 +64,7 @@ void testNIP01Filter() {
String subId = pool->subscribeMany(
{relay},
{{
{"kinds", {"1"}},
{"since", {"1626023056"}},
{"until", {"1846947856"}},
{"limit", {"10"}},
{"kinds", {"1"}}, {"since", {"1626023056"}}, {"until", {"1846947856"}}, {"limit", {"10"}},
// Filters defined in NIP01 are automatically converted to the correct type
// however this library support non-NIP01 filters as well, but you might need to
// specify their type manually, if unspecified the code assumes string[]:
Expand All @@ -91,16 +87,16 @@ void testNIP01Filter() {
std::vector<nostr::NostrRelay *> *relays = pool->getConnectedRelays();
for (nostr::NostrRelay *relay : *relays) {
Serial.println("Registering to connection events of: " + relay->getUrl());
relay->getConnection()->addConnectionStatusListener([&](const nostr::ConnectionStatus &status) {
String sstatus="UNKNOWN";
if(status==nostr::ConnectionStatus::CONNECTED){
sstatus="CONNECTED";
}else if(status==nostr::ConnectionStatus::DISCONNECTED){
sstatus="DISCONNECTED";
}else if(status==nostr::ConnectionStatus::ERROR){
relay->getConnection()->addConnectionStatusListener([&](const nostr::ConnectionStatus &status) {
String sstatus = "UNKNOWN";
if (status == nostr::ConnectionStatus::CONNECTED) {
sstatus = "CONNECTED";
} else if (status == nostr::ConnectionStatus::DISCONNECTED) {
sstatus = "DISCONNECTED";
} else if (status == nostr::ConnectionStatus::ERROR) {
sstatus = "ERROR";
}
Serial.println("Connection status changed: " + sstatus);
Serial.println("Connection status changed: " + sstatus);
});
}
} catch (const std::exception &e) {
Expand Down
30 changes: 16 additions & 14 deletions examples/ESP8266TestNip01/ESP8266TestNip01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ void setup() {
////////////////////////
Serial.begin(115200);

ESP.wdtDisable();
*((volatile uint32_t *)0x60000900) &= ~(1);
// HACK:
// Uncomment this if the code crashes with cause: 4
// it seems there are some issues with the watchdog
// in some boards
// ESP.wdtDisable();
// *((volatile uint32_t *)0x60000900) &= ~(1);

Serial.println("Init wifi");
nostr::esp8266::ESP8266Platform::initWifi(WIFI_SSID, WIFI_PASS);
Expand All @@ -50,17 +54,15 @@ void setup() {
////////////////////////
/// END OF INITIALIZATION
////////////////////////


}

bool initialized = false;
void loop() {
if(!initialized){
if (!initialized) {
initialized = true;
testNIP01();
delay(1000);
}else{
} else {
// We need to call some pool methods in the main loop to make its internal
// loop work properly it is not very important how it is done, here we keep
// a reference of every pool in a vector but you can do it in a different
Expand All @@ -77,27 +79,27 @@ nostr::Transport *transport;

void testNIP01() {
yield();
try{
try {
String relay = RELAY;
String privKey = PRIVKEY;
transport = nostr::esp8266::ESP8266Platform::getTransport();

// We need a NostrPool instance that will handle all the communication
nostr::NostrPool *pool = new nostr::NostrPool(transport);
pools.push_back(pool); // NB. we are adding it to this vector since we need to call
// pool->loop() in the main loop to make it work properly
// pool->loop() in the main loop to make it work properly

// Lets subscribe to the relay
String subId = pool->subscribeMany(
{relay},
{
{// we set the filters here (see
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// You can add another filter here
},
[&](const String &subId, nostr::SignedNostrEvent *event) {
Expand Down
19 changes: 9 additions & 10 deletions examples/PICOWTestNip01/PICOWTestNip01.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

#include "picow/PICOWPlatform.h"

#include "picow/PICOWPlatform.h"

#include "ArduinoJson.h"
#include "NostrEvent.h"
Expand Down Expand Up @@ -64,27 +63,27 @@ void loop() {
nostr::Transport *transport;

void testNIP01() {
try{
try {
String relay = RELAY;
String privKey = PRIVKEY;
transport = nostr::picow::PICOWPlatform::getTransport();

// We need a NostrPool instance that will handle all the communication
nostr::NostrPool *pool = new nostr::NostrPool(transport);
pools.push_back(pool); // NB. we are adding it to this vector since we need to call
// pool->loop() in the main loop to make it work properly
// pool->loop() in the main loop to make it work properly

// Lets subscribe to the relay
String subId = pool->subscribeMany(
{relay},
{
{// we set the filters here (see
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// You can add another filter here
},
[&](const String &subId, nostr::SignedNostrEvent *event) {
Expand Down
17 changes: 8 additions & 9 deletions examples/UNOR4TestNip01/UNOR4TestNip01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,28 @@ void loop() {
}
}


void testNIP01() {
try{
try {
String relay = RELAY;
String privKey = PRIVKEY;
transport = nostr::unor4::UNOR4Platform::getTransport();

// We need a NostrPool instance that will handle all the communication
nostr::NostrPool *pool = new nostr::NostrPool(transport);
pools.push_back(pool); // NB. we are adding it to this vector since we need to call
// pool->loop() in the main loop to make it work properly
// pool->loop() in the main loop to make it work properly

// Lets subscribe to the relay
String subId = pool->subscribeMany(
{relay},
{
{// we set the filters here (see
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// https://github.com/nostr-protocol/nips/blob/master/01.md#from-client-to-relay-sending-events-and-creating-subscriptions)
{"kinds", {"1"}},
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
// You can add another filter here
},
[&](const String &subId, nostr::SignedNostrEvent *event) {
Expand Down
3 changes: 0 additions & 3 deletions src/Nip04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using namespace nostr;

NostrString Nip04::decrypt(NostrString &privateKeyHex, NostrString &senderPubKeyHex, NostrString content) {

int ivParamIndex = NostrString_indexOf(content, "?iv=");
if (ivParamIndex == -1) throw std::invalid_argument("Encrypted string does not contain ?iv= parameter.");

Expand Down Expand Up @@ -31,7 +30,6 @@ NostrString Nip04::decrypt(NostrString &privateKeyHex, NostrString &senderPubKey
}

NostrString Nip04::encrypt(NostrString &privateKeyHex, NostrString &recipientPubKeyHex, NostrString content) {

uint8_t sharedPointX[32];
Utils::ecdh(privateKeyHex, recipientPubKeyHex, sharedPointX);
NostrString sharedPointXHex = Utils::toHex(sharedPointX, 32);
Expand Down Expand Up @@ -70,7 +68,6 @@ void Nip04::stringToByteArray(const char *input, int padding_diff, byte *output)
output[i] = input[i];
i++;
}

// pad between 1 and 16 bytes
for (int j = 0; j < padding_diff; j++) {
output[i + j] = padding_diff;
Expand Down
3 changes: 0 additions & 3 deletions src/Nip04.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ class Nip04 {
public:
NostrString decrypt(NostrString &privateKeyHex, NostrString &senderPubKeyHex, NostrString content);
NostrString encrypt(NostrString &privateKeyHex, NostrString &recipientPubKeyHex, NostrString content);

private:
NostrString encryptData(byte key[32], byte iv[16], NostrString msg);

void stringToByteArray(const char *input, int padding_diff, byte *output);

NostrString decryptData(byte key[32], byte iv[16], NostrString messageHex);
};
}
Expand Down
1 change: 1 addition & 0 deletions src/Nip47.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ SignedNostrEvent Nip47::getInfo() {
JsonObject params = doc["params"].to<JsonObject>();
return this->createEvent("get_info", doc);
}

SignedNostrEvent Nip47::multiPayInvoice(std::initializer_list<Invoice> invoices) {
JsonDocument doc;
JsonObject params = doc["params"].to<JsonObject>();
Expand Down
3 changes: 3 additions & 0 deletions src/Nip47.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#include <initializer_list>
#include <vector>
namespace nostr {

typedef struct s_Invoice {
NostrString invoice;
unsigned long amount;
} Invoice;

typedef struct s_TLVRecords {
NostrString type;
NostrString value;
} TLVRecords;

typedef struct s_KeySend {
NostrString pubkey;
unsigned long amount;
Expand Down
13 changes: 0 additions & 13 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
#include "uBitcoin/src/Bitcoin.h"
#include "uBitcoin/src/Hash.h"


// #define LONESHA256_STATIC
// #include "lonesha256.h"
using namespace nostr;
// size_t context_size = secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
// void* nostr::Utils::secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);

std::function<void(const NostrString &)> Utils::logger = nullptr;
unsigned long long Utils::subs = 0;
Expand Down Expand Up @@ -57,7 +52,6 @@ long int Utils::randomInt(long int min, long int max) {
return realRandom(min, max);
}


void Utils::jsonParse(const NostrString *json, JsonDocument *doc) {
DeserializationError error = deserializeJson(*doc, *json);
if (error) {
Expand All @@ -69,9 +63,6 @@ void Utils::jsonStringify(JsonVariantConst source, NostrString *json) {
serializeJson(source, *json);
}




NostrString Utils::toHex(const uint8_t *array, size_t arraySize) {
return ::toHex(array, arraySize);
}
Expand All @@ -88,20 +79,16 @@ size_t Utils::fromHex(NostrString hex, uint8_t *output, size_t outputSize) {
return ::fromHex(hex, output, outputSize);
}


size_t Utils::fromBase64(const char *encoded, size_t encodedSize, uint8_t *output, size_t outputSize, uint8_t flags) {
return ::fromBase64(encoded, encodedSize, output, outputSize, flags);
}




size_t Utils::toBase64(const uint8_t *array, size_t arraySize, char *output, size_t outputSize, uint8_t flags) {
return ::toBase64(array, arraySize, output, outputSize, flags);
}



size_t Utils::sha256(NostrString message, uint8_t *hash) {
return ::sha256(message, hash);
}
Expand Down
1 change: 1 addition & 0 deletions src/esp8266/ESP8266Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void close() {
* Initialize platform specific code for the nostr library
*/
void initNostr(bool withLogger) {
Utils::init();

nostr::Utils::setUnixTimeSecondsProvider(getUnixTimestamp);
if (withLogger)
Expand Down
1 change: 1 addition & 0 deletions src/picow/PICOWPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void initTime(const char *ntpServer) {
*/
void initNostr(unsigned long seed, bool withLogger) {
randomSeed(seed);
Utils::init();
nostr::Utils::setUnixTimeSecondsProvider(getUnixTimestamp);
if (withLogger)
nostr::Utils::setLogger(serialLogger);
Expand Down
1 change: 1 addition & 0 deletions src/uBitcoin
Submodule uBitcoin added at 877542
Loading

0 comments on commit f58db50

Please sign in to comment.