文档

账户接口

更新时间:

创建账户

CreateAccount 接口用于在链上创建一个新的账户。

请求参数

将以下参数整体封装为 object 传入。

参数 必选 类型 说明
from true string 使用的账户名,用此账户来创建新账户。
to true string 创建的新账户名。
data true object 数据内容,包含创建账户需要的数据。

data 字段内容:

字段 必选 类型 说明
recover_key false string 内容为“0x”开头的 16 进制字符串,长度为 64(不含“0x”)。
auth_key false string 内容为“0x”开头的 16 进制字符串,长度为 64(不含“0x”)。
auth_weight false string 目前 JS SDK 支持一个 auth key,此值应设为 100。

示例

  • 使用已有 public key 创建账户。

    1. chain.ctr.CreateAccount({
    2. from: 'Tester001',
    3. to: 'Tester002',
    4. data: {
    5. recover_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
    6. auth_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
    7. auth_weight: 100
    8. }
    9. }, (err, data) => {
    10. console.log(data)
    11. })
  • 使用新创建的 key 创建账户。

    1. const newKey = Chain.utils.generateECKey();
    2. console.log('newKey priKey:', newKey.privateKey.toString('hex'))
    3. console.log('newKey pubKey:', newKey.publicKey.toString('hex'))
    4. chain.ctr.CreateAccount({
    5. from: 'Tester001',
    6. to: 'Tester003',
    7. data: {
    8. recover_key: '0x'+ newKey.publicKey.toString('hex'),
    9. auth_key: '0x'+ newKey.publicKey.toString('hex'),
    10. auth_weight: 100
    11. }
    12. }, (err, data) => {
    13. console.log(data)
    14. })
  • 使用新创建的 key 创建账户,然后使用新账户发起交易。注意,需要切换账户配置。

    1. const newKey = Chain.utils.generateECKey();
    2. console.log('newKey priKey:', newKey.privateKey.toString('hex'))
    3. console.log('newKey pubKey:', newKey.publicKey.toString('hex'))
    4. const newAccountName = 'Tester001' + Date.now()
    5. chain.ctr.CreateAccount({
    6. from: 'Tester001',
    7. to: newAccountName,
    8. data: {
    9. recover_key: '0x'+ newKey.publicKey.toString('hex'),
    10. auth_key: '0x'+ newKey.publicKey.toString('hex'),
    11. auth_weight: 100
    12. }
    13. }, (err, data) => {
    14. console.log('New Account:', data)
    15. //当前的 chain 实例 切换配置账户
    16. opt.userPrivateKey = '0x'+ newKey.privateKey.toString('hex')
    17. opt.userPublicKey = '0x'+ newKey.publicKey.toString('hex')
    18. opt.userRecoverPrivateKey = '0x'+ newKey.privateKey.toString('hex')
    19. opt.userRecoverPublicKey = '0x'+ newKey.publicKey.toString('hex')
    20. chain.setUserKey(opt)
    21. chain.setUserRecoverKey(opt)
    22. //当前的 chain 实例 切换配置账户完成
    23. //使用刚才新创建的账户发起交易(此示例使用的“存证交易”,在下文内容有详细介绍)
    24. chain.ctr.NativeDepositData({
    25. from: newAccountName,
    26. to: newAccountName,
    27. data: {
    28. payload: '0x1234'
    29. }
    30. }, (err, data) => {
    31. chain.ctr.QueryTransaction({
    32. hash: data.txhash
    33. }, (err, data) => {
    34. console.log('TX data(NativeDepositData):', data)
    35. })
    36. chain.ctr.QueryTransactionReceipt({
    37. hash: data.txhash
    38. }, (err, data) => {
    39. console.log('Receipt data(NativeDepositData):', data)
    40. })
    41. })
    42. })

转账

TransferBalance 接口用于从一个账户转账到另一个账户。

请求参数

将以下参数整体封装为 object 传入。

参数 必选 类型 说明
from true string 使用的账户名
to true string 转账的目标账户名
value true number 转账额度

示例

  1. chain.ctr.TransferBalance({
  2. from: 'Tester001',
  3. to: 'Tester002',
  4. value: 0
  5. }, (err, data) => {
  6. console.log(data)
  7. })

设置恢复公钥

SetRecoverkey 接口用于设置一个账户的恢复公钥。

请求参数

将以下参数整体封装为 object 传入。

参数 必选 类型 说明
from true string 需要配置的当前账户名
data true object 数据字段

data 字段内容:

字段 必选 类型 说明
recover_key true string 内容为“0x”开头的 16 进制字符串,长度为 64(不含“0x”)。

示例

  1. chain.ctr.SetRecoverkey({
  2. from: account,
  3. data: {
  4. recover_key: publicKey
  5. }
  6. }, (err, data) => {
  7. console.log(data)
  8. })

预重置公钥

PreResetPubKey 接口用来预重置一个账户的公钥。

请求参数

将以下参数整体封装为 object 传入。

参数 必选 类型 说明
from true string 需要配置的当前账户名

示例

  1. chain.ctr.PreResetPubKey({
  2. from: 'Tester001'
  3. }, (err, data) => {
  4. console.log(data)
  5. })

重置公钥

ResetPubKey 接口用来重置一个账户的公钥。

请求参数

将以下参数整体封装为 object 传入。

参数 必选 类型 说明
from true string 需要配置的当前账户名
data true object 数据字段

data 字段内容:

字段 必选 类型 说明
auth_key true string 内容为“0x”开头的 16 进制字符串,长度为 64(不含“0x”)。
auth_weight true number 权重值,为 100,当前 JS SDK 支持一个 auth key,因此权重值为 100,未来支持多 auth key,权重值和为 100。

示例

  1. chain.ctr.ResetPubKey({
  2. from: 'Tester001',
  3. data: {
  4. auth_key:'0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
  5. auth_weight: 100,
  6. }
  7. }, (err, data) => {
  8. console.log(data)
  9. })

更新权重

UpdateAuthMap 接口用于更新一个账户的公钥的权重值。

请求参数

将以下参数整体封装为 object 传入。

参数 必选 类型 说明
from true string 需要配置的当前账户名
data true object 数据字段

data 字段内容:

字段 必选 类型 说明
auth_key true string 内容为“0x”开头的 16 进制字符串,长度为 64(不含“0x”)。
auth_weight true number 权重值,为 100,当前 JS SDK 支持一个 auth key,因此权重值为 100,未来支持多 auth key,权重值和为 100。

示例

  1. chain.ctr.UpdateAuthMap({
  2. from: 'Tester001',
  3. data: {
  4. auth_key:'0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
  5. auth_weight: 100,
  6. }
  7. }, (err, data) => {
  8. console.log(data)
  9. })
  • 本页导读 (0)
文档反馈