diff --git a/damus/Nostr/Nostr.swift b/damus/Nostr/Nostr.swift index 6526b88b..909cb3f6 100644 --- a/damus/Nostr/Nostr.swift +++ b/damus/Nostr/Nostr.swift @@ -57,13 +57,25 @@ struct Profile: Codable { set(s) { value["lud16"] = s } } + var lnurl: String? { + guard let addr = lud06 ?? lud16 else { + return nil; + } + + if addr.contains("@") { + return lnaddress_to_lnurl(addr); + } + + return addr; + } + var nip05: String? { get { return value["nip05"]; } set(s) { value["nip05"] = s } } var lightning_uri: URL? { - return make_ln_url(self.lud06) ?? make_ln_url(self.lud16) + return make_ln_url(self.lnurl) } init(from decoder: Decoder) throws { @@ -110,3 +122,17 @@ struct NostrSubscription { let sub_id: String let filter: NostrFilter } + +func lnaddress_to_lnurl(_ lnaddr: String) -> String? { + let parts = lnaddr.split(separator: "@") + guard parts.count == 2 else { + return nil + } + + let url = "https://\(parts[1])/.well-known/lnurlp/\(parts[0])"; + guard let dat = url.data(using: .utf8) else { + return nil + } + + return bech32_encode(hrp: "lnurl", Array(dat)) +} diff --git a/damus/Util/Keys.swift b/damus/Util/Keys.swift index 9bc318a8..6f797f83 100644 --- a/damus/Util/Keys.swift +++ b/damus/Util/Keys.swift @@ -133,7 +133,8 @@ func get_saved_pubkey() -> String? { } func get_saved_privkey() -> String? { - try? Vault.getPrivateKey(keychainConfiguration: DamusKeychainConfiguration()) + let mkey = try? Vault.getPrivateKey(keychainConfiguration: DamusKeychainConfiguration()); + return mkey.map { $0.trimmingCharacters(in: .whitespaces) } } fileprivate func removePrivateKeyFromUserDefaults() throws { diff --git a/damus/Views/EditMetadataView.swift b/damus/Views/EditMetadataView.swift index 9513a7d1..f5e9f735 100644 --- a/damus/Views/EditMetadataView.swift +++ b/damus/Views/EditMetadataView.swift @@ -84,8 +84,8 @@ struct EditMetadataView: View { website: website, nip05: nip05.isEmpty ? nil : nip05, picture: picture.isEmpty ? nil : picture, - lud06: ln.contains("@") ? ln : nil, - lud16: ln.contains("@") ? nil : ln + lud06: ln.contains("@") ? nil : ln, + lud16: ln.contains("@") ? ln : nil ); let m_metadata_ev = make_metadata_event(keypair: damus_state.keypair, metadata: metadata) diff --git a/damus/Views/ProfileView.swift b/damus/Views/ProfileView.swift index 9a78b527..b59af7be 100644 --- a/damus/Views/ProfileView.swift +++ b/damus/Views/ProfileView.swift @@ -126,7 +126,7 @@ struct ProfileView: View { //@EnvironmentObject var profile: ProfileModel - func LNButton(lud06: String?, lud16: String?) -> some View { + func LNButton(lud06: String?, lud16: String?, profile: Profile) -> some View { Button(action: { if let l = lud06 { inv = l @@ -139,6 +139,12 @@ struct ProfileView: View { .symbolRenderingMode(.palette) .font(.system(size: 34).weight(.thin)) .foregroundStyle(colorScheme == .light ? .black : .white, colorScheme == .light ? .black.opacity(0.1) : .white.opacity(0.2)) + }.contextMenu { + Button { + UIPasteboard.general.string = profile.lnurl ?? "" + } label: { + Label("Copy LNUrl", systemImage: "doc.on.doc") + } }.sheet(isPresented: $showingSelectWallet, onDismiss: {showingSelectWallet = false}) { SelectWalletView(showingSelectWallet: $showingSelectWallet, invoice: $inv) } @@ -164,9 +170,11 @@ struct ProfileView: View { ProfilePicView(pubkey: profile.pubkey, size: PFP_SIZE, highlight: .custom(Color.black, 2), profiles: damus_state.profiles) Spacer() - - if (data != nil) && (data?.lud06 != nil || data?.lud16 != nil) { - LNButton(lud06: data?.lud06, lud16: data?.lud16) + + if let profile = data { + if (profile?.lud06 != nil || profile?.lud16 != nil) { + LNButton(lud06: data?.lud06, lud16: data?.lud16,, profile: profile) + } } DMButton