lnwallet: update CommitScriptToSelf to add taproot awareness

This commit is contained in:
Olaoluwa Osuntokun committed 2023-08-22 16:30:57 -07:00
1 parent f7ba08e147
commit 0e7e29455a
1 file changed
+65 -25
+65 -25
View File
@@ -202,36 +202,76 @@ func CommitScriptToSelf(chanType channeldb.ChannelType, initiator bool,
selfKey, revokeKey *btcec.PublicKey, csvDelay, leaseExpiry uint32) ( selfKey, revokeKey *btcec.PublicKey, csvDelay, leaseExpiry uint32) (
*ScriptInfo, error) { *ScriptInfo, error) {
var (
toLocalRedeemScript []byte
err error
)
switch { switch {
// If we are the initiator of a leased channel, then we have an // For taproot scripts, we'll need to make a slightly modified script
// additional CLTV requirement in addition to the usual CSV requirement. // where a NUMS key is used to force a script path reveal of either the
case initiator && chanType.HasLeaseExpiration(): // revocation or the CSV timeout.
toLocalRedeemScript, err = input.LeaseCommitScriptToSelf( //
selfKey, revokeKey, csvDelay, leaseExpiry, // Our "redeem" script here is just the taproot witness program.
) case chanType.IsTaproot():
toLocalOutputKey, err := input.TaprootCommitScriptToSelf(
default:
toLocalRedeemScript, err = input.CommitScriptToSelf(
csvDelay, selfKey, revokeKey, csvDelay, selfKey, revokeKey,
) )
} if err != nil {
if err != nil { return nil, fmt.Errorf("unable to generate taproot "+
return nil, err "key: %w", err)
} }
toLocalScriptHash, err := input.WitnessScriptHash(toLocalRedeemScript) toLocalPkScript, err := input.PayToTaprootScript(
if err != nil { toLocalOutputKey,
return nil, err )
} if err != nil {
return nil, fmt.Errorf("unable to gen taproot "+
"pkscript: %w", err)
}
return &ScriptInfo{ return &ScriptInfo{
PkScript: toLocalScriptHash, PkScript: toLocalPkScript,
WitnessScript: toLocalRedeemScript, }, nil
}, nil
// If we are the initiator of a leased channel, then we have an
// additional CLTV requirement in addition to the usual CSV
// requirement.
case initiator && chanType.HasLeaseExpiration():
toLocalRedeemScript, err := input.LeaseCommitScriptToSelf(
selfKey, revokeKey, csvDelay, leaseExpiry,
)
if err != nil {
return nil, err
}
toLocalScriptHash, err := input.WitnessScriptHash(
toLocalRedeemScript,
)
if err != nil {
return nil, err
}
return &ScriptInfo{
PkScript: toLocalScriptHash,
WitnessScript: toLocalRedeemScript,
}, nil
default:
toLocalRedeemScript, err := input.CommitScriptToSelf(
csvDelay, selfKey, revokeKey,
)
if err != nil {
return nil, err
}
toLocalScriptHash, err := input.WitnessScriptHash(
toLocalRedeemScript,
)
if err != nil {
return nil, err
}
return &ScriptInfo{
PkScript: toLocalScriptHash,
WitnessScript: toLocalRedeemScript,
}, nil
}
} }
// CommitScriptToRemote derives the appropriate to_remote script based on the // CommitScriptToRemote derives the appropriate to_remote script based on the