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
Wallet.rs supports a multi-account approach and a single-account approach.
Iota.js
You can also find this guide in the native iota.js library
Code Example
The following example will:
- Create an account manager.
- Get Alice's account which was created in the first guide.
- Generate an address in Alice's account.
- Rust
- Nodejs
- Python
- Java
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
/**
* This example generates a new address.
*/
const getUnlockedManager = require('./account-manager');
async function run() {
try {
const manager = await getUnlockedManager();
const account = await manager.getAccount('0');
console.log('Account:', account);
const address = await account.generateAddress();
console.log('New address:', address);
// It's also possible to generate multiple addresses
// const addresses = await account.generateAddresses(2);
// console.log('New addresses:', addresses);
// Use the Faucet to send testnet tokens to your address:
console.log("Fill your address with the Faucet: https://faucet.testnet.shimmer.network")
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();
You can run the example by running the following command from the wallet/bindings/nodejs/examples/
folder:
node 2-generate-address.js
from iota_wallet import IotaWallet
# This example generates a new address.
wallet = IotaWallet('./alice-database')
wallet.set_stronghold_password("some_hopefully_secure_password")
account = wallet.get_account('Alice')
address = account.generate_addresses(1)
# address = account.generate_addresses(
# 1, {'internal': True, 'metadata': {'syncing': True, 'network': 'Testnet'}})
print(f'Address: {address}')
You can run the example by running the following command from the binding/python/examples
folder:
python3 1-generate-address.py
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import org.iota.Wallet;
import org.iota.types.*;
import org.iota.types.account_methods.GenerateAddresses;
import org.iota.types.account_methods.SyncAccount;
import org.iota.types.exceptions.InitializeWalletException;
import org.iota.types.exceptions.WalletException;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class GenerateAddress {
public static void main(String[] args) throws WalletException, InitializeWalletException {
// This example assumes that a wallet has already been created using the ´SetupWallet.java´ example.
// If you haven't run the ´SetupWallet.java´ example yet, you must run it first to be able to load the wallet as shown below:
Wallet wallet = new Wallet(new WalletConfig()
.withClientOptions(new ClientConfig().withNodes(Env.NODE))
.withSecretManager(new StrongholdSecretManager(Env.STRONGHOLD_PASSWORD, null, Env.STRONGHOLD_VAULT_PATH))
.withCoinType(CoinType.Shimmer)
.withStoragePath(Env.STORAGE_PATH)
);
// Get account and sync it with the registered node to ensure that its balances are up-to-date.
AccountHandle a = wallet.getAccount(new AccountAlias(Env.ACCOUNT_NAME));
a.syncAccount(new SyncAccount().withOptions(new SyncOptions()));
// Generate two addresses.
AccountAddress[] addresses = a.generateAddresses(new GenerateAddresses().withAmount(2));
// Print the generated addresses.
for (AccountAddress address : addresses)
System.out.println(address.getAddress());
// In case you are done and don't need the wallet instance anymore you can destroy the instance to clean up memory.
// For this, check out the ´DestroyWallet.java´ example.
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
Generated address: rms1qzzk86qv30l4e85ljtccxa0ruy8y7u8zn2dle3g8dv2tl2m4cu227a7n2wj
Account: Account {
meta: {
index: 0,
coinType: 4219,
alias: 'Alice',
publicAddresses: [ [Object] ],
internalAddresses: [],
addressesWithUnspentOutputs: [],
outputs: {},
lockedOutputs: [],
unspentOutputs: {},
transactions: {},
pendingTransactions: [],
incomingTransactions: {}
},
messageHandler: MessageHandler { messageHandler: [External: 7fa6ffff18d0] }
}
New address: {
address: 'rms1qqqp07ychhkc3u68ueug0zqq9g0wtfgeatynr6ksm9jwud30rvlkyqnhpl5',
keyIndex: 1,
internal: false,
used: false
}
Fill your address with the Faucet: https://faucet.testnet.shimmer.network
Address: [
{
'address': 'rms1qplwj3pwn0kgamynajff2xqemu7h9rw0as7ayxt8hq0pl5wxfyxwsfdprdu',
'keyIndex': 1,
'internal': False,
'used': False
}
]
rms1qpx0mcrqq7t6up73n4na0zgsuuy4p0767ut0qq67ngctj7pg4tm2ynsuynp
rms1qz8jdgvrerzv35s43pkdkawdr9x4t6xfnhcrt5tlgsyltgpwyx9ks4c5kct
rms1qzjq2jwzp8ddh0gawgdskvtd6awlv82c8y0a9s6g7kgszn6ts95u6r4kx2n