damus

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

commit e50c8f0dbc1fd13cf525960ede18c4fad3f10714
parent 2920325639b4ae8bd4cf51a0835d2c06c581d10f
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 21 May 2022 21:07:33 -0700

account creation working

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 4++++
Mdamus/ContentView.swift | 1+
Mdamus/Models/CreateAccountModel.swift | 4++++
Mdamus/Nostr/NostrEvent.swift | 27+++++++++++++++++++++++++++
Adamus/Nostr/NostrMetadata.swift | 20++++++++++++++++++++
Mdamus/Views/SaveKeysView.swift | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
6 files changed, 115 insertions(+), 4 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -62,6 +62,7 @@ 4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; }; 4C7FF7D52823313F009601DB /* Mentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7FF7D42823313F009601DB /* Mentions.swift */; }; 4C8682872814DE470026224F /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C8682862814DE470026224F /* ProfileView.swift */; }; + 4C90BD162839DB54008EE7EF /* NostrMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C90BD152839DB54008EE7EF /* NostrMetadata.swift */; }; 4CA2EFA0280E37AC0044ACD8 /* TimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */; }; 4CACA9D5280C31E100D9BBE8 /* ReplyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CACA9D4280C31E100D9BBE8 /* ReplyView.swift */; }; 4CACA9DC280C38C000D9BBE8 /* Profiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CACA9DB280C38C000D9BBE8 /* Profiles.swift */; }; @@ -159,6 +160,7 @@ 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProofOfWork.swift; sourceTree = "<group>"; }; 4C7FF7D42823313F009601DB /* Mentions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mentions.swift; sourceTree = "<group>"; }; 4C8682862814DE470026224F /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; }; + 4C90BD152839DB54008EE7EF /* NostrMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrMetadata.swift; sourceTree = "<group>"; }; 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineView.swift; sourceTree = "<group>"; }; 4CACA9D4280C31E100D9BBE8 /* ReplyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyView.swift; sourceTree = "<group>"; }; 4CACA9DB280C38C000D9BBE8 /* Profiles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profiles.swift; sourceTree = "<group>"; }; @@ -285,6 +287,7 @@ 4CACA9DB280C38C000D9BBE8 /* Profiles.swift */, 4C3BEFD32819DE8F00B3DE84 /* NostrKind.swift */, 4C363A8F28247A1D006E126D /* NostrLink.swift */, + 4C90BD152839DB54008EE7EF /* NostrMetadata.swift */, ); path = Nostr; sourceTree = "<group>"; @@ -551,6 +554,7 @@ 4C3AC79F2833115300E1F516 /* FollowButtonView.swift in Sources */, 4C3BEFD22819DB9B00B3DE84 /* ProfileModel.swift in Sources */, 4C0A3F93280F66F5000448DE /* ReplyMap.swift in Sources */, + 4C90BD162839DB54008EE7EF /* NostrMetadata.swift in Sources */, 4C3AC7A12835A81400E1F516 /* SetupView.swift in Sources */, 4C285C8C28398BC7008A31F1 /* Keys.swift in Sources */, 4CACA9DC280C38C000D9BBE8 /* Profiles.swift in Sources */, diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -412,6 +412,7 @@ struct ContentView: View { let text_filter = NostrFilter.filter_kinds([1,5,6,7]) let profile_filter = NostrFilter.filter_profiles var contacts_filter = NostrFilter.filter_contacts + contacts_filter.authors = [self.pubkey] var filters = [text_filter, profile_filter, contacts_filter] diff --git a/damus/Models/CreateAccountModel.swift b/damus/Models/CreateAccountModel.swift @@ -22,6 +22,10 @@ class CreateAccountModel: ObservableObject { return real_name } + var keypair: Keypair { + return Keypair(pubkey: self.pubkey, privkey: self.privkey) + } + init() { let keypair = generate_new_keypair() self.pubkey = keypair.pubkey diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift @@ -349,6 +349,33 @@ func get_referenced_ids(tags: [[String]], key: String) -> [ReferencedId] { } } +func make_first_contact_event(keypair: Keypair) -> NostrEvent { + let rw_relay_info = RelayInfo(read: true, write: true) + let damus_relay = "wss://relay.damus.io" + let relays: [String: RelayInfo] = ["wss://relay.damus.io": rw_relay_info] + let relay_json = encode_json(relays)! + let damus_pubkey = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681" + let ev = NostrEvent(content: relay_json, + pubkey: keypair.pubkey, + kind: NostrKind.contacts.rawValue, + tags: [["p", damus_pubkey, damus_relay]]) + ev.calculate_id() + ev.sign(privkey: keypair.privkey) + return ev +} + +func make_metadata_event(keypair: Keypair, metadata: NostrMetadata) -> NostrEvent { + let metadata_json = encode_json(metadata)! + let ev = NostrEvent(content: metadata_json, + pubkey: keypair.pubkey, + kind: NostrKind.metadata.rawValue, + tags: []) + + ev.calculate_id() + ev.sign(privkey: keypair.privkey) + return ev +} + func make_boost_event(pubkey: String, privkey: String, 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]) diff --git a/damus/Nostr/NostrMetadata.swift b/damus/Nostr/NostrMetadata.swift @@ -0,0 +1,20 @@ +// +// NostrMetadata.swift +// damus +// +// Created by William Casarin on 2022-05-21. +// + +import Foundation + + +struct NostrMetadata: Codable { + let display_name: String? + let name: String? + let about: String? + let website: String? +} + +func create_account_to_metadata(_ model: CreateAccountModel) -> NostrMetadata { + return NostrMetadata(display_name: model.real_name, name: model.nick_name, about: model.about, website: nil) +} diff --git a/damus/Views/SaveKeysView.swift b/damus/Views/SaveKeysView.swift @@ -9,9 +9,12 @@ import SwiftUI struct SaveKeysView: View { let account: CreateAccountModel + let pool: RelayPool = RelayPool() @State var is_done: Bool = false @State var pub_copied: Bool = false @State var priv_copied: Bool = false + @State var loading: Bool = false + @State var error: String? = nil var body: some View { ZStack(alignment: .top) { @@ -32,7 +35,7 @@ struct SaveKeysView: View { .foregroundColor(.white) .padding(.bottom, 10) - Text("This is your account ID, you can give this to your friends so that they can follow you") + Text("This is your account ID, you can give this to your friends so that they can follow you. Click to copy.") .foregroundColor(.white) .padding(.bottom, 10) @@ -52,15 +55,67 @@ struct SaveKeysView: View { .padding(.bottom, 10) if pub_copied && priv_copied { - DamusWhiteButton("Let's go!") { - save_keypair(pubkey: account.pubkey, privkey: account.privkey) - notify(.login, ()) + if loading { + ProgressView() + .progressViewStyle(.circular) + } else if let err = error { + Text("Error: \(err)") + .foregroundColor(.red) + DamusWhiteButton("Retry") { + complete_account_creation(account) + } + } else { + DamusWhiteButton("Let's go!") { + complete_account_creation(account) + } } } } .padding(20) } } + + func complete_account_creation(_ account: CreateAccountModel) { + add_rw_relay(self.pool, "wss://relay.damus.io") + self.pool.register_handler(sub_id: "signup", handler: handle_event) + + self.loading = true + + self.pool.connect() + } + + func handle_event(relay: String, ev: NostrConnectionEvent) { + switch ev { + case .ws_event(let wsev): + switch wsev { + case .connected: + let metadata = create_account_to_metadata(account) + let metadata_ev = make_metadata_event(keypair: account.keypair, metadata: metadata) + let contacts_ev = make_first_contact_event(keypair: account.keypair) + + self.pool.send(.event(metadata_ev)) + self.pool.send(.event(contacts_ev)) + + save_keypair(pubkey: account.pubkey, privkey: account.privkey) + notify(.login, account.keypair) + case .error(let err): + self.loading = false + self.error = "\(err.debugDescription)" + default: + break + } + case .nostr_event(let resp): + switch resp { + case .notice(let msg): + // TODO handle message + self.loading = false + self.error = msg + print(msg) + case .event: + print("event in signup?") + } + } + } } struct SaveKeyView: View {