From 83ef50586a905f6eb8723df9025ec4e38d530acf Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 9 Jul 2023 07:44:33 -0700 Subject: [PATCH] zaps/refactor: use guard instead of if block not a fan of unncessary nesting --- damus/Models/HomeModel.swift | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift index 4c416e02..94f72f2f 100644 --- a/damus/Models/HomeModel.swift +++ b/damus/Models/HomeModel.swift @@ -1246,14 +1246,9 @@ enum ProcessZapResult { func get_zap_target_pubkey(ev: NostrEvent, events: EventCache) -> String? { let etags = ev.referenced_ids - if let etag = etags.first { - // ensure that there is only 1 etag to stop fake note zap attacks - guard etags.count == 1 else { - return nil - } - // we can't trust the p tag on note zaps because they can be faked - return events.lookup(etag.id)?.pubkey - } else { + guard let etag = etags.first else { + // no etags, ptag-only case + let ptags = ev.referenced_pubkeys // ensure that there is only 1 ptag to stop fake profile zap attacks @@ -1263,6 +1258,16 @@ func get_zap_target_pubkey(ev: NostrEvent, events: EventCache) -> String? { return ptags.first?.id } + + // we have an e-tag + + // ensure that there is only 1 etag to stop fake note zap attacks + guard etags.count == 1 else { + return nil + } + + // we can't trust the p tag on note zaps because they can be faked + return events.lookup(etag.id)?.pubkey } func process_zap_event(damus_state: DamusState, ev: NostrEvent, completion: @escaping (ProcessZapResult) -> Void) {