@@ -148,6 +148,53 @@ private class BDKService {
148148 self . wallet = wallet
149149 }
150150
151+ func createWallet( descriptor: String ? ) throws {
152+ let documentsDirectoryURL = URL . documentsDirectory
153+ let walletDataDirectoryURL = documentsDirectoryURL. appendingPathComponent ( " wallet_data " )
154+
155+ if FileManager . default. fileExists ( atPath: walletDataDirectoryURL. path) {
156+ try FileManager . default. removeItem ( at: walletDataDirectoryURL)
157+ } else {
158+ }
159+
160+ let baseUrl =
161+ try keyClient. getEsploraURL ( ) ?? Constants . Config. EsploraServerURLNetwork. Signet. mutiny
162+
163+ guard let descriptorString = descriptor, !descriptorString. isEmpty else {
164+ throw WalletError . walletNotFound
165+ }
166+
167+ let cleanDescriptor =
168+ descriptorString. split ( separator: " # " ) . first. map ( String . init) ?? descriptorString
169+ let descriptor = try Descriptor ( descriptor: cleanDescriptor, network: network)
170+ let changeDescriptorString = cleanDescriptor. replacingOccurrences ( of: " /0/* " , with: " /1/* " )
171+ let changeDescriptor = try Descriptor ( descriptor: changeDescriptorString, network: network)
172+
173+ let backupInfo = BackupInfo (
174+ mnemonic: " " ,
175+ descriptor: descriptor. toStringWithSecret ( ) ,
176+ changeDescriptor: changeDescriptor. toStringWithSecret ( )
177+ )
178+
179+ try keyClient. saveBackupInfo ( backupInfo)
180+ try keyClient. saveNetwork ( self . network. description)
181+ try keyClient. saveEsploraURL ( baseUrl)
182+
183+ try FileManager . default. ensureDirectoryExists ( at: walletDataDirectoryURL)
184+ try FileManager . default. removeOldFlatFileIfNeeded ( at: documentsDirectoryURL)
185+ let persistenceBackendPath = walletDataDirectoryURL. appendingPathComponent ( " wallet.sqlite " )
186+ . path
187+ let connection = try Connection ( path: persistenceBackendPath)
188+ self . connection = connection
189+ let wallet = try Wallet (
190+ descriptor: descriptor,
191+ changeDescriptor: changeDescriptor,
192+ network: network,
193+ connection: connection
194+ )
195+ self . wallet = wallet
196+ }
197+
151198 private func loadWallet( descriptor: Descriptor , changeDescriptor: Descriptor ) throws {
152199 let documentsDirectoryURL = URL . documentsDirectory
153200 let walletDataDirectoryURL = documentsDirectoryURL. appendingPathComponent ( " wallet_data " )
@@ -313,7 +360,8 @@ extension BDKService {
313360struct BDKClient {
314361 let loadWallet : ( ) throws -> Void
315362 let deleteWallet : ( ) throws -> Void
316- let createWallet : ( String ? ) throws -> Void
363+ let createWalletFromSeed : ( String ? ) throws -> Void
364+ let createWalletFromDescriptor : ( String ? ) throws -> Void
317365 let getBalance : ( ) throws -> Balance
318366 let transactions : ( ) throws -> [ CanonicalTx ]
319367 let listUnspent : ( ) throws -> [ LocalOutput ]
@@ -338,7 +386,10 @@ extension BDKClient {
338386 static let live = Self (
339387 loadWallet: { try BDKService . shared. loadWalletFromBackup ( ) } ,
340388 deleteWallet: { try BDKService . shared. deleteWallet ( ) } ,
341- createWallet: { words in try BDKService . shared. createWallet ( words: words) } ,
389+ createWalletFromSeed: { words in try BDKService . shared. createWallet ( words: words) } ,
390+ createWalletFromDescriptor: { descriptor in
391+ try BDKService . shared. createWallet ( descriptor: descriptor)
392+ } ,
342393 getBalance: { try BDKService . shared. getBalance ( ) } ,
343394 transactions: { try BDKService . shared. transactions ( ) } ,
344395 listUnspent: { try BDKService . shared. listUnspent ( ) } ,
@@ -387,7 +438,8 @@ extension BDKClient {
387438 static let mock = Self (
388439 loadWallet: { } ,
389440 deleteWallet: { } ,
390- createWallet: { _ in } ,
441+ createWalletFromSeed: { _ in } ,
442+ createWalletFromDescriptor: { _ in } ,
391443 getBalance: { . mock } ,
392444 transactions: {
393445 return [
0 commit comments