Merge branch 'master' into add-wallet-modal

This commit is contained in:
Suhail Saqan
2022-12-25 20:41:57 -06:00
committed by GitHub
10 changed files with 121 additions and 41 deletions

View File

@@ -121,21 +121,25 @@ struct ImageCarousel: View {
var body: some View {
TabView {
ForEach(urls, id: \.absoluteString) { url in
KFAnimatedImage(url)
.configure { view in
view.framePreloadCount = 3
Rectangle()
.overlay {
KFAnimatedImage(url)
.configure { view in
view.framePreloadCount = 3
}
.cacheOriginalImage()
.loadDiskFileSynchronously()
.scaleFactor(UIScreen.main.scale)
.fade(duration: 0.1)
.aspectRatio(contentMode: .fill)
.tabItem {
Text(url.absoluteString)
}
.id(url.absoluteString)
}
.cacheOriginalImage()
.loadDiskFileSynchronously()
.scaleFactor(UIScreen.main.scale)
.fade(duration: 0.1)
.aspectRatio(contentMode: .fit)
.tabItem {
Text(url.absoluteString)
}
.id(url.absoluteString)
}
}
.cornerRadius(10)
.sheet(isPresented: $open_sheet) {
ImageViewer(urls: urls)
}

View File

@@ -7,6 +7,7 @@
import Foundation
import secp256k1
import Vault
let PUBKEY_HRP = "npub"
let PRIVKEY_HRP = "nsec"
@@ -29,6 +30,12 @@ enum Bech32Key {
case sec(String)
}
struct DamusKeychainConfiguration: KeychainConfiguration {
var serviceName = "damus"
var accessGroup: String? = nil
var accountName = "privkey"
}
func decode_bech32_key(_ key: String) -> Bech32Key? {
guard let decoded = try? bech32_decode(key) else {
return nil
@@ -86,32 +93,38 @@ func save_pubkey(pubkey: String) {
UserDefaults.standard.set(pubkey, forKey: "pubkey")
}
func save_privkey(privkey: String) {
UserDefaults.standard.set(privkey, forKey: "privkey")
func save_privkey(privkey: String) throws {
try Vault.savePrivateKey(privkey, keychainConfiguration: DamusKeychainConfiguration())
}
func clear_saved_privkey() {
UserDefaults.standard.removeObject(forKey: "privkey")
func clear_saved_privkey() throws {
try Vault.deletePrivateKey(keychainConfiguration: DamusKeychainConfiguration())
}
func clear_saved_pubkey() {
UserDefaults.standard.removeObject(forKey: "pubkey")
}
func save_keypair(pubkey: String, privkey: String) {
func save_keypair(pubkey: String, privkey: String) throws {
save_pubkey(pubkey: pubkey)
save_privkey(privkey: privkey)
try save_privkey(privkey: privkey)
}
func clear_keypair() {
clear_saved_privkey()
func clear_keypair() throws {
try clear_saved_privkey()
clear_saved_pubkey()
}
func get_saved_keypair() -> Keypair? {
get_saved_pubkey().flatMap { pubkey in
let privkey = get_saved_privkey()
return Keypair(pubkey: pubkey, privkey: privkey)
do {
try removePrivateKeyFromUserDefaults()
return get_saved_pubkey().flatMap { pubkey in
let privkey = get_saved_privkey()
return Keypair(pubkey: pubkey, privkey: privkey)
}
} catch {
return nil
}
}
@@ -120,5 +133,11 @@ func get_saved_pubkey() -> String? {
}
func get_saved_privkey() -> String? {
return UserDefaults.standard.string(forKey: "privkey")
try? Vault.getPrivateKey(keychainConfiguration: DamusKeychainConfiguration())
}
fileprivate func removePrivateKeyFromUserDefaults() throws {
guard let privKey = UserDefaults.standard.string(forKey: "privkey") else { return }
try save_privkey(privkey: privKey)
UserDefaults.standard.removeObject(forKey: "privkey")
}

View File

@@ -129,14 +129,16 @@ struct EditMetadataView: View {
}
Section("About Me") {
let placeholder = "Absolute Boss"
ZStack(alignment: .topLeading) {
TextEditor(text: $about)
.textInputAutocapitalization(.sentences)
if about.isEmpty {
Text("Absolute boss")
.offset(x: 0, y: 7)
.foregroundColor(Color(uiColor: .placeholderText))
}
.frame(minHeight: 20, alignment: .leading)
.multilineTextAlignment(.leading)
Text(about.isEmpty ? placeholder : about)
.padding(.leading, 4)
.opacity(about.isEmpty ? 1 : 0)
.foregroundColor(Color(uiColor: .placeholderText))
}
}

View File

@@ -161,7 +161,7 @@ struct EventView: View {
// blame the porn bots for this code
func should_show_images(contacts: Contacts, ev: NostrEvent) -> Bool {
if contacts.is_friend(ev.pubkey) {
if contacts.is_in_friendosphere(ev.pubkey) {
return true
}
return false

View File

@@ -52,14 +52,24 @@ struct LoginView: View {
func process_login(_ key: ParsedKey, is_pubkey: Bool) -> Bool {
switch key {
case .priv(let priv):
save_privkey(privkey: priv)
do {
try save_privkey(privkey: priv)
} catch {
return false
}
guard let pk = privkey_to_pubkey(privkey: priv) else {
return false
}
save_pubkey(pubkey: pk)
case .pub(let pub):
clear_saved_privkey()
do {
try clear_saved_privkey()
} catch {
return false
}
save_pubkey(pubkey: pub)
case .nip05(let id):
@@ -82,10 +92,20 @@ struct LoginView: View {
case .hex(let hexstr):
if is_pubkey {
clear_saved_privkey()
do {
try clear_saved_privkey()
} catch {
return false
}
save_pubkey(pubkey: hexstr)
} else {
save_privkey(privkey: hexstr)
do {
try save_privkey(privkey: hexstr)
} catch {
return false
}
guard let pk = privkey_to_pubkey(privkey: hexstr) else {
return false
}

View File

@@ -107,8 +107,13 @@ struct SaveKeysView: View {
self.pool.send(.event(contacts_ev))
}
save_keypair(pubkey: account.pubkey, privkey: account.privkey)
notify(.login, account.keypair)
do {
try save_keypair(pubkey: account.pubkey, privkey: account.privkey)
notify(.login, account.keypair)
} catch {
self.error = "Failed to save keys"
}
case .error(let err):
self.loading = false
self.error = "\(err.debugDescription)"

View File

@@ -4,5 +4,9 @@
<dict>
<key>aps-environment</key>
<string>development</string>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.jb55.damus2</string>
</array>
</dict>
</plist>

View File

@@ -36,7 +36,7 @@ struct MainView: View {
}
}
.onReceive(handle_notify(.logout)) { _ in
clear_keypair()
try? clear_keypair()
keypair = nil
}
.onAppear {