@@ -18,7 +18,7 @@ use core::str::FromStr;
1818
1919use bech32:: { self , Bech32 , Hrp } ;
2020
21- use super :: nip01:: Coordinate ;
21+ use super :: nip01:: { Coordinate , CoordinateBorrow } ;
2222use super :: nip05:: Nip05Profile ;
2323#[ cfg( feature = "nip49" ) ]
2424use super :: nip49:: { self , EncryptedSecretKey } ;
@@ -647,22 +647,6 @@ impl FromBech32 for Nip19Profile {
647647 }
648648}
649649
650- impl FromBech32 for Coordinate {
651- type Err = Error ;
652-
653- fn from_bech32 ( addr : & str ) -> Result < Self , Self :: Err > {
654- let ( hrp, data) = bech32:: decode ( addr) ?;
655-
656- if hrp != HRP_COORDINATE {
657- return Err ( Error :: WrongPrefix ) ;
658- }
659-
660- let coordinate = Nip19Coordinate :: from_bech32_data ( data) ?;
661-
662- Ok ( coordinate. coordinate )
663- }
664- }
665-
666650#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Serialize , Deserialize ) ]
667651pub struct Nip19Coordinate {
668652 pub coordinate : Coordinate ,
@@ -761,37 +745,47 @@ impl FromBech32 for Nip19Coordinate {
761745impl ToBech32 for Nip19Coordinate {
762746 type Err = Error ;
763747
748+ #[ inline]
764749 fn to_bech32 ( & self ) -> Result < String , Self :: Err > {
765- // Allocate capacity
766- let identifier_len: usize = 2 + self . identifier . len ( ) ;
767- let relays_len: usize = self . relays . iter ( ) . map ( |u| 2 + u. as_str ( ) . len ( ) ) . sum ( ) ;
768- let mut bytes: Vec < u8 > = Vec :: with_capacity (
769- identifier_len + FIXED_1_1_32_BYTES_TVL + FIXED_KIND_BYTES_TVL + relays_len,
770- ) ;
750+ coordinate_to_bech32 ( self . coordinate . borrow ( ) , & self . relays )
751+ }
752+ }
771753
772- // Identifier
773- bytes. push ( SPECIAL ) ; // Type
774- bytes. push ( self . identifier . len ( ) as u8 ) ; // Len
775- bytes. extend ( self . identifier . as_bytes ( ) ) ; // Value
754+ pub ( super ) fn coordinate_to_bech32 < ' a > (
755+ coordinate : CoordinateBorrow < ' a > ,
756+ relays : & [ RelayUrl ] ,
757+ ) -> Result < String , Error > {
758+ let identifier: & ' a str = coordinate. identifier . unwrap_or_default ( ) ;
776759
777- // Author
778- bytes. push ( AUTHOR ) ; // Type
779- bytes. push ( 32 ) ; // Len
780- bytes. extend ( self . public_key . as_bytes ( ) ) ; // Value
760+ // Allocate capacity
761+ let identifier_len: usize = 2 + identifier. len ( ) ;
762+ let relays_len: usize = relays. iter ( ) . map ( |u| 2 + u. as_str ( ) . len ( ) ) . sum ( ) ;
763+ let mut bytes: Vec < u8 > = Vec :: with_capacity (
764+ identifier_len + FIXED_1_1_32_BYTES_TVL + FIXED_KIND_BYTES_TVL + relays_len,
765+ ) ;
781766
782- // Kind
783- bytes. push ( KIND ) ; // Type
784- bytes. push ( 4 ) ; // Len
785- bytes. extend ( ( self . kind . as_u16 ( ) as u32 ) . to_be_bytes ( ) ) ; // Value
767+ // Identifier
768+ bytes. push ( SPECIAL ) ; // Type
769+ bytes. push ( identifier . len ( ) as u8 ) ; // Len
770+ bytes. extend ( identifier . as_bytes ( ) ) ; // Value
786771
787- for relay in self . relays . iter ( ) {
788- bytes. push ( RELAY ) ; // Type
789- bytes. push ( relay. as_str ( ) . len ( ) as u8 ) ; // Len
790- bytes. extend ( relay. as_str ( ) . as_bytes ( ) ) ; // Value
791- }
772+ // Author
773+ bytes. push ( AUTHOR ) ; // Type
774+ bytes. push ( 32 ) ; // Len
775+ bytes. extend ( coordinate. public_key . as_bytes ( ) ) ; // Value
776+
777+ // Kind
778+ bytes. push ( KIND ) ; // Type
779+ bytes. push ( 4 ) ; // Len
780+ bytes. extend ( ( coordinate. kind . as_u16 ( ) as u32 ) . to_be_bytes ( ) ) ; // Value
792781
793- Ok ( bech32:: encode :: < Bech32 > ( HRP_COORDINATE , & bytes) ?)
782+ for relay in relays. iter ( ) {
783+ bytes. push ( RELAY ) ; // Type
784+ bytes. push ( relay. as_str ( ) . len ( ) as u8 ) ; // Len
785+ bytes. extend ( relay. as_str ( ) . as_bytes ( ) ) ; // Value
794786 }
787+
788+ Ok ( bech32:: encode :: < Bech32 > ( HRP_COORDINATE , & bytes) ?)
795789}
796790
797791#[ cfg( test) ]
0 commit comments