コンテンツにスキップ

秘密鍵からアカウントを作成する⚓︎

初級

このチュートリアルでは、既存の 秘密鍵 を使うか、新しいランダムアカウントを生成して、NEM の アカウント を作成する方法を説明します。

前提条件⚓︎

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

完全なコード⚓︎

import os

from symbolchain.CryptoTypes import PrivateKey
from symbolchain.facade.NemFacade import NemFacade

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

# Use an existing private key if provided,
# Otherwise generate a random one.
private_key_string = os.getenv('PRIVATE_KEY')
if private_key_string:
    print('Loading account from environment variable...')
    private_key = PrivateKey(private_key_string)
else:
    print('Generating random account...')
    private_key = PrivateKey.random()

# Create a key pair from the private key
key_pair = facade.KeyPair(private_key)

# Derive the public key from the private key
public_key = key_pair.public_key

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

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

Download source

import { PrivateKey } 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 private key if provided,
// Otherwise generate a random one.
const privateKeyString = process.env.PRIVATE_KEY;
let privateKey;
if (privateKeyString) {
    console.log('Loading account from environment variable...');
    privateKey = new PrivateKey(privateKeyString);
} else {
    console.log('Generating random account...');
    privateKey = PrivateKey.random();
}

// Create a key pair from the private key
const keyPair = new NemFacade.KeyPair(privateKey);

// Derive the public key from the private key
const publicKey = keyPair.publicKey;

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

// Output the account details
console.log('Address:', address.toString());
console.log('Public key:', publicKey.toString());
console.log('Private key:', 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 private key if provided,
# Otherwise generate a random one.
private_key_string = os.getenv('PRIVATE_KEY')
if private_key_string:
    print('Loading account from environment variable...')
    private_key = PrivateKey(private_key_string)
else:
    print('Generating random account...')
    private_key = PrivateKey.random()
// Use an existing private key if provided,
// Otherwise generate a random one.
const privateKeyString = process.env.PRIVATE_KEY;
let privateKey;
if (privateKeyString) {
    console.log('Loading account from environment variable...');
    privateKey = new PrivateKey(privateKeyString);
} else {
    console.log('Generating random account...');
    privateKey = PrivateKey.random();
}

この例では、まず環境変数 PRIVATE_KEY から秘密鍵を 16 進数文字列として取得します。 環境変数が設定されている場合は、その値を オブジェクトに変換します。 設定されていない場合は、代わりに を使って新しいランダムな秘密鍵を生成します。

秘密鍵を安全に保管してください

秘密鍵があれば、アカウントとそこに保有されている資産を完全に管理することができます。 秘密鍵を失うと、アカウントに永久にアクセスできなくなります。 他人が秘密鍵を入手すると、その人物がアカウントを管理できてしまいます。

秘密鍵は決して誰とも共有せず、必ず安全な場所に保管してください。

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

# Create a key pair from the private key
key_pair = facade.KeyPair(private_key)

# Derive the public key from the private key
public_key = key_pair.public_key

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

# Output the account details
print(f'Address: {address}')
print(f'Public key: {public_key}')
print(f'Private key: {private_key}')
// Create a key pair from the private key
const keyPair = new NemFacade.KeyPair(privateKey);

// Derive the public key from the private key
const publicKey = keyPair.publicKey;

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

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

秘密鍵を定義した後、公開鍵とアドレスを導出してアカウントを作成します。

  1. キーペアの作成: コンストラクターは秘密鍵を受け取り、対応する 公開鍵 を数学的に導出します。 秘密鍵は秘密にしておく必要がありますが、公開鍵は安全に誰とでも共有できます。

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

出力⚓︎

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

Loading account from environment variable...
Address: TBONKWCOWBZYZB2I5JD3LSDBQVBYHB757VN3SKPP
Public key: 462EE976890916E54FA825D26BDD0235F5EB5B6A143C199AB0AE5EE9328E08CE
Private key: 0000000000000000000000000000000000000000000000000000000000000000

環境変数なしでプログラムを実行するたびに、異なるランダムなアカウントが生成されます。 秘密鍵を指定した場合は、常に同じ公開鍵とアドレスが導出されます。

まとめ⚓︎

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

手順 関連ドキュメント
秘密鍵を読み込む
ランダムな秘密鍵を作成する
公開鍵を取得する
アドレスを取得する

次のステップ⚓︎

アカウントを作成できたので、次のことを行えます。