1
0
mirror of git://jb55.com/damus synced 2024-09-30 00:40:45 +00:00

ndb: add pubkey to NdbNote

This commit is contained in:
William Casarin 2023-07-21 16:01:28 -07:00
parent 4da23390f8
commit 6d43754e71
4 changed files with 21 additions and 14 deletions

View File

@ -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 = (

View File

@ -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)
}}

View File

@ -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)