From 6d43754e715ecfc59c531e43598352a2430df917 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Fri, 21 Jul 2023 16:01:28 -0700 Subject: [PATCH] ndb: add pubkey to NdbNote --- damus.xcodeproj/project.pbxproj | 20 +++++++++---------- nostrdb/NdbNote.swift | 13 ++++++++---- .../NDB => nostrdb/Test}/NdbTests.swift | 2 ++ {damus-c => nostrdb}/jsmn.h | 0 4 files changed, 21 insertions(+), 14 deletions(-) rename {damusTests/NDB => nostrdb/Test}/NdbTests.swift (90%) rename {damus-c => nostrdb}/jsmn.h (100%) diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj index 4bd5b2ac..e14b3f91 100644 --- a/damus.xcodeproj/project.pbxproj +++ b/damus.xcodeproj/project.pbxproj @@ -901,7 +901,6 @@ 4C06670728FDE62900038D2A /* damus-c */ = { isa = PBXGroup; children = ( - 4CDD1AE72A6B3611001CD4DF /* jsmn.h */, 4C9146FF2A2A891E00DDEA40 /* error.c */, 4CA927752A2A5E2F0098A105 /* typedefs.h */, 4CA927742A2A5E2F0098A105 /* varint.h */, @@ -1312,17 +1311,10 @@ path = Buttons; sourceTree = ""; }; - 4C9054832A6AEA7B00811EEC /* NDB */ = { - isa = PBXGroup; - children = ( - 4C9054842A6AEAA000811EEC /* NdbTests.swift */, - ); - path = NDB; - sourceTree = ""; - }; 4C9054862A6AEB4500811EEC /* nostrdb */ = { isa = PBXGroup; children = ( + 4CE9FBBB2A6B3D9C007E485C /* Test */, 4C9054882A6AED4700811EEC /* NdbTagIterator.swift */, 4C90548A2A6AEDEE00811EEC /* NdbNote.swift */, 4C5D5C9C2A6B2CB40024563C /* AsciiCharacter.swift */, @@ -1330,6 +1322,7 @@ 4CDD1AE12A6B3074001CD4DF /* NdbTagsIterator.swift */, 4CE9FBB82A6B3B26007E485C /* nostrdb.c */, 4CE9FBB92A6B3B26007E485C /* nostrdb.h */, + 4CDD1AE72A6B3611001CD4DF /* jsmn.h */, ); path = nostrdb; sourceTree = ""; @@ -1548,7 +1541,6 @@ 4CE6DEF627F7A08200C66700 /* damusTests */ = { isa = PBXGroup; children = ( - 4C9054832A6AEA7B00811EEC /* NDB */, 4C9B0DEC2A65A74000CBDA21 /* Util */, 4C0C03962A61E2670098B3B8 /* Fixtures */, 4C7D097D2A0C58B900943473 /* WalletConnectTests.swift */, @@ -1618,6 +1610,14 @@ path = Zaps; sourceTree = ""; }; + 4CE9FBBB2A6B3D9C007E485C /* Test */ = { + isa = PBXGroup; + children = ( + 4C9054842A6AEAA000811EEC /* NdbTests.swift */, + ); + path = Test; + sourceTree = ""; + }; 4CEE2AE62804F57B00AB5EEF /* Frameworks */ = { isa = PBXGroup; children = ( diff --git a/nostrdb/NdbNote.swift b/nostrdb/NdbNote.swift index d603e78b..317a9643 100644 --- a/nostrdb/NdbNote.swift +++ b/nostrdb/NdbNote.swift @@ -8,11 +8,12 @@ import Foundation struct NdbNote { + // we can have owned notes, but we can also have lmdb virtual-memory mapped notes so its optional private var owned: Data? let note: UnsafeMutablePointer - init(notePointer: UnsafeMutablePointer, data: Data?) { - self.note = notePointer + init(note: UnsafeMutablePointer, data: Data?) { + self.note = note self.owned = data } @@ -20,6 +21,10 @@ struct NdbNote { Data(buffer: UnsafeBufferPointer(start: ndb_note_id(note), count: 32)) } + var pubkey: Data { + Data(buffer: UnsafeBufferPointer(start: ndb_note_pubkey(note), count: 32)) + } + func tags() -> TagsSequence { return .init(note: note) } @@ -37,6 +42,6 @@ struct NdbNote { guard let note else { return nil } // Create new Data with just the valid bytes - let validData = Data(bytes: ¬e.pointee, count: Int(len)) - return NdbNote(notePointer: note, data: validData) + let smol_data = Data(bytes: ¬e.pointee, count: Int(len)) + return NdbNote(note: note, data: smol_data) }} diff --git a/damusTests/NDB/NdbTests.swift b/nostrdb/Test/NdbTests.swift similarity index 90% rename from damusTests/NDB/NdbTests.swift rename to nostrdb/Test/NdbTests.swift index 1c440a04..3c16c6d3 100644 --- a/damusTests/NDB/NdbTests.swift +++ b/nostrdb/Test/NdbTests.swift @@ -24,7 +24,9 @@ final class NdbTests: XCTestCase { guard let note else { return } let id = "20d0ff27d6fcb13de8366328c5b1a7af26bcac07f2e558fbebd5e9242e608c09" + let pubkey = "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245" XCTAssertEqual(hex_encode(note.id), id) + XCTAssertEqual(hex_encode(note.pubkey), pubkey) XCTAssertEqual(note.tags().reduce(0, { sum, _ in sum + 1 }), 786) XCTAssertEqual(note.tags().reduce(0, { sum, _ in sum + 1 }), 786) diff --git a/damus-c/jsmn.h b/nostrdb/jsmn.h similarity index 100% rename from damus-c/jsmn.h rename to nostrdb/jsmn.h