Skip to content

Commit 96071e3

Browse files
committed
Change clone_with_id function to new constructor.
1 parent 2c73c92 commit 96071e3

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

soroban-sdk/src/muxed_address.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,26 @@ impl crate::testutils::MuxedAddress for MuxedAddress {
291291
sc_val.try_into_val(env).unwrap()
292292
}
293293

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 {
294+
fn new<T: Into<MuxedAddress>>(address: T, id: u64) -> crate::MuxedAddress {
295+
let address: MuxedAddress = address.into();
296+
let sc_val = ScVal::try_from_val(&address.env, address.as_val()).unwrap();
297+
let account_id = match sc_val {
297298
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!(),
299+
ScAddress::MuxedAccount(muxed_account) => muxed_account.ed25519,
300+
ScAddress::Account(crate::env::internal::xdr::AccountId(
301+
crate::env::internal::xdr::PublicKey::PublicKeyTypeEd25519(account_id),
302+
)) => account_id,
303+
ScAddress::Contract(_) => panic!("contract addresses can not be multiplexed"),
304+
ScAddress::ClaimableBalance(_) | ScAddress::LiquidityPool(_) => unreachable!(),
305305
},
306306
_ => unreachable!(),
307-
}
308-
sc_val.try_into_val(&self.env).unwrap()
307+
};
308+
let result_sc_val = ScVal::Address(ScAddress::MuxedAccount(
309+
crate::env::internal::xdr::MuxedEd25519Account {
310+
id,
311+
ed25519: account_id,
312+
},
313+
));
314+
result_sc_val.try_into_val(&address.env).unwrap()
309315
}
310316
}

soroban-sdk/src/tests/muxed_address.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ fn test_muxed_address_component_getters() {
4444
assert_eq!(muxed_address.address(), expected_address);
4545
assert_eq!(muxed_address.id(), Some(123_456));
4646

47-
let muxed_address_with_another_id = muxed_address.clone_with_id(u64::MAX);
47+
let muxed_address_with_another_id = MuxedAddress::new(muxed_address, u64::MAX);
4848
assert_eq!(muxed_address_with_another_id.address(), expected_address);
4949
assert_eq!(muxed_address_with_another_id.id(), Some(u64::MAX));
50+
51+
let muxed_address_from_address = MuxedAddress::new(muxed_address_with_another_id.address(), 0);
52+
assert_eq!(muxed_address_from_address.address(), expected_address);
53+
assert_eq!(muxed_address_from_address.id(), Some(0));
5054
}

soroban-sdk/src/testutils.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,17 @@ pub trait MuxedAddress {
446446
/// underlying `Address` will be an account (not contract) address.
447447
fn generate(env: &Env, id: u64) -> crate::MuxedAddress;
448448

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;
449+
/// Returns a new `MuxedAddress` that has the same `Address` part as the
450+
/// provided `address` and the provided multiplexing id.
451+
///
452+
/// `address` can be either an `Address` or `MuxedAddress` and it has to
453+
/// be an account (non-contract) address.
454+
///
455+
/// Note on usage: the simplest way to test `MuxedAddress` is to generate
456+
/// an arbitrary valid address with `MuxedAddress::generate`, then
457+
/// `MuxedAddress::new` can be used to alter only the multiplexing id part
458+
/// of that address.
459+
fn new<T: Into<crate::MuxedAddress>>(address: T, id: u64) -> crate::MuxedAddress;
452460
}
453461

454462
pub trait Deployer {

0 commit comments

Comments
 (0)