Skip to content

Commit 9917c9e

Browse files
committed
multi: add asset specifier field to CommitmentConstraints struct
This commit replaces the asset ID and asset group public key fields in the `CommitmentConstraints` struct with an asset specifier field.
1 parent 4861718 commit 9917c9e

File tree

5 files changed

+34
-57
lines changed

5 files changed

+34
-57
lines changed

tapdb/assets_store.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -803,14 +803,14 @@ func (a *AssetStore) constraintsToDbFilter(
803803
query.MinAnchorHeight,
804804
)
805805
}
806-
if query.AssetID != nil {
807-
assetID := query.AssetID[:]
808-
assetFilter.AssetIDFilter = assetID
809-
}
810-
if query.GroupKey != nil {
811-
groupKey := query.GroupKey.SerializeCompressed()
812-
assetFilter.KeyGroupFilter = groupKey
813-
}
806+
807+
// Add asset ID bytes and group key bytes to the filter. These
808+
// byte arrays are empty if the asset ID or group key is not
809+
// specified in the query.
810+
assetIDBytes, groupKeyBytes := query.AssetSpecifier.AsBytes()
811+
assetFilter.AssetIDFilter = assetIDBytes
812+
assetFilter.KeyGroupFilter = groupKeyBytes
813+
814814
// TODO(roasbeef): only want to allow asset ID or other and not
815815
// both?
816816

