| | | 1 | | using MessagePack; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace NLightning.Daemon.Ipc.Handlers; |
| | | 6 | | |
| | | 7 | | using Domain.Bitcoin.Enums; |
| | | 8 | | using Domain.Client.Constants; |
| | | 9 | | using Domain.Client.Enums; |
| | | 10 | | using Infrastructure.Bitcoin.Wallet.Interfaces; |
| | | 11 | | using Interfaces; |
| | | 12 | | using Services.Ipc.Factories; |
| | | 13 | | using Transport.Ipc; |
| | | 14 | | using Transport.Ipc.Requests; |
| | | 15 | | using Transport.Ipc.Responses; |
| | | 16 | | |
| | | 17 | | internal class GetAddressIpcHandler : IIpcCommandHandler |
| | | 18 | | { |
| | | 19 | | private readonly ILogger<GetAddressIpcHandler> _logger; |
| | | 20 | | private readonly IServiceProvider _serviceProvider; |
| | | 21 | | |
| | 0 | 22 | | public ClientCommand Command => ClientCommand.GetAddress; |
| | | 23 | | |
| | 0 | 24 | | public GetAddressIpcHandler(ILogger<GetAddressIpcHandler> logger, IServiceProvider serviceProvider) |
| | | 25 | | { |
| | 0 | 26 | | _logger = logger; |
| | 0 | 27 | | _serviceProvider = serviceProvider; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<IpcEnvelope> HandleAsync(IpcEnvelope envelope, CancellationToken ct) |
| | | 31 | | { |
| | | 32 | | try |
| | | 33 | | { |
| | | 34 | | // Deserialize the request |
| | 0 | 35 | | var request = |
| | 0 | 36 | | MessagePackSerializer.Deserialize<GetAddressIpcRequest>(envelope.Payload, cancellationToken: ct); |
| | | 37 | | |
| | 0 | 38 | | string? p2Tr = null; |
| | 0 | 39 | | string? p2Wpkh = null; |
| | | 40 | | |
| | | 41 | | // Create a scope for this call |
| | 0 | 42 | | using var scope = _serviceProvider.CreateScope(); |
| | 0 | 43 | | var walletAddressService = scope.ServiceProvider.GetService<IBitcoinWalletService>() ?? |
| | 0 | 44 | | throw new NullReferenceException( |
| | 0 | 45 | | $"Error activating service {nameof(IBitcoinWalletService)}"); |
| | | 46 | | |
| | | 47 | | // Get unused addresses by type |
| | 0 | 48 | | if (request.AddressType.HasFlag(AddressType.P2Tr)) |
| | 0 | 49 | | p2Tr = (await walletAddressService.GetUnusedAddressAsync(AddressType.P2Tr, false)).Address; |
| | | 50 | | |
| | 0 | 51 | | if (request.AddressType.HasFlag(AddressType.P2Wpkh)) |
| | 0 | 52 | | p2Wpkh = (await walletAddressService.GetUnusedAddressAsync(AddressType.P2Wpkh, false)).Address; |
| | | 53 | | |
| | | 54 | | // Create a success response |
| | 0 | 55 | | var response = new GetAddressIpcResponse |
| | 0 | 56 | | { |
| | 0 | 57 | | AddressP2Tr = p2Tr, |
| | 0 | 58 | | AddressP2Wsh = p2Wpkh |
| | 0 | 59 | | }; |
| | | 60 | | |
| | 0 | 61 | | var payload = MessagePackSerializer.Serialize(response, cancellationToken: ct); |
| | 0 | 62 | | return new IpcEnvelope |
| | 0 | 63 | | { |
| | 0 | 64 | | Version = envelope.Version, |
| | 0 | 65 | | Command = envelope.Command, |
| | 0 | 66 | | CorrelationId = envelope.CorrelationId, |
| | 0 | 67 | | Kind = IpcEnvelopeKind.Response, |
| | 0 | 68 | | Payload = payload |
| | 0 | 69 | | }; |
| | | 70 | | } |
| | 0 | 71 | | catch (Exception e) |
| | | 72 | | { |
| | 0 | 73 | | _logger.LogError(e, "Error getting a unused address"); |
| | 0 | 74 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ServerError, |
| | 0 | 75 | | $"Error getting a unused address: {e.Message}"); |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | } |