|
6 | 6 |
|
7 | 7 | "github.com/btcsuite/btcd/btcec/v2" |
8 | 8 | "github.com/lightninglabs/taproot-assets/asset" |
| 9 | + "github.com/lightninglabs/taproot-assets/fn" |
9 | 10 | "github.com/lightninglabs/taproot-assets/universe/supplycommit" |
10 | 11 | ) |
11 | 12 |
|
@@ -33,3 +34,94 @@ func FetchDelegationKey(ctx context.Context, |
33 | 34 |
|
34 | 35 | return delegationKey, nil |
35 | 36 | } |
| 37 | + |
| 38 | +// FetchPreCommits returns all pre-commitment outputs expected to be spent by |
| 39 | +// the anchoring transaction of the specified supply commitment. |
| 40 | +func FetchPreCommits(ctx context.Context, |
| 41 | + assetLookup supplycommit.AssetLookup, supplyCommitView SupplyCommitView, |
| 42 | + assetSpec asset.Specifier, supplyCommit supplycommit.RootCommitment, |
| 43 | + mintEvents []supplycommit.NewMintEvent) ([]supplycommit.PreCommitment, |
| 44 | + error) { |
| 45 | + |
| 46 | + // Get supply commit block height. This will be used to filter out any |
| 47 | + // pre-commitments that are above the supply commitment height. |
| 48 | + chainCommit, err := supplyCommit.CommitmentBlock.UnwrapOrErr( |
| 49 | + fmt.Errorf("commitment block missing"), |
| 50 | + ) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("unable to extract supply commitment "+ |
| 53 | + "chain block: %w", err) |
| 54 | + } |
| 55 | + supplyCommitBlockHeight := chainCommit.Height |
| 56 | + |
| 57 | + // Fetch all known unspent pre-commitment outputs for the asset |
| 58 | + // group. |
| 59 | + preCommits, err := supplyCommitView.UnspentPrecommits( |
| 60 | + ctx, assetSpec, false, |
| 61 | + ).Unpack() |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("unable to fetch unspent "+ |
| 64 | + "pre-commitments: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Filter out any pre-commitments that are above the supply commitment |
| 68 | + // height. These can't be spent by the current supply commitment. |
| 69 | + filteredPreCommits := make( |
| 70 | + []supplycommit.PreCommitment, 0, len(preCommits), |
| 71 | + ) |
| 72 | + for _, pc := range preCommits { |
| 73 | + if pc.BlockHeight <= supplyCommitBlockHeight { |
| 74 | + filteredPreCommits = append(filteredPreCommits, pc) |
| 75 | + } |
| 76 | + } |
| 77 | + preCommits = filteredPreCommits |
| 78 | + |
| 79 | + // If there are no mint events, then we can return early here because |
| 80 | + // there won't be any new pre-commitments to consider. |
| 81 | + if len(mintEvents) == 0 { |
| 82 | + return preCommits, nil |
| 83 | + } |
| 84 | + |
| 85 | + // We'll need the delegation key to extract any new pre-commitments from |
| 86 | + // the mint events. |
| 87 | + delegationKey, err := FetchDelegationKey( |
| 88 | + ctx, assetLookup, assetSpec, |
| 89 | + ) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("unable to fetch delegation key: %w", |
| 92 | + err) |
| 93 | + } |
| 94 | + |
| 95 | + // Create a set of outpoints from all known pre-commits. This will be |
| 96 | + // used to prevent duplicates when adding new pre-commits from the mint |
| 97 | + // events. |
| 98 | + preCommitOutPoints := fn.NewSet(fn.Map( |
| 99 | + preCommits, func(preCommit supplycommit.PreCommitment) string { |
| 100 | + return preCommit.OutPoint().String() |
| 101 | + }, |
| 102 | + )...) |
| 103 | + |
| 104 | + // Collect pre-commitments from the mint events that we haven't already |
| 105 | + // seen. |
| 106 | + for idx := range mintEvents { |
| 107 | + mintEvent := mintEvents[idx] |
| 108 | + |
| 109 | + preCommit, err := supplycommit.NewPreCommitFromMintEvent( |
| 110 | + mintEvent, delegationKey, |
| 111 | + ) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("unable to extract "+ |
| 114 | + "pre-commitment from mint event: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + // If we already have this pre-commitment, then skip it. |
| 118 | + op := preCommit.OutPoint() |
| 119 | + if preCommitOutPoints.Contains(op.String()) { |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + preCommits = append(preCommits, preCommit) |
| 124 | + } |
| 125 | + |
| 126 | + return preCommits, nil |
| 127 | +} |
0 commit comments