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
169 changes: 116 additions & 53 deletions Bitkit/Services/LightningService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,35 +302,8 @@ class LightningService: NodeLifecycleService {
config.onchainWalletAccounts = try Self.watchOnlyAccountConfigs(walletIndex: walletIndex)
#endif

let builder = Builder.fromConfig(config: config)
builder.setCustomLogger(logWriter: LdkLogWriter())

let resolvedElectrumServerUrl = electrumServerUrl ?? Env.electrumServerUrl

let electrumConfig = ElectrumSyncConfig(
backgroundSyncConfig: .init(
onchainWalletSyncIntervalSecs: Env.walletSyncIntervalSecs,
lightningWalletSyncIntervalSecs: Env.walletSyncIntervalSecs,
feeRateCacheUpdateIntervalSecs: Env.walletSyncIntervalSecs
),
connectionTimeoutSecs: 10,
additionalWalletFullScanBatchSize: 100,
additionalWalletFullScanStopGap: 1000
)
Logger.info("Using Electrum server: \(resolvedElectrumServerUrl)")
builder.setChainSourceElectrum(serverUrl: resolvedElectrumServerUrl, config: electrumConfig)

// Set pathfinding scores source from external scorer
if let scorerUrl = Env.ldkScorerUrl {
Logger.info("Setting pathfinding scores source from scorer url: \(scorerUrl)")
builder.setPathfindingScoresSource(url: scorerUrl)
}

builder.setScoringFeeParams(params: Self.scoringFeeParameters(config: config))

// Configure gossip source from current settings
configureGossipSource(builder: builder, rgsServerUrl: rgsServerUrl)

Logger.debug("Building node...")
let storeId = try await VssStoreIdProvider.shared.getVssStoreId(walletIndex: walletIndex)

Expand All @@ -339,24 +312,57 @@ class LightningService: NodeLifecycleService {
Logger.debug("Building ldk-node with vssUrl: '\(vssUrl)'")
Logger.debug("Building ldk-node with lnurlAuthServerUrl: '\(lnurlAuthServerUrl)'")

if let channelMigration {
builder.setChannelDataMigration(migration: channelMigration)
Logger.info("Applied channel migration: \(channelMigration.channelMonitors.count) monitors", context: "Migration")
}

builder.setEntropyBip39Mnemonic(mnemonic: mnemonic, passphrase: passphrase)

guard !lnurlAuthServerUrl.isEmpty else {
throw CustomServiceError.vssAuthRequired
}

try await ServiceQueue.background(.ldk) {
let node = try builder.buildWithVssStore(
let entropyMnemonic = mnemonic
let entropyPassphrase = passphrase
let buildNode = { (config: Config) throws -> Node in
let builder = Builder.fromConfig(config: config)
builder.setCustomLogger(logWriter: LdkLogWriter())

let electrumConfig = ElectrumSyncConfig(
backgroundSyncConfig: .init(
onchainWalletSyncIntervalSecs: Env.walletSyncIntervalSecs,
lightningWalletSyncIntervalSecs: Env.walletSyncIntervalSecs,
feeRateCacheUpdateIntervalSecs: Env.walletSyncIntervalSecs
),
connectionTimeoutSecs: 10,
additionalWalletFullScanBatchSize: 100,
additionalWalletFullScanStopGap: 1000
)
Logger.info("Using Electrum server: \(resolvedElectrumServerUrl)")
builder.setChainSourceElectrum(serverUrl: resolvedElectrumServerUrl, config: electrumConfig)

// Set pathfinding scores source from external scorer
if let scorerUrl = Env.ldkScorerUrl {
Logger.info("Setting pathfinding scores source from scorer url: \(scorerUrl)")
builder.setPathfindingScoresSource(url: scorerUrl)
}

builder.setScoringFeeParams(params: Self.scoringFeeParameters(config: config))

// Configure gossip source from current settings
self.configureGossipSource(builder: builder, rgsServerUrl: rgsServerUrl)

if let channelMigration {
builder.setChannelDataMigration(migration: channelMigration)
Logger.info("Applied channel migration: \(channelMigration.channelMonitors.count) monitors", context: "Migration")
}

builder.setEntropyBip39Mnemonic(mnemonic: entropyMnemonic, passphrase: entropyPassphrase)

return try builder.buildWithVssStore(
vssUrl: vssUrl,
storeId: storeId,
lnurlAuthServerUrl: lnurlAuthServerUrl,
fixedHeaders: [:]
)
}

try await ServiceQueue.background(.ldk) {
let node = try Self.buildAcceptingPaykitServerAccounts(config: config, build: buildNode)
self.setupLock.withLock { self.configuredNode = (node, resolvedElectrumServerUrl) }
}
shouldReleaseLightningLock = false
Expand All @@ -368,6 +374,32 @@ class LightningService: NodeLifecycleService {
passphrase = nil
}

/// Runs `reconcile` before a wallet sync. A Paykit Server account that fails to load must not stop the wallet from
/// syncing, so its error is logged and the next sync retries it; a cancelled caller still stops here.
static func reconcileBeforeSync(_ reconcile: () async throws -> Void) async throws {
do {
try await reconcile()
} catch is CancellationError {
throw CancellationError()
} catch {
Logger.error(error, context: "Failed to reconcile Paykit Server accounts before sync")
}
}

/// Builds with `config`, and when ldk-node rejects a configured Paykit Server account (its xpub does not match the
/// seed, which fails the whole build) builds again without those accounts, so the wallet still starts. The
/// reconcile on start then adds each account the node accepts.
static func buildAcceptingPaykitServerAccounts(config: Config, build: (Config) throws -> Node) throws -> Node {
do {
return try build(config)
} catch let BuildError.InvalidSeedBytes(message) where !config.onchainWalletAccounts.isEmpty {
Logger.error("Node rejected Paykit Server accounts at build, building without them: \(message)", context: "LightningService")
var fallback = config
fallback.onchainWalletAccounts = []
return try build(fallback)
}
}

/// Runs after any restart already in progress, so overlapping restarts cannot stop or replace each other's node midway.
func restart(electrumServerUrl: String? = nil, rgsServerUrl: String? = nil) async throws {
let restart: Task<Void, Error> = restartLock.withLock {
Expand Down Expand Up @@ -710,7 +742,7 @@ class LightningService: NodeLifecycleService {
}

#if !BITKIT_NOTIFICATION_EXTENSION
try await reconcileWatchOnlyAccounts()
try await Self.reconcileBeforeSync { try await self.reconcileWatchOnlyAccounts() }
#endif

Logger.debug("Syncing LDK...")
Expand All @@ -734,14 +766,30 @@ class LightningService: NodeLifecycleService {
}
}

/// Derives the account xpub from the wallet mnemonic instead of asking the node, so a Pubky auth approval does not
/// depend on the node being built and started. The result must equal `Node.exportOnchainWalletAccountXpub`, which
/// `Node.addOnchainWalletAccount` compares as a string; see `WatchOnlyAccountClaimCodec.accountXpub(fromDescriptor:network:)`.
func exportWatchOnlyAccountXpub(accountIndex: UInt32, addressType: LDKNode.AddressType) async throws -> String {
guard let node else {
throw AppError(serviceError: .nodeNotSetup)
guard addressType == .nativeSegwit else {
throw WatchOnlyAccountError.invalidExtendedPublicKey
}
let walletIndex = currentWalletIndex
guard let mnemonic = try Keychain.loadString(key: .bip39Mnemonic(index: walletIndex)) else {
throw CustomServiceError.mnemonicNotFound
}
let passphraseRaw = try Keychain.loadString(key: .bip39Passphrase(index: walletIndex))
let passphrase = passphraseRaw?.isEmpty == true ? nil : passphraseRaw

return try await ServiceQueue.background(.ldk) {
try node.exportOnchainWalletAccountXpub(addressType: addressType, accountIndex: accountIndex)
let descriptor = try await ServiceQueue.background(.core) {
try BitkitCore.deriveOnchainDescriptor(
mnemonicPhrase: mnemonic,
network: Env.bitkitCoreNetwork,
bip39Passphrase: passphrase,
accountType: .nativeSegwit,
accountIndex: accountIndex
)
}
return try WatchOnlyAccountClaimCodec.accountXpub(fromDescriptor: descriptor, network: Env.network)
}

func setWatchOnlyAccountTracking(
Expand All @@ -750,8 +798,12 @@ class LightningService: NodeLifecycleService {
xpub: String,
enabled: Bool
) async throws {
// Without a node (not built yet, or stopped) only the caller's store changes: the next build loads tracked
// accounts from the store, and `reconcileWatchOnlyAccounts` on start adds or removes any it missed.
guard let node else {
throw AppError(serviceError: .nodeNotSetup)
// Journeys read this line to prove an approval finished before the node was built.
Logger.info("Paykit Server account \(accountIndex) tracking stored without a node; it loads when the node starts")
return
}

try await ServiceQueue.background(.ldk) {
Expand Down Expand Up @@ -821,22 +873,33 @@ class LightningService: NodeLifecycleService {
}
}

// One account the node rejects must not keep the others from loading; the first failure is
// rethrown so the reconciliation is retried.
var firstFailure: Error?
for config in desiredConfigs {
let isTracked = trackedAccounts.contains {
$0.addressType == config.addressType && $0.accountIndex == config.accountIndex
}
if !isTracked {
try node.addOnchainWalletAccount(
do {
let isTracked = trackedAccounts.contains {
$0.addressType == config.addressType && $0.accountIndex == config.accountIndex
}
if !isTracked {
try node.addOnchainWalletAccount(
addressType: config.addressType,
accountIndex: config.accountIndex,
xpub: config.xpub
)
}
try node.onchainPayment().revealReceiveAddressesToAccount(
addressType: config.addressType,
accountIndex: config.accountIndex,
xpub: config.xpub
index: Self.watchOnlyAccountHighestPreRevealedAddressIndex
)
} catch {
Logger.error(error, context: "Failed to load Paykit Server account \(config.accountIndex)")
firstFailure = firstFailure ?? error
}
try node.onchainPayment().revealReceiveAddressesToAccount(
addressType: config.addressType,
accountIndex: config.accountIndex,
index: Self.watchOnlyAccountHighestPreRevealedAddressIndex
)
}
if let firstFailure {
throw firstFailure
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions Bitkit/Services/WatchOnlyAccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,54 @@ enum WatchOnlyAccountClaimCodec {
[0x04, 0x35, 0x87, 0xCF],
]

private static let testnetXpubVersion: [UInt8] = [0x04, 0x35, 0x87, 0xCF]

/// The account xpub in a bitkit-core `deriveOnchainDescriptor` result, with the version bytes ldk-node uses for
/// `network`. bitkit-core always serializes with mainnet `xpub` bytes; ldk-node's
/// `exportOnchainWalletAccountXpub` uses `tpub` on every test network, and `addOnchainWalletAccount` compares
/// the strings exactly.
static func accountXpub(fromDescriptor descriptor: String, network: LDKNode.Network) throws -> String {
guard let start = descriptor.firstIndex(of: "]") else {
throw WatchOnlyAccountError.invalidExtendedPublicKey
}
let key = descriptor[descriptor.index(after: start)...].prefix { $0 != "/" }
guard key.hasPrefix("xpub") else {
throw WatchOnlyAccountError.invalidExtendedPublicKey
}
let xpub = String(key)
guard network != .bitcoin else {
return xpub
}

let payload = try base58CheckDecode(xpub)
guard payload.count == serializedXpubLength else {
throw WatchOnlyAccountError.invalidExtendedPublicKey
}
return base58CheckEncode(Data(testnetXpubVersion) + payload.dropFirst(4))
}

private static func base58CheckEncode(_ payload: Data) -> String {
let checksum = Data(SHA256.hash(data: Data(SHA256.hash(data: payload)))).prefix(4)
let bytes = [UInt8](payload + checksum)
var digits: [UInt8] = [0]
for byte in bytes {
var carry = Int(byte)
for index in digits.indices.reversed() {
carry += Int(digits[index]) << 8
digits[index] = UInt8(carry % 58)
carry /= 58
}
while carry > 0 {
digits.insert(UInt8(carry % 58), at: 0)
carry /= 58
}
}
let leadingZeros = bytes.prefix { $0 == 0 }.count
let significant = digits.drop { $0 == 0 }
let encoded = [UInt8](repeating: base58Alphabet[0], count: leadingZeros) + significant.map { base58Alphabet[Int($0)] }
return String(decoding: encoded, as: UTF8.self)
}

private static let base58Alphabet = Array("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".utf8)

private static func base58CheckDecode(_ string: String) throws -> Data {
Expand Down
59 changes: 59 additions & 0 deletions BitkitTests/LightningServiceConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,65 @@ import LDKNode
import XCTest

final class LightningServiceConfigTests: XCTestCase {
private struct BuildAttemptEnded: Error {}

func testReconcileBeforeSyncContinuesAfterAnAccountFailure() async throws {
try await LightningService.reconcileBeforeSync { throw BuildAttemptEnded() }
}

func testReconcileBeforeSyncStopsWhenCancelled() async {
do {
try await LightningService.reconcileBeforeSync { throw CancellationError() }
XCTFail("Expected cancellation to propagate")
} catch {
XCTAssertTrue(error is CancellationError)
}
}

func testBuildRetriesWithoutPaykitServerAccountsWhenTheNodeRejectsOne() {
var config = LightningService.nodeConfig(walletIndex: 0)
config.onchainWalletAccounts = [OnchainWalletAccountConfig(addressType: .nativeSegwit, accountIndex: 1, xpub: "xpub-1")]
var attempts: [[OnchainWalletAccountConfig]] = []

XCTAssertThrowsError(try LightningService.buildAcceptingPaykitServerAccounts(config: config) { config in
attempts.append(config.onchainWalletAccounts)
if !config.onchainWalletAccounts.isEmpty {
throw BuildError.InvalidSeedBytes(message: "xpub mismatch")
}
throw BuildAttemptEnded()
}) {
XCTAssertTrue($0 is BuildAttemptEnded)
}

XCTAssertEqual(attempts.map(\.count), [1, 0])
}

func testBuildDoesNotRetryWithoutPaykitServerAccounts() {
var config = LightningService.nodeConfig(walletIndex: 0)
config.onchainWalletAccounts = []
var attempts = 0

XCTAssertThrowsError(try LightningService.buildAcceptingPaykitServerAccounts(config: config) { _ in
attempts += 1
throw BuildError.InvalidSeedBytes(message: "bad seed")
})

XCTAssertEqual(attempts, 1)
}

func testBuildDoesNotRetryOnOtherBuildErrors() {
var config = LightningService.nodeConfig(walletIndex: 0)
config.onchainWalletAccounts = [OnchainWalletAccountConfig(addressType: .nativeSegwit, accountIndex: 1, xpub: "xpub-1")]
var attempts = 0

XCTAssertThrowsError(try LightningService.buildAcceptingPaykitServerAccounts(config: config) { _ in
attempts += 1
throw BuildError.WalletSetupFailed(message: "wallet")
})

XCTAssertEqual(attempts, 1)
}

func testNodeConfigTrustsNoPeer() {
let config = LightningService.nodeConfig(walletIndex: 0)

Expand Down
Loading
Loading