Skip to content

Commit 6399009

Browse files
committed
supplycommit: refactor asset check for reuse in supplyverifier package
Extract CheckSupplyCommitSupport function for use in the supplyverifier package. Rename fetchLatestAssetMetadata to FetchLatestAssetMetadata to allow external usage.
1 parent 28ddc1d commit 6399009

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

universe/supplycommit/env.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"crypto/sha256"
7+
"errors"
78
"fmt"
89
"net/url"
910

@@ -15,6 +16,7 @@ import (
1516
"github.com/btcsuite/btcd/txscript"
1617
"github.com/btcsuite/btcd/wire"
1718
"github.com/btcsuite/btclog/v2"
19+
"github.com/lightninglabs/taproot-assets/address"
1820
"github.com/lightninglabs/taproot-assets/asset"
1921
"github.com/lightninglabs/taproot-assets/fn"
2022
"github.com/lightninglabs/taproot-assets/mssmt"
@@ -237,9 +239,9 @@ type AssetLookup interface {
237239
rawKey *btcec.PublicKey) (keychain.KeyLocator, error)
238240
}
239241

240-
// fetchLatestAssetMetadata returns the latest asset metadata for the
242+
// FetchLatestAssetMetadata returns the latest asset metadata for the
241243
// given asset specifier.
242-
func fetchLatestAssetMetadata(ctx context.Context, lookup AssetLookup,
244+
func FetchLatestAssetMetadata(ctx context.Context, lookup AssetLookup,
243245
assetSpec asset.Specifier) (proof.MetaReveal, error) {
244246

245247
var zero proof.MetaReveal
@@ -271,6 +273,68 @@ func fetchLatestAssetMetadata(ctx context.Context, lookup AssetLookup,
271273
return *metaReveal, nil
272274
}
273275

276+
// CheckSupplyCommitSupport verifies that the asset group for the given
277+
// asset specifier supports supply commitments, and that this node can generate
278+
// supply commitments for it.
279+
func CheckSupplyCommitSupport(ctx context.Context, assetLookup AssetLookup,
280+
assetSpec asset.Specifier, locallyControlled bool) error {
281+
282+
// Fetch the latest asset metadata for the asset group.
283+
metaReveal, err := FetchLatestAssetMetadata(
284+
ctx, assetLookup, assetSpec,
285+
)
286+
if err != nil {
287+
return fmt.Errorf("faild to fetch asset meta: %w", err)
288+
}
289+
290+
// If the universe commitment flag is not set on the asset metadata,
291+
// then the asset group does not support supply commitments.
292+
if !metaReveal.UniverseCommitments {
293+
return fmt.Errorf("asset group metadata universe " +
294+
"commitments flag indicates that asset does not " +
295+
"support supply commitments")
296+
}
297+
298+
// If a delegation key is not present, then the asset group does not
299+
// support supply commitments.
300+
if metaReveal.DelegationKey.IsNone() {
301+
return fmt.Errorf("asset group metadata does not " +
302+
"specify delegation key, which is required for " +
303+
"supply commitments")
304+
}
305+
306+
// Extract supply commitment delegation pub key from the asset metadata.
307+
delegationPubKey, err := metaReveal.DelegationKey.UnwrapOrErr(
308+
fmt.Errorf("delegation key not found for given asset"),
309+
)
310+
if err != nil {
311+
return err
312+
}
313+
314+
// Fetch the delegation key locator. We need to ensure that the
315+
// delegation key is owned by this node, so that we can generate
316+
// supply commitments (ignore tuples) for this asset group.
317+
_, err = assetLookup.FetchInternalKeyLocator(
318+
ctx, &delegationPubKey,
319+
)
320+
switch {
321+
case errors.Is(err, address.ErrInternalKeyNotFound):
322+
// If local key control is expected, then we return an error
323+
// if the delegation key locator is not found.
324+
if locallyControlled {
325+
return fmt.Errorf("delegation key locator not found; " +
326+
"only delegation key owners can generate " +
327+
"supply commitments")
328+
}
329+
330+
case err != nil:
331+
return fmt.Errorf("failed to fetch delegation key locator: %w",
332+
err)
333+
}
334+
335+
return nil
336+
}
337+
274338
// SupplyTreeView is an interface that allows the state machine to obtain an up
275339
// to date snapshot of the root supply tree, as the sub trees (ignore, burn,
276340
// mint) committed in the main supply tree.

universe/supplycommit/manager.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,7 @@ func (m *Manager) fetchStateMachine(
287287
ctx, cancel := m.WithCtxQuitNoTimeout()
288288
defer cancel()
289289

290-
metaReveal, err := fetchLatestAssetMetadata(
291-
ctx, m.cfg.AssetLookup, assetSpec,
292-
)
293-
if err != nil {
294-
return nil, fmt.Errorf("faild to fetch asset meta: %w", err)
295-
}
296-
297-
err = m.ensureSupplyCommitSupport(ctx, metaReveal)
290+
err = CheckSupplyCommitSupport(ctx, m.cfg.AssetLookup, assetSpec, true)
298291
if err != nil {
299292
return nil, fmt.Errorf("failed to ensure supply commit "+
300293
"support for asset: %w", err)

universe/supplycommit/transitions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
11091109

11101110
// Retrieve latest canonical universe list from the latest
11111111
// metadata for the asset group.
1112-
metadata, err := fetchLatestAssetMetadata(
1112+
metadata, err := FetchLatestAssetMetadata(
11131113
ctx, env.AssetLookup, env.AssetSpec,
11141114
)
11151115
if err != nil {

0 commit comments

Comments
 (0)