コンテンツにスキップ
初級

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

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

この手法は、単一のシードから複数のアカウントを管理するために、HDウォレット(階層的決定性ウォレット)が一般的に使用されます。

前提条件⚓︎

開発環境のセットアップがまだ完了していない場合は、開発環境のセットアップ から始めてください。

完全なコード⚓︎

このチュートリアルの完全なコード一覧を以下に示します。 詳細な手順ごとの説明は次のセクションで行います。

import os
from symbolchain.Bip32 import Bip32
from symbolchain.facade.SymbolFacade import SymbolFacade

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

# Use an existing mnemonic if provided, otherwise generate a random one
bip32 = Bip32()
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 { SymbolFacade } from 'symbol-sdk/symbol';
import process from 'process';

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

// Use an existing mnemonic if provided, otherwise generate a random one
const bip32 = new Bip32(facade.constructor.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 = facade.constructor.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 = SymbolFacade('testnet')
// Initialize the facade for the testnet network
const facade = new SymbolFacade('testnet');

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

ニーモニックの定義⚓︎

# Use an existing mnemonic if provided, otherwise generate a random one
bip32 = Bip32()
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(facade.constructor.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 環境変数に既存のニーモニックがあるかを確認します。 変数に値がある場合は、そこからニーモニックが読み込まれます。 そうでない場合は、 を使用して新しいランダムなニーモニックが生成されます。

Symbolは 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));

ルートノードは複数のアカウントを生成でき、それぞれが独自の一意の鍵とアドレスを持ちます。 これにより、単一のニーモニックで多くのアカウントを管理しながら、それらを暗号学的に分離しておくことができます。

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

この例では、インデックス 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 = facade.constructor.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: east actual egg series spot express addict always human swallow decrease turn surround direct place burst million curious dish divorce net nephew allow *****
Password: correcthorsebatterystaple
Address: TCHBDENCLKEBILBPWP3JPB2XNY64OE7PYHHE32I
Public key: 3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29
Private key: 0000000000000000000000000000000000000000000000000000000000000000

環境変数を指定せずにコードを実行するたびに、異なるランダムなニーモニックとアカウントが生成されます。 同じニーモニックとパスワードが提供されれば、指定されたアカウントインデックスに対して常に同じアカウントが派生します。

結論⚓︎

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

ステップ 関連ドキュメント
ランダムなニーモニックを作成する
ニーモニックからアカウントを導出させる
アカウントの鍵ペアを取得する ,