使用EOS RPC 以及 eosjs 建立賬號
rpc 上沒有專門的建立建立賬號的介面,eosjs上有
這本地測試環境中我使用裡eosjs 建立賬號,eosjs-cc 進行祕鑰的建立
eosjs 建立
核心程式碼片段如下:
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'
建立後返回的結構如下
RPC介面建立
我是在測試網路中進行測試的
我在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"
}],
}
}
最後就會返回200了: