miden-objects 0.12.4

Core components of the Miden protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
mod r#type;
use alloc::string::ToString;

pub use r#type::AddressType;

mod routing_parameters;
use alloc::borrow::ToOwned;

pub use routing_parameters::RoutingParameters;

mod interface;
mod network_id;
use alloc::string::String;

pub use interface::AddressInterface;
use miden_processor::DeserializationError;
pub use network_id::{CustomNetworkId, NetworkId};

use crate::AddressError;
use crate::account::AccountStorageMode;
use crate::crypto::ies::SealingKey;
use crate::note::NoteTag;
use crate::utils::serde::{ByteWriter, Deserializable, Serializable};

mod address_id;
pub use address_id::AddressId;

/// A user-facing address in Miden.
///
/// An address consists of an [`AddressId`] and optional [`RoutingParameters`].
///
/// A user who wants to receive a note creates an address and sends it to the sender of the note.
/// The sender creates a note intended for the holder of this address ID (e.g., it provides
/// discoverability and potentially access-control) and the routing parameters inform the sender
/// about various aspects like:
/// - what kind of note the receiver's account can consume.
/// - how the receiver discovers the note.
/// - how to encrypt the note for the receiver.
///
/// It can be encoded to a string using [`Self::encode`] and decoded using [`Self::decode`].
/// If routing parameters are present, the ID and parameters are separated by
/// [`Address::SEPARATOR`].
///
/// ## Example
///
/// ```text
/// # account ID
/// mm1apt3l475qemeqqp57xjycfdwcvw0sfhq
/// # account ID + routing parameters (interface & note tag length)
/// mm1apt3l475qemeqqp57xjycfdwcvw0sfhq_qruqqypuyph
/// # account ID + routing parameters (interface, note tag length, encryption key)
/// mm1apt3l475qemeqqp57xjycfdwcvw0sfhq_qruqqqgqjmsgjsh3687mt2w0qtqunxt3th442j48qwdnezl0fv6qm3x9c8zqsv7pku
/// ```
///
/// The encoding of an address without routing parameters matches the encoding of the underlying
/// identifier exactly (e.g. an account ID). This provides compatibility between identifiers and
/// addresses and gives end-users a hint that an address is only an extension of the identifier
/// (e.g. their account's ID) that they are likely to recognize.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Address {
    id: AddressId,
    routing_params: Option<RoutingParameters>,
}

impl Address {
    // CONSTANTS
    // --------------------------------------------------------------------------------------------

    /// The separator character in an encoded address between the ID and routing parameters.
    pub const SEPARATOR: char = '_';

    // CONSTRUCTORS
    // --------------------------------------------------------------------------------------------

    /// Returns a new address from an [`AddressId`] and routing parameters set to `None`.
    ///
    /// To set routing parameters, use [`Self::with_routing_parameters`].
    pub fn new(id: impl Into<AddressId>) -> Self {
        Self { id: id.into(), routing_params: None }
    }

    /// For local (both public and private) accounts, up to 30 bits can be encoded into the tag.
    /// For network accounts, the tag length must be set to 30 bits.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The tag length routing parameter is not [`NoteTag::DEFAULT_NETWORK_TAG_LENGTH`] for
    ///   network accounts.
    pub fn with_routing_parameters(
        mut self,
        routing_params: RoutingParameters,
    ) -> Result<Self, AddressError> {
        if let Some(tag_len) = routing_params.note_tag_len() {
            match self.id {
                AddressId::AccountId(account_id) => {
                    if account_id.storage_mode() == AccountStorageMode::Network
                        && tag_len != NoteTag::DEFAULT_NETWORK_TAG_LENGTH
                    {
                        return Err(AddressError::CustomTagLengthNotAllowedForNetworkAccounts(
                            tag_len,
                        ));
                    }
                },
            }
        }

        self.routing_params = Some(routing_params);

        Ok(self)
    }

    // ACCESSORS
    // --------------------------------------------------------------------------------------------

    /// Returns the identifier of the address.
    pub fn id(&self) -> AddressId {
        self.id
    }

    /// Returns the [`AddressInterface`] of the account to which the address points.
    pub fn interface(&self) -> Option<AddressInterface> {
        self.routing_params.as_ref().map(RoutingParameters::interface)
    }

    /// Returns the preferred tag length.
    ///
    /// This is guaranteed to be in range `0..=30` (e.g. the maximum of
    /// [`NoteTag::MAX_LOCAL_TAG_LENGTH`] and [`NoteTag::DEFAULT_NETWORK_TAG_LENGTH`]).
    pub fn note_tag_len(&self) -> u8 {
        self.routing_params
            .as_ref()
            .and_then(RoutingParameters::note_tag_len)
            .unwrap_or(self.id.default_note_tag_len())
    }

