commit f5ba90978412619fcf7525f7cdd390339525dd1c
parent 6031fe0847fb8ab956465d810a1124a8eb921dc1
Author: William Casarin <jb55@jb55.com>
Date: Sat, 8 Jul 2023 22:06:44 -0700
zaps: move pubkey check into standalone function
Diffstat:
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift
@@ -1236,19 +1236,23 @@ enum ProcessZapResult {
case failed
}
-func process_zap_event(damus_state: DamusState, ev: NostrEvent, completion: @escaping (ProcessZapResult) -> Void) {
- // These are zap notifications
- let etag = event_tag(ev, name: "e")
+// securely get the zap target's pubkey. this can be faked so we need to be
+// careful
+func get_zap_target_pubkey(ev: NostrEvent, events: EventCache) -> String? {
+ let etags = ev.referenced_ids
- var ptag: String? = nil
- if let etag {
+ if let etag = etags.first {
// we can't trust the p tag on note zaps because they can be faked
- ptag = damus_state.events.lookup(etag)?.pubkey
+ return events.lookup(etag.id)?.pubkey
} else {
- ptag = event_tag(ev, name: "p")
+ let ptags = ev.referenced_pubkeys
+ return ptags.first?.id
}
+}
- guard let ptag else {
+func process_zap_event(damus_state: DamusState, ev: NostrEvent, completion: @escaping (ProcessZapResult) -> Void) {
+ // These are zap notifications
+ guard let ptag = get_zap_target_pubkey(ev: ev, events: damus_state.events) else {
completion(.failed)
return
}