importosfromsymbolchain.Bip32importBip32fromsymbolchain.facade.NemFacadeimportNemFacade# Initialize the facade for the testnet networkfacade=NemFacade('testnet')# Use an existing mnemonic if provided, otherwise generate a random onebip32=Bip32(NemFacade.BIP32_CURVE_NAME)mnemonic=os.getenv('MNEMONIC')ifmnemonic:print('Loading mnemonic phrase from environment variable...')else:print('Generating random mnemonic phrase...')mnemonic=bip32.random()print(f'Mnemonic phrase: {mnemonic}')# Load password from environment variable or use defaultpassword=os.getenv('PASSWORD','correcthorsebatterystaple')print(f'Password: {password}')# Derive a root Bip32 node from the mnemonic and a passwordroot_node=bip32.from_mnemonic(mnemonic,password)# Derive a child Bip32 node for the account at index 0account_index=0child_node=root_node.derive_path(facade.bip32_path(account_index))# Convert the Bip32 node to a signing key pairkey_pair=facade.bip32_node_to_key_pair(child_node)# Derive the address from the public keyaddress=facade.network.public_key_to_address(key_pair.public_key)# Output the account detailsprint(f'Address: {address}')print(f'Public key: {key_pair.public_key}')print(f'Private key: {key_pair.private_key}')
import{Bip32}from'symbol-sdk';import{NemFacade}from'symbol-sdk/nem';importprocessfrom'process';// Initialize the facade for the testnet networkconstfacade=newNemFacade('testnet');// Use an existing mnemonic if provided, otherwise generate a random oneconstbip32=newBip32(NemFacade.BIP32_CURVE_NAME);letmnemonic=process.env.MNEMONIC;if(mnemonic){console.log('Loading mnemonic phrase from environment variable...');}else{console.log('Generating random mnemonic phrase...');mnemonic=bip32.random();}console.log('Mnemonic phrase:',mnemonic);// Load password from environment variable or use defaultconstpassword=process.env.PASSWORD||'correcthorsebatterystaple';console.log('Password:',password);// Derive a root Bip32 node from the mnemonic and a passwordconstrootNode=bip32.fromMnemonic(mnemonic,password);// Derive a child Bip32 node for the account at index 0constaccountIndex=0;constchildNode=rootNode.derivePath(facade.bip32Path(accountIndex));// Convert the Bip32 node to a signing key pairconstkeyPair=NemFacade.bip32NodeToKeyPair(childNode);// Derive the address from the public keyconstaddress=facade.network.publicKeyToAddress(keyPair.publicKey);// Output the account detailsconsole.log('Address:',address.toString());console.log('Public key:',keyPair.publicKey.toString());console.log('Private key:',keyPair.privateKey.toString());
//JAVA 21+//DEPS org.symbol:symbol-sdk:3.3.3importorg.symbol.sdk.Bip32;importorg.symbol.sdk.CryptoTypes;importorg.symbol.sdk.facade.NemFacade;importorg.symbol.sdk.nem.Address;importorg.symbol.sdk.nem.KeyPair;publicfinalclassCreateFromMnemonic{// Initialize the facade for the testnet networkprivatefinalNemFacadefacade=newNemFacade("testnet");publicstaticvoidmain(finalString[]args){newCreateFromMnemonic().run();}privatevoidrun(){// Use an existing mnemonic if provided,// otherwise generate a random one.finalBip32bip32=newBip32(NemFacade.BIP32_CURVE_NAME);Stringmnemonic=System.getenv("MNEMONIC");if(null!=mnemonic){System.out.println("Loading mnemonic phrase from environment variable...");}else{System.out.println("Generating random mnemonic phrase...");mnemonic=bip32.random();}System.out.printf("Mnemonic phrase: %s%n",mnemonic);// Load password from environment variable or use defaultfinalStringpassword=System.getenv().getOrDefault("PASSWORD","correcthorsebatterystaple");System.out.printf("Password: %s%n",password);// Derive a root Bip32 node from the mnemonic and a passwordfinalBip32.Bip32NoderootNode=bip32.fromMnemonic(mnemonic,password);// Derive a child Bip32 node for the account at index 0finalintaccountIndex=0;finalBip32.Bip32NodechildNode=rootNode.derivePath(facade.bip32Path(accountIndex));// Convert the Bip32 node to a signing key pairfinalKeyPairkeyPair=NemFacade.bip32NodeToKeyPair(childNode);// Derive the address from the public keyfinalCryptoTypes.PublicKeypublicKey=keyPair.getPublicKey();finalAddressaddress=facade.network.publicKeyToAddress(publicKey);// Output the account detailsSystem.out.printf("Address: %s%n",address);System.out.printf("Public key: %s%n",publicKey);System.out.printf("Private key: %s%n",keyPair.getPrivateKey());}}
// Initialize the facade for the testnet networkprivatefinalNemFacadefacade=newNemFacade("testnet");
The provides access to NEM's cryptographic operations and network utilities.
It is initialized with a network name (testnet or mainnet) to ensure that network-specific values,
such as addresses, are generated correctly.
# Use an existing mnemonic if provided, otherwise generate a random onebip32=Bip32(NemFacade.BIP32_CURVE_NAME)mnemonic=os.getenv('MNEMONIC')ifmnemonic:print('Loading mnemonic phrase from environment variable...')else:print('Generating random mnemonic phrase...')mnemonic=bip32.random()print(f'Mnemonic phrase: {mnemonic}')
// Use an existing mnemonic if provided, otherwise generate a random oneconstbip32=newBip32(NemFacade.BIP32_CURVE_NAME);letmnemonic=process.env.MNEMONIC;if(mnemonic){console.log('Loading mnemonic phrase from environment variable...');}else{console.log('Generating random mnemonic phrase...');mnemonic=bip32.random();}console.log('Mnemonic phrase:',mnemonic);
// Use an existing mnemonic if provided,// otherwise generate a random one.finalBip32bip32=newBip32(NemFacade.BIP32_CURVE_NAME);Stringmnemonic=System.getenv("MNEMONIC");if(null!=mnemonic){System.out.println("Loading mnemonic phrase from environment variable...");}else{System.out.println("Generating random mnemonic phrase...");mnemonic=bip32.random();}System.out.printf("Mnemonic phrase: %s%n",mnemonic);
The example checks for an existing mnemonic in the MNEMONIC environment variable.
If the variable is set, the mnemonic is loaded from it.
Otherwise, a new random mnemonic is generated using .
NEM uses the BIP39 standard, which represents
mnemonics as 24 English words selected from a standardized word list.
These words encode the entropy (randomness) used to create all derived private keys.
Store your mnemonic phrase securely
The mnemonic phrase can be used to regenerate all derived accounts and private keys.
Anyone with access to it can control your accounts, and losing it means losing access permanently.
Never share your mnemonic with anyone, and always store it in a secure location.
# Load password from environment variable or use defaultpassword=os.getenv('PASSWORD','correcthorsebatterystaple')print(f'Password: {password}')# Derive a root Bip32 node from the mnemonic and a passwordroot_node=bip32.from_mnemonic(mnemonic,password)
// Load password from environment variable or use defaultconstpassword=process.env.PASSWORD||'correcthorsebatterystaple';console.log('Password:',password);// Derive a root Bip32 node from the mnemonic and a passwordconstrootNode=bip32.fromMnemonic(mnemonic,password);
// Load password from environment variable or use defaultfinalStringpassword=System.getenv().getOrDefault("PASSWORD","correcthorsebatterystaple");System.out.printf("Password: %s%n",password);// Derive a root Bip32 node from the mnemonic and a passwordfinalBip32.Bip32NoderootNode=bip32.fromMnemonic(mnemonic,password);
After defining the mnemonic, converts the mnemonic and a password into a root node,
which serves as the starting point for deriving child accounts.
The password (sometimes called a "25th word") is an optional string that extends the mnemonic seed.
It can be left empty or set to any value.
When used, it adds another layer of security.
Different passwords with the same mnemonic produce completely different accounts.
Password security
The password is part of the account derivation.
Both the mnemonic and password are required to regenerate the accounts.
If you lose either one, you lose access to all derived accounts.
In this example, the password is loaded from the PASSWORD environment variable.
If not set, the snippet uses a default one.
// Derive a child Bip32 node for the account at index 0finalintaccountIndex=0;finalBip32.Bip32NodechildNode=rootNode.derivePath(facade.bip32Path(accountIndex));
The root node can generate multiple accounts, each with its own unique keys and address.
This allows a single mnemonic to manage many accounts while keeping them cryptographically isolated.
Deriving an account requires specifying an account index.
generates the derivation path
(a standardized string that specifies which account to derive) for that index,
and follows that path to create the account.
In this example, the account at index 0 is derived.
Additional accounts can be derived by using different indices (e.g., 1, 2, 3, ...).
Each index produces a completely different account.
# Convert the Bip32 node to a signing key pairkey_pair=facade.bip32_node_to_key_pair(child_node)# Derive the address from the public keyaddress=facade.network.public_key_to_address(key_pair.public_key)# Output the account detailsprint(f'Address: {address}')print(f'Public key: {key_pair.public_key}')print(f'Private key: {key_pair.private_key}')
// Convert the Bip32 node to a signing key pairconstkeyPair=NemFacade.bip32NodeToKeyPair(childNode);// Derive the address from the public keyconstaddress=facade.network.publicKeyToAddress(keyPair.publicKey);// Output the account detailsconsole.log('Address:',address.toString());console.log('Public key:',keyPair.publicKey.toString());console.log('Private key:',keyPair.privateKey.toString());
// Convert the Bip32 node to a signing key pairfinalKeyPairkeyPair=NemFacade.bip32NodeToKeyPair(childNode);// Derive the address from the public keyfinalCryptoTypes.PublicKeypublicKey=keyPair.getPublicKey();finalAddressaddress=facade.network.publicKeyToAddress(publicKey);// Output the account detailsSystem.out.printf("Address: %s%n",address);System.out.printf("Public key: %s%n",publicKey);System.out.printf("Private key: %s%n",keyPair.getPrivateKey());
Once the child node is derived, it is converted into a usable key pair and address.
Key pair creation: extracts the private key and public key from the
child node.
The private key must remain secret, while the public key can be safely shared.
Address derivation: converts the public key into an address, a shorter,
human-readable, network-specific identifier for the account.
The output shown below corresponds to a typical run of the program.
Generating random mnemonic phrase...
Mnemonic phrase: hamster diagram private dutch cause delay private meat slide toddler razor book happy fancy gospel tennis maple dilemma loan word shrug inflict delay *****
Password: correcthorsebatterystaple
Address: TBONKWCOWBZYZB2I5JD3LSDBQVBYHB757VN3SKPP
Public key: 462EE976890916E54FA825D26BDD0235F5EB5B6A143C199AB0AE5EE9328E08CE
Private key: 0000000000000000000000000000000000000000000000000000000000000000
Each time the code runs without environment variables, it generates a different random mnemonic and account.
If the same mnemonic and password are provided, the same account is always derived for the given account index.