walletcontroller.go 7.6 KB

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