    /// Returns a note tag derived from this address.
    pub fn to_note_tag(&self) -> NoteTag {
        let note_tag_len = self.note_tag_len();

        match self.id {
            AddressId::AccountId(id) => {
                match id.storage_mode() {
                  AccountStorageMode::Network => NoteTag::from_network_account_id(id),
                  AccountStorageMode::Private | AccountStorageMode::Public => {
                      NoteTag::from_local_account_id(id, note_tag_len)
                          .expect("address should validate that tag len does not exceed MAX_LOCAL_TAG_LENGTH bits")
                    }
                }
            },
        }
    }

    /// Returns the optional public encryption key from routing parameters.
    ///
    /// This key can be used for sealed box encryption when sending notes to this address.
    pub fn encryption_key(&self) -> Option<&SealingKey> {
        self.routing_params.as_ref().and_then(RoutingParameters::encryption_key)
    }

    /// Encodes the [`Address`] into a string.
    ///
    /// ## Encoding
    ///
    /// The encoding of an address into a string is done as follows:
    /// - Encode the underlying [`AddressId`] to a bech32 string.
    /// - If routing parameters are present:
    ///   - Append the [`Address::SEPARATOR`] to that string.
    ///   - Append the encoded routing parameters to that string.
    pub fn encode(&self, network_id: NetworkId) -> String {
        let mut encoded = match self.id {
            AddressId::AccountId(id) => id.to_bech32(network_id),
        };

        if let Some(routing_params) = &self.routing_params {
            encoded.push(Self::SEPARATOR);
            encoded.push_str(&routing_params.encode_to_string());
        }

        encoded
    }

    /// Decodes an address string into the [`NetworkId`] and an [`Address`].
    ///
    /// See [`Address::encode`] for details on the format. The procedure for decoding the string
    /// into the address are the inverse operations of encoding.
    pub fn decode(address_str: &str) -> Result<(NetworkId, Self), AddressError> {
        if address_str.ends_with(Self::SEPARATOR) {
            return Err(AddressError::TrailingSeparator);
        }

        let mut split = address_str.split(Self::SEPARATOR);
        let encoded_identifier = split
            .next()
            .ok_or_else(|| AddressError::decode_error("identifier missing in address string"))?;

        let (network_id, identifier) = AddressId::decode(encoded_identifier)?;

        let mut address = Address::new(identifier);

        if let Some(encoded_routing_params) = split.next() {
            let routing_params = RoutingParameters::decode(encoded_routing_params.to_owned())?;
            address = address.with_routing_parameters(routing_params)?;
        }

        Ok((network_id, address))
    }
}

impl Serializable for Address {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        self.id.write_into(target);
        self.routing_params.write_into(target);
    }
}

impl Deserializable for Address {
    fn read_from<R: miden_core::utils::ByteReader>(
        source: &mut R,
    ) -> Result<Self, DeserializationError> {
        let identifier: AddressId = source.read()?;
        let routing_params: Option<RoutingParameters> = source.read()?;

        let mut address = Self::new(identifier);

        if let Some(routing_params) = routing_params {
            address = address
                .with_routing_parameters(routing_params)
                .map_err(|err| DeserializationError::InvalidValue(err.to_string()))?;
        }

        Ok(address)
    }
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
    use alloc::boxed::Box;
    use alloc::str::FromStr;

    use assert_matches::assert_matches;
    use bech32::{Bech32, Bech32m, NoChecksum};

    use super::*;
    use crate::AccountIdError;
    use crate::account::{AccountId, AccountType};
    use crate::address::CustomNetworkId;
    use crate::errors::Bech32Error;
    use crate::testing::account_id::{ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET, AccountIdBuilder};

