Skip to content

Commit 5ed6732

Browse files
committed
Implement to and from for PublicKey and NodeId
1 parent 157af6e commit 5ed6732

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lightning/src/routing/gossip.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use crate::io;
3838
use crate::io_extras::{copy, sink};
3939
use crate::prelude::*;
4040
use core::{cmp, fmt};
41+
use core::convert::TryFrom;
4142
use crate::sync::{RwLock, RwLockReadGuard};
4243
#[cfg(feature = "std")]
4344
use core::sync::atomic::{AtomicUsize, Ordering};
@@ -76,6 +77,11 @@ impl NodeId {
7677
pub fn as_slice(&self) -> &[u8] {
7778
&self.0
7879
}
80+
81+
/// Get the public key from this NodeId
82+
pub fn as_pubkey(&self) -> Result<PublicKey, secp256k1::Error> {
83+
PublicKey::from_slice(&self.0)
84+
}
7985
}
8086

8187
impl fmt::Debug for NodeId {
@@ -130,6 +136,20 @@ impl Readable for NodeId {
130136
}
131137
}
132138

139+
impl From<PublicKey> for NodeId {
140+
fn from(pubkey: PublicKey) -> Self {
141+
Self::from_pubkey(&pubkey)
142+
}
143+
}
144+
145+
impl TryFrom<NodeId> for PublicKey {
146+
type Error = secp256k1::Error;
147+
148+
fn try_from(node_id: NodeId) -> Result<Self, Self::Error> {
149+
node_id.as_pubkey()
150+
}
151+
}
152+
133153
/// Represents the network as nodes and channels between them
134154
pub struct NetworkGraph<L: Deref> where L::Target: Logger {
135155
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,

0 commit comments

Comments
 (0)