Skip to content

Commit d6622c0

Browse files
committed
Impl FromStr for NodeId
1 parent e1b50cf commit d6622c0

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lightning/src/routing/gossip.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ 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};
4445
use crate::sync::Mutex;
4546
use core::ops::{Bound, Deref};
47+
use core::str::FromStr;
4648

4749
#[cfg(feature = "std")]
4850
use std::time::{SystemTime, UNIX_EPOCH};
@@ -136,9 +138,19 @@ impl From<PublicKey> for NodeId {
136138
}
137139
}
138140

139-
impl From<NodeId> for PublicKey {
140-
fn from(node_id: NodeId) -> PublicKey {
141-
PublicKey::from_slice( &node_id.0).unwrap()
141+
impl TryFrom<NodeId> for PublicKey {
142+
type Error = secp256k1::Error;
143+
144+
fn try_from(node_id: NodeId) -> Result<Self, Self::Error> {
145+
PublicKey::from_slice(&node_id.0)
146+
}
147+
}
148+
149+
impl FromStr for NodeId {
150+
type Err = secp256k1::Error;
151+
fn from_str(s: &str) -> Result<Self, Self::Err> {
152+
let pubkey = PublicKey::from_str(s)?;
153+
Ok(NodeId::from_pubkey(&pubkey))
142154
}
143155
}
144156

0 commit comments

Comments
 (0)