mock_ln_client.go raw

   1  package tests
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"time"
   7  
   8  	"github.com/getAlby/hub/lnclient"
   9  )
  10  
  11  // for the invoice:
  12  // lnbcrt5u1pjuywzppp5h69dt59cypca2wxu69sw8ga0g39a3yx7dqug5nthrw3rcqgfdu4qdqqcqzzsxqyz5vqsp5gzlpzszyj2k30qmpme7jsfzr24wqlvt9xdmr7ay34lfelz050krs9qyyssq038x07nh8yuv8hdpjh5y8kqp7zcd62ql9na9xh7pla44htjyy02sz23q7qm2tza6ct4ypljk54w9k9qsrsu95usk8ce726ytep6vhhsq9mhf9a
  13  const MockPaymentHash500 = "be8ad5d0b82071d538dcd160e3a3af444bd890de68388a4d771ba23c01096f2a"
  14  
  15  const MockInvoice = "lntbs1230n1pnkqautdqyw3jsnp4q09a0z84kg4a2m38zjllw43h953fx5zvqe8qxfgw694ymkq26u8zcpp5yvnh6hsnlnj4xnuh2trzlnunx732dv8ta2wjr75pdfxf6p2vlyassp5hyeg97a3ft5u769kjwsn7p0e85h79pzz8kladmnqhpcypz2uawjs9qyysgqcqpcxq8zals8sq9yeg2pa9eywkgj50cyzxd5elatujuc0c0wh6j9nat5mn34pgk8u9ufpgs99tw9ldlfk42cqlkr48au3lmuh09269prg4qkggh4a8cyqpfl0y6j"
  16  const MockPaymentHash = "23277d5e13fce5534f9752c62fcf9337a2a6b0ebea9d21fa816a4c9d054cf93b" // for the above invoice
  17  
  18  const MockZeroAmountInvoice = "lntbs1pnkjfgudqjd3hkueeqv4u8q6tj0ynp4qws83mqzuqptu5kfvxeles7qmyhsj6u2s6zyuft26mcr4tdmcupuupp533y9nwnsaktr9zlvyxmv97ta23faerygh3t9xvsfwytsr28lgggssp5mku3023z3kdxlpx6vrwtfxvvrxpffrquy6veex4ndk7rxhdtslhq9qyysgqcqpcxqxfvltyqva6y7k89jwtcljx399jl6wsq4lkq29vnm3rj4jxmapc6vcs358sx8mtpgh93rdc6ccqpxwwfga59zrla5m55zwzck2y2rsrxumu852sqkvpcm7"
  19  const MockZeroAmountPaymentHash = "8c4859ba70ed96328bec21b6c2f97d5453dc8c88bc56533209711701a8ff4211"
  20  
  21  var MockNodeInfo = lnclient.NodeInfo{
  22  	Alias:       "bob",
  23  	Color:       "#3399FF",
  24  	Pubkey:      "123pubkey",
  25  	Network:     "testnet",
  26  	BlockHeight: 12,
  27  	BlockHash:   "123blockhash",
  28  }
  29  
  30  var MockLNClientBalances = lnclient.BalancesResponse{
  31  	Lightning: lnclient.LightningBalanceResponse{
  32  		TotalSpendableMsat: 21000,
  33  	},
  34  }
  35  
  36  var MockTime = time.Unix(1693876963, 0)
  37  var MockTimeUnix = MockTime.Unix()
  38  
  39  var MockLNClientTransactions = []lnclient.Transaction{
  40  	{
  41  		Type:            "incoming",
  42  		Invoice:         MockInvoice,
  43  		Description:     "mock invoice 1",
  44  		DescriptionHash: "hash1",
  45  		Preimage:        "preimage1",
  46  		PaymentHash:     MockPaymentHash,
  47  		AmountMsat:      1000,
  48  		FeesPaidMsat:    50,
  49  		SettledAt:       &MockTimeUnix,
  50  		Metadata: map[string]interface{}{
  51  			"key1": "value1",
  52  			"key2": 42,
  53  		},
  54  	},
  55  	{
  56  		Type:            "incoming",
  57  		Invoice:         MockInvoice,
  58  		Description:     "mock invoice 2",
  59  		DescriptionHash: "hash2",
  60  		Preimage:        "preimage2",
  61  		PaymentHash:     MockPaymentHash,
  62  		AmountMsat:      2000,
  63  		FeesPaidMsat:    75,
  64  		SettledAt:       &MockTimeUnix,
  65  	},
  66  }
  67  var MockLNClientTransaction = &MockLNClientTransactions[0]
  68  
  69  var MockLNClientHoldTransaction = &lnclient.Transaction{
  70  	Type:            "incoming",
  71  	Invoice:         "lntb10n1p5zg5p7dqud4hkx6eqdphkcepqd9h8vmmfvdjsnp4qw988hn4lhpu0my4rf0qkraft3wdx5aa0jnjmusgd23z3s0e9qv62pp5yaulxt6x83u4u0x2pck5pyg7fdxhjsd65c9lmu2a9r05qh0cgl6ssp5x57tsnnuc9hr99a9xzg5ylqma5fwvckxa50jqqay5zykqp83h9kq9qyysgqcqpcxqxf92hqqxh0avuskdnuzkk7mxslsdwem3qq3sf79a4ypmx0hax3rupp043yhv97h25vaarj0xrlcg2fdfdhpztsthettskyaylrz6vweztn0twqqv7kxmr",
  72  	Description:     "mock hold invoice",
  73  	DescriptionHash: "",
  74  	Preimage:        "4aa083cad11038359b4f614f3a3d6a8298ae17d5275412bc3eca4f5f4d27f2d4",
  75  	PaymentHash:     "2779f32f463c795e3cca0e2d40911e4b4d7941baa60bfdf15d28df405df847f5",
  76  	AmountMsat:      2000,
  77  }
  78  
  79  type MockLn struct {
  80  	MakeInvoiceResponses       []*lnclient.Transaction
  81  	MakeInvoiceErrors          []error
  82  	PayInvoiceResponses        []*lnclient.PayInvoiceResponse
  83  	PayInvoiceErrors           []error
  84  	PayKeysendResponses        []*lnclient.PayKeysendResponse
  85  	PayKeysendErrors           []error
  86  	PaymentDelay               *time.Duration
  87  	Pubkey                     string
  88  	MockTransaction            *lnclient.Transaction
  89  	LastMinCltvExpiryDelta     *uint64
  90  	SupportedNotificationTypes *[]string
  91  }
  92  
  93  func NewMockLn() (*MockLn, error) {
  94  	return &MockLn{}, nil
  95  }
  96  
  97  func (mln *MockLn) SendPaymentSync(payReq string, amountMsat *uint64) (*lnclient.PayInvoiceResponse, error) {
  98  	if len(mln.PayInvoiceResponses) > 0 {
  99  		response := mln.PayInvoiceResponses[0]
 100  		err := mln.PayInvoiceErrors[0]
 101  		mln.PayInvoiceResponses = mln.PayInvoiceResponses[1:]
 102  		mln.PayInvoiceErrors = mln.PayInvoiceErrors[1:]
 103  		return response, err
 104  	}
 105  	if mln.PaymentDelay != nil {
 106  		time.Sleep(*mln.PaymentDelay)
 107  	}
 108  
 109  	return &lnclient.PayInvoiceResponse{
 110  		Preimage: "123preimage",
 111  	}, nil
 112  }
 113  
 114  func (mln *MockLn) SendKeysend(amountMsat uint64, destination string, custom_records []lnclient.TLVRecord, preimage string) (*lnclient.PayKeysendResponse, error) {
 115  	if len(mln.PayKeysendResponses) > 0 {
 116  		response := mln.PayKeysendResponses[0]
 117  		err := mln.PayKeysendErrors[0]
 118  		mln.PayKeysendResponses = mln.PayKeysendResponses[1:]
 119  		mln.PayKeysendErrors = mln.PayKeysendErrors[1:]
 120  		return response, err
 121  	}
 122  	return &lnclient.PayKeysendResponse{
 123  		FeeMsat: 1,
 124  	}, nil
 125  }
 126  
 127  func (mln *MockLn) GetInfo(ctx context.Context) (info *lnclient.NodeInfo, err error) {
 128  	return &MockNodeInfo, nil
 129  }
 130  
 131  func (mln *MockLn) MakeInvoice(ctx context.Context, amountMsat int64, description string, descriptionHash string, expiry int64, throughNodePubkey *string) (transaction *lnclient.Transaction, err error) {
 132  	if len(mln.MakeInvoiceResponses) > 0 {
 133  		response := mln.MakeInvoiceResponses[0]
 134  		err := mln.MakeInvoiceErrors[0]
 135  		mln.MakeInvoiceResponses = mln.MakeInvoiceResponses[1:]
 136  		mln.MakeInvoiceErrors = mln.MakeInvoiceErrors[1:]
 137  		return response, err
 138  	}
 139  	return MockLNClientTransaction, nil
 140  }
 141  
 142  func (mln *MockLn) MakeHoldInvoice(ctx context.Context, amountMsat int64, description string, descriptionHash string, expiry int64, paymentHash string, minCltvExpiryDelta *uint64) (transaction *lnclient.Transaction, err error) {
 143  	if minCltvExpiryDelta == nil {
 144  		mln.LastMinCltvExpiryDelta = nil
 145  	} else {
 146  		value := *minCltvExpiryDelta
 147  		mln.LastMinCltvExpiryDelta = &value
 148  	}
 149  
 150  	if len(mln.MakeInvoiceResponses) > 0 {
 151  		response := mln.MakeInvoiceResponses[0]
 152  		err := mln.MakeInvoiceErrors[0]
 153  		mln.MakeInvoiceResponses = mln.MakeInvoiceResponses[1:]
 154  		mln.MakeInvoiceErrors = mln.MakeInvoiceErrors[1:]
 155  		return response, err
 156  	}
 157  	return MockLNClientHoldTransaction, nil
 158  }
 159  
 160  func (mln *MockLn) SettleHoldInvoice(ctx context.Context, preimage string) (err error) {
 161  	return nil
 162  }
 163  
 164  func (mln *MockLn) CancelHoldInvoice(ctx context.Context, paymentHash string) (err error) {
 165  	return nil
 166  }
 167  
 168  func (mln *MockLn) LookupInvoice(ctx context.Context, paymentHash string) (transaction *lnclient.Transaction, err error) {
 169  	if mln.MockTransaction != nil {
 170  		return mln.MockTransaction, nil
 171  	}
 172  	return MockLNClientTransaction, nil
 173  }
 174  
 175  func (mln *MockLn) Shutdown() error {
 176  	return nil
 177  }
 178  
 179  func (mln *MockLn) ListChannels(ctx context.Context) (channels []lnclient.Channel, err error) {
 180  	return []lnclient.Channel{}, nil
 181  }
 182  func (mln *MockLn) GetNodeConnectionInfo(ctx context.Context) (nodeConnectionInfo *lnclient.NodeConnectionInfo, err error) {
 183  	return nil, nil
 184  }
 185  func (mln *MockLn) ConnectPeer(ctx context.Context, connectPeerRequest *lnclient.ConnectPeerRequest) error {
 186  	return nil
 187  }
 188  func (mln *MockLn) OpenChannel(ctx context.Context, openChannelRequest *lnclient.OpenChannelRequest) (*lnclient.OpenChannelResponse, error) {
 189  	return nil, nil
 190  }
 191  func (mln *MockLn) CloseChannel(ctx context.Context, closeChannelRequest *lnclient.CloseChannelRequest) error {
 192  	return nil
 193  }
 194  func (mln *MockLn) GetNewOnchainAddress(ctx context.Context) (string, error) {
 195  	return "", nil
 196  }
 197  func (mln *MockLn) GetBalances(ctx context.Context, includeInactiveChannels bool) (*lnclient.BalancesResponse, error) {
 198  	return &MockLNClientBalances, nil
 199  }
 200  func (mln *MockLn) GetOnchainBalance(ctx context.Context) (*lnclient.OnchainBalanceResponse, error) {
 201  	return nil, nil
 202  }
 203  func (mln *MockLn) RedeemOnchainFunds(ctx context.Context, toAddress string, amountSat uint64, feeRate *uint64, sendAll bool) (txId string, err error) {
 204  	return "", nil
 205  }
 206  func (mln *MockLn) ResetRouter(key string) error {
 207  	return nil
 208  }
 209  func (mln *MockLn) ListPeers(ctx context.Context) ([]lnclient.PeerDetails, error) {
 210  	return nil, nil
 211  }
 212  func (mln *MockLn) GetLogOutput(ctx context.Context, maxLen int) ([]byte, error) {
 213  	return []byte{}, nil
 214  }
 215  func (mln *MockLn) SignMessage(ctx context.Context, message string) (string, error) {
 216  	return "", nil
 217  }
 218  func (mln *MockLn) GetStorageDir() (string, error) {
 219  	return "", nil
 220  }
 221  func (mln *MockLn) GetNodeStatus(ctx context.Context) (nodeStatus *lnclient.NodeStatus, err error) {
 222  	return &lnclient.NodeStatus{
 223  		IsReady: true,
 224  	}, nil
 225  }
 226  func (mln *MockLn) GetNetworkGraph(ctx context.Context, nodeIds []string) (lnclient.NetworkGraphResponse, error) {
 227  	return nil, nil
 228  }
 229  
 230  func (mln *MockLn) UpdateLastWalletSyncRequest() {}
 231  
 232  func (mln *MockLn) DisconnectPeer(ctx context.Context, peerId string) error {
 233  	return nil
 234  }
 235  
 236  func (mln *MockLn) UpdateChannel(ctx context.Context, updateChannelRequest *lnclient.UpdateChannelRequest) error {
 237  	return nil
 238  }
 239  
 240  func (mln *MockLn) GetSupportedNIP47Methods() []string {
 241  	return []string{"pay_invoice", "pay_keysend", "get_balance", "get_budget", "get_info", "make_invoice", "lookup_invoice", "list_transactions", "multi_pay_invoice", "multi_pay_keysend", "sign_message"}
 242  }
 243  func (mln *MockLn) GetSupportedNIP47NotificationTypes() []string {
 244  	if mln.SupportedNotificationTypes != nil {
 245  		return *mln.SupportedNotificationTypes
 246  	}
 247  
 248  	return []string{"payment_received", "payment_sent"}
 249  }
 250  func (mln *MockLn) GetPubkey() string {
 251  	if mln.Pubkey != "" {
 252  		return mln.Pubkey
 253  	}
 254  
 255  	return "123pubkey"
 256  }
 257  
 258  func (mln *MockLn) GetCustomNodeCommandDefinitions() []lnclient.CustomNodeCommandDef {
 259  	return nil
 260  }
 261  
 262  func (mln *MockLn) ExecuteCustomNodeCommand(ctx context.Context, command *lnclient.CustomNodeCommandRequest) (*lnclient.CustomNodeCommandResponse, error) {
 263  	return nil, nil
 264  }
 265  
 266  func (mln *MockLn) MakeOffer(ctx context.Context, description string) (string, error) {
 267  	return "", errors.New("not supported")
 268  }
 269  
 270  func (mln *MockLn) ListOnchainTransactions(ctx context.Context) ([]lnclient.OnchainTransaction, error) {
 271  	return nil, errors.ErrUnsupported
 272  }
 273