    /// Tests that an account ID address can be encoded and decoded.
    #[test]
    fn address_encode_decode_roundtrip() -> anyhow::Result<()> {
        // We use this to check that encoding does not panic even when using the longest possible
        // HRP.
        let longest_possible_hrp =
            "01234567890123456789012345678901234567890123456789012345678901234567890123456789012";
        assert_eq!(longest_possible_hrp.len(), 83);

        let rng = &mut rand::rng();
        for network_id in [
            NetworkId::Mainnet,
            NetworkId::Custom(Box::new(CustomNetworkId::from_str("custom").unwrap())),
            NetworkId::Custom(Box::new(CustomNetworkId::from_str(longest_possible_hrp).unwrap())),
        ] {
            for (idx, account_id) in [
                AccountIdBuilder::new()
                    .account_type(AccountType::FungibleFaucet)
                    .build_with_rng(rng),
                AccountIdBuilder::new()
                    .account_type(AccountType::NonFungibleFaucet)
                    .build_with_rng(rng),
                AccountIdBuilder::new()
                    .account_type(AccountType::RegularAccountImmutableCode)
                    .build_with_rng(rng),
                AccountIdBuilder::new()
                    .account_type(AccountType::RegularAccountUpdatableCode)
                    .build_with_rng(rng),
            ]
            .into_iter()
            .enumerate()
            {
                // Encode/Decode without routing parameters should be valid.
                let mut address = Address::new(account_id);

                let bech32_string = address.encode(network_id.clone());
                assert!(
                    !bech32_string.contains(Address::SEPARATOR),
                    "separator should not be present in address without routing params"
                );
                let (decoded_network_id, decoded_address) = Address::decode(&bech32_string)?;

                assert_eq!(network_id, decoded_network_id, "network id failed in {idx}");
                assert_eq!(address, decoded_address, "address failed in {idx}");

                let AddressId::AccountId(decoded_account_id) = address.id();
                assert_eq!(account_id, decoded_account_id);

                // Encode/Decode with routing parameters should be valid.
                address = address.with_routing_parameters(
                    RoutingParameters::new(AddressInterface::BasicWallet)
                        .with_note_tag_len(NoteTag::DEFAULT_NETWORK_TAG_LENGTH)?,
                )?;

                let bech32_string = address.encode(network_id.clone());
                assert!(
                    bech32_string.contains(Address::SEPARATOR),
                    "separator should be present in address without routing params"
                );
                let (decoded_network_id, decoded_address) = Address::decode(&bech32_string)?;

                assert_eq!(network_id, decoded_network_id, "network id failed in {idx}");
                assert_eq!(address, decoded_address, "address failed in {idx}");

                let AddressId::AccountId(decoded_account_id) = address.id();
                assert_eq!(account_id, decoded_account_id);
            }
        }

        Ok(())
    }

    #[test]
    fn address_decoding_fails_on_trailing_separator() -> anyhow::Result<()> {
        let id = AccountIdBuilder::new()
            .account_type(AccountType::FungibleFaucet)
            .build_with_rng(&mut rand::rng());

        let address = Address::new(id);
        let mut encoded_address = address.encode(NetworkId::Devnet);
        encoded_address.push(Address::SEPARATOR);

        let err = Address::decode(&encoded_address).unwrap_err();
        assert_matches!(err, AddressError::TrailingSeparator);

        Ok(())
    }

    /// Tests that an invalid checksum returns an error.
    #[test]
    fn bech32_invalid_checksum() -> anyhow::Result<()> {
        let network_id = NetworkId::Mainnet;
        let account_id = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET)?;
        let address = Address::new(account_id).with_routing_parameters(
            RoutingParameters::new(AddressInterface::BasicWallet).with_note_tag_len(14)?,
        )?;

        let bech32_string = address.encode(network_id);
        let mut invalid_bech32_1 = bech32_string.clone();
        invalid_bech32_1.remove(0);
        let mut invalid_bech32_2 = bech32_string.clone();
        invalid_bech32_2.remove(7);

        let error = Address::decode(&invalid_bech32_1).unwrap_err();
        assert_matches!(error, AddressError::Bech32DecodeError(Bech32Error::DecodeError(_)));

        let error = Address::decode(&invalid_bech32_2).unwrap_err();
        assert_matches!(error, AddressError::Bech32DecodeError(Bech32Error::DecodeError(_)));

