< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Wallet.BitcoinWalletService
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Infrastructure.Bitcoin/Wallet/BitcoinWalletService.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 63
Coverable lines: 63
Total lines: 88
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%620%
.ctor(...)0%620%
SendTransactionAsync()100%210%
GetUnusedAddressAsync()0%210140%
GetTransactionAsync()100%210%
GetCurrentBlockHeightAsync()100%210%
GetBlockAsync()100%210%
GetTransactionConfirmationsAsync()100%210%

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Infrastructure.Bitcoin/Wallet/BitcoinWalletService.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2using Microsoft.Extensions.Options;
 3using NBitcoin;
 4
 5namespace NLightning.Infrastructure.Bitcoin.Wallet;
 6
 7using Domain.Bitcoin.Enums;
 8using Domain.Bitcoin.ValueObjects;
 9using Domain.Bitcoin.Wallet.Models;
 10using Domain.Node.Options;
 11using Domain.Persistence.Interfaces;
 12using Domain.Protocol.Interfaces;
 13using Interfaces;
 14
 15public class BitcoinWalletService : IBitcoinWalletService
 16{
 017    private readonly IBlockchainMonitor _blockchainMonitor;
 018    private readonly ILogger<BitcoinWalletService> _logger;
 19    private readonly Network _network;
 020    private readonly ISecureKeyManager _secureKeyManager;
 021    private readonly IUnitOfWork _uow;
 22
 023    public BitcoinWalletService(IBlockchainMonitor blockchainMonitor, ILogger<BitcoinWalletService> logger,
 024                                IOptions<NodeOptions> nodeOptions, ISecureKeyManager secureKeyManager,
 025                                IUnitOfWork uow)
 026    {
 027        _blockchainMonitor = blockchainMonitor;
 028        _logger = logger;
 029        _secureKeyManager = secureKeyManager;
 030        _uow = uow;
 31
 032        _network = Network.GetNetwork(nodeOptions.Value.BitcoinNetwork) ?? Network.Main;
 033        _logger.LogInformation("BitcoinWalletService network: {Network} (config: {ConfigNetwork})", _network, nodeOption
 034    }
 35
 036    public async Task<WalletAddressModel> GetUnusedAddressAsync(AddressType addressType, bool isChange)
 037    {
 038        if (addressType is not (AddressType.P2Wpkh or AddressType.P2Tr))
 039            throw new InvalidOperationException(
 040                "You cannot use flags for this method. Please select only one address type.");
 041
 42        // Find an unused address in the DB
 043        var addressModel = await _uow.WalletAddressesDbRepository.GetUnusedAddressAsync(addressType, isChange);
 044
 045        if (addressModel is not null)
 046            return addressModel;
 47
 48        // If there's none, get the last used index from db
 049        var lastUsedIndex = await _uow.WalletAddressesDbRepository.GetLastUsedAddressIndex(addressType, isChange);
 50
 051        _logger.LogInformation("Generating 10 new {addressType} {change}addresses and saving to the database.",
 052                               Enum.GetName(addressType), isChange ? "change " : string.Empty);
 53
 054        // Generate 10 new addresses
 055        var addressList = new List<WalletAddressModel>(10);
 056        for (var i = lastUsedIndex; i < lastUsedIndex + 10; i++)
 57        {
 058            ExtPrivKey extPrivKey;
 059            if (addressType == AddressType.P2Tr)
 060            {
 061                extPrivKey = _secureKeyManager.GetDepositP2TrKeyAtIndex(i, isChange);
 062                var extKey = ExtKey.CreateFromBytes(extPrivKey);
 063                var address = extKey.Neuter().PubKey.GetAddress(ScriptPubKeyType.TaprootBIP86, _network);
 64
 065                addressList.Add(new WalletAddressModel(addressType, i, isChange, address.ToString()));
 66            }
 67            else
 68            {
 069                extPrivKey = _secureKeyManager.GetDepositP2WpkhKeyAtIndex(i, isChange);
 070                var extKey = ExtKey.CreateFromBytes(extPrivKey);
 071                var address = extKey.Neuter().PubKey.GetAddress(ScriptPubKeyType.Segwit, _network);
 072
 073                addressList.Add(new WalletAddressModel(addressType, i, isChange, address.ToString()));
 074            }
 075        }
 76
 077        _uow.WalletAddressesDbRepository.AddRange(addressList);
 078        await _uow.SaveChangesAsync();
 79
 80        // Register all newly generated addresses with blockchain monitor
 081        foreach (var address in addressList)
 82        {
 083            _blockchainMonitor.WatchBitcoinAddress(address);
 084        }
 85
 086        return addressList[0];
 087    }
 088}