Skip to main content

Generate an Address

After you have created an account, you have a single address in the account. But you can generate more addresses.

Account Approaches
Iota.js

You can also find this guide in the native iota.js library

Code Example

The following example will:

  1. Create an account manager.
  2. Get Alice's account which was created in the first guide.
  3. Generate an address in Alice's account.
Dotenv

This example uses dotenv, which is not safe for use in production environments.

// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example generate_address --release
// In this example we will generate an address
// Rename `.env.example` to `.env` first

use std::env;

use dotenv::dotenv;
use iota_wallet::{account_manager::AccountManager, Result};

#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production
dotenv().ok();

// Create the account manager
let manager = AccountManager::builder().finish().await?;

// Get the account we generated with `01_create_wallet`
let account = manager.get_account("Alice").await?;

// Set the stronghold password
manager
.set_stronghold_password(&env::var("STRONGHOLD_PASSWORD").unwrap())
.await?;

let address = account.generate_addresses(1, None).await?;
println!("Generated address: {}", address[0].address().to_bech32());

Ok(())
}

Run the example by running the following command:

cargo run --example generate_address --release

Expected Output

Generated address: rms1qzzk86qv30l4e85ljtccxa0ruy8y7u8zn2dle3g8dv2tl2m4cu227a7n2wj