Skip to content

Commit 37cd606

Browse files
committed
Impl FromStr for NodeId
1 parent df8a4a8 commit 37cd606

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lightning/src/routing/gossip.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ use crate::io;
3838
use crate::io_extras::{copy, sink};
3939
use crate::prelude::*;
4040
use core::{cmp, fmt};
41-
use core::convert::TryFrom;
41+
use core::convert::{TryFrom, TryInto};
4242
use crate::sync::{RwLock, RwLockReadGuard};
4343
#[cfg(feature = "std")]
4444
use core::sync::atomic::{AtomicUsize, Ordering};
4545
use crate::sync::Mutex;
4646
use core::ops::{Bound, Deref};
47+
use core::str::FromStr;
48+
use hex::{decode, FromHexError};
4749

4850
#[cfg(feature = "std")]
4951
use std::time::{SystemTime, UNIX_EPOCH};
@@ -149,6 +151,16 @@ impl TryFrom<NodeId> for PublicKey {
149151
}
150152
}
151153

154+
impl FromStr for NodeId {
155+
type Err = FromHexError;
156+
fn from_str(s: &str) -> Result<Self, Self::Err> {
157+
let bytes = decode(s)?;
158+
let data: [u8; PUBLIC_KEY_SIZE] =
159+
bytes[..].try_into().map_err(|_| FromHexError::InvalidStringLength)?;
160+
Ok(NodeId(data))
161+
}
162+
}
163+
152164
/// Represents the network as nodes and channels between them
153165
pub struct NetworkGraph<L: Deref> where L::Target: Logger {
154166
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,

0 commit comments

Comments
 (0)