@@ -3375,7 +3375,7 @@ func (r *rpcServer) SendAsset(ctx context.Context,
33753375 }
33763376
33773377 resp , err := r .cfg .ChainPorter .RequestShipment (
3378- tapfreighter .NewAddressParcel (feeRate , tapAddrs ... ),
3378+ tapfreighter .NewAddressParcel (feeRate , req . Label , tapAddrs ... ),
33793379 )
33803380 if err != nil {
33813381 return nil , err
@@ -3662,6 +3662,7 @@ func marshalOutboundParcel(
36623662 AnchorTxBlockHeight : parcel .AnchorTxBlockHeight ,
36633663 Inputs : rpcInputs ,
36643664 Outputs : rpcOutputs ,
3665+ Label : parcel .Label ,
36653666 }, nil
36663667}
36673668
@@ -3846,23 +3847,69 @@ func (r *rpcServer) SubscribeReceiveEvents(
38463847 )
38473848}
38483849
3850+ // shouldNotifyAssetSendEvent returns true if the given AssetSendEvent matches
3851+ // all specified filter criteria. Unset filter values are ignored. If set, all
3852+ // filters must match (logical AND).
3853+ func shouldNotifyAssetSendEvent (event tapfreighter.AssetSendEvent ,
3854+ targetScriptKey fn.Option [btcec.PublicKey ], targetLabel string ) bool {
3855+
3856+ // By default, if no filter values are provided, match on event.
3857+ match := true
3858+
3859+ // Filter by label if specified.
3860+ if targetLabel != "" {
3861+ match = match && (event .TransferLabel == targetLabel )
3862+ }
3863+
3864+ // Filter by target script key if specified.
3865+ if targetScriptKey .IsSome () {
3866+ // If script key is specified but there are no virtual packets,
3867+ // early return as a match is impossible.
3868+ if len (event .VirtualPackets ) == 0 {
3869+ return false
3870+ }
3871+
3872+ scriptKey := targetScriptKey .UnwrapToPtr ()
3873+ found := false
3874+
3875+ for _ , vPacket := range event .VirtualPackets {
3876+ if found {
3877+ break
3878+ }
3879+
3880+ for _ , vOut := range vPacket .Outputs {
3881+ if vOut .ScriptKey .PubKey == nil {
3882+ continue
3883+ }
3884+ if vOut .ScriptKey .PubKey .IsEqual (scriptKey ) {
3885+ found = true
3886+ break
3887+ }
3888+ }
3889+ }
3890+
3891+ match = match && found
3892+ }
3893+
3894+ return match
3895+ }
3896+
38493897// SubscribeSendEvents registers a subscription to the event notification
38503898// stream which relates to the asset sending process.
38513899func (r * rpcServer ) SubscribeSendEvents (req * taprpc.SubscribeSendEventsRequest ,
38523900 ntfnStream sendEventStream ) error {
38533901
3854- var (
3855- targetScriptKey * btcec.PublicKey
3856- err error
3857- )
3902+ var targetScriptKey fn.Option [btcec.PublicKey ]
38583903 if len (req .FilterScriptKey ) > 0 {
3859- targetScriptKey , err = btcec .ParsePubKey (req .FilterScriptKey )
3904+ scriptKey , err : = btcec .ParsePubKey (req .FilterScriptKey )
38603905 if err != nil {
38613906 return fmt .Errorf ("error parsing script key: %w" , err )
38623907 }
3908+
3909+ targetScriptKey = fn .MaybeSome (scriptKey )
38633910 }
38643911
3865- filter := func (event fn.Event ) (bool , error ) {
3912+ shouldNotify := func (event fn.Event ) (bool , error ) {
38663913 var e * tapfreighter.AssetSendEvent
38673914 switch typedEvent := event .(type ) {
38683915 case * tapfreighter.AssetSendEvent :
@@ -3878,44 +3925,14 @@ func (r *rpcServer) SubscribeSendEvents(req *taprpc.SubscribeSendEventsRequest,
38783925 event )
38793926 }
38803927
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
3928+ return shouldNotifyAssetSendEvent (
3929+ * e , targetScriptKey , req .FilterLabel ,
3930+ ), nil
39143931 }
39153932
39163933 return handleEvents [bool , * taprpc.SendEvent ](
3917- r .cfg .ChainPorter , ntfnStream , marshalSendEvent , filter , r . quit ,
3918- false ,
3934+ r .cfg .ChainPorter , ntfnStream , marshalSendEvent , shouldNotify ,
3935+ r . quit , false ,
39193936 )
39203937}
39213938
@@ -4162,6 +4179,7 @@ func marshalSendEvent(event fn.Event) (*taprpc.SendEvent, error) {
41624179 SendState : e .SendState .String (),
41634180 VirtualPackets : make ([][]byte , len (e .VirtualPackets )),
41644181 PassiveVirtualPackets : make ([][]byte , len (e .PassivePackets )),
4182+ TransferLabel : e .TransferLabel ,
41654183 }
41664184
41674185 if e .Error != nil {
0 commit comments