Skip to content

Commit

Permalink
tapdb: parse new fields version and custom_subtree_root to/from db row
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Jan 8, 2025
1 parent 783a92a commit 2311735
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
7 changes: 4 additions & 3 deletions tapdb/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,10 @@ func (t *TapAddressBook) QueryAssetGroup(ctx context.Context,
}

assetGroup.GroupKey, err = parseGroupKeyInfo(
groupInfo.TweakedGroupKey, groupInfo.RawKey,
groupInfo.WitnessStack, groupInfo.TapscriptRoot,
groupInfo.KeyFamily, groupInfo.KeyIndex,
groupInfo.Version, groupInfo.TweakedGroupKey,
groupInfo.RawKey, groupInfo.WitnessStack,
groupInfo.TapscriptRoot, groupInfo.KeyFamily,
groupInfo.KeyIndex, groupInfo.CustomSubtreeRoot,
)

return err
Expand Down
58 changes: 45 additions & 13 deletions tapdb/assets_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
"github.com/lightningnetwork/lnd/keychain"
Expand Down Expand Up @@ -327,11 +328,21 @@ func upsertGroupKey(ctx context.Context, groupKey *asset.GroupKey,
return nullID, ErrTapscriptRootSize
}

// Formulate custom tapscript subtree root bytes if specified.
customTapscriptRoot := fn.MapOptionZ(
groupKey.CustomTapscriptRoot,
func(root chainhash.Hash) []byte {
return root[:]
},
)

groupID, err := q.UpsertAssetGroupKey(ctx, AssetGroupKey{
TweakedGroupKey: tweakedKeyBytes,
TapscriptRoot: groupKey.TapscriptRoot,
InternalKeyID: keyID,
GenesisPointID: genesisPointID,
Version: int32(groupKey.Version),
TweakedGroupKey: tweakedKeyBytes,
TapscriptRoot: groupKey.TapscriptRoot,
InternalKeyID: keyID,
GenesisPointID: genesisPointID,
CustomSubtreeRoot: customTapscriptRoot,
})
if err != nil {
return nullID, fmt.Errorf("%w: %w", ErrUpsertGroupKey, err)
Expand Down Expand Up @@ -541,9 +552,10 @@ func fetchGroupByGenesis(ctx context.Context, q GroupStore,
}

groupKey, err := parseGroupKeyInfo(
groupInfo.TweakedGroupKey, groupInfo.RawKey,
groupInfo.Version, groupInfo.TweakedGroupKey, groupInfo.RawKey,
groupInfo.WitnessStack, groupInfo.TapscriptRoot,
groupInfo.KeyFamily, groupInfo.KeyIndex,
groupInfo.CustomSubtreeRoot,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -575,8 +587,10 @@ func fetchGroupByGroupKey(ctx context.Context, q GroupStore,
}

groupKey, err := parseGroupKeyInfo(
groupKeyQuery, groupInfo.RawKey, groupInfo.WitnessStack,
groupInfo.TapscriptRoot, groupInfo.KeyFamily, groupInfo.KeyIndex,
groupInfo.Version, groupKeyQuery, groupInfo.RawKey,
groupInfo.WitnessStack, groupInfo.TapscriptRoot,
groupInfo.KeyFamily, groupInfo.KeyIndex,
groupInfo.CustomSubtreeRoot,
)
if err != nil {
return nil, err
Expand All @@ -589,8 +603,9 @@ func fetchGroupByGroupKey(ctx context.Context, q GroupStore,
}

// parseGroupKeyInfo maps information on a group key into a GroupKey.
func parseGroupKeyInfo(tweakedKey, rawKey, witness, tapscriptRoot []byte,
keyFamily, keyIndex int32) (*asset.GroupKey, error) {
func parseGroupKeyInfo(version int32, tweakedKey, rawKey, witness,
tapscriptRoot []byte, keyFamily, keyIndex int32,
customSubtreeRoot []byte) (*asset.GroupKey, error) {

tweakedGroupKey, err := btcec.ParsePubKey(tweakedKey)
if err != nil {
Expand Down Expand Up @@ -618,11 +633,28 @@ func parseGroupKeyInfo(tweakedKey, rawKey, witness, tapscriptRoot []byte,
}
}

// Parse group key version from database row.
groupKeyVersion, err := asset.NewGroupKeyVersionFromInt32(version)
if err != nil {
return nil, err
}

// Parse custom tapscript root if specified.
var customSubtreeRootHash fn.Option[chainhash.Hash]
if len(customSubtreeRoot) != 0 {
var rootHash chainhash.Hash
copy(rootHash[:], customSubtreeRoot)

customSubtreeRootHash = fn.Some(rootHash)
}

return &asset.GroupKey{
RawKey: groupRawKey,
GroupPubKey: *tweakedGroupKey,
TapscriptRoot: tapscriptRoot,
Witness: groupWitness,
Version: groupKeyVersion,
RawKey: groupRawKey,
GroupPubKey: *tweakedGroupKey,
TapscriptRoot: tapscriptRoot,
Witness: groupWitness,
CustomTapscriptRoot: customSubtreeRootHash,
}, nil
}

Expand Down

0 comments on commit 2311735

Please sign in to comment.