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 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| 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('新用户名', '新用户的密码', '用来签名的用户的用户名', '推荐用户的用户名');
|