Skip to content

Commit c5077b8

Browse files
committed
!fixup address review comments
1 parent f8be55e commit c5077b8

File tree

5 files changed

+65
-38
lines changed

5 files changed

+65
-38
lines changed

soroban-sdk/src/address.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ impl Debug for Address {
5555
let strkey = Strkey::Contract(Contract(contract_id.0));
5656
write!(f, "Contract({})", strkey.to_string())?;
5757
}
58-
_ => return Err(core::fmt::Error),
58+
ScAddress::MuxedAccount(_)
59+
| ScAddress::ClaimableBalance(_)
60+
| ScAddress::LiquidityPool(_) => {
61+
return Err(core::fmt::Error);
62+
}
5963
}
6064
} else {
6165
return Err(core::fmt::Error);

soroban-sdk/src/env.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -354,27 +354,6 @@ impl Env {
354354
internal::Env::require_auth(self, address.to_object()).unwrap_infallible();
355355
}
356356

357-
#[doc(hidden)]
358-
pub(crate) fn get_address_from_muxed_address(
359-
&self,
360-
muxed_address: MuxedAddressObject,
361-
) -> Address {
362-
Address::try_from_val(
363-
self,
364-
&internal::Env::get_address_from_muxed_address(self, muxed_address).unwrap_infallible(),
365-
)
366-
.unwrap_infallible()
367-
}
368-
369-
#[doc(hidden)]
370-
pub(crate) fn get_id_from_muxed_address(&self, muxed_address: MuxedAddressObject) -> u64 {
371-
u64::try_from_val(
372-
self,
373-
&internal::Env::get_id_from_muxed_address(self, muxed_address).unwrap_infallible(),
374-
)
375-
.unwrap()
376-
}
377-
378357
/// Invokes a function of a contract that is registered in the [Env].
379358
///
380359
/// # Panics

soroban-sdk/src/muxed_address.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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"))]
1010
use 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")))]
271281
impl 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
}

soroban-sdk/src/tests/muxed_address.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::u64;
2+
13
use crate::testutils::MuxedAddress as _;
24
use crate::{
35
env::xdr::{AccountId, ScAddress, Uint256},
@@ -29,14 +31,20 @@ fn test_contract_address_to_muxed_address_conversion() {
2931
#[test]
3032
fn test_muxed_address_component_getters() {
3133
let env = Env::default();
32-
let muxed_address = MuxedAddress::from_account_id(&env, &[1; 32], 123_456);
34+
let muxed_address = MuxedAddress::generate(&env, 123_456);
35+
let mut expected_id = [0_u8; 32];
36+
expected_id[31] = 1;
3337
let expected_address = Address::try_from_val(
3438
&env,
3539
&ScAddress::Account(AccountId(
36-
soroban_env_host::xdr::PublicKey::PublicKeyTypeEd25519(Uint256([1; 32])),
40+
soroban_env_host::xdr::PublicKey::PublicKeyTypeEd25519(Uint256(expected_id)),
3741
)),
3842
)
3943
.unwrap();
4044
assert_eq!(muxed_address.address(), expected_address);
4145
assert_eq!(muxed_address.id(), Some(123_456));
46+
47+
let muxed_address_with_another_id = muxed_address.clone_with_id(u64::MAX);
48+
assert_eq!(muxed_address_with_another_id.address(), expected_address);
49+
assert_eq!(muxed_address_with_another_id.id(), Some(u64::MAX));
4250
}

soroban-sdk/src/testutils.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,16 @@ pub trait Address {
439439
}
440440

441441
pub trait MuxedAddress {
442-
/// Create a new MuxedAddress from a provided account public key and
442+
/// Create a new MuxedAddress with arbitrary `Address` part and provided
443443
/// multiplexing identifier.
444-
fn from_account_id(env: &Env, account_key: &[u8; 32], id: u64) -> crate::MuxedAddress;
444+
///
445+
/// Note, that since currently only accounts can be multiplexed, the
446+
/// underlying `Address` will be an account (not contract) address.
447+
fn generate(env: &Env, id: u64) -> crate::MuxedAddress;
448+
449+
/// Returns a new `MuxedAddress` that has the same `Address` part as this
450+
/// `MuxedAdress`, but a new multiplexing identifier (`new_id`).
451+
fn clone_with_id(&self, new_id: u64) -> crate::MuxedAddress;
445452
}
446453

447454
pub trait Deployer {

0 commit comments

Comments
 (0)