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
32,418 changes: 29,499 additions & 2,919 deletions macadamia/AppAssets/Localizable.xcstrings

Large diffs are not rendered by default.

433 changes: 433 additions & 0 deletions macadamia/Misc/NUT26Codec.swift

Large diffs are not rendered by default.

39 changes: 32 additions & 7 deletions macadamia/Misc/QRView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,52 @@ struct QRView: View {
/// A view that displays a static QR code
struct StaticQRView: View {
let string: String

@State private var image: UIImage? = nil


enum QRState {
case loading
case success(UIImage)
case failed
}

@State private var state: QRState = .loading

private let qrGenerator = QRCodeGenerator()

var body: some View {
Group {
if let image {
switch state {
case .loading:
HStack {
Spacer()
ProgressView()
Spacer()
}
case .success(let image):
Image(uiImage: image)
.interpolation(.none)
.resizable()
.scaledToFit()
.clipShape(RoundedRectangle(cornerRadius: 6.0))
} else {
.transition(.opacity)
case .failed:
Text("Failed to generate QR Code")
.transition(.opacity)
}
}
.animation(.easeInOut(duration: 0.3), value: isLoaded)
.task {
self.image = await qrGenerator.generateQRCode(from: string)
if let image = await qrGenerator.generateQRCode(from: string) {
self.state = .success(image)
} else {
self.state = .failed
}
}
}

private var isLoaded: Bool {
if case .loading = state { return false }
return true
}
}

struct AnimatedQRView: View {
Expand Down
19 changes: 13 additions & 6 deletions macadamia/PersistentModelV1/Operations/send.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ extension AppSchemaV1 {

selection.selected.setState(.pending)

let sendResult = try await CashuSwift.send(inputs: selection.selected.sendable(),
mint: CashuSwift.Mint(mint),
amount: amount,
seed: activeWallet.seed,
memo: memo,
lockToPublicKey: lockingKey)
let sendResult: CashuSwift.SendResult

do {
sendResult = try await CashuSwift.send(inputs: selection.selected.sendable(),
mint: CashuSwift.Mint(mint),
amount: amount,
seed: activeWallet.seed,
memo: memo,
lockToPublicKey: lockingKey)
} catch {
selection.selected.setState(.valid)
throw error
}

selection.selected.setState(.spent)

Expand Down
2 changes: 1 addition & 1 deletion macadamia/Wallet/Contactless.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ struct Contactless: View {
input = input.replacingOccurrences(of: "cashu:", with: "")

do {
return try CashuSwift.PaymentRequest(encodedRequest: input)
return try parsePaymentRequest(input)
} catch {
throw NFCPaymentError.invalidPaymentRequest(error.localizedDescription)
}
Expand Down
4 changes: 3 additions & 1 deletion macadamia/Wallet/MintView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ struct MintView: View {
}
.onAppear { // start the polling timer only when a quote is shown
pollingTimer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { _ in
checkInvoiceState()
Task { @MainActor in
checkInvoiceState()
}
})
}

Expand Down
13 changes: 13 additions & 0 deletions macadamia/Wallet/Redeem/RedeemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ struct RedeemView<AdditionalControls: View>: View {
buttonState = .idle(String(localized: "Transfer"), action: { swap(to: swapTargetMint) })
}
}
.disabled(swapMintsEmpty)
.opacity(swapMintsEmpty ? 0.6 : 1.0)
} footer: {
Text("If you do not trust the mint of this token, you can swap its value to one of your trusted mints via Lightning (will incur fees).")
}
Expand All @@ -225,6 +227,10 @@ struct RedeemView<AdditionalControls: View>: View {
.disabled(buttonState.type == .loading)
}

private var swapMintsEmpty: Bool {
activeWallet?.mints.filter(not: \.hidden).isEmpty ?? true
}

// MARK: - ADD

private func addAndRedeem(mintURLstring: String) {
Expand Down Expand Up @@ -460,3 +466,10 @@ extension Optional where Wrapped == String {
return url.strippingHTTPPrefix()
}
}

extension Sequence {
func filter(not keyPath: KeyPath<Element, Bool>) -> [Element] {
filter { !$0[keyPath: keyPath] }
}
}

2 changes: 1 addition & 1 deletion macadamia/Wallet/RequestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct RequestView: View {
.disabled(paymentRequest != nil)
}

if let paymentRequest, let string = try? paymentRequest.serialize() {
if let paymentRequest, let string = try? NUT26.encode(paymentRequest) {
Section {
StaticQRView(string: string)
Button {
Expand Down
2 changes: 1 addition & 1 deletion macadamia/Wallet/WalletView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct WalletView: View {
navigationDestination = .receive(urlString: result.payload)
case .creq:
do {
let req = try CashuSwift.PaymentRequest(encodedRequest: result.payload)
let req = try parsePaymentRequest(result.payload)
navigationDestination = .reqPay(req: req)
} catch {
displayAlert(alert: AlertDetail(with: error))
Expand Down
115 changes: 115 additions & 0 deletions macadamiaTests/macadamiaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,119 @@ final class macadamiaTests: XCTestCase {
let transactions = BalanceCalculator<String>.calculateTransactions(for: deltas)
XCTAssertEqual(transactions.count, 0, "Empty input should produce no transactions")
}

// MARK: - NUT-26 Codec Tests

func testNUT26EncodeDecodeRoundtrip() throws {
let original = CashuSwift.PaymentRequest(
paymentId: "demo123",
amount: 1000,
unit: "sat",
singleUse: true,
mints: ["https://mint.example.com"],
description: "Coffee payment",
transports: nil,
lockingCondition: nil
)

let encoded = try NUT26.encode(original)
XCTAssertTrue(encoded.hasPrefix("CREQB1"), "NUT-26 output must start with CREQB1")

let decoded = try NUT26.decode(encoded)
XCTAssertEqual(decoded.paymentId, original.paymentId)
XCTAssertEqual(decoded.amount, original.amount)
XCTAssertEqual(decoded.unit, original.unit)
XCTAssertEqual(decoded.singleUse, original.singleUse)
XCTAssertEqual(decoded.mints, original.mints)
XCTAssertEqual(decoded.description, original.description)
}

func testNUT26DecodeSpecVector() throws {
// Example vector from the NUT-26 specification
let specVector = "CREQB1QYQQWER9D4HNZV3NQGQQSQQQQQQQQQQRAQPSQQGQQSQQZQG9QQVXSAR5WPEN5TE0D45KUAPWV4UXZMTSD3JJUCM0D5RQQRJRDANXVET9YPCXZ7TDV4H8GXHR3TQ"
let decoded = try NUT26.decode(specVector)
XCTAssertEqual(decoded.paymentId, "demo123")
XCTAssertEqual(decoded.amount, 1000)
XCTAssertEqual(decoded.unit, "sat")
XCTAssertEqual(decoded.singleUse, true)
XCTAssertEqual(decoded.mints, ["https://mint.example.com"])
XCTAssertEqual(decoded.description, "Coffee payment")
}

func testNUT26DecodeIsCaseInsensitive() throws {
let upper = "CREQB1QYQQWER9D4HNZV3NQGQQSQQQQQQQQQQRAQPSQQGQQSQQZQG9QQVXSAR5WPEN5TE0D45KUAPWV4UXZMTSD3JJUCM0D5RQQRJRDANXVET9YPCXZ7TDV4H8GXHR3TQ"
let lower = upper.lowercased()
let fromUpper = try NUT26.decode(upper)
let fromLower = try NUT26.decode(lower)
XCTAssertEqual(fromUpper.paymentId, fromLower.paymentId)
XCTAssertEqual(fromUpper.amount, fromLower.amount)
}

func testNUT26RoundtripAllFields() throws {
let original = CashuSwift.PaymentRequest(
paymentId: "test-id-42",
amount: 21000,
unit: "msat",
singleUse: false,
mints: ["https://mint.a.com", "https://mint.b.com"],
description: "Multi-mint request",
transports: [CashuSwift.Transport(type: "post", target: "https://callback.example.com/pay")],
lockingCondition: nil
)

let encoded = try NUT26.encode(original)
let decoded = try NUT26.decode(encoded)

XCTAssertEqual(decoded.paymentId, original.paymentId)
XCTAssertEqual(decoded.amount, original.amount)
XCTAssertEqual(decoded.unit, original.unit)
XCTAssertEqual(decoded.singleUse, original.singleUse)
XCTAssertEqual(decoded.mints, original.mints)
XCTAssertEqual(decoded.description, original.description)
XCTAssertEqual(decoded.transports?.count, 1)
XCTAssertEqual(decoded.transports?.first?.type, "post")
XCTAssertEqual(decoded.transports?.first?.target, "https://callback.example.com/pay")
}

func testInputValidatorDetectsCreqb() {
let creqbVector = "CREQB1QYQQWER9D4HNZV3NQGQQSQQQQQQQQQQRAQPSQQGQQSQQZQG9QQVXSAR5WPEN5TE0D45KUAPWV4UXZMTSD3JJUCM0D5RQQRJRDANXVET9YPCXZ7TDV4H8GXHR3TQ"
let result = InputValidator.validate(creqbVector, supportedTypes: [.creq])
if case .valid(let r) = result {
XCTAssertEqual(r.type, .creq)
} else {
XCTFail("creqb string should be detected as .creq")
}
}

func testParsePaymentRequestDispatch() throws {
// NUT-18 format
let nut18 = try CashuSwift.PaymentRequest(
paymentId: "abc",
amount: 500,
unit: "sat",
singleUse: nil,
mints: nil,
description: nil,
transports: nil,
lockingCondition: nil
).serialize()
XCTAssertTrue(nut18.hasPrefix("creqA"))
let fromNUT18 = try parsePaymentRequest(nut18)
XCTAssertEqual(fromNUT18.paymentId, "abc")

// NUT-26 format
let nut26 = try NUT26.encode(CashuSwift.PaymentRequest(
paymentId: "xyz",
amount: 100,
unit: "sat",
singleUse: nil,
mints: nil,
description: nil,
transports: nil,
lockingCondition: nil
))
XCTAssertTrue(nut26.hasPrefix("CREQB1"))
let fromNUT26 = try parsePaymentRequest(nut26)
XCTAssertEqual(fromNUT26.paymentId, "xyz")
}
}