Skip to content

Commit

Permalink
cpp-nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteCat6142 committed Aug 13, 2024
1 parent cffca26 commit 48d0018
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 126 deletions.
21 changes: 19 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@
"typeinfo": "cpp",
"variant": "cpp",
"bitset": "cpp",
"regex": "cpp"
}
"regex": "cpp",
"csignal": "cpp",
"cstring": "cpp",
"*.ipp": "cpp",
"any": "cpp",
"strstream": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"coroutine": "cpp",
"forward_list": "cpp",
"set": "cpp",
"unordered_set": "cpp",
"source_location": "cpp",
"fstream": "cpp",
"cfenv": "cpp",
"typeindex": "cpp",
"valarray": "cpp"
},
"cmake.configureOnOpen": false
}
49 changes: 46 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include <rx_nostr.hpp>
#include <logger_stdout.hpp>
#include <thread>
#include <iostream>

#include <src/nostr_event_sign_yyjson.hpp>
#include <src/utils.hpp>

#include <bech32.h>

#include <ctime>

using namespace std::chrono_literals;
using namespace rx_nostr;
using namespace cpp_nostr;

static const int MAX_EVENTS = 300;
static LoggerInterface *logger = nullptr;
Expand All @@ -21,7 +23,7 @@ void callback(const NostrEvent &event)
{
return;
}
logger->log(LogLevel::INFO, event.content);
//logger->log(LogLevel::INFO, event.content);
}

template <int from, int to, typename Iterator, typename Fn>
Expand Down Expand Up @@ -74,7 +76,7 @@ std::string sign(char *nsec)
sk[pos++] = c;
});
NostrEvent ev;
std::vector<std::vector<char *>> vec{};
std::vector<std::vector<std::string>> vec{};
ev.content = "test";
ev.created_at = now();
ev.kind = 1;
Expand All @@ -84,8 +86,49 @@ std::string sign(char *nsec)
return i.encode(ev);
}



int main(int argc, char *argv[])
{

std::vector<uint8_t> data = hex2bytes("7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e");
bech32::DecodedResult decodedResult = bech32::decode("npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg");
uint8_t sk[32];
// 5bits -> 8bits
convert_bits<5, 8>(decodedResult.dp.begin(), decodedResult.dp.end(),
[&, pos = 0U](unsigned char c) mutable
{
if (pos < 32)
sk[pos++] = c;
});
std::vector<uint8_t> re(std::begin(sk), std::end(sk));
std::cout << decodedResult.hrp << std::endl;
std::copy(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
std::copy(re.begin(), re.end(), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
std::cout << (data == re) << std::endl;
std::cout << (decodedResult.encoding == bech32::Encoding::Bech32) << std::endl;

uint8_t pk[52];
convert_bits<8, 5>(re.begin(), re.end(),
[&, pos = 0U](unsigned char c) mutable
{
if (pos < 52)
pk[pos++] = c;
});
std::vector<uint8_t> re2(std::begin(pk), std::end(pk));
std::cout << (decodedResult.dp == re2) << std::endl;
std::string str = bech32::encodeUsingOriginalConstant(decodedResult.hrp, re2);
std::cout << str << std::endl;

char *message = "Sample Message";

std::cout << sha256(message, strlen(message)) << std::endl;

if (argc > 1)
std::cout << sign(argv[1]) << std::endl;

logger = new LoggerStdout();
RxNostr rx_nostr(logger);

Expand Down
41 changes: 36 additions & 5 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
project('cpp-nostr',
'cpp',
default_options : [
'warning_level=3',
'cpp_std=gnu++17'
])
default_options : {
'warning_level':'2',
'cpp_std':'gnu++20'
})

src = ['main.cpp']

cpp = meson.get_compiler('cpp')

pthread = dependency('threads')

openssl = dependency('openssl')

cmake = import('cmake')

yyjson_subproject = cmake.subproject(
Expand Down Expand Up @@ -43,12 +45,41 @@ libbech32_subproject = cmake.subproject(
)
libbech32_dep = libbech32_subproject.dependency('bech32')


fmt_subproject = cmake.subproject(
'fmt',
cmake_options: [
]
)
fmt_dep = fmt_subproject.dependency('fmt')

nameof_subproject = cmake.subproject(
'nameof',
cmake_options: [
'-DCMAKE_BUILD_TYPE=Release', '-DNAMEOF_OPT_INSTALL=ON'
]
)
nameof_dep = nameof_subproject.dependency('nameof')

validator_cmake_opts = cmake.subproject_options()
validator_cmake_opts.add_cmake_defines({'fmt_DIR': (meson.project_build_root() / 'subprojects' / 'fmt' / '__CMake_build')})
validator_cmake_opts.add_cmake_defines({'nameof_DIR': (meson.project_build_root() / 'subprojects' / 'nameof' / '__CMake_build')})
validator_cmake_opts.add_cmake_defines({'CMAKE_PREFIX_PATH': meson.project_build_root() / 'subprojects' / 'yyjson' / '__CMake_build'})
validator_cmake_opts.add_cmake_defines({'CPPYYJSON_BUILD_TEST':'OFF'})

cpp_yyjson_subproject = cmake.subproject(
'cpp-yyjson',
options: validator_cmake_opts
)
cpp_yyjson_dep = cpp_yyjson_subproject.dependency('cpp_yyjson')

rx_nostr_cpp_proj = subproject('rx-nostr-cpp')
rx_nostr_cpp_dep = rx_nostr_cpp_proj.get_variable('rx_nostr_cpp_dep')

executable('main', src, cpp_args:[
'-pedantic'
],link_args:[
'-fuse-ld=lld'
],dependencies:[
pthread,yyjson_dep,secp256k1_dep,libbech32_dep,libhv_dep,rx_nostr_cpp_dep
pthread,openssl,fmt_dep,nameof_dep,yyjson_dep,cpp_yyjson_dep,secp256k1_dep,libbech32_dep,libhv_dep,rx_nostr_cpp_dep
])
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#ifndef NOSTR_EVENT_HPP
#define NOSTR_EVENT_HPP
#ifndef CPP_NOSTR_EVENT_HPP
#define CPP_NOSTR_EVENT_HPP

#include <cstdint>
#include <string>
#include <vector>

namespace rx_nostr
namespace cpp_nostr
{
using NostrEventId = char*;
using NostrEventPubkey = char*;
using NostrEventId = std::string;
using NostrEventPubkey = std::string;
using NostrEventKind = int;
using NostrEventKinds = std::vector<int>;
using NostrEventTagItem = char*;
using NostrEventTags = std::vector<std::vector<char*>>;
using NostrEventSignature = char*;
using NostrEventContent = char*;
using NostrEventSubId = char*;
using NostrEventTagItem = std::string;
using NostrEventTags = std::vector<std::vector<std::string>>;
using NostrEventSignature = std::string;
using NostrEventContent = std::string;
using NostrEventSubId = std::string;
using NostrEventCreatedAt = uint64_t;

class NostrEvent final
Expand All @@ -28,10 +28,7 @@ class NostrEvent final
NostrEventContent content;
NostrEventSignature sig;
NostrEventCreatedAt created_at;
~NostrEvent()
{
}
};
} // namespace rx_nostr
}

#endif // NOSTR_EVENT_HPP
#endif
12 changes: 6 additions & 6 deletions src/nostr_event_sign_interface.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef NOSTR_EVENT_SIGN_INTERFACE_HPP
#define NOSTR_EVENT_SIGN_INTERFACE_HPP
#ifndef CPP_NOSTR_EVENT_SIGN_INTERFACE_HPP
#define CPP_NOSTR_EVENT_SIGN_INTERFACE_HPP

#include "nostr_event.hpp"
#include <string>

namespace rx_nostr
namespace cpp_nostr
{
class NostrEventSignInterface
{
Expand All @@ -13,8 +13,8 @@ class NostrEventSignInterface
{
}
virtual bool sign_event(const unsigned char *sk, NostrEvent &ev) const = 0;
virtual std::string encode(NostrEvent &ev) const = 0;
virtual const std::string encode(const NostrEvent &ev) const = 0;
};
} // namespace rx_nostr
}

#endif // NOSTR_EVENT_SIGN_INTERFACE_HPP
#endif
Loading

0 comments on commit 48d0018

Please sign in to comment.