使用 bitsharesjs 库创建新用户Demo

in #cn5 years ago (edited)

整理了一下用 js-sdk 创建新用户的最简单的 Demo 实现代码。

代码

  import {PrivateKey, key, FetchChain, TransactionBuilder} from 'bitsharesjs';
  import {Apis, ChainConfig} from 'bitsharesjs-ws';
  
  generateKeyFromPassword(accountName, role, password) {
    const seed = accountName + role + password;
    const privKey = PrivateKey.fromSeed(seed);
    const pubKey = privKey.toPublicKey().toString();
    return {privKey, pubKey};
  }
  
  registerUser(username, password, registrar, referrer) {
    const privKey = '这里是用来签名数据的用户active私钥';
    const pKey = PrivateKey.fromWif(this.privKey);
    const referrerPercent = 0;
    const {pubKey: ownerPubkey} = generateKeyFromPassword(
      username,
      'owner',
      password
    );
    const {pubKey: activePubkey} = generateKeyFromPassword(
      username,
      'active',
      password
    );
    const {pubKey: memoPubkey} = generateKeyFromPassword(
      username,
      'memo',
      password
    );

    try {
      return Promise.all([
        FetchChain("getAccount", registrar),
        FetchChain("getAccount", referrer)
      ]).then((res) => {
        const [chainRegistrar, chainReferrer] = res;
        const tr = new TransactionBuilder();
        tr.add_type_operation("account_create", {
          fee: {
            amount: 0,
            asset_id: 0
          },
          registrar: chainRegistrar.get("id"),
          referrer: chainReferrer.get("id"),
          referrer_percent: referrerPercent,
          name: username,
          owner: {
              weight_threshold: 1,
              account_auths: [],
              key_auths: [[ownerPubkey, 1]],
              address_auths: []
          },
          active: {
              weight_threshold: 1,
              account_auths: [],
              key_auths: [[activePubkey, 1]],
              address_auths: []
          },
          options: {
              memo_key: memoPubkey,
              voting_account: "1.2.1",
              num_witness: 0,
              num_committee: 0,
              votes: []
          }
        });
        return tr.set_required_fees().then(() => {
          tr.add_signer(pKey);
          console.log("serialized transaction:", tr.serialize());
          tr.broadcast();
          return true;
        });
      }).catch((err) => {
        console.log('err:', err);
      });
    } catch(e) {
      console.log('unexpected_error:', e);
    }
  }
  
  registerUser('新用户名', '新用户的密码', '用来签名的用户的用户名', '推荐用户的用户名');

说明

  • registerUser 函数参数中的 registrar 是用来签发数据的用户的用户名,函数里面的 privKeyregistraractive 私钥
  • referrerPercent 是分成比例,这里注意是基于手续费的 50% 再分成。也就是 referrerPercent 设置为 10000 ,则代表 registrar 分成 0%,referrer 分成 50%。
  • 提交的数据中的 voting_account 是设置投票代理人是谁。

ET碎碎念,每周一,晚六点一刻更新,欢迎订阅


欢迎使用 SteemEditor 来编写文章,获取 @steemeditor.bot 的点赞!


感谢你的阅读,我是中文区见证人之一,欢迎通过 SteemConnect 来给我投票,或者打开 https://steemitwallet.com/~witnesses 页面,输入 ety001 进行投票。

2.gif

中文区的见证人目前有:
支持一下他们(按字母顺序),一人可以有30票


Thank you for reading. I'm a witness. I would really appreciate your witness vote! You can vote by SteemConnect. Or open https://steemitwallet.com/~witnesses page, input ety001 to vote.

2.gif

Sort:  

感谢代理200.43SP 给team-cn! 由于你使用CN作为你的首标签,额外获得2%点赞! 你的帖子获得team-cn 14% 点赞!(如果不想看到这个回复,请回复“取消”)

Congratulations @ety001! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got more than 200 replies. Your next target is to reach 300 replies.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.028
BTC 62800.25
ETH 2449.72
USDT 1.00
SBD 2.57