diff --git a/damus/Components/TranslateView.swift b/damus/Components/TranslateView.swift index 4465f97c..104ba560 100644 --- a/damus/Components/TranslateView.swift +++ b/damus/Components/TranslateView.swift @@ -95,7 +95,7 @@ struct TranslateView: View { if #available(iOS 16, *) { noteLanguage = Locale.LanguageCode(stringLiteral: lang).identifier(.alpha2) } else { - noteLanguage = Locale.canonicalLanguageIdentifier(from: lang) + noteLanguage = NSLocale(localeIdentifier: lang).languageCode } } diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift index 8a1f1ce1..5e2c8dc0 100644 --- a/damus/Views/DMChatView.swift +++ b/damus/Views/DMChatView.swift @@ -12,6 +12,7 @@ struct DMChatView: View { let pubkey: String @EnvironmentObject var dms: DirectMessageModel @State var message: String = "" + @State var showPrivateKeyWarning: Bool = false var Messages: some View { ScrollViewReader { scroller in @@ -93,7 +94,21 @@ struct DMChatView: View { InputField if !message.isEmpty { - Button(role: .none, action: send_message) { + Button( + role: .none, + action: { + if #available(iOS 16.0, *) { + showPrivateKeyWarning = message.contains(/nsec1[02-9ac-z]+/) + } else { + let regex = try! NSRegularExpression(pattern: "nsec1[02-9ac-z]+") + showPrivateKeyWarning = (regex.firstMatch(in: message, range: NSRange(location: 0, length: message.count)) != nil) + } + + if !showPrivateKeyWarning { + send_message() + } + } + ) { Label("", systemImage: "arrow.right.circle") .font(.title) } @@ -147,6 +162,14 @@ struct DMChatView: View { } .navigationTitle(NSLocalizedString("DMs", comment: "Navigation title for DMs view, where DM is the English abbreviation for Direct Message.")) .toolbar { Header } + .alert(NSLocalizedString("Note contains \"nsec1\" private key. Are you sure?", comment: "Alert user that they might be attempting to paste a private key and ask them to confirm."), isPresented: $showPrivateKeyWarning, actions: { + Button(NSLocalizedString("No", comment: "Button to cancel out of posting a note after being alerted that it looks like they might be posting a private key."), role: .cancel) { + showPrivateKeyWarning = false + } + Button(NSLocalizedString("Yes, Post with Private Key", comment: "Button to proceed with posting a note even though it looks like they might be posting a private key."), role: .destructive) { + send_message() + } + }) } } diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 3e6d587b..dd1c01cd 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -17,6 +17,7 @@ let POST_PLACEHOLDER = NSLocalizedString("Type your post here...", comment: "Tex struct PostView: View { @State var post: String = "" @FocusState var focus: Bool + @State var showPrivateKeyWarning: Bool = false let replying_to: NostrEvent? let references: [ReferencedId] @@ -65,7 +66,16 @@ struct PostView: View { if !is_post_empty { Button(NSLocalizedString("Post", comment: "Button to post a note.")) { - self.send_post() + if #available(iOS 16.0, *) { + showPrivateKeyWarning = self.post.contains(/nsec1[02-9ac-z]+/) + } else { + let regex = try! NSRegularExpression(pattern: "nsec1[02-9ac-z]+") + showPrivateKeyWarning = (regex.firstMatch(in: self.post, range: NSRange(location: 0, length: self.post.count)) != nil) + } + + if !showPrivateKeyWarning { + self.send_post() + } } } } @@ -99,6 +109,14 @@ struct PostView: View { } } .padding() + .alert(NSLocalizedString("Note contains \"nsec1\" private key. Are you sure?", comment: "Alert user that they might be attempting to paste a private key and ask them to confirm."), isPresented: $showPrivateKeyWarning, actions: { + Button(NSLocalizedString("No", comment: "Button to cancel out of posting a note after being alerted that it looks like they might be posting a private key."), role: .cancel) { + showPrivateKeyWarning = false + } + Button(NSLocalizedString("Yes, Post with Private Key", comment: "Button to proceed with posting a note even though it looks like they might be posting a private key."), role: .destructive) { + self.send_post() + } + }) } }