You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.1 KiB

  1. package test
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. "github.com/btcsuite/btcd/btcec"
  7. "github.com/btcsuite/btcd/chaincfg"
  8. "github.com/btcsuite/btcutil"
  9. "github.com/lightninglabs/loop/lndclient"
  10. "github.com/lightninglabs/loop/utils"
  11. "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
  12. "github.com/lightningnetwork/lnd/lntypes"
  13. "github.com/lightningnetwork/lnd/lnwire"
  14. "github.com/lightningnetwork/lnd/zpay32"
  15. "golang.org/x/net/context"
  16. )
  17. type mockLightningClient struct {
  18. lnd *LndMockServices
  19. wg sync.WaitGroup
  20. }
  21. // PayInvoice pays an invoice.
  22. func (h *mockLightningClient) PayInvoice(ctx context.Context, invoice string,
  23. maxFee btcutil.Amount,
  24. outgoingChannel *uint64) chan lndclient.PaymentResult {
  25. done := make(chan lndclient.PaymentResult, 1)
  26. mockChan := make(chan error)
  27. h.wg.Add(1)
  28. go func() {
  29. defer h.wg.Done()
  30. amt, err := utils.GetInvoiceAmt(&chaincfg.TestNet3Params, invoice)
  31. if err != nil {
  32. select {
  33. case done <- lndclient.PaymentResult{
  34. Err: err,
  35. }:
  36. case <-ctx.Done():
  37. }
  38. return
  39. }
  40. var paidFee btcutil.Amount
  41. err = <-mockChan
  42. if err != nil {
  43. amt = 0
  44. } else {
  45. paidFee = 1
  46. }
  47. select {
  48. case done <- lndclient.PaymentResult{
  49. Err: err,
  50. PaidFee: paidFee,
  51. PaidAmt: amt,
  52. }:
  53. case <-ctx.Done():
  54. }
  55. }()
  56. h.lnd.SendPaymentChannel <- PaymentChannelMessage{
  57. PaymentRequest: invoice,
  58. Done: mockChan,
  59. }
  60. return done
  61. }
  62. func (h *mockLightningClient) WaitForFinished() {
  63. h.wg.Wait()
  64. }
  65. func (h *mockLightningClient) ConfirmedWalletBalance(ctx context.Context) (
  66. btcutil.Amount, error) {
  67. return 1000000, nil
  68. }
  69. func (h *mockLightningClient) GetInfo(ctx context.Context) (*lndclient.Info,
  70. error) {
  71. var pubKey [33]byte
  72. return &lndclient.Info{
  73. BlockHeight: 600,
  74. IdentityPubkey: pubKey,
  75. }, nil
  76. }
  77. func (h *mockLightningClient) GetFeeEstimate(ctx context.Context, amt btcutil.Amount, dest [33]byte) (
  78. lnwire.MilliSatoshi, error) {
  79. return 0, nil
  80. }
  81. func (h *mockLightningClient) AddInvoice(ctx context.Context,
  82. in *invoicesrpc.AddInvoiceData) (lntypes.Hash, string, error) {
  83. h.lnd.lock.Lock()
  84. defer h.lnd.lock.Unlock()
  85. var hash lntypes.Hash
  86. if in.Hash != nil {
  87. hash = *in.Hash
  88. } else {
  89. hash = (*in.Preimage).Hash()
  90. }
  91. // Create and encode the payment request as a bech32 (zpay32) string.
  92. creationDate := time.Now()
  93. payReq, err := zpay32.NewInvoice(
  94. h.lnd.ChainParams, hash, creationDate,
  95. zpay32.Description(in.Memo),
  96. zpay32.CLTVExpiry(in.CltvExpiry),
  97. zpay32.Amount(lnwire.MilliSatoshi(in.Value)),
  98. )
  99. if err != nil {
  100. return lntypes.Hash{}, "", err
  101. }
  102. privKey, err := btcec.NewPrivateKey(btcec.S256())
  103. if err != nil {
  104. return lntypes.Hash{}, "", err
  105. }
  106. payReqString, err := payReq.Encode(
  107. zpay32.MessageSigner{
  108. SignCompact: func(hash []byte) ([]byte, error) {
  109. // btcec.SignCompact returns a pubkey-recoverable signature
  110. sig, err := btcec.SignCompact(
  111. btcec.S256(), privKey, hash, true,
  112. )
  113. if err != nil {
  114. return nil, fmt.Errorf("can't sign the hash: %v", err)
  115. }
  116. return sig, nil
  117. },
  118. },
  119. )
  120. if err != nil {
  121. return lntypes.Hash{}, "", err
  122. }
  123. return hash, payReqString, nil
  124. }