Skip to content

Commit 5c0182f

Browse files
committed
universe: add new CommitPoint() and TapscriptRoot() methods to RootCommitment
1 parent 103ccbe commit 5c0182f

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

universe/supplycommit/env.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535
type SupplySubTree uint8
3636

3737
const (
38+
3839
// MintTreeType is the sub tree that tracks mints.
3940
MintTreeType SupplySubTree = iota
4041

@@ -235,6 +236,39 @@ func (r *RootCommitment) TxOut() (*wire.TxOut, error) {
235236
return txOut, err
236237
}
237238

239+
// CommitPoint returns the outpoint that corresponds to the root commitment.
240+
func (r *RootCommitment) CommitPoint() wire.OutPoint {
241+
return wire.OutPoint{
242+
Hash: r.Txn.TxHash(),
243+
Index: r.TxOutIdx,
244+
}
245+
}
246+
247+
// computeSupplyCommitTapscriptRoot creates the tapscript root hash for a supply
248+
// commitment with the given supply root hash.
249+
func computeSupplyCommitTapscriptRoot(supplyRootHash mssmt.NodeHash,
250+
) ([]byte, error) {
251+
252+
// Create a non-spendable script leaf that commits to the supply root.
253+
tapLeaf, err := asset.NewNonSpendableScriptLeaf(
254+
asset.PedersenVersion, supplyRootHash[:],
255+
)
256+
if err != nil {
257+
return nil, fmt.Errorf("unable to create leaf: %w", err)
258+
}
259+
260+
tapscriptTree := txscript.AssembleTaprootScriptTree(tapLeaf)
261+
rootHash := tapscriptTree.RootNode.TapHash()
262+
return rootHash[:], nil
263+
}
264+
265+
// TapscriptRoot returns the tapscript root hash that commits to the supply
266+
// root. This is tweaked with the internal key to derive the output key.
267+
func (r *RootCommitment) TapscriptRoot() ([]byte, error) {
268+
supplyRootHash := r.SupplyRoot.NodeHash()
269+
return computeSupplyCommitTapscriptRoot(supplyRootHash)
270+
}
271+
238272
// RootCommitTxOut returns the transaction output that corresponds to the root
239273
// commitment. This is used to create a new commitment output.
240274
func RootCommitTxOut(internalKey *btcec.PublicKey,
@@ -245,21 +279,13 @@ func RootCommitTxOut(internalKey *btcec.PublicKey,
245279
if tapOutKey == nil {
246280
// We'll create a new unspendable output that contains a
247281
// commitment to the root.
248-
//
249-
// TODO(roasbeef): need other version info here/
250-
tapLeaf, err := asset.NewNonSpendableScriptLeaf(
251-
asset.PedersenVersion, supplyRootHash[:],
252-
)
282+
rootHash, err := computeSupplyCommitTapscriptRoot(supplyRootHash)
253283
if err != nil {
254-
return nil, nil, fmt.Errorf("unable to create leaf: %w",
255-
err)
284+
return nil, nil, err
256285
}
257286

258-
tapscriptTree := txscript.AssembleTaprootScriptTree(tapLeaf)
259-
260-
rootHash := tapscriptTree.RootNode.TapHash()
261287
taprootOutputKey = txscript.ComputeTaprootOutputKey(
262-
internalKey, rootHash[:],
288+
internalKey, rootHash,
263289
)
264290
} else {
265291
taprootOutputKey = tapOutKey

0 commit comments

Comments
 (0)