Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: lightningnodeclient #152

Merged
merged 5 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 12 additions & 5 deletions LDKNodeMonday/App/LDKNodeMondayApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct LDKNodeMondayApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

private let lightningClient: LightningNodeClient = .live
@State private var appState = AppState.loading
@State private var appError: Error?
@State private var navigationPath = NavigationPath()
Expand All @@ -21,10 +22,16 @@ struct LDKNodeMondayApp: App {
NavigationStack(path: $navigationPath) {
switch appState {
case .onboarding:
OnboardingView(viewModel: .init(appState: $appState))
OnboardingView(
viewModel: .init(appState: $appState, lightningClient: lightningClient)
)
case .wallet:
BitcoinView(
viewModel: .init(appState: $appState, priceClient: .live),
viewModel: .init(
appState: $appState,
priceClient: .live,
lightningClient: lightningClient
),
sendNavigationPath: $navigationPath
)
case .error:
Expand All @@ -50,8 +57,8 @@ struct LDKNodeMondayApp: App {
if backupInfo != nil {
do {
// TODO: .start could take parameters from backupInfo (seed, network, url, lsp)
try await LightningNodeService.shared.start()
LightningNodeService.shared.listenForEvents()
try await lightningClient.start()
lightningClient.listenForEvents()
await MainActor.run {
self.appState = .wallet
}
Expand All @@ -72,7 +79,7 @@ struct LDKNodeMondayApp: App {

class AppDelegate: NSObject, UIApplicationDelegate {
func applicationWillTerminate(_ application: UIApplication) {
try? LightningNodeService.shared.stop()
try? LightningNodeClient.live.stop()
}
}

Expand Down
150 changes: 150 additions & 0 deletions LDKNodeMonday/Service/Lightning Service/LightningNodeService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,153 @@ extension LightningNodeService {
try FileManager.default.deleteAllContentsInDocumentsDirectory()
}
}

struct LightningNodeClient {
let start: () async throws -> Void
let stop: () throws -> Void
let nodeId: () -> String
let newAddress: () async throws -> String
let spendableOnchainBalanceSats: () async -> UInt64
let totalOnchainBalanceSats: () async -> UInt64
let totalLightningBalanceSats: () async -> UInt64
let lightningBalances: () async -> [LightningBalance]
let pendingBalancesFromChannelClosures: () async -> [PendingSweepBalance]
let connect: (PublicKey, String, Bool) async throws -> Void
let disconnect: (PublicKey) throws -> Void
let connectOpenChannel:
(PublicKey, String, UInt64, UInt64?, ChannelConfig?, Bool) async throws -> UserChannelId
let closeChannel: (ChannelId, PublicKey) throws -> Void
let send: (String) async throws -> QrPaymentResult
let receive: (UInt64, String, UInt32) async throws -> String
let receiveViaJitChannel: (UInt64, String, UInt32, UInt64?) async throws -> Bolt11Invoice
let listPeers: () -> [PeerDetails]
let listChannels: () -> [ChannelDetails]
let listPayments: () -> [PaymentDetails]
let status: () -> NodeStatus
let deleteWallet: () throws -> Void
let getBackupInfo: () throws -> BackupInfo
let deleteDocuments: () throws -> Void
let getNetwork: () -> Network
let getNetworkColor: () -> Color
let listenForEvents: () -> Void
}

extension LightningNodeClient {
static let live = Self(
start: { try await LightningNodeService.shared.start() },
stop: { try LightningNodeService.shared.stop() },
nodeId: { LightningNodeService.shared.nodeId() },
newAddress: { try await LightningNodeService.shared.newAddress() },
spendableOnchainBalanceSats: {
await LightningNodeService.shared.spendableOnchainBalanceSats()
},
totalOnchainBalanceSats: { await LightningNodeService.shared.totalOnchainBalanceSats() },
totalLightningBalanceSats: {
await LightningNodeService.shared.totalLightningBalanceSats()
},
lightningBalances: { await LightningNodeService.shared.lightningBalances() },
pendingBalancesFromChannelClosures: {
await LightningNodeService.shared.pendingBalancesFromChannelClosures()
},
connect: { nodeId, address, persist in
try await LightningNodeService.shared.connect(
nodeId: nodeId,
address: address,
persist: persist
)
},
disconnect: { nodeId in try LightningNodeService.shared.disconnect(nodeId: nodeId) },
connectOpenChannel: { nodeId, address, amount, pushMsat, config, announce in
try await LightningNodeService.shared.connectOpenChannel(
nodeId: nodeId,
address: address,
channelAmountSats: amount,
pushToCounterpartyMsat: pushMsat,
channelConfig: config,
announceChannel: announce
)
},
reez marked this conversation as resolved.
Show resolved Hide resolved
closeChannel: { channelId, nodeId in
try LightningNodeService.shared.closeChannel(
userChannelId: channelId,
counterpartyNodeId: nodeId
)
},
send: { uriStr in try await LightningNodeService.shared.send(uriStr: uriStr) },
receive: { amount, message, expiry in
try await LightningNodeService.shared.receive(
amountSat: amount,
message: message,
expirySec: expiry
)
},
receiveViaJitChannel: { amount, description, expiry, maxFee in
try await LightningNodeService.shared.receiveViaJitChannel(
amountMsat: amount,
description: description,
expirySecs: expiry,
maxLspFeeLimitMsat: maxFee
)
},
listPeers: { LightningNodeService.shared.listPeers() },
listChannels: { LightningNodeService.shared.listChannels() },
listPayments: { LightningNodeService.shared.listPayments() },
status: { LightningNodeService.shared.status() },
deleteWallet: { try LightningNodeService.shared.deleteWallet() },
getBackupInfo: { try LightningNodeService.shared.getBackupInfo() },
deleteDocuments: { try LightningNodeService.shared.deleteDocuments() },
getNetwork: { LightningNodeService.shared.network },
getNetworkColor: { LightningNodeService.shared.networkColor },
listenForEvents: {}
reez marked this conversation as resolved.
Show resolved Hide resolved
)
}

#if DEBUG
extension LightningNodeClient {
static let mock = Self(
start: {},
stop: {},
nodeId: { "038474837483784378437843784378437843784378" },
newAddress: { "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx" },
spendableOnchainBalanceSats: { 100_000 },
totalOnchainBalanceSats: { 150_000 },
totalLightningBalanceSats: { 50_000 },
lightningBalances: { [] },
pendingBalancesFromChannelClosures: { [] },
connect: { _, _, _ in },
disconnect: { _ in },
connectOpenChannel: { _, _, _, _, _, _ in UserChannelId("abcdef") },
closeChannel: { _, _ in },
send: { _ in QrPaymentResult.onchain(txid: "txid") },
receive: { _, _, _ in "lightning:lnbc1..." },
receiveViaJitChannel: { _, _, _, _ in Bolt11Invoice("lnbc1...") },
listPeers: { [] },
listChannels: { [] },
listPayments: { [] },
status: {
NodeStatus(
isRunning: true,
isListening: true,
currentBestBlock: BestBlock(
blockHash: BlockHash(
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
),
height: 123456
),
latestLightningWalletSyncTimestamp: UInt64(Date().timeIntervalSince1970),
latestOnchainWalletSyncTimestamp: UInt64(Date().timeIntervalSince1970),
latestFeeRateCacheUpdateTimestamp: UInt64(Date().timeIntervalSince1970),
latestRgsSnapshotTimestamp: UInt64(Date().timeIntervalSince1970),
latestNodeAnnouncementBroadcastTimestamp: UInt64(Date().timeIntervalSince1970),
latestChannelMonitorArchivalHeight: 123456
)
},
deleteWallet: {},
getBackupInfo: { BackupInfo(mnemonic: "test test test") },
deleteDocuments: {},
getNetwork: { .signet },
getNetworkColor: { .orange },
listenForEvents: {}
)
}
#endif
22 changes: 15 additions & 7 deletions LDKNodeMonday/View Model/Home/BitcoinViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,58 @@ class BitcoinViewModel: ObservableObject {
@Published var isTotalBalanceFinished: Bool = false
@Published var isTotalLightningBalanceFinished: Bool = false
@Published var isPriceFinished: Bool = false

let lightningClient: LightningNodeClient
let priceClient: PriceClient
var price: Double = 0.00
var time: Int?

var satsPrice: String {
let usdValue = Double(totalBalance).valueInUSD(price: price)
return usdValue
}

var totalUSDValue: String {
let totalUSD = Double(totalBalance + totalLightningBalance).valueInUSD(price: price)
return totalUSD
}

init(appState: Binding<AppState>, priceClient: PriceClient) {
init(
appState: Binding<AppState>,
priceClient: PriceClient,
lightningClient: LightningNodeClient
) {
_appState = appState
self.priceClient = priceClient
self.lightningClient = lightningClient
}

func getStatus() async {
let status = LightningNodeService.shared.status()
let status = lightningClient.status()
DispatchQueue.main.async {
self.status = status
self.isStatusFinished = true
}
}

func getTotalOnchainBalanceSats() async {
let balance = await LightningNodeService.shared.totalOnchainBalanceSats()
let balance = await lightningClient.totalOnchainBalanceSats()
DispatchQueue.main.async {
self.totalBalance = balance
self.isTotalBalanceFinished = true
}
}

func getSpendableOnchainBalanceSats() async {
let balance = await LightningNodeService.shared.spendableOnchainBalanceSats()
let balance = await lightningClient.spendableOnchainBalanceSats()
DispatchQueue.main.async {
self.spendableBalance = balance
self.isSpendableBalanceFinished = true
}
}

func getTotalLightningBalanceSats() async {
let balance = await LightningNodeService.shared.totalLightningBalanceSats()
let balance = await lightningClient.totalLightningBalanceSats()
DispatchQueue.main.async {
self.totalLightningBalance = balance
self.isTotalLightningBalanceFinished = true
Expand Down Expand Up @@ -95,10 +104,9 @@ class BitcoinViewModel: ObservableObject {
}

func getColor() {
let color = LightningNodeService.shared.networkColor
let color = lightningClient.getNetworkColor()
DispatchQueue.main.async {
self.networkColor = color
}
}

}
30 changes: 18 additions & 12 deletions LDKNodeMonday/View Model/Home/OnboardingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import SwiftUI

class OnboardingViewModel: ObservableObject {
@Binding var appState: AppState
private let lightningClient: LightningNodeClient
private let keyClient: KeyClient
@Published var networkColor = Color.gray
@Published var onboardingViewError: MondayError?
@Published var seedPhrase: String = "" {
Expand All @@ -22,10 +24,10 @@ class OnboardingViewModel: ObservableObject {
didSet {
do {
let networkString = selectedNetwork.description
try KeyClient.live.saveNetwork(networkString)
try keyClient.saveNetwork(networkString)
self.selectedEsploraServer =
availableEsploraServers.first ?? EsploraServer(name: "", url: "")
try KeyClient.live.saveEsploraURL(selectedEsploraServer.url)
try keyClient.saveEsploraURL(selectedEsploraServer.url)
} catch {
DispatchQueue.main.async {
self.onboardingViewError = .init(
Expand All @@ -40,7 +42,7 @@ class OnboardingViewModel: ObservableObject {
{
didSet {
do {
try KeyClient.live.saveEsploraURL(selectedEsploraServer.url)
try keyClient.saveEsploraURL(selectedEsploraServer.url)
} catch {
DispatchQueue.main.async {
self.onboardingViewError = .init(
Expand Down Expand Up @@ -77,14 +79,20 @@ class OnboardingViewModel: ObservableObject {
}
}

init(appState: Binding<AppState>) {
init(
appState: Binding<AppState>,
lightningClient: LightningNodeClient,
keyClient: KeyClient = .live
) {
_appState = appState
self.lightningClient = lightningClient
self.keyClient = keyClient

do {
if let networkString = try KeyClient.live.getNetwork() {
if let networkString = try keyClient.getNetwork() {
self.selectedNetwork = Network(stringValue: networkString) ?? .signet
}
if let esploraURL = try KeyClient.live.getEsploraURL() {
if let esploraURL = try keyClient.getEsploraURL() {
self.selectedEsploraServer =
availableEsploraServers.first(where: {
$0.url == esploraURL
Expand All @@ -98,16 +106,14 @@ class OnboardingViewModel: ObservableObject {
)
}
}

}

func saveSeed() {
do {
let backupInfo = BackupInfo(mnemonic: seedPhrase)
try KeyClient.live.saveBackupInfo(backupInfo)
try KeyClient.live.saveNetwork(selectedNetwork.description)
try KeyClient.live.saveEsploraURL(selectedEsploraServer.url)
LightningNodeService.shared = LightningNodeService()
try keyClient.saveBackupInfo(backupInfo)
try keyClient.saveNetwork(selectedNetwork.description)
try keyClient.saveEsploraURL(selectedEsploraServer.url)
DispatchQueue.main.async {
self.appState = .wallet
}
Expand All @@ -130,7 +136,7 @@ class OnboardingViewModel: ObservableObject {
}

func getColor() {
let color = LightningNodeService.shared.networkColor
let color = lightningClient.getNetworkColor()
DispatchQueue.main.async {
self.networkColor = color
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import SwiftUI

class PaymentsViewModel: ObservableObject {
@Published var payments: [PaymentDetails] = []
private let lightningClient: LightningNodeClient

func listPayments() {
self.payments = LightningNodeService.shared.listPayments()
init(lightningClient: LightningNodeClient) {
self.lightningClient = lightningClient
}

func listPayments() {
self.payments = lightningClient.listPayments()
}
}
Loading