From 5ed6732b875a51913f2257523917b9aa75b55775 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Wed, 12 Apr 2023 19:14:03 -0500 Subject: [PATCH 1/2] Implement to and from for PublicKey and NodeId --- lightning/src/routing/gossip.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 892d1b40a96..4f5b4f202c1 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -38,6 +38,7 @@ use crate::io; use crate::io_extras::{copy, sink}; use crate::prelude::*; use core::{cmp, fmt}; +use core::convert::TryFrom; use crate::sync::{RwLock, RwLockReadGuard}; #[cfg(feature = "std")] use core::sync::atomic::{AtomicUsize, Ordering}; @@ -76,6 +77,11 @@ impl NodeId { pub fn as_slice(&self) -> &[u8] { &self.0 } + + /// Get the public key from this NodeId + pub fn as_pubkey(&self) -> Result { + PublicKey::from_slice(&self.0) + } } impl fmt::Debug for NodeId { @@ -130,6 +136,20 @@ impl Readable for NodeId { } } +impl From for NodeId { + fn from(pubkey: PublicKey) -> Self { + Self::from_pubkey(&pubkey) + } +} + +impl TryFrom for PublicKey { + type Error = secp256k1::Error; + + fn try_from(node_id: NodeId) -> Result { + node_id.as_pubkey() + } +} + /// Represents the network as nodes and channels between them pub struct NetworkGraph where L::Target: Logger { secp_ctx: Secp256k1, From 7a37e2cd23ee1d563ad2b21f115d713b15d9e8a0 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Thu, 13 Apr 2023 12:54:04 -0500 Subject: [PATCH 2/2] Impl FromStr for NodeId --- lightning/src/routing/gossip.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 4f5b4f202c1..866fdc5caa7 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -16,6 +16,7 @@ use bitcoin::secp256k1; use bitcoin::hashes::sha256d::Hash as Sha256dHash; use bitcoin::hashes::Hash; +use bitcoin::hashes::hex::FromHex; use bitcoin::hash_types::BlockHash; use bitcoin::network::constants::Network; @@ -44,6 +45,7 @@ use crate::sync::{RwLock, RwLockReadGuard}; use core::sync::atomic::{AtomicUsize, Ordering}; use crate::sync::Mutex; use core::ops::{Bound, Deref}; +use core::str::FromStr; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; @@ -150,6 +152,15 @@ impl TryFrom for PublicKey { } } +impl FromStr for NodeId { + type Err = bitcoin::hashes::hex::Error; + + fn from_str(s: &str) -> Result { + let data: [u8; PUBLIC_KEY_SIZE] = FromHex::from_hex(s)?; + Ok(NodeId(data)) + } +} + /// Represents the network as nodes and channels between them pub struct NetworkGraph where L::Target: Logger { secp_ctx: Secp256k1,