-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathCommonTests.cs
1260 lines (1117 loc) · 71.9 KB
/
CommonTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using NBitcoin;
using NBitcoin.DataEncoders;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Lightning.Charge;
using BTCPayServer.Lightning.CLightning;
using BTCPayServer.Lightning.Eclair;
using BTCPayServer.Lightning.LND;
using BTCPayServer.Lightning.LndHub;
using NBitcoin.Crypto;
using NBitcoin.RPC;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Abstractions;
using System.Net.Http.Headers;
namespace BTCPayServer.Lightning.Tests
{
[Collection(nameof(NonParallelizableCollectionDefinition))]
public class CommonTests
{
#if DEBUG
public const int Timeout = 20 * 60 * 1000;
#else
public const int Timeout = 2 * 60 * 1000;
#endif
public CommonTests(ITestOutputHelper helper)
{
Docker = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("IN_DOCKER_CONTAINER"));
Logs.Tester = new XUnitLog(helper) { Name = "Tests" };
Logs.LogProvider = new XUnitLogProvider(helper);
ConnectChannels.Logs = Logs.LogProvider.CreateLogger("Tests");
}
public static bool Docker { get; set; }
[Fact(Timeout = Timeout)]
public async Task CanCreateInvoice()
{
const int amount = 10001;
await WaitServersAreUp();
foreach (var client in Tester.GetLightningClients())
{
Logs.Tester.LogInformation($"{client.Name}: {nameof(CanCreateInvoice)}");
var expiry = TimeSpan.FromMinutes(5);
var beyondExpiry = client.Client is LndHubLightningClient
? DateTimeOffset.UtcNow + TimeSpan.FromDays(1) // LNDhub has a fixed expiry of 1 day
: DateTimeOffset.UtcNow + TimeSpan.FromMinutes(6);
var expectedAmount = client.Client is LndHubLightningClient
? LightMoney.Satoshis(amount / 1000) // LNDhub accounts with sats instead of msat
: LightMoney.MilliSatoshis(amount);
var createdInvoice = await client.Client.CreateInvoice(amount, "CanCreateInvoice", expiry);
var retrievedInvoice = await client.Client.GetInvoice(createdInvoice.Id);
AssertUnpaid(createdInvoice, expectedAmount);
Assert.True(createdInvoice.ExpiresAt > DateTimeOffset.UtcNow);
Assert.True(createdInvoice.ExpiresAt < beyondExpiry);
AssertUnpaid(retrievedInvoice, expectedAmount);
Assert.True(retrievedInvoice.ExpiresAt > DateTimeOffset.UtcNow);
Assert.True(retrievedInvoice.ExpiresAt < beyondExpiry);
retrievedInvoice = await client.Client.GetInvoice("c4180c13ae6b43e261c4c6f43c1b6760cfc80ba5a06643f383ece30d7316e4a6");
Assert.Null(retrievedInvoice);
retrievedInvoice = await client.Client.GetInvoice("lol");
Assert.Null(retrievedInvoice);
await Task.Delay(1000);
var invoices = await client.Client.ListInvoices();
Assert.Contains(invoices, invoice => invoice.Id == createdInvoice.Id);
// check that it's also present in the pending only list
var onlyPending = new ListInvoicesParams { PendingOnly = true };
invoices = await client.Client.ListInvoices(onlyPending);
Assert.Contains(invoices, invoice => invoice.Id == createdInvoice.Id);
}
}
[Fact(Timeout = Timeout)]
public async Task CanCreateInvoiceWithDescriptionHash()
{
var expectedHash = new uint256("1d8adbd8794116cfd6044e1d25446685a7c6c750a9e2cec1845acc23656466d1");
var create = new CreateInvoiceParams(10000, "CanCreateInvoiceWithDescriptionHash", TimeSpan.FromMinutes(5))
{
DescriptionHashOnly = true
};
Assert.Equal(expectedHash, create.DescriptionHash);
async Task<LightningInvoice> CreateWithHash(ILightningClient lightningClient)
{
return await lightningClient.CreateInvoice(create);
}
await WaitServersAreUp();
foreach (var client in Tester.GetLightningClients())
{
switch (client.Client)
{
case CLightningClient _:
case LndClient _:
Logs.Tester.LogInformation($"{client.Name}: {nameof(CanCreateInvoiceWithDescriptionHash)}");
var createdInvoice = await CreateWithHash(client.Client);
var retrievedInvoice = await client.Client.GetInvoice(createdInvoice.Id);
Logs.Tester.LogInformation(JObject.FromObject(createdInvoice).ToString());
Logs.Tester.LogInformation(JObject.FromObject(retrievedInvoice).ToString());
AssertUnpaid(createdInvoice);
AssertUnpaid(retrievedInvoice);
var createdInvoiceBOLT = BOLT11PaymentRequest.Parse(createdInvoice.BOLT11, Network.RegTest);
var retrievedInvoiceBOLT = BOLT11PaymentRequest.Parse(retrievedInvoice.BOLT11, Network.RegTest);
Assert.Equal(createdInvoiceBOLT.PaymentHash, retrievedInvoiceBOLT.PaymentHash);
Assert.Equal(expectedHash, createdInvoiceBOLT.DescriptionHash);
break;
case LndHubLightningClient _:
// Once this gets merged, we can support it too: https://github.com/BlueWallet/LndHub/pull/319
// Skip and don't throw the exception, because the compatible lndhub.Go by Alby supports it
break;
default:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await CreateWithHash(client.Client);
});
break;
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanCancelInvoices()
{
async Task CreateAndCancel(ILightningClient lightningClient)
{
var i = await lightningClient.CreateInvoice(new LightMoney(10000), "hi there", TimeSpan.FromMinutes(10),
CancellationToken.None);
await lightningClient.CancelInvoice(i.Id);
i = await lightningClient.GetInvoice(i.Id);
Assert.Null(i);
}
await WaitServersAreUp();
foreach (var client in Tester.GetLightningClients())
{
switch (client.Client)
{
case ChargeClient _:
case CLightningClient _:
case LndClient _:
Logs.Tester.LogInformation($"{client.Name}: {nameof(CanCancelInvoices)}");
await CreateAndCancel(client.Client);
break;
default:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await CreateAndCancel(client.Client);
});
break;
}
}
}
[Fact]
public async Task CanCreateInvoiceUsingConnectionString()
{
var network = Tester.Network;
ILightningClientFactory factory = new LightningClientFactory(network);
var connectionStrings = Docker
? new List<string>
{
"type=charge;server=http://api-token:foiewnccewuify@charge:9112;allowinsecure=true",
"type=lnd-rest;server=http://lnd_dest:8080;allowinsecure=true",
"type=clightning;server=tcp://lightningd:9835",
"type=eclair;server=http://eclair:8080;password=bukkake"
}
: new List<string>
{
"type=charge;server=http://api-token:foiewnccewuify@127.0.0.1:37462;allowinsecure=true",
"type=lnd-rest;server=http://127.0.0.1:42802;allowinsecure=true",
"type=clightning;server=tcp://127.0.0.1:48532",
"type=eclair;server=http://127.0.0.1:4570;password=bukkake"
};
// LNDhub needs an account first
var lndhubServer = Docker ? "http://lndhub:3000" : "http://127.0.0.1:42923";
var lndhubClient = new LndHubLightningClient(new Uri(lndhubServer), "login", "password", network);
var data = await lndhubClient.CreateAccount();
lndhubServer = lndhubServer.Replace("://", $"://{data.Login}:{data.Password}@");
connectionStrings.Add($"type=lndhub;server={lndhubServer};allowinsecure=true");
var clientTypes = Tester.GetLightningClients().Select(l => l.Client.GetType()).ToArray();
foreach (var connectionString in connectionStrings)
{
// check connection string can be parsed and turned into a string again
factory.TryCreate(connectionString, out var parsed, out _);
Assert.NotNull(parsed.ToString());
// apply connection string and create invoice
var client = factory.Create(connectionString);
if (!clientTypes.Contains(client.GetType()))
continue;
var createdInvoice = await client.CreateInvoice(10000, "CanCreateInvoiceUsingConnectionString", TimeSpan.FromMinutes(5));
var retrievedInvoice = await client.GetInvoice(createdInvoice.Id);
AssertUnpaid(createdInvoice);
AssertUnpaid(retrievedInvoice);
}
}
[Fact]
public void CanParseCustomConnectionString()
{
var network = Tester.Network;
ILightningClientFactory factory = new LightningClientFactory(network);
var connectionStrings = new List<string>
{
"lndhub://login:password@http://server.onion"
};
var clientTypes = Tester.GetLightningClients().Select(l => l.Client.GetType()).ToArray();
foreach (var connectionString in connectionStrings)
{
// check connection string can be parsed and turned into a string again
factory.TryCreate(connectionString, out var parsed, out _);
Assert.NotNull(parsed.ToString());
// apply connection string and check client
var client = factory.Create(connectionString);
Assert.Contains(client.GetType(), clientTypes);
}
}
[Fact(Timeout = Timeout)]
public async Task CanGetInfo()
{
await WaitServersAreUp();
foreach (var client in Tester.GetLightningClients())
{
Logs.Tester.LogInformation($"{client.Name}: {nameof(CanGetInfo)}");
var info = await client.Client.GetInfo();
Assert.NotNull(info);
Assert.True(info.BlockHeight > 0);
Assert.NotEmpty(info.NodeInfoList);
Assert.NotNull(info.Alias);
Assert.NotNull(info.Version);
Assert.NotNull(info.Color);
switch (client.Client)
{
case LndClient _:
case ChargeClient _:
case CLightningClient _:
case LndHubLightningClient _:
Assert.NotNull(info.PeersCount);
Assert.NotNull(info.ActiveChannelsCount);
Assert.NotNull(info.InactiveChannelsCount);
Assert.NotNull(info.PendingChannelsCount);
break;
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanGetBalance()
{
// for channel values to be in reasonable bounds
var lowerBound = LightMoney.Zero;
var upperBound = LightMoney.Satoshis(16777215);
await WaitServersAreUp();
foreach (var test in Tester.GetTestedPairs())
{
await EnsureConnectedToDestinations(test);
var client = test.Customer;
LightningNodeBalance balance;
Logs.Tester.LogInformation($"{test.Name}: {nameof(CanGetBalance)}");
switch (client)
{
case LndClient _:
case CLightningClient _:
case EclairLightningClient _:
balance = await client.GetBalance();
// onchain
Assert.True(balance.OnchainBalance.Confirmed > 0L);
Assert.Equal(Money.Zero, balance.OnchainBalance.Unconfirmed);
// offchain
Assert.NotNull(balance.OffchainBalance);
Assert.Equal(LightMoney.Zero, balance.OffchainBalance.Opening);
Assert.InRange(balance.OffchainBalance.Local, lowerBound, upperBound);
Assert.InRange(balance.OffchainBalance.Remote, lowerBound, upperBound);
Assert.Equal(LightMoney.Zero, balance.OffchainBalance.Closing);
Logs.Tester.LogInformation($"{test.Name}: {Pretty(balance)}");
if (client is not EclairLightningClient)
{
// make sure we catch msat/sat bugs
// Eclair can't check this, because it uses the same wallet as bitcoin core
// thus it get all the coinbases.
Assert.True(balance.OnchainBalance.Confirmed < Money.Coins(10m));
Assert.True(balance.OnchainBalance.Confirmed > Money.Coins(0.010m));
}
break;
case LndHubLightningClient _:
balance = await client.GetBalance();
// onchain
Assert.Null(balance.OnchainBalance);
// offchain
Assert.NotNull(balance.OffchainBalance);
Assert.Null(balance.OffchainBalance.Opening);
Assert.InRange(balance.OffchainBalance.Local, lowerBound, upperBound);
Assert.Null(balance.OffchainBalance.Remote);
Assert.Null(balance.OffchainBalance.Closing);
break;
default:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await client.GetBalance();
});
break;
}
}
}
private string Pretty(LightningNodeBalance balance)
{
return $"Confirmed:{balance.OnchainBalance.Confirmed}, Local:{balance.OffchainBalance.Local}";
}
[Fact(Timeout = Timeout)]
public async Task CanHandleSelfPayment()
{
await WaitServersAreUp();
foreach (var client in Tester.GetLightningClients())
{
Logs.Tester.LogInformation($"{client.Name}: {nameof(CanHandleSelfPayment)}");
var expiry = TimeSpan.FromSeconds(5000);
var amount = LightMoney.Satoshis(21);
var invoice = await client.Client.CreateInvoice(amount, "CanHandleSelfPayment", expiry);
switch (client.Client)
{
case LndClient _:
case EclairLightningClient _:
var response = await client.Client.Pay(invoice.BOLT11);
Assert.Equal(PayResult.CouldNotFindRoute, response.Result);
break;
case CLightningClient _:
case LndHubLightningClient _:
// The senders LNDhub wallet needs some initial funds.
if (client.Client is LndHubLightningClient)
await FundLndHubWallet(client.Client, amount + 10);
// LNDhub handles self-payment internally
var res = await client.Client.Pay(invoice.BOLT11);
Assert.Equal(PayResult.Ok, res.Result);
Assert.Equal(amount, res.Details.TotalAmount);
Assert.Equal(0, res.Details.FeeAmount);
break;
default:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await client.Client.Pay(invoice.BOLT11);
});
break;
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanHandleKeysend()
{
await WaitServersAreUp();
foreach (var test in Tester.GetTestedPairs())
{
await EnsureConnectedToDestinations(test);
var src = test.Customer;
var dest = test.Merchant;
var info = await dest.GetInfo();
var node = info.NodeInfoList.First();
var amount = LightMoney.Satoshis(21);
var preimage = new byte[32];
new Random().NextBytes(preimage);
var paymentHash = new uint256(Hashes.SHA256(preimage));
// https://github.com/satoshisstream/satoshis.stream/blob/main/TLV_registry.md
var val5482373484 = Encoders.Base64.EncodeData(preimage);
var val696969 = Encoders.Base64.EncodeData(Encoding.Default.GetBytes("123456"));
var val112111100 = Encoders.Base64.EncodeData(Encoding.Default.GetBytes("wal_hrDHs0RBEM576"));
var tlvData = new Dictionary<ulong, string>
{
{ 696969, val696969 },
{ 112111100, val112111100 },
{ 5482373484, val5482373484 }
};
var param = new PayInvoiceParams
{
Destination = node.NodeId,
PaymentHash = paymentHash,
Amount = amount,
CustomRecords = tlvData
};
Logs.Tester.LogInformation($"Test {src.GetType()}");
switch (src)
{
case LndClient _:
case CLightningClient _:
case EclairLightningClient _:
var response = await src.Pay(param);
Assert.Equal(PayResult.Ok, response.Result);
Assert.Null(response.ErrorDetail);
Assert.NotNull(response.Details.PaymentHash);
var h1 = new uint256(Hashes.SHA256(response.Details.Preimage.ToBytes(false)), false);
var h2 = response.Details.PaymentHash;
Assert.Equal(h1, h2);
var invoice = await dest.GetInvoice(response.Details.PaymentHash);
Assert.NotNull(invoice);
break;
default:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await src.Pay(param);
});
break;
}
// Check the custom records are present in the invoice
// Only works for LND right now, Core Lightning support might come, see:
// https://github.com/ElementsProject/lightning/issues/4470#issuecomment-873599548
if (src is LndClient)
{
var invoiceId = Encoders.Hex.EncodeData(paymentHash.ToBytes());
var invoice = await dest.GetInvoice(invoiceId);
Assert.NotNull(invoice.CustomRecords);
Assert.Contains(invoice.CustomRecords, pair => pair.Key == 696969 && pair.Value == val696969);
Assert.Contains(invoice.CustomRecords, pair => pair.Key == 112111100 && pair.Value == val112111100);
Assert.Contains(invoice.CustomRecords, pair => pair.Key == 5482373484 && pair.Value == val5482373484);
}
}
}
private async Task WaitServersAreUp()
{
var clients = Tester.GetLightningClients().Select(c => WaitServersAreUp(c.Name, c.Client)).ToArray();
await Task.WhenAll(clients);
}
private async Task WaitServersAreUp(string name, ILightningClient client)
{
var rpc = await GetRPCClient();
await rpc.GenerateAsync(1);
Exception realException = null;
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(Timeout - 5)))
{
retry:
try
{
if (realException != null)
await Task.Delay(1000, cts.Token);
await client.GetInfo(cts.Token);
Logs.Tester.LogInformation($"{name}: Server is up");
}
catch (Exception ex) when (!cts.IsCancellationRequested)
{
realException = ex;
goto retry;
}
catch (Exception)
{
if (realException != null)
Logs.Tester.LogInformation(realException.ToString());
Assert.Fail($"{name}: The server could not be started");
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanPayInvoiceAndReceive()
{
foreach (var test in Tester.GetTestedPairs())
{
Logs.Tester.LogInformation($"{test.Name}: {nameof(CanPayInvoiceAndReceive)}");
await EnsureConnectedToDestinations(test);
var expiry = TimeSpan.FromSeconds(5000);
var amount = LightMoney.Satoshis(21);
var invoice = await test.Merchant.CreateInvoice(amount, "CanPayInvoiceAndReceive", expiry);
Assert.NotNull(invoice.Id);
Assert.NotNull(invoice.PaymentHash);
Assert.Null(invoice.PaidAt);
var invoiceFetchedById = await test.Merchant.GetInvoice(invoice.Id);
Assert.NotNull(invoiceFetchedById);
Assert.Equal(invoice.Id, invoiceFetchedById.Id);
Assert.Equal(invoice.PaymentHash, invoiceFetchedById.PaymentHash);
var invoiceFetchedByPaymentHash = await test.Merchant.GetInvoice(invoice.PaymentHash);
Assert.NotNull(invoiceFetchedByPaymentHash);
Assert.Equal(invoice.Id, invoiceFetchedByPaymentHash.Id);
Assert.Equal(invoice.PaymentHash, invoiceFetchedByPaymentHash.PaymentHash);
if (test.Customer is LndHubLightningClient)
{
// The senders LNDhub wallet needs some initial funds.
await FundLndHubWallet(test.Customer, LightMoney.Satoshis(2100));
}
using var listener = await test.Merchant.Listen();
var waiting = listener.WaitInvoice(default);
var paidReply = await test.Customer.Pay(invoice.BOLT11);
var paidInvoice = await GetPaidInvoice(listener, waiting, invoice.Id);
Assert.Equal(PayResult.Ok, paidReply.Result);
Assert.Equal(amount, paidReply.Details.TotalAmount);
Assert.Equal(0, paidReply.Details.FeeAmount);
Assert.Equal(LightningInvoiceStatus.Paid, paidInvoice.Status);
Assert.Equal(amount, paidInvoice.Amount);
Assert.Equal(amount, paidInvoice.AmountReceived);
Assert.NotNull(paidReply.Details.Preimage);
Assert.NotNull(paidReply.Details.PaymentHash);
Assert.NotNull(paidInvoice.Id);
Assert.NotNull(paidInvoice.PaymentHash);
Assert.NotNull(paidInvoice.PaidAt);
Assert.Equal(paidInvoice.PaymentHash, paidReply.Details.PaymentHash.ToString());
if (test.Customer is not LndHubLightningClient)
{
// LNDhub doesn't have the preimage in the invoice response
Assert.NotNull(paidInvoice.Preimage);
Assert.Equal(paidInvoice.Preimage, paidReply.Details.Preimage.ToString());
}
// check payment hash corresponds to preimage
var hashedPreimage = new uint256(Hashes.SHA256(paidReply.Details.Preimage.ToBytes(false)), false);
Assert.Equal(hashedPreimage, paidReply.Details.PaymentHash);
await Task.Delay(1000);
// check invoices lists: not present in pending, but in general list
var onlyPending = new ListInvoicesParams { PendingOnly = true };
var invoices = await test.Merchant.ListInvoices(onlyPending);
Assert.DoesNotContain(invoices, i => i.Id == paidInvoice.Id);
invoices = await test.Merchant.ListInvoices();
// if the test ran too many times the invoice might be on a later page
if (invoices.Length < 100)
Assert.Contains(invoices, i => i.Id == paidInvoice.Id);
// check payment
var hash = GetInvoicePaymentHash(invoice).ToString();
var payment = await GetInvoicePayment(invoice, test.Customer);
await test.Customer.GetPayment(hash);
Assert.Null(await test.Customer.GetPayment(new uint256(0).ToString()));
Assert.Equal(hash, payment.PaymentHash);
Assert.Equal(amount, payment.Amount);
Assert.Equal(amount, payment.AmountSent);
Assert.Equal(0, payment.Fee);
Assert.Equal(LightningPaymentStatus.Complete, payment.Status);
// check payments lists
if (test.Customer is not EclairLightningClient)
{
var payments = await test.Customer.ListPayments();
Assert.Contains(payments, p => p.PaymentHash == payment.PaymentHash);
}
else
{
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await test.Customer.ListPayments();
});
}
// record this timestamp now to use it later as an offset in the second payments list check
// the delay is needed to reliably mark the line between previous and upcoming payments
var offsetIndex = DateTimeOffset.Now.ToUnixTimeMilliseconds();
await Task.Delay(1000);
// with max fee percent
var invoiceMaxFeePercent = await test.Merchant.CreateInvoice(amount, "CanPayInvoiceWithMaxFeeAndReceivePercent", expiry);
paidReply = await test.Customer.Pay(invoiceMaxFeePercent.BOLT11, new PayInvoiceParams { MaxFeePercent = 6.15f });
Assert.Equal(PayResult.Ok, paidReply.Result);
Assert.Equal(amount, paidReply.Details.TotalAmount);
Assert.Equal(0, paidReply.Details.FeeAmount);
var paymentMaxFeePercent = await GetInvoicePayment(invoiceMaxFeePercent, test.Customer);
var paymentMaxFeePercentHash = GetInvoicePaymentHash(invoiceMaxFeePercent).ToString();
Assert.Equal(paymentMaxFeePercentHash, paymentMaxFeePercent.PaymentHash);
Assert.Equal(amount, paymentMaxFeePercent.Amount);
Assert.Equal(amount, paymentMaxFeePercent.AmountSent);
Assert.Equal(0, paymentMaxFeePercent.Fee);
// with fee below 1%
var invoiceMaxFeePercent2 = await test.Merchant.CreateInvoice(amount, "CanPayInvoiceWithMaxFeeAndReceivePercent2", expiry);
paidReply = await test.Customer.Pay(invoiceMaxFeePercent2.BOLT11, new PayInvoiceParams { MaxFeePercent = 0.5f });
Assert.Equal(PayResult.Ok, paidReply.Result);
Assert.Equal(amount, paidReply.Details.TotalAmount);
Assert.Equal(0, paidReply.Details.FeeAmount);
var paymentMaxFeePercent2 = await GetInvoicePayment(invoiceMaxFeePercent2, test.Customer);
var paymentMaxFeePercent2Hash = GetInvoicePaymentHash(invoiceMaxFeePercent2).ToString();
Assert.Equal(paymentMaxFeePercent2Hash, paymentMaxFeePercent2.PaymentHash);
Assert.Equal(amount, paymentMaxFeePercent2.Amount);
Assert.Equal(amount, paymentMaxFeePercent2.AmountSent);
Assert.Equal(0, paymentMaxFeePercent2.Fee);
// with max fee limit
var invoiceMaxFeeLimit = await test.Merchant.CreateInvoice(amount, "CanPayInvoiceWithMaxFeeAndReceiveLimit", expiry);
paidReply = await test.Customer.Pay(invoiceMaxFeeLimit.BOLT11, new PayInvoiceParams { MaxFeeFlat = Money.Satoshis(100) });
Assert.Equal(PayResult.Ok, paidReply.Result);
Assert.Equal(amount, paidReply.Details.TotalAmount);
Assert.Equal(0, paidReply.Details.FeeAmount);
var paymentMaxFeeLimit = await GetInvoicePayment(invoiceMaxFeeLimit, test.Customer);
var paymentMaxFeeLimitHash = GetInvoicePaymentHash(invoiceMaxFeeLimit).ToString();
Assert.Equal(paymentMaxFeeLimitHash, paymentMaxFeeLimit.PaymentHash);
Assert.Equal(amount, paymentMaxFeeLimit.Amount);
Assert.Equal(amount, paymentMaxFeeLimit.AmountSent);
Assert.Equal(0, paymentMaxFeeLimit.Fee);
// with zero/explicit amount
if (test.Customer is LndHubLightningClient)
{
await Assert.ThrowsAsync<LndHubClient.LndHubApiException>(async () =>
{
await test.Merchant.CreateInvoice(LightMoney.Zero, "CanPayInvoiceWithZeroAmount", expiry);
});
}
else
{
var invoiceZeroAmount = await test.Merchant.CreateInvoice(LightMoney.Zero, "CanPayInvoiceWithZeroAmount", expiry);
paidReply = await test.Customer.Pay(invoiceZeroAmount.BOLT11, new PayInvoiceParams { Amount = amount });
Assert.Equal(PayResult.Ok, paidReply.Result);
Assert.Equal(amount, paidReply.Details.TotalAmount);
Assert.Equal(0, paidReply.Details.FeeAmount);
}
// check payments lists with offset
if (test.Customer is not EclairLightningClient)
{
var param = new ListPaymentsParams { IncludePending = true, OffsetIndex = offsetIndex };
var payments = await test.Customer.ListPayments(param);
Assert.InRange(payments.Length, 3, 4);
// Initial payment should be skipped because of offset
Assert.DoesNotContain(payments, p => p.PaymentHash == payment.PaymentHash);
// Later payments should be included
Assert.Contains(payments, p => p.PaymentHash == paymentMaxFeePercent.PaymentHash);
Assert.Contains(payments, p => p.PaymentHash == paymentMaxFeePercent2.PaymentHash);
Assert.Contains(payments, p => p.PaymentHash == paymentMaxFeeLimit.PaymentHash);
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanSendCorrectConnectError()
{
foreach (var test in Tester.GetTestedPairs())
{
await EnsureConnectedToDestinations(test);
var src = test.Customer;
var dest = test.Merchant;
var info = await dest.GetInfo();
var node = info.NodeInfoList.First();
switch (src)
{
case LndHubLightningClient _:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await src.ConnectTo(node);
});
break;
default:
// Reconnecting to same node should be no op
Assert.Equal(ConnectionResult.Ok, await src.ConnectTo(node));
Assert.Equal(ConnectionResult.CouldNotConnect, await src.ConnectTo(new NodeInfo(new Key().PubKey, "127.0.0.2", node.Port)));
Assert.Equal(ConnectionResult.CouldNotConnect, await src.ConnectTo(new NodeInfo(new Key().PubKey, node.Host, node.Port)));
break;
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanGetDepositAddress()
{
await WaitServersAreUp();
foreach (var client in Tester.GetLightningClients())
{
Logs.Tester.LogInformation($"{client.Name}: {nameof(CanGetDepositAddress)}");
try
{
var address = await client.Client.GetDepositAddress();
Assert.NotNull(address);
}
catch (NotSupportedException)
{
Logs.Tester.LogInformation($"{client.Name}: {nameof(CanGetDepositAddress)} not supported");
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanWaitListenInvoice()
{
foreach (var test in Tester.GetTestedPairs())
{
await EnsureConnectedToDestinations(test);
var amount1 = new LightMoney(6150);
var amount2 = new LightMoney(8251);
var src = test.Customer;
var dest = test.Merchant;
Logs.Tester.LogInformation($"{test.Name}: {nameof(CanWaitListenInvoice)}");
var merchantInvoice1 = await dest.CreateInvoice(amount1, "Hello world", TimeSpan.FromSeconds(3600));
Logs.Tester.LogInformation($"{test.Name}: Created invoice {merchantInvoice1.Id}");
var merchantInvoice2 = await dest.CreateInvoice(amount2, "Hello world", TimeSpan.FromSeconds(3600));
Logs.Tester.LogInformation($"{test.Name}: Created invoice {merchantInvoice2.Id}");
var waitToken = default(CancellationToken);
var listener = await dest.Listen(waitToken);
var waitTask = listener.WaitInvoice(waitToken);
if (src is LndHubLightningClient)
{
// Change amounts to whole sats for comparison - LNDhub only returns sats
amount1 = LightMoney.Satoshis(6);
amount2 = LightMoney.Satoshis(8);
// The senders LNDhub wallet needs some initial funds.
await FundLndHubWallet(src, LightMoney.Satoshis(2100));
}
var payResponse = await src.Pay(merchantInvoice1.BOLT11, waitToken);
Assert.Equal(PayResult.Ok, payResponse.Result);
AssertEqual(amount1, payResponse.Details.TotalAmount);
Logs.Tester.LogInformation($"{test.Name}: Paid invoice {merchantInvoice1.Id}");
var invoice = await waitTask;
Logs.Tester.LogInformation($"{test.Name}: Notification received for {invoice.Id}");
Assert.Equal(invoice.Id, merchantInvoice1.Id);
Assert.True(invoice.PaidAt.HasValue);
AssertEqual(amount1, invoice.AmountReceived);
var waitTask2 = listener.WaitInvoice(waitToken);
payResponse = await src.Pay(merchantInvoice2.BOLT11, waitToken);
AssertEqual(amount2, payResponse.Details.TotalAmount);
Logs.Tester.LogInformation($"{test.Name}: Paid invoice {merchantInvoice2.Id}");
invoice = await waitTask2;
Logs.Tester.LogInformation($"{test.Name}: Notification received for {invoice.Id}");
Assert.True(invoice.PaidAt.HasValue);
AssertEqual(invoice.Amount, invoice.AmountReceived);
Assert.Equal(invoice.Id, merchantInvoice2.Id);
AssertEqual(amount2, invoice.AmountReceived);
var waitTask3 = listener.WaitInvoice(waitToken);
await Task.Delay(100, waitToken);
listener.Dispose();
Logs.Tester.LogInformation($"{test.Name}: Listener disposed, should throw exception");
await Assert.ThrowsAsync<OperationCanceledException>(async () => await waitTask3);
}
}
private static void AssertEqual(LightMoney a, LightMoney b)
{
Assert.Equal(a.ToDecimal(LightMoneyUnit.Satoshi), b.ToDecimal(LightMoneyUnit.Satoshi), 2);
}
[Fact]
public void LightMoneyOverflowTest()
{
var maxSupply = 21_000_000m;
var v = LightMoney.Coins(maxSupply);
Assert.Equal(v, LightMoney.Satoshis(maxSupply * (decimal)Math.Pow(10, 8)));
Assert.Equal(v, LightMoney.Bits(maxSupply * (decimal)Math.Pow(10, 6)));
Assert.Equal(v, LightMoney.MilliSatoshis((long)(maxSupply * (decimal)Math.Pow(10, 11))));
}
[Fact(Timeout = Timeout)]
public async Task DoNotReportOkIfChannelCantConnect()
{
foreach (var client in Tester.GetTestedPairs())
{
await EnsureConnectedToDestinations(client);
Logs.Tester.LogInformation(client.Customer.GetType().Name);
switch (client.Customer)
{
case LndHubLightningClient _:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await client.Customer.ConnectTo(new NodeInfo(new Key().PubKey, "127.0.0.1", 64_000));
});
break;
default:
var result = await client.Customer.ConnectTo(new NodeInfo(new Key().PubKey, "127.0.0.1", 64_000));
Assert.Equal(ConnectionResult.CouldNotConnect, result);
var ni = (await client.Merchant.GetInfo()).NodeInfoList.FirstOrDefault();
Assert.Equal(ConnectionResult.Ok, await client.Customer.ConnectTo(ni));
break;
}
}
}
[Fact(Timeout = Timeout)]
public async Task CanListChannels()
{
// for channel values to be in reasonable bounds
// The initial funding by `EnsureConnectedToDestinations` is 90% to the sender, 10% to destination
var capacity = 0.16777215m;
var funding = capacity * 0.9m;
var lowerBound = funding * 0.9m;
var upperBound = funding * 1.10m;
foreach (var test in Tester.GetTestedPairs())
{
switch (test.Customer)
{
case LndHubLightningClient _:
await Assert.ThrowsAsync<NotSupportedException>(async () =>
{
await test.Customer.ListChannels();
});
break;
default:
await EnsureConnectedToDestinations(test);
var senderChannels = await test.Customer.ListChannels();
var senderInfo = await test.Customer.GetInfo();
Assert.NotEmpty(senderChannels);
Assert.Single(senderChannels.Where(s => s.IsActive));
var destChannels = await test.Merchant.ListChannels();
var destInfo = await test.Merchant.GetInfo();
Assert.NotEmpty(destChannels);
Assert.Single(destChannels.GroupBy(s => s.RemoteNode));
foreach (var c in senderChannels)
{
Assert.NotNull(c.RemoteNode);
Assert.True(c.IsPublic);
Assert.True(c.IsActive);
Assert.NotNull(c.ChannelPoint);
Assert.Equal(capacity, c.Capacity.ToDecimal(LightMoneyUnit.BTC));
Assert.InRange(c.LocalBalance.ToDecimal(LightMoneyUnit.BTC), lowerBound, upperBound);
}
Assert.Contains(senderChannels, c => c.RemoteNode.Equals(destInfo.NodeInfoList.FirstOrDefault()?.NodeId));
Assert.Contains(destChannels, c => c.RemoteNode.Equals(senderInfo.NodeInfoList.FirstOrDefault()?.NodeId));
break;
}
}
}
[Fact]
public void CanParseBOLT11()
{
var key = new Key(Encoders.Hex.DecodeData("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734"));
var p = BOLT11PaymentRequest.Parse("lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcqca784w", Network.Main);
Assert.Equal("lnbc", p.Prefix);
Assert.Equal(LightMoney.Zero, p.MinimumAmount);
Assert.Equal(1496314658UL, Utils.DateTimeToUnixTime(p.Timestamp));
Assert.Equal(1496314658UL + 60 * 60, Utils.DateTimeToUnixTime(p.ExpiryDate));
Assert.Equal("0001020304050607080900010203040506070809000102030405060708090102", p.PaymentHash.ToString());
Assert.Equal("Please consider supporting this project", p.ShortDescription);
Assert.Null(p.PaymentSecret);
Assert.True(p.FeatureBits.HasFlag(FeatureBits.None));
var preimage = Encoders.Hex.DecodeData("6c6e62630b25fe64410d00004080c1014181c20240004080c1014181c20240004080c1014181c202404081a1fa83632b0b9b29031b7b739b4b232b91039bab83837b93a34b733903a3434b990383937b532b1ba0");
var hash = new uint256(Encoders.Hex.DecodeData("c3d4e83f646fa79a393d75277b1d858db1d1f7ab7137dcb7835db2ecd518e1c9"));
Assert.Equal(hash, new uint256(Hashes.SHA256(preimage)));
Assert.Equal(hash, p.Hash);
Assert.True(key.PubKey.Verify(hash, p.ECDSASignature));
Assert.True(p.GetPayeePubKey().Verify(hash, p.ECDSASignature));
Assert.True(p.VerifySignature());
Assert.Equal(key.PubKey, p.GetPayeePubKey());
p = BOLT11PaymentRequest.Parse("lnbcrt20u1psd66dppp5m4ughz9keyptj80qcn35cx9w52p7gc8eyx4m6y5456jlhm04wfvsdqqcqzpgxqyz5vqsp5pdsxhsnrs69n940373fnec2zxw5yzlksnev40ejcq39lnju5lt3s9qyyssqpq760qvf46y3cch948wau8e5ym0zungnqfvdx5wruy6f0hru2pp9txtc9up2lfc439a2xuz6nvgjw40vsddhywjpc5qmm0q3dj4m3dcqxzjjeg", Network.RegTest);
Assert.Equal("lnbcrt", p.Prefix);
Assert.Equal(40, p.MinFinalCLTVExpiry);
Assert.Equal(LightMoney.FromUnit(2000m, LightMoneyUnit.Satoshi), p.MinimumAmount);
Assert.Equal(LightMoney.FromUnit(2000m * 0.00000001m, LightMoneyUnit.BTC), p.MinimumAmount);
Assert.Equal(1625123233UL, Utils.DateTimeToUnixTime(p.Timestamp));
Assert.Equal(1625123233UL + 24 * 60 * 60, Utils.DateTimeToUnixTime(p.ExpiryDate));
Assert.Equal("dd788b88b6c902b91de0c4e34c18aea283e460f921abbd1295a6a5fbedf57259", p.PaymentHash.ToString());
Assert.Equal("", p.ShortDescription);
Assert.Equal("0b606bc263868b32d5f1f4533ce14233a8417ed09e5957e658044bf9cb94fae3", p.PaymentSecret.ToString());
Assert.True((p.FeatureBits | (FeatureBits.MPPOptional | FeatureBits.PaymentAddrRequired | FeatureBits.TLVOnionPayloadOptional)) ==
(FeatureBits.MPPOptional | FeatureBits.PaymentAddrRequired | FeatureBits.TLVOnionPayloadOptional));
Assert.True(p.VerifySignature());
p = BOLT11PaymentRequest.Parse("lnbcrt1p3w0278pp5n8ky9m98m7ppjw4gr4mhgvxhm8r0830w870raccvu6tgnavrs60sdqqcqzpgxqyz5vqsp5v50decplk2phztne8xqjxyvrrr2k0nf2q5sflnn4vqc6mc048fds9q2gqqqqqyssqk6tlvhclzm7ejg6p8szg34tt28puz5hvmuv93rkhnaq63t6k92sk0q2g7arltwqahvhg3ks6l922zsdtnf7wt540ypmqqulzvke8gyqqttv7fu", Network.RegTest);
Assert.True((p.FeatureBits | (FeatureBits.MPPOptional | FeatureBits.PaymentAddrRequired | FeatureBits.TLVOnionPayloadOptional | FeatureBits.PaymentMetadataRequired)) ==
(FeatureBits.MPPOptional | FeatureBits.PaymentAddrRequired | FeatureBits.TLVOnionPayloadOptional | FeatureBits.PaymentMetadataRequired));
p = BOLT11PaymentRequest.Parse("lnbc2500u1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5xysxxatsyp3k7enxv4jsxqzpuaztrnwngzn3kdzw5hydlzf03qdgm2hdq27cqv3agm2awhz5se903vruatfhq77w3ls4evs3ch9zw97j25emudupq63nyw24cg27h2rspfj9srp", Network.Main);
Assert.Equal("lnbc", p.Prefix);
Assert.Equal(9, p.MinFinalCLTVExpiry);
Assert.Equal(LightMoney.FromUnit(2500m, LightMoneyUnit.Micro), p.MinimumAmount);
Assert.Equal(LightMoney.FromUnit(2500m * 0.000001m, LightMoneyUnit.BTC), p.MinimumAmount);
Assert.Equal(1496314658UL, Utils.DateTimeToUnixTime(p.Timestamp));
Assert.Equal(1496314658UL + 60, Utils.DateTimeToUnixTime(p.ExpiryDate));
Assert.Equal("0001020304050607080900010203040506070809000102030405060708090102", p.PaymentHash.ToString());
Assert.Equal("1 cup coffee", p.ShortDescription);
Assert.True(p.VerifySignature());
// Same but with uppercase and url prefix
p = BOLT11PaymentRequest.Parse("lightniNG:LNBC2500U1PVJLUEZPP5QQQSYQCYQ5RQWZQFQQQSYQCYQ5RQWZQFQQQSYQCYQ5RQWZQFQYPQDQ5XYSXXATSYP3K7ENXV4JSXQZPUAZTRNWNGZN3KDZW5HYDLZF03QDGM2HDQ27CQV3AGM2AWHZ5SE903VRUATFHQ77W3LS4EVS3CH9ZW97J25EMUDUPQ63NYW24CG27H2RSPFJ9SRP".ToUpperInvariant(), Network.Main);
Assert.Equal("lnbc", p.Prefix);
Assert.Equal(LightMoney.FromUnit(2500m, LightMoneyUnit.Micro), p.MinimumAmount);
Assert.Equal(LightMoney.FromUnit(2500m * 0.000001m, LightMoneyUnit.BTC), p.MinimumAmount);
Assert.Equal(1496314658UL, Utils.DateTimeToUnixTime(p.Timestamp));
Assert.Equal(1496314658UL + 60, Utils.DateTimeToUnixTime(p.ExpiryDate));
Assert.Equal("0001020304050607080900010203040506070809000102030405060708090102", p.PaymentHash.ToString());
Assert.Equal("1 cup coffee", p.ShortDescription);
p = BOLT11PaymentRequest.Parse("lnbc2500u1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpquwpc4curk03c9wlrswe78q4eyqc7d8d0xqzpuyk0sg5g70me25alkluzd2x62aysf2pyy8edtjeevuv4p2d5p76r4zkmneet7uvyakky2zr4cusd45tftc9c5fh0nnqpnl2jfll544esqchsrny", Network.Main);
Assert.Equal("lnbc", p.Prefix);
Assert.Equal(LightMoney.FromUnit(2500m, LightMoneyUnit.Micro), p.MinimumAmount);
Assert.Equal(LightMoney.FromUnit(2500m * 0.000001m, LightMoneyUnit.BTC), p.MinimumAmount);
Assert.Equal(1496314658UL, Utils.DateTimeToUnixTime(p.Timestamp));
Assert.Equal(1496314658UL + 60, Utils.DateTimeToUnixTime(p.ExpiryDate));
Assert.Equal("0001020304050607080900010203040506070809000102030405060708090102", p.PaymentHash.ToString());
Assert.Equal("ナンセンス 1杯", p.ShortDescription);
hash = new uint256(Encoders.Hex.DecodeData("197a3061f4f333d86669b8054592222b488f3c657a9d3e74f34f586fb3e7931c"));
Assert.True(key.PubKey.Verify(hash, p.ECDSASignature));
Assert.Equal(key.PubKey, p.GetPayeePubKey());
p = BOLT11PaymentRequest.Parse("lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqscc6gd6ql3jrc5yzme8v4ntcewwz5cnw92tz0pc8qcuufvq7khhr8wpald05e92xw006sq94mg8v2ndf4sefvf9sygkshp5zfem29trqq2yxxz7", Network.Main);
Assert.Equal("lnbc", p.Prefix);
Assert.Equal(LightMoney.FromUnit(20m, LightMoneyUnit.MilliBTC), p.MinimumAmount);
Assert.Equal(LightMoney.FromUnit(20m * 0.001m, LightMoneyUnit.BTC), p.MinimumAmount);
Assert.Equal(1496314658UL, Utils.DateTimeToUnixTime(p.Timestamp));
Assert.Equal(1496314658UL + 60 * 60, Utils.DateTimeToUnixTime(p.ExpiryDate));
Assert.Equal("0001020304050607080900010203040506070809000102030405060708090102", p.PaymentHash.ToString());
Assert.Null(p.ShortDescription);
hash = new uint256(Encoders.Hex.DecodeData("b6025e8a10539dddbcbe6840a9650707ae3f147b8dcfda338561ada710508916"));
Assert.Equal(hash, p.Hash);
Assert.True(key.PubKey.Verify(hash, p.ECDSASignature));
Assert.True(p.VerifySignature());
Assert.Equal(key.PubKey, p.GetPayeePubKey());
Assert.True(p.VerifyDescriptionHash("One piece of chocolate cake, one icecream cone, one pickle, one slice of swiss cheese, one slice of salami, one lollypop, one piece of cherry pie, one sausage, one cupcake, and one slice of watermelon"));
Assert.False(p.VerifyDescriptionHash("lol"));
p = BOLT11PaymentRequest.Parse("lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqscc6gd6ql3jrc5yzme8v4ntcewwz5cnw92tz0pc8qcuufvq7khhr8wpald05e92xw006sq94mg8v2ndf4sefvf9sygkshp5zfem29trqq2yxxz7", Network.Main);
Assert.Equal("3925b6f67e2c340036ed12093dd44e0368df1b6ea26c53dbe4811f58fd5db8c1", p.DescriptionHash.ToString());
p = BOLT11PaymentRequest.Parse("lntb20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqfpp3x9et2e20v6pu37c5d9vax37wxq72un98kmzzhznpurw9sgl2v0nklu2g4d0keph5t7tj9tcqd8rexnd07ux4uv2cjvcqwaxgj7v4uwn5wmypjd5n69z2xm3xgksg28nwht7f6zspwp3f9t", Network.TestNet);
hash = new uint256(Encoders.Hex.DecodeData("00c17b39642becc064615ef196a6cc0cce262f1d8dde7b3c23694aeeda473abe"));
Assert.True(key.PubKey.Verify(hash, p.ECDSASignature));
Assert.True(p.VerifySignature());
Assert.Equal("mk2QpYatsKicvFVuTAQLBryyccRXMUaGHP", p.FallbackAddresses[0].ToString());
p = BOLT11PaymentRequest.Parse("lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqfppj3a24vwu6r8ejrss3axul8rxldph2q7z9kmrgvr7xlaqm47apw3d48zm203kzcq357a4ls9al2ea73r8jcceyjtya6fu5wzzpe50zrge6ulk4nvjcpxlekvmxl6qcs9j3tz0469gq5g658y", Network.Main);
hash = new uint256(Encoders.Hex.DecodeData("64f1ff500bcc62a1b211cd6db84a1d93d1f77c6a132904465b6ff912420176be"));
Assert.True(key.PubKey.Verify(hash, p.ECDSASignature));
Assert.True(p.VerifySignature());
Assert.Equal(key.PubKey, p.GetPayeePubKey());
Assert.Equal("3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX", p.FallbackAddresses[0].ToString());
p = BOLT11PaymentRequest.Parse("lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqfppqw508d6qejxtdg4y5r3zarvary0c5xw7kepvrhrm9s57hejg0p662ur5j5cr03890fa7k2pypgttmh4897d3raaq85a293e9jpuqwl0rnfuwzam7yr8e690nd2ypcq9hlkdwdvycqa0qza8", Network.Main);
Assert.Equal("lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqfppqw508d6qejxtdg4y5r3zarvary0c5xw7kepvrhrm9s57hejg0p662ur5j5cr03890fa7k2pypgttmh4897d3raaq85a293e9jpuqwl0rnfuwzam7yr8e690nd2ypcq9hlkdwdvycqa0qza8", p.ToString());
hash = new uint256(Encoders.Hex.DecodeData("b3df27aaa01d891cc9de272e7609557bdf4bd6fd836775e4470502f71307b627"));
Assert.True(key.PubKey.Verify(hash, p.ECDSASignature));
Assert.True(p.VerifySignature());
Assert.Equal(key.PubKey, p.GetPayeePubKey());
Assert.Equal("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", p.FallbackAddresses[0].ToString());
p = BOLT11PaymentRequest.Parse("lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqfp4qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q28j0v3rwgy9pvjnd48ee2pl8xrpxysd5g44td63g6xcjcu003j3qe8878hluqlvl3km8rm92f5stamd3jw763n3hck0ct7p8wwj463cql26ava", Network.Main);
hash = new uint256(Encoders.Hex.DecodeData("399a8b167029fda8564fd2e99912236b0b8017e7d17e416ae17307812c92cf42"));
Assert.True(key.PubKey.Verify(hash, p.ECDSASignature));
Assert.True(p.VerifySignature());
Assert.Equal(key.PubKey, p.GetPayeePubKey());
Assert.Equal("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", p.FallbackAddresses[0].ToString());
p = BOLT11PaymentRequest.Parse("lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqsfpp3qjmp7lwpagxun9pygexvgpjdc4jdj85fr9yq20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqafqxu92d8lr6fvg0r5gv0heeeqgcrqlnm6jhphu9y00rrhy4grqszsvpcgpy9qqqqqqgqqqqq7qqzqj9n4evl6mr5aj9f58zp6fyjzup6ywn3x6sk8akg5v4tgn2q8g4fhx05wf6juaxu9760yp46454gpg5mtzgerlzezqcqvjnhjh8z3g2qqdhhwkj", Network.Main);
hash = new uint256(Encoders.Hex.DecodeData("ff68246c5ad4b48c90cf8ff3b33b5cea61e62f08d0e67910ffdce1edecade71b"));
Assert.True(p.VerifySignature());
Assert.Equal(key.PubKey, p.GetPayeePubKey());
Assert.Equal("1RustyRX2oai4EYYDpQGWvEL62BBGqN9T", p.FallbackAddresses[0].ToString());
Assert.Single(p.Routes);
Assert.Equal(2, p.Routes[0].Hops.Count);
Assert.Equal("029e03a901b85534ff1e92c43c74431f7ce72046060fcf7a95c37e148f78c77255", p.Routes[0].Hops[0].PubKey.ToString());
Assert.Equal("0102030405060708", p.Routes[0].Hops[0].ShortChannelId);
Assert.Equal(LightMoney.FromUnit(1m, LightMoneyUnit.MilliSatoshi), p.Routes[0].Hops[0].FeeBase);
Assert.Equal(20m / 1_000_000m, p.Routes[0].Hops[0].FeeProportional);
Assert.Equal(3, p.Routes[0].Hops[0].CLTVExpiryDelay);
Assert.Equal("039e03a901b85534ff1e92c43c74431f7ce72046060fcf7a95c37e148f78c77255", p.Routes[0].Hops[1].PubKey.ToString());