LoginSignup
17
12

More than 5 years have passed since last update.

ethers.jsの概要、実装方法

Last updated at Posted at 2018-04-12

ethers.jsとは

公式ドキュメントでは以下のように書かれています。
https://docs.ethers.io/ethers.js/html/

This library is designed to make it easier to write client-side JavaScript based wallets, keeping the private key on the owners machine at all times.

また、下記ブログではこんなことも書かれています。
https://medium.com/l4-media/announcing-ethers-js-a-web3-alternative-6f134fdd06f3

In short, ethers.js is an alternative to web3, the most commonly used library for ethereum applications.

特徴

以下のような特徴を持っています。

  • ノードから独立したwallet機能をもつ。クライアント側に閉じた状態でアドレスの生成・復元などができる。
  • web3でできることは基本的に全てできる(らしい)ので、webの代替となる。
  • ライブラリ単体でBIP39&BIP32に対応したウォレットを作成できる。
  • Fallback Provider、ENSの設定などをデフォルトでサポートしている。

ウォレットの生成方法

以下の5つの方法でウォレットが生成できます。

  • Private Key
    • 既存のPrivate Keyから復元したウォレット。
  • Mnemonic Phrase
    • 既存のMnemonicから復元したウォレット。
  • Random Wallet
    • cryptoモジュールのrandomBytesを用いてランダムに生成されるウォレット。
  • Secret Storage Wallet (e.g. Geth or Parity)
    • ノードで生成されるkeystoreより復元されるウォレット
  • Brain Wallet
    • 任意のidとパスワードからハッシュを作成してPrivate Keyとするウォレット。クラックされる危険性があるため非推奨とされている。

実装方法

web3.jsとehters.jsの実装方法を比べてみます。
(TypeScriptで必要最低限の実装をしています。)

1. web3.jsの場合

import Web3 from 'web3';
import { default as contract } from 'truffle-contract';

export class Web3Service {
  private web3: Web3;

  // 初期化
  constructor() {
    this.web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/<your-api-key>'));
    // or
    // this.web3 = new Web3(window.web3.currentProvider);
  }

  // コントラクト取得
  public getContract(abi: any, address: string): any {
    return new this.web3.eth.Contract(abi, address);
  }

  // ウォレット作成(ランダム)
  getNewWallet(): any {
    return this.web3.eth.accounts.create();
  }
}

2. ehrers.jsの場合

import * as ethers from 'ethers';

declare const web3: any;

export class EthersService {
  private currentProvider: any;

  // 初期化
  constructor() {
    const providers = ethers.providers;
    const network = providers.networks['mainnet'];

    const web3Provider = new providers.Web3Provider(web3.currentProvider, network);
    const infuraProvider = new providers.InfuraProvider(network, '<your-api-key>');

    this.currentProvider = new providers.FallbackProvider([ web3Provider, infuraProvider ]);
  }

  // コントラクト取得
  getContract(abi: any, address: string): any {
    return new ethers.Contract(address, abi, this.currentProvider);
  }

  // ウォレット作成(ランダム)
  getNewWallet(): any {
    return ethers.Wallet.createRandom();
  }
}

懸念点

  • その他ライブラリでweb3に依存しているものが存在する場合は、ethers.jsだけでは完結できず結局web.jsが必要になるのでは。
  • WebSoketモードで起動しているノードに対応していない。(issueには上がっている。)

<参考>
https://medium.com/@julien.m./what-is-an-ethereum-keystore-file-86c8c5917b97
https://security.srad.jp/story/16/02/16/0641240/

17
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
12