Skip to content

Commit

Permalink
purple: update client to work with damus-api post express refactor
Browse files Browse the repository at this point in the history
these changes were made to adapt the client-side to the new improvements
done to the Purple API server (See
damus-io/api#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 <[email protected]>
Reviewed-by: William Casarin <[email protected]>
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
danieldaquino authored and jb55 committed Dec 28, 2023
1 parent 5ca5420 commit 39b6dfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
12 changes: 5 additions & 7 deletions damus/Models/Purple/DamusPurple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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")
}
}

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

Expand All @@ -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")
}
}

Expand Down
29 changes: 21 additions & 8 deletions damus/Nostr/NIP98AuthenticatedRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion damus/Util/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39b6dfb

Please sign in to comment.