        Ok(())
    }

    /// Tests that an unknown address type returns an error.
    #[test]
    fn bech32_unknown_address_type() {
        let invalid_bech32_address =
            bech32::encode::<Bech32m>(NetworkId::Mainnet.into_hrp(), &[250]).unwrap();

        let error = Address::decode(&invalid_bech32_address).unwrap_err();
        assert_matches!(
            error,
            AddressError::Bech32DecodeError(Bech32Error::UnknownAddressType(250))
        );
    }

    /// Tests that a bech32 using a disallowed checksum returns an error.
    #[test]
    fn bech32_invalid_other_checksum() {
        let account_id = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET).unwrap();
        let address_id_bytes = AddressId::from(account_id).to_bytes();

        // Use Bech32 instead of Bech32m which is disallowed.
        let invalid_bech32_regular =
            bech32::encode::<Bech32>(NetworkId::Mainnet.into_hrp(), &address_id_bytes).unwrap();
        let error = Address::decode(&invalid_bech32_regular).unwrap_err();
        assert_matches!(error, AddressError::Bech32DecodeError(Bech32Error::DecodeError(_)));

        // Use no checksum instead of Bech32m which is disallowed.
        let invalid_bech32_no_checksum =
            bech32::encode::<NoChecksum>(NetworkId::Mainnet.into_hrp(), &address_id_bytes).unwrap();
        let error = Address::decode(&invalid_bech32_no_checksum).unwrap_err();
        assert_matches!(error, AddressError::Bech32DecodeError(Bech32Error::DecodeError(_)));
    }

    /// Tests that a bech32 string encoding data of an unexpected length returns an error.
    #[test]
    fn bech32_invalid_length() {
        let account_id = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET).unwrap();
        let mut address_id_bytes = AddressId::from(account_id).to_bytes();
        // Add one byte to make the length invalid.
        address_id_bytes.push(5);

        let invalid_bech32 =
            bech32::encode::<Bech32m>(NetworkId::Mainnet.into_hrp(), &address_id_bytes).unwrap();

        let error = Address::decode(&invalid_bech32).unwrap_err();
        assert_matches!(
            error,
            AddressError::AccountIdDecodeError(AccountIdError::Bech32DecodeError(
                Bech32Error::InvalidDataLength { .. }
            ))
        );
    }

    /// Tests that an Address can be serialized and deserialized
    #[test]
    fn address_serialization() -> anyhow::Result<()> {
        let rng = &mut rand::rng();

        for account_type in [
            AccountType::FungibleFaucet,
            AccountType::NonFungibleFaucet,
            AccountType::RegularAccountImmutableCode,
            AccountType::RegularAccountUpdatableCode,
        ]
        .into_iter()
        {
            let account_id = AccountIdBuilder::new().account_type(account_type).build_with_rng(rng);
            let address = Address::new(account_id).with_routing_parameters(
                RoutingParameters::new(AddressInterface::BasicWallet)
                    .with_note_tag_len(NoteTag::DEFAULT_NETWORK_TAG_LENGTH)?,
            )?;

            let serialized = address.to_bytes();
            let deserialized = Address::read_from_bytes(&serialized)?;
            assert_eq!(address, deserialized);
        }

        Ok(())
    }

    /// Tests that an address with encryption key can be created and used.
    #[test]
    fn address_with_encryption_key() -> anyhow::Result<()> {
        use crate::crypto::dsa::eddsa_25519::SecretKey;
        use crate::crypto::ies::{SealingKey, UnsealingKey};

        let rng = &mut rand::rng();
        let account_id = AccountIdBuilder::new()
            .account_type(AccountType::FungibleFaucet)
            .build_with_rng(rng);

        // Create keypair using rand::rng()
        let secret_key = SecretKey::with_rng(rng);
        let public_key = secret_key.public_key();
        let sealing_key = SealingKey::X25519XChaCha20Poly1305(public_key.clone());
        let unsealing_key = UnsealingKey::X25519XChaCha20Poly1305(secret_key.clone());

        // Create address with encryption key
        let address = Address::new(account_id).with_routing_parameters(
            RoutingParameters::new(AddressInterface::BasicWallet)
                .with_encryption_key(sealing_key.clone()),
        )?;

        // Verify encryption key is present
        let retrieved_key =
            address.encryption_key().expect("encryption key should be present").clone();
        assert_eq!(retrieved_key, sealing_key);

        // Test seal/unseal round-trip
        let plaintext = b"hello world";
        let sealed_message =
            retrieved_key.seal_bytes(rng, plaintext).expect("sealing should succeed");
        let decrypted =
            unsealing_key.unseal_bytes(sealed_message).expect("unsealing should succeed");
        assert_eq!(decrypted.as_slice(), plaintext);

        Ok(())
    }

    /// Tests that an address with encryption key can be encoded/decoded.
    #[test]
    fn address_encryption_key_encode_decode() -> anyhow::Result<()> {
        use crate::crypto::dsa::eddsa_25519::SecretKey;

        let rng = &mut rand::rng();
        // Use a local account type (RegularAccountImmutableCode) instead of network
        // (FungibleFaucet)
        let account_id = AccountIdBuilder::new()
            .account_type(AccountType::RegularAccountImmutableCode)
            .storage_mode(AccountStorageMode::Public)
            .build_with_rng(rng);

        // Create keypair
        let secret_key = SecretKey::with_rng(rng);
        let public_key = secret_key.public_key();
        let sealing_key = SealingKey::X25519XChaCha20Poly1305(public_key);

        // Create address with encryption key
        let address = Address::new(account_id).with_routing_parameters(
            RoutingParameters::new(AddressInterface::BasicWallet)
                .with_encryption_key(sealing_key.clone()),
        )?;

        // Encode and decode
        let encoded = address.encode(NetworkId::Mainnet);
        let (decoded_network, decoded_address) = Address::decode(&encoded)?;

        assert_eq!(decoded_network, NetworkId::Mainnet);
        assert_eq!(address, decoded_address);

        // Verify encryption key is preserved
        let decoded_key = decoded_address
            .encryption_key()
            .expect("encryption key should be present")
            .clone();
        assert_eq!(decoded_key, sealing_key);

        Ok(())
    }
}