Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions Bitkit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
961058EA2C35793000E1F1D8 /* LnPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9637E6DE2C32ED7B004A92FC /* LnPeer.swift */; };
961058EB2C35793000E1F1D8 /* WalletNetwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9637E6DC2C32EAA8004A92FC /* WalletNetwork.swift */; };
961058EE2C35798C00E1F1D8 /* LDKNode in Frameworks */ = {isa = PBXBuildFile; productRef = 961058ED2C35798C00E1F1D8 /* LDKNode */; };
961058F02C35799400E1F1D8 /* BitcoinDevKit in Frameworks */ = {isa = PBXBuildFile; productRef = 961058EF2C35799400E1F1D8 /* BitcoinDevKit */; };
961301882C50202A00878183 /* MigrationsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961301872C50202A00878183 /* MigrationsService.swift */; };
9613018C2C5022D700878183 /* SQLite in Frameworks */ = {isa = PBXBuildFile; productRef = 9613018B2C5022D700878183 /* SQLite */; };
9613018E2C50288900878183 /* LdkMigration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9613018D2C50288900878183 /* LdkMigration.swift */; };
Expand All @@ -25,7 +24,6 @@
962B92252C5A4F5D00B21057 /* ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 962B92242C5A4F5D00B21057 /* ViewModel.swift */; };
9637E6D32C32CE79004A92FC /* Env.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9637E6D22C32CE79004A92FC /* Env.swift */; };
9637E6D52C32D811004A92FC /* OnChainService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9637E6D42C32D811004A92FC /* OnChainService.swift */; };
9637E6D82C32D8A7004A92FC /* BitcoinDevKit in Frameworks */ = {isa = PBXBuildFile; productRef = 9637E6D72C32D8A7004A92FC /* BitcoinDevKit */; };
9637E6DA2C32E573004A92FC /* OnChainViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9637E6D92C32E573004A92FC /* OnChainViewModel.swift */; };
9637E6DD2C32EAA8004A92FC /* WalletNetwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9637E6DC2C32EAA8004A92FC /* WalletNetwork.swift */; };
9637E6DF2C32ED7B004A92FC /* LnPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9637E6DE2C32ED7B004A92FC /* LnPeer.swift */; };
Expand All @@ -47,6 +45,8 @@
966DE6722C512C1E00A7B0EF /* seed.bin in Resources */ = {isa = PBXBuildFile; fileRef = 966DE6712C512C1E00A7B0EF /* seed.bin */; };
966DE6742C512C9300A7B0EF /* HexBytes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966DE6732C512C9300A7B0EF /* HexBytes.swift */; };
966DE6752C512FD100A7B0EF /* HexBytes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966DE6732C512C9300A7B0EF /* HexBytes.swift */; };
968A68082C6B6D1F00D4C7CD /* BitcoinDevKit in Frameworks */ = {isa = PBXBuildFile; productRef = 968A68072C6B6D1F00D4C7CD /* BitcoinDevKit */; };
968A680F2C6BA5EC00D4C7CD /* BitcoinDevKit in Frameworks */ = {isa = PBXBuildFile; productRef = 968A680E2C6BA5EC00D4C7CD /* BitcoinDevKit */; };
96B129FD2C2EC05D00DD07B0 /* LDKNode in Frameworks */ = {isa = PBXBuildFile; productRef = 96B129FC2C2EC05D00DD07B0 /* LDKNode */; };
96B12A002C2EC37B00DD07B0 /* LightningViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96B129FF2C2EC37B00DD07B0 /* LightningViewModel.swift */; };
96B12A032C2EC65000DD07B0 /* LightningService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96B12A022C2EC65000DD07B0 /* LightningService.swift */; };
Expand Down Expand Up @@ -150,7 +150,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
961058F02C35799400E1F1D8 /* BitcoinDevKit in Frameworks */,
961058EE2C35798C00E1F1D8 /* LDKNode in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -159,9 +158,10 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
968A68082C6B6D1F00D4C7CD /* BitcoinDevKit in Frameworks */,
9613018C2C5022D700878183 /* SQLite in Frameworks */,
9637E6D82C32D8A7004A92FC /* BitcoinDevKit in Frameworks */,
966DE6702C51210000A7B0EF /* LightningDevKit in Frameworks */,
968A680F2C6BA5EC00D4C7CD /* BitcoinDevKit in Frameworks */,
96B129FD2C2EC05D00DD07B0 /* LDKNode in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -389,7 +389,6 @@
name = BitkitNotification;
packageProductDependencies = (
961058ED2C35798C00E1F1D8 /* LDKNode */,
961058EF2C35799400E1F1D8 /* BitcoinDevKit */,
);
productName = BitkitNotification;
productReference = 961058DC2C355B5500E1F1D8 /* BitkitNotification.appex */;
Expand All @@ -412,9 +411,10 @@
name = Bitkit;
packageProductDependencies = (
96B129FC2C2EC05D00DD07B0 /* LDKNode */,
9637E6D72C32D8A7004A92FC /* BitcoinDevKit */,
9613018B2C5022D700878183 /* SQLite */,
966DE66F2C51210000A7B0EF /* LightningDevKit */,
968A68072C6B6D1F00D4C7CD /* BitcoinDevKit */,
968A680E2C6BA5EC00D4C7CD /* BitcoinDevKit */,
);
productName = Bitkit;
productReference = 96FE1F612C2DE6AA006D0C8B /* Bitkit.app */;
Expand Down Expand Up @@ -493,9 +493,9 @@
mainGroup = 96FE1F582C2DE6AA006D0C8B;
packageReferences = (
96B129FB2C2EC05D00DD07B0 /* XCRemoteSwiftPackageReference "ldk-node" */,
9637E6D62C32D8A7004A92FC /* XCRemoteSwiftPackageReference "bdk-swift" */,
961301892C50215500878183 /* XCRemoteSwiftPackageReference "SQLite.swift" */,
966DE66E2C51210000A7B0EF /* XCRemoteSwiftPackageReference "ldk-swift" */,
968A680D2C6BA5EC00D4C7CD /* XCRemoteSwiftPackageReference "bdk-swift" */,
);
productRefGroup = 96FE1F622C2DE6AA006D0C8B /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -1042,20 +1042,20 @@
minimumVersion = 0.15.3;
};
};
9637E6D62C32D8A7004A92FC /* XCRemoteSwiftPackageReference "bdk-swift" */ = {
966DE66E2C51210000A7B0EF /* XCRemoteSwiftPackageReference "ldk-swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/bitcoindevkit/bdk-swift";
repositoryURL = "https://github.com/lightningdevkit/ldk-swift/";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.31.1;
minimumVersion = 0.0.123;
};
};
966DE66E2C51210000A7B0EF /* XCRemoteSwiftPackageReference "ldk-swift" */ = {
968A680D2C6BA5EC00D4C7CD /* XCRemoteSwiftPackageReference "bdk-swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/lightningdevkit/ldk-swift/";
repositoryURL = "https://github.com/bitcoindevkit/bdk-swift";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.0.123;
kind = exactVersion;
version = "1.0.0-alpha.11";
};
};
96B129FB2C2EC05D00DD07B0 /* XCRemoteSwiftPackageReference "ldk-node" */ = {
Expand All @@ -1074,26 +1074,25 @@
package = 96B129FB2C2EC05D00DD07B0 /* XCRemoteSwiftPackageReference "ldk-node" */;
productName = LDKNode;
};
961058EF2C35799400E1F1D8 /* BitcoinDevKit */ = {
isa = XCSwiftPackageProductDependency;
package = 9637E6D62C32D8A7004A92FC /* XCRemoteSwiftPackageReference "bdk-swift" */;
productName = BitcoinDevKit;
};
9613018B2C5022D700878183 /* SQLite */ = {
isa = XCSwiftPackageProductDependency;
package = 961301892C50215500878183 /* XCRemoteSwiftPackageReference "SQLite.swift" */;
productName = SQLite;
};
9637E6D72C32D8A7004A92FC /* BitcoinDevKit */ = {
isa = XCSwiftPackageProductDependency;
package = 9637E6D62C32D8A7004A92FC /* XCRemoteSwiftPackageReference "bdk-swift" */;
productName = BitcoinDevKit;
};
966DE66F2C51210000A7B0EF /* LightningDevKit */ = {
isa = XCSwiftPackageProductDependency;
package = 966DE66E2C51210000A7B0EF /* XCRemoteSwiftPackageReference "ldk-swift" */;
productName = LightningDevKit;
};
968A68072C6B6D1F00D4C7CD /* BitcoinDevKit */ = {
isa = XCSwiftPackageProductDependency;
productName = BitcoinDevKit;
};
968A680E2C6BA5EC00D4C7CD /* BitcoinDevKit */ = {
isa = XCSwiftPackageProductDependency;
package = 968A680D2C6BA5EC00D4C7CD /* XCRemoteSwiftPackageReference "bdk-swift" */;
productName = BitcoinDevKit;
};
96B129FC2C2EC05D00DD07B0 /* LDKNode */ = {
isa = XCSwiftPackageProductDependency;
package = 96B129FB2C2EC05D00DD07B0 /* XCRemoteSwiftPackageReference "ldk-node" */;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,43 @@
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>3</integer>
<integer>4</integer>
</dict>
<key>SQLite (Playground) 2.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>4</integer>
<integer>5</integer>
</dict>
<key>SQLite (Playground).xcscheme</key>
<key>SQLite (Playground) 3.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>SQLite (Playground) 4.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>6</integer>
</dict>
<key>SQLite (Playground) 5.xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>7</integer>
</dict>
<key>SQLite (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>3</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
Expand Down
3 changes: 2 additions & 1 deletion Bitkit/Constants/Env.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct Env {
static let network: WalletNetwork = .regtest
static let defaultWalletWordCount: WordCount = .words12
static let onchainWalletStopGap = UInt64(20)
static let esploraParallelRequests = UInt64(5)
static var esploraServerUrl: String {
switch network {
case .regtest:
Expand Down Expand Up @@ -107,7 +108,7 @@ struct Env {
switch network {
case .regtest:
return [
.init(nodeId: "02b61365f14c5070465e014485fa91cee5a131cf2a4b7cb37309fcd1cc53975238", address: "192.168.0.106:9735")
// .init(nodeId: "02b61365f14c5070465e014485fa91cee5a131cf2a4b7cb37309fcd1cc53975238", address: "192.168.0.106:9735")
]
case .bitcoin:
return []
Expand Down
1 change: 1 addition & 0 deletions Bitkit/Services/LightningService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class LightningService {
Logger.debug("Stopping node...")
try await ServiceQueue.background(.ldk) {
try node.stop()
self.node = nil
}
Logger.info("Node stopped")
}
Expand Down
67 changes: 43 additions & 24 deletions Bitkit/Services/OnChainService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ import BitcoinDevKit
class OnChainService {
private var wallet: Wallet?
var currentWalletIndex: Int = 0
private var hasSynced = false

private var blockchainConfig: BlockchainConfig {
let esploraConfig = EsploraConfig(
baseUrl: Env.esploraServerUrl,
proxy: nil,
concurrency: nil,
stopGap: Env.onchainWalletStopGap,
timeout: nil
)

return BlockchainConfig.esplora(config: esploraConfig)
private var esploraClient: EsploraClient {
EsploraClient(url: Env.esploraServerUrl)
}

static var shared = OnChainService()
Expand Down Expand Up @@ -53,22 +46,19 @@ class OnChainService {
keychain: .internal,
network: Env.network.bdkNetwork
)

//TODO save to keychain


Logger.debug("Creating onchain wallet...")

let bdkStorage = Env.bdkStorage(walletIndex: walletIndex)
try FileManager.default.createDirectory(at: bdkStorage, withIntermediateDirectories: true, attributes: nil)

let dbConfig = DatabaseConfig.sqlite(config: .init(path: bdkStorage.appendingPathComponent("db.sqlite").path))

let sqlitePath = bdkStorage.appendingPathComponent("db.sqlite").path

try await ServiceQueue.background(.bdk) {
self.wallet = try Wallet(
descriptor: descriptor,
changeDescriptor: changeDescriptor,
network: Env.network.bdkNetwork,
databaseConfig: dbConfig
persistenceBackendPath: sqlitePath,
network: Env.network.bdkNetwork
)
}

Expand Down Expand Up @@ -107,30 +97,59 @@ class OnChainService {
}

return try await ServiceQueue.background(.bdk) {
let addressInfo = try wallet.getAddress(addressIndex: .new)
let addressInfo = try wallet.revealNextAddress(keychain: .external)
return addressInfo.address.asString()
}
}

func sync() async throws {
/// Partial sync. Collects all revealed script pubkeys from the wallet keychain.
func syncWithRevealedSpks() async throws {
guard let wallet else {
throw AppError(serviceError: .onchainWalletNotInitialized)
}

Logger.debug("Syncing BDK...")

try await ServiceQueue.background(.bdk) {
let request = wallet.startSyncWithRevealedSpks()
let update = try self.esploraClient.sync(syncRequest: request, parallelRequests: 5)
try wallet.applyUpdate(update: update)

//TODO: persist wallet??
}

let blockchain = try Blockchain(config: blockchainConfig)
hasSynced = true

Logger.info("BDK synced")
}

/// Required on restore or manually from settings. Performs a full scan of the wallet.
func fullScan() async throws {
guard let wallet else {
throw AppError(serviceError: .onchainWalletNotInitialized)
}

Logger.debug("Full on chain scan...")

try await ServiceQueue.background(.bdk) {
try wallet.sync(blockchain: blockchain, progress: nil)
let request = wallet.startFullScan()
let update = try self.esploraClient.fullScan(
fullScanRequest: request,
stopGap: Env.onchainWalletStopGap,
parallelRequests: Env.esploraParallelRequests
)
try wallet.applyUpdate(update: update)
//TODO: persist wallet once BDK is updated to beta release
}

Logger.info("BDK synced")
hasSynced = true

Logger.info("Full scan complete")
}
}

//MARK: UI Helpers (Published via OnChainViewModel)
extension OnChainService {
//TODO catch errors?
var balance: Balance? { try? wallet?.getBalance() }
var balance: Balance? { hasSynced ? wallet?.getBalance() : nil }
}
10 changes: 8 additions & 2 deletions Bitkit/Services/ServiceQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class ServiceQueue {
}
}

static func background<T>(_ service: ServiceTypes, _ blocking: @escaping () throws -> T) async throws -> T {
try await withCheckedThrowingContinuation { continuation in
static func background<T>(_ service: ServiceTypes, _ blocking: @escaping () throws -> T, functionName: String = #function) async throws -> T {
let startTime = CFAbsoluteTimeGetCurrent()
let result = try await withCheckedThrowingContinuation { continuation in
service.queue.async {
do {
let res = try blocking()
Expand All @@ -45,5 +46,10 @@ class ServiceQueue {
}
}
}

let timeElapsed = Double(round(100 * (CFAbsoluteTimeGetCurrent() - startTime)) / 100)
Logger.performance("\(functionName) took \(timeElapsed) seconds on \(service) queue")

return result
}
}
Loading