lnwallet: export ValidateUpfrontShutdown and restrict allowed addrs

In this commit, we catch up our logic with the latest version of the
spec that removed support for normal p2kh and p2sh addresses for co-op
closes, in order to make dust calculations more uniform.
This commit is contained in:
Olaoluwa Osuntokun 2022-06-10 11:16:02 -07:00
parent 5f9c1b902f
commit c79ffc07ce
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306

View File

@ -1329,7 +1329,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
shutdown := req.contribution.UpfrontShutdown shutdown := req.contribution.UpfrontShutdown
if len(shutdown) > 0 { if len(shutdown) > 0 {
// Validate the shutdown script. // Validate the shutdown script.
if !validateUpfrontShutdown(shutdown, &l.Cfg.NetParams) { if !ValidateUpfrontShutdown(shutdown, &l.Cfg.NetParams) {
req.err <- fmt.Errorf("invalid shutdown script") req.err <- fmt.Errorf("invalid shutdown script")
return return
} }
@ -1660,7 +1660,7 @@ func (l *LightningWallet) handleSingleContribution(req *addSingleContributionMsg
shutdown := req.contribution.UpfrontShutdown shutdown := req.contribution.UpfrontShutdown
if len(shutdown) > 0 { if len(shutdown) > 0 {
// Validate the shutdown script. // Validate the shutdown script.
if !validateUpfrontShutdown(shutdown, &l.Cfg.NetParams) { if !ValidateUpfrontShutdown(shutdown, &l.Cfg.NetParams) {
req.err <- fmt.Errorf("invalid shutdown script") req.err <- fmt.Errorf("invalid shutdown script")
return return
} }
@ -2087,8 +2087,8 @@ func (l *LightningWallet) WithCoinSelectLock(f func() error) error {
// state hints from the root to be used for a new channel. The obfuscator is // state hints from the root to be used for a new channel. The obfuscator is
// generated via the following computation: // generated via the following computation:
// //
// * sha256(initiatorKey || responderKey)[26:] // - sha256(initiatorKey || responderKey)[26:]
// * where both keys are the multi-sig keys of the respective parties // -- where both keys are the multi-sig keys of the respective parties
// //
// The first 6 bytes of the resulting hash are used as the state hint. // The first 6 bytes of the resulting hash are used as the state hint.
func DeriveStateHintObfuscator(key1, key2 *btcec.PublicKey) [StateHintSize]byte { func DeriveStateHintObfuscator(key1, key2 *btcec.PublicKey) [StateHintSize]byte {
@ -2254,24 +2254,37 @@ func (s *shimKeyRing) DeriveNextKey(keyFam keychain.KeyFamily) (keychain.KeyDesc
return *fundingKeys.LocalKey, nil return *fundingKeys.LocalKey, nil
} }
// validateUpfrontShutdown checks whether the provided upfront_shutdown_script // ValidateUpfrontShutdown checks whether the provided upfront_shutdown_script
// is of a valid type that we accept. // is of a valid type that we accept.
func validateUpfrontShutdown(shutdown lnwire.DeliveryAddress, func ValidateUpfrontShutdown(shutdown lnwire.DeliveryAddress,
params *chaincfg.Params) bool { params *chaincfg.Params) bool {
// We don't need to worry about a large UpfrontShutdownScript since it // We don't need to worry about a large UpfrontShutdownScript since it
// was already checked in lnwire when decoding from the wire. // was already checked in lnwire when decoding from the wire.
scriptClass, _, _, _ := txscript.ExtractPkScriptAddrs(shutdown, params) scriptClass, _, _, _ := txscript.ExtractPkScriptAddrs(shutdown, params)
switch scriptClass { switch {
case txscript.PubKeyHashTy, case scriptClass == txscript.WitnessV0PubKeyHashTy,
txscript.WitnessV0PubKeyHashTy, scriptClass == txscript.WitnessV0ScriptHashTy,
txscript.ScriptHashTy, scriptClass == txscript.WitnessV1TaprootTy:
txscript.WitnessV0ScriptHashTy:
// The above four types are permitted according to BOLT#02. // The above three types are permitted according to BOLT#02 and
// Everything else is disallowed. // BOLT#05. Everything else is disallowed.
return true return true
// In this case, we don't know about the actual script template, but it
// might be a witness program with versions 2-16. So we'll check that
// now
case txscript.IsWitnessProgram(shutdown):
version, _, err := txscript.ExtractWitnessProgramInfo(shutdown)
if err != nil {
walletLog.Warnf("unable to extract witness program "+
"version (script=%x): %v", shutdown, err)
return false
}
return version >= 1 && version <= 16
default: default:
return false return false
} }