multi: add p2tr keyspend wallet support
This commit is contained in:
24 files changed
+416
-163
No files matched your search
@@ -355,7 +355,7 @@ func assertTxFeeRate(t *testing.T, tx *wire.MsgTx,
|
||||
outputAmt := tx.TxOut[0].Value
|
||||
|
||||
fee := btcutil.Amount(inputAmt - outputAmt)
|
||||
_, estimator := getWeightEstimate(inputs, nil, 0)
|
||||
_, estimator := getWeightEstimate(inputs, nil, 0, nil)
|
||||
txWeight := estimator.weight()
|
||||
|
||||
expectedFee := expectedFeeRate.FeeForWeight(int64(txWeight))
|
||||
|
||||
+14
-7
@@ -139,7 +139,9 @@ func createSweepTx(inputs []input.Input, outputs []*wire.TxOut,
|
||||
feePerKw chainfee.SatPerKWeight, signer input.Signer) (*wire.MsgTx,
|
||||
error) {
|
||||
|
||||
inputs, estimator := getWeightEstimate(inputs, outputs, feePerKw)
|
||||
inputs, estimator := getWeightEstimate(
|
||||
inputs, outputs, feePerKw, changePkScript,
|
||||
)
|
||||
txFee := estimator.fee()
|
||||
|
||||
var (
|
||||
@@ -312,7 +314,8 @@ func createSweepTx(inputs []input.Input, outputs []*wire.TxOut,
|
||||
// getWeightEstimate returns a weight estimate for the given inputs.
|
||||
// Additionally, it returns counts for the number of csv and cltv inputs.
|
||||
func getWeightEstimate(inputs []input.Input, outputs []*wire.TxOut,
|
||||
feeRate chainfee.SatPerKWeight) ([]input.Input, *weightEstimator) {
|
||||
feeRate chainfee.SatPerKWeight, outputPkScript []byte) ([]input.Input,
|
||||
*weightEstimator) {
|
||||
|
||||
// We initialize a weight estimator so we can accurately asses the
|
||||
// amount of fees we need to pay for this sweep transaction.
|
||||
@@ -327,14 +330,18 @@ func getWeightEstimate(inputs []input.Input, outputs []*wire.TxOut,
|
||||
}
|
||||
|
||||
// If there is any leftover change after paying to the given outputs
|
||||
// and required outputs, it will go to a single segwit p2wkh address.
|
||||
// This will be our change address, so ensure it contributes to our
|
||||
// weight estimate. Note that if we have other outputs, we might end up
|
||||
// creating a sweep tx without a change output. It is okay to add the
|
||||
// and required outputs, it will go to a single segwit p2wkh or p2tr
|
||||
// address. This will be our change address, so ensure it contributes to
|
||||
// our weight estimate. Note that if we have other outputs, we might end
|
||||
// up creating a sweep tx without a change output. It is okay to add the
|
||||
// change output to the weight estimate regardless, since the estimated
|
||||
// fee will just be subtracted from this already dust output, and
|
||||
// trimmed.
|
||||
weightEstimate.addP2WKHOutput()
|
||||
if txscript.IsPayToTaproot(outputPkScript) {
|
||||
weightEstimate.addP2TROutput()
|
||||
} else {
|
||||
weightEstimate.addP2WKHOutput()
|
||||
}
|
||||
|
||||
// For each output, use its witness type to determine the estimate
|
||||
// weight of its witness, and add it to the proper set of spendable
|
||||
|
||||
@@ -39,7 +39,7 @@ func TestWeightEstimate(t *testing.T) {
|
||||
))
|
||||
}
|
||||
|
||||
_, estimator := getWeightEstimate(inputs, nil, 0)
|
||||
_, estimator := getWeightEstimate(inputs, nil, 0, nil)
|
||||
weight := int64(estimator.weight())
|
||||
if weight != expectedWeight {
|
||||
t.Fatalf("unexpected weight. expected %d but got %d.",
|
||||
|
||||
@@ -255,6 +255,10 @@ func CraftSweepAllTx(feeRate chainfee.SatPerKWeight, blockHeight uint32,
|
||||
case lnwallet.NestedWitnessPubKey:
|
||||
witnessType = input.NestedWitnessKeyHash
|
||||
|
||||
case lnwallet.TaprootPubkey:
|
||||
witnessType = input.TaprootPubKeySpend
|
||||
signDesc.HashType = txscript.SigHashDefault
|
||||
|
||||
// All other output types we count as unknown and will fail to
|
||||
// sweep.
|
||||
default:
|
||||
|
||||
@@ -77,6 +77,12 @@ func (w *weightEstimator) addP2WKHOutput() {
|
||||
w.estimator.AddP2WKHOutput()
|
||||
}
|
||||
|
||||
// addP2TROutput updates the weight estimate to account for an additional native
|
||||
// SegWit v1 P2TR output.
|
||||
func (w *weightEstimator) addP2TROutput() {
|
||||
w.estimator.AddP2TROutput()
|
||||
}
|
||||
|
||||
// addOutput updates the weight estimate to account for the known
|
||||
// output given.
|
||||
func (w *weightEstimator) addOutput(txOut *wire.TxOut) {
|
||||
|
||||
Reference in new issue
Block a user