Skip to content

Commit

Permalink
ui: export descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
reez authored Oct 18, 2024
1 parent e49bf53 commit 5d58dd6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
7 changes: 7 additions & 0 deletions BDKSwiftExampleWallet/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@
}
},
"Copy" : {
"extractionState" : "stale",
"localizations" : {
"fr" : {
"stringUnit" : {
Expand Down Expand Up @@ -327,6 +328,9 @@
}
}
}
},
"Descriptors" : {

},
"Destructive Zone" : {
"localizations" : {
Expand Down Expand Up @@ -579,6 +583,9 @@
}
}
}
},
"Seed" : {

},
"Seed is not synced across devices." : {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TransactionDetailViewModel {

func getEsploraUrl() {
let savedEsploraURL = bdkClient.getEsploraURL()

switch network {
case "signet":
if savedEsploraURL == Constants.Config.EsploraServerURLNetwork.Signet.bdk {
Expand Down
32 changes: 26 additions & 6 deletions BDKSwiftExampleWallet/View Model/Settings/SeedViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 42 additions & 5 deletions BDKSwiftExampleWallet/View/Settings/SeedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -55,7 +58,7 @@ struct SeedView: View {
? "document.on.document.fill" : "document.on.document"
)
.contentTransition(.symbolEffect(.replace))
Text("Copy")
Text("Seed")
.bold()
}
}
Expand All @@ -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")
Expand All @@ -79,7 +115,8 @@ struct SeedView: View {
}
.padding()
.onAppear {
viewModel.getSeed()
let network = viewModel.getNetwork()
viewModel.getBackupInfo(network: network)
}
}
.alert(isPresented: $viewModel.showingSeedViewErrorAlert) {
Expand Down

0 comments on commit 5d58dd6

Please sign in to comment.