@@ -16,6 +16,7 @@ use bitcoin::secp256k1;
16
16
17
17
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
18
18
use bitcoin:: hashes:: Hash ;
19
+ use bitcoin:: hashes:: hex:: FromHex ;
19
20
use bitcoin:: hash_types:: BlockHash ;
20
21
21
22
use bitcoin:: network:: constants:: Network ;
@@ -38,11 +39,13 @@ use crate::io;
38
39
use crate :: io_extras:: { copy, sink} ;
39
40
use crate :: prelude:: * ;
40
41
use core:: { cmp, fmt} ;
42
+ use core:: convert:: TryFrom ;
41
43
use crate :: sync:: { RwLock , RwLockReadGuard } ;
42
44
#[ cfg( feature = "std" ) ]
43
45
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
44
46
use crate :: sync:: Mutex ;
45
47
use core:: ops:: { Bound , Deref } ;
48
+ use core:: str:: FromStr ;
46
49
47
50
#[ cfg( feature = "std" ) ]
48
51
use std:: time:: { SystemTime , UNIX_EPOCH } ;
@@ -76,6 +79,11 @@ impl NodeId {
76
79
pub fn as_slice ( & self ) -> & [ u8 ] {
77
80
& self . 0
78
81
}
82
+
83
+ /// Get the public key from this NodeId
84
+ pub fn as_pubkey ( & self ) -> Result < PublicKey , secp256k1:: Error > {
85
+ PublicKey :: from_slice ( & self . 0 )
86
+ }
79
87
}
80
88
81
89
impl fmt:: Debug for NodeId {
@@ -130,6 +138,29 @@ impl Readable for NodeId {
130
138
}
131
139
}
132
140
141
+ impl From < PublicKey > for NodeId {
142
+ fn from ( pubkey : PublicKey ) -> Self {
143
+ Self :: from_pubkey ( & pubkey)
144
+ }
145
+ }
146
+
147
+ impl TryFrom < NodeId > for PublicKey {
148
+ type Error = secp256k1:: Error ;
149
+
150
+ fn try_from ( node_id : NodeId ) -> Result < Self , Self :: Error > {
151
+ node_id. as_pubkey ( )
152
+ }
153
+ }
154
+
155
+ impl FromStr for NodeId {
156
+ type Err = bitcoin:: hashes:: hex:: Error ;
157
+
158
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
159
+ let data: [ u8 ; PUBLIC_KEY_SIZE ] = FromHex :: from_hex ( s) ?;
160
+ Ok ( NodeId ( data) )
161
+ }
162
+ }
163
+
133
164
/// Represents the network as nodes and channels between them
134
165
pub struct NetworkGraph < L : Deref > where L :: Target : Logger {
135
166
secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
0 commit comments