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