damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 6d43754e715ecfc59c531e43598352a2430df917
parent 4da23390f81f1ea8b986c4e49f222e76926df95f
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 21 Jul 2023 16:01:28 -0700

ndb: add pubkey to NdbNote

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 20++++++++++----------
DdamusTests/NDB/NdbTests.swift | 48------------------------------------------------
Mnostrdb/NdbNote.swift | 13+++++++++----
Anostrdb/Test/NdbTests.swift | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Rdamus-c/jsmn.h -> nostrdb/jsmn.h | 0
5 files changed, 69 insertions(+), 62 deletions(-)

diff --git 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 = "<group>"; }; - 4C9054832A6AEA7B00811EEC /* NDB */ = { - isa = PBXGroup; - children = ( - 4C9054842A6AEAA000811EEC /* NdbTests.swift */, - ); - path = NDB; - sourceTree = "<group>"; - }; 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 = "<group>"; @@ -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 = "<group>"; }; + 4CE9FBBB2A6B3D9C007E485C /* Test */ = { + isa = PBXGroup; + children = ( + 4C9054842A6AEAA000811EEC /* NdbTests.swift */, + ); + path = Test; + sourceTree = "<group>"; + }; 4CEE2AE62804F57B00AB5EEF /* Frameworks */ = { isa = PBXGroup; children = ( diff --git a/damusTests/NDB/NdbTests.swift b/damusTests/NDB/NdbTests.swift @@ -1,48 +0,0 @@ -// -// NDBIterTests.swift -// damusTests -// -// Created by William Casarin on 2023-07-21. -// - -import XCTest -@testable import damus - -final class NdbTests: XCTestCase { - - override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. - } - - override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. - } - - func test_ndb_note() throws { - let note = NdbNote.owned_from_json(json: test_contact_list_json) - XCTAssertNotNil(note) - guard let note else { return } - - let id = "20d0ff27d6fcb13de8366328c5b1a7af26bcac07f2e558fbebd5e9242e608c09" - XCTAssertEqual(hex_encode(note.id), id) - - XCTAssertEqual(note.tags().reduce(0, { sum, _ in sum + 1 }), 786) - XCTAssertEqual(note.tags().reduce(0, { sum, _ in sum + 1 }), 786) - - //let tags = note.tags() - for tag in note.tags() { - for elem in tag { - print("test_ndb_iterator \(elem.string())") - } - } - - } - - func testPerformanceExample() throws { - // This is an example of a performance test case. - self.measure { - // Put the code you want to measure the time of here. - } - } - -} diff --git 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<ndb_note> - init(notePointer: UnsafeMutablePointer<ndb_note>, data: Data?) { - self.note = notePointer + init(note: UnsafeMutablePointer<ndb_note>, 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: &note.pointee, count: Int(len)) - return NdbNote(notePointer: note, data: validData) + let smol_data = Data(bytes: &note.pointee, count: Int(len)) + return NdbNote(note: note, data: smol_data) }} diff --git a/nostrdb/Test/NdbTests.swift b/nostrdb/Test/NdbTests.swift @@ -0,0 +1,50 @@ +// +// NDBIterTests.swift +// damusTests +// +// Created by William Casarin on 2023-07-21. +// + +import XCTest +@testable import damus + +final class NdbTests: XCTestCase { + + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test_ndb_note() throws { + let note = NdbNote.owned_from_json(json: test_contact_list_json) + XCTAssertNotNil(note) + 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) + + //let tags = note.tags() + for tag in note.tags() { + for elem in tag { + print("test_ndb_iterator \(elem.string())") + } + } + + } + + func testPerformanceExample() throws { + // This is an example of a performance test case. + self.measure { + // Put the code you want to measure the time of here. + } + } + +} diff --git a/damus-c/jsmn.h b/nostrdb/jsmn.h