SteemJs注册的两种方法  / 网络研习社#56

SteemJs注册的两种方法 / 网络研习社#56

claimaccount.jpg

参考steemsnippets | 创建免费用户 | Claiming a discounted (“free”) account | 创建用户

这两天在社区中查找了SteemJs用户注册的实现方法,还好,测试了下,也都实现了!Steem在原有的基础上提供了一种新的免费注册的方法(discounted_account),这种新方法可以比较快速地完成新用户注册。

SteemJs注册主要有两种方法:一是create_discounted_account,另一个是accountCreate。create_discounted_account是用消耗RC注册,一个新号需要约10,445,334,023,565 RC,SP大户才能玩得转。像我的帐户(lemooljiang)一天也只能注册2~3个。accountCreate就是付费注册(3个steem),其它的就没什么限制了。

create_discounted_account

如上图所示。

这个是用dsteem来完成注册功能,有两步,一步是申明帐户(claim_account),使其处于Pending状态;第二步是就是直接注册了(create_discounted_account)。

  1. 安装和挂载

    1
    2
    3
    4
    5
    6
    cnpm install dsteem --save
    //main.js
    const dsteem = require('dsteem')
    const client = new dsteem.Client('https://api.steemit.com')
    Vue.prototype.dsteem = dsteem
    Vue.prototype.client = client
  2. 两个注册函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    claim_account() {
    const creator = "lemooljiang"  //创建帐户的用户名
    const active_key = "5Jffffffffff" //创建帐户的资金密钥
    return new Promise(resolve => {
    const wif = this.dsteem.PrivateKey.fromString(active_key)
    const fee = this.dsteem.Asset.from(0, 'STEEM')
    const op = [
    'claim_account',
    {
    creator: creator,
    extensions: [],
    fee: fee
    }]

    this.client.broadcast.sendOperations([op], wif).then(function (result) {
    console.log('11,Included in block: ' + result.block_num)
    console.log('222,申明新帐户创建成功!' )
    resolve("ok")
    }, function (error) {
    console.error(error)
    console.log('333,申明新帐户创建失败!' )
    resolve("err")
    })
    })
    },
    create_discounted_account() {
    const creator = "lemooljiang"  //创建帐户的用户名
    const active_key = "5Jofffffffffffff" //创建帐户的资金密钥
    const username = this.username  //新帐户名
    const password = this.password  //新帐户的密码
    return new Promise(resolve => {
    const wif = this.dsteem.PrivateKey.fromString(active_key)
    const prefix = this.client.addressPrefix
    const ownerKey = this.dsteem.PrivateKey.fromLogin(username, password, 'owner').createPublic(prefix)
    let owner = this.dsteem.Authority.from(ownerKey)
    const activeKey = this.dsteem.PrivateKey.fromLogin(username, password, 'active').createPublic(prefix)
    let active = this.dsteem.Authority.from(activeKey)
    const postingKey = this.dsteem.PrivateKey.fromLogin(username, password, 'posting').createPublic(prefix)
    let posting = this.dsteem.Authority.from(postingKey)
    let memo_key = this.dsteem.PrivateKey.fromLogin(username, password, 'memo').createPublic(prefix)

    const metadata = {date: new Date()}
    const create_op = [
    'create_claimed_account',
    {
    active,
    creator,
    extensions: [],
    json_metadata: metadata ? JSON.stringify(metadata) : '',
    memo_key,
    new_account_name: username,
    owner,
    posting,
    }
    ]

    this.client.broadcast.sendOperations([create_op], wif).then(function (result) {
    console.log('22, Included in block: ' + result.block_num)
    console.log('666,新帐户创建成功!' )
    resolve("ok")
    }, function (error) {
    console.log('444,新帐户创建失败!' )
    console.error(error)
    resolve("err")
    })
    })
    },

accountCreate

这种方法简单直接,付点小费创建就可以啦。

它主要由steem的两个方法来实现:steem.auth.generateKeys 用于生成4组密钥(owner, active, posting, memo),steem.broadcast.accountCreate 用于创建账号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
npm install steem --save
//main.js
import steem from 'steem'
steem.api.setOptions({ url: 'https://api.steemit.com' })
Vue.prototype.steem= steem

register() {
let newAccountName = "bettyliu"  //新帐户名
let newAccountPassword = "FDJUJUJUJU"  //新帐户的密码
let publicKeys = this.steem.auth.generateKeys(newAccountName, newAccountPassword, ['owner', 'active', 'posting', 'memo'])
let owner = { weight_threshold: 1, account_auths: [], key_auths: [[publicKeys.owner, 1]] }
let active = { weight_threshold: 1, account_auths: [], key_auths: [[publicKeys.active, 1]] }
let posting = { weight_threshold: 1, account_auths: [], key_auths: [[publicKeys.posting, 1]] }
let memoKey = publicKeys.memo

let creator = "lemooljiang" //创建帐户的用户名
let creatorWif = "5Jffffffffffff" //创建帐户的资金密钥
let fee = "3.000 STEEM" //手续费
let jsonMetadata = ""
this.steem.broadcast.accountCreate(
creatorWif,
fee,
creator,
newAccountName,
owner,
active,
posting,
memoKey,
jsonMetadata,
(err, result) => {
if(err){
console.log(444, '创建失败!', err)
}else{
console.log(666, '创建成功!', result)
}
})
}

This page is synchronized from the post: ‘SteemJs注册的两种方法 / 网络研习社#56’

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×