lnwallet+lntest: add FetchOutpointInfo and FetchDerivationInfo

This commit is contained in:
yyforyongyu
2024-08-08 15:01:53 +08:00
parent 8f35612364
commit 9801ee036b
4 changed files with 104 additions and 3 deletions

View File

@@ -58,6 +58,21 @@ func (w *WalletController) FetchInputInfo(
return utxo, nil return utxo, nil
} }
// FetchOutpointInfo will be called to get info about the inputs to the funding
// transaction.
func (w *WalletController) FetchOutpointInfo(
prevOut *wire.OutPoint) (*lnwallet.Utxo, error) {
utxo := &lnwallet.Utxo{
AddressType: lnwallet.WitnessPubKey,
Value: 10 * btcutil.SatoshiPerBitcoin,
PkScript: []byte("dummy"),
Confirmations: 1,
OutPoint: *prevOut,
}
return utxo, nil
}
// ScriptForOutput returns the address, witness program and redeem script for a // ScriptForOutput returns the address, witness program and redeem script for a
// given UTXO. An error is returned if the UTXO does not belong to our wallet or // given UTXO. An error is returned if the UTXO does not belong to our wallet or
// it is not a managed pubKey address. // it is not a managed pubKey address.
@@ -292,3 +307,11 @@ func (w *WalletController) RemoveDescendants(*wire.MsgTx) error {
func (w *WalletController) CheckMempoolAcceptance(tx *wire.MsgTx) error { func (w *WalletController) CheckMempoolAcceptance(tx *wire.MsgTx) error {
return nil return nil
} }
// FetchDerivationInfo queries for the wallet's knowledge of the passed
// pkScript and constructs the derivation info and returns it.
func (w *WalletController) FetchDerivationInfo(
pkScript []byte) (*psbt.Bip32Derivation, error) {
return nil, nil
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/btcsuite/btcd/btcec/v2/schnorr" "github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/hdkeychain" "github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@@ -56,6 +57,49 @@ func (b *BtcWallet) FetchInputInfo(prevOut *wire.OutPoint) (*lnwallet.Utxo,
}, nil }, nil
} }
// FetchOutpointInfo queries for the WalletController's knowledge of the passed
// outpoint. If the base wallet determines this output is under its control,
// then the original txout should be returned. Otherwise, a non-nil error value
// of ErrNotMine should be returned instead.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) FetchOutpointInfo(prevOut *wire.OutPoint) (*lnwallet.Utxo,
error) {
prevTx, txOut, confirmations, err := b.wallet.FetchOutpointInfo(prevOut)
if err != nil {
return nil, err
}
// Then, we'll populate all of the information required by the struct.
addressType := lnwallet.UnknownAddressType
switch {
case txscript.IsPayToWitnessPubKeyHash(txOut.PkScript):
addressType = lnwallet.WitnessPubKey
case txscript.IsPayToScriptHash(txOut.PkScript):
addressType = lnwallet.NestedWitnessPubKey
case txscript.IsPayToTaproot(txOut.PkScript):
addressType = lnwallet.TaprootPubkey
}
return &lnwallet.Utxo{
AddressType: addressType,
Value: btcutil.Amount(txOut.Value),
PkScript: txOut.PkScript,
Confirmations: confirmations,
OutPoint: *prevOut,
PrevTx: prevTx,
}, nil
}
// FetchDerivationInfo queries for the wallet's knowledge of the passed
// pkScript and constructs the derivation info and returns it.
func (b *BtcWallet) FetchDerivationInfo(
pkScript []byte) (*psbt.Bip32Derivation, error) {
return b.wallet.FetchDerivationInfo(pkScript)
}
// ScriptForOutput returns the address, witness program and redeem script for a // ScriptForOutput returns the address, witness program and redeem script for a
// given UTXO. An error is returned if the UTXO does not belong to our wallet or // given UTXO. An error is returned if the UTXO does not belong to our wallet or
// it is not a managed pubKey address. // it is not a managed pubKey address.

View File

@@ -228,11 +228,21 @@ type TransactionSubscription interface {
// across all concrete implementations. // across all concrete implementations.
type WalletController interface { type WalletController interface {
// FetchInputInfo queries for the WalletController's knowledge of the // FetchInputInfo queries for the WalletController's knowledge of the
// passed outpoint. If the base wallet determines this output is under // passed outpoint. It returns the same info as `FetchOutpointInfo`
// its control, then the original txout should be returned. Otherwise, // plus the Bip32Derivation info.
// a non-nil error value of ErrNotMine should be returned instead.
FetchInputInfo(prevOut *wire.OutPoint) (*Utxo, error) FetchInputInfo(prevOut *wire.OutPoint) (*Utxo, error)
// FetchOutpointInfo queries for the WalletController's knowledge of
// the passed outpoint. If the base wallet determines this output is
// under its control, then the original txout should be returned.
// Otherwise, a non-nil error value of ErrNotMine should be returned
// instead.
FetchOutpointInfo(prevOut *wire.OutPoint) (*Utxo, error)
// FetchDerivationInfo queries for the wallet's knowledge of the passed
// pkScript and constructs the derivation info and returns it.
FetchDerivationInfo(pkScript []byte) (*psbt.Bip32Derivation, error)
// ScriptForOutput returns the address, witness program and redeem // ScriptForOutput returns the address, witness program and redeem
// script for a given UTXO. An error is returned if the UTXO does not // script for a given UTXO. An error is returned if the UTXO does not
// belong to our wallet or it is not a managed pubKey address. // belong to our wallet or it is not a managed pubKey address.

View File

@@ -61,6 +61,22 @@ func (w *mockWalletController) FetchInputInfo(
return utxo, nil return utxo, nil
} }
// FetchOutpointInfo will be called to get info about the inputs to the funding
// transaction.
func (w *mockWalletController) FetchOutpointInfo(
prevOut *wire.OutPoint) (*Utxo, error) {
utxo := &Utxo{
AddressType: WitnessPubKey,
Value: 10 * btcutil.SatoshiPerBitcoin,
PkScript: []byte("dummy"),
Confirmations: 1,
OutPoint: *prevOut,
}
return utxo, nil
}
// ScriptForOutput returns the address, witness program and redeem script for a // ScriptForOutput returns the address, witness program and redeem script for a
// given UTXO. An error is returned if the UTXO does not belong to our wallet or // given UTXO. An error is returned if the UTXO does not belong to our wallet or
// it is not a managed pubKey address. // it is not a managed pubKey address.
@@ -300,6 +316,14 @@ func (w *mockWalletController) RemoveDescendants(*wire.MsgTx) error {
return nil return nil
} }
// FetchDerivationInfo queries for the wallet's knowledge of the passed
// pkScript and constructs the derivation info and returns it.
func (w *mockWalletController) FetchDerivationInfo(
pkScript []byte) (*psbt.Bip32Derivation, error) {
return nil, nil
}
func (w *mockWalletController) CheckMempoolAcceptance(tx *wire.MsgTx) error { func (w *mockWalletController) CheckMempoolAcceptance(tx *wire.MsgTx) error {
return nil return nil
} }