|
| 1 | +use std::{sync::{Arc}}; |
| 2 | + |
| 3 | +use bitcoin::{BlockHash, Block}; |
| 4 | +use lightning_block_sync::{rpc::RpcClient, http::{HttpEndpoint}, BlockSource, AsyncBlockSourceResult, BlockHeaderData}; |
| 5 | +use tokio::sync::Mutex; |
| 6 | + |
| 7 | +use crate::convert::{CreateWalletResponse, BlockchainInfoResponse, NewAddressResponse, GetBalanceResponse, GenerateToAddressResponse}; |
| 8 | + |
| 9 | + |
| 10 | +pub struct BitcoindClient { |
| 11 | + bitcoind_rpc_client: Arc<Mutex<RpcClient>>, |
| 12 | + host: String, |
| 13 | + port: u16, |
| 14 | + rpc_user: String, |
| 15 | + rpc_password: String, |
| 16 | +} |
| 17 | + |
| 18 | +impl BlockSource for &BitcoindClient { |
| 19 | + fn get_header<'a>( |
| 20 | + &'a mut self, header_hash: &'a BlockHash, height_hint: Option<u32>, |
| 21 | + ) -> AsyncBlockSourceResult<'a, BlockHeaderData> { |
| 22 | + Box::pin(async move { |
| 23 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 24 | + rpc.get_header(header_hash, height_hint).await |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + fn get_block<'a>( |
| 29 | + &'a mut self, header_hash: &'a BlockHash, |
| 30 | + ) -> AsyncBlockSourceResult<'a, Block> { |
| 31 | + Box::pin(async move { |
| 32 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 33 | + rpc.get_block(header_hash).await |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> { |
| 38 | + Box::pin(async move { |
| 39 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 40 | + rpc.get_best_block().await |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl BitcoindClient { |
| 46 | + pub async fn new(host: String, port: u16, rpc_user: String, rpc_password: String) -> std::io::Result<Self> { |
| 47 | + let http_endpoint = HttpEndpoint::for_host(host.clone()).with_port(port); |
| 48 | + let rpc_creditials = |
| 49 | + base64::encode(format!("{}:{}", rpc_user.clone(), rpc_password.clone())); |
| 50 | + let mut bitcoind_rpc_client = RpcClient::new(&rpc_creditials, http_endpoint)?; |
| 51 | + let _dummy = bitcoind_rpc_client |
| 52 | + .call_method::<BlockchainInfoResponse>("getblockchaininfo", &vec![]) |
| 53 | + .await |
| 54 | + .map_err(|_| { |
| 55 | + std::io::Error::new(std::io::ErrorKind::PermissionDenied, |
| 56 | + "Failed to make initial call to bitcoind - please check your RPC user/password and access settings") |
| 57 | + })?; |
| 58 | + |
| 59 | + let client = Self { |
| 60 | + bitcoind_rpc_client: Arc::new(Mutex::new(bitcoind_rpc_client)), |
| 61 | + host, |
| 62 | + port, |
| 63 | + rpc_user, |
| 64 | + rpc_password, |
| 65 | + }; |
| 66 | + |
| 67 | + Ok(client) |
| 68 | + } |
| 69 | + |
| 70 | + pub fn get_new_rpc_client(&self) -> std::io::Result<RpcClient> { |
| 71 | + let http_endpoint = HttpEndpoint::for_host(self.host.clone()).with_port(self.port); |
| 72 | + let rpc_credentials = |
| 73 | + base64::encode(format!("{}:{}", self.rpc_user.clone(), self.rpc_password.clone())); |
| 74 | + RpcClient::new(&rpc_credentials, http_endpoint) |
| 75 | + } |
| 76 | + |
| 77 | + pub async fn get_blockchain_info(&self) -> BlockchainInfoResponse { |
| 78 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 79 | + rpc.call_method::<BlockchainInfoResponse>("getblockchaininfo", &vec![]).await.unwrap() |
| 80 | + } |
| 81 | + |
| 82 | + pub async fn create_wallet(&self) -> CreateWalletResponse { |
| 83 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 84 | + let create_wallet_args = vec![serde_json::json!("test-wallet")]; |
| 85 | + |
| 86 | + rpc.call_method::<CreateWalletResponse>("createwallet", &create_wallet_args).await.unwrap() |
| 87 | + } |
| 88 | + |
| 89 | + pub async fn get_new_address(&self) -> String { |
| 90 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 91 | + |
| 92 | + let addr_args = vec![serde_json::json!("LDK output address")]; |
| 93 | + let addr = rpc.call_method::<NewAddressResponse>("getnewaddress", &addr_args).await.unwrap(); |
| 94 | + addr.0.to_string() |
| 95 | + } |
| 96 | + |
| 97 | + pub async fn get_balance(&self) -> GetBalanceResponse { |
| 98 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 99 | + |
| 100 | + rpc.call_method::<GetBalanceResponse>("getbalance", &vec![]).await.unwrap() |
| 101 | + } |
| 102 | + |
| 103 | + pub async fn generate_to_address(&self, block_num: u64, address: &str) -> GenerateToAddressResponse { |
| 104 | + let mut rpc = self.bitcoind_rpc_client.lock().await; |
| 105 | + |
| 106 | + let generate_to_address_args = vec![serde_json::json!(block_num), serde_json::json!(address)]; |
| 107 | + |
| 108 | + |
| 109 | + rpc.call_method::<GenerateToAddressResponse>("generatetoaddress", &generate_to_address_args).await.unwrap() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
0 commit comments