tapdb/assets_store_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ func TestImportAssetProof(t *testing.T) {
280280

281281
// Add a random asset and corresponding proof into the database.
282282
testAsset, testProof := dbHandle.AddRandomAssetProof(t)
283-
assetID := testAsset.ID()
284283
initialBlob := testProof.Blob
285284

286285
// We should now be able to retrieve the set of all assets inserted on
@@ -313,11 +312,8 @@ func TestImportAssetProof(t *testing.T) {
313312
// We should also be able to fetch the created asset above based on
314313
// either the asset ID, or key group via the main coin selection
315314
// routine.
316-
var assetConstraints tapfreighter.CommitmentConstraints
317-
if testAsset.GroupKey != nil {
318-
assetConstraints.GroupKey = &testAsset.GroupKey.GroupPubKey
319-
} else {
320-
assetConstraints.AssetID = &assetID
315+
assetConstraints := tapfreighter.CommitmentConstraints{
316+
AssetSpecifier: testAsset.Specifier(),
321317
}
322318
selectedAssets, err := assetStore.ListEligibleCoins(
323319
ctxb, assetConstraints,
@@ -659,16 +655,20 @@ func (a *assetGenerator) genAssets(t *testing.T, assetStore *AssetStore,
659655
}
660656
}
661657

662-
func (a *assetGenerator) bindAssetID(i int, op wire.OutPoint) *asset.ID {
658+
func (a *assetGenerator) assetSpecifierAssetID(i int,
659+
op wire.OutPoint) asset.Specifier {
660+
663661
gen := a.assetGens[i]
664662
gen.FirstPrevOut = op
665663

666664
id := gen.ID()
667665

668-
return &id
666+
return asset.NewSpecifierFromId(id)
669667
}
670668

671-
func (a *assetGenerator) bindGroupKey(i int, op wire.OutPoint) *btcec.PublicKey {
669+
func (a *assetGenerator) assetSpecifierGroupKey(i int,
670+
op wire.OutPoint) asset.Specifier {
671+
672672
gen := a.assetGens[i]
673673
gen.FirstPrevOut = op
674674
genTweak := gen.ID()
@@ -677,8 +677,9 @@ func (a *assetGenerator) bindGroupKey(i int, op wire.OutPoint) *btcec.PublicKey
677677

678678
internalPriv := input.TweakPrivKey(&groupPriv, genTweak[:])
679679
tweakedPriv := txscript.TweakTaprootPrivKey(*internalPriv, nil)
680+
groupPubKey := tweakedPriv.PubKey()
680681

681-
return tweakedPriv.PubKey()
682+
return asset.NewSpecifierFromGroupKey(*groupPubKey)
682683
}
683684

684685
// TestFetchAllAssets tests that the different AssetQueryFilters work as
@@ -1000,7 +1001,7 @@ func TestSelectCommitment(t *testing.T) {
10001001
},
10011002
},
10021003
constraints: tapfreighter.CommitmentConstraints{
1003-
AssetID: assetGen.bindAssetID(
1004+
AssetSpecifier: assetGen.assetSpecifierAssetID(
10041005
0, assetGen.anchorPoints[0],
10051006
),
10061007
MinAmt: 2,
@@ -1022,7 +1023,7 @@ func TestSelectCommitment(t *testing.T) {
10221023
},
10231024
},
10241025
constraints: tapfreighter.CommitmentConstraints{
1025-
AssetID: assetGen.bindAssetID(
1026+
AssetSpecifier: assetGen.assetSpecifierAssetID(
10261027
0, assetGen.anchorPoints[0],
10271028
),
10281029
MinAmt: 10,
@@ -1043,7 +1044,7 @@ func TestSelectCommitment(t *testing.T) {
10431044
},
10441045
},
10451046
constraints: tapfreighter.CommitmentConstraints{
1046-
AssetID: assetGen.bindAssetID(
1047+
AssetSpecifier: assetGen.assetSpecifierAssetID(
10471048
1, assetGen.anchorPoints[1],
10481049
),
10491050
MinAmt: 10,
@@ -1074,7 +1075,7 @@ func TestSelectCommitment(t *testing.T) {
10741075
},
10751076
},
10761077
constraints: tapfreighter.CommitmentConstraints{
1077-
GroupKey: assetGen.bindGroupKey(
1078+
AssetSpecifier: assetGen.assetSpecifierGroupKey(
10781079
0, assetGen.anchorPoints[0],
10791080
),
10801081
MinAmt: 1,
@@ -1104,7 +1105,7 @@ func TestSelectCommitment(t *testing.T) {
11041105
},
11051106
},
11061107
constraints: tapfreighter.CommitmentConstraints{
1107-
AssetID: assetGen.bindAssetID(
1108+
AssetSpecifier: assetGen.assetSpecifierAssetID(
11081109
0, assetGen.anchorPoints[0],
11091110
),
11101111
MinAmt: 2,
@@ -1146,7 +1147,7 @@ func TestSelectCommitment(t *testing.T) {
11461147
},
11471148
},
11481149
constraints: tapfreighter.CommitmentConstraints{
1149-
GroupKey: assetGen.bindGroupKey(
1150+
AssetSpecifier: assetGen.assetSpecifierGroupKey(
11501151
0, assetGen.anchorPoints[0],
11511152
),
11521153
MinAmt: 1,

tapfreighter/coin_select.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ func (s *CoinSelect) SelectCoins(ctx context.Context,
5252
}
5353

5454
listConstraints := CommitmentConstraints{
55-
GroupKey: constraints.GroupKey,
56-
AssetID: constraints.AssetID,
57-
MinAmt: 1,
55+
AssetSpecifier: constraints.AssetSpecifier,
56+
MinAmt: 1,
5857
}
5958
eligibleCommitments, err := s.coinLister.ListEligibleCoins(
6059
ctx, listConstraints,

tapfreighter/interface.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"time"
77

8-
"github.com/btcsuite/btcd/btcec/v2"
98
"github.com/btcsuite/btcd/btcutil"
109
"github.com/btcsuite/btcd/btcutil/psbt"
1110
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -28,13 +27,8 @@ import (
2827
//
2928
// NOTE: Only the GroupKey or the AssetID should be set.
3029
type CommitmentConstraints struct {
31-
// GroupKey is the required group key. This is an optional field, if
32-
// set then the asset returned may have a distinct asset ID to the one
33-
// specified below.
34-
GroupKey *btcec.PublicKey
35-
36-
// AssetID is the asset ID that needs to be satisfied.
37-
AssetID *asset.ID
30+
// AssetSpecifier specifies the asset.
31+
AssetSpecifier asset.Specifier
3832

3933
// MinAmt is the minimum amount that an asset commitment needs to hold
4034
// to satisfy the constraints.
@@ -47,13 +41,8 @@ type CommitmentConstraints struct {
4741

4842
// String returns the string representation of the commitment constraints.
4943
func (c *CommitmentConstraints) String() string {
50-
var groupKeyBytes, assetIDBytes []byte
51-
if c.GroupKey != nil {
52-
groupKeyBytes = c.GroupKey.SerializeCompressed()
53-
}
54-
if c.AssetID != nil {
55-
assetIDBytes = c.AssetID[:]
56-
}
44+
assetIDBytes, groupKeyBytes := c.AssetSpecifier.AsBytes()
45+
5746
return fmt.Sprintf("group_key=%x, asset_id=%x, min_amt=%d",
5847
groupKeyBytes, assetIDBytes, c.MinAmt)
5948
}

tapfreighter/wallet.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,11 @@ func (f *AssetWallet) FundPacket(ctx context.Context,
362362
return nil, address.ErrMismatchedHRP
363363
}
364364

365-
// Extract the asset ID and group key from the funding descriptor.
366-
assetId := fundDesc.AssetSpecifier.UnwrapIdToPtr()
367-
if assetId == nil {
368-
return nil, fmt.Errorf("unable to unwrap asset ID")
369-
}
370-
371-
groupKey := fundDesc.AssetSpecifier.UnwrapGroupKeyToPtr()
372-
373365
// We need to find a commitment that has enough assets to satisfy this
374366
// send request. We'll map the address to a set of constraints, so we
375367
// can use that to do Taproot asset coin selection.
376368
constraints := CommitmentConstraints{
377-
GroupKey: groupKey,
378-
AssetID: assetId,
369+
AssetSpecifier: fundDesc.AssetSpecifier,
379370
MinAmt: fundDesc.Amount,
380371
Bip86ScriptKeysOnly: true,
381372
}
@@ -410,15 +401,12 @@ func (f *AssetWallet) FundBurn(ctx context.Context,
410401
return nil, err
411402
}
412403

413-
groupKey := fundDesc.AssetSpecifier.UnwrapGroupKeyToPtr()
414-
415404
// We need to find a commitment that has enough assets to satisfy this
416405
// send request. We'll map the address to a set of constraints, so we
417406
// can use that to do Taproot asset coin selection.
418407
constraints := CommitmentConstraints{
419-
GroupKey: groupKey,
420-
AssetID: assetId,
421-
MinAmt: fundDesc.Amount,
408+
AssetSpecifier: fundDesc.AssetSpecifier,
409+
MinAmt: fundDesc.Amount,
422410
}
423411
selectedCommitments, err := f.cfg.CoinSelector.SelectCoins(
424412
ctx, constraints, PreferMaxAmount, commitment.TapCommitmentV2,

0 commit comments

Comments
 (0)