Skip to content

Commit c8a0304

Browse files
committed
asset: function asset.TapCommitmentKey takes an asset specifier as arg
This commit modifies the `asset.TapCommitmentKey` function such that it takes the asset specifier as an argument.
1 parent aadd2fe commit c8a0304

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

address/address.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ func (a *Tap) AttachGenesis(gen asset.Genesis) {
276276
// TapCommitmentKey is the key that maps to the root commitment for the asset
277277
// group specified by a Taproot Asset address.
278278
func (a *Tap) TapCommitmentKey() [32]byte {
279-
return asset.TapCommitmentKey(a.AssetID, a.GroupKey)
279+
assetSpecifier := asset.NewSpecifierOptionalGroupPubKey(
280+
a.AssetID, a.GroupKey,
281+
)
282+
283+
return asset.TapCommitmentKey(assetSpecifier)
280284
}
281285

282286
// AssetCommitmentKey is the key that maps to the asset leaf for the asset

asset/asset.go

+24-9
Original file line numberDiff line numberDiff line change
@@ -1552,23 +1552,38 @@ func New(genesis Genesis, amount, locktime, relativeLocktime uint64,
15521552
}
15531553

15541554
// TapCommitmentKey is the key that maps to the root commitment for a specific
1555-
// asset group within a TapCommitment.
1555+
// asset within a TapCommitment.
15561556
//
15571557
// NOTE: This function is also used outside the asset package.
1558-
func TapCommitmentKey(assetID ID, groupKey *btcec.PublicKey) [32]byte {
1559-
if groupKey == nil {
1560-
return assetID
1558+
func TapCommitmentKey(assetSpecifier Specifier) [32]byte {
1559+
var commitmentKey [32]byte
1560+
1561+
switch {
1562+
case assetSpecifier.HasGroupPubKey():
1563+
assetSpecifier.WhenGroupPubKey(func(pubKey btcec.PublicKey) {
1564+
serializedPubKey := schnorr.SerializePubKey(&pubKey)
1565+
commitmentKey = sha256.Sum256(serializedPubKey)
1566+
})
1567+
1568+
case assetSpecifier.HasId():
1569+
assetSpecifier.WhenId(func(id ID) {
1570+
commitmentKey = id
1571+
})
1572+
1573+
default:
1574+
// We should never reach this point as the asset specifier
1575+
// should always have either a group public key, an asset ID, or
1576+
// both.
1577+
panic("invalid asset specifier")
15611578
}
1562-
return sha256.Sum256(schnorr.SerializePubKey(groupKey))
1579+
1580+
return commitmentKey
15631581
}
15641582

15651583
// TapCommitmentKey is the key that maps to the root commitment for a specific
15661584
// asset group within a TapCommitment.
15671585
func (a *Asset) TapCommitmentKey() [32]byte {
1568-
if a.GroupKey == nil {
1569-
return TapCommitmentKey(a.Genesis.ID(), nil)
1570-
}
1571-
return TapCommitmentKey(a.Genesis.ID(), &a.GroupKey.GroupPubKey)
1586+
return TapCommitmentKey(a.Specifier())
15721587
}
15731588

15741589
// AssetCommitmentKey returns a key which can be used to locate an

commitment/asset.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010

11-
"github.com/btcsuite/btcd/btcec/v2"
1211
"github.com/lightninglabs/taproot-assets/asset"
1312
"github.com/lightninglabs/taproot-assets/fn"
1413
"github.com/lightninglabs/taproot-assets/mssmt"
@@ -151,16 +150,15 @@ func parseCommon(assets ...*asset.Asset) (*AssetCommitment, error) {
151150
assetsMap[key] = newAsset
152151
}
153152

154-
var groupPubKey *btcec.PublicKey
155-
if assetGroupKey != nil {
156-
groupPubKey = &assetGroupKey.GroupPubKey
157-
}
158-
159153
// The tapKey here is what will be used to place this asset commitment
160154
// into the top-level Taproot Asset commitment. For assets without a
161155
// group key, then this will be the normal asset ID. Otherwise, this'll
162156
// be the sha256 of the group key.
163-
tapKey := asset.TapCommitmentKey(firstAssetID, groupPubKey)
157+
assetSpecifier := asset.NewSpecifierOptionalGroupKey(
158+
firstAssetID, assetGroupKey,
159+
)
160+
161+
tapKey := asset.TapCommitmentKey(assetSpecifier)
164162

165163
return &AssetCommitment{
166164
Version: maxVersion,

tapsend/send.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ type FundingDescriptor struct {
172172
// TapCommitmentKey is the key that maps to the root commitment for the asset
173173
// group specified by a recipient descriptor.
174174
func (r *FundingDescriptor) TapCommitmentKey() [32]byte {
175-
return asset.TapCommitmentKey(r.ID, r.GroupKey)
175+
assetSpecifier := asset.NewIdSpecifier(r.ID)
176+
if r.GroupKey != nil {
177+
assetSpecifier = asset.NewSpecifier(r.ID, *r.GroupKey)
178+
}
179+
180+
return asset.TapCommitmentKey(assetSpecifier)
176181
}
177182

178183
// DescribeRecipients extracts the recipient descriptors from a Taproot Asset

0 commit comments

Comments
 (0)