commit aaddbd847ab90bc85c63f314b0b28d08b1ae6923
parent 8b020e2bd60eaeb902726eadea44b095e0c2ac4b
Author: William Casarin <jb55@jb55.com>
Date: Wed, 28 Aug 2024 10:49:38 +0300
Merge 'Fix AlbyHub zaps'
William Casarin (2):
Fix AlbyHub zaps
Diffstat:
2 files changed, 39 insertions(+), 20 deletions(-)
diff --git a/damus/Util/Zap.swift b/damus/Util/Zap.swift
@@ -309,14 +309,10 @@ struct Zap {
return nil
}
*/
- guard let desc = get_zap_description(zap_ev, inv_desc: zap_invoice.description) else {
+ guard let zap_req = get_zap_request(zap_ev) else {
return nil
}
- guard let zap_req = decode_nostr_event_json(desc) else {
- return nil
- }
-
guard validate_event(ev: zap_req) == .ok else {
return nil
}
@@ -399,21 +395,12 @@ func event_has_tag(ev: NostrEvent, tag: String) -> Bool {
return false
}
-/// Fetches the description from either the invoice, or tags, depending on the type of invoice
-func get_zap_description(_ ev: NostrEvent, inv_desc: InvoiceDescription) -> String? {
- switch inv_desc {
- case .description(let string):
- return string
- case .description_hash(let deschash):
- guard let desc = event_tag(ev, name: "description") else {
- return nil
- }
- guard let data = desc.data(using: .utf8) else {
- return nil
- }
-
- return desc
+func get_zap_request(_ ev: NostrEvent) -> NostrEvent? {
+ guard let desc = event_tag(ev, name: "description") else {
+ return nil
}
+
+ return decode_nostr_event_json(desc)
}
func invoice_to_zap_invoice(_ invoice: Invoice) -> ZapInvoice? {
diff --git a/damusTests/ZapTests.swift b/damusTests/ZapTests.swift
@@ -16,7 +16,39 @@ final class ZapTests: XCTestCase {
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
-
+
+ func test_alby_zap() throws {
+ let zapjson = "eyJjb250ZW50Ijoi4pqhTm9uLWN1c3RvZGlhbCB6YXAgZnJvbSBteSBBbGJ5IEh1YiIsImNyZWF0ZWRfYXQiOjE3MjQ2ODUwNDcsImlkIjoiNGM3NWFiMWU3MDk4Y2NiN2FlYjhmZjdkNDIwMjM2ZDM1N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YTJiZDdlMTE2MDMxNWJmNlwiLFwiaWRcIjpcIjU3ZDg2MTIwMDc1MjFjMGI1MzJiOTFhZjI0OTgwOTVhMjUxZTYzZjQyNTE4N2U2Yzk1NzAwZmQwYTZiYWI3ZDRcIixcInNpZ1wiOlwiNzk4ZDczNTExOGJjZDE0MjI4YTEyYjZkNTI0MjNmZjI1YmI0ZWQ4Y2Q1ZGFjZjJmNTk3MWVmNTczZmRjM2ZjMDVmYzc5MzE4NWU2OTY4MmNjYTI0M2Q2NGYxNDdhNDQ5ODk2OGEwYmMyODhhZTgzZTc1YzAzZTk5ZjkzNmE2MDNcIn0iXV19Cg=="
+
+ guard let json_data = Data(base64Encoded: zapjson) else {
+ XCTAssert(false)
+ return
+ }
+
+ let json_str = String(decoding: json_data, as: UTF8.self)
+
+ guard let ev = decode_nostr_event_json(json: json_str) else {
+ XCTAssert(false)
+ return
+ }
+
+ let zapper = Pubkey(hex: "79f00d3f5a19ec806189fcab03c1be4ff81d18ee4f653c88fac41fe03570f432")!
+ guard let zap = Zap.from_zap_event(zap_ev: ev, zapper: zapper, our_privkey: nil) else {
+ XCTAssert(false)
+ return
+ }
+
+ let note_id = NoteId(hex: "bcb2fcfe1c467c5ec8285e385c36ec13879709ced9d8800cb340ebf218c3210d")!
+ let author = Pubkey(hex: "15b5cf6cdf4fd1c02f28bcce0f197cafae4c8c7c66a3e2e23af9fe610875315e")!
+ XCTAssertEqual(zap.zapper, zapper)
+ XCTAssertEqual(zap.target, ZapTarget.note(id: note_id, author: author))
+
+ XCTAssertEqual(NotificationFormatter.zap_notification_title(zap), "Zap")
+ XCTAssertEqual(NotificationFormatter.zap_notification_body(profiles: Profiles(ndb: test_damus_state.ndb), zap: zap), "You received 1k sats from 1q5sah9f:mqzxky65: \"⚡Non-custodial zap from my Alby Hub\"")
+
+
+ }
+
func test_private_zap() throws {
let alice = generate_new_keypair()
let bob = generate_new_keypair()