|
| 1 | +use crate::hex_utils; |
| 2 | + |
1 | 3 | use std::convert::TryInto;
|
2 | 4 | use std::env;
|
3 |
| -use std::net::SocketAddr; |
4 | 5 | use std::io::Cursor;
|
| 6 | +use std::net::{SocketAddr, ToSocketAddrs}; |
| 7 | + |
| 8 | +use bitcoin::hashes::hex::FromHex; |
5 | 9 | use bitcoin::secp256k1::PublicKey;
|
| 10 | +use futures::stream::{FuturesUnordered, StreamExt}; |
6 | 11 | use lightning::ln::msgs::ChannelAnnouncement;
|
7 | 12 | use lightning::util::ser::Readable;
|
8 | 13 | use lightning_block_sync::http::HttpEndpoint;
|
9 | 14 | use tokio_postgres::Config;
|
10 |
| -use crate::hex_utils; |
11 |
| - |
12 |
| -use futures::stream::{FuturesUnordered, StreamExt}; |
13 | 15 |
|
14 | 16 | pub(crate) const SCHEMA_VERSION: i32 = 8;
|
15 | 17 | pub(crate) const SNAPSHOT_CALCULATION_INTERVAL: u32 = 3600 * 24; // every 24 hours, in seconds
|
@@ -209,19 +211,54 @@ pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client)
|
209 | 211 | let _ = client.execute("ALTER TABLE channel_announcements SET ( autovacuum_vacuum_insert_scale_factor = 0.005 );", &[]).await;
|
210 | 212 | }
|
211 | 213 |
|
212 |
| -/// EDIT ME |
213 | 214 | pub(crate) fn ln_peers() -> Vec<(PublicKey, SocketAddr)> {
|
214 |
| - vec![ |
215 |
| - // Bitfinex |
216 |
| - // (hex_utils::to_compressed_pubkey("033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025").unwrap(), "34.65.85.39:9735".parse().unwrap()), |
| 215 | + const WALLET_OF_SATOSHI: &str = "035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735"; |
| 216 | + let list = env::var("LN_PEERS").unwrap_or(WALLET_OF_SATOSHI.to_string()); |
| 217 | + let mut peers = Vec::new(); |
| 218 | + for peer_info in list.split(',') { |
| 219 | + peers.push(resolve_peer_info(peer_info).expect("Invalid peer info in LN_PEERS")); |
| 220 | + } |
| 221 | + peers |
| 222 | +} |
| 223 | + |
| 224 | +fn resolve_peer_info(peer_info: &str) -> Result<(PublicKey, SocketAddr), &str> { |
| 225 | + let mut peer_info = peer_info.splitn(2, '@'); |
| 226 | + |
| 227 | + let pubkey = peer_info.next().ok_or("Invalid peer info. Should be formatted as: `pubkey@host:port`")?; |
| 228 | + let pubkey = Vec::from_hex(pubkey).map_err(|_| "Invalid node pubkey")?; |
| 229 | + let pubkey = PublicKey::from_slice(&pubkey).map_err(|_| "Invalid node pubkey")?; |
| 230 | + |
| 231 | + let socket_address = peer_info.next().ok_or("Invalid peer info. Should be formatted as: `pubkey@host:port`")?; |
| 232 | + let socket_address = socket_address |
| 233 | + .to_socket_addrs() |
| 234 | + .map_err(|_| "Cannot resolve node address")? |
| 235 | + .next() |
| 236 | + .ok_or("Cannot resolve node address")?; |
217 | 237 |
|
218 |
| - // Matt Corallo |
219 |
| - // (hex_utils::to_compressed_pubkey("03db10aa09ff04d3568b0621750794063df401e6853c79a21a83e1a3f3b5bfb0c8").unwrap(), "69.59.18.80:9735".parse().unwrap()) |
| 238 | + Ok((pubkey, socket_address)) |
| 239 | +} |
| 240 | + |
| 241 | +#[cfg(test)] |
| 242 | +mod tests { |
| 243 | + use super::resolve_peer_info; |
| 244 | + use bitcoin::hashes::hex::ToHex; |
220 | 245 |
|
221 |
| - // River Financial |
222 |
| - // (hex_utils::to_compressed_pubkey("03037dc08e9ac63b82581f79b662a4d0ceca8a8ca162b1af3551595b8f2d97b70a").unwrap(), "104.196.249.140:9735".parse().unwrap()) |
| 246 | + #[test] |
| 247 | + fn test_resolve_peer_info() { |
| 248 | + let wallet_of_satoshi = "035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735"; |
| 249 | + let (pubkey, socket_address) = resolve_peer_info(wallet_of_satoshi).unwrap(); |
| 250 | + assert_eq!(pubkey.serialize().to_hex(), "035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226"); |
| 251 | + assert_eq!(socket_address.to_string(), "170.75.163.209:9735"); |
223 | 252 |
|
224 |
| - // Wallet of Satoshi | 035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735 |
225 |
| - (hex_utils::to_compressed_pubkey("035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226").unwrap(), "170.75.163.209:9735".parse().unwrap()) |
226 |
| - ] |
| 253 | + let ipv6 = "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025@[2001:db8::1]:80"; |
| 254 | + let (pubkey, socket_address) = resolve_peer_info(ipv6).unwrap(); |
| 255 | + assert_eq!(pubkey.serialize().to_hex(), "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025"); |
| 256 | + assert_eq!(socket_address.to_string(), "[2001:db8::1]:80"); |
| 257 | + |
| 258 | + let localhost = "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025@localhost:9735"; |
| 259 | + let (pubkey, socket_address) = resolve_peer_info(localhost).unwrap(); |
| 260 | + assert_eq!(pubkey.serialize().to_hex(), "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025"); |
| 261 | + let socket_address = socket_address.to_string(); |
| 262 | + assert!(socket_address == "127.0.0.1:9735" || socket_address == "[::1]:9735"); |
| 263 | + } |
227 | 264 | }
|
0 commit comments