EOS 学習メモ:コントラクト実行権限編steemCreated with Sketch.

in #eos5 years ago (edited)

dg1uxga7sv.jpg

EOS 学習メモ:Hello World 編 の続き。

まず、前回デプロイした contract を こんな感じ に改良する。具体的には、require_auth(user) という行を追加しただけ。これで、コントラクトの実行ユーザーと user が同じじゃないと hi action が実行できなくなる。

ということで、改良した contract をコンテナ上に持っていく。

 $ docker cp contracts/hello-with-auth/hello.cpp eos:/tmp/contracts/hello/hello.cpp



コンテナに接続。

 $ docker exec -it eos bash



以降、コンテナ上で作業を進める。

前回と同じようにコンパイルして WASM をつくりなおす。

 $ cd /tmp/contracts/hello
 $ eosio-cpp -o hello.wasm hello.cpp --abigen



hello account に対して新しい contract をデプロイしてアップデートする。こんな感じで軽々しく contract をアップデートできる辺り、Ethereum と感覚が違う。

 $ cleos set contract hello . -p hello@active
 Reading WASM from /tmp/contracts/hello/hello.wasm...
 Skipping set abi because the new abi is the same as the existing abi
 Publishing contract...
 executed transaction: f5fa8e0140accdcc98d32a1983d7988fb1289b5c49029e4bb430190d9e8f05c2  1400 bytes  35142 us
 #         eosio <= eosio::setcode               {"account":"hello","vmtype":0,"vmversion":0,"code":"0061736d0100000001390b60027f7e006000017f60027f7f...



ABI は変わってないので更新しなかったよとのこと。インターフェースなので当然ではある。



contract の更新ができたようなので、前回は実行できた「alice が引数 "bob"hi action を実行する」をやってみる。

 $ cleos push action hello hi '["bob"]' -p alice@active
 Error 3090004: Missing required authority
 Ensure that you have the related authority inside your transaction!;
 If you are currently using 'cleos push action' command, try to add the relevant authority using -p option.
 Error Details:
 missing authority of bob
 pending console output:



想定通り、権限がないのでエラーになった。

引数を "alice" に変えてやってみる。

 $ cleos push action hello hi '["alice"]' -p alice@active
 executed transaction: 46bb434dfd147d9b7902faa834b4222032b5a429a44362598bcb3c23ced12413  104 bytes  2905 us
 #         hello <= hello::hi                    {"user":"alice"}
 >> Hello, alice



こちらは実行できた。OK。

今日はここまで。

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 62423.21
ETH 2897.82
USDT 1.00
SBD 3.56