Skip to content

Commit 5994191

Browse files
committed
use into_single_descriptor
1 parent bca81ae commit 5994191

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,34 +164,27 @@ private class BDKService {
164164
throw WalletError.walletNotFound
165165
}
166166

167-
let descriptor: Descriptor
168-
let changeDescriptor: Descriptor
169-
170-
let cleanDescriptorString = descriptorString.components(separatedBy: "\n")
167+
let descriptorStrings = descriptorString.components(separatedBy: "\n")
171168
.map { $0.split(separator: "#").first?.trimmingCharacters(in: .whitespaces) ?? "" }
172169
.filter { !$0.isEmpty }
173-
.joined(separator: "\n")
174-
175-
if let firstDescriptor = try? Descriptor(
176-
descriptor: cleanDescriptorString.components(separatedBy: "\n")[0],
177-
network: network
178-
),
179-
firstDescriptor.isMultipath()
180-
{
181-
let baseDescriptor = cleanDescriptorString.components(separatedBy: "\n")[0]
182-
descriptor = try Descriptor(
183-
descriptor: baseDescriptor.replacingOccurrences(of: "<0;1>", with: "0"),
184-
network: network
185-
)
186-
changeDescriptor = try Descriptor(
187-
descriptor: baseDescriptor.replacingOccurrences(of: "<0;1>", with: "1"),
170+
let descriptor: Descriptor
171+
let changeDescriptor: Descriptor
172+
if descriptorStrings.count == 1 {
173+
let parsedDescriptor = try Descriptor(
174+
descriptor: descriptorStrings[0],
188175
network: network
189176
)
177+
let singleDescriptors = try parsedDescriptor.toSingleDescriptors()
178+
guard singleDescriptors.count >= 2 else {
179+
throw WalletError.walletNotFound
180+
}
181+
descriptor = singleDescriptors[0]
182+
changeDescriptor = singleDescriptors[1]
183+
} else if descriptorStrings.count == 2 {
184+
descriptor = try Descriptor(descriptor: descriptorStrings[0], network: network)
185+
changeDescriptor = try Descriptor(descriptor: descriptorStrings[1], network: network)
190186
} else {
191-
let descriptors = cleanDescriptorString.components(separatedBy: "\n")
192-
guard descriptors.count == 2 else { throw WalletError.walletNotFound }
193-
descriptor = try Descriptor(descriptor: descriptors[0], network: network)
194-
changeDescriptor = try Descriptor(descriptor: descriptors[1], network: network)
187+
throw WalletError.walletNotFound
195188
}
196189

197190
let backupInfo = BackupInfo(

0 commit comments

Comments
 (0)