Skip to content

Commit 6ed9f88

Browse files
committed
Impl FromStr for NodeId
1 parent 5ed6732 commit 6ed9f88

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lightning/src/routing/gossip.rs

Lines changed: 13 additions & 1 deletion
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};
@@ -150,6 +152,16 @@ impl TryFrom<NodeId> for PublicKey {
150152
}
151153
}
152154

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

0 commit comments

Comments
 (0)