Skip to content

Commit

Permalink
tapgarden: reduce indentation by refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jan 20, 2025
1 parent 9da7023 commit c957068
Showing 1 changed file with 86 additions and 83 deletions.
169 changes: 86 additions & 83 deletions tapgarden/planter.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,108 +809,111 @@ func buildGroupReqs(genesisPoint wire.OutPoint, assetOutputIndex uint32,
// transaction have been created.
}

// If emission isn't enabled, we don't have to do anything else
// for this seedling.
if !seedling.EnableEmission {
continue
}

// If emission is enabled, an internal key for the group should
// already be specified. Use that to derive the key group
// signature along with the tweaked key group.
if seedling.EnableEmission {
if seedling.GroupInternalKey == nil &&
seedling.ExternalKey.IsNone() {

return nil, nil, fmt.Errorf("unable to " +
"derive group key, both internal and " +
"external keys are unspecified")
}
if seedling.GroupInternalKey == nil &&
seedling.ExternalKey.IsNone() {

// If seedling.GroupTapscriptRoot is specified and the
// seedling includes an external key, we must use group
// key V1. As a result, seedling.GroupTapscriptRoot will
// be treated as a custom tapscript subtree root, which
// we will graft into the group key's tapscript tree. We
// will proceed with this now.
var (
tsRoot = seedling.GroupTapscriptRoot
customRootHash fn.Option[chainhash.Hash]
)
if seedling.ExternalKey.IsSome() {
// If seedling.GroupTapscriptRoot is specified,
// set it to the custom root hash. Then we will
// calculate a new tapscript root hash which
// includes the custom root as a grafted
// subtree.
if len(tsRoot) > 0 {
r, err := chainhash.NewHash(tsRoot)
if err != nil {
return nil, nil, err
}

customRootHash = fn.Some(*r)
}
return nil, nil, fmt.Errorf("unable to " +
"derive group key, both internal and " +
"external keys are unspecified")
}

// Construct an asset group tapscript tree,
// incorporating the optional custom subtree
// through grafting.
//
// At this point, we are constructing the group
// tapscript tree root whether the
// customRootHash is defined.
tapscriptTree, _, err :=
asset.NewGroupKeyTapscriptRoot(
// TODO(guggero): Make this
// configurable in the future.
asset.PedersenVersion,
assetGen.ID(), customRootHash,
)
// If seedling.GroupTapscriptRoot is specified and the
// seedling includes an external key, we must use group
// key V1. As a result, seedling.GroupTapscriptRoot will
// be treated as a custom tapscript subtree root, which
// we will graft into the group key's tapscript tree. We
// will proceed with this now.
var (
tsRoot = seedling.GroupTapscriptRoot
customRootHash fn.Option[chainhash.Hash]
)
if seedling.ExternalKey.IsSome() {
// If seedling.GroupTapscriptRoot is specified,
// set it to the custom root hash. Then we will
// calculate a new tapscript root hash which
// includes the custom root as a grafted
// subtree.
if len(tsRoot) > 0 {
r, err := chainhash.NewHash(tsRoot)
if err != nil {
return nil, nil, err
}

// Update the group tapscript tree root hash to
// the new root hash. If customRootHash is
// defined, the new root hash incorporates it as
// a subtree.
tsRoot = fn.ByteSlice(tapscriptTree.Root())
customRootHash = fn.Some(*r)
}

// The group internal key should be set at this point.
// Construct an asset group tapscript tree,
// incorporating the optional custom subtree
// through grafting.
//
// If an external key is present, the internal key
// should be a public key derived from the external
// key.
if seedling.GroupInternalKey == nil {
return nil, nil, fmt.Errorf("internal key is " +
"missing for seedling")
}

groupReq, err := asset.NewGroupKeyRequest(
*seedling.GroupInternalKey,
seedling.ExternalKey, assetGen,
protoAsset, tsRoot, customRootHash,
)
if err != nil {
return nil, nil, fmt.Errorf("unable to "+
"request asset group creation: %w", err)
}

genTx, err := groupReq.BuildGroupVirtualTx(
genBuilder,
// At this point, we are constructing the group
// tapscript tree root whether the
// customRootHash is defined.
tapscriptTree, _, err := asset.NewGroupKeyTapscriptRoot(
// TODO(guggero): Make this configurable in the
// future.
asset.PedersenVersion, assetGen.ID(),
customRootHash,
)
if err != nil {
return nil, nil, err
}

groupReqs = append(groupReqs, *groupReq)
genTXs = append(genTXs, *genTx)
// Update the group tapscript tree root hash to
// the new root hash. If customRootHash is
// defined, the new root hash incorporates it as
// a subtree.
tsRoot = fn.ByteSlice(tapscriptTree.Root())
}

newGroupKey := &asset.GroupKey{
Version: groupReq.Version,
RawKey: *seedling.GroupInternalKey,
TapscriptRoot: seedling.GroupTapscriptRoot,
}
// The group internal key should be set at this point.
//
// If an external key is present, the internal key
// should be a public key derived from the external
// key.
if seedling.GroupInternalKey == nil {
return nil, nil, fmt.Errorf("internal key is " +
"missing for seedling")
}

groupReq, err := asset.NewGroupKeyRequest(
*seedling.GroupInternalKey,
seedling.ExternalKey, assetGen,
protoAsset, tsRoot, customRootHash,
)
if err != nil {
return nil, nil, fmt.Errorf("unable to "+
"request asset group creation: %w", err)
}

newGroups[seedlingName] = &asset.AssetGroup{
Genesis: &assetGen,
GroupKey: newGroupKey,
}
genTx, err := groupReq.BuildGroupVirtualTx(
genBuilder,
)
if err != nil {
return nil, nil, err
}

groupReqs = append(groupReqs, *groupReq)
genTXs = append(genTXs, *genTx)

newGroupKey := &asset.GroupKey{
Version: groupReq.Version,
RawKey: *seedling.GroupInternalKey,
TapscriptRoot: seedling.GroupTapscriptRoot,
}

newGroups[seedlingName] = &asset.AssetGroup{
Genesis: &assetGen,
GroupKey: newGroupKey,
}
}

Expand Down

0 comments on commit c957068

Please sign in to comment.