Macのローカル環境でEthereumの送金を行う




Gethのインストール

Mac OS上でEthereumネットワークを稼働させるためにCLIクライアントであるGethをインストールします。インストールにはbrewを使用します。

$ brew tap ethereum/ethereum
$ brew install ethereum

brew tapはbrew公式以外のリポジトリをフォーミュラとしてHomebrewに追加することができるコマンドで、brew経由でinstall,uninstall,upgradeが行えるようになります。

バージョンのアップグレードは下記です。

$ brew update
$ brew upgrade ethereum

以上でEthereumのCLIクライアントであるGethが使用できるようになります。

Gethでできることとして下記があります。

  • etherの採掘
  • etherの送金
  • スマート・コントラクトの生成
  • トランザクションの生成
  • ブロックチェーンの確認

Gethでプライベート・ネットに接続するための初期設定

プライベート・ネットは自分のPC上だけのネットワークのことでEthereumの本番のネットワークに接続するわけではありません。なので、etherの採掘や送金のテストを簡単に試してみることができます。

プライベート・ネットではブロックが一個もないので最初のブロック用の設定ファイルを作ります。

eth_private_netというディレクトリにmyGenesis.jsonというファイルを作成します。

$ mkdir /User/ユーザー/eth_private_net
$ vi /User/ユーザー/eth_private_net/myGenesis.json

{
  "nonce": "0x0000000000000042",
  "timestamp": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "extraData": "0x0",
  "gasLimit": "0x8000000",
  "difficulty": "0x4000",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x3333333333333333333333333333333333333333",
  "alloc": {}
}

ブロックチェーンの初期化処理

作成したmyGenesis.jsonで最初のブロックの初期化を行います。実行後に下記のようなログが出れば成功です。

$ geth --datadir /Users/ユーザー/eth_private_net init /Users/ユーザー/eth_private_net/myGenesis.json
WARN [10-08|15:01:37] No etherbase set and no accounts found as default
INFO [10-08|15:01:37] Allocated cache and file handles database=/Users/a13139/private_work/eth_data/geth/chaindata cache=16 handles=16
INFO [10-08|15:01:37] Writing custom genesis block
INFO [10-08|15:01:37] Successfully wrote genesis state database=chaindata hash=6231b0…a0300b
INFO [10-08|15:01:37] Allocated cache and file handles database=/Users/a13139/private_work/eth_data/geth/lightchaindata cache=16 handles=16
INFO [10-08|15:01:37] Writing custom genesis block
INFO [10-08|15:01:37] Successfully wrote genesis state database=lightchaindata hash=6231b0…a0300b

実行時に下記のようなエラーが出る場合はmyGeneis.jsonの中身が間違っている可能性があります。

$ geth --datadir /Users/ユーザー/eth_private_net init /Users/ユーザー/eth_private_net/myGenesis.json
Fatal: invalid genesis file: json: cannot unmarshal hex string of odd length into Go struct field Genesis.extraData of type hexutil.Bytes

プライベート・ネットへの接続

geth --networkid "10" --nodiscover --datadir "/Users/ユーザー/eth_private_net" console
  • networkid:0,1,2,3以外の任意の整数を設定します。
  • nodiscover:他のノードを探さないという設定です。プライベート・ネットでは探す必要がないのでこのオプションをつけます。
  • console:gethのコンソールを起動するためのオプションです。

アカウント作成

送金用にアカウントを2つ作っておきます。user1とuser2という文字列はユーザー名ではなくパスワードになります。

> personal.newAccount("user1")
"0x2e401f8be61e00247e59db415d6c40afbd9e2b85"

> personal.newAccount("user2")
"0x53ea9ac1cd526f85e1a13d275027cb073c038f31"

アカウント確認

> eth.accounts
["0x2e401f8be61e00247e59db415d6c40afbd9e2b85", "0x53ea9ac1cd526f85e1a13d275027cb073c038f31"]

アカウントの所持ether確認

> eth.getBalance(eth.accounts[0])
0

coinbaseの確認

coinbaseとは採掘を行った際に報酬を受け取るアドレスのことです。初期設定は一番最初に作ったアカウントがcoinbaseに設定されます。

> eth.coinbase
"0x2e401f8be61e00247e59db415d6c40afbd9e2b85"
> eth.accounts[0]
"0x2e401f8be61e00247e59db415d6c40afbd9e2b85"

マイニングの開始と終了

