# EOS创建账号说明

By [Untitled](https://paragraph.com/@0x111a1dc049f50fd0a8bf7b780fd9e2c21c267c24) · 2021-11-08

---

### 步骤：

**1、解锁钱包，RPC API：v1/wallet/unlock**

**2、创建秘钥对，RPC API：v1/wallet/create\_key**

**3、获取区块链信息，RPC API：v1/chain/get\_info，通过这一步得到两个数据：head\_block\_num、chain\_id**

**4、根据head\_block\_num获取区块数据，RPC API：v1/chain/get\_block，通过这一步，主要得到三个数据：**

**timestamp、block\_num、ref\_block\_prefix**

**以上是数据准备节点，接下来是重点。**

**5、调用接口abi\_json\_to\_bin编码**

涉及到一个json字符串：

    {
      "code": "%s",
      "action": "%s",
      "args": {
        "creator": "%s",
        "name": "%s",
        "owner": {
          "threshold": 1,
          "keys": [
            {
              "key": "%s",
              "weight": 1
            }
          ],
          "accounts": [],   
          "waits": []      
        },
        "active": {
          "threshold": 1,
          "keys": [
            {
              "key": "%s",
              "weight": 1
            }
          ],
          "accounts": [],    
          "waits": []        
        }
      }
    }
    

格式化代码是：

    fmt.Sprintf(utils.ABI_JSON_TO_BIN,code,action,creator,name,owner_key,active_key)
    

> 其中，code：eosio，action：newaccount，这两个是固定的。
> 
> creator：创建者账号，eos新建账号必须用已经存在的账号
> 
> name：要创建的账号名称
> 
> owner\_key：公钥
> 
> active\_key：公钥
> 
> 通过第5步，可以得到一个json字符串，找到字符串中的字段binargs的值，第六步我们将会用到这个值

**6、签名交易**

涉及到的json字符串为：

    [
      {
        "ref_block_num": %d,
        "ref_block_prefix": %d,
        "expiration": "%s",
        "actions": [
          {
            "account": "%s",
            "name": "%s",
            "authorization": [
              {
                "actor": "%s",
                "permission": "active"
              }
            ],
            "data": "%s"
          }
        ],
        "signatures": []
      },
      [
        "%s"
      ],
      "%s"
    ]
    

格式化代码如下：

    fmt.Sprintf(utils.SIGN_TRANSACTION,ref_block_num,ref_block_prefix,newTime,account,name,actor,data,public_key,chain_id)
    

> 其中utils.SIGN\_TRANSACTION是上面的json字符串，ref\_block\_num、ref\_block\_prefix、newTime是第四步得到的，其中newTime要比获取到的多加十几秒，主要用于交易时效，eos交易打包较快，多加10秒足以，我这边加的是35秒
> 
> account：eosio，固定的
> 
> name：newaccount，固定的
> 
> actor：要创建的账户名称
> 
> data：是第5步得到的binargs
> 
> publick\_key：是创建者的公钥
> 
> chain\_id：是第三步得到的
> 
> 通过这一步，会得到一个json字符串，找到字符串的字段signatures的值，下一步将会用到

**7、推送交易**

涉及到的json字符串为：

    {
      "compression": "none",
      "transaction": {
        "expiration": "%s",
        "ref_block_num": %d,
        "ref_block_prefix": %d,
        "actions": [
          {
            "account": "eosio",
            "name": "newaccount",
            "authorization": [
              {
                "actor": "%s",
                "permission": "active"
              }
            ],
            "data": "%s"
          }
        ]
      },
      "signatures":%s
    }
    

格式化数据代码：

    fmt.Sprintf(utils.PUSH_TRANSACTION,newTime,ref_block_num,ref_block_prefix,actor,data,signatures)
    

> 其中utils.PUSH\_TRANSACTION是上面的json字符串，ref\_block\_num、ref\_block\_prefix、newTime是第四步得到的，其中newTime要比获取到的多加十几秒，主要用于交易时效，eos交易打包较快，多加10秒足以，我这边加的是35秒
> 
> actor：是要创建的账户名称
> 
> data：是第5步得到的binargs
> 
> signatures：是第6步得到的signatures
> 
> 如果这一步没有报错，就完成了创建账号。

**交易和创建账号类似，其实创建账号就是一个交易。**

* * *

交易说明
----

设计到的json字符串：

    const TRANSFER_ABI_JSON_TO_BIN = `
    {
      "code": "%s",
      "action": "transfer",
      "args": {
        "from": "%s",
        "to": "%s",
        "quantity": "%s",
        "memo": "%s",
      }
    }
    `
    
    const TRANSFER_SIGN_TRANSACTION = `
    [
      {
        "ref_block_num": %d,
        "ref_block_prefix": %d,
        "expiration": "%s",
        "actions": [
          {
            "account": "%s",
            "name": "transfer",
            "authorization": [
              {
                "actor": "%s",
                "permission": "active"
              }
            ],
            "data": "%s"
          }
        ],
        "signatures": []
      },
      [
        "%s"
      ],
      "%s"
    ]
    `
    
    const TRANSFER_PUSH_TRANSACTION = `
    {
      "compression": "none",
      "transaction": {
        "expiration": "%s",
        "ref_block_num": %d,
        "ref_block_prefix": %d,
        "context_free_actions": [],
        "actions": [
            {
                "account": "%s",
                "name": "transfer",
                "authorization": [
                    {
                        "actor": "%s",
                        "permission": "active"
                    }
                ],
                "data": "%s"
            }
        ],
        "transaction_extensions": []
      },
      "signatures": %s
    }
    `
    

相关格式化代码如下：

abiJsonToBin：`fmt.Sprintf(utils.TRANSFER_ABI_JSON_TO_BIN,code,from,to,quantity,memo)`

signTransaction：

`fmt.Sprintf(utils.TRANSFER_SIGN_TRANSACTION,ref_block_num,ref_block_prefix,newTime,code,actor,data,public_key,chain_id)`

pushTranaction：

`fmt.Sprintf(utils.TRANSFER_PUSH_TRANSACTION,newTime,ref_block_num,ref_block_prefix,code,actor,data,signatures)`

---

*Originally published on [Untitled](https://paragraph.com/@0x111a1dc049f50fd0a8bf7b780fd9e2c21c267c24/eos)*
