nostrdb: port everything over to be in as sync as possible
for now
This commit is contained in:
committed by
Daniel D’Aquino
parent
954f48b23d
commit
1fb88a912a
168
nostrdb/Makefile
Normal file
168
nostrdb/Makefile
Normal file
@@ -0,0 +1,168 @@
|
||||
CFLAGS = -Wall -Wno-misleading-indentation -Wno-unused-function -Werror -O2 -g -Ideps/secp256k1/include -Ideps/lmdb -Ideps/flatcc/include
|
||||
HEADERS = sha256.h nostrdb.h cursor.h hex.h jsmn.h config.h sha256.h random.h memchr.h cpu.h $(C_BINDINGS)
|
||||
FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/verifier.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c
|
||||
BOLT11_SRCS = bolt11/bolt11.c bolt11/bech32.c bolt11/tal.c bolt11/talstr.c bolt11/take.c bolt11/list.c bolt11/utf8.c bolt11/amount.c bolt11/hash_u5.c
|
||||
SRCS = nostrdb.c sha256.c $(BOLT11_SRCS) $(FLATCC_SRCS)
|
||||
LDS = $(OBJS) $(ARS)
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
DEPS = $(OBJS) $(HEADERS) $(ARS)
|
||||
ARS = deps/lmdb/liblmdb.a deps/secp256k1/.libs/libsecp256k1.a
|
||||
LMDB_VER=0.9.31
|
||||
FLATCC_VER=05dc16dc2b0316e61063bb1fc75426647badce48
|
||||
PREFIX ?= /usr/local
|
||||
SUBMODULES = deps/secp256k1
|
||||
C_BINDINGS_PROFILE=bindings/c/profile_builder.h bindings/c/profile_reader.h bindings/c/profile_verifier.h bindings/c/profile_json_parser.h
|
||||
C_BINDINGS_META=bindings/c/meta_builder.h bindings/c/meta_reader.h bindings/c/meta_verifier.h bindings/c/meta_json_parser.h
|
||||
C_BINDINGS_COMMON=bindings/c/flatbuffers_common_builder.h bindings/c/flatbuffers_common_reader.h
|
||||
C_BINDINGS=$(C_BINDINGS_COMMON) $(C_BINDINGS_PROFILE) $(C_BINDINGS_META)
|
||||
BINDINGS=bindings
|
||||
BIN=ndb
|
||||
|
||||
CHECKDATA=testdata/db/v0/data.mdb
|
||||
|
||||
all: lib ndb
|
||||
|
||||
lib: benches test
|
||||
|
||||
ndb: ndb.c $(DEPS)
|
||||
$(CC) $(CFLAGS) ndb.c $(LDS) -o $@
|
||||
|
||||
bindings: bindings-swift bindings-rust bindings-c
|
||||
|
||||
check: test $(CHECKDATA)
|
||||
rm -rf testdata/db/*.mdb
|
||||
./test || rm -rf testdata/db/v0
|
||||
rm -rf testdata/db/v0
|
||||
|
||||
clean:
|
||||
rm -rf test bench bench-ingest bench-ingest-many
|
||||
|
||||
benches: bench
|
||||
|
||||
distclean: clean
|
||||
rm -rf deps
|
||||
|
||||
tags:
|
||||
find . -name '*.c' -or -name '*.h' | xargs ctags
|
||||
|
||||
configurator: configurator.c
|
||||
$(CC) $< -o $@
|
||||
|
||||
config.h: configurator
|
||||
./configurator > $@
|
||||
|
||||
bindings-c: $(C_BINDINGS)
|
||||
|
||||
bindings/%/.dir:
|
||||
mkdir -p $(shell dirname $@)
|
||||
touch $@
|
||||
|
||||
bindings/c/%_builder.h: schemas/%.fbs bindings/c/.dir
|
||||
flatcc --builder $< -o bindings/c
|
||||
|
||||
bindings/c/%_verifier.h bindings/c/%_reader.h: schemas/%.fbs bindings/c/.dir
|
||||
flatcc --verifier -o bindings/c $<
|
||||
|
||||
bindings/c/flatbuffers_common_reader.h: bindings/c/.dir
|
||||
flatcc --common_reader -o bindings/c
|
||||
|
||||
bindings/c/flatbuffers_common_builder.h: bindings/c/.dir
|
||||
flatcc --common_builder -o bindings/c
|
||||
|
||||
bindings/c/%_json_parser.h: schemas/%.fbs bindings/c/.dir
|
||||
flatcc --json-parser $< -o bindings/c
|
||||
|
||||
bindings-rust: bindings/rust/ndb_profile.rs bindings/rust/ndb_meta.rs
|
||||
|
||||
bindings/rust/ndb_profile.rs: schemas/profile.fbs bindings/rust
|
||||
flatc --gen-json-emit --rust $<
|
||||
@mv profile_generated.rs $@
|
||||
|
||||
bindings/rust/ndb_meta.rs: schemas/meta.fbs bindings/swift
|
||||
flatc --rust $<
|
||||
@mv meta_generated.rs $@
|
||||
|
||||
bindings-swift: bindings/swift/NdbProfile.swift bindings/swift/NdbMeta.swift
|
||||
|
||||
bindings/swift/NdbProfile.swift: schemas/profile.fbs bindings/swift
|
||||
flatc --gen-json-emit --swift $<
|
||||
@mv profile_generated.swift $@
|
||||
|
||||
bindings/swift/NdbMeta.swift: schemas/meta.fbs bindings/swift
|
||||
flatc --swift $<
|
||||
@mv meta_generated.swift $@
|
||||
|
||||
deps/.dir:
|
||||
@mkdir -p deps
|
||||
touch deps/.dir
|
||||
|
||||
deps/LMDB_$(LMDB_VER).tar.gz: deps/.dir
|
||||
curl -L https://github.com/LMDB/lmdb/archive/refs/tags/LMDB_$(LMDB_VER).tar.gz -o $@
|
||||
|
||||
deps/flatcc_$(FLATCC_VER).tar.gz: deps/.dir
|
||||
curl -L https://github.com/jb55/flatcc/archive/$(FLATCC_VER).tar.gz -o $@
|
||||
|
||||
deps/flatcc/src/runtime/json_parser.c: deps/flatcc_$(FLATCC_VER).tar.gz deps/.dir
|
||||
tar xf $<
|
||||
rm -rf deps/flatcc
|
||||
mv flatcc-$(FLATCC_VER) deps/flatcc
|
||||
touch $@
|
||||
|
||||
deps/lmdb/lmdb.h: deps/LMDB_$(LMDB_VER).tar.gz deps/.dir
|
||||
tar xf $<
|
||||
rm -rf deps/lmdb
|
||||
mv lmdb-LMDB_$(LMDB_VER)/libraries/liblmdb deps/lmdb
|
||||
rm -rf lmdb-LMDB_$(LMDB_VER)
|
||||
touch $@
|
||||
|
||||
deps/secp256k1/.git: deps/.dir
|
||||
@devtools/refresh-submodules.sh $(SUBMODULES)
|
||||
|
||||
deps/secp256k1/include/secp256k1.h: deps/secp256k1/.git
|
||||
|
||||
deps/secp256k1/configure: deps/secp256k1/.git
|
||||
cd deps/secp256k1; \
|
||||
./autogen.sh
|
||||
|
||||
deps/secp256k1/.libs/libsecp256k1.a: deps/secp256k1/config.log
|
||||
cd deps/secp256k1; \
|
||||
make -j libsecp256k1.la
|
||||
|
||||
deps/secp256k1/config.log: deps/secp256k1/configure
|
||||
cd deps/secp256k1; \
|
||||
./configure --disable-shared --enable-module-ecdh --enable-module-schnorrsig --enable-module-extrakeys
|
||||
|
||||
deps/lmdb/liblmdb.a: deps/lmdb/lmdb.h
|
||||
$(MAKE) -C deps/lmdb liblmdb.a
|
||||
|
||||
bench: bench.c $(DEPS)
|
||||
$(CC) $(CFLAGS) bench.c $(LDS) -o $@
|
||||
|
||||
testdata/db/ndb-v0.tar.zst:
|
||||
curl https://cdn.jb55.com/s/ndb-v0.tar.zst -o $@
|
||||
|
||||
testdata/db/ndb-v0.tar: testdata/db/ndb-v0.tar.zst
|
||||
zstd -d < $< > $@
|
||||
|
||||
testdata/db/v0/data.mdb: testdata/db/ndb-v0.tar
|
||||
tar xf $<
|
||||
rm -rf testdata/db/v0
|
||||
mv v0 testdata/db
|
||||
|
||||
testdata/many-events.json.zst:
|
||||
curl https://cdn.jb55.com/s/many-events.json.zst -o $@
|
||||
|
||||
testdata/many-events.json: testdata/many-events.json.zst
|
||||
zstd -d $<
|
||||
|
||||
bench-ingest-many: bench-ingest-many.c $(DEPS) testdata/many-events.json
|
||||
$(CC) $(CFLAGS) $< $(LDS) -o $@
|
||||
|
||||
testdata/db/.dir:
|
||||
@mkdir -p testdata/db
|
||||
touch testdata/db/.dir
|
||||
|
||||
test: test.c $(DEPS) testdata/db/.dir
|
||||
$(CC) $(CFLAGS) test.c $(LDS) -o $@
|
||||
|
||||
.PHONY: tags clean
|
||||
75
nostrdb/bench-ingest-many.c
Normal file
75
nostrdb/bench-ingest-many.c
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
|
||||
#include "io.h"
|
||||
#include "nostrdb.h"
|
||||
#include <sys/mman.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int map_file(const char *filename, unsigned char **p, size_t *flen)
|
||||
{
|
||||
struct stat st;
|
||||
int des;
|
||||
stat(filename, &st);
|
||||
*flen = st.st_size;
|
||||
|
||||
des = open(filename, O_RDONLY);
|
||||
|
||||
*p = mmap(NULL, *flen, PROT_READ, MAP_PRIVATE, des, 0);
|
||||
close(des);
|
||||
|
||||
return *p != MAP_FAILED;
|
||||
}
|
||||
|
||||
static int bench_parser()
|
||||
{
|
||||
long nanos, ms;
|
||||
size_t written;
|
||||
struct ndb *ndb;
|
||||
struct timespec t1, t2;
|
||||
char *json;
|
||||
int times = 1;
|
||||
struct ndb_config config;
|
||||
ndb_default_config(&config);
|
||||
|
||||
ndb_config_set_mapsize(&config, 1024ULL * 1024ULL * 400ULL * 10ULL);
|
||||
ndb_config_set_ingest_threads(&config, 8);
|
||||
ndb_config_set_flags(&config, NDB_FLAG_SKIP_NOTE_VERIFY);
|
||||
|
||||
assert(ndb_init(&ndb, "testdata/db", &config));
|
||||
const char *filename = "testdata/many-events.json";
|
||||
if (!map_file(filename, (unsigned char**)&json, &written)) {
|
||||
printf("mapping testdata/many-events.json failed\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
printf("mapped %ld bytes in %s\n", written, filename);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &t1);
|
||||
|
||||
ndb_process_events(ndb, json, written);
|
||||
|
||||
ndb_destroy(ndb);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &t2);
|
||||
|
||||
nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);
|
||||
ms = nanos / 1e6;
|
||||
printf("ns/run\t%ld\nms/run\t%f\nns\t%ld\nms\t%ld\n",
|
||||
nanos/times, (double)ms/(double)times, nanos, ms);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char **env)
|
||||
{
|
||||
if (!bench_parser())
|
||||
return 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
51
nostrdb/bench.c
Normal file
51
nostrdb/bench.c
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
#include "io.h"
|
||||
#include "nostrdb.h"
|
||||
#include <sys/mman.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int bench_parser(int times, const char *json, int len)
|
||||
{
|
||||
static unsigned char buf[2<<18];
|
||||
|
||||
struct timespec t1, t2;
|
||||
int i;
|
||||
long nanos, ms;
|
||||
struct ndb_note *note;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &t1);
|
||||
for (i = 0; i < times; i++) {
|
||||
if (!ndb_note_from_json(json, len, ¬e, buf, sizeof(buf))) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &t2);
|
||||
|
||||
nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec);
|
||||
ms = nanos / 1e6;
|
||||
printf("ns/run\t%ld\nms/run\t%f\nns\t%ld\nms\t%ld\n",
|
||||
nanos/times, (double)ms/(double)times, nanos, ms);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char **env)
|
||||
{
|
||||
static const int alloc_size = 2 << 18;
|
||||
int times = 10000, len = 0;
|
||||
unsigned char buf[alloc_size];
|
||||
|
||||
if (!read_file("testdata/contacts.json", buf, alloc_size, &len))
|
||||
return 1;
|
||||
|
||||
if (argc >= 2)
|
||||
times = atoi(argv[1]);
|
||||
|
||||
fprintf(stderr, "benching parser %d times\n", times);
|
||||
if (!bench_parser(times, (const char*)&buf[0], len))
|
||||
return 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
|
||||
/* Common FlatBuffers build functionality for C. */
|
||||
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
#ifndef FLATBUILDER_H
|
||||
#include "flatcc_builder.h"
|
||||
#include "flatcc/flatcc_builder.h"
|
||||
#endif
|
||||
typedef flatcc_builder_t flatbuffers_builder_t;
|
||||
typedef flatcc_builder_ref_t flatbuffers_ref_t;
|
||||
@@ -681,5 +681,5 @@ __flatbuffers_build_scalar(flatbuffers_, flatbuffers_double, double)
|
||||
__flatbuffers_build_string(flatbuffers_)
|
||||
|
||||
__flatbuffers_build_buffer(flatbuffers_)
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* FLATBUFFERS_COMMON_BUILDER_H */
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
/* Common FlatBuffers read functionality for C. */
|
||||
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc_flatbuffers.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_flatbuffers.h"
|
||||
|
||||
|
||||
#define __flatbuffers_read_scalar_at_byteoffset(N, p, o) N ## _read_from_pe((uint8_t *)(p) + (o))
|
||||
@@ -574,5 +574,5 @@ static inline N ## _ ## K ## t N ## _as_typed_root(const void *buffer__tmp)\
|
||||
#define __flatbuffers_struct_as_root(N) __flatbuffers_buffer_as_root(N, struct_)
|
||||
#define __flatbuffers_table_as_root(N) __flatbuffers_buffer_as_root(N, table_)
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* FLATBUFFERS_COMMON_H */
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#ifndef FLATBUFFERS_COMMON_BUILDER_H
|
||||
#include "flatbuffers_common_builder.h"
|
||||
#endif
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
#ifndef flatbuffers_identifier
|
||||
#define flatbuffers_identifier 0
|
||||
#endif
|
||||
@@ -65,5 +65,5 @@ static NdbEventMeta_ref_t NdbEventMeta_clone(flatbuffers_builder_t *B, NdbEventM
|
||||
__flatbuffers_memoize_end(B, t, NdbEventMeta_end(B));
|
||||
}
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* META_BUILDER_H */
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
/* Generated by flatcc 0.6.1 FlatBuffers schema compiler for C by dvide.com */
|
||||
|
||||
#include "flatcc_json_parser.h"
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_json_parser.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
|
||||
/*
|
||||
* Parses the default root table or struct of the schema and constructs a FlatBuffer.
|
||||
@@ -246,5 +246,5 @@ static int meta_parse_json(flatcc_builder_t *B, flatcc_json_parser_t *ctx,
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* META_JSON_PARSER_H */
|
||||
|
||||
@@ -6,11 +6,11 @@
|
||||
#ifndef FLATBUFFERS_COMMON_READER_H
|
||||
#include "flatbuffers_common_reader.h"
|
||||
#endif
|
||||
#include "flatcc_flatbuffers.h"
|
||||
#include "flatcc/flatcc_flatbuffers.h"
|
||||
#ifndef __alignas_is_defined
|
||||
#include <stdalign.h>
|
||||
#endif
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
#ifndef flatbuffers_identifier
|
||||
#define flatbuffers_identifier 0
|
||||
#endif
|
||||
@@ -54,5 +54,5 @@ __flatbuffers_define_scalar_field(4, NdbEventMeta, zaps, flatbuffers_int32, int3
|
||||
__flatbuffers_define_scalar_field(5, NdbEventMeta, zap_total, flatbuffers_int64, int64_t, INT64_C(0))
|
||||
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* META_READER_H */
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#ifndef META_READER_H
|
||||
#include "meta_reader.h"
|
||||
#endif
|
||||
#include "flatcc_verifier.h"
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_verifier.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
|
||||
static int NdbEventMeta_verify_table(flatcc_table_verifier_descriptor_t *td);
|
||||
|
||||
@@ -43,5 +43,5 @@ static inline int NdbEventMeta_verify_as_root_with_type_hash(const void *buf, si
|
||||
return flatcc_verify_table_as_typed_root(buf, bufsiz, thash, &NdbEventMeta_verify_table);
|
||||
}
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* META_VERIFIER_H */
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#ifndef FLATBUFFERS_COMMON_BUILDER_H
|
||||
#include "flatbuffers_common_builder.h"
|
||||
#endif
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
#ifndef flatbuffers_identifier
|
||||
#define flatbuffers_identifier 0
|
||||
#endif
|
||||
@@ -127,5 +127,5 @@ static NdbProfileRecord_ref_t NdbProfileRecord_clone(flatbuffers_builder_t *B, N
|
||||
__flatbuffers_memoize_end(B, t, NdbProfileRecord_end(B));
|
||||
}
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* PROFILE_BUILDER_H */
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
/* Generated by flatcc 0.6.1 FlatBuffers schema compiler for C by dvide.com */
|
||||
|
||||
#include "flatcc_json_parser.h"
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_json_parser.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
|
||||
/*
|
||||
* Parses the default root table or struct of the schema and constructs a FlatBuffer.
|
||||
@@ -408,5 +408,5 @@ static int profile_parse_json(flatcc_builder_t *B, flatcc_json_parser_t *ctx,
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* PROFILE_JSON_PARSER_H */
|
||||
|
||||
@@ -6,11 +6,11 @@
|
||||
#ifndef FLATBUFFERS_COMMON_READER_H
|
||||
#include "flatbuffers_common_reader.h"
|
||||
#endif
|
||||
#include "flatcc_flatbuffers.h"
|
||||
#include "flatcc/flatcc_flatbuffers.h"
|
||||
#ifndef __alignas_is_defined
|
||||
#include <stdalign.h>
|
||||
#endif
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
#ifndef flatbuffers_identifier
|
||||
#define flatbuffers_identifier 0
|
||||
#endif
|
||||
@@ -89,5 +89,5 @@ __flatbuffers_define_scalar_field(2, NdbProfileRecord, note_key, flatbuffers_uin
|
||||
__flatbuffers_define_string_field(3, NdbProfileRecord, lnurl, 0)
|
||||
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* PROFILE_READER_H */
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#ifndef PROFILE_READER_H
|
||||
#include "profile_reader.h"
|
||||
#endif
|
||||
#include "flatcc_verifier.h"
|
||||
#include "flatcc_prologue.h"
|
||||
#include "flatcc/flatcc_verifier.h"
|
||||
#include "flatcc/flatcc_prologue.h"
|
||||
|
||||
static int NdbProfile_verify_table(flatcc_table_verifier_descriptor_t *td);
|
||||
static int NdbProfileRecord_verify_table(flatcc_table_verifier_descriptor_t *td);
|
||||
@@ -80,5 +80,5 @@ static inline int NdbProfileRecord_verify_as_root_with_type_hash(const void *buf
|
||||
return flatcc_verify_table_as_typed_root(buf, bufsiz, thash, &NdbProfileRecord_verify_table);
|
||||
}
|
||||
|
||||
#include "flatcc_epilogue.h"
|
||||
#include "flatcc/flatcc_epilogue.h"
|
||||
#endif /* PROFILE_VERIFIER_H */
|
||||
|
||||
0
nostrdb/bindings/rust/.dir
Normal file
0
nostrdb/bindings/rust/.dir
Normal file
264
nostrdb/bindings/rust/ndb_meta.rs
Normal file
264
nostrdb/bindings/rust/ndb_meta.rs
Normal file
@@ -0,0 +1,264 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
|
||||
// @generated
|
||||
|
||||
use core::mem;
|
||||
use core::cmp::Ordering;
|
||||
|
||||
extern crate flatbuffers;
|
||||
use self::flatbuffers::{EndianScalar, Follow};
|
||||
|
||||
pub enum NdbEventMetaOffset {}
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
||||
pub struct NdbEventMeta<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for NdbEventMeta<'a> {
|
||||
type Inner = NdbEventMeta<'a>;
|
||||
#[inline]
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self { _tab: flatbuffers::Table::new(buf, loc) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> NdbEventMeta<'a> {
|
||||
pub const VT_RECEIVED_AT: flatbuffers::VOffsetT = 4;
|
||||
pub const VT_REACTIONS: flatbuffers::VOffsetT = 6;
|
||||
pub const VT_QUOTES: flatbuffers::VOffsetT = 8;
|
||||
pub const VT_REPOSTS: flatbuffers::VOffsetT = 10;
|
||||
pub const VT_ZAPS: flatbuffers::VOffsetT = 12;
|
||||
pub const VT_ZAP_TOTAL: flatbuffers::VOffsetT = 14;
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
NdbEventMeta { _tab: table }
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
|
||||
args: &'args NdbEventMetaArgs
|
||||
) -> flatbuffers::WIPOffset<NdbEventMeta<'bldr>> {
|
||||
let mut builder = NdbEventMetaBuilder::new(_fbb);
|
||||
builder.add_zap_total(args.zap_total);
|
||||
builder.add_zaps(args.zaps);
|
||||
builder.add_reposts(args.reposts);
|
||||
builder.add_quotes(args.quotes);
|
||||
builder.add_reactions(args.reactions);
|
||||
builder.add_received_at(args.received_at);
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn received_at(&self) -> i32 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(NdbEventMeta::VT_RECEIVED_AT, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn reactions(&self) -> i32 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(NdbEventMeta::VT_REACTIONS, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn quotes(&self) -> i32 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(NdbEventMeta::VT_QUOTES, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn reposts(&self) -> i32 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(NdbEventMeta::VT_REPOSTS, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn zaps(&self) -> i32 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(NdbEventMeta::VT_ZAPS, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn zap_total(&self) -> i64 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i64>(NdbEventMeta::VT_ZAP_TOTAL, Some(0)).unwrap()}
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::Verifiable for NdbEventMeta<'_> {
|
||||
#[inline]
|
||||
fn run_verifier(
|
||||
v: &mut flatbuffers::Verifier, pos: usize
|
||||
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
|
||||
use self::flatbuffers::Verifiable;
|
||||
v.visit_table(pos)?
|
||||
.visit_field::<i32>("received_at", Self::VT_RECEIVED_AT, false)?
|
||||
.visit_field::<i32>("reactions", Self::VT_REACTIONS, false)?
|
||||
.visit_field::<i32>("quotes", Self::VT_QUOTES, false)?
|
||||
.visit_field::<i32>("reposts", Self::VT_REPOSTS, false)?
|
||||
.visit_field::<i32>("zaps", Self::VT_ZAPS, false)?
|
||||
.visit_field::<i64>("zap_total", Self::VT_ZAP_TOTAL, false)?
|
||||
.finish();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub struct NdbEventMetaArgs {
|
||||
pub received_at: i32,
|
||||
pub reactions: i32,
|
||||
pub quotes: i32,
|
||||
pub reposts: i32,
|
||||
pub zaps: i32,
|
||||
pub zap_total: i64,
|
||||
}
|
||||
impl<'a> Default for NdbEventMetaArgs {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
NdbEventMetaArgs {
|
||||
received_at: 0,
|
||||
reactions: 0,
|
||||
quotes: 0,
|
||||
reposts: 0,
|
||||
zaps: 0,
|
||||
zap_total: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NdbEventMetaBuilder<'a: 'b, 'b> {
|
||||
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
|
||||
}
|
||||
impl<'a: 'b, 'b> NdbEventMetaBuilder<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn add_received_at(&mut self, received_at: i32) {
|
||||
self.fbb_.push_slot::<i32>(NdbEventMeta::VT_RECEIVED_AT, received_at, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_reactions(&mut self, reactions: i32) {
|
||||
self.fbb_.push_slot::<i32>(NdbEventMeta::VT_REACTIONS, reactions, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_quotes(&mut self, quotes: i32) {
|
||||
self.fbb_.push_slot::<i32>(NdbEventMeta::VT_QUOTES, quotes, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_reposts(&mut self, reposts: i32) {
|
||||
self.fbb_.push_slot::<i32>(NdbEventMeta::VT_REPOSTS, reposts, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_zaps(&mut self, zaps: i32) {
|
||||
self.fbb_.push_slot::<i32>(NdbEventMeta::VT_ZAPS, zaps, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_zap_total(&mut self, zap_total: i64) {
|
||||
self.fbb_.push_slot::<i64>(NdbEventMeta::VT_ZAP_TOTAL, zap_total, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> NdbEventMetaBuilder<'a, 'b> {
|
||||
let start = _fbb.start_table();
|
||||
NdbEventMetaBuilder {
|
||||
fbb_: _fbb,
|
||||
start_: start,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish(self) -> flatbuffers::WIPOffset<NdbEventMeta<'a>> {
|
||||
let o = self.fbb_.end_table(self.start_);
|
||||
flatbuffers::WIPOffset::new(o.value())
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for NdbEventMeta<'_> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let mut ds = f.debug_struct("NdbEventMeta");
|
||||
ds.field("received_at", &self.received_at());
|
||||
ds.field("reactions", &self.reactions());
|
||||
ds.field("quotes", &self.quotes());
|
||||
ds.field("reposts", &self.reposts());
|
||||
ds.field("zaps", &self.zaps());
|
||||
ds.field("zap_total", &self.zap_total());
|
||||
ds.finish()
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies that a buffer of bytes contains a `NdbEventMeta`
|
||||
/// and returns it.
|
||||
/// Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `root_as_ndb_event_meta_unchecked`.
|
||||
pub fn root_as_ndb_event_meta(buf: &[u8]) -> Result<NdbEventMeta, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::root::<NdbEventMeta>(buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies that a buffer of bytes contains a size prefixed
|
||||
/// `NdbEventMeta` and returns it.
|
||||
/// Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `size_prefixed_root_as_ndb_event_meta_unchecked`.
|
||||
pub fn size_prefixed_root_as_ndb_event_meta(buf: &[u8]) -> Result<NdbEventMeta, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::size_prefixed_root::<NdbEventMeta>(buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies, with the given options, that a buffer of bytes
|
||||
/// contains a `NdbEventMeta` and returns it.
|
||||
/// Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `root_as_ndb_event_meta_unchecked`.
|
||||
pub fn root_as_ndb_event_meta_with_opts<'b, 'o>(
|
||||
opts: &'o flatbuffers::VerifierOptions,
|
||||
buf: &'b [u8],
|
||||
) -> Result<NdbEventMeta<'b>, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::root_with_opts::<NdbEventMeta<'b>>(opts, buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies, with the given verifier options, that a buffer of
|
||||
/// bytes contains a size prefixed `NdbEventMeta` and returns
|
||||
/// it. Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `root_as_ndb_event_meta_unchecked`.
|
||||
pub fn size_prefixed_root_as_ndb_event_meta_with_opts<'b, 'o>(
|
||||
opts: &'o flatbuffers::VerifierOptions,
|
||||
buf: &'b [u8],
|
||||
) -> Result<NdbEventMeta<'b>, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::size_prefixed_root_with_opts::<NdbEventMeta<'b>>(opts, buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Assumes, without verification, that a buffer of bytes contains a NdbEventMeta and returns it.
|
||||
/// # Safety
|
||||
/// Callers must trust the given bytes do indeed contain a valid `NdbEventMeta`.
|
||||
pub unsafe fn root_as_ndb_event_meta_unchecked(buf: &[u8]) -> NdbEventMeta {
|
||||
flatbuffers::root_unchecked::<NdbEventMeta>(buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Assumes, without verification, that a buffer of bytes contains a size prefixed NdbEventMeta and returns it.
|
||||
/// # Safety
|
||||
/// Callers must trust the given bytes do indeed contain a valid size prefixed `NdbEventMeta`.
|
||||
pub unsafe fn size_prefixed_root_as_ndb_event_meta_unchecked(buf: &[u8]) -> NdbEventMeta {
|
||||
flatbuffers::size_prefixed_root_unchecked::<NdbEventMeta>(buf)
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish_ndb_event_meta_buffer<'a, 'b>(
|
||||
fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
root: flatbuffers::WIPOffset<NdbEventMeta<'a>>) {
|
||||
fbb.finish(root, None);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn finish_size_prefixed_ndb_event_meta_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset<NdbEventMeta<'a>>) {
|
||||
fbb.finish_size_prefixed(root, None);
|
||||
}
|
||||
514
nostrdb/bindings/rust/ndb_profile.rs
Normal file
514
nostrdb/bindings/rust/ndb_profile.rs
Normal file
@@ -0,0 +1,514 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
|
||||
// @generated
|
||||
|
||||
use core::mem;
|
||||
use core::cmp::Ordering;
|
||||
|
||||
extern crate flatbuffers;
|
||||
use self::flatbuffers::{EndianScalar, Follow};
|
||||
|
||||
pub enum NdbProfileOffset {}
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
||||
pub struct NdbProfile<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for NdbProfile<'a> {
|
||||
type Inner = NdbProfile<'a>;
|
||||
#[inline]
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self { _tab: flatbuffers::Table::new(buf, loc) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> NdbProfile<'a> {
|
||||
pub const VT_NAME: flatbuffers::VOffsetT = 4;
|
||||
pub const VT_WEBSITE: flatbuffers::VOffsetT = 6;
|
||||
pub const VT_ABOUT: flatbuffers::VOffsetT = 8;
|
||||
pub const VT_LUD16: flatbuffers::VOffsetT = 10;
|
||||
pub const VT_BANNER: flatbuffers::VOffsetT = 12;
|
||||
pub const VT_DISPLAY_NAME: flatbuffers::VOffsetT = 14;
|
||||
pub const VT_REACTIONS: flatbuffers::VOffsetT = 16;
|
||||
pub const VT_PICTURE: flatbuffers::VOffsetT = 18;
|
||||
pub const VT_NIP05: flatbuffers::VOffsetT = 20;
|
||||
pub const VT_DAMUS_DONATION: flatbuffers::VOffsetT = 22;
|
||||
pub const VT_DAMUS_DONATION_V2: flatbuffers::VOffsetT = 24;
|
||||
pub const VT_LUD06: flatbuffers::VOffsetT = 26;
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
NdbProfile { _tab: table }
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
|
||||
args: &'args NdbProfileArgs<'args>
|
||||
) -> flatbuffers::WIPOffset<NdbProfile<'bldr>> {
|
||||
let mut builder = NdbProfileBuilder::new(_fbb);
|
||||
if let Some(x) = args.lud06 { builder.add_lud06(x); }
|
||||
builder.add_damus_donation_v2(args.damus_donation_v2);
|
||||
builder.add_damus_donation(args.damus_donation);
|
||||
if let Some(x) = args.nip05 { builder.add_nip05(x); }
|
||||
if let Some(x) = args.picture { builder.add_picture(x); }
|
||||
if let Some(x) = args.display_name { builder.add_display_name(x); }
|
||||
if let Some(x) = args.banner { builder.add_banner(x); }
|
||||
if let Some(x) = args.lud16 { builder.add_lud16(x); }
|
||||
if let Some(x) = args.about { builder.add_about(x); }
|
||||
if let Some(x) = args.website { builder.add_website(x); }
|
||||
if let Some(x) = args.name { builder.add_name(x); }
|
||||
builder.add_reactions(args.reactions);
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn name(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_NAME, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn website(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_WEBSITE, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn about(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_ABOUT, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn lud16(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_LUD16, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn banner(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_BANNER, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn display_name(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_DISPLAY_NAME, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn reactions(&self) -> bool {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<bool>(NdbProfile::VT_REACTIONS, Some(true)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn picture(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_PICTURE, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn nip05(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_NIP05, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn damus_donation(&self) -> i32 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(NdbProfile::VT_DAMUS_DONATION, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn damus_donation_v2(&self) -> i32 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(NdbProfile::VT_DAMUS_DONATION_V2, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn lud06(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfile::VT_LUD06, None)}
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::Verifiable for NdbProfile<'_> {
|
||||
#[inline]
|
||||
fn run_verifier(
|
||||
v: &mut flatbuffers::Verifier, pos: usize
|
||||
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
|
||||
use self::flatbuffers::Verifiable;
|
||||
v.visit_table(pos)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("name", Self::VT_NAME, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("website", Self::VT_WEBSITE, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("about", Self::VT_ABOUT, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("lud16", Self::VT_LUD16, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("banner", Self::VT_BANNER, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("display_name", Self::VT_DISPLAY_NAME, false)?
|
||||
.visit_field::<bool>("reactions", Self::VT_REACTIONS, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("picture", Self::VT_PICTURE, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("nip05", Self::VT_NIP05, false)?
|
||||
.visit_field::<i32>("damus_donation", Self::VT_DAMUS_DONATION, false)?
|
||||
.visit_field::<i32>("damus_donation_v2", Self::VT_DAMUS_DONATION_V2, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("lud06", Self::VT_LUD06, false)?
|
||||
.finish();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub struct NdbProfileArgs<'a> {
|
||||
pub name: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub website: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub about: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub lud16: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub banner: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub display_name: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub reactions: bool,
|
||||
pub picture: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub nip05: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub damus_donation: i32,
|
||||
pub damus_donation_v2: i32,
|
||||
pub lud06: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
}
|
||||
impl<'a> Default for NdbProfileArgs<'a> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
NdbProfileArgs {
|
||||
name: None,
|
||||
website: None,
|
||||
about: None,
|
||||
lud16: None,
|
||||
banner: None,
|
||||
display_name: None,
|
||||
reactions: true,
|
||||
picture: None,
|
||||
nip05: None,
|
||||
damus_donation: 0,
|
||||
damus_donation_v2: 0,
|
||||
lud06: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NdbProfileBuilder<'a: 'b, 'b> {
|
||||
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
|
||||
}
|
||||
impl<'a: 'b, 'b> NdbProfileBuilder<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn add_name(&mut self, name: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_NAME, name);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_website(&mut self, website: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_WEBSITE, website);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_about(&mut self, about: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_ABOUT, about);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_lud16(&mut self, lud16: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_LUD16, lud16);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_banner(&mut self, banner: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_BANNER, banner);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_display_name(&mut self, display_name: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_DISPLAY_NAME, display_name);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_reactions(&mut self, reactions: bool) {
|
||||
self.fbb_.push_slot::<bool>(NdbProfile::VT_REACTIONS, reactions, true);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_picture(&mut self, picture: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_PICTURE, picture);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_nip05(&mut self, nip05: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_NIP05, nip05);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_damus_donation(&mut self, damus_donation: i32) {
|
||||
self.fbb_.push_slot::<i32>(NdbProfile::VT_DAMUS_DONATION, damus_donation, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_damus_donation_v2(&mut self, damus_donation_v2: i32) {
|
||||
self.fbb_.push_slot::<i32>(NdbProfile::VT_DAMUS_DONATION_V2, damus_donation_v2, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_lud06(&mut self, lud06: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfile::VT_LUD06, lud06);
|
||||
}
|
||||
#[inline]
|
||||
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> NdbProfileBuilder<'a, 'b> {
|
||||
let start = _fbb.start_table();
|
||||
NdbProfileBuilder {
|
||||
fbb_: _fbb,
|
||||
start_: start,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish(self) -> flatbuffers::WIPOffset<NdbProfile<'a>> {
|
||||
let o = self.fbb_.end_table(self.start_);
|
||||
flatbuffers::WIPOffset::new(o.value())
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for NdbProfile<'_> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let mut ds = f.debug_struct("NdbProfile");
|
||||
ds.field("name", &self.name());
|
||||
ds.field("website", &self.website());
|
||||
ds.field("about", &self.about());
|
||||
ds.field("lud16", &self.lud16());
|
||||
ds.field("banner", &self.banner());
|
||||
ds.field("display_name", &self.display_name());
|
||||
ds.field("reactions", &self.reactions());
|
||||
ds.field("picture", &self.picture());
|
||||
ds.field("nip05", &self.nip05());
|
||||
ds.field("damus_donation", &self.damus_donation());
|
||||
ds.field("damus_donation_v2", &self.damus_donation_v2());
|
||||
ds.field("lud06", &self.lud06());
|
||||
ds.finish()
|
||||
}
|
||||
}
|
||||
pub enum NdbProfileRecordOffset {}
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
||||
pub struct NdbProfileRecord<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for NdbProfileRecord<'a> {
|
||||
type Inner = NdbProfileRecord<'a>;
|
||||
#[inline]
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self { _tab: flatbuffers::Table::new(buf, loc) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> NdbProfileRecord<'a> {
|
||||
pub const VT_PROFILE: flatbuffers::VOffsetT = 4;
|
||||
pub const VT_RECEIVED_AT: flatbuffers::VOffsetT = 6;
|
||||
pub const VT_NOTE_KEY: flatbuffers::VOffsetT = 8;
|
||||
pub const VT_LNURL: flatbuffers::VOffsetT = 10;
|
||||
|
||||
#[inline]
|
||||
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
NdbProfileRecord { _tab: table }
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
|
||||
args: &'args NdbProfileRecordArgs<'args>
|
||||
) -> flatbuffers::WIPOffset<NdbProfileRecord<'bldr>> {
|
||||
let mut builder = NdbProfileRecordBuilder::new(_fbb);
|
||||
builder.add_note_key(args.note_key);
|
||||
builder.add_received_at(args.received_at);
|
||||
if let Some(x) = args.lnurl { builder.add_lnurl(x); }
|
||||
if let Some(x) = args.profile { builder.add_profile(x); }
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn profile(&self) -> Option<NdbProfile<'a>> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<NdbProfile>>(NdbProfileRecord::VT_PROFILE, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn received_at(&self) -> u64 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u64>(NdbProfileRecord::VT_RECEIVED_AT, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn note_key(&self) -> u64 {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u64>(NdbProfileRecord::VT_NOTE_KEY, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn lnurl(&self) -> Option<&'a str> {
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(NdbProfileRecord::VT_LNURL, None)}
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::Verifiable for NdbProfileRecord<'_> {
|
||||
#[inline]
|
||||
fn run_verifier(
|
||||
v: &mut flatbuffers::Verifier, pos: usize
|
||||
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
|
||||
use self::flatbuffers::Verifiable;
|
||||
v.visit_table(pos)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<NdbProfile>>("profile", Self::VT_PROFILE, false)?
|
||||
.visit_field::<u64>("received_at", Self::VT_RECEIVED_AT, false)?
|
||||
.visit_field::<u64>("note_key", Self::VT_NOTE_KEY, false)?
|
||||
.visit_field::<flatbuffers::ForwardsUOffset<&str>>("lnurl", Self::VT_LNURL, false)?
|
||||
.finish();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub struct NdbProfileRecordArgs<'a> {
|
||||
pub profile: Option<flatbuffers::WIPOffset<NdbProfile<'a>>>,
|
||||
pub received_at: u64,
|
||||
pub note_key: u64,
|
||||
pub lnurl: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
}
|
||||
impl<'a> Default for NdbProfileRecordArgs<'a> {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
NdbProfileRecordArgs {
|
||||
profile: None,
|
||||
received_at: 0,
|
||||
note_key: 0,
|
||||
lnurl: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NdbProfileRecordBuilder<'a: 'b, 'b> {
|
||||
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
|
||||
}
|
||||
impl<'a: 'b, 'b> NdbProfileRecordBuilder<'a, 'b> {
|
||||
#[inline]
|
||||
pub fn add_profile(&mut self, profile: flatbuffers::WIPOffset<NdbProfile<'b >>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<NdbProfile>>(NdbProfileRecord::VT_PROFILE, profile);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_received_at(&mut self, received_at: u64) {
|
||||
self.fbb_.push_slot::<u64>(NdbProfileRecord::VT_RECEIVED_AT, received_at, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_note_key(&mut self, note_key: u64) {
|
||||
self.fbb_.push_slot::<u64>(NdbProfileRecord::VT_NOTE_KEY, note_key, 0);
|
||||
}
|
||||
#[inline]
|
||||
pub fn add_lnurl(&mut self, lnurl: flatbuffers::WIPOffset<&'b str>) {
|
||||
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(NdbProfileRecord::VT_LNURL, lnurl);
|
||||
}
|
||||
#[inline]
|
||||
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> NdbProfileRecordBuilder<'a, 'b> {
|
||||
let start = _fbb.start_table();
|
||||
NdbProfileRecordBuilder {
|
||||
fbb_: _fbb,
|
||||
start_: start,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish(self) -> flatbuffers::WIPOffset<NdbProfileRecord<'a>> {
|
||||
let o = self.fbb_.end_table(self.start_);
|
||||
flatbuffers::WIPOffset::new(o.value())
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for NdbProfileRecord<'_> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let mut ds = f.debug_struct("NdbProfileRecord");
|
||||
ds.field("profile", &self.profile());
|
||||
ds.field("received_at", &self.received_at());
|
||||
ds.field("note_key", &self.note_key());
|
||||
ds.field("lnurl", &self.lnurl());
|
||||
ds.finish()
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies that a buffer of bytes contains a `NdbProfileRecord`
|
||||
/// and returns it.
|
||||
/// Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `root_as_ndb_profile_record_unchecked`.
|
||||
pub fn root_as_ndb_profile_record(buf: &[u8]) -> Result<NdbProfileRecord, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::root::<NdbProfileRecord>(buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies that a buffer of bytes contains a size prefixed
|
||||
/// `NdbProfileRecord` and returns it.
|
||||
/// Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `size_prefixed_root_as_ndb_profile_record_unchecked`.
|
||||
pub fn size_prefixed_root_as_ndb_profile_record(buf: &[u8]) -> Result<NdbProfileRecord, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::size_prefixed_root::<NdbProfileRecord>(buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies, with the given options, that a buffer of bytes
|
||||
/// contains a `NdbProfileRecord` and returns it.
|
||||
/// Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `root_as_ndb_profile_record_unchecked`.
|
||||
pub fn root_as_ndb_profile_record_with_opts<'b, 'o>(
|
||||
opts: &'o flatbuffers::VerifierOptions,
|
||||
buf: &'b [u8],
|
||||
) -> Result<NdbProfileRecord<'b>, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::root_with_opts::<NdbProfileRecord<'b>>(opts, buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Verifies, with the given verifier options, that a buffer of
|
||||
/// bytes contains a size prefixed `NdbProfileRecord` and returns
|
||||
/// it. Note that verification is still experimental and may not
|
||||
/// catch every error, or be maximally performant. For the
|
||||
/// previous, unchecked, behavior use
|
||||
/// `root_as_ndb_profile_record_unchecked`.
|
||||
pub fn size_prefixed_root_as_ndb_profile_record_with_opts<'b, 'o>(
|
||||
opts: &'o flatbuffers::VerifierOptions,
|
||||
buf: &'b [u8],
|
||||
) -> Result<NdbProfileRecord<'b>, flatbuffers::InvalidFlatbuffer> {
|
||||
flatbuffers::size_prefixed_root_with_opts::<NdbProfileRecord<'b>>(opts, buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Assumes, without verification, that a buffer of bytes contains a NdbProfileRecord and returns it.
|
||||
/// # Safety
|
||||
/// Callers must trust the given bytes do indeed contain a valid `NdbProfileRecord`.
|
||||
pub unsafe fn root_as_ndb_profile_record_unchecked(buf: &[u8]) -> NdbProfileRecord {
|
||||
flatbuffers::root_unchecked::<NdbProfileRecord>(buf)
|
||||
}
|
||||
#[inline]
|
||||
/// Assumes, without verification, that a buffer of bytes contains a size prefixed NdbProfileRecord and returns it.
|
||||
/// # Safety
|
||||
/// Callers must trust the given bytes do indeed contain a valid size prefixed `NdbProfileRecord`.
|
||||
pub unsafe fn size_prefixed_root_as_ndb_profile_record_unchecked(buf: &[u8]) -> NdbProfileRecord {
|
||||
flatbuffers::size_prefixed_root_unchecked::<NdbProfileRecord>(buf)
|
||||
}
|
||||
#[inline]
|
||||
pub fn finish_ndb_profile_record_buffer<'a, 'b>(
|
||||
fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>,
|
||||
root: flatbuffers::WIPOffset<NdbProfileRecord<'a>>) {
|
||||
fbb.finish(root, None);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn finish_size_prefixed_ndb_profile_record_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset<NdbProfileRecord<'a>>) {
|
||||
fbb.finish_size_prefixed(root, None);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// swiftlint:disable all
|
||||
// swiftformat:disable all
|
||||
|
||||
|
||||
import FlatBuffers
|
||||
|
||||
public struct NdbProfile: FlatBufferObject, Verifiable {
|
||||
|
||||
|
||||
217
nostrdb/bolt11/bech32.c
Normal file
217
nostrdb/bolt11/bech32.c
Normal file
@@ -0,0 +1,217 @@
|
||||
/* Stolen from https://github.com/sipa/bech32/blob/master/ref/c/segwit_addr.c,
|
||||
* with only the two ' > 90' checks hoisted, and more internals exposed */
|
||||
|
||||
/* Copyright (c) 2017, 2021 Pieter Wuille
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
#include "../config.h"
|
||||
#include <assert.h>
|
||||
#include "bech32.h"
|
||||
#include <string.h>
|
||||
|
||||
static uint32_t bech32_polymod_step(uint32_t pre) {
|
||||
uint8_t b = pre >> 25;
|
||||
return ((pre & 0x1FFFFFF) << 5) ^
|
||||
(-((b >> 0) & 1) & 0x3b6a57b2UL) ^
|
||||
(-((b >> 1) & 1) & 0x26508e6dUL) ^
|
||||
(-((b >> 2) & 1) & 0x1ea119faUL) ^
|
||||
(-((b >> 3) & 1) & 0x3d4233ddUL) ^
|
||||
(-((b >> 4) & 1) & 0x2a1462b3UL);
|
||||
}
|
||||
|
||||
static uint32_t bech32_final_constant(bech32_encoding enc) {
|
||||
if (enc == BECH32_ENCODING_BECH32) return 1;
|
||||
if (enc == BECH32_ENCODING_BECH32M) return 0x2bc830a3;
|
||||
assert(0);
|
||||
}
|
||||
|
||||
const char bech32_charset[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
|
||||
|
||||
const int8_t bech32_charset_rev[128] = {
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
|
||||
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
|
||||
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
|
||||
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
|
||||
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
|
||||
};
|
||||
|
||||
int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, size_t max_input_len, bech32_encoding enc) {
|
||||
uint32_t chk = 1;
|
||||
size_t i = 0;
|
||||
while (hrp[i] != 0) {
|
||||
int ch = hrp[i];
|
||||
if (ch < 33 || ch > 126) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ch >= 'A' && ch <= 'Z') return 0;
|
||||
chk = bech32_polymod_step(chk) ^ (ch >> 5);
|
||||
++i;
|
||||
}
|
||||
if (i + 7 + data_len > max_input_len) return 0;
|
||||
chk = bech32_polymod_step(chk);
|
||||
while (*hrp != 0) {
|
||||
chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f);
|
||||
*(output++) = *(hrp++);
|
||||
}
|
||||
*(output++) = '1';
|
||||
for (i = 0; i < data_len; ++i) {
|
||||
if (*data >> 5) return 0;
|
||||
chk = bech32_polymod_step(chk) ^ (*data);
|
||||
*(output++) = bech32_charset[*(data++)];
|
||||
}
|
||||
for (i = 0; i < 6; ++i) {
|
||||
chk = bech32_polymod_step(chk);
|
||||
}
|
||||
chk ^= bech32_final_constant(enc);
|
||||
for (i = 0; i < 6; ++i) {
|
||||
*(output++) = bech32_charset[(chk >> ((5 - i) * 5)) & 0x1f];
|
||||
}
|
||||
*output = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bech32_encoding bech32_decode_len(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t input_len) {
|
||||
uint32_t chk = 1;
|
||||
size_t i;
|
||||
size_t hrp_len;
|
||||
int have_lower = 0, have_upper = 0;
|
||||
if (input_len < 8) {
|
||||
return BECH32_ENCODING_NONE;
|
||||
}
|
||||
*data_len = 0;
|
||||
while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') {
|
||||
++(*data_len);
|
||||
}
|
||||
hrp_len = input_len - (1 + *data_len);
|
||||
if (1 + *data_len >= input_len || *data_len < 6) {
|
||||
return BECH32_ENCODING_NONE;
|
||||
}
|
||||
*(data_len) -= 6;
|
||||
for (i = 0; i < hrp_len; ++i) {
|
||||
int ch = input[i];
|
||||
if (ch < 33 || ch > 126) {
|
||||
return BECH32_ENCODING_NONE;
|
||||
}
|
||||
if (ch >= 'a' && ch <= 'z') {
|
||||
have_lower = 1;
|
||||
} else if (ch >= 'A' && ch <= 'Z') {
|
||||
have_upper = 1;
|
||||
ch = (ch - 'A') + 'a';
|
||||
}
|
||||
hrp[i] = ch;
|
||||
chk = bech32_polymod_step(chk) ^ (ch >> 5);
|
||||
}
|
||||
hrp[i] = 0;
|
||||
chk = bech32_polymod_step(chk);
|
||||
for (i = 0; i < hrp_len; ++i) {
|
||||
chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f);
|
||||
}
|
||||
++i;
|
||||
while (i < input_len) {
|
||||
int v = (input[i] & 0x80) ? -1 : bech32_charset_rev[(int)input[i]];
|
||||
if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1;
|
||||
if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1;
|
||||
if (v == -1) {
|
||||
return BECH32_ENCODING_NONE;
|
||||
}
|
||||
chk = bech32_polymod_step(chk) ^ v;
|
||||
if (i + 6 < input_len) {
|
||||
data[i - (1 + hrp_len)] = v;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
if (have_lower && have_upper) {
|
||||
return BECH32_ENCODING_NONE;
|
||||
}
|
||||
if (chk == bech32_final_constant(BECH32_ENCODING_BECH32)) {
|
||||
return BECH32_ENCODING_BECH32;
|
||||
} else if (chk == bech32_final_constant(BECH32_ENCODING_BECH32M)) {
|
||||
return BECH32_ENCODING_BECH32M;
|
||||
} else {
|
||||
return BECH32_ENCODING_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
bech32_encoding bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t max_input_len) {
|
||||
size_t len = strlen(input);
|
||||
if (len > max_input_len) {
|
||||
return BECH32_ENCODING_NONE;
|
||||
}
|
||||
return bech32_decode_len(hrp, data, data_len, input, len);
|
||||
}
|
||||
|
||||
int bech32_convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) {
|
||||
uint32_t val = 0;
|
||||
int bits = 0;
|
||||
uint32_t maxv = (((uint32_t)1) << outbits) - 1;
|
||||
while (inlen--) {
|
||||
val = (val << inbits) | *(in++);
|
||||
bits += inbits;
|
||||
while (bits >= outbits) {
|
||||
bits -= outbits;
|
||||
out[(*outlen)++] = (val >> bits) & maxv;
|
||||
}
|
||||
}
|
||||
if (pad) {
|
||||
if (bits) {
|
||||
out[(*outlen)++] = (val << (outbits - bits)) & maxv;
|
||||
}
|
||||
} else if (((val << (outbits - bits)) & maxv) || bits >= inbits) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) {
|
||||
uint8_t data[65];
|
||||
size_t datalen = 0;
|
||||
bech32_encoding enc = BECH32_ENCODING_BECH32;
|
||||
if (witver > 16) return 0;
|
||||
if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0;
|
||||
if (witprog_len < 2 || witprog_len > 40) return 0;
|
||||
if (witver > 0) enc = BECH32_ENCODING_BECH32M;
|
||||
data[0] = witver;
|
||||
bech32_convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1);
|
||||
++datalen;
|
||||
return bech32_encode(output, hrp, data, datalen, 90, enc);
|
||||
}
|
||||
|
||||
int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) {
|
||||
uint8_t data[84];
|
||||
char hrp_actual[84];
|
||||
size_t data_len;
|
||||
bech32_encoding enc = bech32_decode(hrp_actual, data, &data_len, addr, 90);
|
||||
if (enc == BECH32_ENCODING_NONE) return 0;
|
||||
if (data_len == 0 || data_len > 65) return 0;
|
||||
if (strncmp(hrp, hrp_actual, 84) != 0) return 0;
|
||||
if (data[0] > 16) return 0;
|
||||
if (data[0] == 0 && enc != BECH32_ENCODING_BECH32) return 0;
|
||||
if (data[0] > 0 && enc != BECH32_ENCODING_BECH32M) return 0;
|
||||
*witdata_len = 0;
|
||||
if (!bech32_convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0;
|
||||
if (*witdata_len < 2 || *witdata_len > 40) return 0;
|
||||
if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0;
|
||||
*witver = data[0];
|
||||
return 1;
|
||||
}
|
||||
142
nostrdb/bolt11/bech32.h
Normal file
142
nostrdb/bolt11/bech32.h
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Stolen from https://github.com/sipa/bech32/blob/master/ref/c/segwit_addr.h,
|
||||
* with only the two ' > 90' checks hoisted */
|
||||
|
||||
/* Copyright (c) 2017, 2021 Pieter Wuille
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LIGHTNING_COMMON_BECH32_H
|
||||
#define LIGHTNING_COMMON_BECH32_H
|
||||
#include "../config.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** Encode a SegWit address
|
||||
*
|
||||
* Out: output: Pointer to a buffer of size 73 + strlen(hrp) that will be
|
||||
* updated to contain the null-terminated address.
|
||||
* In: hrp: Pointer to the null-terminated human readable part to use
|
||||
* (chain/network specific).
|
||||
* ver: Version of the witness program (between 0 and 16 inclusive).
|
||||
* prog: Data bytes for the witness program (between 2 and 40 bytes).
|
||||
* prog_len: Number of data bytes in prog.
|
||||
* Returns 1 if successful.
|
||||
*/
|
||||
int segwit_addr_encode(
|
||||
char *output,
|
||||
const char *hrp,
|
||||
int ver,
|
||||
const uint8_t *prog,
|
||||
size_t prog_len
|
||||
);
|
||||
|
||||
/** Decode a SegWit address
|
||||
*
|
||||
* Out: ver: Pointer to an int that will be updated to contain the witness
|
||||
* program version (between 0 and 16 inclusive).
|
||||
* prog: Pointer to a buffer of size 40 that will be updated to
|
||||
* contain the witness program bytes.
|
||||
* prog_len: Pointer to a size_t that will be updated to contain the length
|
||||
* of bytes in prog.
|
||||
* hrp: Pointer to the null-terminated human readable part that is
|
||||
* expected (chain/network specific).
|
||||
* addr: Pointer to the null-terminated address.
|
||||
* Returns 1 if successful.
|
||||
*/
|
||||
int segwit_addr_decode(
|
||||
int* ver,
|
||||
uint8_t* prog,
|
||||
size_t* prog_len,
|
||||
const char* hrp,
|
||||
const char* addr
|
||||
);
|
||||
|
||||
/** Supported encodings. */
|
||||
typedef enum {
|
||||
BECH32_ENCODING_NONE,
|
||||
BECH32_ENCODING_BECH32,
|
||||
BECH32_ENCODING_BECH32M
|
||||
} bech32_encoding;
|
||||
|
||||
/** Encode a Bech32 or Bech32m string
|
||||
*
|
||||
* Out: output: Pointer to a buffer of size strlen(hrp) + data_len + 8 that
|
||||
* will be updated to contain the null-terminated Bech32 string.
|
||||
* In: hrp : Pointer to the null-terminated human readable part.
|
||||
* data : Pointer to an array of 5-bit values.
|
||||
* data_len: Length of the data array.
|
||||
* max_input_len: Maximum valid length of input (90 for segwit usage).
|
||||
* enc: Which encoding to use (BECH32_ENCODING_BECH32{,M}).
|
||||
* Returns 1 if successful.
|
||||
*/
|
||||
int bech32_encode(
|
||||
char *output,
|
||||
const char *hrp,
|
||||
const uint8_t *data,
|
||||
size_t data_len,
|
||||
size_t max_input_len,
|
||||
bech32_encoding enc
|
||||
);
|
||||
|
||||
/** Decode a Bech32 or Bech32m string
|
||||
*
|
||||
* Out: hrp: Pointer to a buffer of size strlen(input) - 6. Will be
|
||||
* updated to contain the null-terminated human readable part.
|
||||
* data: Pointer to a buffer of size strlen(input) - 8 that will
|
||||
* hold the encoded 5-bit data values.
|
||||
* data_len: Pointer to a size_t that will be updated to be the number
|
||||
* of entries in data.
|
||||
* In: input: Pointer to a null-terminated Bech32 string.
|
||||
* max_input_len: Maximum valid length of input (90 for segwit usage).
|
||||
* Returns BECH32_ENCODING_BECH32{,M} to indicate decoding was successful
|
||||
* with the specified encoding standard. BECH32_ENCODING_NONE is returned if
|
||||
* decoding failed.
|
||||
*/
|
||||
bech32_encoding bech32_decode(
|
||||
char *hrp,
|
||||
uint8_t *data,
|
||||
size_t *data_len,
|
||||
const char *input,
|
||||
size_t max_input_len
|
||||
);
|
||||
|
||||
bech32_encoding bech32_decode_len(
|
||||
char *hrp,
|
||||
uint8_t *data,
|
||||
size_t *data_len,
|
||||
const char *input,
|
||||
size_t input_len
|
||||
);
|
||||
|
||||
/* Helper from bech32: translates inbits-bit bytes to outbits-bit bytes.
|
||||
* @outlen is incremented as bytes are added.
|
||||
* @pad is true if we're to pad, otherwise truncate last byte if necessary
|
||||
*/
|
||||
int bech32_convert_bits(uint8_t* out, size_t* outlen, int outbits,
|
||||
const uint8_t* in, size_t inlen, int inbits,
|
||||
int pad);
|
||||
|
||||
/* The charset, and reverse mapping */
|
||||
extern const char bech32_charset[32];
|
||||
extern const int8_t bech32_charset_rev[128];
|
||||
|
||||
#endif /* LIGHTNING_COMMON_BECH32_H */
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#define HAVE_UNALIGNED_ACCESS 1
|
||||
#define HAVE_TYPEOF 1
|
||||
#define HAVE_BIG_ENDIAN 0
|
||||
#define HAVE_BYTESWAP_H 0
|
||||
#define HAVE_BSWAP_64 0
|
||||
#define HAVE_BYTESWAP_H 1
|
||||
#define HAVE_BSWAP_64 1
|
||||
#define HAVE_LITTLE_ENDIAN 1
|
||||
#endif /* CCAN_CONFIG_H */
|
||||
|
||||
1110
nostrdb/configurator.c
Normal file
1110
nostrdb/configurator.c
Normal file
File diff suppressed because it is too large
Load Diff
114
nostrdb/endian.h
114
nostrdb/endian.h
@@ -1,8 +1,10 @@
|
||||
/* CC0 (Public domain) - see LICENSE file for details */
|
||||
/* CC0 (Public domain) */
|
||||
#ifndef CCAN_ENDIAN_H
|
||||
#define CCAN_ENDIAN_H
|
||||
#include <stdint.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "cursor.h"
|
||||
|
||||
/**
|
||||
* BSWAP_16 - reverse bytes in a constant uint16_t value.
|
||||
@@ -11,13 +13,13 @@
|
||||
* Designed to be usable in constant-requiring initializers.
|
||||
*
|
||||
* Example:
|
||||
* struct mystruct {
|
||||
* char buf[BSWAP_16(0x1234)];
|
||||
* };
|
||||
* struct mystruct {
|
||||
* char buf[BSWAP_16(0x1234)];
|
||||
* };
|
||||
*/
|
||||
#define BSWAP_16(val) \
|
||||
((((uint16_t)(val) & 0x00ff) << 8) \
|
||||
| (((uint16_t)(val) & 0xff00) >> 8))
|
||||
#define BSWAP_16(val) \
|
||||
((((uint16_t)(val) & 0x00ff) << 8) \
|
||||
| (((uint16_t)(val) & 0xff00) >> 8))
|
||||
|
||||
/**
|
||||
* BSWAP_32 - reverse bytes in a constant uint32_t value.
|
||||
@@ -26,15 +28,15 @@
|
||||
* Designed to be usable in constant-requiring initializers.
|
||||
*
|
||||
* Example:
|
||||
* struct mystruct {
|
||||
* char buf[BSWAP_32(0xff000000)];
|
||||
* };
|
||||
* struct mystruct {
|
||||
* char buf[BSWAP_32(0xff000000)];
|
||||
* };
|
||||
*/
|
||||
#define BSWAP_32(val) \
|
||||
((((uint32_t)(val) & 0x000000ff) << 24) \
|
||||
| (((uint32_t)(val) & 0x0000ff00) << 8) \
|
||||
| (((uint32_t)(val) & 0x00ff0000) >> 8) \
|
||||
| (((uint32_t)(val) & 0xff000000) >> 24))
|
||||
#define BSWAP_32(val) \
|
||||
((((uint32_t)(val) & 0x000000ff) << 24) \
|
||||
| (((uint32_t)(val) & 0x0000ff00) << 8) \
|
||||
| (((uint32_t)(val) & 0x00ff0000) >> 8) \
|
||||
| (((uint32_t)(val) & 0xff000000) >> 24))
|
||||
|
||||
/**
|
||||
* BSWAP_64 - reverse bytes in a constant uint64_t value.
|
||||
@@ -43,19 +45,19 @@
|
||||
* Designed to be usable in constant-requiring initializers.
|
||||
*
|
||||
* Example:
|
||||
* struct mystruct {
|
||||
* char buf[BSWAP_64(0xff00000000000000ULL)];
|
||||
* };
|
||||
* struct mystruct {
|
||||
* char buf[BSWAP_64(0xff00000000000000ULL)];
|
||||
* };
|
||||
*/
|
||||
#define BSWAP_64(val) \
|
||||
((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \
|
||||
| (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \
|
||||
| (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \
|
||||
| (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \
|
||||
| (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \
|
||||
| (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \
|
||||
| (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \
|
||||
| (((uint64_t)(val) & 0xff00000000000000ULL) >> 56))
|
||||
#define BSWAP_64(val) \
|
||||
((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \
|
||||
| (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \
|
||||
| (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \
|
||||
| (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \
|
||||
| (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \
|
||||
| (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \
|
||||
| (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \
|
||||
| (((uint64_t)(val) & 0xff00000000000000ULL) >> 56))
|
||||
|
||||
#if HAVE_BYTESWAP_H
|
||||
#include <byteswap.h>
|
||||
@@ -65,12 +67,12 @@
|
||||
* @val: value whose bytes to swap.
|
||||
*
|
||||
* Example:
|
||||
* // Output contains "1024 is 4 as two bytes reversed"
|
||||
* printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
|
||||
* // Output contains "1024 is 4 as two bytes reversed"
|
||||
* printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
|
||||
*/
|
||||
static inline uint16_t bswap_16(uint16_t val)
|
||||
{
|
||||
return BSWAP_16(val);
|
||||
return BSWAP_16(val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,12 +80,12 @@ static inline uint16_t bswap_16(uint16_t val)
|
||||
* @val: value whose bytes to swap.
|
||||
*
|
||||
* Example:
|
||||
* // Output contains "1024 is 262144 as four bytes reversed"
|
||||
* printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
|
||||
* // Output contains "1024 is 262144 as four bytes reversed"
|
||||
* printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
|
||||
*/
|
||||
static inline uint32_t bswap_32(uint32_t val)
|
||||
{
|
||||
return BSWAP_32(val);
|
||||
return BSWAP_32(val);
|
||||
}
|
||||
#endif /* !HAVE_BYTESWAP_H */
|
||||
|
||||
@@ -93,19 +95,19 @@ static inline uint32_t bswap_32(uint32_t val)
|
||||
* @val: value whose bytes to swap.
|
||||
*
|
||||
* Example:
|
||||
* // Output contains "1024 is 1125899906842624 as eight bytes reversed"
|
||||
* printf("1024 is %llu as eight bytes reversed\n",
|
||||
* (unsigned long long)bswap_64(1024));
|
||||
* // Output contains "1024 is 1125899906842624 as eight bytes reversed"
|
||||
* printf("1024 is %llu as eight bytes reversed\n",
|
||||
* (unsigned long long)bswap_64(1024));
|
||||
*/
|
||||
static inline uint64_t bswap_64(uint64_t val)
|
||||
{
|
||||
return BSWAP_64(val);
|
||||
return BSWAP_64(val);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Needed for Glibc like endiness check */
|
||||
#define __LITTLE_ENDIAN 1234
|
||||
#define __BIG_ENDIAN 4321
|
||||
#define __LITTLE_ENDIAN 1234
|
||||
#define __BIG_ENDIAN 4321
|
||||
|
||||
/* Sanity check the defines. We don't handle weird endianness. */
|
||||
#if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
|
||||
@@ -114,13 +116,13 @@ static inline uint64_t bswap_64(uint64_t val)
|
||||
#error "Can't compile for both big and little endian."
|
||||
#elif HAVE_LITTLE_ENDIAN
|
||||
#ifndef __BYTE_ORDER
|
||||
#define __BYTE_ORDER __LITTLE_ENDIAN
|
||||
#define __BYTE_ORDER __LITTLE_ENDIAN
|
||||
#elif __BYTE_ORDER != __LITTLE_ENDIAN
|
||||
#error "__BYTE_ORDER already defined, but not equal to __LITTLE_ENDIAN"
|
||||
#endif
|
||||
#elif HAVE_BIG_ENDIAN
|
||||
#ifndef __BYTE_ORDER
|
||||
#define __BYTE_ORDER __BIG_ENDIAN
|
||||
#define __BYTE_ORDER __BIG_ENDIAN
|
||||
#elif __BYTE_ORDER != __BIG_ENDIAN
|
||||
#error "__BYTE_ORDER already defined, but not equal to __BIG_ENDIAN"
|
||||
#endif
|
||||
@@ -242,7 +244,7 @@ typedef uint16_t ENDIAN_TYPE beint16_t;
|
||||
*/
|
||||
static inline leint64_t cpu_to_le64(uint64_t native)
|
||||
{
|
||||
return CPU_TO_LE64(native);
|
||||
return CPU_TO_LE64(native);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -251,7 +253,7 @@ static inline leint64_t cpu_to_le64(uint64_t native)
|
||||
*/
|
||||
static inline leint32_t cpu_to_le32(uint32_t native)
|
||||
{
|
||||
return CPU_TO_LE32(native);
|
||||
return CPU_TO_LE32(native);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,7 +262,7 @@ static inline leint32_t cpu_to_le32(uint32_t native)
|
||||
*/
|
||||
static inline leint16_t cpu_to_le16(uint16_t native)
|
||||
{
|
||||
return CPU_TO_LE16(native);
|
||||
return CPU_TO_LE16(native);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -269,7 +271,7 @@ static inline leint16_t cpu_to_le16(uint16_t native)
|
||||
*/
|
||||
static inline uint64_t le64_to_cpu(leint64_t le_val)
|
||||
{
|
||||
return LE64_TO_CPU(le_val);
|
||||
return LE64_TO_CPU(le_val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -278,7 +280,7 @@ static inline uint64_t le64_to_cpu(leint64_t le_val)
|
||||
*/
|
||||
static inline uint32_t le32_to_cpu(leint32_t le_val)
|
||||
{
|
||||
return LE32_TO_CPU(le_val);
|
||||
return LE32_TO_CPU(le_val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -287,7 +289,7 @@ static inline uint32_t le32_to_cpu(leint32_t le_val)
|
||||
*/
|
||||
static inline uint16_t le16_to_cpu(leint16_t le_val)
|
||||
{
|
||||
return LE16_TO_CPU(le_val);
|
||||
return LE16_TO_CPU(le_val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,7 +298,7 @@ static inline uint16_t le16_to_cpu(leint16_t le_val)
|
||||
*/
|
||||
static inline beint64_t cpu_to_be64(uint64_t native)
|
||||
{
|
||||
return CPU_TO_BE64(native);
|
||||
return CPU_TO_BE64(native);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -305,7 +307,7 @@ static inline beint64_t cpu_to_be64(uint64_t native)
|
||||
*/
|
||||
static inline beint32_t cpu_to_be32(uint32_t native)
|
||||
{
|
||||
return CPU_TO_BE32(native);
|
||||
return CPU_TO_BE32(native);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -314,7 +316,7 @@ static inline beint32_t cpu_to_be32(uint32_t native)
|
||||
*/
|
||||
static inline beint16_t cpu_to_be16(uint16_t native)
|
||||
{
|
||||
return CPU_TO_BE16(native);
|
||||
return CPU_TO_BE16(native);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -323,7 +325,7 @@ static inline beint16_t cpu_to_be16(uint16_t native)
|
||||
*/
|
||||
static inline uint64_t be64_to_cpu(beint64_t be_val)
|
||||
{
|
||||
return BE64_TO_CPU(be_val);
|
||||
return BE64_TO_CPU(be_val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,7 +334,7 @@ static inline uint64_t be64_to_cpu(beint64_t be_val)
|
||||
*/
|
||||
static inline uint32_t be32_to_cpu(beint32_t be_val)
|
||||
{
|
||||
return BE32_TO_CPU(be_val);
|
||||
return BE32_TO_CPU(be_val);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -341,11 +343,9 @@ static inline uint32_t be32_to_cpu(beint32_t be_val)
|
||||
*/
|
||||
static inline uint16_t be16_to_cpu(beint16_t be_val)
|
||||
{
|
||||
return BE16_TO_CPU(be_val);
|
||||
return BE16_TO_CPU(be_val);
|
||||
}
|
||||
|
||||
/* Whichever they include first, they get these definitions. */
|
||||
#ifdef CCAN_SHORT_TYPES_H
|
||||
/**
|
||||
* be64/be32/be16 - 64/32/16 bit big-endian representation.
|
||||
*/
|
||||
@@ -359,5 +359,7 @@ typedef beint16_t be16;
|
||||
typedef leint64_t le64;
|
||||
typedef leint32_t le32;
|
||||
typedef leint16_t le16;
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* CCAN_ENDIAN_H */
|
||||
|
||||
|
||||
124
nostrdb/hex.h
124
nostrdb/hex.h
@@ -1,84 +1,68 @@
|
||||
/* CC0 (Public domain) - see LICENSE file for details */
|
||||
#ifndef CCAN_HEX_H
|
||||
#define CCAN_HEX_H
|
||||
#include "config.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef HEX_H
|
||||
#define HEX_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* hex_decode - Unpack a hex string.
|
||||
* @str: the hexadecimal string
|
||||
* @slen: the length of @str
|
||||
* @buf: the buffer to write the data into
|
||||
* @bufsize: the length of
|
||||
*
|
||||
* Returns false if there are any characters which aren't 0-9, a-f or A-F,
|
||||
* of the string wasn't the right length for @bufsize.
|
||||
*
|
||||
* Example:
|
||||
* unsigned char data[20];
|
||||
*
|
||||
* if (!hex_decode(argv[1], strlen(argv[1]), data, 20))
|
||||
* printf("String is malformed!\n");
|
||||
*/
|
||||
bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize);
|
||||
static const char hex_table[256] = {
|
||||
['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3,
|
||||
['4'] = 4, ['5'] = 5, ['6'] = 6, ['7'] = 7,
|
||||
['8'] = 8, ['9'] = 9, ['a'] = 10, ['b'] = 11,
|
||||
['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15,
|
||||
['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13,
|
||||
['E'] = 14, ['F'] = 15
|
||||
};
|
||||
|
||||
/**
|
||||
* hex_encode - Create a nul-terminated hex string
|
||||
* @buf: the buffer to read the data from
|
||||
* @bufsize: the length of buf
|
||||
* @dest: the string to fill
|
||||
* @destsize: the max size of the string
|
||||
*
|
||||
* Returns true if the string, including terminator, fit in @destsize;
|
||||
*
|
||||
* Example:
|
||||
* unsigned char buf[] = { 0x1F, 0x2F };
|
||||
* char str[5];
|
||||
*
|
||||
* if (!hex_encode(buf, sizeof(buf), str, sizeof(str)))
|
||||
* abort();
|
||||
*/
|
||||
bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize);
|
||||
|
||||
/**
|
||||
* hex_str_size - Calculate how big a nul-terminated hex string is
|
||||
* @bytes: bytes of data to represent
|
||||
*
|
||||
* Example:
|
||||
* unsigned char buf[] = { 0x1F, 0x2F };
|
||||
* char str[hex_str_size(sizeof(buf))];
|
||||
*
|
||||
* hex_encode(buf, sizeof(buf), str, sizeof(str));
|
||||
*/
|
||||
static inline size_t hex_str_size(size_t bytes)
|
||||
static inline int char_to_hex(unsigned char *val, unsigned char c)
|
||||
{
|
||||
return 2 * bytes + 1;
|
||||
if (hex_table[c] || c == '0') {
|
||||
*val = hex_table[c];
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* hex_data_size - Calculate how many bytes of data in a hex string
|
||||
* @strlen: the length of the string (with or without NUL)
|
||||
*
|
||||
* Example:
|
||||
* const char str[] = "1F2F";
|
||||
* unsigned char buf[hex_data_size(sizeof(str))];
|
||||
*
|
||||
* hex_decode(str, strlen(str), buf, sizeof(buf));
|
||||
*/
|
||||
static inline size_t hex_data_size(size_t strlen)
|
||||
static inline int hex_decode(const char *str, size_t slen, void *buf, size_t bufsize)
|
||||
{
|
||||
return strlen / 2;
|
||||
unsigned char v1, v2;
|
||||
unsigned char *p = buf;
|
||||
|
||||
while (slen > 1) {
|
||||
if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1]))
|
||||
return 0;
|
||||
if (!bufsize)
|
||||
return 0;
|
||||
*(p++) = (v1 << 4) | v2;
|
||||
str += 2;
|
||||
slen -= 2;
|
||||
bufsize--;
|
||||
}
|
||||
return slen == 0 && bufsize == 0;
|
||||
}
|
||||
|
||||
|
||||
static inline char hexchar(unsigned int val)
|
||||
{
|
||||
if (val < 10)
|
||||
return '0' + val;
|
||||
if (val < 16)
|
||||
return 'a' + val - 10;
|
||||
abort();
|
||||
if (val < 10)
|
||||
return '0' + val;
|
||||
if (val < 16)
|
||||
return 'a' + val - 10;
|
||||
abort();
|
||||
}
|
||||
|
||||
static int hex_encode(const void *buf, size_t bufsize, char *dest)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < bufsize; i++) {
|
||||
unsigned int c = ((const unsigned char *)buf)[i];
|
||||
*(dest++) = hexchar(c >> 4);
|
||||
*(dest++) = hexchar(c & 0xF);
|
||||
}
|
||||
*dest = '\0';
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#endif /* CCAN_HEX_H */
|
||||
#endif
|
||||
|
||||
48
nostrdb/io.h
Normal file
48
nostrdb/io.h
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static int read_fd(FILE *fd, unsigned char *buf, int buflen, int *written)
|
||||
{
|
||||
unsigned char *p = buf;
|
||||
int len = 0;
|
||||
*written = 0;
|
||||
|
||||
do {
|
||||
len = fread(p, 1, 4096, fd);
|
||||
*written += len;
|
||||
p += len;
|
||||
if (p > buf + buflen)
|
||||
return 0;
|
||||
} while (len == 4096);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int write_file(const char *filename, unsigned char *buf, int buflen)
|
||||
{
|
||||
FILE *file = NULL;
|
||||
int ok;
|
||||
|
||||
file = fopen(filename, "w");
|
||||
if (file == NULL)
|
||||
return 0;
|
||||
|
||||
ok = fwrite(buf, buflen, 1, file);
|
||||
fclose(file);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int read_file(const char *filename, unsigned char *buf, int buflen, int *written)
|
||||
{
|
||||
FILE *file = NULL;
|
||||
int ok;
|
||||
|
||||
file = fopen(filename, "r");
|
||||
if (file == NULL)
|
||||
return 1;
|
||||
|
||||
ok = read_fd(file, buf, buflen, written);
|
||||
fclose(file);
|
||||
return ok;
|
||||
}
|
||||
|
||||
100
nostrdb/lmdb_util.h
Normal file
100
nostrdb/lmdb_util.h
Normal file
@@ -0,0 +1,100 @@
|
||||
|
||||
// Define callback function type
|
||||
typedef bool (*lmdb_callback_t)(const MDB_val*, const MDB_val*);
|
||||
|
||||
|
||||
int lmdb_foreach(MDB_txn *txn, MDB_dbi dbi, MDB_val *start, MDB_val *start_dup, callback_t cb, bool reverse) {
|
||||
int success = 0;
|
||||
MDB_cursor *cursor;
|
||||
|
||||
// Open a cursor on the provided transaction and database
|
||||
int rc = mdb_cursor_open(txn, dbi, &cursor);
|
||||
|
||||
if (rc != 0)
|
||||
return 0;
|
||||
|
||||
MDB_val k = *start, v = *start_dup;
|
||||
MDB_cursor_op op = reverse ? MDB_PREV : MDB_NEXT;
|
||||
|
||||
// If we're scanning in reverse...
|
||||
if (reverse) {
|
||||
// Try to position the cursor at the first key-value pair where
|
||||
// both the key and the value are greater than or equal to our
|
||||
// starting point.
|
||||
rc = mdb_cursor_get(cursor, &k, &v, MDB_GET_BOTH_RANGE);
|
||||
if (rc == 0) {
|
||||
if (v.mv_size != start_dup->mv_size ||
|
||||
memcmp(v.mv_data, start_dup->mv_data, v.mv_size) != 0) {
|
||||
// If the value doesn't match our starting
|
||||
// point, step back to the previous record.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_PREV) != 0)
|
||||
goto cleanup;
|
||||
}
|
||||
} else {
|
||||
// If we couldn't find a record that matches both our
|
||||
// starting key and value, try to find a record that
|
||||
// matches just our starting key.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_SET) == 0) {
|
||||
// If we find a match, move to the last value
|
||||
// for this key, since we're scanning in
|
||||
// reverse.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_LAST_DUP) != 0)
|
||||
goto cleanup;
|
||||
} else {
|
||||
// If we can't find a record with our starting
|
||||
// key, try to find the first record with a key
|
||||
// greater than our starting key.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_SET_RANGE) == 0) {
|
||||
// If we find such a record, step back
|
||||
// to the previous record.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_PREV) != 0)
|
||||
goto cleanup;
|
||||
} else {
|
||||
// If we can't even find a record with
|
||||
// a key greater than our starting key,
|
||||
// fall back to starting from the last
|
||||
// record in the database.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_LAST) != 0)
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we're not scanning in reverse...
|
||||
else {
|
||||
// Try to position the cursor at the first key-value
|
||||
// pair where both the key and the value are greater
|
||||
// than or equal to our starting point.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_SET) != 0) {
|
||||
// If we couldn't find a record that matches
|
||||
// both our starting key and value, try to find
|
||||
// a record that matches just our starting key.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_SET_RANGE) != 0)
|
||||
goto cleanup;
|
||||
|
||||
// If we can't find a record with our starting
|
||||
// key, try to find the first record with a key
|
||||
// greater than our starting key.
|
||||
if (mdb_cursor_get(cursor, &k, &v, MDB_FIRST_DUP) != 0)
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Whether we're scanning forward or backward, start the actual
|
||||
// iteration, moving one step at a time in the appropriate direction
|
||||
// and calling the provided callback for each record.
|
||||
do {
|
||||
if (!cb(&k, &v))
|
||||
goto cleanup;
|
||||
} while (mdb_cursor_get(cursor, &k, &v, op) == 0);
|
||||
|
||||
// If we make it through the entire iteration without the callback
|
||||
// returning false, return true to signal success.
|
||||
success = 1;
|
||||
|
||||
cleanup:
|
||||
mdb_cursor_close(cursor);
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
185
nostrdb/ndb.c
Normal file
185
nostrdb/ndb.c
Normal file
@@ -0,0 +1,185 @@
|
||||
|
||||
|
||||
#include "nostrdb.h"
|
||||
#include "print_util.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static int usage()
|
||||
{
|
||||
printf("usage: ndb [--skip-verification] [-d db_dir] <command>\n\n");
|
||||
printf("commands\n\n");
|
||||
printf(" stat\n");
|
||||
printf(" search [--oldest-first] [--limit 42] <fulltext query>\n");
|
||||
printf(" import <line-delimited json file>\n\n");
|
||||
printf("settings\n\n");
|
||||
printf(" --skip-verification skip signature validation\n");
|
||||
printf(" -d <db_dir> set database directory\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int map_file(const char *filename, unsigned char **p, size_t *flen)
|
||||
{
|
||||
struct stat st;
|
||||
int des;
|
||||
stat(filename, &st);
|
||||
*flen = st.st_size;
|
||||
|
||||
des = open(filename, O_RDONLY);
|
||||
|
||||
*p = mmap(NULL, *flen, PROT_READ, MAP_PRIVATE, des, 0);
|
||||
close(des);
|
||||
|
||||
return *p != MAP_FAILED;
|
||||
}
|
||||
|
||||
static inline void print_stat_counts(struct ndb_stat_counts *counts)
|
||||
{
|
||||
printf("%zu\t%zu\t%zu\t%zu\n",
|
||||
counts->count,
|
||||
counts->key_size,
|
||||
counts->value_size,
|
||||
counts->key_size + counts->value_size);
|
||||
}
|
||||
|
||||
static void print_stats(struct ndb_stat *stat)
|
||||
{
|
||||
int i;
|
||||
const char *name;
|
||||
struct ndb_stat_counts *c;
|
||||
|
||||
struct ndb_stat_counts total;
|
||||
ndb_stat_counts_init(&total);
|
||||
|
||||
printf("name\tcount\tkey_bytes\tvalue_bytes\ttotal_bytes\n");
|
||||
printf("---\ndbs\n---\n");
|
||||
for (i = 0; i < NDB_DBS; i++) {
|
||||
name = ndb_db_name(i);
|
||||
|
||||
total.count += stat->dbs[i].count;
|
||||
total.key_size += stat->dbs[i].key_size;
|
||||
total.value_size += stat->dbs[i].value_size;
|
||||
|
||||
printf("%s\t", name);
|
||||
print_stat_counts(&stat->dbs[i]);
|
||||
}
|
||||
|
||||
printf("total\t");
|
||||
print_stat_counts(&total);
|
||||
|
||||
printf("-----\nkinds\n-----\n");
|
||||
for (i = 0; i < NDB_CKIND_COUNT; i++) {
|
||||
c = &stat->common_kinds[i];
|
||||
if (c->count == 0)
|
||||
continue;
|
||||
|
||||
printf("%s\t", ndb_kind_name(i));
|
||||
print_stat_counts(c);
|
||||
}
|
||||
|
||||
if (stat->other_kinds.count != 0) {
|
||||
printf("other\t");
|
||||
print_stat_counts(&stat->other_kinds);
|
||||
}
|
||||
}
|
||||
|
||||
int ndb_print_search_keys(struct ndb_txn *txn);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct ndb *ndb;
|
||||
int i, flags, limit;
|
||||
struct ndb_stat stat;
|
||||
struct ndb_txn txn;
|
||||
struct ndb_text_search_results results;
|
||||
struct ndb_text_search_result *result;
|
||||
const char *dir;
|
||||
unsigned char *data;
|
||||
size_t data_len;
|
||||
struct ndb_config config;
|
||||
struct ndb_text_search_config search_config;
|
||||
ndb_default_config(&config);
|
||||
ndb_default_text_search_config(&search_config);
|
||||
ndb_config_set_mapsize(&config, 1024ULL * 1024ULL * 1024ULL * 1024ULL /* 1 TiB */);
|
||||
|
||||
if (argc < 2) {
|
||||
return usage();
|
||||
}
|
||||
|
||||
dir = ".";
|
||||
flags = 0;
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
if (!strcmp(argv[1], "-d") && argv[2]) {
|
||||
dir = argv[2];
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else if (!strcmp(argv[1], "--skip-verification")) {
|
||||
flags = NDB_FLAG_SKIP_NOTE_VERIFY;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
ndb_config_set_flags(&config, flags);
|
||||
|
||||
fprintf(stderr, "using db '%s'\n", dir);
|
||||
|
||||
if (!ndb_init(&ndb, dir, &config)) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (argc >= 3 && !strcmp(argv[1], "search")) {
|
||||
for (i = 0; i < 2; i++) {
|
||||
if (!strcmp(argv[2], "--oldest-first")) {
|
||||
ndb_text_search_config_set_order(&search_config, NDB_ORDER_ASCENDING);
|
||||
argv++;
|
||||
argc--;
|
||||
} else if (!strcmp(argv[2], "--limit")) {
|
||||
limit = atoi(argv[3]);
|
||||
ndb_text_search_config_set_limit(&search_config, limit);
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
}
|
||||
}
|
||||
|
||||
ndb_begin_query(ndb, &txn);
|
||||
ndb_text_search(&txn, argv[2], &results, &search_config);
|
||||
|
||||
// print results for now
|
||||
for (i = 0; i < results.num_results; i++) {
|
||||
result = &results.results[i];
|
||||
printf("[%02d] ", i+1);
|
||||
ndb_print_text_search_result(&txn, result);
|
||||
}
|
||||
|
||||
ndb_end_query(&txn);
|
||||
} else if (argc == 2 && !strcmp(argv[1], "stat")) {
|
||||
if (!ndb_stat(ndb, &stat)) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
print_stats(&stat);
|
||||
} else if (argc == 3 && !strcmp(argv[1], "import")) {
|
||||
if (!strcmp(argv[2], "-")) {
|
||||
ndb_process_events_stream(ndb, stdin);
|
||||
} else {
|
||||
map_file(argv[2], &data, &data_len);
|
||||
ndb_process_events(ndb, (const char *)data, data_len);
|
||||
ndb_process_client_events(ndb, (const char *)data, data_len);
|
||||
}
|
||||
} else if (argc == 2 && !strcmp(argv[1], "print-search-keys")) {
|
||||
ndb_begin_query(ndb, &txn);
|
||||
ndb_print_search_keys(&txn);
|
||||
ndb_end_query(&txn);
|
||||
} else {
|
||||
return usage();
|
||||
}
|
||||
|
||||
ndb_destroy(ndb);
|
||||
}
|
||||
@@ -7,10 +7,8 @@
|
||||
|
||||
#include "nostr_bech32.h"
|
||||
#include <stdlib.h>
|
||||
#include "endian.h"
|
||||
#include "cursor.h"
|
||||
#include "bech32.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_TLVS 16
|
||||
|
||||
@@ -147,11 +145,6 @@ static int tlvs_to_relays(struct nostr_tlvs *tlvs, struct relays *relays) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint32_t decode_tlv_u32(const uint8_t *bytes) {
|
||||
beint32_t *be32_bytes = (beint32_t*)bytes;
|
||||
return be32_to_cpu(*be32_bytes);
|
||||
}
|
||||
|
||||
static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *nevent) {
|
||||
struct nostr_tlvs tlvs;
|
||||
struct nostr_tlv *tlv;
|
||||
@@ -173,13 +166,6 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n
|
||||
nevent->pubkey = NULL;
|
||||
}
|
||||
|
||||
if(find_tlv(&tlvs, TLV_KIND, &tlv)) {
|
||||
nevent->kind = decode_tlv_u32(tlv->value);
|
||||
nevent->has_kind = true;
|
||||
} else {
|
||||
nevent->has_kind = false;
|
||||
}
|
||||
|
||||
return tlvs_to_relays(&tlvs, &nevent->relays);
|
||||
}
|
||||
|
||||
@@ -201,11 +187,6 @@ static int parse_nostr_bech32_naddr(struct cursor *cur, struct bech32_naddr *nad
|
||||
|
||||
naddr->pubkey = tlv->value;
|
||||
|
||||
if(!find_tlv(&tlvs, TLV_KIND, &tlv)) {
|
||||
return 0;
|
||||
}
|
||||
naddr->kind = decode_tlv_u32(tlv->value);
|
||||
|
||||
return tlvs_to_relays(&tlvs, &naddr->relays);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,6 @@
|
||||
#include <stdio.h>
|
||||
#include "str_block.h"
|
||||
#include "cursor.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef unsigned char u8;
|
||||
#define MAX_RELAYS 10
|
||||
|
||||
@@ -47,8 +45,6 @@ struct bech32_nevent {
|
||||
struct relays relays;
|
||||
const u8 *event_id;
|
||||
const u8 *pubkey; // optional
|
||||
uint32_t kind;
|
||||
bool has_kind;
|
||||
};
|
||||
|
||||
struct bech32_nprofile {
|
||||
@@ -60,7 +56,6 @@ struct bech32_naddr {
|
||||
struct relays relays;
|
||||
struct str_block identifier;
|
||||
const u8 *pubkey;
|
||||
uint32_t kind;
|
||||
};
|
||||
|
||||
struct bech32_nrelay {
|
||||
|
||||
@@ -912,9 +912,9 @@ static int _ndb_begin_query(struct ndb *ndb, struct ndb_txn *txn, int flags)
|
||||
{
|
||||
txn->lmdb = &ndb->lmdb;
|
||||
MDB_txn **mdb_txn = (MDB_txn **)&txn->mdb_txn;
|
||||
if (!txn->lmdb->env)
|
||||
return 0;
|
||||
return mdb_txn_begin(txn->lmdb->env, NULL, flags, mdb_txn) == 0;
|
||||
if (!txn->lmdb->env)
|
||||
return 0;
|
||||
return mdb_txn_begin(txn->lmdb->env, NULL, flags, mdb_txn) == 0;
|
||||
}
|
||||
|
||||
int ndb_begin_query(struct ndb *ndb, struct ndb_txn *txn)
|
||||
@@ -2163,15 +2163,6 @@ static int ndb_write_note_kind_index(struct ndb_txn *txn, struct ndb_note *note,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void consume_whitespace_or_punctuation(struct cursor *cur)
|
||||
{
|
||||
while (cur->p < cur->end) {
|
||||
if (!is_right_boundary(*cur->p))
|
||||
return;
|
||||
cur->p++;
|
||||
}
|
||||
}
|
||||
|
||||
static int ndb_write_word_to_index(struct ndb_txn *txn, const char *word,
|
||||
int word_len, int word_index,
|
||||
uint64_t timestamp, uint64_t note_id)
|
||||
@@ -3570,7 +3561,7 @@ static int ndb_event_commitment(struct ndb_note *ev, unsigned char *buf, int buf
|
||||
struct cursor cur;
|
||||
int ok;
|
||||
|
||||
if (!hex_encode(ev->pubkey, sizeof(ev->pubkey), pubkey, sizeof(pubkey)))
|
||||
if (!hex_encode(ev->pubkey, sizeof(ev->pubkey), pubkey))
|
||||
return 0;
|
||||
|
||||
make_cursor(buf, buf + buflen, &cur);
|
||||
@@ -4111,17 +4102,6 @@ int ndb_ws_event_from_json(const char *json, int len, struct ndb_tce *tce,
|
||||
tce->command_result.msg = json + tok->start;
|
||||
tce->command_result.msglen = toksize(tok);
|
||||
|
||||
return 1;
|
||||
} else if (tok_len == 4 && !memcmp("AUTH", json + tok->start, 4)) {
|
||||
tce->evtype = NDB_TCE_AUTH;
|
||||
|
||||
tok = &parser.toks[parser.i++];
|
||||
if (tok->type != JSMN_STRING)
|
||||
return 0;
|
||||
|
||||
tce->subid = json + tok->start;
|
||||
tce->subid_len = toksize(tok);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,6 @@ enum tce_type {
|
||||
NDB_TCE_OK = 0x2,
|
||||
NDB_TCE_NOTICE = 0x3,
|
||||
NDB_TCE_EOSE = 0x4,
|
||||
NDB_TCE_AUTH = 0x5,
|
||||
};
|
||||
|
||||
enum ndb_ingest_filter_action {
|
||||
|
||||
36
nostrdb/print_util.h
Normal file
36
nostrdb/print_util.h
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
static void ndb_print_text_search_key(struct ndb_text_search_key *key)
|
||||
{
|
||||
printf("K<'%.*s' %d %" PRIu64 " note_id:%" PRIu64 ">", key->str_len, key->str,
|
||||
key->word_index,
|
||||
key->timestamp,
|
||||
key->note_id);
|
||||
}
|
||||
|
||||
static void print_hex(unsigned char* data, size_t size) {
|
||||
size_t i;
|
||||
for (i = 0; i < size; i++) {
|
||||
printf("%02x", data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void ndb_print_text_search_result(struct ndb_txn *txn,
|
||||
struct ndb_text_search_result *r)
|
||||
{
|
||||
size_t len;
|
||||
struct ndb_note *note;
|
||||
|
||||
ndb_print_text_search_key(&r->key);
|
||||
|
||||
if (!(note = ndb_get_note_by_key(txn, r->key.note_id, &len))) {
|
||||
printf(": note not found");
|
||||
return;
|
||||
}
|
||||
|
||||
printf(" ");
|
||||
print_hex(ndb_note_id(note), 32);
|
||||
|
||||
printf("\n%s\n\n---\n", ndb_note_content(note));
|
||||
}
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
#include <ntstatus.h>
|
||||
#include <bcrypt.h>
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
|
||||
//#include <sys/random.h>
|
||||
#include <Security/SecRandom.h>
|
||||
#include <sys/random.h>
|
||||
#elif defined(__OpenBSD__)
|
||||
#include <unistd.h>
|
||||
#else
|
||||
@@ -54,7 +53,7 @@ static int fill_random(unsigned char* data, size_t size) {
|
||||
#elif defined(__APPLE__) || defined(__OpenBSD__)
|
||||
/* If `getentropy(2)` is not available you should fallback to either
|
||||
* `SecRandomCopyBytes` or /dev/urandom */
|
||||
int res = SecRandomCopyBytes(kSecRandomDefault, size, data);
|
||||
int res = getentropy(data, size);
|
||||
if (res == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
|
||||
338
nostrdb/sha256.c
338
nostrdb/sha256.c
@@ -7,8 +7,8 @@
|
||||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
*/
|
||||
#include "sha256.h"
|
||||
#include "compiler.h"
|
||||
#include "endian.h"
|
||||
#include "compiler.h"
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
@@ -16,293 +16,287 @@
|
||||
static void invalidate_sha256(struct sha256_ctx *ctx)
|
||||
{
|
||||
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
|
||||
ctx->c.md_len = 0;
|
||||
ctx->c.md_len = 0;
|
||||
#else
|
||||
ctx->bytes = (size_t)-1;
|
||||
ctx->bytes = (size_t)-1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void check_sha256(struct sha256_ctx *ctx UNUSED)
|
||||
static void check_sha256(struct sha256_ctx *ctx)
|
||||
{
|
||||
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
|
||||
assert(ctx->c.md_len != 0);
|
||||
assert(ctx->c.md_len != 0);
|
||||
#else
|
||||
assert(ctx->bytes != (size_t)-1);
|
||||
assert(ctx->bytes != (size_t)-1);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
|
||||
void sha256_init(struct sha256_ctx *ctx)
|
||||
{
|
||||
SHA256_Init(&ctx->c);
|
||||
SHA256_Init(&ctx->c);
|
||||
}
|
||||
|
||||
void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size)
|
||||
{
|
||||
check_sha256(ctx);
|
||||
SHA256_Update(&ctx->c, p, size);
|
||||
check_sha256(ctx);
|
||||
SHA256_Update(&ctx->c, p, size);
|
||||
}
|
||||
|
||||
void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
|
||||
{
|
||||
SHA256_Final(res->u.u8, &ctx->c);
|
||||
invalidate_sha256(ctx);
|
||||
SHA256_Final(res->u.u8, &ctx->c);
|
||||
invalidate_sha256(ctx);
|
||||
}
|
||||
#else
|
||||
static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
|
||||
{
|
||||
return z ^ (x & (y ^ z));
|
||||
return z ^ (x & (y ^ z));
|
||||
}
|
||||
static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
|
||||
{
|
||||
return (x & y) | (z & (x | y));
|
||||
return (x & y) | (z & (x | y));
|
||||
}
|
||||
static uint32_t Sigma0(uint32_t x)
|
||||
{
|
||||
return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10);
|
||||
return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10);
|
||||
}
|
||||
static uint32_t Sigma1(uint32_t x)
|
||||
{
|
||||
return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7);
|
||||
return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7);
|
||||
}
|
||||
static uint32_t sigma0(uint32_t x)
|
||||
{
|
||||
return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3);
|
||||
return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3);
|
||||
}
|
||||
static uint32_t sigma1(uint32_t x)
|
||||
{
|
||||
return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10);
|
||||
return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10);
|
||||
}
|
||||
|
||||
/** One round of SHA-256. */
|
||||
static void Round(uint32_t a, uint32_t b, uint32_t c, uint32_t *d, uint32_t e, uint32_t f, uint32_t g, uint32_t *h, uint32_t k, uint32_t w)
|
||||
{
|
||||
uint32_t t1 = *h + Sigma1(e) + Ch(e, f, g) + k + w;
|
||||
uint32_t t2 = Sigma0(a) + Maj(a, b, c);
|
||||
*d += t1;
|
||||
*h = t1 + t2;
|
||||
uint32_t t1 = *h + Sigma1(e) + Ch(e, f, g) + k + w;
|
||||
uint32_t t2 = Sigma0(a) + Maj(a, b, c);
|
||||
*d += t1;
|
||||
*h = t1 + t2;
|
||||
}
|
||||
|
||||
/** Perform one SHA-256 transformation, processing a 64-byte chunk. */
|
||||
static void Transform(uint32_t *s, const uint32_t *chunk)
|
||||
{
|
||||
uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7];
|
||||
uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
|
||||
uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7];
|
||||
uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
|
||||
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x428a2f98, w0 = be32_to_cpu(chunk[0]));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x71374491, w1 = be32_to_cpu(chunk[1]));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0xb5c0fbcf, w2 = be32_to_cpu(chunk[2]));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0xe9b5dba5, w3 = be32_to_cpu(chunk[3]));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x3956c25b, w4 = be32_to_cpu(chunk[4]));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x59f111f1, w5 = be32_to_cpu(chunk[5]));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x923f82a4, w6 = be32_to_cpu(chunk[6]));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0xab1c5ed5, w7 = be32_to_cpu(chunk[7]));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0xd807aa98, w8 = be32_to_cpu(chunk[8]));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x12835b01, w9 = be32_to_cpu(chunk[9]));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x243185be, w10 = be32_to_cpu(chunk[10]));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x550c7dc3, w11 = be32_to_cpu(chunk[11]));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x72be5d74, w12 = be32_to_cpu(chunk[12]));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x80deb1fe, w13 = be32_to_cpu(chunk[13]));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x9bdc06a7, w14 = be32_to_cpu(chunk[14]));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0xc19bf174, w15 = be32_to_cpu(chunk[15]));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x428a2f98, w0 = be32_to_cpu(chunk[0]));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x71374491, w1 = be32_to_cpu(chunk[1]));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0xb5c0fbcf, w2 = be32_to_cpu(chunk[2]));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0xe9b5dba5, w3 = be32_to_cpu(chunk[3]));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x3956c25b, w4 = be32_to_cpu(chunk[4]));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x59f111f1, w5 = be32_to_cpu(chunk[5]));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x923f82a4, w6 = be32_to_cpu(chunk[6]));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0xab1c5ed5, w7 = be32_to_cpu(chunk[7]));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0xd807aa98, w8 = be32_to_cpu(chunk[8]));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x12835b01, w9 = be32_to_cpu(chunk[9]));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x243185be, w10 = be32_to_cpu(chunk[10]));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x550c7dc3, w11 = be32_to_cpu(chunk[11]));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x72be5d74, w12 = be32_to_cpu(chunk[12]));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x80deb1fe, w13 = be32_to_cpu(chunk[13]));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x9bdc06a7, w14 = be32_to_cpu(chunk[14]));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0xc19bf174, w15 = be32_to_cpu(chunk[15]));
|
||||
|
||||
Round(a, b, c, &d, e, f, g, &h, 0xe49b69c1, w0 += sigma1(w14) + w9 + sigma0(w1));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0xefbe4786, w1 += sigma1(w15) + w10 + sigma0(w2));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x0fc19dc6, w2 += sigma1(w0) + w11 + sigma0(w3));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x240ca1cc, w3 += sigma1(w1) + w12 + sigma0(w4));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x2de92c6f, w4 += sigma1(w2) + w13 + sigma0(w5));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x4a7484aa, w5 += sigma1(w3) + w14 + sigma0(w6));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x5cb0a9dc, w6 += sigma1(w4) + w15 + sigma0(w7));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x76f988da, w7 += sigma1(w5) + w0 + sigma0(w8));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x983e5152, w8 += sigma1(w6) + w1 + sigma0(w9));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0xa831c66d, w9 += sigma1(w7) + w2 + sigma0(w10));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0xb00327c8, w10 += sigma1(w8) + w3 + sigma0(w11));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0xbf597fc7, w11 += sigma1(w9) + w4 + sigma0(w12));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0xc6e00bf3, w12 += sigma1(w10) + w5 + sigma0(w13));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0xd5a79147, w13 += sigma1(w11) + w6 + sigma0(w14));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x06ca6351, w14 += sigma1(w12) + w7 + sigma0(w15));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x14292967, w15 += sigma1(w13) + w8 + sigma0(w0));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0xe49b69c1, w0 += sigma1(w14) + w9 + sigma0(w1));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0xefbe4786, w1 += sigma1(w15) + w10 + sigma0(w2));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x0fc19dc6, w2 += sigma1(w0) + w11 + sigma0(w3));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x240ca1cc, w3 += sigma1(w1) + w12 + sigma0(w4));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x2de92c6f, w4 += sigma1(w2) + w13 + sigma0(w5));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x4a7484aa, w5 += sigma1(w3) + w14 + sigma0(w6));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x5cb0a9dc, w6 += sigma1(w4) + w15 + sigma0(w7));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x76f988da, w7 += sigma1(w5) + w0 + sigma0(w8));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x983e5152, w8 += sigma1(w6) + w1 + sigma0(w9));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0xa831c66d, w9 += sigma1(w7) + w2 + sigma0(w10));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0xb00327c8, w10 += sigma1(w8) + w3 + sigma0(w11));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0xbf597fc7, w11 += sigma1(w9) + w4 + sigma0(w12));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0xc6e00bf3, w12 += sigma1(w10) + w5 + sigma0(w13));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0xd5a79147, w13 += sigma1(w11) + w6 + sigma0(w14));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x06ca6351, w14 += sigma1(w12) + w7 + sigma0(w15));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x14292967, w15 += sigma1(w13) + w8 + sigma0(w0));
|
||||
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x27b70a85, w0 += sigma1(w14) + w9 + sigma0(w1));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x2e1b2138, w1 += sigma1(w15) + w10 + sigma0(w2));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x4d2c6dfc, w2 += sigma1(w0) + w11 + sigma0(w3));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x53380d13, w3 += sigma1(w1) + w12 + sigma0(w4));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x650a7354, w4 += sigma1(w2) + w13 + sigma0(w5));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x766a0abb, w5 += sigma1(w3) + w14 + sigma0(w6));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x81c2c92e, w6 += sigma1(w4) + w15 + sigma0(w7));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x92722c85, w7 += sigma1(w5) + w0 + sigma0(w8));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0xa2bfe8a1, w8 += sigma1(w6) + w1 + sigma0(w9));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0xa81a664b, w9 += sigma1(w7) + w2 + sigma0(w10));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0xc24b8b70, w10 += sigma1(w8) + w3 + sigma0(w11));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0xc76c51a3, w11 += sigma1(w9) + w4 + sigma0(w12));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0xd192e819, w12 += sigma1(w10) + w5 + sigma0(w13));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0xd6990624, w13 += sigma1(w11) + w6 + sigma0(w14));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0xf40e3585, w14 += sigma1(w12) + w7 + sigma0(w15));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x106aa070, w15 += sigma1(w13) + w8 + sigma0(w0));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x27b70a85, w0 += sigma1(w14) + w9 + sigma0(w1));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x2e1b2138, w1 += sigma1(w15) + w10 + sigma0(w2));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x4d2c6dfc, w2 += sigma1(w0) + w11 + sigma0(w3));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x53380d13, w3 += sigma1(w1) + w12 + sigma0(w4));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x650a7354, w4 += sigma1(w2) + w13 + sigma0(w5));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x766a0abb, w5 += sigma1(w3) + w14 + sigma0(w6));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x81c2c92e, w6 += sigma1(w4) + w15 + sigma0(w7));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x92722c85, w7 += sigma1(w5) + w0 + sigma0(w8));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0xa2bfe8a1, w8 += sigma1(w6) + w1 + sigma0(w9));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0xa81a664b, w9 += sigma1(w7) + w2 + sigma0(w10));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0xc24b8b70, w10 += sigma1(w8) + w3 + sigma0(w11));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0xc76c51a3, w11 += sigma1(w9) + w4 + sigma0(w12));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0xd192e819, w12 += sigma1(w10) + w5 + sigma0(w13));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0xd6990624, w13 += sigma1(w11) + w6 + sigma0(w14));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0xf40e3585, w14 += sigma1(w12) + w7 + sigma0(w15));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x106aa070, w15 += sigma1(w13) + w8 + sigma0(w0));
|
||||
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x19a4c116, w0 += sigma1(w14) + w9 + sigma0(w1));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x1e376c08, w1 += sigma1(w15) + w10 + sigma0(w2));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x2748774c, w2 += sigma1(w0) + w11 + sigma0(w3));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x34b0bcb5, w3 += sigma1(w1) + w12 + sigma0(w4));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x391c0cb3, w4 += sigma1(w2) + w13 + sigma0(w5));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x4ed8aa4a, w5 += sigma1(w3) + w14 + sigma0(w6));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x5b9cca4f, w6 += sigma1(w4) + w15 + sigma0(w7));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x682e6ff3, w7 += sigma1(w5) + w0 + sigma0(w8));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x748f82ee, w8 += sigma1(w6) + w1 + sigma0(w9));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x78a5636f, w9 += sigma1(w7) + w2 + sigma0(w10));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x84c87814, w10 += sigma1(w8) + w3 + sigma0(w11));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x8cc70208, w11 += sigma1(w9) + w4 + sigma0(w12));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x90befffa, w12 += sigma1(w10) + w5 + sigma0(w13));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0xa4506ceb, w13 += sigma1(w11) + w6 + sigma0(w14));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0xbef9a3f7, w14 + sigma1(w12) + w7 + sigma0(w15));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0xc67178f2, w15 + sigma1(w13) + w8 + sigma0(w0));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x19a4c116, w0 += sigma1(w14) + w9 + sigma0(w1));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x1e376c08, w1 += sigma1(w15) + w10 + sigma0(w2));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x2748774c, w2 += sigma1(w0) + w11 + sigma0(w3));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x34b0bcb5, w3 += sigma1(w1) + w12 + sigma0(w4));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x391c0cb3, w4 += sigma1(w2) + w13 + sigma0(w5));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0x4ed8aa4a, w5 += sigma1(w3) + w14 + sigma0(w6));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0x5b9cca4f, w6 += sigma1(w4) + w15 + sigma0(w7));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0x682e6ff3, w7 += sigma1(w5) + w0 + sigma0(w8));
|
||||
Round(a, b, c, &d, e, f, g, &h, 0x748f82ee, w8 += sigma1(w6) + w1 + sigma0(w9));
|
||||
Round(h, a, b, &c, d, e, f, &g, 0x78a5636f, w9 += sigma1(w7) + w2 + sigma0(w10));
|
||||
Round(g, h, a, &b, c, d, e, &f, 0x84c87814, w10 += sigma1(w8) + w3 + sigma0(w11));
|
||||
Round(f, g, h, &a, b, c, d, &e, 0x8cc70208, w11 += sigma1(w9) + w4 + sigma0(w12));
|
||||
Round(e, f, g, &h, a, b, c, &d, 0x90befffa, w12 += sigma1(w10) + w5 + sigma0(w13));
|
||||
Round(d, e, f, &g, h, a, b, &c, 0xa4506ceb, w13 += sigma1(w11) + w6 + sigma0(w14));
|
||||
Round(c, d, e, &f, g, h, a, &b, 0xbef9a3f7, w14 + sigma1(w12) + w7 + sigma0(w15));
|
||||
Round(b, c, d, &e, f, g, h, &a, 0xc67178f2, w15 + sigma1(w13) + w8 + sigma0(w0));
|
||||
|
||||
s[0] += a;
|
||||
s[1] += b;
|
||||
s[2] += c;
|
||||
s[3] += d;
|
||||
s[4] += e;
|
||||
s[5] += f;
|
||||
s[6] += g;
|
||||
s[7] += h;
|
||||
s[0] += a;
|
||||
s[1] += b;
|
||||
s[2] += c;
|
||||
s[3] += d;
|
||||
s[4] += e;
|
||||
s[5] += f;
|
||||
s[6] += g;
|
||||
s[7] += h;
|
||||
}
|
||||
|
||||
static bool alignment_ok(const void *p UNUSED, size_t n UNUSED)
|
||||
{
|
||||
#if HAVE_UNALIGNED_ACCESS
|
||||
return true;
|
||||
#else
|
||||
return ((size_t)p % n == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void add(struct sha256_ctx *ctx, const void *p, size_t len)
|
||||
{
|
||||
const unsigned char *data = p;
|
||||
size_t bufsize = ctx->bytes % 64;
|
||||
const unsigned char *data = p;
|
||||
size_t bufsize = ctx->bytes % 64;
|
||||
|
||||
if (bufsize + len >= 64) {
|
||||
/* Fill the buffer, and process it. */
|
||||
memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
|
||||
ctx->bytes += 64 - bufsize;
|
||||
data += 64 - bufsize;
|
||||
len -= 64 - bufsize;
|
||||
Transform(ctx->s, ctx->buf.u32);
|
||||
bufsize = 0;
|
||||
}
|
||||
if (bufsize + len >= 64) {
|
||||
/* Fill the buffer, and process it. */
|
||||
memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
|
||||
ctx->bytes += 64 - bufsize;
|
||||
data += 64 - bufsize;
|
||||
len -= 64 - bufsize;
|
||||
Transform(ctx->s, ctx->buf.u32);
|
||||
bufsize = 0;
|
||||
}
|
||||
|
||||
while (len >= 64) {
|
||||
/* Process full chunks directly from the source. */
|
||||
if (alignment_ok(data, sizeof(uint32_t)))
|
||||
Transform(ctx->s, (const uint32_t *)data);
|
||||
else {
|
||||
memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
|
||||
Transform(ctx->s, ctx->buf.u32);
|
||||
}
|
||||
ctx->bytes += 64;
|
||||
data += 64;
|
||||
len -= 64;
|
||||
}
|
||||
|
||||
if (len) {
|
||||
/* Fill the buffer with what remains. */
|
||||
memcpy(ctx->buf.u8 + bufsize, data, len);
|
||||
ctx->bytes += len;
|
||||
}
|
||||
while (len >= 64) {
|
||||
/* Process full chunks directly from the source. */
|
||||
if (alignment_ok(data, sizeof(uint32_t)))
|
||||
Transform(ctx->s, (const uint32_t *)data);
|
||||
else {
|
||||
memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
|
||||
Transform(ctx->s, ctx->buf.u32);
|
||||
}
|
||||
ctx->bytes += 64;
|
||||
data += 64;
|
||||
len -= 64;
|
||||
}
|
||||
|
||||
if (len) {
|
||||
/* Fill the buffer with what remains. */
|
||||
memcpy(ctx->buf.u8 + bufsize, data, len);
|
||||
ctx->bytes += len;
|
||||
}
|
||||
}
|
||||
|
||||
void sha256_init(struct sha256_ctx *ctx)
|
||||
{
|
||||
struct sha256_ctx init = SHA256_INIT;
|
||||
*ctx = init;
|
||||
struct sha256_ctx init = SHA256_INIT;
|
||||
*ctx = init;
|
||||
}
|
||||
|
||||
void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size)
|
||||
{
|
||||
check_sha256(ctx);
|
||||
add(ctx, p, size);
|
||||
check_sha256(ctx);
|
||||
add(ctx, p, size);
|
||||
}
|
||||
|
||||
void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
|
||||
{
|
||||
static const unsigned char pad[64] = {0x80};
|
||||
uint64_t sizedesc;
|
||||
size_t i;
|
||||
static const unsigned char pad[64] = {0x80};
|
||||
uint64_t sizedesc;
|
||||
size_t i;
|
||||
|
||||
sizedesc = cpu_to_be64((uint64_t)ctx->bytes << 3);
|
||||
/* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
|
||||
add(ctx, pad, 1 + ((128 - 8 - (ctx->bytes % 64) - 1) % 64));
|
||||
/* Add number of bits of data (big endian) */
|
||||
add(ctx, &sizedesc, 8);
|
||||
for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
|
||||
res->u.u32[i] = cpu_to_be32(ctx->s[i]);
|
||||
invalidate_sha256(ctx);
|
||||
sizedesc = cpu_to_be64((uint64_t)ctx->bytes << 3);
|
||||
/* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
|
||||
add(ctx, pad, 1 + ((128 - 8 - (ctx->bytes % 64) - 1) % 64));
|
||||
/* Add number of bits of data (big endian) */
|
||||
add(ctx, &sizedesc, 8);
|
||||
for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
|
||||
res->u.u32[i] = cpu_to_be32(ctx->s[i]);
|
||||
invalidate_sha256(ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
void sha256(struct sha256 *sha, const void *p, size_t size)
|
||||
{
|
||||
struct sha256_ctx ctx;
|
||||
struct sha256_ctx ctx;
|
||||
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, p, size);
|
||||
sha256_done(&ctx, sha);
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, p, size);
|
||||
sha256_done(&ctx, sha);
|
||||
}
|
||||
|
||||
|
||||
void sha256_u8(struct sha256_ctx *ctx, uint8_t v)
|
||||
{
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
}
|
||||
|
||||
void sha256_u16(struct sha256_ctx *ctx, uint16_t v)
|
||||
{
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
}
|
||||
|
||||
void sha256_u32(struct sha256_ctx *ctx, uint32_t v)
|
||||
{
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
}
|
||||
|
||||
void sha256_u64(struct sha256_ctx *ctx, uint64_t v)
|
||||
{
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
sha256_update(ctx, &v, sizeof(v));
|
||||
}
|
||||
|
||||
/* Add as little-endian */
|
||||
void sha256_le16(struct sha256_ctx *ctx, uint16_t v)
|
||||
{
|
||||
leint16_t lev = cpu_to_le16(v);
|
||||
sha256_update(ctx, &lev, sizeof(lev));
|
||||
leint16_t lev = cpu_to_le16(v);
|
||||
sha256_update(ctx, &lev, sizeof(lev));
|
||||
}
|
||||
|
||||
|
||||
void sha256_le32(struct sha256_ctx *ctx, uint32_t v)
|
||||
{
|
||||
leint32_t lev = cpu_to_le32(v);
|
||||
sha256_update(ctx, &lev, sizeof(lev));
|
||||
leint32_t lev = cpu_to_le32(v);
|
||||
sha256_update(ctx, &lev, sizeof(lev));
|
||||
}
|
||||
|
||||
|
||||
void sha256_le64(struct sha256_ctx *ctx, uint64_t v)
|
||||
{
|
||||
leint64_t lev = cpu_to_le64(v);
|
||||
sha256_update(ctx, &lev, sizeof(lev));
|
||||
leint64_t lev = cpu_to_le64(v);
|
||||
sha256_update(ctx, &lev, sizeof(lev));
|
||||
}
|
||||
|
||||
/* Add as big-endian */
|
||||
void sha256_be16(struct sha256_ctx *ctx, uint16_t v)
|
||||
{
|
||||
beint16_t bev = cpu_to_be16(v);
|
||||
sha256_update(ctx, &bev, sizeof(bev));
|
||||
beint16_t bev = cpu_to_be16(v);
|
||||
sha256_update(ctx, &bev, sizeof(bev));
|
||||
}
|
||||
|
||||
|
||||
void sha256_be32(struct sha256_ctx *ctx, uint32_t v)
|
||||
{
|
||||
beint32_t bev = cpu_to_be32(v);
|
||||
sha256_update(ctx, &bev, sizeof(bev));
|
||||
beint32_t bev = cpu_to_be32(v);
|
||||
sha256_update(ctx, &bev, sizeof(bev));
|
||||
}
|
||||
|
||||
|
||||
void sha256_be64(struct sha256_ctx *ctx, uint64_t v)
|
||||
{
|
||||
beint64_t bev = cpu_to_be64(v);
|
||||
sha256_update(ctx, &bev, sizeof(bev));
|
||||
beint64_t bev = cpu_to_be64(v);
|
||||
sha256_update(ctx, &bev, sizeof(bev));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
|
||||
#ifndef CCAN_CRYPTO_SHA256_H
|
||||
#define CCAN_CRYPTO_SHA256_H
|
||||
|
||||
|
||||
/** Output length for `wally_sha256` */
|
||||
#define SHA256_LEN 32
|
||||
|
||||
|
||||
/* BSD-MIT - see LICENSE file for details */
|
||||
#include "config.h"
|
||||
/* #include "config.h" */
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -20,17 +27,17 @@
|
||||
* Other fields may be added to the union in future.
|
||||
*/
|
||||
struct sha256 {
|
||||
union {
|
||||
uint32_t u32[8];
|
||||
unsigned char u8[32];
|
||||
} u;
|
||||
union {
|
||||
uint32_t u32[8];
|
||||
unsigned char u8[32];
|
||||
} u;
|
||||
};
|
||||
|
||||
/**
|
||||
* sha256 - return sha256 of an object.
|
||||
* @sha256: the sha256 to fill in
|
||||
* @p: pointer to memory,
|
||||
* @size: the number of bytes pointed to by
|
||||
* @size: the number of bytes pointed to by @p
|
||||
*
|
||||
* The bytes pointed to by @p is SHA256 hashed into @sha256. This is
|
||||
* equivalent to sha256_init(), sha256_update() then sha256_done().
|
||||
@@ -42,14 +49,14 @@ void sha256(struct sha256 *sha, const void *p, size_t size);
|
||||
*/
|
||||
struct sha256_ctx {
|
||||
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
|
||||
SHA256_CTX c;
|
||||
SHA256_CTX c;
|
||||
#else
|
||||
uint32_t s[8];
|
||||
union {
|
||||
uint32_t u32[16];
|
||||
unsigned char u8[64];
|
||||
} buf;
|
||||
size_t bytes;
|
||||
uint32_t s[8];
|
||||
union {
|
||||
uint32_t u32[16];
|
||||
unsigned char u8[64];
|
||||
} buf;
|
||||
size_t bytes;
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -66,13 +73,13 @@ struct sha256_ctx {
|
||||
* Example:
|
||||
* static void hash_all(const char **arr, struct sha256 *hash)
|
||||
* {
|
||||
* size_t i;
|
||||
* struct sha256_ctx ctx;
|
||||
* size_t i;
|
||||
* struct sha256_ctx ctx;
|
||||
*
|
||||
* sha256_init(&ctx);
|
||||
* for (i = 0; arr[i]; i++)
|
||||
* sha256_update(&ctx, arr[i], strlen(arr[i]));
|
||||
* sha256_done(&ctx, hash);
|
||||
* sha256_init(&ctx);
|
||||
* for (i = 0; arr[i]; i++)
|
||||
* sha256_update(&ctx, arr[i], strlen(arr[i]));
|
||||
* sha256_done(&ctx, hash);
|
||||
* }
|
||||
*/
|
||||
void sha256_init(struct sha256_ctx *ctx);
|
||||
@@ -86,33 +93,33 @@ void sha256_init(struct sha256_ctx *ctx);
|
||||
* Example:
|
||||
* static void hash_all(const char **arr, struct sha256 *hash)
|
||||
* {
|
||||
* size_t i;
|
||||
* struct sha256_ctx ctx = SHA256_INIT;
|
||||
* size_t i;
|
||||
* struct sha256_ctx ctx = SHA256_INIT;
|
||||
*
|
||||
* for (i = 0; arr[i]; i++)
|
||||
* sha256_update(&ctx, arr[i], strlen(arr[i]));
|
||||
* sha256_done(&ctx, hash);
|
||||
* for (i = 0; arr[i]; i++)
|
||||
* sha256_update(&ctx, arr[i], strlen(arr[i]));
|
||||
* sha256_done(&ctx, hash);
|
||||
* }
|
||||
*/
|
||||
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
|
||||
#define SHA256_INIT \
|
||||
{ { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
|
||||
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
|
||||
0x0, 0x0, \
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
|
||||
0x0, 0x20 } }
|
||||
#define SHA256_INIT \
|
||||
{ { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
|
||||
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
|
||||
0x0, 0x0, \
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
|
||||
0x0, 0x20 } }
|
||||
#else
|
||||
#define SHA256_INIT \
|
||||
{ { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
|
||||
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
|
||||
{ { 0 } }, 0 }
|
||||
#define SHA256_INIT \
|
||||
{ { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
|
||||
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
|
||||
{ { 0 } }, 0 }
|
||||
#endif
|
||||
|
||||
/**
|
||||
* sha256_update - include some memory in the hash.
|
||||
* @ctx: the sha256_ctx to use
|
||||
* @p: pointer to memory,
|
||||
* @size: the number of bytes pointed to by
|
||||
* @size: the number of bytes pointed to by @p
|
||||
*
|
||||
* You can call this multiple times to hash more data, before calling
|
||||
* sha256_done().
|
||||
@@ -144,4 +151,5 @@ void sha256_le64(struct sha256_ctx *ctx, uint64_t v);
|
||||
void sha256_be16(struct sha256_ctx *ctx, uint16_t v);
|
||||
void sha256_be32(struct sha256_ctx *ctx, uint32_t v);
|
||||
void sha256_be64(struct sha256_ctx *ctx, uint64_t v);
|
||||
|
||||
#endif /* CCAN_CRYPTO_SHA256_H */
|
||||
|
||||
1009
nostrdb/test.c
Normal file
1009
nostrdb/test.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -87,7 +87,7 @@ static inline void threadpool_destroy(struct threadpool *tp)
|
||||
{
|
||||
struct thread *t;
|
||||
|
||||
for (uint64_t i = 0; i < tp->num_threads; i++) {
|
||||
for (int i = 0; i < tp->num_threads; i++) {
|
||||
t = &tp->pool[i];
|
||||
if (!prot_queue_push(&t->inbox, tp->quit_msg)) {
|
||||
pthread_exit(&t->thread_id);
|
||||
|
||||
Reference in New Issue
Block a user