> miner.start()
INFO [10-08|17:30:28] Updated mining threads threads=0
INFO [10-08|17:30:28] Transaction pool price threshold updated price=18000000000
INFO [10-08|17:30:28] Starting mining operation
null
INFO [10-08|17:30:28] Commit new mining work number=1 txs=0 uncles=0 elapsed=315.647µs
INFO [10-08|17:30:29] Successfully sealed new block number=1 hash=0f933a…441247
INFO [10-08|17:30:29] 🔨 mined potential block number=1 hash=0f933a…441247
INFO [10-08|17:30:29] Commit new mining work number=2 txs=0 uncles=0 elapsed=258.255µs
INFO [10-08|17:30:29] Successfully sealed new block number=2 hash=a3dd21…e26e16
INFO [10-08|17:30:29] 🔨 mined potential block number=2 hash=a3dd21…e26e16
INFO [10-08|17:30:29] Commit new mining work number=3 txs=0 uncles=0 elapsed=167.86µs
INFO [10-08|17:30:33] Successfully sealed new block number=3 hash=90b763…070f62
INFO [10-08|17:30:33] 🔨 mined potential block number=3 hash=90b763…070f62
INFO [10-08|17:30:33] Commit new mining work number=4 txs=0 uncles=0 elapsed=153.475µs
INFO [10-08|17:30:36] Successfully sealed new block number=4 hash=544c44…3c4cfe
INFO [10-08|17:30:36] 🔨 mined potential block number=4 hash=544c44…3c4cfe
INFO [10-08|17:30:36] Commit new mining work number=5 txs=0 uncles=0 elapsed=163.849µs
INFO [10-08|17:30:59] Successfully sealed new block number=5 hash=c5bb6d…5b443a
INFO [10-08|17:30:59] 🔨 mined potential block number=5 hash=c5bb6d…5b443a
INFO [10-08|17:30:59] Commit new mining work number=6 txs=0 uncles=0 elapsed=152.6µs
INFO [10-08|17:31:13] Successfully sealed new block number=6 hash=9a7770…add5c5
INFO [10-08|17:31:13] 🔗 block reached canonical chain number=1 hash=0f933a…441247
INFO [10-08|17:31:13] 🔨 mined potential block number=6 hash=9a7770…add5c5
INFO [10-08|17:31:13] Commit new mining work number=7 txs=0 uncles=0 elapsed=151.02µs
> eth.hashrate
151138
> miner.stop()
true
> eth.hashrate
0

eth.hashrateは1秒間にhashを計算している回数で、これが0より大きい時はetherをマイニングしていることになります。

ブロック数の確認

マイニングを続けているとブロックが勝手に作られて行きます。下記では7個のブロックが生成されたことになります。

> eth.blockNumber
7

所持etherの確認

この場合、単位がetherではなく「wei」というもので表示されます。1ether = 10^18 weiなのでetherに直すと35etherとなります。

web3.fromWei()関数を使うと単位をetherとして出力できます。

> eth.getBalance(eth.accounts[0])
35000000000000000000

> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
35

etherの送金

etherを送金するにはまずアカウントのロックを解除しなければいけないため送信元のアカウントをunlockAccountコマンドでアンロックします。

> personal.unlockAccount(eth.accounts[0])

10etherをaccounts[0]からaccounts[1]に送金します。accounts[1]に10ether送金できていることが確認できます。

> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, "ether")})
INFO [10-08|17:40:12] Submitted transaction fullhash=0xe72bf564c5073997308ec7a730e76a7dc81893aec37728a2818c9ed98f76508d recipient=0xB412662DDCD7adc0Bb6ccD5ab79E23FE5f7E021B
"0xe72bf564c5073997308ec7a730e76a7dc81893aec37728a2818c9ed98f76508d"

> eth.getBalance(eth.accounts[1])
10

送金を確定させるためにマイニングを開始し、できたらストップします。

> miner.start()
INFO [10-08|17:41:17] Updated mining threads                   threads=0
INFO [10-08|17:41:17] Transaction pool price threshold updated price=18000000000
INFO [10-08|17:41:17] Starting mining operation
null
INFO [10-08|17:41:17] Commit new mining work                   number=8 txs=1 uncles=0 elapsed=884.382µs
INFO [10-08|17:41:22] Successfully sealed new block            number=8 hash=5a57c4…347c74
INFO [10-08|17:41:22] 🔗 block reached canonical chain          number=3 hash=90b763…070f62
INFO [10-08|17:41:22] 🔨 mined potential block                  number=8 hash=5a57c4…347c74
INFO [10-08|17:41:22] Commit new mining work                   number=9 txs=0 uncles=0 elapsed=349.735µs
> miner.stop()
true

accounts[0]とaccounts[1]の所持ETHを確認すると、ちゃんと送金できていることが確認できます。accounts[0]はマイニング分の増えています。

>  web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
30
>  web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
10

まとめ

gethのインストールから送金まで説明しましたがこれが基本的なgethの使い方になります。

自分はまだまだ仕組みをはっきり理解できているわけではないので間違いなどありましたらコメントで教えていただけたらと思います。

Ethereumの技術的なことを学んだらどんどんまとめていきます。

Bitcoinの基礎はこちらを参照してください

Bitcoinの基礎と構成技術

おすすめ書籍

Ethereumを使ったDApps開発を学びたいなら今だとこの1冊が1番良いです!開発環境の構築から使うべきツール、フレームワーク、実装方法・注意点まで網羅的に解説されている書籍なのでおすすめです。出版も2018年1月ということでかなり新しい本で、DMM Bitcoinを作っているネクストカレンシー所属の方が書いているので信頼できます。

ビットコインとブロックチェーンの詳細をしっかりと学びたい方にはこちらの書籍が非常におすすめです。ウォレットの仕組み、楕円曲線暗号、P2Pプロトコル、公開鍵暗号などビットコインを支える技術について詳細に解説されています。また、サンプルコードを通して実際に手を動かして学べるので非常に濃い内容となっています。

The following two tabs change content below.

髙妻智一

2013年CyberAgent新卒入社 スマホゲームを作る子会社に所属し、サーバーサイドのエンジニアを担当。2年目の終わりから新規子会社の立ち上げに参加し、サーバーサイドのエンジニアリーダーとしてサービースのリリースから運用までを担当。 2018年仮想通貨のスマホウォレットを提供するGinco Incにブロックチェーンエンジニアとして入社。






よく読まれている関連記事はこちら




コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です