@@ -3846,23 +3846,69 @@ func (r *rpcServer) SubscribeReceiveEvents(
38463846 )
38473847}
38483848
3849+ // shouldNotifyAssetSendEvent returns true if the given AssetSendEvent matches
3850+ // all specified filter criteria. Unset filter values are ignored. If set, all
3851+ // filters must match (logical AND).
3852+ func shouldNotifyAssetSendEvent (event tapfreighter.AssetSendEvent ,
3853+ targetScriptKey fn.Option [btcec.PublicKey ], targetLabel string ) bool {
3854+
3855+ // By default, if no filter values are provided, match on event.
3856+ match := true
3857+
3858+ // Filter by label if specified.
3859+ if targetLabel != "" {
3860+ match = match && (event .TransferLabel == targetLabel )
3861+ }
3862+
3863+ // Filter by target script key if specified.
3864+ if targetScriptKey .IsSome () {
3865+ // If script key is specified but there are no virtual packets,
3866+ // early return as a match is impossible.
3867+ if len (event .VirtualPackets ) == 0 {
3868+ return false
3869+ }
3870+
3871+ scriptKey := targetScriptKey .UnwrapToPtr ()
3872+ found := false
3873+
3874+ for _ , vPacket := range event .VirtualPackets {
3875+ if found {
3876+ break
3877+ }
3878+
3879+ for _ , vOut := range vPacket .Outputs {
3880+ if vOut .ScriptKey .PubKey == nil {
3881+ continue
3882+ }
3883+ if vOut .ScriptKey .PubKey .IsEqual (scriptKey ) {
3884+ found = true
3885+ break
3886+ }
3887+ }
3888+ }
3889+
3890+ match = match && found
3891+ }
3892+
3893+ return match
3894+ }
3895+
38493896// SubscribeSendEvents registers a subscription to the event notification
38503897// stream which relates to the asset sending process.
38513898func (r * rpcServer ) SubscribeSendEvents (req * taprpc.SubscribeSendEventsRequest ,
38523899 ntfnStream sendEventStream ) error {
38533900
3854- var (
3855- targetScriptKey * btcec.PublicKey
3856- err error
3857- )
3901+ var targetScriptKey fn.Option [btcec.PublicKey ]
38583902 if len (req .FilterScriptKey ) > 0 {
3859- targetScriptKey , err = btcec .ParsePubKey (req .FilterScriptKey )
3903+ scriptKey , err : = btcec .ParsePubKey (req .FilterScriptKey )
38603904 if err != nil {
38613905 return fmt .Errorf ("error parsing script key: %w" , err )
38623906 }
3907+
3908+ targetScriptKey = fn .MaybeSome (scriptKey )
38633909 }
38643910
3865- filter := func (event fn.Event ) (bool , error ) {
3911+ shouldNotify := func (event fn.Event ) (bool , error ) {
38663912 var e * tapfreighter.AssetSendEvent
38673913 switch typedEvent := event .(type ) {
38683914 case * tapfreighter.AssetSendEvent :
@@ -3878,44 +3924,14 @@ func (r *rpcServer) SubscribeSendEvents(req *taprpc.SubscribeSendEventsRequest,
38783924 event )
38793925 }
38803926
3881- // If we're not filtering on a specific script key, we return
3882- // all events.
3883- if targetScriptKey == nil {
3884- return true , nil
3885- }
3886-
3887- // We can only match the script key on the active virtual
3888- // packets, so if there are none, then this isn't an event we're
3889- // interested in.
3890- if len (e .VirtualPackets ) == 0 {
3891- return false , nil
3892- }
3893-
3894- for _ , vPacket := range e .VirtualPackets {
3895- for _ , vOut := range vPacket .Outputs {
3896- if vOut .ScriptKey .PubKey == nil {
3897- continue
3898- }
3899-
3900- // This packet sends to the filtered script key,
3901- // we want to include this event.
3902- if vOut .ScriptKey .PubKey .IsEqual (
3903- targetScriptKey ,
3904- ) {
3905-
3906- return true , nil
3907- }
3908- }
3909- }
3910-
3911- // No packets with outputs going to the filtered script key
3912- // found.
3913- return false , nil
3927+ return shouldNotifyAssetSendEvent (
3928+ * e , targetScriptKey , req .FilterLabel ,
3929+ ), nil
39143930 }
39153931
39163932 return handleEvents [bool , * taprpc.SendEvent ](
3917- r .cfg .ChainPorter , ntfnStream , marshalSendEvent , filter , r . quit ,
3918- false ,
3933+ r .cfg .ChainPorter , ntfnStream , marshalSendEvent , shouldNotify ,
3934+ r . quit , false ,
39193935 )
39203936}
39213937
0 commit comments