[Terra.js] 2. 簡単ガイド(接続、クエリー、キー)

3809 ワード

Making a connection
ユーザがブロックチェーンと対話する場合、2つの方法がある.
アクセス
  • データ自体(データ照会)
  • ブロックチェーンの状態(ブロードキャストtransacion)
  • を変更
    ただし、これらの操作を実行するには、接続を確立する必要があります.LCD-Clientオブジェクトを使用して、HTTPを介してRESTful APIを提供する液晶画面を動作させるノードを表すブロックチェーンに接続できます.Terra.jsは、初期API呼び出しの詳細を抽象化および処理するための簡潔なインタフェースを提供する.
    import { LCDClient } from '@terra-money/terra.js';
    
    const terra = new LCDClient({
       URL: 'https://lcd.terra.dev',
       chainID: 'columbus-3'
    });
    Querying
    LCDクライアントインスタンスを介してブロックチェーンに接続すると、ブロックチェーンのデータ照会が開始されます.データアクセスは、複数のモジュールAPIからなり、LCDクライアントインスタンスにアクセスできます.バックグラウンドでHTTPリクエストが発行されるので、ネットワークIO中にブロックされないことを確認します.
    // 예시코드
    async main() {
      const marketParams = await terra.market.parameters();
      const exchangeRates = await terra.oracle.exchangeRates();
      console.log(marketParams.base_pool);
      console.log(exchangeRates.get('uusd'));
    }
    
    main();
    使用可能なモジュールのタイプ
    auth
    bank
    distribution
    gov
    market
    mint
    msgauth
    oracle
    slashing
    staking
    supply
    tendermint
    treasury
    tx
    wasm
    Keys
    Terra.jsでアカウントを使用して操作を実行するには、アカウント署名機能の鍵が必要です.

    key interface

    interface Key {
      publicKey: Buffer;
      accAddress: AccAddress;
      valAddress: ValAddress;
      accPubKey: AccPubKey;
      valPubKey: ValPubKey;
      
      createSignature(tx: StdSignMsg): StdSignature;
      signTx(tx: StdSignMsg): Promise<StdTx>;
      sign(payload: Buffer): Promise<Buffer>;
    }
    Terra.jsは、署名機能を有するアカウントをプログラムにロードする方法を提供するいくつかの標準鍵実装を提供する.

    RawKey


    最も基本的な鍵はRawKeyです.デフォルトの秘密鍵.
    import { RawKey } from '@terra-money/terra.js';
    
    const rk = new RawKey("<private key>");
    秘密鍵は作成したRawKeyで接続できます
    console.log(rk.privateKey);

    MnemonicKey

    import { MnemonicKey } from '@terra-money/terra.js';
    
    const mk = new MnemonicKey({
      mnemonic: "<24-word mnemonic>",
    });
    ランダムなmnemonicを作成する場合は、mnemonicKeyを作成します.
    const mk = new MnemonicKey();
    console.log(mk.mnemonic);
    BIP 4 HD pathでmnemonkeyを使用して財布を復元することもできます.
    : m/44'/coinType′/{coinType}'/coinType′/{account}'/0/${index}
    const mk = new MnemonicKey({
      mnemonic: "<seed-phrase>", // optional, will be random if not provided
      coinType: 330, // optional, default
      account: 0, // optional, default
      index: 0, // optional, default
    });
    たとえば、ATOM(118)のコインタイプを使用して古いTeraWallet HDパスからNimonicを復元するには、次の手順に従います.
    const mk = new MnemonicKey({
      mnemonic: "<seed-phrase>",
      coinType: 118
    });

    CLIKey


    commingsoon

    Custom Key Implementation


    前述はTerrajs公式ドキュメンタリーを参考に書いた文章.
    commingsoon