開発日報

窓際エンジニアの開発備忘。日報は嘘です。

NodeJsでイーサリアム触ってみる

概要

こちらの公式サイトが参考になります。

ドキュメントやチュートリアルも豊富です。

ある程度ブロックチェーンの仕組みがわかっているとよいです。

今回のサンプル完成品はこちら

準備

infuraサービス登録

今回Infuraというサービスを利用します。

開発者が簡単にイーサリアムをテストできるように、イーサリアムのノードと便利なAPIを提供してくれています。

infura.io

↑に登録し、イーサリアムのプロジェクトを作成し、「Ropsten」(テスト環境)のエンドポイントURLを取得してください。

https://ropsten.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxx

ローカル環境構築

nodejs は8.10.0以上ならおそらくどれでもOK(?)です。
以下のコマンドを順に実行してください。

$ mkdir eth-test
$ cd eth-test
$ mkdir src
$ touch src/eth-test.js
$ npm install web3@1.0.0-beta.28

コード・解説

以下、コード全文。
今回は1ファイルで済みます。

// set up
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/8f14e867632c4318a350a99a52c57ee8'))

// create eth account
var aliceKeys = web3.eth.accounts.create();
console.log(aliceKeys);
var bobKeys = web3.eth.accounts.create();
console.log(bobKeys);

// get balance
var getBalance = function(addr) {
  web3.eth.getBalance(addr)
    .then(console.log);
};
getBalance(aliceKeys.address);

// create transaction and sign to transation
var signTransaction = function() {
  var tx = {
    from: aliceKeys.addr,
    gasPrice: "200000000",
    gas: "420000",
    to: bobKeys.addr,
    value: "100000",
    data: ""
  }

  web3.eth.accounts.signTransaction(
    tx, aliceKeys.privateKey
  ).then(function(signedTx) {
    console.log(signedTx.rawTransaction);

    // submit transaction
    web3.eth.sendSignedTransaction(signedTx.rawTransaction)
      .then(console.log)
      .catch(console.log);
    });
};
signTransaction()

セットアップ

infura登録時に取得したエンドポイントのURLを記載してください。

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/xxxxxxxxx'))

送金用のアカウントを作成します。
今回はボブとアリス間で送金します。

// create eth account
var aliceKeys = web3.eth.accounts.create();
console.log(aliceKeys);
var bobKeys = web3.eth.accounts.create();
console.log(bobKeys);

実行結果は以下の感じになります。
addressが各人のイーサリアムブロックチェーンアドレスです。

送金に際してはこちらのアドレスを使用します。

{
  address: '0xBbE4784A841485Ffd3857eA4CfD79f976Dd012c6',
  privateKey: '0xd92a94e71d9dc4d8a1970fe244fe5514aa653361670494dc7e845019eb20031b',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt]
}
{
  address: '0x4bc4AE06a98A0fd78e95f26576A0dA44B73C3ad5',
  privateKey: '0x0b54c174c88ee0d674b5ee230741d317be708feb3c8d805ce30d58d3800794ff',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt]
}

残高確認

引数 addr に各人のブロックチェーンアドレスを指定して残高を確認します。

var getBalance = function(addr) {
  web3.eth.getBalance(addr)
    .then(console.log);
};
getBalance(aliceKeys.address);

トランザクション(送金処理)の準備

こんな感じです。from to に各人のアドレスを指定します。
valueに送金金額をいれます。

gasについてはこちら

var tx = {
    from: aliceKeys.addr,
    gasPrice: "200000000",
    gas: "420000",
    to: bobKeys.addr,
    value: "100000",
    data: ""
  }

イーサリアムネットワークにトランザクションを送信する。

web3.eth.accounts.signTransaction秘密鍵トランザクションに署名しています。

その後、web3.eth.sendSignedTransactionトランザクションを送信します。

このとき引数に指定している、「signedTx.rawTransaction」は署名付きトランザクションの16進数となります。

// create transaction and sign to transation
var signTransaction = function() {
  var tx = {
    from: aliceKeys.addr,
    gasPrice: "200000000",
    gas: "420000",
    to: bobKeys.addr,
    value: "100000",
    data: ""
  }

  web3.eth.accounts.signTransaction(
    tx, aliceKeys.privateKey
  ).then(function(signedTx) {
    console.log(signedTx.rawTransaction);

    // submit transaction
    web3.eth.sendSignedTransaction(signedTx.rawTransaction)
      .then(console.log)
      .catch(console.log);
    });
};
signTransaction()

実行

$ node src/eth-test.js