Skip to content

Commit

Permalink
Features/polly (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
using-system authored Aug 5, 2021
1 parent 9fec623 commit c1a3378
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Or with custodial solution for lightning (without having a node of your own) :
- [x] LNDHub / BlueWallet ([Documentation](documentation/client-lndhub.md))
- [x] LNBits ([Documentation](documentation/client-lnbits.md))

## Extend
## Extensible

With `LightningPay`, you can easly extend your client by add extension methods to the `ILightningClient` interface.

Expand Down
1 change: 1 addition & 0 deletions documentation/client-eclair.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The `AddEclairLightningClient` method has optionnal pamameters to configure your
| --------------------- | -------- | -------- | ------------------------------------------------------------ |
| address | `Uri` | Yes | Address of your node server with port (example : http://localhost:8080/) |
| password | `String` | No | Eclair api password |
| retryOnHttpError | `int` | No | Number of retry on http error |
| certificateThumbprint | `String` | No | Certificate thumbprint used for your https address if the certificate is not public<br />Ex : "284800A04D0C046636EBE60C37A4F527B8B550F3" |
| allowInsecure | `bool` | No | If you use https address, determine if you allow non secure transport (certificateThumbprint parameter will be ignored) |

Expand Down
1 change: 1 addition & 0 deletions documentation/client-lnbits.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The `AddLNBitsLightningClient` method has optionnal pamameters to configure your
| --------------------- | -------- | -------- | ------------------------------------------------------------ |
| address | `Uri` | Yes | Address of the LNBits api (example : https://lnbits.com/) |
| apiKey | `String` | Yes | LNBits api key |
| retryOnHttpError | `int` | No | Number of retry on http error |
| certificateThumbprint | `String` | No | Certificate thumbprint used for your https address if the certificate is not public<br />Ex : "284800A04D0C046636EBE60C37A4F527B8B550F3" |
| allowInsecure | `bool` | No | If you use https address, determine if you allow non secure transport (certificateThumbprint parameter will be ignored) |

Expand Down
1 change: 1 addition & 0 deletions documentation/client-lnd.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The `AddLndLightningClient` method has optionnal pamameters to configure your cl
| address | `Uri` | Yes | Address of your node server with port (example : http://localhost:8080/) |
| macaroonHexString | `String` | No | Authentication assertion in hex string format<br /><u>Tip :</u> To get the hex string of your, type the command xxd -p -c2000 admin.macaroon to get the hex representation of your file. |
| macaroonBytes | `byte[]` | No | Authentication assertion in Byte array (to load macaron from file with .NET code `File.ReadAllBytes(macaroonFilePath)` ) |
| retryOnHttpError | `int` | No | Number of retry on http error |
| certificateThumbprint | `String` | No | Certificate thumbprint used for your https address if the certificate is not public<br />Ex : "284800A04D0C046636EBE60C37A4F527B8B550F3" |
| allowInsecure | `bool` | No | If you use https address, determine if you allow non secure transport (certificateThumbprint parameter will be ignored) |

Expand Down
1 change: 1 addition & 0 deletions documentation/client-lndhub.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The `AddLndHubLightningClient` method has optionnal pamameters to configure your
| address | `Uri` | Yes | Address of the LNDHub api (example : https://lndhub.herokuapp.com/) |
| login | `String` | Yes | LNDHub login |
| password | `String` | Yes | LNDHub Password |
| retryOnHttpError | `int` | No | Number of retry on http error |
| certificateThumbprint | `String` | No | Certificate thumbprint used for your https address if the certificate is not public<br />Ex : "284800A04D0C046636EBE60C37A4F527B8B550F3" |
| allowInsecure | `bool` | No | If you use https address, determine if you allow non secure transport (certificateThumbprint parameter will be ignored) |

Expand Down
2 changes: 1 addition & 1 deletion samples/LightningPay.Samples.WebAppMvc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void ConfigureServices(IServiceCollection services)
*/

services.AddLndLightningClient(new Uri("tcp://127.0.0.1:9835"));
services.AddLndLightningClient(new Uri("http://localhost:32736/"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

using Microsoft.Extensions.DependencyInjection;

using Polly;
using Polly.Extensions.Http;

using LightningPay.Clients.Eclair;

namespace LightningPay
Expand Down Expand Up @@ -34,14 +37,14 @@ public static IServiceCollection AddEclairLightningClient(this IServiceCollectio
/// <param name="services">The services.</param>
/// <param name="address">The address of the LNBits api.</param>
/// <param name="password">The password.</param>
/// <param name="retryOnHttpError">Number of retry on http error</param>
/// <param name="allowInsecure">if set to <c>true</c> [allow insecure].</param>
/// <param name="certificateThumbprint">The certificate thumbprint.</param>
/// <returns>
/// ServiceCollection
/// </returns>
/// <returns>ServiceCollection</returns>
public static IServiceCollection AddEclairLightningClient(this IServiceCollection services,
Uri address,
string password,
int retryOnHttpError = 10,
bool allowInsecure = false,
string certificateThumbprint = null)
{
Expand All @@ -57,7 +60,11 @@ public static IServiceCollection AddEclairLightningClient(this IServiceCollectio
CertificateThumbprint = certificateThumbprint.HexStringToByteArray()
});
services.AddSingleton<DependencyInjection.DefaultHttpClientHandler>();

services.AddHttpClient<ILightningClient, EclairClient>()
.AddPolicyHandler(HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(retryOnHttpError, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))))
.ConfigurePrimaryHttpMessageHandler<DependencyInjection.DefaultHttpClientHandler>();

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

using Microsoft.Extensions.DependencyInjection;

using Polly;
using Polly.Extensions.Http;

using LightningPay.Clients.LNBits;

namespace LightningPay
Expand Down Expand Up @@ -34,6 +37,7 @@ public static IServiceCollection AddLNBitsLightningClient(this IServiceCollectio
/// <param name="services">The services.</param>
/// <param name="address">The address of the LNBits api.</param>
/// <param name="apiKey">The api key.</param>
/// <param name="retryOnHttpError">Number of retry on http error</param>
/// <param name="allowInsecure">if set to <c>true</c> [allow insecure].</param>
/// <param name="certificateThumbprint">The certificate thumbprint.</param>
/// <returns>
Expand All @@ -42,6 +46,7 @@ public static IServiceCollection AddLNBitsLightningClient(this IServiceCollectio
public static IServiceCollection AddLNBitsLightningClient(this IServiceCollection services,
Uri address,
string apiKey,
int retryOnHttpError = 10,
bool allowInsecure = false,
string certificateThumbprint = null)
{
Expand All @@ -57,7 +62,11 @@ public static IServiceCollection AddLNBitsLightningClient(this IServiceCollectio
CertificateThumbprint = certificateThumbprint.HexStringToByteArray()
});
services.AddSingleton<DependencyInjection.DefaultHttpClientHandler>();

services.AddHttpClient<ILightningClient, LNBitsClient>()
.AddPolicyHandler(HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(retryOnHttpError, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))))
.ConfigurePrimaryHttpMessageHandler<DependencyInjection.DefaultHttpClientHandler>();

return services;
Expand Down
20 changes: 17 additions & 3 deletions src/LightningPay.DependencyInjection/Extensions/LndExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Net;
using System.Net.Http;

using Microsoft.Extensions.DependencyInjection;

using LightningPay.Clients.Lnd;
using Polly;

using LightningPay.Clients.Lnd;

namespace LightningPay
{
Expand Down Expand Up @@ -50,6 +53,7 @@ public static IServiceCollection AddLndLightningClient(this IServiceCollection s
/// <param name="services">The services.</param>
/// <param name="address">The address of the LND server.</param>
/// <param name="macaroonHexString">The macaroon hexadecimal string.</param>
/// <param name="retryOnHttpError">Number of retry on http error</param>
/// <param name="allowInsecure">if set to <c>true</c> [allow insecure].</param>
/// <param name="certificateThumbprint">The certificate thumbprint.</param>
/// <returns>
Expand All @@ -58,20 +62,23 @@ public static IServiceCollection AddLndLightningClient(this IServiceCollection s
public static IServiceCollection AddLndLightningClient(this IServiceCollection services,
Uri address,
string macaroonHexString = null,
int retryOnHttpError = 10,
bool allowInsecure = false,
string certificateThumbprint = null)
{
return AddLndLightningClient(services,
address,
macaroonHexString.HexStringToByteArray(),
allowInsecure,
certificateThumbprint);
retryOnHttpError: retryOnHttpError,
allowInsecure: allowInsecure,
certificateThumbprint: certificateThumbprint);
}

/// <summary>Adds the LND lightning client.</summary>
/// <param name="services">The services.</param>
/// <param name="address">The address of the LND server.</param>
/// <param name="macaroonBytes">The macaroon bytes.</param>
/// <param name="retryOnHttpError">Number of retry on http error</param>
/// <param name="allowInsecure">if set to <c>true</c> [allow insecure].</param>
/// <param name="certificateThumbprint">The certificate thumbprint.</param>
/// <returns>
Expand All @@ -80,6 +87,7 @@ public static IServiceCollection AddLndLightningClient(this IServiceCollection s
public static IServiceCollection AddLndLightningClient(this IServiceCollection services,
Uri address,
byte[] macaroonBytes = null,
int retryOnHttpError = 10,
bool allowInsecure = false,
string certificateThumbprint = null)
{
Expand All @@ -96,7 +104,13 @@ public static IServiceCollection AddLndLightningClient(this IServiceCollection s
CertificateThumbprint = certificateThumbprint.HexStringToByteArray()
});
services.AddSingleton<DependencyInjection.DefaultHttpClientHandler>();

services.AddHttpClient<ILightningClient, LndClient>()
.AddPolicyHandler(Policy
// Add custom handling to exclude 500 because api return 500 for bad request errors
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => (int)r.StatusCode > 500 || r.StatusCode == HttpStatusCode.RequestTimeout)
.WaitAndRetryAsync(retryOnHttpError, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))))
.ConfigurePrimaryHttpMessageHandler<DependencyInjection.DefaultHttpClientHandler>();

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

using Microsoft.Extensions.DependencyInjection;

using Polly;
using Polly.Extensions.Http;

using LightningPay.Clients.LndHub;

namespace LightningPay
Expand Down Expand Up @@ -37,6 +40,7 @@ public static IServiceCollection AddLndHubLightningClient(this IServiceCollectio
/// <param name="address">The address of the LNDHub.</param>
/// <param name="login">The login.</param>
/// <param name="password">The password.</param>
/// <param name="retryOnHttpError">Number of retry on http error</param>
/// <param name="allowInsecure">if set to <c>true</c> [allow insecure].</param>
/// <param name="certificateThumbprint">The certificate thumbprint.</param>
/// <returns>
Expand All @@ -46,6 +50,7 @@ public static IServiceCollection AddLndHubLightningClient(this IServiceCollectio
Uri address,
string login,
string password,
int retryOnHttpError = 10,
bool allowInsecure = false,
string certificateThumbprint = null)
{
Expand All @@ -62,7 +67,11 @@ public static IServiceCollection AddLndHubLightningClient(this IServiceCollectio
CertificateThumbprint = certificateThumbprint.HexStringToByteArray()
});
services.AddSingleton<DependencyInjection.DefaultHttpClientHandler>();

services.AddHttpClient<ILightningClient, LndHubClient>()
.AddPolicyHandler(HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(retryOnHttpError, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))))
.ConfigurePrimaryHttpMessageHandler<DependencyInjection.DefaultHttpClientHandler>();

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.0" Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.0" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" Condition="'$(TargetFramework)' == 'net5.0'" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" Condition="'$(TargetFramework)' == 'net5.0'" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="5.0.0" Condition="'$(TargetFramework)' == 'net5.0'" />
</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System.Threading.Tasks;
using System;

using LightningPay.Clients.CLightning;
using Microsoft.Extensions.DependencyInjection;

namespace LightningPay.IntegrationTest
{
public class CLightningClientIntegrationTest : LightningClientIntegrationTestBase
{
protected override bool NeedBitcoind => true;

protected override Task<ILightningClient> GetClient()
{
ILightningClient client = CLightningClient.New("tcp://127.0.0.1:48532");

return Task.FromResult(client);
protected override void ConfigureServices(IServiceCollection services)
{
services.AddCLightningClient(new Uri("tcp://127.0.0.1:48532"));
}

protected override string SelfPaymentErrorMesssage => "Error code 210";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using System.Threading.Tasks;
using System;

using LightningPay.Clients.Eclair;
using Microsoft.Extensions.DependencyInjection;

namespace LightningPay.IntegrationTest
{
public class EclairClientIntegrationTest : LightningClientIntegrationTestBase
{
protected override bool NeedBitcoind => true;

protected override Task<ILightningClient> GetClient()
protected override void ConfigureServices(IServiceCollection services)
{
ILightningClient client = EclairClient.New("http://localhost:4570/", "eclairpassword");

return Task.FromResult(client);
services.AddEclairLightningClient(new Uri("http://localhost:4570/"), "eclairpassword");
}

protected override string SelfPaymentErrorMesssage => "Cannot process to the payment";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using System.Threading.Tasks;
using System;

using LightningPay.Clients.LNBits;
using Microsoft.Extensions.DependencyInjection;

namespace LightningPay.IntegrationTest
{
public class LNBitsClientIntegrationTest : LightningClientIntegrationTestBase
{
protected override bool NeedBitcoind => false;

protected override Task<ILightningClient> GetClient()
protected override void ConfigureServices(IServiceCollection services)
{
ILightningClient client =
LNBitsClient.New("https://lnbits.lndev.link/", apiKey: "0f920e085f96413fa754b73b9895abbd");

return Task.FromResult(client);
services.AddLNBitsLightningClient(new Uri("https://lnbits.lndev.link/"), "0f920e085f96413fa754b73b9895abbd");
}

protected override string SelfPaymentErrorMesssage => "Insufficient balance";
Expand Down
Loading

0 comments on commit c1a3378

Please sign in to comment.