From: benthecarman Date: Thu, 13 Apr 2023 00:14:03 +0000 (-0500) Subject: Implement to and from for PublicKey and NodeId X-Git-Tag: v0.0.115~23^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=5ed6732b875a51913f2257523917b9aa75b55775;p=rust-lightning Implement to and from for PublicKey and NodeId --- diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 892d1b40a..4f5b4f202 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,