From fde982ad78cbaf568dce59102f6e78a4b680d47a Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 23 Aug 2023 11:09:10 +0200 Subject: [PATCH] input: extract to_local script builders Factor out the building of the delay and revoke scripts from NewLocalCommitScriptTree so that they can be re-used later on. --- input/script_utils.go | 46 +++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/input/script_utils.go b/input/script_utils.go index d907737b8..fde0c6f5e 100644 --- a/input/script_utils.go +++ b/input/script_utils.go @@ -2124,27 +2124,14 @@ func NewLocalCommitScriptTree(csvTimeout uint32, // First, we'll need to construct the tapLeaf that'll be our delay CSV // clause. - builder := txscript.NewScriptBuilder() - builder.AddData(schnorr.SerializePubKey(selfKey)) - builder.AddOp(txscript.OP_CHECKSIG) - builder.AddInt64(int64(csvTimeout)) - builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY) - builder.AddOp(txscript.OP_DROP) - - delayScript, err := builder.Script() + delayScript, err := TaprootLocalCommitDelayScript(csvTimeout, selfKey) if err != nil { return nil, err } // Next, we'll need to construct the revocation path, which is just a // simple checksig script. - builder = txscript.NewScriptBuilder() - builder.AddData(schnorr.SerializePubKey(selfKey)) - builder.AddOp(txscript.OP_DROP) - builder.AddData(schnorr.SerializePubKey(revokeKey)) - builder.AddOp(txscript.OP_CHECKSIG) - - revokeScript, err := builder.Script() + revokeScript, err := TaprootLocalCommitRevokeScript(selfKey, revokeKey) if err != nil { return nil, err } @@ -2176,6 +2163,35 @@ func NewLocalCommitScriptTree(csvTimeout uint32, }, nil } +// TaprootLocalCommitDelayScript builds the tap leaf with the CSV delay script +// for the to-local output. +func TaprootLocalCommitDelayScript(csvTimeout uint32, + selfKey *btcec.PublicKey) ([]byte, error) { + + builder := txscript.NewScriptBuilder() + builder.AddData(schnorr.SerializePubKey(selfKey)) + builder.AddOp(txscript.OP_CHECKSIG) + builder.AddInt64(int64(csvTimeout)) + builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY) + builder.AddOp(txscript.OP_DROP) + + return builder.Script() +} + +// TaprootLocalCommitRevokeScript builds the tap leaf with the revocation path +// for the to-local output. +func TaprootLocalCommitRevokeScript(selfKey, revokeKey *btcec.PublicKey) ( + []byte, error) { + + builder := txscript.NewScriptBuilder() + builder.AddData(schnorr.SerializePubKey(selfKey)) + builder.AddOp(txscript.OP_DROP) + builder.AddData(schnorr.SerializePubKey(revokeKey)) + builder.AddOp(txscript.OP_CHECKSIG) + + return builder.Script() +} + // TaprootCommitScriptToSelf creates the taproot witness program that commits // to the revocation (script path) and delay path (script path) in a single // taproot output key. Both the delay script and the revocation script are part