Skip to content

Commit f1d133e

Browse files
committed
rfqmsg+tapchannelmsg: add helper functions for asset ID detection
To easily find out if all asset IDs from a set are actually committed to a channel, we add helper functions for the different channel messages that we can encounter in our subsystems (depending on whether we get the message from a blob in a hook or as JSON over the RPC interface).
1 parent 73eac89 commit f1d133e

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

rfqmsg/custom_channel_data.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package rfqmsg
22

3+
import (
4+
"github.com/lightninglabs/taproot-assets/asset"
5+
"github.com/lightninglabs/taproot-assets/fn"
6+
)
7+
38
// JsonAssetBalance is a struct that represents the balance of a single asset ID
49
// within a channel.
510
type JsonAssetBalance struct {
@@ -43,6 +48,26 @@ type JsonAssetChannel struct {
4348
IncomingHtlcBalance uint64 `json:"incoming_htlc_balance"`
4449
}
4550

51+
// HasAllAssetIDs checks if the OpenChannel contains all asset IDs in the
52+
// provided set. It returns true if all asset IDs are present, false otherwise.
53+
func (c *JsonAssetChannel) HasAllAssetIDs(ids fn.Set[asset.ID]) bool {
54+
for id := range ids {
55+
found := false
56+
for _, fundingAsset := range c.FundingAssets {
57+
if fundingAsset.AssetGenesis.AssetID == id.String() {
58+
found = true
59+
break
60+
}
61+
}
62+
63+
if !found {
64+
return false
65+
}
66+
}
67+
68+
return true
69+
}
70+
4671
// JsonAssetChannelBalances is a struct that represents the balance information
4772
// of all assets within open and pending channels.
4873
type JsonAssetChannelBalances struct {

tapchannelmsg/records.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ func (o *OpenChannel) Bytes() []byte {
157157
return buf.Bytes()
158158
}
159159

160+
// HasAllAssetIDs checks if the OpenChannel contains all asset IDs in the
161+
// provided set. It returns true if all asset IDs are present, false otherwise.
162+
func (o *OpenChannel) HasAllAssetIDs(ids fn.Set[asset.ID]) bool {
163+
for id := range ids {
164+
found := false
165+
for _, output := range o.Assets() {
166+
if output.AssetID.Val == id {
167+
found = true
168+
break
169+
}
170+
}
171+
172+
if !found {
173+
return false
174+
}
175+
}
176+
177+
return true
178+
}
179+
160180
// DecodeOpenChannel deserializes an OpenChannel from the given blob.
161181
func DecodeOpenChannel(blob tlv.Blob) (*OpenChannel, error) {
162182
var o OpenChannel
@@ -536,6 +556,42 @@ func (c *Commitment) Leaves() lnwallet.CommitAuxLeaves {
536556
return leaves
537557
}
538558

559+
// HasAllAssetIDs checks if the OpenChannel contains all asset IDs in the
560+
// provided set. It returns true if all asset IDs are present, false otherwise.
561+
func (c *Commitment) HasAllAssetIDs(assetIDs fn.Set[asset.ID]) bool {
562+
// First, we collect all possible asset IDs from the local and remote
563+
// outputs, including the HTLCs.
564+
channelAssetIDs := fn.NewSet[asset.ID]()
565+
for _, local := range c.LocalOutputs() {
566+
channelAssetIDs.Add(local.AssetID.Val)
567+
}
568+
for _, remote := range c.RemoteOutputs() {
569+
channelAssetIDs.Add(remote.AssetID.Val)
570+
}
571+
for _, incoming := range c.IncomingHtlcAssets.Val.Outputs() {
572+
channelAssetIDs.Add(incoming.AssetID.Val)
573+
}
574+
for _, outgoing := range c.OutgoingHtlcAssets.Val.Outputs() {
575+
channelAssetIDs.Add(outgoing.AssetID.Val)
576+
}
577+
578+
for id := range assetIDs {
579+
found := false
580+
for channelAssetID := range channelAssetIDs {
581+
if channelAssetID == id {
582+
found = true
583+
break
584+
}
585+
}
586+
587+
if !found {
588+
return false
589+
}
590+
}
591+
592+
return true
593+
}
594+
539595
// DecodeCommitment deserializes a Commitment from the given blob.
540596
func DecodeCommitment(blob tlv.Blob) (*Commitment, error) {
541597
var c Commitment

0 commit comments

Comments
 (0)