[PATCH damus] Damus Purple: Update client to work with damus-api post express refactor

5 views
Skip to first unread message

Daniel D’Aquino

unread,
Dec 27, 2023, 7:08:24 PM12/27/23
to pat...@damus.io, Daniel D’Aquino
These changes were made to adapt the client-side to the new improvements done to the Purple API server (See https://github.com/damus-io/api/issues/1)

Testing
---------

PASS

Device: iPhone 15 Pro simulator
iOS: 17.2
Damus: This commit
damus-api: bda56590be7eb47e21dfd61ab94b17f6a8595d0c
Server: Ubuntu 22.04 (VM)
Setup:
1. On the server, delete the `mdb` database files to start from scratch
2. Enable subscriptions support via developer settings with localhost test mode and restart app
3. Get Damus Purple (Purchase it via Xcode test environment)
4. Start server with mock parameters (Run `npm run dev`)
5. Restart app
Steps:
1. Post something
2. Gold star should appear beside your name
3. Look at the server logs. There should be some requests to create the account (POST), to send the receipt (POST), and to get account status. Those three requests should have returned HTTP status 200.

Signed-off-by: Daniel D’Aquino <dan...@daquino.me>
---
damus/Models/Purple/DamusPurple.swift | 12 ++++-----
damus/Nostr/NIP98AuthenticatedRequest.swift | 29 +++++++++++++++------
damus/Util/Constants.swift | 2 +-
3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/damus/Models/Purple/DamusPurple.swift b/damus/Models/Purple/DamusPurple.swift
index bfe76aee..ce84996c 100644
--- a/damus/Models/Purple/DamusPurple.swift
+++ b/damus/Models/Purple/DamusPurple.swift
@@ -62,17 +62,14 @@ class DamusPurple: StoreObserverDelegate {

func create_account(pubkey: Pubkey) async throws {
let url = environment.get_base_url().appendingPathComponent("accounts")
- let payload: [String: String] = [
- "pubkey": pubkey.hex()
- ]
- let encoded_payload = try JSONEncoder().encode(payload)

Log.info("Creating account with Damus Purple server", for: .damus_purple)

let (data, response) = try await make_nip98_authenticated_request(
method: .post,
url: url,
- payload: encoded_payload,
+ payload: nil,
+ payload_type: nil,
auth_keypair: self.keypair
)

@@ -81,7 +78,7 @@ class DamusPurple: StoreObserverDelegate {
case 200:
Log.info("Created an account with Damus Purple server", for: .damus_purple)
default:
- Log.error("Error in creating account with Damus Purple. HTTP status code: %d", for: .damus_purple, httpResponse.statusCode)
+ Log.error("Error in creating account with Damus Purple. HTTP status code: %d; Response: %s", for: .damus_purple, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown")
}
}

@@ -110,6 +107,7 @@ class DamusPurple: StoreObserverDelegate {
method: .post,
url: url,
payload: receiptData,
+ payload_type: .binary,
auth_keypair: self.keypair
)

@@ -118,7 +116,7 @@ class DamusPurple: StoreObserverDelegate {
case 200:
Log.info("Sent in-app purchase receipt to Damus Purple server successfully", for: .damus_purple)
default:
- Log.error("Error in sending in-app purchase receipt to Damus Purple. HTTP status code: %d", for: .damus_purple, httpResponse.statusCode)
+ Log.error("Error in sending in-app purchase receipt to Damus Purple. HTTP status code: %d; Response: %s", for: .damus_purple, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown")
}
}

diff --git a/damus/Nostr/NIP98AuthenticatedRequest.swift b/damus/Nostr/NIP98AuthenticatedRequest.swift
index 2d613813..84d2dd68 100644
--- a/damus/Nostr/NIP98AuthenticatedRequest.swift
+++ b/damus/Nostr/NIP98AuthenticatedRequest.swift
@@ -14,28 +14,41 @@ enum HTTPMethod: String {
case delete = "DELETE"
}

-func make_nip98_authenticated_request(method: HTTPMethod, url: URL, payload: Data, auth_keypair: Keypair) async throws -> (data: Data, response: URLResponse) {
+enum HTTPPayloadType: String {
+ case json = "application/json"
+ case binary = "application/octet-stream"
+}
+
+func make_nip98_authenticated_request(method: HTTPMethod, url: URL, payload: Data?, payload_type: HTTPPayloadType?, auth_keypair: Keypair) async throws -> (data: Data, response: URLResponse) {
var request = URLRequest(url: url)
request.httpMethod = method.rawValue
request.httpBody = payload

- let payload_hash = sha256(payload)
- let payload_hash_hex = payload_hash.map({ String(format: "%02hhx", $0) }).joined()
+ var tag_pairs = [
+ ["u", url.absoluteString],
+ ["method", method.rawValue],
+ ]

+ if let payload {
+ let payload_hash = sha256(payload)
+ let payload_hash_hex = hex_encode(payload_hash)
+ tag_pairs.append(["payload", payload_hash_hex])
+ }
+
let auth_note = NdbNote(
content: "",
keypair: auth_keypair,
kind: 27235,
- tags: [
- ["u", url.absoluteString],
- ["method", method.rawValue],
- ["payload", payload_hash_hex]
- ],
+ tags: tag_pairs,
createdAt: UInt32(Date().timeIntervalSince1970)
)
+
let auth_note_json_data: Data = try JSONEncoder().encode(auth_note)
let auth_note_base64: String = base64_encode(auth_note_json_data.bytes)

request.setValue("Nostr " + auth_note_base64, forHTTPHeaderField: "Authorization")
+ if let payload_type {
+ request.setValue(payload_type.rawValue, forHTTPHeaderField: "Content-Type")
+ }
return try await URLSession.shared.data(for: request)
}
diff --git a/damus/Util/Constants.swift b/damus/Util/Constants.swift
index 1eb5f376..1fe7d8b3 100644
--- a/damus/Util/Constants.swift
+++ b/damus/Util/Constants.swift
@@ -9,7 +9,7 @@ import Foundation

class Constants {
static let PURPLE_API_PRODUCTION_BASE_URL: URL = URL(string: "https://purple.damus.io")!
- static let PURPLE_API_TEST_BASE_URL: URL = URL(string: "http://localhost:8989")!
+ static let PURPLE_API_TEST_BASE_URL: URL = URL(string: "http://127.0.0.1:8989")!
static let DEVICE_TOKEN_RECEIVER_PRODUCTION_URL: URL = URL(string: "https://notify.damus.io:8000/user-info")!
static let DEVICE_TOKEN_RECEIVER_TEST_URL: URL = URL(string: "http://localhost:8000/user-info")!
static let EXAMPLE_DEMOS: DamusState = .empty
--
2.39.1


William Casarin

unread,
Dec 27, 2023, 9:42:29 PM12/27/23
to Daniel D’Aquino, pat...@damus.io
On Thu, Dec 28, 2023 at 12:08:07AM +0000, Daniel D’Aquino wrote:
>These changes were made to adapt the client-side to the new
>improvements done to the Purple API server (See
>https://github.com/damus-io/api/issues/1)
>
>
>Signed-off-by: Daniel D’Aquino <dan...@daquino.me>

Reviewed-by: William Casarin <jb...@jb55.com>
Reply all
Reply to author
Forward
0 new messages