lnlink

iOS app for connecting to lightning nodes
git clone git://jb55.com/lnlink
Log | Files | Refs | Submodules | README | LICENSE

lightninglinkApp.swift (2680B)


      1 //
      2 //  lightninglinkApp.swift
      3 //  lightninglink
      4 //
      5 //  Created by William Casarin on 2022-01-07.
      6 //
      7 
      8 import SwiftUI
      9 
     10 public struct Dashboard {
     11     public let info: GetInfo
     12     public let funds: ListFunds
     13 
     14     public static var empty: Dashboard = Dashboard(info: .empty, funds: .empty)
     15 }
     16 
     17 
     18 func fetch_dashboard(lnlink: LNLink) -> Either<String, Dashboard> {
     19     let ln = LNSocket()
     20 
     21     guard ln.connect_and_init(node_id: lnlink.node_id, host: lnlink.host) else {
     22         return .left("Connect failed :(")
     23     }
     24 
     25     let res = rpc_getinfo(ln: ln, token: lnlink.token)
     26     switch res {
     27     case .failure(let res_err):
     28         return .left(res_err.description )
     29     case .success(let info):
     30         let res2 = rpc_listfunds(ln: ln, token: lnlink.token)
     31         switch res2 {
     32         case .failure(let err):
     33             return .left(err.description)
     34         case .success(let funds):
     35             return .right(Dashboard(info: info, funds: funds))
     36         }
     37     }
     38 }
     39 
     40 @main
     41 struct lightninglinkApp: App {
     42     init() {
     43         preventCrash()
     44     }
     45     var body: some Scene {
     46         WindowGroup {
     47             SetupView()
     48         }
     49     }
     50 }
     51 
     52 func fetch_info(ln: LNSocket, token: String) -> GetInfo {
     53     switch rpc_getinfo(ln: ln, token: token) {
     54     case .failure(let err):
     55         print("fetch_info err: \(err)")
     56         return .empty
     57 
     58     case .success(let getinfo):
     59         return getinfo
     60     }
     61 }
     62 
     63 func fetch_funds(ln: LNSocket, token: String) -> ListFunds {
     64     switch rpc_listfunds(ln: ln, token: token) {
     65     case .failure(let err):
     66         print("fetch_funds error: \(err)")
     67         return .empty
     68     case .success(let funds):
     69         return funds
     70     }
     71 }
     72 
     73 func save_lnlink(lnlink: LNLink) {
     74     UserDefaults.standard.set(lnlink.token, forKey: "lnlink_token")
     75     UserDefaults.standard.set(lnlink.node_id, forKey: "lnlink_nodeid")
     76     UserDefaults.standard.set(lnlink.host, forKey: "lnlink_host")
     77 }
     78 
     79 func reset_lnlink() {
     80     UserDefaults.standard.removeObject(forKey: "lnlink_token")
     81     UserDefaults.standard.removeObject(forKey: "lnlink_nodeid")
     82     UserDefaults.standard.removeObject(forKey: "lnlink_host")
     83 }
     84 
     85 func load_lnlink() -> LNLink? {
     86     let m_token = UserDefaults.standard.string(forKey: "lnlink_token")
     87     let m_nodeid = UserDefaults.standard.string(forKey: "lnlink_nodeid")
     88     let m_host = UserDefaults.standard.string(forKey: "lnlink_host")
     89 
     90     guard let token = m_token else { return nil }
     91     guard let node_id = m_nodeid else { return nil }
     92     guard let host = m_host else { return nil }
     93 
     94     return LNLink(token: token, host: host, node_id: node_id)
     95 }
     96 
     97 private extension lightninglinkApp {
     98     func preventCrash() {
     99         signal(SIGPIPE, SIG_IGN)
    100     }
    101 }