From 5d58dd6f6bc7d97365e203bf74ca1cc4bafb7986 Mon Sep 17 00:00:00 2001 From: Matthew Ramsden <6657488+reez@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:50:15 -0500 Subject: [PATCH] ui: export descriptor --- .../Resources/Localizable.xcstrings | 7 +++ .../Activity/TransactionDetailViewModel.swift | 2 +- .../View Model/Settings/SeedViewModel.swift | 32 ++++++++++--- .../View/Settings/SeedView.swift | 47 +++++++++++++++++-- 4 files changed, 76 insertions(+), 12 deletions(-) diff --git a/BDKSwiftExampleWallet/Resources/Localizable.xcstrings b/BDKSwiftExampleWallet/Resources/Localizable.xcstrings index 5678d1a..ee4207f 100644 --- a/BDKSwiftExampleWallet/Resources/Localizable.xcstrings +++ b/BDKSwiftExampleWallet/Resources/Localizable.xcstrings @@ -289,6 +289,7 @@ } }, "Copy" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -327,6 +328,9 @@ } } } + }, + "Descriptors" : { + }, "Destructive Zone" : { "localizations" : { @@ -579,6 +583,9 @@ } } } + }, + "Seed" : { + }, "Seed is not synced across devices." : { diff --git a/BDKSwiftExampleWallet/View Model/Activity/TransactionDetailViewModel.swift b/BDKSwiftExampleWallet/View Model/Activity/TransactionDetailViewModel.swift index 02852c1..e500e67 100644 --- a/BDKSwiftExampleWallet/View Model/Activity/TransactionDetailViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/Activity/TransactionDetailViewModel.swift @@ -42,7 +42,7 @@ class TransactionDetailViewModel { func getEsploraUrl() { let savedEsploraURL = bdkClient.getEsploraURL() - + switch network { case "signet": if savedEsploraURL == Constants.Config.EsploraServerURLNetwork.Signet.bdk { diff --git a/BDKSwiftExampleWallet/View Model/Settings/SeedViewModel.swift b/BDKSwiftExampleWallet/View Model/Settings/SeedViewModel.swift index b510b8c..baa5523 100644 --- a/BDKSwiftExampleWallet/View Model/Settings/SeedViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/Settings/SeedViewModel.swift @@ -14,26 +14,46 @@ import SwiftUI class SeedViewModel { let bdkClient: BDKClient - var seed: BackupInfo? + var backupInfo: BackupInfo? + var publicDescriptor: Descriptor? + var publicChangeDescriptor: Descriptor? var seedViewError: AppError? var showingSeedViewErrorAlert: Bool init( bdkClient: BDKClient = .live, - seed: BackupInfo? = nil, + backupInfo: BackupInfo? = nil, seedViewError: AppError? = nil, showingSeedViewErrorAlert: Bool = false ) { self.bdkClient = bdkClient - self.seed = seed + self.backupInfo = backupInfo self.seedViewError = seedViewError self.showingSeedViewErrorAlert = showingSeedViewErrorAlert } - func getSeed() { + func getNetwork() -> Network { + let savedNetwork = bdkClient.getNetwork() + return savedNetwork + } + + func getBackupInfo(network: Network) { do { - let seed = try bdkClient.getBackupInfo() - self.seed = seed + let backupInfo = try bdkClient.getBackupInfo() + + let externalPublicDescriptor = try Descriptor.init( + descriptor: backupInfo.descriptor, + network: network + ) + self.publicDescriptor = externalPublicDescriptor + + let internalPublicDescriptor = try Descriptor.init( + descriptor: backupInfo.changeDescriptor, + network: network + ) + self.publicChangeDescriptor = internalPublicDescriptor + + self.backupInfo = backupInfo } catch { self.seedViewError = .generic(message: error.localizedDescription) self.showingSeedViewErrorAlert = true diff --git a/BDKSwiftExampleWallet/View/Settings/SeedView.swift b/BDKSwiftExampleWallet/View/Settings/SeedView.swift index 5138376..6c3e6ef 100644 --- a/BDKSwiftExampleWallet/View/Settings/SeedView.swift +++ b/BDKSwiftExampleWallet/View/Settings/SeedView.swift @@ -20,10 +20,13 @@ struct SeedView: View { .ignoresSafeArea() VStack { - if let seed = viewModel.seed { + if let backupInfo = viewModel.backupInfo, + let publicDescriptor = viewModel.publicDescriptor, + let publicChangeDescriptor = viewModel.publicChangeDescriptor + { SeedPhraseView( - words: seed.mnemonic.components(separatedBy: " "), + words: backupInfo.mnemonic.components(separatedBy: " "), preferredWordsPerRow: 2, usePaging: true, wordsPerPage: 4 @@ -41,7 +44,7 @@ struct SeedView: View { HStack { Spacer() Button { - UIPasteboard.general.string = seed.mnemonic + UIPasteboard.general.string = backupInfo.mnemonic isCopied = true showCheckmark = true DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) { @@ -55,7 +58,7 @@ struct SeedView: View { ? "document.on.document.fill" : "document.on.document" ) .contentTransition(.symbolEffect(.replace)) - Text("Copy") + Text("Seed") .bold() } } @@ -70,6 +73,39 @@ struct SeedView: View { ) Spacer() } + + HStack { + Spacer() + + let formattedDescriptors = """ + External Private: \(backupInfo.descriptor) + + External Public: \(publicDescriptor) + + Internal Private: \(backupInfo.changeDescriptor) + + Internal Public: \(publicChangeDescriptor) + """ + + ShareLink(item: formattedDescriptors) { + HStack { + Image(systemName: "square.and.arrow.up") + Text("Descriptors") + .bold() + } + } + .buttonStyle( + BitcoinFilled( + width: 160, + height: 40, + tintColor: .primary, + textColor: Color(uiColor: .systemBackground), + isCapsule: true + ) + ) + + Spacer() + } .padding() } else { Text("No seed available") @@ -79,7 +115,8 @@ struct SeedView: View { } .padding() .onAppear { - viewModel.getSeed() + let network = viewModel.getNetwork() + viewModel.getBackupInfo(network: network) } } .alert(isPresented: $viewModel.showingSeedViewErrorAlert) {