Skip to content

Commit f66ec78

Browse files
committed
rfq: add asset specifier helpers
We add some asset specifier related helpers, for checking whether an asset matches a specifier, and also another helper that checks if a channels' assets satisfy a specifier. These methods will be used in a following commit.
1 parent 7714730 commit f66ec78

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

rfq/manager.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rfq
22

33
import (
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.
9671033
func (m *Manager) publishSubscriberEvent(event fn.Event) {
9681034
// Iterate over the subscribers and deliver the event to each one.

0 commit comments

Comments
 (0)