Skip to content

Commit

Permalink
rpc: add tapscript sibling to FinalizeBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed Feb 22, 2024
1 parent 4d6630b commit 1c8dda0
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
Expand Down Expand Up @@ -515,12 +516,44 @@ func (r *rpcServer) FinalizeBatch(_ context.Context,
req *mintrpc.FinalizeBatchRequest) (*mintrpc.FinalizeBatchResponse,
error) {

var batchSibling *asset.TapscriptTreeNodes

feeRate, err := checkFeeRateSanity(req.FeeRate)
if err != nil {
return nil, err
}
feeRateOpt := fn.MaybeSome(feeRate)

batchTapscriptTree := req.GetFullTree()
batchTapBranch := req.GetBranch()

switch {
case batchTapscriptTree != nil && batchTapBranch != nil:
return nil, fmt.Errorf("cannot specify both tapscript tree " +
"and tapscript tree branches")

case batchTapscriptTree != nil:
batchSibling, err = marshalTapscriptFullTree(batchTapscriptTree)
if err != nil {
return nil, fmt.Errorf("invalid tapscript tree: %w",
err)
}

case batchTapBranch != nil:
batchSibling, err = marshalTapscriptBranch(batchTapBranch)
if err != nil {
return nil, fmt.Errorf("invalid tapscript branch: %w",
err)
}
}
tapTreeOpt := fn.MaybeSome(batchSibling)

batch, err := r.cfg.AssetMinter.FinalizeBatch(feeRate)
batch, err := r.cfg.AssetMinter.FinalizeBatch(
tapgarden.FinalizeParams{
FeeRate: feeRateOpt,
TapTree: tapTreeOpt,
},
)
if err != nil {
return nil, fmt.Errorf("unable to finalize batch: %w", err)
}
Expand Down Expand Up @@ -2917,6 +2950,29 @@ func marshalBatchState(batch *tapgarden.MintingBatch) (mintrpc.BatchState,
}
}

func marshalTapscriptFullTree(tree *taprpc.TapscriptFullTree) (
*asset.TapscriptTreeNodes, error) {

rpcLeaves := tree.GetAllLeaves()
leaves := fn.Map(rpcLeaves, func(l *taprpc.TapLeaf) txscript.TapLeaf {
return txscript.NewBaseTapLeaf(l.Script)
})

return asset.TapTreeNodesFromLeaves(leaves)
}

func marshalTapscriptBranch(branch *taprpc.TapBranch) (*asset.TapscriptTreeNodes,
error) {

branchData := [][]byte{branch.LeftTaphash, branch.RightTaphash}
tapBranch, err := asset.DecodeTapBranchNodes(branchData)
if err != nil {
return nil, err
}

return fn.Ptr(asset.FromBranch(*tapBranch)), nil
}

// UnmarshalScriptKey parses the RPC script key into the native counterpart.
func UnmarshalScriptKey(rpcKey *taprpc.ScriptKey) (*asset.ScriptKey, error) {
var (
Expand Down

0 comments on commit 1c8dda0

Please sign in to comment.