|
| 1 | +package scbforceclose |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/btcsuite/btcd/btcec/v2" |
| 8 | + "github.com/btcsuite/btcd/btcutil" |
| 9 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 10 | + "github.com/btcsuite/btcd/txscript" |
| 11 | + "github.com/btcsuite/btcd/wire" |
| 12 | + "github.com/lightningnetwork/lnd/chanbackup" |
| 13 | + "github.com/lightningnetwork/lnd/channeldb" |
| 14 | + "github.com/lightningnetwork/lnd/fn/v2" |
| 15 | + "github.com/lightningnetwork/lnd/input" |
| 16 | + "github.com/lightningnetwork/lnd/keychain" |
| 17 | + "github.com/lightningnetwork/lnd/lnwallet" |
| 18 | + "github.com/lightningnetwork/lnd/shachain" |
| 19 | +) |
| 20 | + |
| 21 | +// SignCloseTx produces a signed commit tx from a channel backup. |
| 22 | +func SignCloseTx(s chanbackup.Single, keyRing keychain.KeyRing, |
| 23 | + ecdher keychain.ECDHRing, signer input.Signer) (*wire.MsgTx, error) { |
| 24 | + |
| 25 | + var errNoInputs = errors.New("channel backup does not have data " + |
| 26 | + "needed to sign force close tx") |
| 27 | + |
| 28 | + closeTxInputs, err := s.CloseTxInputs.UnwrapOrErr(errNoInputs) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + // Each of the keys in our local channel config only have their |
| 34 | + // locators populated, so we'll re-derive the raw key now. |
| 35 | + localMultiSigKey, err := keyRing.DeriveKey( |
| 36 | + s.LocalChanCfg.MultiSigKey.KeyLocator, |
| 37 | + ) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("unable to derive multisig key: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + // Determine the value of tapscriptRoot option. |
| 43 | + tapscriptRootOpt := fn.None[chainhash.Hash]() |
| 44 | + if s.Version.HasTapscriptRoot() { |
| 45 | + tapscriptRootOpt = closeTxInputs.TapscriptRoot |
| 46 | + } |
| 47 | + |
| 48 | + // Create signature descriptor. |
| 49 | + signDesc, err := createSignDesc( |
| 50 | + localMultiSigKey, s.RemoteChanCfg.MultiSigKey.PubKey, |
| 51 | + s.Version, s.Capacity, tapscriptRootOpt, |
| 52 | + ) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("failed to create signDesc: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + // Build inputs for GetSignedCommitTx. |
| 58 | + inputs := lnwallet.SignedCommitTxInputs{ |
| 59 | + CommitTx: closeTxInputs.CommitTx, |
| 60 | + CommitSig: closeTxInputs.CommitSig, |
| 61 | + OurKey: localMultiSigKey, |
| 62 | + TheirKey: s.RemoteChanCfg.MultiSigKey, |
| 63 | + SignDesc: signDesc, |
| 64 | + } |
| 65 | + |
| 66 | + // Add special fields in case of a taproot channel. |
| 67 | + if s.Version.IsTaproot() { |
| 68 | + producer, err := createTaprootNonceProducer( |
| 69 | + s.ShaChainRootDesc, localMultiSigKey.PubKey, ecdher, |
| 70 | + ) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + inputs.Taproot = fn.Some(lnwallet.TaprootSignedCommitTxInputs{ |
| 75 | + CommitHeight: closeTxInputs.CommitHeight, |
| 76 | + TaprootNonceProducer: producer, |
| 77 | + TapscriptRoot: tapscriptRootOpt, |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + return lnwallet.GetSignedCommitTx(inputs, signer) |
| 82 | +} |
| 83 | + |
| 84 | +// createSignDesc creates SignDescriptor from local and remote keys, |
| 85 | +// backup version and capacity. |
| 86 | +// See LightningChannel.createSignDesc on how signDesc is produced. |
| 87 | +func createSignDesc(localMultiSigKey keychain.KeyDescriptor, |
| 88 | + remoteKey *btcec.PublicKey, version chanbackup.SingleBackupVersion, |
| 89 | + capacity btcutil.Amount, tapscriptRoot fn.Option[chainhash.Hash]) ( |
| 90 | + *input.SignDescriptor, error) { |
| 91 | + |
| 92 | + var fundingPkScript, multiSigScript []byte |
| 93 | + |
| 94 | + localKey := localMultiSigKey.PubKey |
| 95 | + |
| 96 | + var err error |
| 97 | + if version.IsTaproot() { |
| 98 | + fundingPkScript, _, err = input.GenTaprootFundingScript( |
| 99 | + localKey, remoteKey, int64(capacity), tapscriptRoot, |
| 100 | + ) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + } else { |
| 105 | + multiSigScript, err = input.GenMultiSigScript( |
| 106 | + localKey.SerializeCompressed(), |
| 107 | + remoteKey.SerializeCompressed(), |
| 108 | + ) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + fundingPkScript, err = input.WitnessScriptHash(multiSigScript) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return &input.SignDescriptor{ |
| 120 | + KeyDesc: localMultiSigKey, |
| 121 | + WitnessScript: multiSigScript, |
| 122 | + Output: &wire.TxOut{ |
| 123 | + PkScript: fundingPkScript, |
| 124 | + Value: int64(capacity), |
| 125 | + }, |
| 126 | + HashType: txscript.SigHashAll, |
| 127 | + PrevOutputFetcher: txscript.NewCannedPrevOutputFetcher( |
| 128 | + fundingPkScript, int64(capacity), |
| 129 | + ), |
| 130 | + InputIndex: 0, |
| 131 | + }, nil |
| 132 | +} |
| 133 | + |
| 134 | +// createTaprootNonceProducer makes taproot nonce producer from a |
| 135 | +// ShaChainRootDesc and our public multisig key. |
| 136 | +func createTaprootNonceProducer(shaChainRootDesc keychain.KeyDescriptor, |
| 137 | + localKey *btcec.PublicKey, ecdher keychain.ECDHRing) (shachain.Producer, |
| 138 | + error) { |
| 139 | + |
| 140 | + if shaChainRootDesc.PubKey != nil { |
| 141 | + return nil, errors.New("taproot channels always use ECDH, " + |
| 142 | + "but legacy ShaChainRootDesc with pubkey found") |
| 143 | + } |
| 144 | + |
| 145 | + // This is the scheme in which the shachain root is derived via an ECDH |
| 146 | + // operation on the private key of ShaChainRootDesc and our public |
| 147 | + // multisig key. |
| 148 | + ecdh, err := ecdher.ECDH(shaChainRootDesc, localKey) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("ecdh failed: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + // The shachain root that seeds RevocationProducer for this channel. |
| 154 | + revRoot := chainhash.Hash(ecdh) |
| 155 | + |
| 156 | + revocationProducer := shachain.NewRevocationProducer(revRoot) |
| 157 | + |
| 158 | + return channeldb.DeriveMusig2Shachain(revocationProducer) |
| 159 | +} |
0 commit comments