1
0
mirror of git://jb55.com/damus synced 2024-09-19 19:46:51 +00:00

Initial NIP05 code

Also add the ability to login with a NIP05 id
This commit is contained in:
William Casarin 2022-11-18 17:51:01 -08:00
parent 937009167f
commit 7493641896
2 changed files with 119 additions and 44 deletions

View File

@ -9,7 +9,7 @@ import SwiftUI
import Starscream
import Kingfisher
let BOOTSTRAP_RELAYS = [
var BOOTSTRAP_RELAYS = [
"wss://relay.damus.io",
"wss://nostr-relay.wlvs.space",
"wss://nostr.oxtr.dev",

View File

@ -11,11 +11,16 @@ enum ParsedKey {
case pub(String)
case priv(String)
case hex(String)
case nip05(String)
var is_pub: Bool {
if case .pub = self {
return true
}
if case .nip05 = self {
return true
}
return false
}
@ -44,6 +49,55 @@ struct LoginView: View {
return nil
}
func process_login(_ key: ParsedKey, is_pubkey: Bool) -> Bool {
switch key {
case .priv(let priv):
save_privkey(privkey: priv)
guard let pk = privkey_to_pubkey(privkey: priv) else {
return false
}
save_pubkey(pubkey: pk)
case .pub(let pub):
clear_saved_privkey()
save_pubkey(pubkey: pub)
case .nip05(let id):
Task.init {
guard let nip05 = await get_nip05_pubkey(id: id) else {
self.error = "Could not fetch pubkey"
return
}
for relay in nip05.relays {
if !(BOOTSTRAP_RELAYS.contains { $0 == relay }) {
BOOTSTRAP_RELAYS.append(relay)
}
}
save_pubkey(pubkey: nip05.pubkey)
notify(.login, ())
}
case .hex(let hexstr):
if is_pubkey {
clear_saved_privkey()
save_pubkey(pubkey: hexstr)
} else {
save_privkey(privkey: hexstr)
guard let pk = privkey_to_pubkey(privkey: hexstr) else {
return false
}
save_pubkey(pubkey: pk)
}
}
notify(.login, ())
return true
}
var body: some View {
ZStack(alignment: .top) {
DamusGradient()
@ -113,10 +167,15 @@ func parse_key(_ thekey: String) -> ParsedKey? {
if key.count > 0 && key.first! == "@" {
key = String(key.dropFirst())
}
if hex_decode(key) != nil {
return .hex(key)
}
if (key.contains { $0 == "@" }) {
return .nip05(key)
}
if let bech_key = decode_bech32_key(key) {
switch bech_key {
case .pub(let pk):
@ -129,34 +188,50 @@ func parse_key(_ thekey: String) -> ParsedKey? {
return nil
}
func process_login(_ key: ParsedKey, is_pubkey: Bool) -> Bool {
switch key {
case .priv(let priv):
save_privkey(privkey: priv)
guard let pk = privkey_to_pubkey(privkey: priv) else {
return false
}
save_pubkey(pubkey: pk)
struct NIP05Result: Decodable {
let names: Dictionary<String, String>
let relays: Dictionary<String, [String]>?
}
case .pub(let pub):
clear_saved_privkey()
save_pubkey(pubkey: pub)
struct NIP05User {
let pubkey: String
let relays: [String]
}
case .hex(let hexstr):
if is_pubkey {
clear_saved_privkey()
save_pubkey(pubkey: hexstr)
} else {
save_privkey(privkey: hexstr)
guard let pk = privkey_to_pubkey(privkey: hexstr) else {
return false
func get_nip05_pubkey(id: String) async -> NIP05User? {
let parts = id.components(separatedBy: "@")
guard parts.count == 2 else {
return nil
}
save_pubkey(pubkey: pk)
let user = parts[0]
let host = parts[1]
guard let url = URL(string: "https://\(host)/.well-known/nostr.json?name=\(user)") else {
return nil
}
guard let (data, _) = try? await URLSession.shared.data(for: URLRequest(url: url)) else {
return nil
}
guard let json: NIP05Result = decode_data(data) else {
return nil
}
guard let pubkey = json.names[user] else {
return nil
}
var relays: [String] = []
if let rs = json.relays {
if let rs = rs[user] {
relays = rs
}
}
notify(.login, ())
return true
return NIP05User(pubkey: pubkey, relays: relays)
}
struct KeyInput: View {