Implement NIP-21 URI handling

Changelog-Added: Added nostr: uri handling
This commit is contained in:
William Casarin
2023-01-28 08:30:06 -08:00
parent b6ea17a0eb
commit 552bd9cae5
8 changed files with 78 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ struct KeyEvent {
let relay_url: String
}
struct ReferencedId: Identifiable, Hashable {
struct ReferencedId: Identifiable, Hashable, Equatable {
let ref_id: String
let relay_id: String?
let key: String

View File

@@ -7,7 +7,7 @@
import Foundation
struct NostrFilter: Codable {
struct NostrFilter: Codable, Equatable {
var ids: [String]?
var kinds: [Int]?
var referenced_ids: [String]?

View File

@@ -8,7 +8,7 @@
import Foundation
enum NostrLink {
enum NostrLink: Equatable {
case ref(ReferencedId)
case filter(NostrFilter)
}
@@ -101,6 +101,24 @@ func decode_universal_link(_ s: String) -> NostrLink? {
return nil
}
func decode_nostr_bech32_uri(_ s: String) -> NostrLink? {
guard let obj = Bech32Object.parse(s) else {
return nil
}
switch obj {
case .nsec(let privkey):
guard let pubkey = privkey_to_pubkey(privkey: privkey) else {
return nil
}
return .ref(ReferencedId(ref_id: pubkey, relay_id: nil, key: "p"))
case .npub(let pubkey):
return .ref(ReferencedId(ref_id: pubkey, relay_id: nil, key: "p"))
case .note(let id):
return .ref(ReferencedId(ref_id: id, relay_id: nil, key: "e"))
}
}
func decode_nostr_uri(_ s: String) -> NostrLink? {
if s.starts(with: "https://damus.io/") {
return decode_universal_link(s)
@@ -122,5 +140,15 @@ func decode_nostr_uri(_ s: String) -> NostrLink? {
return .filter(NostrFilter.filter_hashtag([parts[1].lowercased()]))
}
return tag_to_refid(parts).map { .ref($0) }
if let rid = tag_to_refid(parts) {
return .ref(rid)
}
guard parts.count == 1 else {
return nil
}
let part = parts[0]
return decode_nostr_bech32_uri(part)
}

View File

@@ -0,0 +1,31 @@
//
// Bech32Object.swift
// damus
//
// Created by William Casarin on 2023-01-28.
//
import Foundation
enum Bech32Object {
case nsec(String)
case npub(String)
case note(String)
static func parse(_ str: String) -> Bech32Object? {
guard let decoded = try? bech32_decode(str) else {
return nil
}
if decoded.hrp == "npub" {
return .npub(hex_encode(decoded.data))
} else if decoded.hrp == "nsec" {
return .nsec(hex_encode(decoded.data))
} else if decoded.hrp == "note" {
return .note(hex_encode(decoded.data))
}
return nil
}
}

View File

@@ -24,7 +24,7 @@ func create_or_update_list_event(keypair: FullKeypair, mprev: NostrEvent?, to_ad
}
}
var tags = [["d", list_name], [list_type, to_add]]
let tags = [["d", list_name], [list_type, to_add]]
let ev = NostrEvent(content: "", pubkey: pubkey, kind: 30000, tags: tags)
ev.tags = tags

View File

@@ -71,7 +71,7 @@ struct EventDetailView: View {
}
toggle_thread_view()
}
case .event(let ev, let _):
case .event(let ev, _):
EventView(damus: damus, event: ev, has_action_bar: true)
.onTapGesture {
if thread.initial_event.id == ev.id {