2019-03-07 02:20:51 +00:00
|
|
|
package swap
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-04 10:20:45 +00:00
|
|
|
"crypto/sha256"
|
2019-03-06 20:13:50 +00:00
|
|
|
"errors"
|
2020-07-20 14:28:08 +00:00
|
|
|
"fmt"
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2022-05-11 14:37:50 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2022-04-14 16:40:30 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
2022-05-11 14:37:50 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2022-05-11 14:37:50 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2022-04-14 16:40:30 +00:00
|
|
|
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
)
|
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
// HtlcOutputType defines the output type of the htlc that is published.
|
|
|
|
type HtlcOutputType uint8
|
|
|
|
|
|
|
|
const (
|
2022-12-15 15:43:38 +00:00
|
|
|
// HtlcP2WSH is a pay-to-witness-script-hash output (segwit only).
|
2019-04-04 10:20:45 +00:00
|
|
|
HtlcP2WSH HtlcOutputType = iota
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
// HtlcP2TR is a pay-to-taproot output with three separate spend paths.
|
|
|
|
HtlcP2TR
|
2019-04-04 10:20:45 +00:00
|
|
|
)
|
|
|
|
|
2020-07-20 14:28:08 +00:00
|
|
|
// ScriptVersion defines the HTLC script version.
|
|
|
|
type ScriptVersion uint8
|
|
|
|
|
|
|
|
const (
|
2020-07-20 16:16:28 +00:00
|
|
|
// HtlcV2 refers to the improved version of the HTLC script.
|
2022-11-04 15:06:22 +00:00
|
|
|
HtlcV2 ScriptVersion = iota
|
2022-04-14 16:40:30 +00:00
|
|
|
|
|
|
|
// HtlcV3 refers to an upgraded version of HtlcV2 implemented with
|
|
|
|
// tapscript.
|
|
|
|
HtlcV3
|
2020-07-20 14:28:08 +00:00
|
|
|
)
|
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
// htlcScript defines an interface for the different HTLC implementations.
|
|
|
|
type HtlcScript interface {
|
|
|
|
// genSuccessWitness returns the success script to spend this htlc with
|
|
|
|
// the preimage.
|
2022-04-14 16:40:30 +00:00
|
|
|
genSuccessWitness(receiverSig []byte,
|
|
|
|
preimage lntypes.Preimage) (wire.TxWitness, error)
|
2020-07-20 16:16:28 +00:00
|
|
|
|
|
|
|
// GenTimeoutWitness returns the timeout script to spend this htlc after
|
|
|
|
// timeout.
|
2022-04-14 16:40:30 +00:00
|
|
|
GenTimeoutWitness(senderSig []byte) (wire.TxWitness, error)
|
2020-07-20 16:16:28 +00:00
|
|
|
|
|
|
|
// IsSuccessWitness checks whether the given stack is valid for
|
|
|
|
// redeeming the htlc.
|
|
|
|
IsSuccessWitness(witness wire.TxWitness) bool
|
|
|
|
|
2022-03-29 09:31:23 +00:00
|
|
|
// lockingConditions return the address, pkScript and sigScript (if
|
|
|
|
// required) for a htlc script.
|
|
|
|
lockingConditions(HtlcOutputType, *chaincfg.Params) (btcutil.Address,
|
|
|
|
[]byte, []byte, error)
|
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
// MaxSuccessWitnessSize returns the maximum witness size for the
|
|
|
|
// success case witness.
|
|
|
|
MaxSuccessWitnessSize() int
|
|
|
|
|
|
|
|
// MaxTimeoutWitnessSize returns the maximum witness size for the
|
|
|
|
// timeout case witness.
|
|
|
|
MaxTimeoutWitnessSize() int
|
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
// TimeoutScript returns the redeem script required to unlock the htlc
|
|
|
|
// after timeout.
|
|
|
|
TimeoutScript() []byte
|
|
|
|
|
|
|
|
// SuccessScript returns the redeem script required to unlock the htlc
|
|
|
|
// using the preimage.
|
|
|
|
SuccessScript() []byte
|
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
// SuccessSequence returns the sequence to spend this htlc in the
|
|
|
|
// success case.
|
|
|
|
SuccessSequence() uint32
|
2022-03-29 11:17:19 +00:00
|
|
|
|
|
|
|
// SigHash is the signature hash to use for transactions spending from
|
|
|
|
// the htlc.
|
|
|
|
SigHash() txscript.SigHashType
|
2020-07-20 16:16:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// Htlc contains relevant htlc information from the receiver perspective.
|
|
|
|
type Htlc struct {
|
2020-07-20 16:16:28 +00:00
|
|
|
HtlcScript
|
|
|
|
|
2020-07-20 14:28:08 +00:00
|
|
|
Version ScriptVersion
|
2019-04-04 10:20:45 +00:00
|
|
|
PkScript []byte
|
|
|
|
Hash lntypes.Hash
|
|
|
|
OutputType HtlcOutputType
|
|
|
|
ChainParams *chaincfg.Params
|
|
|
|
Address btcutil.Address
|
|
|
|
SigScript []byte
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-05-16 16:37:53 +00:00
|
|
|
// dummyPubKey is a valid public key use for the quote htlc
|
|
|
|
// construction.
|
|
|
|
dummyPubKey = [33]byte{
|
|
|
|
0x03, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33, 0x09, 0xfb,
|
|
|
|
0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34, 0x25, 0x21, 0x88,
|
|
|
|
0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4, 0x7f, 0x5b, 0x2a, 0x4b,
|
|
|
|
0x7d, 0x44, 0x8e,
|
|
|
|
}
|
|
|
|
|
|
|
|
// quoteHash is an empty hash used for the quote htlc construction.
|
2019-03-06 20:13:50 +00:00
|
|
|
quoteHash lntypes.Hash
|
|
|
|
|
2022-05-16 16:37:53 +00:00
|
|
|
// QuoteHtlcP2WSH is a template script just used for sweep fee
|
|
|
|
// estimation.
|
2022-11-16 18:01:28 +00:00
|
|
|
QuoteHtlcP2WSH, _ = NewHtlcV2(
|
|
|
|
^int32(0), dummyPubKey, dummyPubKey, quoteHash,
|
|
|
|
&chaincfg.MainNetParams,
|
2022-05-16 16:37:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// QuoteHtlcP2TR is a template script just used for sweep fee
|
|
|
|
// estimation.
|
2022-11-16 18:01:28 +00:00
|
|
|
QuoteHtlcP2TR, _ = NewHtlcV3(
|
2023-01-09 17:49:26 +00:00
|
|
|
input.MuSig2Version100RC2, ^int32(0), dummyPubKey, dummyPubKey,
|
|
|
|
dummyPubKey, dummyPubKey, quoteHash, &chaincfg.MainNetParams,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
2020-07-20 16:16:28 +00:00
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
// ErrInvalidScriptVersion is returned when an unknown htlc version
|
2022-11-04 15:06:22 +00:00
|
|
|
// is provided to NewHtlc. The supported version are HtlcV2, HtlcV3
|
|
|
|
// as enums.
|
2020-07-20 16:16:28 +00:00
|
|
|
ErrInvalidScriptVersion = fmt.Errorf("invalid script version")
|
2022-04-14 16:40:30 +00:00
|
|
|
|
|
|
|
// ErrInvalidOutputSelected is returned when a taproot output is
|
2022-11-04 15:06:22 +00:00
|
|
|
// selected for a v2 script.
|
2022-04-14 16:40:30 +00:00
|
|
|
ErrInvalidOutputSelected = fmt.Errorf("taproot output selected for " +
|
|
|
|
"non taproot htlc")
|
2022-11-16 18:01:28 +00:00
|
|
|
|
|
|
|
// ErrInvalidOutputType is returned when an unknown output type is
|
|
|
|
// associated with a certain swap htlc.
|
|
|
|
ErrInvalidOutputType = fmt.Errorf("invalid htlc output type")
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2020-04-30 13:37:01 +00:00
|
|
|
// String returns the string value of HtlcOutputType.
|
|
|
|
func (h HtlcOutputType) String() string {
|
|
|
|
switch h {
|
|
|
|
case HtlcP2WSH:
|
|
|
|
return "P2WSH"
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
case HtlcP2TR:
|
|
|
|
return "P2TR"
|
|
|
|
|
2020-04-30 13:37:01 +00:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
// NewHtlcV2 returns a new V2 (P2WSH) HTLC instance.
|
|
|
|
func NewHtlcV2(cltvExpiry int32, senderKey, receiverKey [33]byte,
|
|
|
|
hash lntypes.Hash, chainParams *chaincfg.Params) (*Htlc, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
htlc, err := newHTLCScriptV2(
|
|
|
|
cltvExpiry, senderKey, receiverKey, hash,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
2020-07-20 14:28:08 +00:00
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-14 16:40:30 +00:00
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
address, pkScript, sigScript, err := htlc.lockingConditions(
|
|
|
|
HtlcP2WSH, chainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get address: %w", err)
|
2020-07-20 14:28:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
return &Htlc{
|
|
|
|
HtlcScript: htlc,
|
|
|
|
Hash: hash,
|
|
|
|
Version: HtlcV2,
|
|
|
|
PkScript: pkScript,
|
|
|
|
OutputType: HtlcP2WSH,
|
|
|
|
ChainParams: chainParams,
|
|
|
|
Address: address,
|
|
|
|
SigScript: sigScript,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHtlcV3 returns a new V3 HTLC (P2TR) instance. Internal pubkey generated
|
|
|
|
// by both participants must be provided.
|
2023-01-09 17:49:26 +00:00
|
|
|
func NewHtlcV3(muSig2Version input.MuSig2Version, cltvExpiry int32,
|
|
|
|
senderInternalKey, receiverInternalKey, senderKey, receiverKey [33]byte,
|
|
|
|
hash lntypes.Hash, chainParams *chaincfg.Params) (*Htlc, error) {
|
2022-11-16 18:01:28 +00:00
|
|
|
|
|
|
|
htlc, err := newHTLCScriptV3(
|
2023-01-09 17:49:26 +00:00
|
|
|
muSig2Version, cltvExpiry, senderInternalKey,
|
|
|
|
receiverInternalKey, senderKey, receiverKey, hash,
|
2022-11-16 18:01:28 +00:00
|
|
|
)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:31:23 +00:00
|
|
|
address, pkScript, sigScript, err := htlc.lockingConditions(
|
2022-11-16 18:01:28 +00:00
|
|
|
HtlcP2TR, chainParams,
|
2022-03-29 09:31:23 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Htlc{
|
|
|
|
HtlcScript: htlc,
|
|
|
|
Hash: hash,
|
2022-11-16 18:01:28 +00:00
|
|
|
Version: HtlcV3,
|
2022-03-29 09:31:23 +00:00
|
|
|
PkScript: pkScript,
|
2022-11-16 18:01:28 +00:00
|
|
|
OutputType: HtlcP2TR,
|
2022-03-29 09:31:23 +00:00
|
|
|
ChainParams: chainParams,
|
|
|
|
Address: address,
|
|
|
|
SigScript: sigScript,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// segwitV0LockingConditions provides the address, pkScript and sigScript (if
|
|
|
|
// required) for the segwit v0 script and output type provided.
|
|
|
|
func segwitV0LockingConditions(outputType HtlcOutputType,
|
|
|
|
chainParams *chaincfg.Params, script []byte) (btcutil.Address,
|
|
|
|
[]byte, []byte, error) {
|
2019-04-04 10:20:45 +00:00
|
|
|
|
|
|
|
switch outputType {
|
|
|
|
case HtlcP2WSH:
|
2022-03-29 09:31:23 +00:00
|
|
|
pkScript, err := input.WitnessScriptHash(script)
|
2022-04-14 16:40:30 +00:00
|
|
|
if err != nil {
|
2022-03-29 09:31:23 +00:00
|
|
|
return nil, nil, nil, err
|
2022-04-14 16:40:30 +00:00
|
|
|
}
|
2019-04-04 10:20:45 +00:00
|
|
|
|
2022-03-29 09:31:23 +00:00
|
|
|
address, err := btcutil.NewAddressWitnessScriptHash(
|
2022-04-14 16:40:30 +00:00
|
|
|
pkScript[2:],
|
2019-04-04 10:20:45 +00:00
|
|
|
chainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-03-29 09:31:23 +00:00
|
|
|
return nil, nil, nil, err
|
2022-04-14 16:40:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 09:31:23 +00:00
|
|
|
// Pay to witness script hash (segwit v0) does not need a
|
|
|
|
// sigScript (we provide it in the witness instead), so we
|
|
|
|
// return nil for our sigScript.
|
|
|
|
return address, pkScript, nil, nil
|
2022-04-14 16:40:30 +00:00
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
default:
|
2022-03-29 09:31:23 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unexpected output type: %d",
|
|
|
|
outputType)
|
2019-04-04 10:20:45 +00:00
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
// GenSuccessWitness returns the success script to spend this htlc with
|
|
|
|
// the preimage.
|
|
|
|
func (h *Htlc) GenSuccessWitness(receiverSig []byte,
|
|
|
|
preimage lntypes.Preimage) (wire.TxWitness, error) {
|
|
|
|
|
|
|
|
if h.Hash != preimage.Hash() {
|
|
|
|
return nil, errors.New("preimage doesn't match hash")
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
return h.genSuccessWitness(receiverSig, preimage)
|
2020-07-20 16:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddSuccessToEstimator adds a successful spend to a weight estimator.
|
2022-04-14 16:40:30 +00:00
|
|
|
func (h *Htlc) AddSuccessToEstimator(estimator *input.TxWeightEstimator) error {
|
2020-07-20 16:16:28 +00:00
|
|
|
maxSuccessWitnessSize := h.MaxSuccessWitnessSize()
|
|
|
|
|
|
|
|
switch h.OutputType {
|
2022-04-14 16:40:30 +00:00
|
|
|
case HtlcP2TR:
|
|
|
|
// Generate tapscript.
|
|
|
|
trHtlc, ok := h.HtlcScript.(*HtlcScriptV3)
|
|
|
|
if !ok {
|
|
|
|
return ErrInvalidOutputSelected
|
|
|
|
}
|
2022-03-29 11:17:19 +00:00
|
|
|
successLeaf := txscript.NewBaseTapLeaf(trHtlc.SuccessScript())
|
|
|
|
timeoutLeaf := txscript.NewBaseTapLeaf(trHtlc.TimeoutScript())
|
2022-06-02 16:26:17 +00:00
|
|
|
timeoutLeafHash := timeoutLeaf.TapHash()
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
tapscript := input.TapscriptPartialReveal(
|
2022-06-02 16:26:17 +00:00
|
|
|
trHtlc.InternalPubKey, successLeaf, timeoutLeafHash[:],
|
2022-04-14 16:40:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
estimator.AddTapscriptInput(maxSuccessWitnessSize, tapscript)
|
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
case HtlcP2WSH:
|
|
|
|
estimator.AddWitnessInput(maxSuccessWitnessSize)
|
|
|
|
}
|
2022-04-14 16:40:30 +00:00
|
|
|
|
|
|
|
return nil
|
2020-07-20 16:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTimeoutToEstimator adds a timeout spend to a weight estimator.
|
2022-04-14 16:40:30 +00:00
|
|
|
func (h *Htlc) AddTimeoutToEstimator(estimator *input.TxWeightEstimator) error {
|
2020-07-20 16:16:28 +00:00
|
|
|
maxTimeoutWitnessSize := h.MaxTimeoutWitnessSize()
|
|
|
|
|
|
|
|
switch h.OutputType {
|
2022-04-14 16:40:30 +00:00
|
|
|
case HtlcP2TR:
|
|
|
|
// Generate tapscript.
|
|
|
|
trHtlc, ok := h.HtlcScript.(*HtlcScriptV3)
|
|
|
|
if !ok {
|
|
|
|
return ErrInvalidOutputSelected
|
|
|
|
}
|
2022-03-29 11:17:19 +00:00
|
|
|
successLeaf := txscript.NewBaseTapLeaf(trHtlc.SuccessScript())
|
|
|
|
timeoutLeaf := txscript.NewBaseTapLeaf(trHtlc.TimeoutScript())
|
2022-06-02 16:26:17 +00:00
|
|
|
successLeafHash := successLeaf.TapHash()
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
tapscript := input.TapscriptPartialReveal(
|
2022-06-02 16:26:17 +00:00
|
|
|
trHtlc.InternalPubKey, timeoutLeaf, successLeafHash[:],
|
2022-04-14 16:40:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
estimator.AddTapscriptInput(maxTimeoutWitnessSize, tapscript)
|
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
case HtlcP2WSH:
|
|
|
|
estimator.AddWitnessInput(maxTimeoutWitnessSize)
|
|
|
|
}
|
2022-04-14 16:40:30 +00:00
|
|
|
|
|
|
|
return nil
|
2020-07-20 16:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HtlcScriptV2 encapsulates the htlc v2 script.
|
|
|
|
type HtlcScriptV2 struct {
|
|
|
|
script []byte
|
|
|
|
senderKey [33]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// newHTLCScriptV2 construct an HtlcScipt with the HTLC V2 witness script.
|
|
|
|
//
|
|
|
|
// <receiverHtlcKey> OP_CHECKSIG OP_NOTIF
|
2022-12-15 15:43:38 +00:00
|
|
|
//
|
|
|
|
// OP_DUP OP_HASH160 <HASH160(senderHtlcKey)> OP_EQUALVERIFY OP_CHECKSIGVERIFY
|
|
|
|
// <cltv timeout> OP_CHECKLOCKTIMEVERIFY
|
|
|
|
//
|
2020-07-20 16:16:28 +00:00
|
|
|
// OP_ELSE
|
2022-12-15 15:43:38 +00:00
|
|
|
//
|
|
|
|
// OP_SIZE <20> OP_EQUALVERIFY OP_HASH160 <ripemd(swapHash)> OP_EQUALVERIFY 1
|
|
|
|
// OP_CHECKSEQUENCEVERIFY
|
|
|
|
//
|
2022-05-20 06:57:06 +00:00
|
|
|
// OP_ENDIF .
|
2020-07-20 16:16:28 +00:00
|
|
|
func newHTLCScriptV2(cltvExpiry int32, senderHtlcKey,
|
|
|
|
receiverHtlcKey [33]byte, swapHash lntypes.Hash) (*HtlcScriptV2, error) {
|
|
|
|
|
|
|
|
builder := txscript.NewScriptBuilder()
|
|
|
|
builder.AddData(receiverHtlcKey[:])
|
|
|
|
builder.AddOp(txscript.OP_CHECKSIG)
|
|
|
|
|
|
|
|
builder.AddOp(txscript.OP_NOTIF)
|
|
|
|
|
|
|
|
builder.AddOp(txscript.OP_DUP)
|
|
|
|
builder.AddOp(txscript.OP_HASH160)
|
|
|
|
senderHtlcKeyHash := sha256.Sum256(senderHtlcKey[:])
|
|
|
|
builder.AddData(input.Ripemd160H(senderHtlcKeyHash[:]))
|
|
|
|
|
|
|
|
builder.AddOp(txscript.OP_EQUALVERIFY)
|
|
|
|
builder.AddOp(txscript.OP_CHECKSIGVERIFY)
|
|
|
|
|
|
|
|
builder.AddInt64(int64(cltvExpiry))
|
|
|
|
builder.AddOp(txscript.OP_CHECKLOCKTIMEVERIFY)
|
|
|
|
|
|
|
|
builder.AddOp(txscript.OP_ELSE)
|
|
|
|
|
|
|
|
builder.AddOp(txscript.OP_SIZE)
|
|
|
|
builder.AddInt64(0x20)
|
|
|
|
builder.AddOp(txscript.OP_EQUALVERIFY)
|
|
|
|
builder.AddOp(txscript.OP_HASH160)
|
|
|
|
builder.AddData(input.Ripemd160H(swapHash[:]))
|
|
|
|
builder.AddOp(txscript.OP_EQUALVERIFY)
|
|
|
|
builder.AddOp(txscript.OP_1)
|
|
|
|
|
|
|
|
builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY)
|
|
|
|
|
|
|
|
builder.AddOp(txscript.OP_ENDIF)
|
|
|
|
|
|
|
|
script, err := builder.Script()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &HtlcScriptV2{
|
|
|
|
script: script,
|
|
|
|
senderKey: senderHtlcKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// genSuccessWitness returns the success script to spend this htlc with
|
|
|
|
// the preimage.
|
|
|
|
func (h *HtlcScriptV2) genSuccessWitness(receiverSig []byte,
|
2022-04-14 16:40:30 +00:00
|
|
|
preimage lntypes.Preimage) (wire.TxWitness, error) {
|
2020-07-20 16:16:28 +00:00
|
|
|
|
|
|
|
witnessStack := make(wire.TxWitness, 3)
|
|
|
|
witnessStack[0] = preimage[:]
|
|
|
|
witnessStack[1] = append(receiverSig, byte(txscript.SigHashAll))
|
|
|
|
witnessStack[2] = h.script
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
return witnessStack, nil
|
2020-07-20 16:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsSuccessWitness checks whether the given stack is valid for redeeming the
|
|
|
|
// htlc.
|
|
|
|
func (h *HtlcScriptV2) IsSuccessWitness(witness wire.TxWitness) bool {
|
|
|
|
isTimeoutTx := len(witness) == 4
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
return !isTimeoutTx
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenTimeoutWitness returns the timeout script to spend this htlc after
|
|
|
|
// timeout.
|
2022-04-14 16:40:30 +00:00
|
|
|
func (h *HtlcScriptV2) GenTimeoutWitness(
|
|
|
|
senderSig []byte) (wire.TxWitness, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
witnessStack := make(wire.TxWitness, 4)
|
2019-03-06 20:13:50 +00:00
|
|
|
witnessStack[0] = append(senderSig, byte(txscript.SigHashAll))
|
2020-07-20 16:16:28 +00:00
|
|
|
witnessStack[1] = h.senderKey[:]
|
|
|
|
witnessStack[2] = []byte{}
|
|
|
|
witnessStack[3] = h.script
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
return witnessStack, nil
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2019-04-04 10:20:45 +00:00
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
// TimeoutScript returns the redeem script required to unlock the htlc after
|
|
|
|
// timeout.
|
|
|
|
//
|
|
|
|
// In the case of HtlcScriptV2, this is the full segwit v0 script.
|
|
|
|
func (h *HtlcScriptV2) TimeoutScript() []byte {
|
|
|
|
return h.script
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuccessScript returns the redeem script required to unlock the htlc using
|
|
|
|
// the preimage.
|
|
|
|
//
|
|
|
|
// In the case of HtlcScriptV2, this is the full segwit v0 script.
|
|
|
|
func (h *HtlcScriptV2) SuccessScript() []byte {
|
2020-07-20 16:16:28 +00:00
|
|
|
return h.script
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxSuccessWitnessSize returns maximum success witness size.
|
|
|
|
func (h *HtlcScriptV2) MaxSuccessWitnessSize() int {
|
2019-04-04 10:20:45 +00:00
|
|
|
// Calculate maximum success witness size
|
|
|
|
//
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - receiver_sig_length: 1 byte
|
|
|
|
// - receiver_sig: 73 bytes
|
|
|
|
// - preimage_length: 1 byte
|
2020-07-20 16:16:28 +00:00
|
|
|
// - preimage: 32 bytes
|
2019-04-04 10:20:45 +00:00
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script: len(script) bytes
|
2020-07-20 16:16:28 +00:00
|
|
|
return 1 + 1 + 73 + 1 + 32 + 1 + len(h.script)
|
2019-04-04 10:20:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
// MaxTimeoutWitnessSize returns maximum timeout witness size.
|
|
|
|
func (h *HtlcScriptV2) MaxTimeoutWitnessSize() int {
|
2019-04-04 10:20:45 +00:00
|
|
|
// Calculate maximum timeout witness size
|
|
|
|
//
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sender_sig_length: 1 byte
|
|
|
|
// - sender_sig: 73 bytes
|
2020-07-20 16:16:28 +00:00
|
|
|
// - sender_key_length: 1 byte
|
|
|
|
// - sender_key: 33 bytes
|
2019-04-04 10:20:45 +00:00
|
|
|
// - zero: 1 byte
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script: len(script) bytes
|
2020-07-20 16:16:28 +00:00
|
|
|
return 1 + 1 + 73 + 1 + 33 + 1 + 1 + len(h.script)
|
|
|
|
}
|
2019-04-04 10:20:45 +00:00
|
|
|
|
2020-07-20 16:16:28 +00:00
|
|
|
// SuccessSequence returns the sequence to spend this htlc in the success case.
|
|
|
|
func (h *HtlcScriptV2) SuccessSequence() uint32 {
|
|
|
|
return 1
|
2019-04-04 10:20:45 +00:00
|
|
|
}
|
2022-04-14 16:40:30 +00:00
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
// Sighash is the signature hash to use for transactions spending from the htlc.
|
|
|
|
func (h *HtlcScriptV2) SigHash() txscript.SigHashType {
|
|
|
|
return txscript.SigHashAll
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:31:23 +00:00
|
|
|
// lockingConditions return the address, pkScript and sigScript (if
|
|
|
|
// required) for a htlc script.
|
|
|
|
func (h *HtlcScriptV2) lockingConditions(htlcOutputType HtlcOutputType,
|
|
|
|
params *chaincfg.Params) (btcutil.Address, []byte, []byte, error) {
|
|
|
|
|
|
|
|
return segwitV0LockingConditions(htlcOutputType, params, h.script)
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
// HtlcScriptV3 encapsulates the htlc v3 script.
|
|
|
|
type HtlcScriptV3 struct {
|
2022-03-29 11:17:19 +00:00
|
|
|
// timeoutScript is the final locking script for the timeout path which
|
2022-05-11 14:37:50 +00:00
|
|
|
// is available to the sender after the set blockheight.
|
2022-03-29 11:17:19 +00:00
|
|
|
timeoutScript []byte
|
2022-04-14 16:40:30 +00:00
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
// successScript is the final locking script for the success path in
|
2022-05-11 14:37:50 +00:00
|
|
|
// which the receiver reveals the preimage.
|
2022-03-29 11:17:19 +00:00
|
|
|
successScript []byte
|
2022-04-14 16:40:30 +00:00
|
|
|
|
2022-05-11 14:37:50 +00:00
|
|
|
// InternalPubKey is the public key for the keyspend path which bypasses
|
|
|
|
// the above two locking scripts.
|
2022-04-14 16:40:30 +00:00
|
|
|
InternalPubKey *btcec.PublicKey
|
|
|
|
|
2022-05-11 14:37:50 +00:00
|
|
|
// TaprootKey is the taproot public key which is created with the above
|
|
|
|
// 3 inputs.
|
2022-04-14 16:40:30 +00:00
|
|
|
TaprootKey *btcec.PublicKey
|
2022-05-11 14:37:50 +00:00
|
|
|
|
|
|
|
// RootHash is the root hash of the taptree.
|
|
|
|
RootHash chainhash.Hash
|
2022-04-14 16:40:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 17:49:26 +00:00
|
|
|
// parsePubKey will parse a serialized public key into a btcec.PublicKey
|
|
|
|
// depending on the passed MuSig2 version.
|
|
|
|
func parsePubKey(muSig2Version input.MuSig2Version, key [33]byte) (
|
|
|
|
*btcec.PublicKey, error) {
|
|
|
|
|
|
|
|
// Make sure that we have the correct public keys depending on the
|
|
|
|
// MuSig2 version.
|
|
|
|
switch muSig2Version {
|
|
|
|
case input.MuSig2Version100RC2:
|
|
|
|
return btcec.ParsePubKey(key[:])
|
|
|
|
|
|
|
|
case input.MuSig2Version040:
|
|
|
|
return schnorr.ParsePubKey(key[1:])
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported MuSig2 version: %v",
|
|
|
|
muSig2Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
// newHTLCScriptV3 constructs a HtlcScipt with the HTLC V3 taproot script.
|
2023-01-09 17:49:26 +00:00
|
|
|
func newHTLCScriptV3(muSig2Version input.MuSig2Version, cltvExpiry int32,
|
|
|
|
senderInternalKey, receiverInternalKey, senderHtlcKey,
|
|
|
|
receiverHtlcKey [33]byte, swapHash lntypes.Hash) (*HtlcScriptV3, error) {
|
2022-04-14 16:40:30 +00:00
|
|
|
|
2023-01-09 17:49:26 +00:00
|
|
|
senderPubKey, err := parsePubKey(muSig2Version, senderHtlcKey)
|
2022-04-14 16:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:49:26 +00:00
|
|
|
receiverPubKey, err := parsePubKey(muSig2Version, receiverHtlcKey)
|
2022-04-14 16:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create our success path script, we'll use this separately
|
|
|
|
// to generate the success path leaf.
|
|
|
|
successPathScript, err := GenSuccessPathScript(
|
2022-05-11 14:37:50 +00:00
|
|
|
receiverPubKey, swapHash,
|
2022-04-14 16:40:30 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create our timeout path leaf, we'll use this separately
|
|
|
|
// to generate the timeout path leaf.
|
|
|
|
timeoutPathScript, err := GenTimeoutPathScript(
|
2022-05-11 14:37:50 +00:00
|
|
|
senderPubKey, int64(cltvExpiry),
|
2022-04-14 16:40:30 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble our taproot script tree from our leaves.
|
|
|
|
tree := txscript.AssembleTaprootScriptTree(
|
|
|
|
txscript.NewBaseTapLeaf(successPathScript),
|
|
|
|
txscript.NewBaseTapLeaf(timeoutPathScript),
|
|
|
|
)
|
|
|
|
|
|
|
|
rootHash := tree.RootNode.TapHash()
|
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
// Parse the pub keys used in the internal aggregate key. They are
|
|
|
|
// optional and may just be the same keys that are used for the script
|
|
|
|
// paths.
|
2023-01-09 17:49:26 +00:00
|
|
|
senderInternalPubKey, err := parsePubKey(
|
|
|
|
muSig2Version, senderInternalKey,
|
|
|
|
)
|
2022-11-16 18:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:49:26 +00:00
|
|
|
receiverInternalPubKey, err := parsePubKey(
|
|
|
|
muSig2Version, receiverInternalKey,
|
2022-11-16 18:01:28 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:49:26 +00:00
|
|
|
var aggregateKey *musig2.AggregateKey
|
2022-11-16 18:01:28 +00:00
|
|
|
// Calculate the internal aggregate key.
|
2023-01-09 17:49:26 +00:00
|
|
|
aggregateKey, err = input.MuSig2CombineKeys(
|
|
|
|
muSig2Version,
|
2022-11-16 18:01:28 +00:00
|
|
|
[]*btcec.PublicKey{
|
|
|
|
senderInternalPubKey, receiverInternalPubKey,
|
2023-01-09 17:49:26 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
&input.MuSig2Tweaks{
|
|
|
|
TaprootTweak: rootHash[:],
|
|
|
|
},
|
2022-11-16 18:01:28 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:40:30 +00:00
|
|
|
return &HtlcScriptV3{
|
2022-03-29 11:17:19 +00:00
|
|
|
timeoutScript: timeoutPathScript,
|
|
|
|
successScript: successPathScript,
|
2022-05-11 14:37:50 +00:00
|
|
|
InternalPubKey: aggregateKey.PreTweakedKey,
|
2023-01-09 17:49:26 +00:00
|
|
|
TaprootKey: aggregateKey.FinalKey,
|
2022-05-11 14:37:50 +00:00
|
|
|
RootHash: rootHash,
|
2022-04-14 16:40:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenTimeoutPathScript constructs an HtlcScript for the timeout payment path.
|
|
|
|
// Largest possible bytesize of the script is 32 + 1 + 2 + 1 = 36.
|
|
|
|
//
|
|
|
|
// <senderHtlcKey> OP_CHECKSIGVERIFY <cltvExpiry> OP_CHECKLOCKTIMEVERIFY
|
2022-05-11 14:37:50 +00:00
|
|
|
func GenTimeoutPathScript(senderHtlcKey *btcec.PublicKey, cltvExpiry int64) (
|
|
|
|
[]byte, error) {
|
2022-04-14 16:40:30 +00:00
|
|
|
|
|
|
|
builder := txscript.NewScriptBuilder()
|
2022-05-11 14:37:50 +00:00
|
|
|
builder.AddData(schnorr.SerializePubKey(senderHtlcKey))
|
2022-04-14 16:40:30 +00:00
|
|
|
builder.AddOp(txscript.OP_CHECKSIGVERIFY)
|
|
|
|
builder.AddInt64(cltvExpiry)
|
|
|
|
builder.AddOp(txscript.OP_CHECKLOCKTIMEVERIFY)
|
|
|
|
return builder.Script()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenSuccessPathScript constructs an HtlcScript for the success payment path.
|
|
|
|
// Largest possible bytesize of the script is 32 + 5*1 + 20 + 3*1 = 60.
|
|
|
|
//
|
|
|
|
// <receiverHtlcKey> OP_CHECKSIGVERIFY
|
|
|
|
// OP_SIZE 32 OP_EQUALVERIFY
|
|
|
|
// OP_HASH160 <ripemd160h(swapHash)> OP_EQUALVERIFY
|
|
|
|
// 1 OP_CHECKSEQUENCEVERIFY
|
2022-05-11 14:37:50 +00:00
|
|
|
func GenSuccessPathScript(receiverHtlcKey *btcec.PublicKey,
|
2022-04-14 16:40:30 +00:00
|
|
|
swapHash lntypes.Hash) ([]byte, error) {
|
|
|
|
|
|
|
|
builder := txscript.NewScriptBuilder()
|
|
|
|
|
2022-05-11 14:37:50 +00:00
|
|
|
builder.AddData(schnorr.SerializePubKey(receiverHtlcKey))
|
2022-04-14 16:40:30 +00:00
|
|
|
builder.AddOp(txscript.OP_CHECKSIGVERIFY)
|
|
|
|
builder.AddOp(txscript.OP_SIZE)
|
|
|
|
builder.AddInt64(32)
|
|
|
|
builder.AddOp(txscript.OP_EQUALVERIFY)
|
|
|
|
builder.AddOp(txscript.OP_HASH160)
|
|
|
|
builder.AddData(input.Ripemd160H(swapHash[:]))
|
|
|
|
builder.AddOp(txscript.OP_EQUALVERIFY)
|
|
|
|
builder.AddInt64(1)
|
|
|
|
builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY)
|
|
|
|
|
|
|
|
return builder.Script()
|
|
|
|
}
|
|
|
|
|
|
|
|
// genControlBlock constructs the control block with the depth 1 leaf of the
|
|
|
|
// unused path to compute the proof. For example if spending path a of (root ->
|
|
|
|
// a, root -> b), genControlBlock(b.Script) would be used to create the
|
|
|
|
// controlBlock for a.
|
|
|
|
func (h *HtlcScriptV3) genControlBlock(leafScript []byte) ([]byte, error) {
|
|
|
|
var outputKeyYIsOdd bool
|
|
|
|
|
|
|
|
// Check for odd bit.
|
|
|
|
if h.TaprootKey.SerializeCompressed()[0] == secp.PubKeyFormatCompressedOdd {
|
|
|
|
outputKeyYIsOdd = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate proof with unused script path.
|
|
|
|
leaf := txscript.NewBaseTapLeaf(leafScript)
|
|
|
|
proof := leaf.TapHash()
|
|
|
|
|
|
|
|
controlBlock := txscript.ControlBlock{
|
|
|
|
InternalKey: h.InternalPubKey,
|
|
|
|
OutputKeyYIsOdd: outputKeyYIsOdd,
|
|
|
|
LeafVersion: txscript.BaseLeafVersion,
|
|
|
|
InclusionProof: proof[:],
|
|
|
|
}
|
|
|
|
|
|
|
|
return controlBlock.ToBytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// genSuccessWitness returns the success script to spend this htlc with
|
|
|
|
// the preimage.
|
|
|
|
func (h *HtlcScriptV3) genSuccessWitness(
|
|
|
|
receiverSig []byte, preimage lntypes.Preimage) (wire.TxWitness, error) {
|
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
controlBlockBytes, err := h.genControlBlock(h.timeoutScript)
|
2022-04-14 16:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wire.TxWitness{
|
|
|
|
preimage[:],
|
|
|
|
receiverSig,
|
2022-03-29 11:17:19 +00:00
|
|
|
h.successScript,
|
2022-04-14 16:40:30 +00:00
|
|
|
controlBlockBytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenTimeoutWitness returns the timeout script to spend this htlc after
|
|
|
|
// timeout.
|
|
|
|
func (h *HtlcScriptV3) GenTimeoutWitness(
|
|
|
|
senderSig []byte) (wire.TxWitness, error) {
|
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
controlBlockBytes, err := h.genControlBlock(h.successScript)
|
2022-04-14 16:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wire.TxWitness{
|
|
|
|
senderSig,
|
2022-03-29 11:17:19 +00:00
|
|
|
h.timeoutScript,
|
2022-04-14 16:40:30 +00:00
|
|
|
controlBlockBytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSuccessWitness checks whether the given stack is valid for
|
|
|
|
// redeeming the htlc.
|
|
|
|
func (h *HtlcScriptV3) IsSuccessWitness(witness wire.TxWitness) bool {
|
2022-05-05 09:59:31 +00:00
|
|
|
// The witness has four elements if this is a script spend or one
|
|
|
|
// element if this is a keyspend.
|
|
|
|
return len(witness) == 4 || len(witness) == 1
|
2022-04-14 16:40:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
// TimeoutScript returns the redeem script required to unlock the htlc after
|
|
|
|
// timeout.
|
|
|
|
//
|
|
|
|
// In the case of HtlcScriptV3, this is the timeout tapleaf.
|
|
|
|
func (h *HtlcScriptV3) TimeoutScript() []byte {
|
|
|
|
return h.timeoutScript
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuccessScript returns the redeem script required to unlock the htlc using
|
|
|
|
// the preimage.
|
|
|
|
//
|
|
|
|
// In the case of HtlcScriptV3, this is the claim tapleaf.
|
|
|
|
func (h *HtlcScriptV3) SuccessScript() []byte {
|
|
|
|
return h.successScript
|
2022-04-14 16:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxSuccessWitnessSize returns the maximum witness size for the
|
|
|
|
// success case witness.
|
|
|
|
func (h *HtlcScriptV3) MaxSuccessWitnessSize() int {
|
|
|
|
// Calculate maximum success witness size
|
|
|
|
//
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sigLength: 1 byte
|
|
|
|
// - sig: 64 bytes
|
|
|
|
// - preimage_length: 1 byte
|
|
|
|
// - preimage: 32 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script: 60 bytes
|
|
|
|
// - control_block_length: 1 byte
|
|
|
|
// - control_block: 65 bytes
|
|
|
|
// - leafVersionAndParity: 1
|
|
|
|
// - internalPubkey: 32
|
|
|
|
// - proof: 32
|
|
|
|
return 1 + 1 + 64 + 1 + 32 + 1 + 60 + 1 + 65
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxTimeoutWitnessSize returns the maximum witness size for the
|
|
|
|
// timeout case witness.
|
|
|
|
func (h *HtlcScriptV3) MaxTimeoutWitnessSize() int {
|
|
|
|
// Calculate maximum timeout witness size
|
|
|
|
//
|
|
|
|
// - number_of_witness_elements: 1 byte
|
|
|
|
// - sigLength: 1 byte
|
|
|
|
// - sig: 64 bytes
|
|
|
|
// - witness_script_length: 1 byte
|
|
|
|
// - witness_script: 36 bytes
|
|
|
|
// - control_block_length: 1 byte
|
|
|
|
// - control_block: 65 bytes
|
|
|
|
// - leafVersionAndParity: 1
|
|
|
|
// - internalPubkey: 32
|
|
|
|
// - proof: 32
|
|
|
|
return 1 + 1 + 64 + 1 + 36 + 1 + 65
|
|
|
|
}
|
|
|
|
|
|
|
|
// SuccessSequence returns the sequence to spend this htlc in the
|
|
|
|
// success case.
|
|
|
|
func (h *HtlcScriptV3) SuccessSequence() uint32 {
|
|
|
|
return 1
|
|
|
|
}
|
2022-03-29 09:31:23 +00:00
|
|
|
|
2022-03-29 11:17:19 +00:00
|
|
|
// Sighash is the signature hash to use for transactions spending from the htlc.
|
|
|
|
func (h *HtlcScriptV3) SigHash() txscript.SigHashType {
|
|
|
|
return txscript.SigHashDefault
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:31:23 +00:00
|
|
|
// lockingConditions return the address, pkScript and sigScript (if required)
|
|
|
|
// for a htlc script.
|
|
|
|
func (h *HtlcScriptV3) lockingConditions(outputType HtlcOutputType,
|
|
|
|
chainParams *chaincfg.Params) (btcutil.Address, []byte, []byte, error) {
|
|
|
|
|
|
|
|
// HtlcV3 can only have taproot output type, because we utilize
|
|
|
|
// tapscript claim paths.
|
|
|
|
if outputType != HtlcP2TR {
|
|
|
|
return nil, nil, nil, fmt.Errorf("htlc v3 only supports P2TR "+
|
|
|
|
"outputs, got: %v", outputType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a tapscript address from our tree.
|
|
|
|
address, err := btcutil.NewAddressTaproot(
|
|
|
|
schnorr.SerializePubKey(h.TaprootKey), chainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate locking script.
|
|
|
|
pkScript, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Taproot (segwit v1) does not need a sigScript (we provide it in the
|
|
|
|
// witness instead), so we return nil for our sigScript.
|
|
|
|
return address, pkScript, nil, nil
|
|
|
|
}
|