创建账户
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 创建账户。
chain.ctr.CreateAccount({from: 'Tester001',to: 'Tester002',data: {recover_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',auth_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',auth_weight: 100}}, (err, data) => {console.log(data)})
使用新创建的 key 创建账户。
const newKey = Chain.utils.generateECKey();console.log('newKey priKey:', newKey.privateKey.toString('hex'))console.log('newKey pubKey:', newKey.publicKey.toString('hex'))chain.ctr.CreateAccount({from: 'Tester001',to: 'Tester003',data: {recover_key: '0x'+ newKey.publicKey.toString('hex'),auth_key: '0x'+ newKey.publicKey.toString('hex'),auth_weight: 100}}, (err, data) => {console.log(data)})
使用新创建的 key 创建账户,然后使用新账户发起交易。注意,需要切换账户配置。
const newKey = Chain.utils.generateECKey();console.log('newKey priKey:', newKey.privateKey.toString('hex'))console.log('newKey pubKey:', newKey.publicKey.toString('hex'))const newAccountName = 'Tester001' + Date.now()chain.ctr.CreateAccount({from: 'Tester001',to: newAccountName,data: {recover_key: '0x'+ newKey.publicKey.toString('hex'),auth_key: '0x'+ newKey.publicKey.toString('hex'),auth_weight: 100}}, (err, data) => {console.log('New Account:', data)//当前的 chain 实例 切换配置账户opt.userPrivateKey = '0x'+ newKey.privateKey.toString('hex')opt.userPublicKey = '0x'+ newKey.publicKey.toString('hex')opt.userRecoverPrivateKey = '0x'+ newKey.privateKey.toString('hex')opt.userRecoverPublicKey = '0x'+ newKey.publicKey.toString('hex')chain.setUserKey(opt)chain.setUserRecoverKey(opt)//当前的 chain 实例 切换配置账户完成//使用刚才新创建的账户发起交易(此示例使用的“存证交易”,在下文内容有详细介绍)chain.ctr.NativeDepositData({from: newAccountName,to: newAccountName,data: {payload: '0x1234'}}, (err, data) => {chain.ctr.QueryTransaction({hash: data.txhash}, (err, data) => {console.log('TX data(NativeDepositData):', data)})chain.ctr.QueryTransactionReceipt({hash: data.txhash}, (err, data) => {console.log('Receipt data(NativeDepositData):', data)})})})
转账
TransferBalance 接口用于从一个账户转账到另一个账户。
请求参数
将以下参数整体封装为 object 传入。
| 参数 | 必选 | 类型 | 说明 |
|---|---|---|---|
| from | true | string | 使用的账户名 |
| to | true | string | 转账的目标账户名 |
| value | true | number | 转账额度 |
示例
chain.ctr.TransferBalance({from: 'Tester001',to: 'Tester002',value: 0}, (err, data) => {console.log(data)})
设置恢复公钥
SetRecoverkey 接口用于设置一个账户的恢复公钥。
请求参数
将以下参数整体封装为 object 传入。
| 参数 | 必选 | 类型 | 说明 |
|---|---|---|---|
| from | true | string | 需要配置的当前账户名 |
| data | true | object | 数据字段 |
data 字段内容:
| 字段 | 必选 | 类型 | 说明 |
|---|---|---|---|
| recover_key | true | string | 内容为“0x”开头的 16 进制字符串,长度为 64(不含“0x”)。 |
示例
chain.ctr.SetRecoverkey({from: account,data: {recover_key: publicKey}}, (err, data) => {console.log(data)})
预重置公钥
PreResetPubKey 接口用来预重置一个账户的公钥。
请求参数
将以下参数整体封装为 object 传入。
| 参数 | 必选 | 类型 | 说明 |
|---|---|---|---|
| from | true | string | 需要配置的当前账户名 |
示例
chain.ctr.PreResetPubKey({from: 'Tester001'}, (err, data) => {console.log(data)})
重置公钥
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。 |
示例
chain.ctr.ResetPubKey({from: 'Tester001',data: {auth_key:'0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',auth_weight: 100,}}, (err, data) => {console.log(data)})
更新权重
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。 |
示例
chain.ctr.UpdateAuthMap({from: 'Tester001',data: {auth_key:'0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',auth_weight: 100,}}, (err, data) => {console.log(data)})
该文章对您有帮助吗?