コンテンツにスキップ

ニーモニックからアカウントを作成する⚓︎

初級

このチュートリアルでは、ニーモニックフレーズ(単に ニーモニック とも呼ばれます)を使って、NEM の アカウント を作成する方法を説明します。

この方法は、1 つのシードから複数のアカウントを管理する HD ウォレット で一般的に使用されます。

前提条件⚓︎

まだ準備できていない場合は、まず 開発環境のセットアップ を行ってください。

完全なコード⚓︎

import os

from symbolchain.Bip32 import Bip32
from symbolchain.facade.NemFacade import NemFacade

# Initialize the facade for the testnet network
facade = NemFacade('testnet')

# Use an existing mnemonic if provided, otherwise generate a random one
bip32 = Bip32(NemFacade.BIP32_CURVE_NAME)
mnemonic = os.getenv('MNEMONIC')
if mnemonic:
    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 default
password = os.getenv('PASSWORD', 'correcthorsebatterystaple')
print(f'Password: {password}')

# Derive a root Bip32 node from the mnemonic and a password
root_node = bip32.from_mnemonic(mnemonic, password)

# Derive a child Bip32 node for the account at index 0
account_index = 0
child_node = root_node.derive_path(facade.bip32_path(account_index))

# Convert the Bip32 node to a signing key pair
key_pair = facade.bip32_node_to_key_pair(child_node)

# Derive the address from the public key
address = facade.network.public_key_to_address(key_pair.public_key)

# Output the account details
print(f'Address: {address}')
print(f'Public key: {key_pair.public_key}')
print(f'Private key: {key_pair.private_key}')

Download source

import { Bip32 } from 'symbol-sdk';
import { NemFacade } from 'symbol-sdk/nem';
import process from 'process';

// Initialize the facade for the testnet network
const facade = new NemFacade('testnet');

// Use an existing mnemonic if provided, otherwise generate a random one
const bip32 = new Bip32(NemFacade.BIP32_CURVE_NAME);
let mnemonic = 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 default
const password = process.env.PASSWORD || 'correcthorsebatterystaple';
console.log('Password:', password);

// Derive a root Bip32 node from the mnemonic and a password
const rootNode = bip32.fromMnemonic(mnemonic, password);

// Derive a child Bip32 node for the account at index 0
const accountIndex = 0;
const childNode = rootNode.derivePath(facade.bip32Path(accountIndex));

// Convert the Bip32 node to a signing key pair
const keyPair = NemFacade.bip32NodeToKeyPair(childNode);

// Derive the address from the public key
const address = facade.network.publicKeyToAddress(keyPair.publicKey);

// Output the account details
console.log('Address:', address.toString());
console.log('Public key:', keyPair.publicKey.toString());
console.log('Private key:', keyPair.privateKey.toString());

Download source

コードの説明⚓︎

ファサードを初期化する⚓︎

# Initialize the facade for the testnet network
facade = NemFacade('testnet')
// Initialize the facade for the testnet network
const facade = new NemFacade('testnet');

は、NEM の暗号処理とネットワークユーティリティへのアクセスを提供します。 ネットワーク名(testnet または mainnet)で初期化することで、アドレス などのネットワーク固有の値が正しく生成されるようにします。

ニーモニックを定義する⚓︎

# Use an existing mnemonic if provided, otherwise generate a random one
bip32 = Bip32(NemFacade.BIP32_CURVE_NAME)
mnemonic = os.getenv('MNEMONIC')
if mnemonic:
    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 one
const bip32 = new Bip32(NemFacade.BIP32_CURVE_NAME);
let mnemonic = 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);

この例では、環境変数 MNEMONIC に既存のニーモニックがあるかを確認します。 環境変数が設定されている場合は、そこからニーモニックを読み込みます。 設定されていない場合は、 を使って新しいランダムなニーモニックを生成します。

NEM は BIP39 標準を使用します。 この標準では、ニーモニックを標準化された単語リストから選ばれた 24 個の英単語で表します。 これらの単語は、派生するすべての秘密鍵を作成するために使われるエントロピー(ランダム性)を符号化します。

ニーモニックフレーズを安全に保管してください

