Skip to content

Commit

Permalink
multi: add asset specifier field to CommitmentConstraints struct
Browse files Browse the repository at this point in the history
This commit replaces the asset ID and asset group public key fields in
the `CommitmentConstraints` struct with an asset specifier field.
  • Loading branch information
ffranr committed Jul 16, 2024
1 parent 4861718 commit 9917c9e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 57 deletions.
16 changes: 8 additions & 8 deletions tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,14 +803,14 @@ func (a *AssetStore) constraintsToDbFilter(
query.MinAnchorHeight,
)
}
if query.AssetID != nil {
assetID := query.AssetID[:]
assetFilter.AssetIDFilter = assetID
}
if query.GroupKey != nil {
groupKey := query.GroupKey.SerializeCompressed()
assetFilter.KeyGroupFilter = groupKey
}

// Add asset ID bytes and group key bytes to the filter. These
// byte arrays are empty if the asset ID or group key is not
// specified in the query.
assetIDBytes, groupKeyBytes := query.AssetSpecifier.AsBytes()
assetFilter.AssetIDFilter = assetIDBytes
assetFilter.KeyGroupFilter = groupKeyBytes

// TODO(roasbeef): only want to allow asset ID or other and not
// both?

Expand Down
33 changes: 17 additions & 16 deletions tapdb/assets_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ func TestImportAssetProof(t *testing.T) {

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

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

func (a *assetGenerator) bindAssetID(i int, op wire.OutPoint) *asset.ID {
func (a *assetGenerator) assetSpecifierAssetID(i int,
op wire.OutPoint) asset.Specifier {

gen := a.assetGens[i]
gen.FirstPrevOut = op

id := gen.ID()

return &id
return asset.NewSpecifierFromId(id)
}

func (a *assetGenerator) bindGroupKey(i int, op wire.OutPoint) *btcec.PublicKey {
func (a *assetGenerator) assetSpecifierGroupKey(i int,
op wire.OutPoint) asset.Specifier {

gen := a.assetGens[i]
gen.FirstPrevOut = op
genTweak := gen.ID()
Expand All @@ -677,8 +677,9 @@ func (a *assetGenerator) bindGroupKey(i int, op wire.OutPoint) *btcec.PublicKey

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

return tweakedPriv.PubKey()
return asset.NewSpecifierFromGroupKey(*groupPubKey)
}

// TestFetchAllAssets tests that the different AssetQueryFilters work as
Expand Down Expand Up @@ -1000,7 +1001,7 @@ func TestSelectCommitment(t *testing.T) {
},
},
constraints: tapfreighter.CommitmentConstraints{
AssetID: assetGen.bindAssetID(
AssetSpecifier: assetGen.assetSpecifierAssetID(
0, assetGen.anchorPoints[0],
),
MinAmt: 2,
Expand All @@ -1022,7 +1023,7 @@ func TestSelectCommitment(t *testing.T) {
},
},
constraints: tapfreighter.CommitmentConstraints{
AssetID: assetGen.bindAssetID(
AssetSpecifier: assetGen.assetSpecifierAssetID(
0, assetGen.anchorPoints[0],
),
MinAmt: 10,
Expand All @@ -1043,7 +1044,7 @@ func TestSelectCommitment(t *testing.T) {
},
},
constraints: tapfreighter.CommitmentConstraints{
AssetID: assetGen.bindAssetID(
AssetSpecifier: assetGen.assetSpecifierAssetID(
1, assetGen.anchorPoints[1],
),
MinAmt: 10,
Expand Down Expand Up @@ -1074,7 +1075,7 @@ func TestSelectCommitment(t *testing.T) {
},
},
constraints: tapfreighter.CommitmentConstraints{
GroupKey: assetGen.bindGroupKey(
AssetSpecifier: assetGen.assetSpecifierGroupKey(
0, assetGen.anchorPoints[0],
),
MinAmt: 1,
Expand Down Expand Up @@ -1104,7 +1105,7 @@ func TestSelectCommitment(t *testing.T) {
},
},
constraints: tapfreighter.CommitmentConstraints{
AssetID: assetGen.bindAssetID(
AssetSpecifier: assetGen.assetSpecifierAssetID(
0, assetGen.anchorPoints[0],
),
MinAmt: 2,
Expand Down Expand Up @@ -1146,7 +1147,7 @@ func TestSelectCommitment(t *testing.T) {
},
},
constraints: tapfreighter.CommitmentConstraints{
GroupKey: assetGen.bindGroupKey(
AssetSpecifier: assetGen.assetSpecifierGroupKey(
0, assetGen.anchorPoints[0],
),
MinAmt: 1,
Expand Down
5 changes: 2 additions & 3 deletions tapfreighter/coin_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func (s *CoinSelect) SelectCoins(ctx context.Context,
}

listConstraints := CommitmentConstraints{
GroupKey: constraints.GroupKey,
AssetID: constraints.AssetID,
MinAmt: 1,
AssetSpecifier: constraints.AssetSpecifier,
MinAmt: 1,
}
eligibleCommitments, err := s.coinLister.ListEligibleCoins(
ctx, listConstraints,
Expand Down
19 changes: 4 additions & 15 deletions tapfreighter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand All @@ -28,13 +27,8 @@ import (
//
// NOTE: Only the GroupKey or the AssetID should be set.
type CommitmentConstraints struct {
// GroupKey is the required group key. This is an optional field, if
// set then the asset returned may have a distinct asset ID to the one
// specified below.
GroupKey *btcec.PublicKey

// AssetID is the asset ID that needs to be satisfied.
AssetID *asset.ID
// AssetSpecifier specifies the asset.
AssetSpecifier asset.Specifier

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

// String returns the string representation of the commitment constraints.
func (c *CommitmentConstraints) String() string {
var groupKeyBytes, assetIDBytes []byte
if c.GroupKey != nil {
groupKeyBytes = c.GroupKey.SerializeCompressed()
}
if c.AssetID != nil {
assetIDBytes = c.AssetID[:]
}
assetIDBytes, groupKeyBytes := c.AssetSpecifier.AsBytes()

return fmt.Sprintf("group_key=%x, asset_id=%x, min_amt=%d",
groupKeyBytes, assetIDBytes, c.MinAmt)
}
Expand Down
18 changes: 3 additions & 15 deletions tapfreighter/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,20 +362,11 @@ func (f *AssetWallet) FundPacket(ctx context.Context,
return nil, address.ErrMismatchedHRP
}

// Extract the asset ID and group key from the funding descriptor.
assetId := fundDesc.AssetSpecifier.UnwrapIdToPtr()
if assetId == nil {
return nil, fmt.Errorf("unable to unwrap asset ID")
}

groupKey := fundDesc.AssetSpecifier.UnwrapGroupKeyToPtr()

// We need to find a commitment that has enough assets to satisfy this
// send request. We'll map the address to a set of constraints, so we
// can use that to do Taproot asset coin selection.
constraints := CommitmentConstraints{
GroupKey: groupKey,
AssetID: assetId,
AssetSpecifier: fundDesc.AssetSpecifier,
MinAmt: fundDesc.Amount,
Bip86ScriptKeysOnly: true,
}
Expand Down Expand Up @@ -410,15 +401,12 @@ func (f *AssetWallet) FundBurn(ctx context.Context,
return nil, err
}

groupKey := fundDesc.AssetSpecifier.UnwrapGroupKeyToPtr()

// We need to find a commitment that has enough assets to satisfy this
// send request. We'll map the address to a set of constraints, so we
// can use that to do Taproot asset coin selection.
constraints := CommitmentConstraints{
GroupKey: groupKey,
AssetID: assetId,
MinAmt: fundDesc.Amount,
AssetSpecifier: fundDesc.AssetSpecifier,
MinAmt: fundDesc.Amount,
}
selectedCommitments, err := f.cfg.CoinSelector.SelectCoins(
ctx, constraints, PreferMaxAmount, commitment.TapCommitmentV2,
Expand Down

0 comments on commit 9917c9e

Please sign in to comment.