Refactor private key detection logic
This commit is contained in:
@@ -158,6 +158,20 @@ func get_saved_privkey() -> String? {
|
||||
return mkey.map { $0.trimmingCharacters(in: .whitespaces) }
|
||||
}
|
||||
|
||||
/**
|
||||
Detects whether a string might contain an nsec1 prefixed private key.
|
||||
It does not determine if it's the current user's private key and does not verify if it is properly encoded or has the right length.
|
||||
*/
|
||||
func contentContainsPrivateKey(_ content: String) -> Bool {
|
||||
if #available(iOS 16.0, *) {
|
||||
return content.contains(/nsec1[02-9ac-z]+/)
|
||||
} else {
|
||||
let regex = try! NSRegularExpression(pattern: "nsec1[02-9ac-z]+")
|
||||
return (regex.firstMatch(in: content, range: NSRange(location: 0, length: content.count)) != nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fileprivate func removePrivateKeyFromUserDefaults() throws {
|
||||
guard let privKey = UserDefaults.standard.string(forKey: "privkey") else { return }
|
||||
try save_privkey(privkey: privKey)
|
||||
|
||||
@@ -97,12 +97,7 @@ struct DMChatView: View {
|
||||
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)
|
||||
}
|
||||
showPrivateKeyWarning = contentContainsPrivateKey(message)
|
||||
|
||||
if !showPrivateKeyWarning {
|
||||
send_message()
|
||||
|
||||
@@ -66,12 +66,7 @@ struct PostView: View {
|
||||
|
||||
if !is_post_empty {
|
||||
Button(NSLocalizedString("Post", comment: "Button to post a note.")) {
|
||||
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)
|
||||
}
|
||||
showPrivateKeyWarning = contentContainsPrivateKey(self.post)
|
||||
|
||||
if !showPrivateKeyWarning {
|
||||
self.send_post()
|
||||
|
||||
Reference in New Issue
Block a user