@@ -14,6 +14,7 @@ use serde_with::{DeserializeFromStr, SerializeDisplay};
1414use crate as plutus_ledger_api;
1515use crate :: csl:: csl_to_pla:: { FromCSL , TryFromCSL , TryFromCSLError , TryToPLA } ;
1616use crate :: csl:: pla_to_csl:: { TryFromPLA , TryFromPLAError , TryToCSL } ;
17+ use crate :: error:: ConversionError ;
1718use crate :: plutus_data:: {
1819 parse_constr, parse_fixed_len_constr_fields, IsPlutusData , PlutusData , PlutusDataError ,
1920} ;
@@ -39,7 +40,7 @@ pub struct Address {
3940}
4041
4142impl Address {
42- pub fn with_extra_info < ' a > ( & ' a self , network_tag : u8 ) -> AddressWithExtraInfo < ' a > {
43+ pub fn with_extra_info ( self , network_tag : u8 ) -> AddressWithExtraInfo {
4344 AddressWithExtraInfo {
4445 address : self ,
4546 network_tag,
@@ -89,13 +90,13 @@ impl TryFromCSL<csl::Address> for Address {
8990#[ cfg_attr( feature = "serde" , derive( SerializeDisplay , DeserializeFromStr ) ) ]
9091/// Address with network information. The `WithExtraInfo` variant has Display instance, serializing into
9192/// a bech32 address format.
92- pub struct AddressWithExtraInfo < ' a > {
93- pub address : & ' a Address ,
93+ pub struct AddressWithExtraInfo {
94+ pub address : Address ,
9495 pub network_tag : u8 ,
9596}
9697
97- impl TryFromPLA < AddressWithExtraInfo < ' _ > > for csl:: Address {
98- fn try_from_pla ( val : & AddressWithExtraInfo < ' _ > ) -> Result < Self , TryFromPLAError > {
98+ impl TryFromPLA < AddressWithExtraInfo > for csl:: Address {
99+ fn try_from_pla ( val : & AddressWithExtraInfo ) -> Result < Self , TryFromPLAError > {
99100 let payment = val. address . credential . try_to_csl ( ) ?;
100101
101102 Ok ( match val. address . staking_credential {
@@ -113,20 +114,71 @@ impl TryFromPLA<AddressWithExtraInfo<'_>> for csl::Address {
113114 }
114115}
115116
117+ impl TryFromCSL < csl:: Address > for AddressWithExtraInfo {
118+ fn try_from_csl ( value : & csl:: Address ) -> Result < Self , TryFromCSLError >
119+ where
120+ Self : Sized ,
121+ {
122+ Ok ( AddressWithExtraInfo {
123+ address : value. try_to_pla ( ) ?,
124+ network_tag : value. network_id ( ) . map_err ( |err| {
125+ TryFromCSLError :: ImpossibleConversion ( format ! (
126+ "Couldn't extract network tag from address: {err}"
127+ ) )
128+ } ) ?,
129+ } )
130+ }
131+ }
132+
116133/// Serializing into a bech32 address format.
117- impl std:: fmt:: Display for AddressWithExtraInfo < ' _ > {
134+ impl std:: fmt:: Display for AddressWithExtraInfo {
118135 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
119136 let bech32_addr: Option < String > = self
120137 . try_to_csl ( )
121138 . ok ( )
122139 . and_then ( |csl_addr : csl:: Address | csl_addr. to_bech32 ( None ) . ok ( ) ) ;
123140 match bech32_addr {
124141 Some ( addr) => write ! ( f, "{}" , addr) ,
125- None => write ! ( f , "INVALID ADDRESS {:?}" , self ) ,
142+ None => Err ( std :: fmt :: Error ) ,
126143 }
127144 }
128145}
129146
147+ impl FromStr for AddressWithExtraInfo {
148+ type Err = ConversionError ;
149+
150+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
151+ let csl_addr = csl:: Address :: from_bech32 ( s) . map_err ( |err| {
152+ ConversionError :: ParseError ( anyhow ! (
153+ "Error while parsing bech32 address '{}': {}" ,
154+ s,
155+ err
156+ ) )
157+ } ) ?;
158+
159+ let address = Address :: try_from_csl ( & csl_addr) . map_err ( |err| {
160+ ConversionError :: ParseError ( anyhow ! (
161+ "Error while parsing bech32 address '{}': {}" ,
162+ s,
163+ err
164+ ) )
165+ } ) ?;
166+
167+ let network_tag = csl_addr. network_id ( ) . map_err ( |err| {
168+ ConversionError :: ParseError ( anyhow ! (
169+ "Error while parsing extracting network tag from address '{}': {}" ,
170+ s,
171+ err
172+ ) )
173+ } ) ?;
174+
175+ Ok ( AddressWithExtraInfo {
176+ address,
177+ network_tag,
178+ } )
179+ }
180+ }
181+
130182////////////////
131183// Credential //
132184////////////////
0 commit comments