github.com/decred/dcrlnd@v0.7.6/lntest/mock/walletcontroller.go (about)

     1  package mock
     2  
     3  import (
     4  	"encoding/hex"
     5  	"sync/atomic"
     6  	"time"
     7  
     8  	"decred.org/dcrwallet/v4/wallet"
     9  	"decred.org/dcrwallet/v4/wallet/txauthor"
    10  	"github.com/decred/dcrd/chaincfg/chainhash"
    11  	"github.com/decred/dcrd/chaincfg/v3"
    12  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
    13  	"github.com/decred/dcrd/dcrutil/v4"
    14  	"github.com/decred/dcrd/hdkeychain/v3"
    15  	"github.com/decred/dcrd/txscript/v4/stdaddr"
    16  	"github.com/decred/dcrd/wire"
    17  
    18  	"github.com/decred/dcrlnd/btcwalletcompat"
    19  	"github.com/decred/dcrlnd/internal/psbt"
    20  	"github.com/decred/dcrlnd/lnwallet"
    21  	"github.com/decred/dcrlnd/lnwallet/chainfee"
    22  )
    23  
    24  var (
    25  	CoinPkScript, _ = hex.DecodeString("76a914000000000000000000000000000000000000000088ac")
    26  )
    27  
    28  // WalletController is a mock implementation of the WalletController
    29  // interface. It let's us mock the interaction with the bitcoin network.
    30  type WalletController struct {
    31  	RootKey               *secp256k1.PrivateKey
    32  	PublishedTransactions chan *wire.MsgTx
    33  	index                 uint32
    34  	Utxos                 []*lnwallet.Utxo
    35  }
    36  
    37  // BackEnd returns "mock" to signify a mock wallet controller.
    38  func (w *WalletController) BackEnd() string {
    39  	return "mock"
    40  }
    41  
    42  // FetchInputInfo will be called to get info about the inputs to the funding
    43  // transaction.
    44  func (w *WalletController) FetchInputInfo(
    45  	prevOut *wire.OutPoint) (*lnwallet.Utxo, error) {
    46  
    47  	utxo := &lnwallet.Utxo{
    48  		AddressType:   lnwallet.PubKeyHash,
    49  		Value:         15 * dcrutil.AtomsPerCoin,
    50  		PkScript:      CoinPkScript,
    51  		Confirmations: 1,
    52  		OutPoint:      *prevOut,
    53  	}
    54  	return utxo, nil
    55  }
    56  
    57  // ScriptForOutput returns the address, witness program and redeem script for a
    58  // given UTXO. An error is returned if the UTXO does not belong to our wallet or
    59  // it is not a managed pubKey address.
    60  func (w *WalletController) ScriptForOutput(*wire.TxOut) (
    61  	btcwalletcompat.ManagedPubKeyAddress, []byte, []byte, error) {
    62  
    63  	return nil, nil, nil, nil
    64  }
    65  
    66  // ConfirmedBalance currently returns dummy values.
    67  func (w *WalletController) ConfirmedBalance(int32, string) (dcrutil.Amount,
    68  	error) {
    69  
    70  	return 0, nil
    71  }
    72  
    73  // NewAddress is called to get new addresses for delivery, change etc.
    74  func (w *WalletController) NewAddress(lnwallet.AddressType, bool,
    75  	string) (stdaddr.Address, error) {
    76  
    77  	addr, _ := stdaddr.NewAddressPubKeyEcdsaSecp256k1V0(
    78  		w.RootKey.PubKey(), chaincfg.RegNetParams(),
    79  	)
    80  	return addr, nil
    81  }
    82  
    83  // LastUnusedAddress currently returns dummy values.
    84  func (w *WalletController) LastUnusedAddress(lnwallet.AddressType,
    85  	string) (stdaddr.Address, error) {
    86  
    87  	return nil, nil
    88  }
    89  
    90  // IsOurAddress currently returns a dummy value.
    91  func (w *WalletController) IsOurAddress(stdaddr.Address) bool {
    92  	return false
    93  }
    94  
    95  // AddressInfo currently returns a dummy value.
    96  func (w *WalletController) AddressInfo(
    97  	stdaddr.Address) (btcwalletcompat.ManagedAddress, error) {
    98  
    99  	return nil, nil
   100  }
   101  
   102  // ListAccounts currently returns a dummy value.
   103  func (w *WalletController) ListAccounts(string) (
   104  	[]wallet.AccountProperties, error) {
   105  
   106  	return nil, nil
   107  }
   108  
   109  // ImportAccount currently returns a dummy value.
   110  func (w *WalletController) ImportAccount(string, *hdkeychain.ExtendedKey,
   111  	bool) (*wallet.AccountProperties,
   112  	[]stdaddr.Address, []stdaddr.Address, error) {
   113  
   114  	return nil, nil, nil, nil
   115  }
   116  
   117  // ImportPublicKeycurrently returns a dummy value.
   118  func (w *WalletController) ImportPublicKey(pubKey *secp256k1.PublicKey) error {
   119  	return nil
   120  }
   121  
   122  // SendOutputs currently returns dummy values.
   123  func (w *WalletController) SendOutputs([]*wire.TxOut,
   124  	chainfee.AtomPerKByte, int32, string, string) (*wire.MsgTx, error) {
   125  
   126  	return nil, nil
   127  }
   128  
   129  // CreateSimpleTx currently returns dummy values.
   130  func (w *WalletController) CreateSimpleTx([]*wire.TxOut,
   131  	chainfee.AtomPerKByte, int32, bool) (*txauthor.AuthoredTx, error) {
   132  
   133  	return nil, nil
   134  }
   135  
   136  // ListUnspentWitness is called by the wallet when doing coin selection. We just
   137  // need one unspent for the funding transaction.
   138  func (w *WalletController) ListUnspentWitness(int32, int32,
   139  	string) ([]*lnwallet.Utxo, error) {
   140  
   141  	// If the mock already has a list of utxos, return it.
   142  	if w.Utxos != nil {
   143  		return w.Utxos, nil
   144  	}
   145  
   146  	// Otherwise create one to return.
   147  	utxo := &lnwallet.Utxo{
   148  		AddressType: lnwallet.PubKeyHash,
   149  		Value:       dcrutil.Amount(15 * dcrutil.AtomsPerCoin),
   150  		PkScript:    CoinPkScript,
   151  		OutPoint: wire.OutPoint{
   152  			Hash:  chainhash.Hash{},
   153  			Index: w.index,
   154  		},
   155  	}
   156  	atomic.AddUint32(&w.index, 1)
   157  	var ret []*lnwallet.Utxo
   158  	ret = append(ret, utxo)
   159  	return ret, nil
   160  }
   161  
   162  // ListTransactionDetails currently returns dummy values.
   163  func (w *WalletController) ListTransactionDetails(int32, int32,
   164  	string) ([]*lnwallet.TransactionDetail, error) {
   165  
   166  	return nil, nil
   167  }
   168  
   169  // LockOutpoint currently does nothing.
   170  func (w *WalletController) LockOutpoint(o wire.OutPoint) {}
   171  
   172  // UnlockOutpoint currently does nothing.
   173  func (w *WalletController) UnlockOutpoint(o wire.OutPoint) {}
   174  
   175  // LeaseOutput returns the current time and a nil error.
   176  func (w *WalletController) LeaseOutput(lnwallet.LockID, wire.OutPoint,
   177  	time.Duration) (time.Time, error) {
   178  	return time.Now(), nil
   179  }
   180  
   181  // ReleaseOutput currently does nothing.
   182  func (w *WalletController) ReleaseOutput(lnwallet.LockID, wire.OutPoint) error {
   183  	return nil
   184  }
   185  
   186  func (w *WalletController) ListLeasedOutputs() ([]*lnwallet.LockedOutput, error) {
   187  	return nil, nil
   188  }
   189  
   190  // FundPsbt currently does nothing.
   191  func (w *WalletController) FundPsbt(*psbt.Packet, int32, chainfee.AtomPerKByte,
   192  	string) (int32, error) {
   193  
   194  	return 0, nil
   195  }
   196  
   197  // SignPsbt currently does nothing.
   198  func (w *WalletController) SignPsbt(*psbt.Packet) error {
   199  	return nil
   200  }
   201  
   202  // FinalizePsbt currently does nothing.
   203  func (w *WalletController) FinalizePsbt(_ *psbt.Packet) error {
   204  	return nil
   205  }
   206  
   207  // PublishTransaction sends a transaction to the PublishedTransactions chan.
   208  func (w *WalletController) PublishTransaction(tx *wire.MsgTx, _ string) error {
   209  	w.PublishedTransactions <- tx
   210  	return nil
   211  }
   212  
   213  // LabelTransaction currently does nothing.
   214  func (w *WalletController) LabelTransaction(chainhash.Hash, string,
   215  	bool) error {
   216  
   217  	return nil
   218  }
   219  
   220  // SubscribeTransactions currently does nothing.
   221  func (w *WalletController) SubscribeTransactions() (lnwallet.TransactionSubscription,
   222  	error) {
   223  
   224  	return nil, nil
   225  }
   226  
   227  // AbandonDoubleSpends currently returns dummy values.
   228  func (w *WalletController) AbandonDoubleSpends(spentOutpoints ...*wire.OutPoint) error {
   229  	return nil
   230  }
   231  
   232  // InitialSyncChannelcurrently returns dummy values.
   233  func (w *WalletController) InitialSyncChannel() <-chan struct{} {
   234  	return nil
   235  }
   236  
   237  // BestBlock currently returns dummy values.
   238  func (w *WalletController) BestBlock() (int64, chainhash.Hash, int64, error) {
   239  	return 0, chainhash.Hash{}, 0, nil
   240  }
   241  
   242  // IsSynced currently returns dummy values.
   243  func (w *WalletController) IsSynced() (bool, int64, error) {
   244  	return true, int64(0), nil
   245  }
   246  
   247  // GetRecoveryInfo currently returns dummy values.
   248  func (w *WalletController) GetRecoveryInfo() (bool, float64, error) {
   249  	return true, float64(1), nil
   250  }
   251  
   252  // Start currently does nothing.
   253  func (w *WalletController) Start() error {
   254  	return nil
   255  }
   256  
   257  // Stop currently does nothing.
   258  func (w *WalletController) Stop() error {
   259  	return nil
   260  }
   261  
   262  func (w *WalletController) FetchTx(chainhash.Hash) (*wire.MsgTx, error) {
   263  	return nil, nil
   264  }
   265  
   266  func (w *WalletController) RemoveDescendants(*wire.MsgTx) error {
   267  	return nil
   268  }