コンテンツにスキップ

秘密鍵からのアカウント作成⚓︎

初級

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

前提条件⚓︎

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

完全なコード⚓︎

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

import os

from symbolchain.CryptoTypes import PrivateKey
from symbolchain.facade.SymbolFacade import SymbolFacade

# Initialize the facade for the testnet network
facade = SymbolFacade('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 { SymbolFacade } from 'symbol-sdk/symbol';
import process from 'process';

// Initialize the facade for the testnet network
const facade = new SymbolFacade('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 SymbolFacade.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

//JAVA 21+
//DEPS org.symbol:symbol-sdk:3.3.1

import org.symbol.sdk.CryptoTypes;
import org.symbol.sdk.facade.SymbolFacade;
import org.symbol.sdk.symbol.Address;
import org.symbol.sdk.symbol.KeyPair;

final class CreateFromPrivateKey {
    // Initialize the facade for the testnet network
    private final SymbolFacade facade = new SymbolFacade("testnet");


    public static void main(final String[] args) {
        new CreateFromPrivateKey().run();
    }

    private void run() {
        // Use an existing private key if provided,
        // Otherwise generate a random one.
        final String privateKeyString = System.getenv("PRIVATE_KEY");
        final CryptoTypes.PrivateKey privateKey;
        if (null != privateKeyString) {
            System.out.println(
                "Loading account from environment variable...");
            privateKey = new CryptoTypes.PrivateKey(privateKeyString);
        } else {
            System.out.println("Generating random account...");
            privateKey = CryptoTypes.PrivateKey.random();
        }
        // Create a key pair from the private key
        final KeyPair keyPair = new KeyPair(privateKey);

        // Derive the public key from the private key
        final CryptoTypes.PublicKey publicKey = keyPair.getPublicKey();

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

        // Output the account details
        System.out.printf("Address: %s%n", address);
        System.out.printf("Public key: %s%n", publicKey);
        System.out.printf("Private key: %s%n", privateKey);
    }
}

Download source

コード解説⚓︎

ファサードの初期化⚓︎

# Initialize the facade for the testnet network
facade = SymbolFacade('testnet')
// Initialize the facade for the testnet network
const facade = new SymbolFacade('testnet');
    // Initialize the facade for the testnet network
    private final SymbolFacade facade = new SymbolFacade("testnet");

は、Symbolの暗号化処理とネットワークユーティリティへのアクセスを提供します。 アドレス などのネットワーク固有の値が正しく生成されるように、ネットワーク名(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();
}
        // Use an existing private key if provided,
        // Otherwise generate a random one.
        final String privateKeyString = System.getenv("PRIVATE_KEY");
        final CryptoTypes.PrivateKey privateKey;
        if (null != privateKeyString) {
            System.out.println(
                "Loading account from environment variable...");
            privateKey = new CryptoTypes.PrivateKey(privateKeyString);
        } else {
            System.out.println("Generating random account...");
            privateKey = CryptoTypes.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 SymbolFacade.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());
        // Create a key pair from the private key
        final KeyPair keyPair = new KeyPair(privateKey);

        // Derive the public key from the private key
        final CryptoTypes.PublicKey publicKey = keyPair.getPublicKey();

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

        // Output the account details
        System.out.printf("Address: %s%n", address);
        System.out.printf("Public key: %s%n", publicKey);
        System.out.printf("Private key: %s%n", privateKey);

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

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

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

出力⚓︎

以下に示す出力は、プログラムの典型的な実行結果に対応しています。

Loading account from environment variable...
Address: TCHBDENCLKEBILBPWP3JPB2XNY64OE7PYHHE32I
Public key: 3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29
Private key: 0000000000000000000000000000000000000000000000000000000000000000

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

結論⚓︎

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

ステップ 関連ドキュメント
秘密鍵の読み込む
ランダムな秘密鍵の作成
公開鍵の取得
アドレスの取得

次のステップ⚓︎

アカウントを作成したら、以下のことができます。