@@ -961,6 +961,36 @@ func AssertReceiveEvents(t *testing.T, addr *taprpc.Addr,
961961 }
962962}
963963
964+ // makeFilterSendEventScriptKey returns a filter function that checks if the
965+ // given script key is present in the send event. If it is, the event is
966+ // included in the stream.
967+ func makeFilterSendEventScriptKey (
968+ scriptKey btcec.PublicKey ) func (* taprpc.SendEvent ) (bool , error ) {
969+
970+ return func (event * taprpc.SendEvent ) (bool , error ) {
971+ for _ , vPacketBytes := range event .VirtualPackets {
972+ vPkt , err := tappsbt .Decode (vPacketBytes )
973+ if err != nil {
974+ return false , err
975+ }
976+
977+ for _ , vOut := range vPkt .Outputs {
978+ if vOut .ScriptKey .PubKey == nil {
979+ continue
980+ }
981+
982+ // This packet sends to the filtered script key,
983+ // we want to include this event.
984+ if vOut .ScriptKey .PubKey .IsEqual (& scriptKey ) {
985+ return true , nil
986+ }
987+ }
988+ }
989+
990+ return false , nil
991+ }
992+ }
993+
964994// AssertSendEventsComplete makes sure the two remaining events for the given
965995// script key are sent on the stream.
966996func AssertSendEventsComplete (t * testing.T , scriptKey []byte ,
@@ -974,39 +1004,27 @@ func AssertSendEventsComplete(t *testing.T, scriptKey []byte,
9741004
9751005// AssertSendEvents makes sure all events with incremental status are sent
9761006// on the stream for the given script key.
977- func AssertSendEvents (t * testing.T , scriptKey []byte ,
1007+ func AssertSendEvents (t * testing.T , targetScriptKey []byte ,
9781008 stream * EventSubscription [* taprpc.SendEvent ], from ,
9791009 to tapfreighter.SendState ) {
9801010
9811011 success := make (chan struct {})
9821012 timeout := time .After (defaultWaitTimeout )
9831013 startStatus := from
9841014
985- targetScriptKey , err := btcec .ParsePubKey (scriptKey )
986- require .NoError (t , err )
987-
988- sendsToKey := func (e * taprpc.SendEvent ) bool {
989- for _ , vPacketBytes := range e .VirtualPackets {
990- vPkt , err := tappsbt .Decode (vPacketBytes )
991- require .NoError (t , err )
992-
993- for _ , vOut := range vPkt .Outputs {
994- if vOut .ScriptKey .PubKey == nil {
995- continue
996- }
997-
998- // This packet sends to the filtered script key,
999- // we want to include this event.
1000- if vOut .ScriptKey .PubKey .IsEqual (
1001- targetScriptKey ,
1002- ) {
1015+ // By default, if the target script key is not given we will accept all
1016+ // send events.
1017+ sendsToKey := func (* taprpc.SendEvent ) (bool , error ) {
1018+ return true , nil
1019+ }
10031020
1004- return true
1005- }
1006- }
1007- }
1021+ // If a target script key is given, we will only accept send events that
1022+ // relate to that key.
1023+ if targetScriptKey != nil {
1024+ targetScriptKey , err := btcec .ParsePubKey (targetScriptKey )
1025+ require .NoError (t , err )
10081026
1009- return false
1027+ sendsToKey = makeFilterSendEventScriptKey ( * targetScriptKey )
10101028 }
10111029
10121030 // To make sure we don't forever hang on receiving on the stream, we'll
@@ -1026,7 +1044,11 @@ func AssertSendEvents(t *testing.T, scriptKey []byte,
10261044 event , err := stream .Recv ()
10271045 require .NoError (t , err , "receiving send event" )
10281046
1029- require .True (t , sendsToKey (event ))
1047+ // Ensure that the event complies with the target script key
1048+ // check.
1049+ res , err := sendsToKey (event )
1050+ require .NoError (t , err )
1051+ require .True (t , res )
10301052
10311053 // Check the event's error field for unexpected errors. Perform
10321054 // this check before verifying the expected send state, as
0 commit comments