Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Aug 25, 2024
1 parent 6be02aa commit aa9973a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 72 deletions.
51 changes: 5 additions & 46 deletions LDKNodeMonday/Extensions/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,49 @@ import Foundation
extension String {

func bolt11amount() -> String? {
print("Entering bolt11amount() with input: \(self)")

// Updated regex pattern to be more flexible
let regex = try! NSRegularExpression(
pattern: "ln(?:bc|tb|tbs)(?<amount>\\d+)(?<multiplier>[munp]?)",
options: [.caseInsensitive]
)
print("Regex pattern: \(regex.pattern)")

if let match = regex.firstMatch(
in: self,
options: [],
range: NSRange(location: 0, length: self.utf16.count)
) {
print("Regex match found")

guard let amountRange = Range(match.range(withName: "amount"), in: self),
let multiplierRange = Range(match.range(withName: "multiplier"), in: self)
else {
print("Failed to extract amount or multiplier ranges")
return nil
}

let amountString = String(self[amountRange])
let multiplierString = String(self[multiplierRange])

print("Extracted amount: \(amountString), multiplier: \(multiplierString)")

guard let amount = Int(amountString) else {
print("Failed to convert amount to Int")
return nil
}

var conversion = Double(amount)
print("Initial conversion: \(conversion)")

switch multiplierString.lowercased() {
case "m":
conversion *= 0.001
print("Applied 'm' multiplier")
case "u":
conversion *= 0.000001
print("Applied 'u' multiplier")
case "n":
conversion *= 0.000000001
print("Applied 'n' multiplier")
case "p":
conversion *= 0.000000000001
print("Applied 'p' multiplier")
default:
print("No multiplier applied")
break
}

let convertedAmount = conversion * 100_000_000
let formattedAmount = String(format: "%.0f", convertedAmount)
print("Final converted amount: \(formattedAmount) satoshis")
return formattedAmount
} else {
print("No regex match found")
}

print("Returning nil from bolt11amount()")
return nil
}

Expand Down Expand Up @@ -181,11 +162,9 @@ extension String {
}

func processBIP21(_ input: String, spendableBalance: UInt64) -> (String, String, Payment) {
print("Processing BIP21 URI")
guard let url = URL(string: input),
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
else {
print("Failed to parse BIP21 URI")
return ("", "0", .isNone)
}

Expand All @@ -198,7 +177,7 @@ extension String {
switch item.name.lowercased() {
case "amount":
if let value = item.value, let btcAmount = Double(value) {
amount = String(format: "%.0f", btcAmount * 100_000_000) // Convert BTC to satoshis
amount = String(format: "%.0f", btcAmount * 100_000_000)
}
case "lightning":
bolt11Invoice = item.value
Expand All @@ -209,44 +188,26 @@ extension String {
}
}

// Priority: Bolt 12 > Bolt 11 > On-chain
if let offer = bolt12Offer {
print("Bolt 12 offer found in BIP21 URI")
return processLightningAddress(offer)
}

if let invoice = bolt11Invoice {
print("Bolt 11 invoice found in BIP21 URI")
return processLightningAddress(invoice)
}

print("Using on-chain Bitcoin address from BIP21 URI")
return (bitcoinAddress, amount, .isBitcoin)
}

func extractPaymentInfo(spendableBalance: UInt64) -> (
address: String, amount: String, payment: Payment
) {
// BIP 21
if self.lowercased().starts(with: "bitcoin:") && self.contains("?") {
return processBIP21(self, spendableBalance: spendableBalance)
}

// Bolt 11 JIT
// Check for BOLT11 invoice, including those prefixed with "lightning:"
if self.lowercased().starts(with: "lightning:") {
} else if self.lowercased().starts(with: "lightning:") {
let invoice = String(self.dropFirst(10)) // Remove "lightning:" prefix
return processLightningAddress(invoice)
} else if self.lowercased().starts(with: "lnbc") || self.lowercased().starts(with: "lntb") {
return processLightningAddress(self)
}

// let queryParams = self.queryParameters()
//
// if let lightningAddress = queryParams["lightning"], !lightningAddress.isEmpty {
// return processLightningAddress(lightningAddress)
// }
else if self.isBitcoinAddress {
} else if self.isBitcoinAddress {
return processBitcoinAddress(spendableBalance)
} else if self.starts(with: "lnurl") {
return ("LNURL not supported yet", "0", .isLightningURL)
Expand All @@ -260,7 +221,6 @@ extension String {
let queryParams = self.queryParameters()
let amount = queryParams["amount"] ?? "0"

// Validate the amount against the spendable balance
if let amountValue = UInt64(amount), amountValue <= spendableBalance {
return (address, amount, .isBitcoin)
} else {
Expand All @@ -281,9 +241,8 @@ extension String {

private func extractBitcoinAddress() -> String {
if self.lowercased().hasPrefix("bitcoin:") {
// Extract the address from the "bitcoin:" URI, ignoring any query parameters
let address = self.replacingOccurrences(of: "bitcoin:", with: "")
if let addressEnd = address.range(of: "?")?.lowerBound { // New: Handles query parameters in BIP21
if let addressEnd = address.range(of: "?")?.lowerBound {
return String(address[..<addressEnd]).uppercased()
}
return address.uppercased()
Expand Down
9 changes: 2 additions & 7 deletions LDKNodeMonday/Model/ReceiveOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
import Foundation

enum ReceiveOption: String, CaseIterable, Identifiable {
// case bolt11Zero = "Bolt11 0"
// case bolt11 = "Bolt11"
var id: Self { self }

case bolt11JIT = "Bolt11 JIT"
// case bolt12Zero = "Bolt12 0"
// case bolt12 = "Bolt12"
// case bitcoin = "Address"
case bip21 = "BIP21"

var id: Self { self }
}

extension ReceiveOption {
Expand Down
5 changes: 1 addition & 4 deletions LDKNodeMonday/View Model/Home/AmountViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ class AmountViewModel {

func sendPaymentBolt12(invoice: Bolt12Invoice) async {
do {
let paymentId = try await LightningNodeService.shared.send(bolt12Invoice: invoice)
print("sendPaymentBolt12 success w paymentId: \(paymentId)")
let _ = try await LightningNodeService.shared.send(bolt12Invoice: invoice)
} catch let error as NodeError {
print("sendPaymentBolt12 NodeError: \(error.localizedDescription)")
NotificationCenter.default.post(name: .ldkErrorReceived, object: error)
let errorString = handleNodeError(error)
DispatchQueue.main.async {
Expand All @@ -104,7 +102,6 @@ class AmountViewModel {
}
} catch {
DispatchQueue.main.async {
print("sendPaymentBolt12 error: \(error.localizedDescription)")
self.amountConfirmationViewError = .init(
title: "Unexpected error",
detail: error.localizedDescription
Expand Down
4 changes: 1 addition & 3 deletions LDKNodeMonday/View/Home/Receive/BIP21View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct BIP21View: View {

Button {
Task {
let amountSat = (UInt64(viewModel.amountSat) ?? 0) // * 1000
let amountSat = (UInt64(viewModel.amountSat) ?? 0)
await viewModel.receivePayment(
amountSat: amountSat,
message: "Monday Wallet",
Expand All @@ -76,7 +76,6 @@ struct BIP21View: View {
} else {

QRCodeView(qrCodeType: .bip21(viewModel.unified))
// .padding(-30.0)

VStack(spacing: 5.0) {

Expand Down Expand Up @@ -242,7 +241,6 @@ struct BIP21View: View {
}
.onAppear {
viewModel.getColor()
print("unified: \n \(viewModel.unified)")
}
.onReceive(viewModel.$receiveViewError) { errorMessage in
if errorMessage != nil {
Expand Down
12 changes: 0 additions & 12 deletions LDKNodeMonday/View/Home/Receive/ReceiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,8 @@ struct ReceiveView: View {
Spacer()

switch selectedOption {
// case .bolt11Zero:
// ZeroInvoiceView(viewModel: .init())
// case .bolt11:
// AmountInvoiceView(viewModel: .init())
case .bolt11JIT:
JITInvoiceView(viewModel: .init())
// case .bolt12Zero:
// Bolt12ZeroInvoiceView(viewModel: .init())
// case .bolt12:
// Bolt12InvoiceView(viewModel: .init())
// case .bitcoin:
// AddressView(viewModel: .init())
case .bip21:
BIP21View(viewModel: .init())
}
Expand All @@ -54,9 +44,7 @@ struct CustomSegmentedPicker: View {
}) {
VStack {
Image(systemName: option.systemImageName)
//.font(.system(size: 6))
Text(option.rawValue)
//.font(.system(size: 6))
}
.padding()
.font(.body)
Expand Down

0 comments on commit aa9973a

Please sign in to comment.