Skip to main content

Generate Addresses

You can generate an IOTA address using the Client.get_addresses() function that will return a list of tuples with the generated addresses.

The following code example will:

  1. Create a Client which will connect to the Shimmer Testnet.
  2. Create a SecretManager from a mnemonic.
  3. Generate a public address.
  4. Generate an internal address.
  5. Generate an offline address.
Iota.js

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

Code Example

Dotenv

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

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

//! cargo run --example generate_addresses --release -- [NODE URL]

use iota_client::{
api::GetAddressesBuilder,
secret::{mnemonic::MnemonicSecretManager, SecretManager},
Client, Result,
};

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

// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());

// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;

let secret_manager = SecretManager::Mnemonic(MnemonicSecretManager::try_from_mnemonic(
&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap(),
)?);

// Generate addresses with default account index and range
let addresses = client.get_addresses(&secret_manager).finish().await?;

println!("List of generated public addresses:\n{addresses:#?}\n");

// Generate addresses with custom account index and range
let addresses = client
.get_addresses(&secret_manager)
.with_account_index(0)
.with_range(0..4)
.finish()
.await?;

println!("List of generated public addresses:\n{addresses:#?}\n");

// Generating addresses with `client.get_addresses(&secret_manager)`, will by default get the bech32_hrp (Bech32
// human readable part) from the node info, generating it "offline" requires setting it with
// `with_bech32_hrp(bech32_hrp)`
let addresses = GetAddressesBuilder::new(&secret_manager)
.with_bech32_hrp(client.get_bech32_hrp().await?)
.with_account_index(0)
.with_range(0..4)
.finish()
.await?;

println!("List of offline generated public addresses:\n{addresses:#?}\n");

Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example generate_addresses --release -- https://api.testnet.shimmer.network
  • You can replace https://api.testnet.shimmer.network with any node url.

Expected Output

First public address: rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy