Generate a Mnemonic
The following example will generate a mnemonic using a Client
instance. Since this functionality doesn't need to
communicate with any nodes, you can start your Client
in offline mode.
The following code example will:
- Create a
Client
in offline mode. - Use the created client to generate a mnemonic.
Code Examples
- Rust
- Nodejs
- Python
- Java
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! cargo run --example generate_mnemonic --release
use iota_client::{Client, Result};
#[tokio::main]
async fn main() -> Result<()> {
let mnemonic = Client::generate_mnemonic()?;
println!("Mnemonic: {mnemonic}");
Ok(())
}
Run the example by running the following command:
cargo run --example generate_mnemonic --release
// Copyright 2021-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { Client, initLogger } from '@iota/client';
// Run with command:
// node ./dist/01_mnemonic.js
// In this example we will generate a random BIP39 mnemonic
async function run() {
initLogger();
const client = new Client({});
try {
const mnemonic = await client.generateMnemonic();
console.log('Mnemonic: ' + mnemonic);
// Example output:
// Mnemonic: endorse answer radar about source reunion marriage tag sausage weekend frost daring base attack because joke dream slender leisure group reason prepare broken river
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());
You can run the example by running the following command from the bindings/node/examples/
folder:
node dist/01_mnemonic.js
from iota_client import IotaClient
# Create an IotaClient instance
client = IotaClient()
# Generate a random BIP39 mnemonic
mnemonic = client.generate_mnemonic()
print(f'Mnemonic: {mnemonic}')
You can run the example by running the following command from the binding/python/examples
folder:
python3 01_mnemonic.py
import org.iota.Client;
import org.iota.types.ClientConfig;
import org.iota.types.expections.ClientException;
import org.iota.types.expections.InitializeClientException;
public class GenerateMnemonic {
public static void main(String[] args) throws ClientException, InitializeClientException {
// Build the client.
Client client = new Client(new ClientConfig().withNodes(new String[]{"https://api.testnet.shimmer.network"}));
// Generate a mnemonic.
String mnemonic = client.generateMnemonic();
// Print the mnemonic.
System.out.println(mnemonic);
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
Mnemonic: glance zoo dove legend razor season evoke twenty flush solution essence destroy loud traffic friend decorate emotion total borrow expect actor switch shock zer
Mnemonic: glance zoo dove legend razor season evoke twenty flush solution essence destroy loud traffic friend decorate emotion total borrow expect actor switch shock zer
Mnemonic: glance zoo dove legend razor season evoke twenty flush solution essence destroy loud traffic friend decorate emotion total borrow expect actor switch shock zer
glance zoo dove legend razor season evoke twenty flush solution essence destroy loud traffic friend decorate emotion total borrow expect actor switch shock zer