purple: add support for LN checkout flow

This commit adds support for the LN checkout flow, and the Purple
landing page:

1. It adds a "learn more" button on the Damus Purple view, where the
   user can learn more
2. It adds new `damus:purple` urls to enable the LN checkout flow

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Daniel D’Aquino
2024-01-14 21:55:04 +00:00
committed by William Casarin
parent 75a9b4df7f
commit 534969e616
8 changed files with 243 additions and 35 deletions

View File

@@ -156,6 +156,30 @@ class DamusPurple: StoreObserverDelegate {
throw PurpleError.translation_no_response
}
}
func verify_npub_for_checkout(checkout_id: String) async throws {
var url = environment.get_base_url()
url.append(path: "/ln-checkout/\(checkout_id)/verify")
let (data, response) = try await make_nip98_authenticated_request(
method: .put,
url: url,
payload: nil,
payload_type: nil,
auth_keypair: self.keypair
)
if let httpResponse = response as? HTTPURLResponse {
switch httpResponse.statusCode {
case 200:
Log.info("Verified npub for checkout id `%s` with Damus Purple server", for: .damus_purple, checkout_id)
default:
Log.error("Error in verifying npub with Damus Purple. HTTP status code: %d; Response: %s; Checkout id: ", for: .damus_purple, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown", checkout_id)
throw PurpleError.checkout_npub_verification_error
}
}
}
}
// MARK: API types
@@ -189,6 +213,7 @@ extension DamusPurple {
enum PurpleError: Error {
case translation_error(status_code: Int, response: Data)
case translation_no_response
case checkout_npub_verification_error
}
struct TranslationResult: Codable {

View File

@@ -0,0 +1,43 @@
//
// DamusPurpleURL.swift
// damus
//
// Created by Daniel Nogueira on 2024-01-13.
//
import Foundation
enum DamusPurpleURL {
case verify_npub(checkout_id: String)
case welcome(checkout_id: String)
case landing
static func from_url(url: URL) -> DamusPurpleURL? {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return nil }
guard components.scheme == "damus" else { return nil }
switch components.path {
case "purple:verify":
guard let checkout_id: String = components.queryItems?.first(where: { $0.name == "id" })?.value else { return nil }
return .verify_npub(checkout_id: checkout_id)
case "purple:welcome":
guard let checkout_id: String = components.queryItems?.first(where: { $0.name == "id" })?.value else { return nil }
return .welcome(checkout_id: checkout_id)
case "purple:landing":
return .landing
default:
return nil
}
}
func url_string() -> String {
switch self {
case .verify_npub(let id):
return "damus:purple:verify?id=\(id)"
case .welcome(let id):
return "damus:purple:welcome?id=\(id)"
case .landing:
return "damus:purple:landing"
}
}
}