ニーモニックフレーズを使うと、派生したすべてのアカウントと秘密鍵を再生成できます。 ニーモニックフレーズにアクセスできる人はアカウントを管理でき、失うと永久にアクセスできなくなります。

ニーモニックは決して他人と共有せず、必ず安全な場所に保管してください。

ルートノードを導出する⚓︎

# Load password from environment variable or use default
password = os.getenv('PASSWORD', 'correcthorsebatterystaple')
print(f'Password: {password}')

# Derive a root Bip32 node from the mnemonic and a password
root_node = bip32.from_mnemonic(mnemonic, password)
// Load password from environment variable or use default
const password = process.env.PASSWORD || 'correcthorsebatterystaple';
console.log('Password:', password);

// Derive a root Bip32 node from the mnemonic and a password
const rootNode = bip32.fromMnemonic(mnemonic, password);

ニーモニックを定義した後、 はニーモニックとパスワードをルートノードに変換します。 ルートノードは、子アカウントを導出する際の開始点になります。

パスワードは「25 番目の単語」と呼ばれることもある、ニーモニックシードを拡張するオプションの文字列です。 空のままにすることも、任意の値を設定することもできます。 使用すると、セキュリティをさらに高められます。 同じニーモニックでも、パスワードが異なればまったく異なるアカウントが生成されます。

パスワードのセキュリティ

パスワードはアカウントの導出に含まれます。 アカウントを再生成するには、ニーモニックとパスワードの両方が必要です。 どちらかを失うと、派生したすべてのアカウントにアクセスできなくなります。

この例では、パスワードを環境変数 PASSWORD から読み込みます。 設定されていない場合、スニペットはデフォルト値を使用します。

子アカウントを導出する⚓︎

# Derive a child Bip32 node for the account at index 0
account_index = 0
child_node = root_node.derive_path(facade.bip32_path(account_index))
// Derive a child Bip32 node for the account at index 0
const accountIndex = 0;
const childNode = rootNode.derivePath(facade.bip32Path(accountIndex));

ルートノードは、それぞれ固有のキーとアドレスを持つ複数のアカウントを生成できます。 これにより、1 つのニーモニックで多くのアカウントを管理しながら、暗号学的には分離できます。

アカウントを導出するには、アカウントインデックスを指定する必要があります。 は、そのインデックスに対する導出パス(どのアカウントを導出するかを指定する標準化された文字列)を生成し、 はそのパスに従ってアカウントを作成します。

この例では、インデックス 0 のアカウントを導出します。 別のインデックス(例: 123、...)を使えば、追加のアカウントを導出できます。 各インデックスからは、まったく異なるアカウントが生成されます。

アカウントを作成する⚓︎

# Convert the Bip32 node to a signing key pair
key_pair = facade.bip32_node_to_key_pair(child_node)

# Derive the address from the public key
address = facade.network.public_key_to_address(key_pair.public_key)

# Output the account details
print(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 pair
const keyPair = NemFacade.bip32NodeToKeyPair(childNode);

// Derive the address from the public key
const address = facade.network.publicKeyToAddress(keyPair.publicKey);

// Output the account details
console.log('Address:', address.toString());
console.log('Public key:', keyPair.publicKey.toString());
console.log('Private key:', keyPair.privateKey.toString());

子ノードを導出したら、使用可能なキーペアとアドレスに変換します。

  1. キーペアの作成: は子ノードから 秘密鍵公開鍵 を取り出します。 秘密鍵は秘密にしておく必要がありますが、公開鍵は安全に共有できます。

  2. アドレスの導出: は公開鍵を アドレス に変換します。 アドレスは、アカウントを識別する、より短く人間が読みやすいネットワーク固有の識別子です。

出力⚓︎

以下は、プログラムの実行時の出力例です。

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

環境変数なしでコードを実行するたびに、異なるランダムニーモニックとアカウントが生成されます。 同じニーモニックとパスワードを指定した場合は、指定したアカウントインデックスから常に同じアカウントが導出されます。

まとめ⚓︎

このチュートリアルでは、次の方法を説明しました。

手順 関連ドキュメント
ランダムなニーモニックを作成する
ニーモニックからアカウントを導出する
アカウントのキーペアを取得する