Skip to content

Commit df8a4a8

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

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lightning/src/routing/gossip.rs

Lines changed: 19 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,10 @@ impl NodeId {
7677
pub fn as_slice(&self) -> &[u8] {
7778
&self.0
7879
}
80+
81+
pub fn as_pubkey(&self) -> Result<PublicKey, secp256k1::Error> {
82+
PublicKey::from_slice(&self.0)
83+
}
7984
}
8085

8186
impl fmt::Debug for NodeId {
@@ -130,6 +135,20 @@ impl Readable for NodeId {
130135
}
131136
}
132137

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

0 commit comments

Comments
 (0)