Skip to content

Commit a13b895

Browse files
committed
Generate arbitrary id for MuxedAddress
1 parent 9a4cd8d commit a13b895

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

soroban-sdk/src/muxed_address.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,13 @@ impl TryFromVal<Env, ScAddress> for MuxedAddress {
279279
#[cfg(any(test, feature = "testutils"))]
280280
#[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]
281281
impl crate::testutils::MuxedAddress for MuxedAddress {
282-
fn generate(env: &Env, id: u64) -> crate::MuxedAddress {
282+
fn generate(env: &Env) -> crate::MuxedAddress {
283283
let sc_val = ScVal::Address(crate::env::internal::xdr::ScAddress::MuxedAccount(
284284
crate::env::internal::xdr::MuxedEd25519Account {
285285
ed25519: crate::env::internal::xdr::Uint256(
286286
env.with_generator(|mut g| g.address()),
287287
),
288-
id,
288+
id: env.with_generator(|mut g| g.mux_id()),
289289
},
290290
));
291291
sc_val.try_into_val(env).unwrap()

soroban-sdk/src/tests/muxed_address.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn test_contract_address_to_muxed_address_conversion() {
3131
#[test]
3232
fn test_muxed_address_component_getters() {
3333
let env = Env::default();
34-
let muxed_address = MuxedAddress::generate(&env, 123_456);
34+
let muxed_address = MuxedAddress::generate(&env);
3535
let mut expected_id = [0_u8; 32];
3636
expected_id[31] = 1;
3737
let expected_address = Address::try_from_val(
@@ -42,7 +42,6 @@ fn test_muxed_address_component_getters() {
4242
)
4343
.unwrap();
4444
assert_eq!(muxed_address.address(), expected_address);
45-
assert_eq!(muxed_address.id(), Some(123_456));
4645

4746
let muxed_address_with_another_id = MuxedAddress::new(muxed_address, u64::MAX);
4847
assert_eq!(muxed_address_with_another_id.address(), expected_address);

soroban-sdk/src/testutils.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,16 @@ impl AuthSnapshot {
178178
#[serde(rename_all = "snake_case")]
179179
pub struct Generators {
180180
address: u64,
181-
nonce: u64,
181+
nonce: i64,
182+
mux_id: u64,
182183
}
183184

184185
impl Default for Generators {
185186
fn default() -> Generators {
186187
Generators {
187188
address: 0,
188189
nonce: 0,
190+
mux_id: 0,
189191
}
190192
}
191193
}
@@ -230,7 +232,12 @@ impl Generators {
230232

231233
pub fn nonce(&mut self) -> i64 {
232234
self.nonce = self.nonce.checked_add(1).unwrap();
233-
self.nonce as i64
235+
self.nonce
236+
}
237+
238+
pub fn mux_id(&mut self) -> u64 {
239+
self.mux_id = self.mux_id.checked_add(1).unwrap();
240+
self.mux_id
234241
}
235242
}
236243

@@ -439,12 +446,11 @@ pub trait Address {
439446
}
440447

441448
pub trait MuxedAddress {
442-
/// Create a new MuxedAddress with arbitrary `Address` part and provided
443-
/// multiplexing identifier.
449+
/// Create a new MuxedAddress with arbitrary `Address` and id parts.
444450
///
445451
/// Note, that since currently only accounts can be multiplexed, the
446452
/// underlying `Address` will be an account (not contract) address.
447-
fn generate(env: &Env, id: u64) -> crate::MuxedAddress;
453+
fn generate(env: &Env) -> crate::MuxedAddress;
448454

449455
/// Returns a new `MuxedAddress` that has the same `Address` part as the
450456
/// provided `address` and the provided multiplexing id.

0 commit comments

Comments
 (0)