@@ -4,7 +4,7 @@ use super::{
44 env:: internal:: { AddressObject , Env as _, MuxedAddressObject , Tag } ,
55 ConversionError , Env , TryFromVal , TryIntoVal , Val ,
66} ;
7- use crate :: { unwrap:: UnwrapInfallible , Address } ;
7+ use crate :: { env :: internal , unwrap:: UnwrapInfallible , Address } ;
88
99#[ cfg( not( target_family = "wasm" ) ) ]
1010use crate :: env:: internal:: xdr:: { ScAddress , ScVal } ;
@@ -20,15 +20,15 @@ enum AddressObjectWrapper {
2020/// and can be used for representing the 'virtual' accounts that allows for
2121/// managing multiple balances off-chain with only a single on-chain balance
2222/// entry. The address part can be used as a regular `Address`, and the id
23- /// part can be used only in the events for the off-chain processing.
23+ /// part should be used only in the events for the off-chain processing.
2424///
2525/// This type is only necessary in a few special cases, such as token transfers
2626/// that support non-custodial accounts (e.g. for the exchange support). Prefer
2727/// using the regular `Address` type unless multiplexing support is necessary.
2828///
2929/// This type is compatible with `Address` at the contract interface level, i.e.
3030/// if a contract accepts `MuxedAddress` as an input, then its callers may still
31- /// pass `Address` into the call and nothing will break . This means that if a
31+ /// pass `Address` into the call successfully . This means that if a
3232/// contract has upgraded its interface to switch from `Address` argument to
3333/// `MuxedAddress` argument, it won't break any of its existing clients.
3434///
@@ -178,9 +178,12 @@ impl MuxedAddress {
178178 AddressObjectWrapper :: Address ( address_object) => {
179179 Address :: try_from_val ( & self . env , address_object) . unwrap_infallible ( )
180180 }
181- AddressObjectWrapper :: MuxedAddress ( muxed_address_object) => self
182- . env
183- . get_address_from_muxed_address ( * muxed_address_object) ,
181+ AddressObjectWrapper :: MuxedAddress ( muxed_address_object) => Address :: try_from_val (
182+ & self . env ,
183+ & internal:: Env :: get_address_from_muxed_address ( & self . env , * muxed_address_object)
184+ . unwrap_infallible ( ) ,
185+ )
186+ . unwrap_infallible ( ) ,
184187 }
185188 }
186189
@@ -194,9 +197,14 @@ impl MuxedAddress {
194197 pub fn id ( & self ) -> Option < u64 > {
195198 match & self . obj {
196199 AddressObjectWrapper :: Address ( _) => None ,
197- AddressObjectWrapper :: MuxedAddress ( muxed_address_object) => {
198- Some ( self . env . get_id_from_muxed_address ( * muxed_address_object) )
199- }
200+ AddressObjectWrapper :: MuxedAddress ( muxed_address_object) => Some (
201+ u64:: try_from_val (
202+ & self . env ,
203+ & internal:: Env :: get_id_from_muxed_address ( & self . env , * muxed_address_object)
204+ . unwrap_infallible ( ) ,
205+ )
206+ . unwrap ( ) ,
207+ ) ,
200208 }
201209 }
202210
@@ -251,7 +259,9 @@ impl TryFromVal<Env, ScVal> for MuxedAddress {
251259 ScAddress :: MuxedAccount ( _) => Ok ( MuxedAddressObject :: try_from_val ( env, & v) ?
252260 . try_into_val ( env)
253261 . unwrap_infallible ( ) ) ,
254- _ => panic ! ( "unsupported ScAddress type" ) ,
262+ ScAddress :: ClaimableBalance ( _) | ScAddress :: LiquidityPool ( _) => {
263+ panic ! ( "unsupported ScAddress type" )
264+ }
255265 } ,
256266 _ => panic ! ( "incorrect scval type" ) ,
257267 }
@@ -269,13 +279,32 @@ impl TryFromVal<Env, ScAddress> for MuxedAddress {
269279#[ cfg( any( test, feature = "testutils" ) ) ]
270280#[ cfg_attr( feature = "docs" , doc( cfg( feature = "testutils" ) ) ) ]
271281impl crate :: testutils:: MuxedAddress for MuxedAddress {
272- fn from_account_id ( env : & Env , account_key : & [ u8 ; 32 ] , id : u64 ) -> crate :: MuxedAddress {
282+ fn generate ( env : & Env , id : u64 ) -> crate :: MuxedAddress {
273283 let sc_val = ScVal :: Address ( crate :: env:: internal:: xdr:: ScAddress :: MuxedAccount (
274284 crate :: env:: internal:: xdr:: MuxedEd25519Account {
275- ed25519 : crate :: env:: internal:: xdr:: Uint256 ( account_key. clone ( ) ) ,
285+ ed25519 : crate :: env:: internal:: xdr:: Uint256 (
286+ env. with_generator ( |mut g| g. address ( ) ) ,
287+ ) ,
276288 id,
277289 } ,
278290 ) ) ;
279291 sc_val. try_into_val ( env) . unwrap ( )
280292 }
293+
294+ fn clone_with_id ( & self , new_id : u64 ) -> crate :: MuxedAddress {
295+ let mut sc_val = ScVal :: try_from_val ( & self . env , self . as_val ( ) ) . unwrap ( ) ;
296+ match & mut sc_val {
297+ ScVal :: Address ( address) => match address {
298+ ScAddress :: MuxedAccount ( muxed_account) => {
299+ muxed_account. id = new_id;
300+ }
301+ ScAddress :: Account ( _)
302+ | ScAddress :: Contract ( _)
303+ | ScAddress :: ClaimableBalance ( _)
304+ | ScAddress :: LiquidityPool ( _) => unreachable ! ( ) ,
305+ } ,
306+ _ => unreachable ! ( ) ,
307+ }
308+ sc_val. try_into_val ( & self . env ) . unwrap ( )
309+ }
281310}
0 commit comments