@@ -2,6 +2,7 @@ package rfq
22
33import (
44 "context"
5+ "encoding/hex"
56 "encoding/json"
67 "fmt"
78 "sync"
@@ -963,6 +964,71 @@ func (m *Manager) getAssetGroupKey(ctx context.Context,
963964 return fn .Some (group .GroupPubKey ), nil
964965}
965966
967+ // AssetMatchesSpecifier checks if the provided asset satisfies the provided
968+ // specifier. If the specifier includes a group key, we will check if the asset
969+ // belongs to that group.
970+ func (m * Manager ) AssetMatchesSpecifier (ctx context.Context ,
971+ specifier asset.Specifier , id asset.ID ) (bool , error ) {
972+
973+ switch {
974+ case specifier .HasGroupPubKey ():
975+ group , err := m .getAssetGroupKey (ctx , id )
976+ if err != nil {
977+ return false , err
978+ }
979+
980+ if group .IsNone () {
981+ return false , nil
982+ }
983+
984+ specifierGK := specifier .UnwrapGroupKeyToPtr ()
985+
986+ return group .UnwrapToPtr ().IsEqual (specifierGK ), nil
987+
988+ case specifier .HasId ():
989+ specifierID := specifier .UnwrapIdToPtr ()
990+
991+ return * specifierID == id , nil
992+
993+ default :
994+ return false , fmt .Errorf ("specifier is empty" )
995+ }
996+ }
997+
998+ // ChannelCompatible checks a channel's assets against an asset specifier. If
999+ // the specifier is an asset ID, then all assets must be of that specific ID,
1000+ // if the specifier is a group key, then all assets in the channel must belong
1001+ // to that group.
1002+ func (m * Manager ) ChannelCompatible (ctx context.Context ,
1003+ jsonAssets []rfqmsg.JsonAssetChanInfo , specifier asset.Specifier ) (bool ,
1004+ error ) {
1005+
1006+ for _ , chanAsset := range jsonAssets {
1007+ gen := chanAsset .AssetInfo .AssetGenesis
1008+ assetIDBytes , err := hex .DecodeString (
1009+ gen .AssetID ,
1010+ )
1011+ if err != nil {
1012+ return false , fmt .Errorf ("error decoding asset ID: %w" ,
1013+ err )
1014+ }
1015+
1016+ var assetID asset.ID
1017+ copy (assetID [:], assetIDBytes )
1018+
1019+ match , err := m .AssetMatchesSpecifier (ctx , specifier , assetID )
1020+ if err != nil {
1021+ return false , err
1022+ }
1023+
1024+ if ! match {
1025+ return false , err
1026+ }
1027+ }
1028+
1029+ return true , nil
1030+ }
1031+
9661032// publishSubscriberEvent publishes an event to all subscribers.
9671033func (m * Manager ) publishSubscriberEvent (event fn.Event ) {
9681034 // Iterate over the subscribers and deliver the event to each one.
0 commit comments