EOS系列 - 使用cleos(命令行)发起裸包交易

it2023-03-15  106

eos构造裸交易并签名

1. 需要到链上获取到的数据

获取 ref_block_prefix get_info以获取最后一个不可逆的块号:last_irreversible_block_num 这是一个屏幕截图get_block获取有关该区块的信息:这是屏幕截图从中找出 ref_block_prefix 的数据

2.1 之后本地可离线构造出未签名交易数据

然后计算 ref_block_num

const ref_block_num = (info.last_irreversible_block_num) & 0xFFFF

expiration:

当前系统时间(按与节点相同的时区算) + 300秒 (cleos默认是30秒)

编码构造 data :

组装对象

{ "code": "eosio.token", "action": "transfer", "args": { "from": "fromaccount", "to": "toaccount", "quantity": "1.0000 EOS", "memo": "memo" } }

编码

参考 https://developers.eos.io/eosio-nodeos/reference#abi_bin_to_json

参考 https://github.com/OracleChain/PocketEOS-IOS/blob/ca34cd96ebaa773d806a6aa7ddf913504d6a66d5/pocketEOS/SecureModule/EC/EosByteWriter.m

通过api接口构造action所需的data

curl http://192.168.1.201:30088/v1/chain/abi_json_to_bin -X POST -d '{"code":"eosio.token", "action":"transfer", "args":{"from":"xxx", "to":"xxx", "quantity":"xxx", "memo":"xxx"}}'

签名

chainidsha256ECDSA-K1

构造结果(未签名)

{ "compression": "none", "transaction": { "expiration": "2018-08-01T06:11:23", "ref_block_num": 10855, "ref_block_prefix": 473148127, "max_net_usage_words": 0, "max_cpu_usage_ms": 0, "delay_sec": 0, "context_free_actions": [], "actions": [{ "account": "eosio.token", "name": "transfer", "authorization": [{ "actor": "fromaccount", "permission": "active" }], "data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d" }], "transaction_extensions": [], "signatures": null, "context_free_data": [] }, "signatures": ["SIG_K1_JwLVG5pRdhvLfJGWkDEBPa7wdLbNeqeRFdvFrKDEryahSwCRpPb75m4auZh8frq6cXsm3dHit8GMbmuuBWxEjH"] }

2.2 也可以通过cleos命令构造未签名交易数据

cleos push action eosio.token transfer '{"from":"xxx", "to":"xxx", "quantity":"xxx", "memo":"xxx"}' -p fromaccount@active -jds -j: print result as json -d: don't broadcast transaction to the network (just print to stdout) -s: Specify if unlocked wallet keys should be used to sign transaction

3. 签名并发送交易

使用上面构造的 [transaction json], 和传入的私钥签名, 并发送交易

./cleos sign -p -k [private-key] '[transaction json]' -k,--private-key TEXT The private key that will be used to sign the transaction -p,--push-transaction Push transaction after signing
最新回复(0)