From ecbfb3714b6622f4454a01cf0183e75705590884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Wed, 10 Sep 2025 16:07:03 -0700 Subject: [PATCH] Fix incompatibilities with new nostrdb version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel D’Aquino --- nostrdb/Ndb.swift | 4 +++- nostrdb/NdbNote.swift | 18 +++--------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/nostrdb/Ndb.swift b/nostrdb/Ndb.swift index f0de8f12..bbc73abb 100644 --- a/nostrdb/Ndb.swift +++ b/nostrdb/Ndb.swift @@ -34,6 +34,8 @@ class Ndb { var generation: Int private var closed: Bool private var callbackHandler: Ndb.CallbackHandler + + private static let DEFAULT_WRITER_SCRATCH_SIZE: Int32 = 2097152; // 2mb scratch size for the writer thread, it should match with the one specified in nostrdb.c var is_closed: Bool { self.closed || self.ndb.ndb == nil @@ -111,7 +113,7 @@ class Ndb { let ok = path.withCString { testdir in var ok = false while !ok && mapsize > 1024 * 1024 * 700 { - var cfg = ndb_config(flags: 0, ingester_threads: ingest_threads, mapsize: mapsize, filter_context: nil, ingest_filter: nil, sub_cb_ctx: nil, sub_cb: nil) + var cfg = ndb_config(flags: 0, ingester_threads: ingest_threads, writer_scratch_buffer_size: DEFAULT_WRITER_SCRATCH_SIZE, mapsize: mapsize, filter_context: nil, ingest_filter: nil, sub_cb_ctx: nil, sub_cb: nil) // Here we hook up the global callback function for subscription callbacks. // We do an "unretained" pass here because the lifetime of the callback handler is larger than the lifetime of the nostrdb monitor in the C code. diff --git a/nostrdb/NdbNote.swift b/nostrdb/NdbNote.swift index 2ce94553..553ccae1 100644 --- a/nostrdb/NdbNote.swift +++ b/nostrdb/NdbNote.swift @@ -303,12 +303,10 @@ class NdbNote: Codable, Equatable, Hashable { let scratch_buf = malloc(scratch_buf_len) defer { free(scratch_buf) } // Ensure we deallocate as soon as we leave this scope, regardless of the outcome - // Calculate the ID based on the content - guard ndb_calculate_id(n.ptr, scratch_buf, Int32(scratch_buf_len)) == 1 else { throw InitError.generic } - // Verify the signature against the pubkey and the computed ID, to verify the validity of the whole note var ctx = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_VERIFY)) - guard ndb_note_verify(&ctx, ndb_note_pubkey(n.ptr), ndb_note_id(n.ptr), ndb_note_sig(n.ptr)) == 1 else { throw InitError.generic } + + guard ndb_note_verify(&ctx, scratch_buf, scratch_buf_len, n.ptr) == 1 else { throw InitError.generic } } catch { free(buf) @@ -351,19 +349,9 @@ class NdbNote: Codable, Equatable, Hashable { let scratch_buf = malloc(scratch_buf_len) defer { free(scratch_buf) } // Ensure we deallocate as soon as we leave this scope, regardless of the outcome - let current_id = self.id - - // Calculate the ID based on the content - guard ndb_calculate_id(self.note.ptr, scratch_buf, Int32(scratch_buf_len)) == 1 else { return false } - - let computed_id = self.id - - // Ensure computed ID matches given id to prevent ID tampering - guard computed_id == current_id else { return false } - // Verify the signature against the pubkey and the computed ID, to verify the validity of the whole note var ctx = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_VERIFY)) - guard ndb_note_verify(&ctx, ndb_note_pubkey(self.note.ptr), ndb_note_id(self.note.ptr), ndb_note_sig(self.note.ptr)) == 1 else { return false } + guard ndb_note_verify(&ctx, scratch_buf, scratch_buf_len, self.note.ptr) == 1 else { return false } return true }