目錄
EOS開(kāi)發(fā)(1) -- 安裝及常用工具介紹
EOS開(kāi)發(fā)(2) -- 發(fā)布智能合約
EOS開(kāi)發(fā)(3) -- currency合約分析
之前我寫(xiě)過(guò)一篇文章,如何在以太坊上構(gòu)建dapp,感興趣的可以先去看下以太坊上如何編寫(xiě)發(fā)布智能合約的,eos現(xiàn)在測(cè)試鏈3.0還沒(méi)有發(fā)布,所以相比以太坊來(lái)說(shuō)工具還不完善,所以想要深入了解的話需要花點(diǎn)功夫研究.
EOS 常用命令介紹
- eosd
eosd 是 eos的核心后臺(tái)進(jìn)程,想錢(qián)包賬戶(hù)查詢(xún)服務(wù),生產(chǎn)區(qū)塊服務(wù)都是依賴(lài)這個(gè)進(jìn)程的啟動(dòng)
- eosc
eosc 用來(lái)和eosd暴露出的rest接口交互的一個(gè)命令行工具為了使用eosc,
你需要指定eosd實(shí)例的ip地址和端口號(hào),執(zhí)行以下eosc可以看到下面列出的各種用法
$ ./eosc
ERROR: RequiredError: Subcommand required
Command Line Interface to Eos Client
Usage: ./eosc [OPTIONS] SUBCOMMAND
Options:
-h,--help Print this help message and exit
# 下面這個(gè)用來(lái)指定eosd實(shí)例ip,默認(rèn)本地地址
-H,--host TEXT=localhost the host where eosd is running 這
# 下面用來(lái)指定端口號(hào) 默認(rèn) 8888
-p,--port UINT=8888 the port where eosd is running
--wallet-host TEXT=localhost
the host where eos-walletd is running
--wallet-port UINT=8888 the port where eos-walletd is running
-v,--verbose output verbose messages on error
Subcommands:
# 獲取版本信息
version Retrieve version information
# 創(chuàng)建錢(qián)包 或者賬戶(hù)等命令
create Create various items, on and off the blockchain
# 去區(qū)塊鏈上查詢(xún)的命令
get Retrieve various items and information from the blockchain
# 發(fā)布或者更新智能合約命令
set Set or update blockchain state
# 賬戶(hù)間轉(zhuǎn)賬的命令
transfer Transfer EOS from account to account
# 點(diǎn)對(duì)點(diǎn)網(wǎng)絡(luò)節(jié)點(diǎn)交互連接的命令
net Interact with local p2p network connections
# 和錢(qián)包交互的命令
wallet Interact with local wallet
# 執(zhí)行基準(zhǔn)測(cè)試的命令
benchmark Configure and execute benchmarks
# 推消息到鏈上的命令
push Push arbitrary transactions to the blockchain
發(fā)布合約前準(zhǔn)備工作
- 創(chuàng)建一個(gè)錢(qián)包
創(chuàng)建錢(qián)包前我們需要知道一個(gè)錢(qián)包可能有多個(gè)賬戶(hù),賬戶(hù)之間是交互的主體,eos可以鎖定錢(qián)包來(lái)控制該錢(qián)包下所有賬戶(hù)資金的安全,所以首先我們需要?jiǎng)?chuàng)建一個(gè)錢(qián)包,然后再創(chuàng)建一些賬戶(hù),把這些賬戶(hù)的私鑰導(dǎo)入到錢(qián)包中
cd ~/eos/build/programs/eosc/
./eosc wallet create
上面命令會(huì)輸出一個(gè)密碼 這個(gè)密碼是用來(lái)鎖定或者介紹錢(qián)包用的 這個(gè)需要保存好,而且會(huì)生成一個(gè)名為的default錢(qián)包
- 此時(shí)我們default錢(qián)包里沒(méi)有賬戶(hù)所以我們要導(dǎo)入一個(gè)賬戶(hù),還記得上一節(jié)我們備份的genisis.json.back那個(gè)文件嗎前塔,這個(gè)文件中有個(gè)inita賬戶(hù)的私鑰,我們可以把它導(dǎo)入到我們的錢(qián)包中
./eosc wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
- 創(chuàng)建秘鑰對(duì)
因?yàn)閑os提供了權(quán)限控制的概念,提供了有owner 和 active的角色,owner代表所有者,active代表可以操作(其實(shí)這兩個(gè)都是特別高的權(quán)限,都能做任何交易上的操作,只是owner權(quán)限更高一點(diǎn)),eos 創(chuàng)建賬戶(hù)需要指定 owner key 和 active key亭饵,所以我們需要先創(chuàng)建兩對(duì)key
cd ~/eos/build/programs/eosc/
$ ./eosc create key
Private key: 5JL6auvLndKnbtTk63EjPFVZ2giFKiUZo97siZx5PXcXsbQVEcq
Public key: EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1 # owner key
$ ./eosc create key
Private key: 5JXgdQwueP9WNXpYWkQkRgL18LEvVy4e9KwyAPzdVxdT9ibqVDW
Public key: EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ # active key
每執(zhí)行一次create key ,都會(huì)生成一對(duì)公私鑰對(duì)
- 創(chuàng)建賬戶(hù)
./eosc create account inita test PUBLIC_KEY_1(owner_key) PUBLIC_KEY_2(active_key)
上面的命令相當(dāng)于創(chuàng)建了一個(gè)test用戶(hù),因?yàn)閕nita是我們已經(jīng)導(dǎo)入進(jìn)來(lái)的,這里inita可以當(dāng)做test的創(chuàng)建者,key1,key2把我們上面生成的兩個(gè)公鑰輸入進(jìn)去就ok了
$ ./eosc create account inita test EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1 EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ
441183ms main.cpp:1195 main ] Failed with error: Assert Exception (10)
status_code == 200: Error code 401
: {"code":401,"message":"UnAuthorized","details":"signatures do not satisfy declared authorizations (3030002)\nTransaction declares authority '{\"account\":\"inita\",\"permission\":\"active\"}', but does not have signatures for it.\n"}
參數(shù)替換梁厉,執(zhí)行后我們發(fā)現(xiàn)上面執(zhí)行失敗辜羊,提示未授權(quán),其實(shí)是我們還沒(méi)有把剛剛創(chuàng)建的active 秘鑰導(dǎo)入錢(qián)包
- 導(dǎo)入創(chuàng)建的active key 秘鑰到錢(qián)包
$ ./eosc wallet import 5JXgdQwueP9WNXpYWkQkRgL18LEvVy4e9KwyAPzdVxdT9ibqVDW
imported private key for: EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ
可以看到導(dǎo)入成功
- 再次執(zhí)行創(chuàng)建賬戶(hù) test
$ ./eosc create account inita test EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1 EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ
{
"transaction_id": "76f574b783e3c67599047009c84acb72e2be3d1c08c1b5e6f303d0f19be99b60",
"processed": {
"ref_block_num": 36260,
"ref_block_prefix": 3203619337,
"expiration": "2018-03-14T15:19:25",
"scope": [
"eos",
"inita"
],
"signatures": [
"20422d4f3f4900344ef774c65e5704fa1abea3796e9353eda466478b80d47d59e54d00cd9e1c65a3507242cb91eebc0f52334d21f129db1494ee3d832ddf9b85ac"
],
"messages": [{
"code": "eos",
"type": "newaccount",
"authorization": [{
"account": "inita",
"permission": "active"
}
],
"data": {
"creator": "inita",
"name": "test",
"owner": {
"threshold": 1,
"keys": [{
"key": "EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1",
"weight": 1
}
],
"accounts": []
},
"active": {
"threshold": 1,
"keys": [{
"key": "EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ",
"weight": 1
}
],
"accounts": []
},
"recovery": {
"threshold": 1,
"keys": [],
"accounts": [{
"permission": {
"account": "inita",
"permission": "active"
},
"weight": 1
}
]
},
"deposit": "0.0001 EOS"
},
"hex_data": "000000000093dd74000000000090b1ca0100000001024c59ba35833eabc71d794ed79ba8ab26a232ddd4c4790c2282f20200cc0baf73010000010000000102343387e05944b65216def1599960aca2d6a03adcfd37930b5aac21b97f4c5839010000010000000001000000000093dd7400000000a8ed32320100010000000000000004454f5300000000"
}
],
"output": [{
"notify": [],
"deferred_trxs": []
}
]
}
}
可以看到執(zhí)行成功了,是這樣的創(chuàng)建賬戶(hù)前需要?jiǎng)?chuàng)建者賬戶(hù)里有錢(qián),因?yàn)閑os基于dpos一種pos的變種,有stake(eos token)才能去使用資源,這筆錢(qián)會(huì)從創(chuàng)建者賬戶(hù)里扣除,這里也就是inita
- 查詢(xún)test賬戶(hù)信息
$ ./eosc get account test
{
"account_name": "test",
"eos_balance": "0.0000 EOS",
"staked_balance": "0.0001 EOS",
"unstaking_balance": "0.0000 EOS",
"last_unstaking_time": "2106-02-07T06:28:15",
"permissions": [{
"perm_name": "active",
"parent": "owner",
"required_auth": {
"threshold": 1,
"keys": [{
"key": "EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ",
"weight": 1
}
],
"accounts": []
}
},{
"perm_name": "owner",
"parent": "",
"required_auth": {
"threshold": 1,
"keys": [{
"key": "EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1",
"weight": 1
}
],
"accounts": []
}
}
]
}
可以看到 "staked_balance": "0.0001 EOS",表明test賬戶(hù)里有0.0001個(gè)eos词顾,不過(guò)這寫(xiě)token被鎖定了
- 部署currency合約
$ ./eosc set contract test ../../contracts/currency/currency.wast ../../contracts/currency/currency.abi
test 這里表示合約發(fā)布的賬戶(hù)
執(zhí)行命令后后你會(huì)看到返回一個(gè)帶有transaction_id的json字符串,說(shuō)明部署成功了