lnlink

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

commit 45e2d55f14a2f015f3cc9a6b93589c6da3f08d50
parent 16f910c035c946f06888775b642bddd65706f2cc
Author: William Casarin <jb55@jb55.com>
Date:   Sat,  8 Oct 2022 09:21:09 -0700

backwards-compatible msat type change

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mlightninglink.xcodeproj/project.pbxproj | 4++--
Mlightninglink/RPC.swift | 44+++++++++++++++++++++++++++++++++++---------
Mlightninglink/Views/ContentView.swift | 10+++++-----
Mlightninglink/Views/PayView.swift | 4++--
4 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/lightninglink.xcodeproj/project.pbxproj b/lightninglink.xcodeproj/project.pbxproj @@ -590,7 +590,7 @@ "$(PROJECT_DIR)/lnsocket/target/ios", ); MACOSX_DEPLOYMENT_TARGET = 12.2; - MARKETING_VERSION = 0.4.1; + MARKETING_VERSION = 0.4.2; PRODUCT_BUNDLE_IDENTIFIER = com.jb55.lightninglink; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; @@ -631,7 +631,7 @@ "$(PROJECT_DIR)/lnsocket/target/ios", ); MACOSX_DEPLOYMENT_TARGET = 12.2; - MARKETING_VERSION = 0.4.1; + MARKETING_VERSION = 0.4.2; PRODUCT_BUNDLE_IDENTIFIER = com.jb55.lightninglink; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; diff --git a/lightninglink/RPC.swift b/lightninglink/RPC.swift @@ -31,7 +31,7 @@ public struct Output: Decodable { //public var txid: String //public var output: Int //public var value: Int64 - public var amount_msat: Int64 + public var amount_msat: MSat //public var scriptpubkey: String //public var address: String //public var status: String @@ -43,8 +43,8 @@ public struct Channel: Decodable { //public var peer_id: String //public var connected: Bool //public var state: String - public var our_amount_msat: Int64 - public var amount_msat: Int64 + public var our_amount_msat: MSat + public var amount_msat: MSat } public struct InvoiceRes: Decodable { @@ -96,7 +96,7 @@ public enum Decode { func amount_msat() -> Int64? { switch self { case .invoice(let inv): - return inv.amount_msat + return inv.amount_msat?.msat case .lnurlp: return nil } @@ -114,7 +114,7 @@ public struct InvoiceDecode: Decodable { public var quantity_min: Int? public var description: String? public var node_id: String? - public var amount_msat: Int64? + public var amount_msat: MSat? public var vendor: String? } @@ -148,23 +148,49 @@ public struct Pay: Decodable { public var payment_hash: String public var created_at: Float public var parts: Int - public var amount_msat: Int64 - public var amount_sent_msat: Int64 + public var amount_msat: MSat + public var amount_sent_msat: MSat public var payment_preimage: String public var status: String } +public struct MSat: Decodable { + public var msat: Int64 + + public init(msats: Int64) { + self.msat = msats + } + + public init(from decoder: Decoder) throws { + if let int = try? decoder.singleValueContainer().decode(Int64.self) { + self.msat = int + return + } + if let string = try? decoder.singleValueContainer().decode(String.self) { + if let msat = parse_msat(string) { + self.msat = msat + return + } + } + throw MSatError.missingValue + } + + public enum MSatError: Error { + case missingValue + } +} + public struct GetInfo: Decodable { public var alias: String public var id: String public var color: String public var network: String public var num_peers: Int - public var fees_collected_msat: Int + public var fees_collected_msat: MSat public var num_active_channels: Int public var blockheight: Int - public static var empty = GetInfo(alias: "", id: "", color: "", network: "", num_peers: 0, fees_collected_msat: 0, num_active_channels: 0, blockheight: 0) + public static var empty = GetInfo(alias: "", id: "", color: "", network: "", num_peers: 0, fees_collected_msat: .init(msats: 0), num_active_channels: 0, blockheight: 0) } public enum RequestErrorType: Error { diff --git a/lightninglink/Views/ContentView.swift b/lightninglink/Views/ContentView.swift @@ -69,11 +69,11 @@ struct Funds { for channel in channels { //our_sats += channel.our_amount_msat - channel_sats += channel.our_amount_msat / 1000 + channel_sats += channel.our_amount_msat.msat / 1000 } for output in outputs { - onchain_sats += output.amount_msat / 1000 + onchain_sats += output.amount_msat.msat / 1000 } return Funds(onchain_sats: onchain_sats, channel_sats: channel_sats) @@ -116,8 +116,8 @@ struct ContentView: View { return "" } - let fee = pay.amount_sent_msat - pay.amount_msat - return "-\(render_amount_msats(pay.amount_msat)) (\(render_amount_msats(fee)) fee)" + let fee = pay.amount_sent_msat.msat - pay.amount_msat.msat + return "-\(render_amount_msats(pay.amount_msat.msat)) (\(render_amount_msats(fee)) fee)" } func receive_pay() { @@ -156,7 +156,7 @@ struct ContentView: View { } HStack { - Text("\(self.dashboard.info.fees_collected_msat / 1000) sats earned") + Text("\(self.dashboard.info.fees_collected_msat.msat / 1000) sats earned") .font(.footnote) .foregroundColor(.gray) diff --git a/lightninglink/Views/PayView.swift b/lightninglink/Views/PayView.swift @@ -592,7 +592,7 @@ struct PayView: View { return } - amount = .amount(amt) + amount = .amount(amt.msat) } self.state = .ready(Invoice(invstr: inv, amount: amount)) @@ -788,7 +788,7 @@ func handle_bolt12_offer(ln: LNSocket, decoded: InvoiceDecode, inv: String) -> E guard let min_amt = decoded.amount_msat else { return .left("Error parsing amount_msat: '\(decoded.amount_msat!)'") } - let offer = Offer(offer: inv, amount: .min(min_amt), decoded: decoded) + let offer = Offer(offer: inv, amount: .min(min_amt.msat), decoded: decoded) return .right(.invoice_request(.offer(offer))) } else { let offer = Offer(offer: inv, amount: .any, decoded: decoded)