84/666 EOS 执行智能合约
2018-11-25 本文已影响77人
红叔笔记
这是666计划的第84篇笔记
参考这篇 Learn about Keys, Wallets and Accounts with cleos
创建钱包
alias cleos
alias cleos='docker exec -it keosd /opt/eosio/bin/cleos --url http://172.18.0.2:8888 --wallet-url http://127.0.0.1:9876'
cleos wallet create -n alex-wallet --to-console
Creating wallet: alex-wallet
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5JqZNx7rcfDncbvFZG5MK7Q9J5aQDPEbRJ4KnqarES2eAT2LyHt"
创建密钥对
➜ ~ cleos create key --to-console
Private key: 5JywyoJd2Rcp7iK25Eo8tfW948Z2MALLZVHrs8y3eqpLuQN6c61
Public key: EOS5a6853arFA45voyGqBdzB4fmcLh1GXuknN7tpdkLgZrVbc8TYC
➜ ~
➜ ~ cleos create key --to-console
Private key: 5K2pVCChktdFBHLsXPd8saxWsLp4acybi2WfPjv8NErXEG8rtNE
Public key: EOS7z641AZEiThbBMGUDx1uH3457npKyW5s7VWAXNf48YW6eaw7sb
➜ ~
将密钥导入wallet:
cleos wallet import -n alex-wallet --private-key
private key: imported private key for: EOS5a6853arFA45voyGqBdzB4fmcLh1GXuknN7tpdkLgZrVbc8TYC
cleos wallet import -n alex-wallet --private-key
private key: imported private key for: EOS7z641AZEiThbBMGUDx1uH3457npKyW5s7VWAXNf48YW6eaw7sb
创建账户
先查看下钱包里的公钥
➜ ~ cleos wallet keys
[
"EOS5a6853arFA45voyGqBdzB4fmcLh1GXuknN7tpdkLgZrVbc8TYC",
"EOS7z641AZEiThbBMGUDx1uH3457npKyW5s7VWAXNf48YW6eaw7sb"
]
创建账户:
cleos create account eosio alex EOS5a6853arFA45voyGqBdzB4fmcLh1GXuknN7tpdkLgZrVbc8TYC EOS7z641AZEiThbBMGUDx1uH3457npKyW5s7VWAXNf48YW6eaw7sb
报错:
Error 3090003: Provided keys, permissions, and delays do not satisfy declared authorizations
Ensure that you have the related private keys inside your wallet and your wallet is unlocked.
解决:
cleos wallet import -n alex-wallet --private-key 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
private key: imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
重新创建账户:
cleos create account eosio alex EOS5a6853arFA45voyGqBdzB4fmcLh1GXuknN7tpdkLgZrVbc8TYC EOS7z641AZEiThbBMGUDx1uH3457npKyW5s7VWAXNf48YW6eaw7sb
executed transaction: 264389c10aa973ce8b2935c1428de86f96f355f8eef3bd591ce36bb1043d0b35 200 bytes 555 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"alex","owner":{"threshold":1,"keys":[{"key":"EOS5a6853arFA45voyGqBdzB4fmc...
warning: transaction executed locally, but may not be confirmed by the network yet ]
通过公钥查询账户信息:
cleos get accounts EOS5a6853arFA45voyGqBdzB4fmcLh1GXuknN7tpdkLgZrVbc8TYC
{
"account_names": [
"alex"
]
}
根据账户名查询账户:
cleos get account alex
created: 2018-11-25T03:27:08.500
permissions:
owner 1: 1 EOS5a6853arFA45voyGqBdzB4fmcLh1GXuknN7tpdkLgZrVbc8TYC
active 1: 1 EOS7z641AZEiThbBMGUDx1uH3457npKyW5s7VWAXNf48YW6eaw7sb
memory:
quota: unlimited used: 2.66 KiB
net bandwidth:
used: unlimited
available: unlimited
limit: unlimited
cpu bandwidth:
used: unlimited
available: unlimited
limit: unlimited
➜ ~ cleos get account eosio
created: 2018-06-01T12:00:00.000
privileged: true
permissions:
owner 1: 1 EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
active 1: 1 EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
memory:
quota: unlimited used: 2.66 KiB
net bandwidth:
used: unlimited
available: unlimited
limit: unlimited
cpu bandwidth:
used: unlimited
available: unlimited
limit: unlimited
创建智能合约
eosiocpp -n hello
eosiocpp -g /hello/hello.abi /hello/hello.cpp
eosiocpp -o /hello/hello.wast /hello/hello.cpp
部署智能合约:
cleos set contract alex /hello hello.wast hello.abi -p alex@active
Reading WASM from /hello/hello.wast...
WARNING: /hello/hello.wast doesn't look like a binary WASM file. Is it something else, like WAST? Trying anyways...
Publishing contract...
Error 3070003: Serialization Error Processing WASM
报错, 将wasm换成wast
➜ ~ cleos set contract alex /hello hello.wasm hello.abi -p alex@active
Reading WASM from /hello/hello.wasm...
Publishing contract...
executed transaction: 294cb95f3fa53d236449400888401d56e1bef971c988f5de831fc52c7bcdadd0 1792 bytes 32431 us
# eosio <= eosio::setcode {"account":"alex","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e60027e7e0...
# eosio <= eosio::setabi {"account":"alex","abi":"0e656f73696f3a3a6162692f312e30000102686900010475736572046e616d6501000000000...
warning: transaction executed locally, but may not be confirmed by the network yet
调用合约
cleos push action alex hi '["jack"]' -p alex@active
executed transaction: 3406ae3e0ac2c15da5257bad415ea16167e690023bcbc1d94ffd3ec530e82886 104 bytes 288 us
# alex <= alex::hi {"user":"jack"}
>> Hello, jack