| | | 1 | | using System.Net; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using NBitcoin; |
| | | 5 | | using NBitcoin.RPC; |
| | | 6 | | |
| | | 7 | | namespace NLightning.Infrastructure.Bitcoin.Wallet; |
| | | 8 | | |
| | | 9 | | using Domain.Node.Options; |
| | | 10 | | using Interfaces; |
| | | 11 | | using Options; |
| | | 12 | | |
| | | 13 | | public class BitcoinChainService : IBitcoinChainService |
| | | 14 | | { |
| | | 15 | | private readonly RPCClient _rpcClient; |
| | | 16 | | private readonly ILogger<BitcoinChainService> _logger; |
| | | 17 | | |
| | 0 | 18 | | public BitcoinChainService(IOptions<BitcoinOptions> bitcoinOptions, ILogger<BitcoinChainService> logger, |
| | 0 | 19 | | IOptions<NodeOptions> nodeOptions) |
| | | 20 | | { |
| | 0 | 21 | | _logger = logger; |
| | 0 | 22 | | var network = Network.GetNetwork(nodeOptions.Value.BitcoinNetwork) ?? Network.Main; |
| | | 23 | | |
| | 0 | 24 | | var rpcCredentials = new RPCCredentialString |
| | 0 | 25 | | { |
| | 0 | 26 | | UserPassword = new NetworkCredential(bitcoinOptions.Value.RpcUser, bitcoinOptions.Value.RpcPassword) |
| | 0 | 27 | | }; |
| | | 28 | | |
| | 0 | 29 | | _rpcClient = new RPCClient(rpcCredentials, bitcoinOptions.Value.RpcEndpoint, network); |
| | 0 | 30 | | _rpcClient.GetBlockchainInfo(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<uint256> SendTransactionAsync(Transaction transaction) |
| | | 34 | | { |
| | | 35 | | try |
| | | 36 | | { |
| | 0 | 37 | | if (_logger.IsEnabled(LogLevel.Information)) |
| | 0 | 38 | | _logger.LogInformation("Broadcasting transaction {TxId}", transaction.GetHash()); |
| | | 39 | | |
| | 0 | 40 | | var result = await _rpcClient.SendRawTransactionAsync(transaction); |
| | | 41 | | |
| | 0 | 42 | | if (_logger.IsEnabled(LogLevel.Information)) |
| | 0 | 43 | | _logger.LogInformation("Successfully broadcast transaction {TxId}", result); |
| | | 44 | | |
| | 0 | 45 | | return result; |
| | | 46 | | } |
| | 0 | 47 | | catch (Exception ex) |
| | | 48 | | { |
| | 0 | 49 | | _logger.LogError(ex, "Failed to broadcast transaction {TxId}", transaction.GetHash()); |
| | 0 | 50 | | throw; |
| | | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | public async Task<Transaction?> GetTransactionAsync(uint256 txId) |
| | | 55 | | { |
| | | 56 | | try |
| | | 57 | | { |
| | 0 | 58 | | return await _rpcClient.GetRawTransactionAsync(new uint256(txId), false); |
| | | 59 | | } |
| | 0 | 60 | | catch (RPCException ex) when (ex.RPCCode == RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY) |
| | | 61 | | { |
| | 0 | 62 | | return null; // Transaction not found |
| | | 63 | | } |
| | 0 | 64 | | catch (Exception ex) |
| | | 65 | | { |
| | 0 | 66 | | _logger.LogError(ex, "Failed to get transaction {TxId}", txId); |
| | 0 | 67 | | throw; |
| | | 68 | | } |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | public async Task<uint> GetCurrentBlockHeightAsync() |
| | | 72 | | { |
| | | 73 | | try |
| | | 74 | | { |
| | 0 | 75 | | var blockCount = await _rpcClient.GetBlockCountAsync(); |
| | 0 | 76 | | return (uint)blockCount; |
| | | 77 | | } |
| | 0 | 78 | | catch (Exception ex) |
| | | 79 | | { |
| | 0 | 80 | | _logger.LogError(ex, "Failed to get current block height"); |
| | 0 | 81 | | throw; |
| | | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public async Task<Block?> GetBlockAsync(uint height) |
| | | 86 | | { |
| | | 87 | | try |
| | | 88 | | { |
| | 0 | 89 | | var blockHash = await _rpcClient.GetBlockHashAsync((int)height); |
| | 0 | 90 | | return await _rpcClient.GetBlockAsync(blockHash); |
| | | 91 | | } |
| | 0 | 92 | | catch (Exception ex) |
| | | 93 | | { |
| | 0 | 94 | | _logger.LogError(ex, "Failed to get block at height {Height}", height); |
| | 0 | 95 | | throw; |
| | | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | public async Task<uint> GetTransactionConfirmationsAsync(uint256 txId) |
| | | 100 | | { |
| | | 101 | | try |
| | | 102 | | { |
| | 0 | 103 | | var txInfo = await _rpcClient.GetRawTransactionInfoAsync(new uint256(txId)); |
| | 0 | 104 | | return txInfo.Confirmations; |
| | | 105 | | } |
| | 0 | 106 | | catch (RPCException ex) when (ex.RPCCode == RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY) |
| | | 107 | | { |
| | 0 | 108 | | return 0; // Transaction not found |
| | | 109 | | } |
| | 0 | 110 | | catch (Exception ex) |
| | | 111 | | { |
| | 0 | 112 | | _logger.LogError(ex, "Failed to get confirmations for transaction {TxId}", txId); |
| | 0 | 113 | | throw; |
| | | 114 | | } |
| | 0 | 115 | | } |
| | | 116 | | } |