Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Jul 7, 2024
1 parent e204d94 commit 7a7e2cc
Show file tree
Hide file tree
Showing 19 changed files with 864 additions and 1,175 deletions.
36 changes: 12 additions & 24 deletions examples/ESP32TestNWC/ESP32TestNWC.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

#include "services/NWC.h"
#include "Transport.h"
#include "esp32/ESP32Platform.h"

#include "services/NWC.h"

// CONFIGURATION
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#define WIFI_CHANNEL 6
// IMPORTANT !!! Set a valid NWC url here
#define NWC_URL "nostr+walletconnect://69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9?relay=wss://relay.getalby.com/v1&secret=1205ebd25d79a52a4e51920f6e1981a43400dc49daaa65d367bd8090218d206c&[email protected]"
#define NWC_URL \
"nostr+walletconnect://69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9?relay=wss://relay.getalby.com/" \
"v1&secret=1205ebd25d79a52a4e51920f6e1981a43400dc49daaa65d367bd8090218d206c&[email protected]"
// Note: running this setting with default values will send 10 sats to the address below
#define PAYOUT_ADDRESS "[email protected]"
#define PAYOUT_AMOUNT_MSAT 10000


void testNWC();

void setup() {
Expand Down Expand Up @@ -46,6 +46,7 @@ void setup() {

testNWC();
}

nostr::NWC *nwc;
nostr::Transport *transport;

Expand All @@ -57,13 +58,8 @@ void testNWC() {
transport = nostr::esp32::ESP32Platform::getTransport();
nwc = new nostr::NWC(transport, NWC_URL);

nwc->getBalance(
[&](nostr::GetBalanceResponse resp) {
Serial.println("[!] Balance: " + String(resp.balance) + " msatoshis");
},
[](String err, String errMsg) {
Serial.println("[!] Error: " + err + " " + errMsg);
});
nwc->getBalance([&](nostr::GetBalanceResponse resp) { Serial.println("[!] Balance: " + String(resp.balance) + " msatoshis"); },
[](String err, String errMsg) { Serial.println("[!] Error: " + err + " " + errMsg); });
nwc->getInfo(
[&](nostr::GetInfoResponse resp) {
Serial.println("[!] Alias: " + resp.alias);
Expand All @@ -77,19 +73,11 @@ void testNWC() {
Serial.println(" " + method);
}
},
[](String err, String errMsg) {
Serial.println("[!] Error: " + err + " " + errMsg);
});
[](String err, String errMsg) { Serial.println("[!] Error: " + err + " " + errMsg); });

NostrString invoice = transport->getInvoiceFromLNAddr( PAYOUT_ADDRESS, PAYOUT_AMOUNT_MSAT, "Arduino NWC test");
Serial.println("[!] Paying " + String(PAYOUT_AMOUNT_MSAT) + " msats to " +
PAYOUT_ADDRESS + " invoice: " + invoice);
NostrString invoice = transport->getInvoiceFromLNAddr(PAYOUT_ADDRESS, PAYOUT_AMOUNT_MSAT, "Arduino NWC test");
Serial.println("[!] Paying " + String(PAYOUT_AMOUNT_MSAT) + " msats to " + PAYOUT_ADDRESS + " invoice: " + invoice);
nwc->payInvoice(
invoice, PAYOUT_AMOUNT_MSAT,
[&](nostr::PayInvoiceResponse resp) {
Serial.println("[!] Payment successful");
},
[](String err, String errMsg) {
Serial.println("[!] Error: " + err + " " + errMsg);
});
invoice, PAYOUT_AMOUNT_MSAT, [&](nostr::PayInvoiceResponse resp) { Serial.println("[!] Payment successful"); },
[](String err, String errMsg) { Serial.println("[!] Error: " + err + " " + errMsg); });
}
52 changes: 20 additions & 32 deletions examples/ESP32TestNip01/ESP32TestNip01.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "ArduinoJson.h"
#include "NostrEvent.h"
#include "NostrPool.h"
#include "Transport.h"
#include "Utils.h"
#include "esp32/ESP32Platform.h"
#include "Transport.h"
#include "time.h"

#define WIFI_SSID "Wokwi-GUEST"
Expand All @@ -13,11 +13,10 @@
#define RELAY "wss://nostr.rblb.it:7777"
#define PRIVKEY "1558dadfae151555818a6aa6cf046ca3dfbb196c419efc18482479a74b74009a"

std::vector<nostr::NostrPool*> pools;
std::vector<nostr::NostrPool *> pools;
void testNIP01();

void setup()
{
void setup() {
////////////////////////
/// INITIALIZATION
/// Note: you need some form of this code in your sketch
Expand Down Expand Up @@ -47,34 +46,29 @@ void setup()
testNIP01();
}



void loop(){

// 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 way
for(nostr::NostrPool* pool:pools){
// Run internal loop: refresh relays, complete pending connections, send pending messages
void loop() {
// 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
// way
for (nostr::NostrPool *pool : pools) {
// Run internal loop: refresh relays, complete pending connections, send
// pending messages
pool->loop();
}
}

nostr::Transport* transport;
nostr::Transport *transport;

// Just a test for basic
// nostr functionality
void testNIP01() {
String relay = RELAY;
// nostr private key
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
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

// Lets subscribe to the relay
String subId = pool->subscribeMany(
Expand All @@ -86,15 +80,13 @@ void testNIP01() {
// {"since",{"1234567890"}},
// {"until",{"1234567890"}},
// {"limit",{"10"}},
{"#t", {"arduinoTest"}}} //,
{"#t", {"arduinoTest"}}} //,
// You can add another filter here
},
[&](const String &subId, nostr::SignedNostrEvent *event) {
// This is the callback that will be called when an event is
// received We can access the event content with event->getContent()

// Here you should handle the event, for this test we will just
// serialize it and print to console
// Received events callback, we can access the event content with
// event->getContent() Here you should handle the event, for this
// test we will just serialize it and print to console
JsonDocument doc;
JsonArray arr = doc["data"].to<JsonArray>();
event->toSendableEvent(arr);
Expand All @@ -112,8 +104,7 @@ void testNIP01() {
// This is the callback that will be called when the subscription is
// EOSE
Serial.println("Subscription EOSE: " + subId);
}
);
});

// NB. you might want to save the subId somewhere since you are going to
// need it to close the subscription like so: pool.closeSubscription(subId);
Expand All @@ -123,8 +114,7 @@ void testNIP01() {

// Lets try to send an event
// First we create an unsigned event
nostr::UnsignedNostrEvent ev(1, "Hello, World!",
nostr::Utils::unixTimeSeconds());
nostr::UnsignedNostrEvent ev(1, "Hello, World!", nostr::Utils::unixTimeSeconds());
// we can add some tags
ev.getTags()->addTag("t", {"arduinoTest"});
// then we sign it with our private key and we will get a SignedNostrEvent
Expand All @@ -142,5 +132,3 @@ void testNIP01() {
pool->publish({relay}, &signEv);
// The event will be sent in the next loop() call
}


23 changes: 8 additions & 15 deletions examples/ESP32TestNip04/ESP32TestNip04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

void testNIP04();

void setup()
{
void setup() {
////////////////////////
/// INITIALIZATION
/// Note: you need some form of this code in your sketch
Expand All @@ -23,7 +22,6 @@ void setup()
////////////////////////
Serial.begin(115200);


Serial.println("Init time");
nostr::esp32::ESP32Platform::initTime("pool.ntp.org");

Expand All @@ -39,30 +37,25 @@ void setup()
testNIP04();
}


void testNIP04(){
// Test encryption used in dms and other things


void testNIP04() {
String privKeyA = "1558dadfae151555818a6aa6cf046ca3dfbb196c419efc18482479a74b74009a";
String pubKeyA = nostr::Utils::getPublicKey(privKeyA);
String privKeyB = "4ba55ee68773c243f9bc714fac149b623e3a583f92738a0c633d38535110f6f2";
String pubKeyB = nostr::Utils::getPublicKey(privKeyB);

String text = "Hello world 123";
Serial.println("Text: \"" + text+"\"");
Serial.println("Text: \"" + text + "\"");
nostr::Nip04 nip04;
String encryptedText = nip04.encrypt(privKeyA, pubKeyB, text);
Serial.println("Encrypted text: \"" + encryptedText+"\"");
Serial.println("Encrypted text: \"" + encryptedText + "\"");
String decryptedText = nip04.decrypt(privKeyB, pubKeyA, encryptedText);
Serial.println("Decrypted text: \"" + decryptedText+"\"");
if(text.equals(decryptedText)){
Serial.println("Decrypted text: \"" + decryptedText + "\"");

if (text.equals(decryptedText)) {
Serial.println("NIP04 test passed");
}else{
} else {
Serial.println("NIP04 test failed");
}

}

void loop() {}
33 changes: 11 additions & 22 deletions src/Nip04.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#include "Nip04.h"
using namespace nostr;
NostrString Nip04::decrypt(NostrString& privateKeyHex,
NostrString& senderPubKeyHex, NostrString content) {

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

int ivParamIndex = NostrString_indexOf(content, "?iv=");
NostrString encryptedMessage =
NostrString_substring(content, 0, ivParamIndex);
NostrString encryptedMessage = NostrString_substring(content, 0, ivParamIndex);

NostrString encryptedMessageHex = NostrString_base64ToHex(encryptedMessage);
int encryptedMessageSize = NostrString_length(encryptedMessageHex) / 2;
byte encryptedMessageBin[encryptedMessageSize];
NostrString_hexToBytes(encryptedMessageHex, encryptedMessageBin,
encryptedMessageSize);
NostrString_hexToBytes(encryptedMessageHex, encryptedMessageBin, encryptedMessageSize);
// fromHex(encryptedMessageHex, encryptedMessageBin,
// encryptedMessageSize);

Expand Down Expand Up @@ -40,13 +37,11 @@ NostrString Nip04::decrypt(NostrString& privateKeyHex,
NostrString sharedPointXHex = NostrString_bytesToHex(sharedPointX, 32);

NostrString message = decryptData(sharedPointX, ivBin, encryptedMessageHex);
message= NostrString_trim(message);
message = NostrString_trim(message);


return message;
}
NostrString Nip04::encrypt(NostrString& privateKeyHex,
NostrString& recipientPubKeyHex, NostrString content) {
NostrString Nip04::encrypt(NostrString &privateKeyHex, NostrString &recipientPubKeyHex, NostrString content) {
// Get shared point
// Create the private key object
int byteSize = 32;
Expand All @@ -57,8 +52,7 @@ NostrString Nip04::encrypt(NostrString& privateKeyHex,

byte publicKeyBin[64];
// fromHex("02" + NostrString(recipientPubKeyHex), publicKeyBin, 64);
NostrString_hexToBytes("02" + NostrString(recipientPubKeyHex), publicKeyBin,
64);
NostrString_hexToBytes("02" + NostrString(recipientPubKeyHex), publicKeyBin, 64);
PublicKey otherDhPublicKey(publicKeyBin);

byte sharedPointX[32];
Expand All @@ -83,8 +77,7 @@ NostrString Nip04::encrypt(NostrString& privateKeyHex,
uint8_t encryptedMessage[encryptedMessageSize];
// fromHex(encryptedMessageHex, encryptedMessage,
// encryptedMessageSize);
NostrString_hexToBytes(encryptedMessageHex, encryptedMessage,
encryptedMessageSize);
NostrString_hexToBytes(encryptedMessageHex, encryptedMessage, encryptedMessageSize);

String encryptedMessageBase64 = hexToBase64(encryptedMessageHex);

Expand All @@ -95,9 +88,7 @@ NostrString Nip04::encrypt(NostrString& privateKeyHex,

NostrString Nip04::encryptData(byte key[32], byte iv[16], NostrString msg) {
// message has to be padded at the end so it is a multiple of 16
int padding_diff = NostrString_length(msg) % 16 == 0
? 16
: 16 - (NostrString_length(msg) % 16);
int padding_diff = NostrString_length(msg) % 16 == 0 ? 16 : 16 - (NostrString_length(msg) % 16);

int byteSize = NostrString_length(msg) + padding_diff;
byte messageBin[byteSize];
Expand All @@ -111,8 +102,7 @@ NostrString Nip04::encryptData(byte key[32], byte iv[16], NostrString msg) {
return toHex(messageBin, sizeof(messageBin));
}

void Nip04::stringToByteArray(const char *input, int padding_diff,
byte *output) {
void Nip04::stringToByteArray(const char *input, int padding_diff, byte *output) {
int i = 0;
// remove end-of-string char
while (input[i] != '\0') {
Expand All @@ -126,8 +116,7 @@ void Nip04::stringToByteArray(const char *input, int padding_diff,
}
}

NostrString Nip04::decryptData(byte key[32], byte iv[16],
NostrString messageHex) {
NostrString Nip04::decryptData(byte key[32], byte iv[16], NostrString messageHex) {
int byteSize = NostrString_length(messageHex) / 2;
byte messageBin[byteSize];
NostrString_hexToBytes(messageHex, messageBin, byteSize);
Expand Down
29 changes: 11 additions & 18 deletions src/Nip04.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,17 @@
#include "Utils.h"

namespace nostr {
class Nip04 {
public:
NostrString decrypt(NostrString& privateKeyHex,
NostrString& senderPubKeyHex,
NostrString content);
NostrString encrypt(NostrString &privateKeyHex,
NostrString& recipientPubKeyHex,
NostrString content);
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) ;
private:
NostrString encryptData(byte key[32], byte iv[16], NostrString msg);

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

NostrString decryptData(byte key[32], byte iv[16],
NostrString messageHex) ;
};
} // namespace nostr
#endif // NIP47_H
NostrString decryptData(byte key[32], byte iv[16], NostrString messageHex);
};
} // namespace nostr
#endif // NIP47_H
Loading

0 comments on commit 7a7e2cc

Please sign in to comment.