rpc 上没有专门的创建创建账号的接口,eosjs上有
这本地测试环境中我使用里eosjs 创建账号,eosjs-cc 进行秘钥的创建
核心代码片段如下:
this.eosClient.transaction(tr => {
tr.newaccount({
creator: creator,
name: name,
owner: owner,
active: active,
});
tr.buyram({
payer: creator,
receiver: name,
quant: ram
});
tr.delegatebw({
from: creator,
receiver: name,
stake_net_quantity: net,
stake_cpu_quantity: cpu,
transfer: 0
});
}, options).then(r => {
console.log(r);
}).catch(e => {
console.log(e)
});
这也就是下面指令的翻版版了。
cleos system newaccount eosio voter1 ${owerkey} ${activkey} \
--stake-net '10.000 SYS' --stake-cpu '10.000 SYS' --buy-ram '10.000 SYS'
我是在测试网络中进行测试的
我在eosio.system智能合约中看到了这几行:
"structs": [
...
{
"name": "newaccount",
"base": "",
"fields": [
{"name":"creator", "type":"account_name"},
{"name":"name", "type":"account_name"},
{"name":"owner", "type":"authority"},
{"name":"active", "type":"authority"}
]
}
....
{
"name": "buyram",
"base": "",
"fields": [
{"name":"payer", "type":"account_name"},
{"name":"receiver", "type":"account_name"},
{"name":"quant", "type":"asset"}
]
},{
"name": "delegatebw",
"base": "",
"fields": [
{"name":"from", "type":"account_name"},
{"name":"receiver", "type":"account_name"},
{"name":"stake_net_quantity", "type":"asset"},
{"name":"stake_cpu_quantity", "type":"asset"},
{"name":"transfer", "type":"bool"}
]
}
....
],
"actions": [{
"name": "newaccount",
"type": "newaccount",
"ricardian_contract": ""
},
.....
{
"name": "buyrambytes",
"type": "buyrambytes",
"ricardian_contract": ""
},{
"name": "buyram",
"type": "buyram",
"ricardian_contract": ""
},{
"name": "sellram",
"type": "sellram",
"ricardian_contract": ""
},{
{
"name": "delegatebw",
"type": "delegatebw",
"ricardian_contract": ""
},
....
]
由于prc 没有直接的接口,使用eosjs 创建时可以反推需要将action进行序列化.签名后再push transaction 这样就能间接实现创建账户功能,我总结了一下,创建一共账户会用到的接口一共有
http://127.0.0.1:8888/v1/chain/get_info
返回:
{ server_version: '379cb1a9',
chain_id: 'cec278a9dced800d9a695adc1e265ed11efa0ad8a70cdaac1eb65718bbe2434f',
head_block_num: 1180202,
last_irreversible_block_num: 1180201,
last_irreversible_block_id: '00120229e41ce6e5ca02d7714f6ed105c162b8b0339f421bd3ea07f74453e8e4',
head_block_id: '0012022a4593b6f7a379e092dd743a561ce5dd87ae8c17927b1d74db849d4d47',
head_block_time: '2018-07-05T01:39:42',
head_block_producer: 'eosio',
virtual_block_cpu_limit: 100000000,
virtual_block_net_limit: 1048576000,
block_cpu_limit: 99900,
block_net_limit: 1048576 }
这里会用到chain_id 和head_block_num
http://127.0.0.1:8888/v1/chain/get_block
返回:
{ timestamp: '2018-07-05T01:39:42.500',
producer: 'eosio',
confirmed: 0,
previous: '00120229e41ce6e5ca02d7714f6ed105c162b8b0339f421bd3ea07f74453e8e4',
transaction_mroot: '0000000000000000000000000000000000000000000000000000000000000000',
action_mroot: 'a17c5ca119ac26223213c95998ee14d57bf322d105f2495061ce60fc43fcb69b',
schedule_version: 0,
new_producers: null,
header_extensions: [],
producer_signature: 'SIG_K1_KZudHJqkpfpnQEJzyMhPi4mMQyqTMNJBWEWkF7UkSQXRcEB33Q8VkfLp7vKnsKuVy3uT66L9fkubesrDLx3hXKL6bMwbPV',
transactions: [],
block_extensions: [],
id: '0012022a4593b6f7a379e092dd743a561ce5dd87ae8c17927b1d74db849d4d47',
block_num: 1180202,
ref_block_prefix: 2464184739 }
这里会用到 block_num 和 ref_block_prefix
然后是常用的钱包操作:
http://127.0.0.1:8888/v1/wallet/create
http://127.0.0.1:8888/v1/wallet/import_key
引入已经认证的账户的私钥到创建的钱包中去。
http://127.0.0.1:8888/v1/wallet/get_public_keys
查看一下公钥是否正确
http://127.0.0.1:8888/v1/wallet/unlock
然后就是分别序列化 action :newaccount、buyram、delegatebw
http://127.0.0.1:8888/v1/chain/abi_json_to_bin
newaccount 的data:
{
code: "eosio",
action: "newaccount",
args: {
creator: "acooleosgeek",
name: "rpccreate111",
owner: {
threshold: 1,
keys: [
{
key: "EOS6DA4UQnqKy46Z34WyDpUcPHQRuh3AZry9NY46Fuu4Uddt1zrvj",
weight: 1
}
],
accounts: [],
waits: []
},
active: {
threshold: 1,
keys: [
{
key: "EOS6DA4UQnqKy46Z34WyDpUcPHQRuh3AZry9NY46Fuu4Uddt1zrvj",
weight: 1
}
],
accounts: [],
waits: []
}
}
}
buyram 的 data
{
code: "eosio",
action: "buyram",
args: {
payer: "acooleosgeek",
receiver: "rpccreate111",
quant: "20.0000 SYS",
}
}
delegatebw的data
{
code: "eosio",
action: "delegatebw",
args: {
from: "acooleosgeek",
receiver: "rpccreate111",
stake_net_quantity: "20.0000 SYS",
stake_cpu_quantity: "20.0000 SYS",
transfer: 0,
}
}
然后就是签名操作了
http://127.0.0.1:8888/v1/wallet/sign_transaction
data的格式如下
{
ref_block_num: '1046911',//block_num
ref_block_prefix: "515467051",//对应 ref_block_prefix
expiration: "2018-07-4T09:28:49", //注意设置超时
actions: [{
account: "eosio",
name: "newaccount",
authorization: [{
actor: "acooleosgeek",
permission: "owner"
}],
data: "00956298aa482932104250d9a88b50bd01000000010002ae152701b13d2f5e80aad26a332392d483213a5529f1bed8f7f7d59b24282c530100000001000000010002ae152701b13d2f5e80aad26a332392d483213a5529f1bed8f7f7d59b24282c5301000000"
}, {
account: "eosio",
name: "delegatebw",
authorization: [{
actor: "acooleosgeek",
permission: "owner"
}],
data: "00956298aa482932104250d9a88b50bd400d0300000000000453595300000000400d030000000000045359530000000000"
},{
account: "eosio",
name: "buyram",
authorization: [{
actor: "acooleosgeek",
permission: "owner"
}],
data: "00956298aa482932104250d9a88b50bd400d0300000000000453595300000000"
}],
signatures: []
},
["EOS6DA4UQnqKy46Z34WyDpUcPHQRuh3AZry9NY46Fuu4Uddt1zrvj"], // acooleosgeek 的公钥 必须import到钱吧
"cec278a9dced800d9a695adc1e265ed11efa0ad8a70cdaac1eb65718bbe2434f" //区块id
]
签完名 推送交易
http://127.0.0.1:8888/v1/chain/push_transaction
push_transaction 的data格式如下:
{
signatures: ["SIG_K1_KVo7keubHWn5sQ3P2dnxzuW4Ehs7JYT6ChDs92JvQSBQcpQAXro8VixzNY18x56GisG5gzfSQWcyE89cr9qpCtepZrhdXV"],
compression: "none",
transaction: {
expiration: '2018-07-04T09:28:49',
ref_block_num: 63871,
ref_block_prefix: 515467051,
max_net_usage_words: 0,
max_cpu_usage_ms: 0,
transaction_extensions: [],
context_free_data: [],
actions:
[{
account: "eosio",
name: "newaccount",
authorization: [{
actor: "acooleosgeek",
permission: "owner"
}],
data: "00956298aa482932104250d9a88b50bd01000000010002ae152701b13d2f5e80aad26a332392d483213a5529f1bed8f7f7d59b24282c530100000001000000010002ae152701b13d2f5e80aad26a332392d483213a5529f1bed8f7f7d59b24282c5301000000"
}, {
account: "eosio",
name: "delegatebw",
authorization: [{
actor: "acooleosgeek",
permission: "owner"
}],
data: "00956298aa482932104250d9a88b50bd400d0300000000000453595300000000400d030000000000045359530000000000"
}, {
account: "eosio",
name: "buyram",
authorization: [{
actor: "acooleosgeek",
permission: "owner"
}],
data: "00956298aa482932104250d9a88b50bd400d0300000000000453595300000000"
}],
}
}