@@ -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 } ;
@@ -761,37 +761,47 @@ impl FromBech32 for Nip19Coordinate {
761761impl ToBech32 for Nip19Coordinate {
762762 type Err = Error ;
763763
764+ #[ inline]
764765 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- ) ;
766+ coordinate_to_bech32 ( self . coordinate . borrow ( ) , & self . relays )
767+ }
768+ }
771769
772- // Identifier
773- bytes. push ( SPECIAL ) ; // Type
774- bytes. push ( self . identifier . len ( ) as u8 ) ; // Len
775- bytes. extend ( self . identifier . as_bytes ( ) ) ; // Value
770+ pub ( super ) fn coordinate_to_bech32 < ' a > (
771+ coordinate : CoordinateBorrow < ' a > ,
772+ relays : & [ RelayUrl ] ,
773+ ) -> Result < String , Error > {
774+ let identifier: & ' a str = coordinate. identifier . unwrap_or_default ( ) ;
776775
777- // Author
778- bytes. push ( AUTHOR ) ; // Type
779- bytes. push ( 32 ) ; // Len
780- bytes. extend ( self . public_key . as_bytes ( ) ) ; // Value
776+ // Allocate capacity
777+ let identifier_len: usize = 2 + identifier. len ( ) ;
778+ let relays_len: usize = relays. iter ( ) . map ( |u| 2 + u. as_str ( ) . len ( ) ) . sum ( ) ;
779+ let mut bytes: Vec < u8 > = Vec :: with_capacity (
780+ identifier_len + FIXED_1_1_32_BYTES_TVL + FIXED_KIND_BYTES_TVL + relays_len,
781+ ) ;
781782
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
783+ // Identifier
784+ bytes. push ( SPECIAL ) ; // Type
785+ bytes. push ( identifier . len ( ) as u8 ) ; // Len
786+ bytes. extend ( identifier . as_bytes ( ) ) ; // Value
786787
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- }
788+ // Author
789+ bytes. push ( AUTHOR ) ; // Type
790+ bytes. push ( 32 ) ; // Len
791+ bytes. extend ( coordinate. public_key . as_bytes ( ) ) ; // Value
792+
793+ // Kind
794+ bytes. push ( KIND ) ; // Type
795+ bytes. push ( 4 ) ; // Len
796+ bytes. extend ( ( coordinate. kind . as_u16 ( ) as u32 ) . to_be_bytes ( ) ) ; // Value
792797
793- Ok ( bech32:: encode :: < Bech32 > ( HRP_COORDINATE , & bytes) ?)
798+ for relay in relays. iter ( ) {
799+ bytes. push ( RELAY ) ; // Type
800+ bytes. push ( relay. as_str ( ) . len ( ) as u8 ) ; // Len
801+ bytes. extend ( relay. as_str ( ) . as_bytes ( ) ) ; // Value
794802 }
803+
804+ Ok ( bech32:: encode :: < Bech32 > ( HRP_COORDINATE , & bytes) ?)
795805}
796806
797807#[ cfg( test) ]
0 commit comments