@@ -38,8 +38,8 @@ use lightning::offers::payer_proof::{
3838use lightning:: offers:: refund:: Refund as LdkRefund ;
3939use lightning:: offers:: static_invoice:: StaticInvoice as LdkStaticInvoice ;
4040use lightning:: onion_message:: dns_resolution:: HumanReadableName as LdkHumanReadableName ;
41- use lightning:: routing:: gossip:: NodeAlias as LdkNodeAlias ;
42- pub use lightning:: routing:: gossip:: { NodeId , RoutingFees } ;
41+ pub use lightning:: routing:: gossip:: RoutingFees ;
42+ use lightning:: routing:: gossip:: { NodeAlias as LdkNodeAlias , NodeId as LdkNodeId } ;
4343pub use lightning:: routing:: router:: RouteParametersConfig ;
4444use lightning:: util:: persist:: PageToken as LdkPageToken ;
4545use lightning:: util:: ser:: { Readable , RequiredWrapper , Writeable , Writer } ;
@@ -242,19 +242,71 @@ impl ReadablePublicKey for RequiredWrapper<Arc<PublicKey>> {
242242 }
243243}
244244
245- uniffi :: custom_type! ( NodeId , String , {
246- remote ,
247- try_lift : |val| {
248- if let Ok ( key ) = NodeId :: from_str ( & val ) {
249- return Ok ( key ) ;
250- }
245+ /// A compressed public key identifying a node in the network graph.
246+ # [ derive ( Debug , Clone , PartialEq , Eq , uniffi :: Object ) ]
247+ # [ uniffi :: export ( Debug , Display , Eq ) ]
248+ pub struct NodeId {
249+ pub ( crate ) inner : LdkNodeId ,
250+ }
251251
252- Err ( Error :: InvalidNodeId . into( ) )
253- } ,
254- lower: |obj| {
255- obj. to_string( )
256- } ,
257- } ) ;
252+ #[ uniffi:: export]
253+ impl NodeId {
254+ /// Constructs a node id from its serialized representation.
255+ #[ uniffi:: constructor]
256+ pub fn from_str ( node_id_str : & str ) -> Result < Self , Error > {
257+ node_id_str. parse ( )
258+ }
259+
260+ /// Constructs a node id from a public key.
261+ #[ uniffi:: constructor]
262+ pub fn from_public_key ( public_key : Arc < PublicKey > ) -> Self {
263+ LdkNodeId :: from_pubkey ( public_key. as_ref ( ) . as_ref ( ) ) . into ( )
264+ }
265+
266+ /// Returns the serialized node id.
267+ pub fn as_bytes ( & self ) -> Vec < u8 > {
268+ self . inner . as_slice ( ) . to_vec ( )
269+ }
270+
271+ /// Returns the node id as a public key.
272+ pub fn as_public_key ( & self ) -> Result < Arc < PublicKey > , Error > {
273+ self . inner . as_pubkey ( ) . map ( PublicKey :: from) . map ( Arc :: new) . map_err ( |_| Error :: InvalidNodeId )
274+ }
275+ }
276+
277+ impl FromStr for NodeId {
278+ type Err = Error ;
279+
280+ fn from_str ( node_id_str : & str ) -> Result < Self , Self :: Err > {
281+ node_id_str. parse :: < LdkNodeId > ( ) . map ( Self :: from) . map_err ( |_| Error :: InvalidNodeId )
282+ }
283+ }
284+
285+ impl From < LdkNodeId > for NodeId {
286+ fn from ( inner : LdkNodeId ) -> Self {
287+ Self { inner }
288+ }
289+ }
290+
291+ impl Deref for NodeId {
292+ type Target = LdkNodeId ;
293+
294+ fn deref ( & self ) -> & Self :: Target {
295+ & self . inner
296+ }
297+ }
298+
299+ impl AsRef < LdkNodeId > for NodeId {
300+ fn as_ref ( & self ) -> & LdkNodeId {
301+ self . deref ( )
302+ }
303+ }
304+
305+ impl std:: fmt:: Display for NodeId {
306+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
307+ write ! ( f, "{}" , self . inner)
308+ }
309+ }
258310
259311uniffi:: custom_type!( Address , String , {
260312 remote,
0 commit comments