refactor: add Pubkey, Privkey, NoteId string aliases

This is a non-behavioral change in preparation for the actual switchover
from Strings to Ids. The purpose of this kit is to reduce the size of
the switchover commit which is going to be very large.
This commit is contained in:
William Casarin
2023-07-31 03:57:26 -07:00
parent f9d21ef901
commit 7040235605
101 changed files with 427 additions and 426 deletions

View File

@@ -201,7 +201,7 @@ class Profile: Codable {
try container.encode(value)
}
static func displayName(profile: Profile?, pubkey: String) -> DisplayName {
static func displayName(profile: Profile?, pubkey: Pubkey) -> DisplayName {
return parse_display_name(profile: profile, pubkey: pubkey)
}
}

View File

@@ -379,7 +379,7 @@ func decode_data<T: Decodable>(_ data: Data) -> T? {
return nil
}
func event_commitment(pubkey: String, created_at: UInt32, kind: UInt32, tags: [[String]], content: String) -> String {
func event_commitment(pubkey: Pubkey, created_at: UInt32, kind: UInt32, tags: [[String]], content: String) -> String {
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
let str_data = try! encoder.encode(content)
@@ -393,12 +393,12 @@ func event_commitment(pubkey: String, created_at: UInt32, kind: UInt32, tags: [[
return "[0,\"\(pubkey)\",\(created_at),\(kind),\(tags),\(content)]"
}
func calculate_event_commitment(pubkey: String, created_at: UInt32, kind: UInt32, tags: [[String]], content: String) -> Data {
func calculate_event_commitment(pubkey: Pubkey, created_at: UInt32, kind: UInt32, tags: [[String]], content: String) -> Data {
let target = event_commitment(pubkey: pubkey, created_at: created_at, kind: kind, tags: tags, content: content)
return target.data(using: .utf8)!
}
func calculate_event_id(pubkey: String, created_at: UInt32, kind: UInt32, tags: [[String]], content: String) -> Data {
func calculate_event_id(pubkey: Pubkey, created_at: UInt32, kind: UInt32, tags: [[String]], content: String) -> Data {
let commitment = calculate_event_commitment(pubkey: pubkey, created_at: created_at, kind: kind, tags: tags, content: content)
return sha256(commitment)
}
@@ -497,7 +497,7 @@ func make_first_contact_event(keypair: Keypair) -> NostrEvent? {
let damus_pubkey = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"
let tags = [
["p", damus_pubkey],
["p", keypair.pubkey] // you're a friend of yourself!
["p", keypair.pubkey.hex()] // you're a friend of yourself!
]
return NostrEvent(content: relay_json, keypair: keypair, kind: NostrKind.contacts.rawValue, tags: tags)
}
@@ -514,24 +514,25 @@ func make_boost_event(keypair: FullKeypair, boosted: NostrEvent) -> NostrEvent?
var tags: [[String]] = boosted.tags.filter { tag in tag.count >= 2 && (tag[0] == "e" || tag[0] == "p") }
tags.append(["e", boosted.id, "", "root"])
tags.append(["p", boosted.pubkey])
tags.append(["p", boosted.pubkey.hex()])
return NostrEvent(content: event_to_json(ev: boosted), keypair: keypair.to_keypair(), kind: 6, tags: tags)
}
func make_like_event(keypair: FullKeypair, liked: NostrEvent, content: String = "🤙") -> NostrEvent? {
var tags: [[String]] = liked.tags.filter { tag in tag.count >= 2 && (tag[0] == "e" || tag[0] == "p") }
tags.append(["e", liked.id])
tags.append(["p", liked.pubkey])
tags.append(["e", liked.id.hex()])
tags.append(["p", liked.pubkey.hex()])
return NostrEvent(content: content, keypair: keypair.to_keypair(), kind: 7, tags: tags)
}
func zap_target_to_tags(_ target: ZapTarget) -> [[String]] {
switch target {
case .profile(let pk):
return [["p", pk]]
return [["p", pk.hex()]]
case .note(let note_target):
return [["e", note_target.note_id], ["p", note_target.author]]
return [["e", note_target.note_id.hex()],
["p", note_target.author.hex()]]
}
}
@@ -554,7 +555,7 @@ func make_private_zap_request_event(identity: FullKeypair, enc_key: FullKeypair,
return PrivateZapRequest(req: ZapRequest(ev: note), enc: enc)
}
func decrypt_private_zap(our_privkey: String, zapreq: NostrEvent, target: ZapTarget) -> NostrEvent? {
func decrypt_private_zap(our_privkey: Privkey, zapreq: NostrEvent, target: ZapTarget) -> NostrEvent? {
guard let anon_tag = zapreq.tags.first(where: { t in t.count >= 2 && t[0] == "anon" }) else {
return nil
}
@@ -732,7 +733,7 @@ func event_to_json(ev: NostrEvent) -> String {
return str
}
func decrypt_dm(_ privkey: String?, pubkey: String, content: String, encoding: EncEncoding) -> String? {
func decrypt_dm(_ privkey: Privkey?, pubkey: Pubkey, content: String, encoding: EncEncoding) -> String? {
guard let privkey = privkey else {
return nil
}
@@ -748,7 +749,7 @@ func decrypt_dm(_ privkey: String?, pubkey: String, content: String, encoding: E
return String(data: dat, encoding: .utf8)
}
func decrypt_note(our_privkey: String, their_pubkey: String, enc_note: String, encoding: EncEncoding) -> NostrEvent? {
func decrypt_note(our_privkey: Privkey, their_pubkey: Pubkey, enc_note: String, encoding: EncEncoding) -> NostrEvent? {
guard let dec = decrypt_dm(our_privkey, pubkey: their_pubkey, content: enc_note, encoding: encoding) else {
return nil
}

View File

@@ -8,14 +8,14 @@
import Foundation
struct NostrFilter: Codable, Equatable {
var ids: [String]?
var ids: [NoteId]?
var kinds: [NostrKind]?
var referenced_ids: [String]?
var pubkeys: [String]?
var referenced_ids: [NoteId]?
var pubkeys: [Pubkey]?
var since: UInt32?
var until: UInt32?
var limit: UInt32?
var authors: [String]?
var authors: [Pubkey]?
var hashtag: [String]?
var parameter: [String]?
@@ -32,7 +32,7 @@ struct NostrFilter: Codable, Equatable {
case limit
}
init(ids: [String]? = nil, kinds: [NostrKind]? = nil, referenced_ids: [String]? = nil, pubkeys: [String]? = nil, since: UInt32? = nil, until: UInt32? = nil, limit: UInt32? = nil, authors: [String]? = nil, hashtag: [String]? = nil) {
init(ids: [NoteId]? = nil, kinds: [NostrKind]? = nil, referenced_ids: [NoteId]? = nil, pubkeys: [Pubkey]? = nil, since: UInt32? = nil, until: UInt32? = nil, limit: UInt32? = nil, authors: [Pubkey]? = nil, hashtag: [String]? = nil) {
self.ids = ids
self.kinds = kinds
self.referenced_ids = referenced_ids

View File

@@ -8,7 +8,7 @@
import Foundation
struct CommandResult {
let event_id: String
let event_id: NoteId
let ok: Bool
let msg: String
}

View File

@@ -25,8 +25,8 @@ final class ProfileDatabase {
private var queue = DispatchQueue(label: "io.damus.profile_db",
qos: .userInteractive,
attributes: .concurrent)
private var network_pull_date_cache = [String: Date]()
private var network_pull_date_cache = [Pubkey: Date]()
init(cache_url: URL = ProfileDatabase.profile_cache_url) {
self.cache_url = cache_url
set_up()
@@ -73,14 +73,14 @@ final class ProfileDatabase {
background_context?.mergePolicy = NSMergePolicy(merge: .mergeByPropertyObjectTrumpMergePolicyType)
}
private func get_persisted(id: String, context: NSManagedObjectContext) -> PersistedProfile? {
private func get_persisted(id: Pubkey, context: NSManagedObjectContext) -> PersistedProfile? {
let request = NSFetchRequest<PersistedProfile>(entityName: entity_name)
request.predicate = NSPredicate(format: "id == %@", id)
request.predicate = NSPredicate(format: "id == %@", id.hex())
request.fetchLimit = 1
return try? context.fetch(request).first
}
func get_network_pull_date(id: String) -> Date? {
func get_network_pull_date(id: Pubkey) -> Date? {
var pull_date: Date?
queue.sync {
pull_date = network_pull_date_cache[id]
@@ -90,7 +90,7 @@ final class ProfileDatabase {
}
let request = NSFetchRequest<PersistedProfile>(entityName: entity_name)
request.predicate = NSPredicate(format: "id == %@", id)
request.predicate = NSPredicate(format: "id == %@", id.hex())
request.fetchLimit = 1
request.propertiesToFetch = ["network_pull_date"]
guard let profile = try? persistent_container?.viewContext.fetch(request).first else {
@@ -111,7 +111,7 @@ final class ProfileDatabase {
/// - id: Profile id (pubkey)
/// - profile: Profile object to be stored
/// - last_update: Date that the Profile was updated
func upsert(id: String, profile: Profile, last_update: Date) async throws {
func upsert(id: Pubkey, profile: Profile, last_update: Date) async throws {
guard let context = background_context else {
throw ProfileDatabaseError.missing_context
}
@@ -126,7 +126,7 @@ final class ProfileDatabase {
}
} else {
persisted_profile = NSEntityDescription.insertNewObject(forEntityName: self.entity_name, into: context) as? PersistedProfile
persisted_profile?.id = id
persisted_profile?.id = id.hex()
}
persisted_profile?.copyValues(from: profile)
persisted_profile?.last_update = last_update
@@ -141,7 +141,7 @@ final class ProfileDatabase {
}
}
func get(id: String) -> Profile? {
func get(id: Pubkey) -> Profile? {
guard let container = persistent_container,
let profile = get_persisted(id: id, context: container.viewContext) else {
return nil

View File

@@ -21,11 +21,11 @@ class Profiles {
qos: .userInteractive,
attributes: .concurrent)
private var profiles: [String: TimestampedProfile] = [:]
private var validated: [String: NIP05] = [:]
var nip05_pubkey: [String: String] = [:]
var zappers: [String: String] = [:]
private var profiles: [Pubkey: TimestampedProfile] = [:]
private var validated: [Pubkey: NIP05] = [:]
var nip05_pubkey: [String: Pubkey] = [:]
var zappers: [Pubkey: Pubkey] = [:]
private let database = ProfileDatabase()
let user_search_cache: UserSearchCache
@@ -34,35 +34,35 @@ class Profiles {
self.user_search_cache = user_search_cache
}
func is_validated(_ pk: String) -> NIP05? {
func is_validated(_ pk: Pubkey) -> NIP05? {
validated_queue.sync {
validated[pk]
}
}
func invalidate_nip05(_ pk: String) {
func invalidate_nip05(_ pk: Pubkey) {
validated_queue.async(flags: .barrier) {
self.validated.removeValue(forKey: pk)
}
}
func set_validated(_ pk: String, nip05: NIP05?) {
func set_validated(_ pk: Pubkey, nip05: NIP05?) {
validated_queue.async(flags: .barrier) {
self.validated[pk] = nip05
}
}
func enumerated() -> EnumeratedSequence<[String: TimestampedProfile]> {
func enumerated() -> EnumeratedSequence<[Pubkey: TimestampedProfile]> {
return profiles_queue.sync {
return profiles.enumerated()
}
}
func lookup_zapper(pubkey: String) -> String? {
func lookup_zapper(pubkey: Pubkey) -> Pubkey? {
zappers[pubkey]
}
func add(id: String, profile: TimestampedProfile) {
func add(id: Pubkey, profile: TimestampedProfile) {
profiles_queue.async(flags: .barrier) {
let old_timestamped_profile = self.profiles[id]
self.profiles[id] = profile
@@ -78,7 +78,7 @@ class Profiles {
}
}
func lookup(id: String) -> Profile? {
func lookup(id: Pubkey) -> Profile? {
var profile: Profile?
profiles_queue.sync {
profile = profiles[id]?.profile
@@ -86,14 +86,13 @@ class Profiles {
return profile ?? database.get(id: id)
}
func lookup_with_timestamp(id: String) -> TimestampedProfile? {
func lookup_with_timestamp(id: Pubkey) -> TimestampedProfile? {
profiles_queue.sync {
return profiles[id]
}
}
func has_fresh_profile(id: String) -> Bool {
// check memory first
func has_fresh_profile(id: Pubkey) -> Bool {
var profile: Profile?
profiles_queue.sync {
profile = profiles[id]?.profile
@@ -111,7 +110,7 @@ class Profiles {
}
func invalidate_zapper_cache(pubkey: String, profiles: Profiles, lnurl: LNUrls) {
func invalidate_zapper_cache(pubkey: Pubkey, profiles: Profiles, lnurl: LNUrls) {
profiles.zappers.removeValue(forKey: pubkey)
lnurl.endpoints.removeValue(forKey: pubkey)
}

View File

@@ -68,7 +68,7 @@ struct Limitations: Codable {
struct RelayMetadata: Codable {
let name: String?
let description: String?
let pubkey: String?
let pubkey: Pubkey?
let contact: String?
let supported_nips: [Int]?
let software: String?

View File

@@ -21,7 +21,7 @@ struct QueuedRequest {
struct SeenEvent: Hashable {
let relay_id: String
let evid: String
let evid: NoteId
}
class RelayPool {