2019-03-06 20:13:50 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-06-05 17:33:08 +00:00
|
|
|
"time"
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2020-06-05 17:33:08 +00:00
|
|
|
"github.com/btcsuite/btcwallet/wtxmgr"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2020-06-05 17:33:08 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-11-16 00:16:30 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockWalletKit struct {
|
2019-10-01 15:21:17 +00:00
|
|
|
lnd *LndMockServices
|
|
|
|
keyIndex int32
|
2019-11-16 00:16:30 +00:00
|
|
|
feeEstimates map[int32]chainfee.SatPerKWeight
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 15:21:17 +00:00
|
|
|
var _ lndclient.WalletKitClient = (*mockWalletKit)(nil)
|
|
|
|
|
2020-06-05 17:33:08 +00:00
|
|
|
func (m *mockWalletKit) ListUnspent(ctx context.Context, minConfs,
|
|
|
|
maxConfs int32) ([]*lnwallet.Utxo, error) {
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWalletKit) LeaseOutput(ctx context.Context, lockID wtxmgr.LockID,
|
|
|
|
op wire.OutPoint) (time.Time, error) {
|
|
|
|
|
|
|
|
return time.Now(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWalletKit) ReleaseOutput(ctx context.Context,
|
|
|
|
lockID wtxmgr.LockID, op wire.OutPoint) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
func (m *mockWalletKit) DeriveNextKey(ctx context.Context, family int32) (
|
|
|
|
*keychain.KeyDescriptor, error) {
|
|
|
|
|
|
|
|
index := m.keyIndex
|
|
|
|
|
|
|
|
_, pubKey := CreateKey(index)
|
|
|
|
m.keyIndex++
|
|
|
|
|
|
|
|
return &keychain.KeyDescriptor{
|
|
|
|
KeyLocator: keychain.KeyLocator{
|
|
|
|
Family: keychain.KeyFamily(family),
|
|
|
|
Index: uint32(index),
|
|
|
|
},
|
|
|
|
PubKey: pubKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWalletKit) DeriveKey(ctx context.Context, in *keychain.KeyLocator) (
|
|
|
|
*keychain.KeyDescriptor, error) {
|
|
|
|
|
|
|
|
_, pubKey := CreateKey(int32(in.Index))
|
|
|
|
|
|
|
|
return &keychain.KeyDescriptor{
|
|
|
|
KeyLocator: *in,
|
|
|
|
PubKey: pubKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWalletKit) NextAddr(ctx context.Context) (btcutil.Address, error) {
|
|
|
|
addr, err := btcutil.NewAddressWitnessPubKeyHash(
|
|
|
|
make([]byte, 20), &chaincfg.TestNet3Params,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 07:15:25 +00:00
|
|
|
func (m *mockWalletKit) PublishTransaction(ctx context.Context, tx *wire.MsgTx,
|
|
|
|
_ string) error {
|
|
|
|
|
2019-12-10 00:16:41 +00:00
|
|
|
m.lnd.AddTx(tx)
|
2019-03-06 20:13:50 +00:00
|
|
|
m.lnd.TxPublishChannel <- tx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWalletKit) SendOutputs(ctx context.Context, outputs []*wire.TxOut,
|
2020-10-15 07:15:25 +00:00
|
|
|
feeRate chainfee.SatPerKWeight, _ string) (*wire.MsgTx, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
var inputTxHash chainhash.Hash
|
|
|
|
|
|
|
|
tx := wire.MsgTx{}
|
|
|
|
tx.AddTxIn(&wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
|
|
|
Hash: inputTxHash,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, out := range outputs {
|
|
|
|
tx.AddTxOut(&wire.TxOut{
|
|
|
|
PkScript: out.PkScript,
|
|
|
|
Value: out.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:16:41 +00:00
|
|
|
m.lnd.AddTx(&tx)
|
2019-03-06 20:13:50 +00:00
|
|
|
m.lnd.SendOutputsChannel <- tx
|
|
|
|
|
|
|
|
return &tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockWalletKit) EstimateFee(ctx context.Context, confTarget int32) (
|
2019-11-16 00:16:30 +00:00
|
|
|
chainfee.SatPerKWeight, error) {
|
2019-10-01 15:21:17 +00:00
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
if confTarget <= 1 {
|
|
|
|
return 0, errors.New("conf target must be greater than 1")
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:21:17 +00:00
|
|
|
feeEstimate, ok := m.feeEstimates[confTarget]
|
|
|
|
if !ok {
|
|
|
|
return 10000, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return feeEstimate, nil
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-06-17 06:44:44 +00:00
|
|
|
|
|
|
|
// ListSweeps returns a list of the sweep transaction ids known to our node.
|
|
|
|
func (m *mockWalletKit) ListSweeps(_ context.Context) ([]string, error) {
|
|
|
|
return m.lnd.Sweeps, nil
|
|
|
|
}
|
2020-10-15 07:15:25 +00:00
|
|
|
|
|
|
|
// BumpFee attempts to bump the fee of a transaction by spending one of
|
|
|
|
// its outputs at the given fee rate. This essentially results in a
|
|
|
|
// child-pays-for-parent (CPFP) scenario. If the given output has been
|
|
|
|
// used in a previous BumpFee call, then a transaction replacing the
|
|
|
|
// previous is broadcast, resulting in a replace-by-fee (RBF) scenario.
|
|
|
|
func (m *mockWalletKit) BumpFee(context.Context, wire.OutPoint,
|
|
|
|
chainfee.SatPerKWeight) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|