こんにちは。
先日、6月30日に株式会社INDETAILにて勉強会「初めてでもここまでできるブロックチェーン」を実施しました。
ご参加いただいた方々、どうもありがとうございました。
本日は、その第2部で実際にお見せしました、「Ethereum(geth)でのプライベートネットへの接続方法・コマンドの紹介・web3.jsの使用方法」を当ブログでご紹介します。
環境
OS:Ubuntu 16.04
geth:1.6.6-stable-10a45cb5
Ethereum(geth)でのプライベートネットへの接続方法
1.gethをインストール (※Ubuntuの場合)
1 2 3 4 |
$ sudo apt-get install software-properties-common $ sudo add-apt-repository -y ppa:ethereum/ethereum $ sudo apt-get update $ sudo apt-get install ethereum |
各OSのgethインストール方法はこちらを参照して下さい。
2.EOAを作成
1 2 3 4 5 |
$ geth account new --datadir ./ Your new account is locked with a password. Please give a password. Do not forget this password. Passphrase: Repeat passphrase: Address: {dde946e95edcce8f74f360f0cfae31d96aa6da1e} |
パスワード入力が求められますので、EOAのパスワードを設定します。
2回入力後、作成されたEOAのアドレスが返されます。
※EOA(Externally Owned Account)は、Ethereumに存在する2種類のアカウントのうちの1種です。
マイニング・etherの送金等が実行できます。
3.設定ファイル genesis.json を作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "config": { "chainId": 5, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "alloc" : {}, "coinbase" : "0x0000000000000000000000000000000000000000", "difficulty" : "0x20000", "extraData" : "", "gasLimit" : "0x2fefd8", "nonce" : "0x0000000000000042", "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp" : "0x00" } |
chainIDは、0〜3以外の整数を指定します。
jenesis.json で設定すべき内容はこちらを参照して下さい。
4.genesis.jsonファイルをもとにgenesisブロックを作成
1 |
$ geth init ./genesis.json --datadir ./ |
5.geth 起動
1 2 3 4 5 6 7 |
$ geth --networkid "5" \ --datadir "./" \ --nodiscover \ console 2>> "./geth.log" Welcome to the Geth JavaScript console! (省略) > |
networkidには genesis.json の chainId を指定します。
gethが起動しCLIクライアントに切り替わります。
(終了時は、exitコマンドを入力して下さい。)
gethのコマンド紹介
1.EOAの作成
1 2 |
> personal.newAccount("PW02") "0x6eeee34190aedbc10c7e25f7d5f08563f23fb84e" |
personal.newAccount(作成するEOAのパスワード)
作成されたEOAのアドレスが返されます。
2.EOAのアドレス一覧を取得
1 2 |
> eth.accounts ["0xdde946e95edcce8f74f360f0cfae31d96aa6da1e", "0x6eeee34190aedbc10c7e25f7d5f08563f23fb84e"] |
eth.accountsにEOAのアドレスが配列で格納されています。
3.マイニング成功時に報酬を受け取るEOAの確認と変更
1 2 3 4 5 6 |
> eth.coinbase "0xdde946e95edcce8f74f360f0cfae31d96aa6da1e" > miner.setEtherbase("0x6eeee34190aedbc10c7e25f7d5f08563f23fb84e") true > eth.coinbase "0x6eeee34190aedbc10c7e25f7d5f08563f23fb84e" |
miner.setEtherbaseの引数に変更先のEOAアドレスを指定します。
EOAアドレス部分をeth.accounts[1]に置き換えることも可能です。
4.マイニングの開始・終了
1 2 3 4 |
> miner.stop() true > miner.start() null |
5.保有etherの確認
1 2 3 4 |
> eth.getBalance(eth.accounts[1]) 25000000000000000000 > web3.fromWei(eth.getBalance(eth.accounts[1]),"ether") 25 |
上は単位weiで表示、下は単位etherで表示します。
※ 1ether = 1 × 1018 wei
6.送金
1 2 3 4 |
> personal.unlockAccount(eth.accounts[1], "PW02", 30) true > eth.sendTransaction({from:eth.accounts[1], to:eth.accounts[0], value:web3.toWei(5, "ether")}) "0x2db12c03ba84f02881d0b7a2a80c5bda4437699a1604e6184b134f7f699aac17" |
web3.jsの使用方法
1.web3.jsをダウンロード
web3.jsをGitHubからダウンロードします。
※今回必要なファイルは「web3.js/dist/web3.min.js」のみ
2.gethを再起動
rpcを使用可能にするため、[rpc][rpcapi]を指定してgethを起動します。
1 2 3 4 5 6 7 8 9 10 |
$ geth --networkid "5" \ --datadir "./" \ --nodiscover \ --mine \ --rpc \ --rpcapi eth,web3,personal \ --rpcaddr "192.168.56.101" \ --rpcport "8545" \ --rpccorsdomain "*" \ console 2>> "./geth.log" |
–mine 起動時にマイニングを開始
–rpc HTTP-RPCサーバーを有効にする
–rpcapi RPCインターフェースの種類の指定
–rpcaddr gethノードのIPアドレス
–rpcport RCP APIのポート番号
3.HTMLを作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>web3.js を使用した送金</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript" src="./js/bignumber.min.js"></script> <script type="text/javascript" src="./js/crypto-js.js"></script> <script type="text/javascript" src="./js/utf8.js"></script> <script type="text/javascript" src="./js/web3.min.js"></script> <script> var web3 = new Web3(); var provider = new web3.providers.HttpProvider("http://192.168.56.101:8545"); web3.setProvider(provider); web3.eth.defaultAccount = web3.eth.accounts[0]; // 送金 function sendTransaction() { //unlock web3.personal.unlockAccount($("#from_eoa").val(), $("#password").val(), 5); //sendTransaction web3.eth.sendTransaction({from :$("#from_eoa").val(), to :$("#to_eoa").val(), value:web3.toWei($("#value_eth").val(), "ether") }); } //EOA一覧 function insertAccountRow() { var accounts = web3.eth.accounts; if (accounts.length > 0) { for (var i = 0; i < accounts.length; i++) { var table = $("#eoa_list").get(0); var row = table.insertRow(); var td = row.insertCell(0); td.innerHTML = accounts[i]; var td = row.insertCell(1); td.innerHTML = web3.fromWei(web3.eth.getBalance(accounts[i]),"ether").toString(); } } } $(function(){ insertAccountRow(); }); </script> </head> <body> <table id="eoa_list" border="1"> <tr> <th>Account</th> <th>Eth</th> </tr> </table> <br /> From:<input type="text" id="from_eoa" value="" /> To:<input type="text" id="to_eoa" value="" /> Value:<input type="text" id="value_eth" value="" />Eth PW:<input type="text" id="password" value="" /> <input type="button" value="送金" onclick="sendTransaction();" /> </body> </html> |
10行目で、GitHubから取得した「web3.js/dist/web3.min.js」を読み込みます。
14行目は、geth起動時の[rpcaddr][rpcport]の値を指定します。
13〜15行目の設定により、以降はweb3変数の後にgethコマンドを記述するだけで実行可能になります。
4.ブラウザで表示
Fromに送信元EOA、Toに送信先EOA、Valueに送信するether、PWに送信元EOAのパスワードを入力し、送金ボタンを押下すると送金されます。
まとめ
このようにEtherumは簡単に環境構築ができるように作られています。
これを機にEtherumを触ってみてはいかがでしょうか。
参考情報
書籍:「ブロックチェーン仕組みと理論」
Web:「Ethereum入門」
- ブロックチェーンのデメリットを改善した新しい分散型台帳「Hashgraph」について - 2018年2月8日
- 勉強会「初めてでもここまでできるブロックチェーン」第2部の内容紹介 - 2017年7月7日
本記事に対するコメントはまだありません