@@ -201,31 +201,60 @@ public final class BuzzPushNotificationResolver: BuzzPushNotificationResolving {
201201 completion ( [ ] , nil )
202202 return
203203 }
204- federatedIdentity ( relayURL : relayURL , privateKey : privateKey ) { assertion in
204+ withFederatedIdentity ( relayURL : relayURL , privateKey : privateKey ) {
205+ ( assertion , finish : @escaping ( ( Data ? , URLResponse ? ) , Bool ) -> Void ) in
205206 self . send (
206207 url : url , body : body , privateKey : privateKey , federatedIdentity : assertion ? . header ,
207208 session : self . session ,
208209 canRetry : {
209210 self . matchingPrivateKey ( for : community ) == privateKey && self . isCurrent ( assertion )
210211 }
211212 ) { data , response in
212- guard self . matchingPrivateKey ( for : community ) != nil , self . isCurrent ( assertion ) ,
213- let response = response as? HTTPURLResponse , ( 200 ..< 300 ) . contains ( response . statusCode ) ,
214- let data , let events = try? JSONDecoder ( ) . decode ( [ VerifiedNostrEvent ] . self , from : data )
215- else {
216- completion ( [ ] , assertion )
213+ finish ( ( data , response ) , BuzzRelayAssertionIssuer . isChallenge ( response ) )
214+ }
215+ } completion : { result , assertion in
216+ let ( data , response ) = result
217+ guard self . matchingPrivateKey ( for : community ) != nil , self . isCurrent ( assertion ) ,
218+ let response = response as? HTTPURLResponse , ( 200 ..< 300 ) . contains ( response . statusCode ) ,
219+ let data , let events = try? JSONDecoder ( ) . decode ( [ VerifiedNostrEvent ] . self , from : data )
220+ else {
221+ completion ( [ ] , assertion )
222+ return
223+ }
224+ completion (
225+ Self . messages (
226+ events : events . filter { event in
227+ event . hasValidIDAndSignature ( )
228+ && community . policies . contains { policy in
229+ PushLeaseMatcher . matches ( event : event , policy : policy )
230+ }
231+ } ,
232+ community : community
233+ ) , assertion )
234+ }
235+ }
236+
237+ /// Runs `attempt` with the relay's NIP-FI assertion, if any. A hosted relay is tried without
238+ /// one first; only if it challenges does the extension ask Builderlab, then retry once.
239+ private func withFederatedIdentity < Output > (
240+ relayURL : URL , privateKey : String ,
241+ attempt : @escaping ( BuzzRelayAssertion ? , @escaping ( Output , Bool ) -> Void ) -> Void ,
242+ completion : @escaping ( Output , BuzzRelayAssertion ? ) -> Void
243+ ) {
244+ federatedIdentity ( relayURL : relayURL , privateKey : privateKey ) { assertion in
245+ attempt ( assertion ) { output , challenged in
246+ guard challenged , assertion == nil , let assertionIssuer = self . assertionIssuer else {
247+ completion ( output , assertion )
217248 return
218249 }
219- completion (
220- Self . messages (
221- events : events . filter { event in
222- event . hasValidIDAndSignature ( )
223- && community . policies . contains { policy in
224- PushLeaseMatcher . matches ( event : event , policy : policy )
225- }
226- } ,
227- community : community
228- ) , assertion )
250+ assertionIssuer . header ( relayURL : relayURL , privateKeyHex : privateKey , challenged : true ) {
251+ answer in
252+ guard let answer else {
253+ completion ( output , nil )
254+ return
255+ }
256+ attempt ( answer ) { output , _ in completion ( output , answer ) }
257+ }
229258 }
230259 }
231260 }
@@ -412,15 +441,16 @@ public final class BuzzPushNotificationResolver: BuzzPushNotificationResolving {
412441 completion ( [ ] )
413442 return
414443 }
415- federatedIdentity ( relayURL : relayURL , privateKey : privateKey ) { assertion in
444+ withFederatedIdentity ( relayURL : relayURL , privateKey : privateKey ) {
445+ ( assertion , finish : @escaping ( Result < Data , Error > ? , Bool ) -> Void ) in
416446 var request = URLRequest ( url : url )
417447 request . httpMethod = " POST "
418448 request . httpBody = body
419449 request . setValue ( " application/json " , forHTTPHeaderField : " Content-Type " )
420450 guard let auth = try? NostrHTTPAuth . authorizationHeader (
421451 url : url , method : " POST " , body : body , privateKeyHex : privateKey )
422452 else {
423- completion ( [ ] )
453+ finish ( nil , false )
424454 return
425455 }
426456 request . setValue ( auth , forHTTPHeaderField : " Authorization " )
@@ -431,15 +461,21 @@ public final class BuzzPushNotificationResolver: BuzzPushNotificationResolving {
431461 request : request , configuration : self . session . configuration ,
432462 maximumBytes : Self . maximumPresentationResponseBytes
433463 ) { result in
434- guard self . matchingPrivateKey ( for : community ) != nil , self . isCurrent ( assertion ) ,
435- case . success ( let data ) = result ,
436- let events = try? JSONDecoder ( ) . decode ( [ VerifiedNostrEvent ] . self , from : data )
437- else {
438- completion ( [ ] )
464+ guard case . failure ( BuzzNotificationDownload . Failure . challenged ) = result else {
465+ finish ( result , false )
439466 return
440467 }
441- completion ( events . filter { $0 . hasValidIDAndSignature ( ) } )
468+ finish ( result , true )
469+ }
470+ } completion : { result , assertion in
471+ guard self . matchingPrivateKey ( for : community ) != nil , self . isCurrent ( assertion ) ,
472+ case . success ( let data ) ? = result ,
473+ let events = try? JSONDecoder ( ) . decode ( [ VerifiedNostrEvent ] . self , from : data )
474+ else {
475+ completion ( [ ] )
476+ return
442477 }
478+ completion ( events . filter { $0 . hasValidIDAndSignature ( ) } )
443479 }
444480 }
445481
0 commit comments