|
| 1 | +# Create an account |
| 2 | + |
| 3 | +_In this tutorial, we will create Steem account using Steemconnect as well as with Client-side signing method._ |
| 4 | + |
| 5 | +This tutorial runs on the main Steem blockchain. And accounts queried/searched are real accounts and creator account show have sufficient liquid balance to create new account. |
| 6 | + |
| 7 | +## Intro |
| 8 | + |
| 9 | +This tutorial will show few functions such as querying account by name and check if username is taken or available to register. We are using the `call` function provided by the `dsteem` library to pull account from the Steem blockchain. We then create proper private keys for new account. A simple HTML interface is used to enter payment of account creation fee and create account right inside tutorial. |
| 10 | + |
| 11 | +## Steps |
| 12 | + |
| 13 | +1. [**App setup**](#app-setup) Setup `dsteem` to use the proper connection and network. |
| 14 | +2. [**Search account**](#search-account) Get account details after input has account name |
| 15 | +3. [**Generate private keys**](#generate-keys) Generate proper keys for new account |
| 16 | +4. [**Create account**](#create-account) Create account via Client-side or Steemconnect |
| 17 | + |
| 18 | +#### 1. App setup <a name="app-setup"></a> |
| 19 | + |
| 20 | +Below we have `dsteem` pointing to the production network with the proper chainId, addressPrefix, and endpoint. There is a `public/app.js` file which holds the Javascript segment of this tutorial. In the first few lines we define the configured library and packages: |
| 21 | + |
| 22 | +```javascript |
| 23 | +const dsteem = require('dsteem'); |
| 24 | +let opts = {}; |
| 25 | +//connect to production server |
| 26 | +opts.addressPrefix = 'STM'; |
| 27 | +opts.chainId = |
| 28 | + '0000000000000000000000000000000000000000000000000000000000000000'; |
| 29 | +//connect to server which is connected to the network/production |
| 30 | +const client = new dsteem.Client('https://api.steemit.com'); |
| 31 | +``` |
| 32 | + |
| 33 | +#### 2. Search account <a name="search-account"></a> |
| 34 | + |
| 35 | +After account name field is filled with some name, tutorial has Search button to search for account by name. HTML input forms can be found in the `index.html` file. The values are pulled from that screen with the below: |
| 36 | + |
| 37 | +```javascript |
| 38 | + const accSearch = document.getElementById('username').value; |
| 39 | + let avail = 'Account is NOT available to register' |
| 40 | + if (accSearch.length>2) { |
| 41 | + const _account = await client.database.call('get_accounts', [[accSearch]]); |
| 42 | + console.log(`_account:`, _account, accSearch.length); |
| 43 | + |
| 44 | + if (_account.length==0) { |
| 45 | + avail = 'Account is available to register' |
| 46 | + } |
| 47 | + } |
| 48 | +``` |
| 49 | + |
| 50 | +We will then do simple check if account is taken or not. |
| 51 | + |
| 52 | +#### 3. Generate private keys <a name="generate-keys"></a> |
| 53 | + |
| 54 | +After we know that account is available to register, we will fill form with password we wish for that account and enter creation fee. Note, that creation fees are deposited into new account, if VESTS is entered it will be powered up to new account. |
| 55 | + |
| 56 | +```javascript |
| 57 | +const username = document.getElementById('username').value; |
| 58 | +const password = document.getElementById('password').value; |
| 59 | + |
| 60 | +const ownerKey = dsteem.PrivateKey.fromLogin(username, password, 'owner'); |
| 61 | +const activeKey = dsteem.PrivateKey.fromLogin(username, password, 'active'); |
| 62 | +const postingKey = dsteem.PrivateKey.fromLogin(username, password, 'posting'); |
| 63 | +const memoKey = dsteem.PrivateKey.fromLogin(username, password, 'memo'); |
| 64 | + |
| 65 | +const ownerAuth = { |
| 66 | + weight_threshold: 1, |
| 67 | + account_auths: [], |
| 68 | + key_auths: [[ownerKey.createPublic(), 1]], |
| 69 | +}; |
| 70 | +const activeAuth = { |
| 71 | + weight_threshold: 1, |
| 72 | + account_auths: [], |
| 73 | + key_auths: [[activeKey.createPublic(), 1]], |
| 74 | +}; |
| 75 | +const postingAuth = { |
| 76 | + weight_threshold: 1, |
| 77 | + account_auths: [], |
| 78 | + key_auths: [[postingKey.createPublic(), 1]], |
| 79 | +}; |
| 80 | +``` |
| 81 | + |
| 82 | +Above script shows, how to properly setup private keys of new account. |
| 83 | + |
| 84 | +#### 4. Create account <a name="create-account"></a> |
| 85 | + |
| 86 | +After following all steps properly, we can now submit transaction to create new account. |
| 87 | + |
| 88 | +```javascript |
| 89 | +const privateKey = dsteem.PrivateKey.fromString( |
| 90 | + document.getElementById('wif').value |
| 91 | +); |
| 92 | +const op = [ |
| 93 | + 'account_create', |
| 94 | + { |
| 95 | + creator: document.getElementById('account').value, |
| 96 | + new_account_name: username, |
| 97 | + owner: ownerAuth, |
| 98 | + active: activeAuth, |
| 99 | + posting: postingAuth, |
| 100 | + memo_key: memoKey, |
| 101 | + json_metadata: '', |
| 102 | + }, |
| 103 | +]; |
| 104 | + |
| 105 | +client.broadcast.sendOperations([op], privateKey).then( |
| 106 | + function(result) { |
| 107 | + document.getElementById('result').style.display = 'block'; |
| 108 | + document.getElementById( |
| 109 | + 'result' |
| 110 | + ).innerHTML = `<br/><p>Included in block: ${ |
| 111 | + result.block_num |
| 112 | + }</p><br/><br/>`; |
| 113 | + }, |
| 114 | + function(error) { |
| 115 | + console.error(error); |
| 116 | + } |
| 117 | +); |
| 118 | +``` |
| 119 | + |
| 120 | +Note in above creator's account name and active private keys are used for creation. |
| 121 | + |
| 122 | +That's it! |
| 123 | + |
| 124 | +### To run this tutorial |
| 125 | + |
| 126 | +1. clone this repo |
| 127 | +1. `cd tutorials/24_create_account` |
| 128 | +1. `npm i` |
| 129 | +1. `npm run dev-server` or `npm run start` |
| 130 | +1. After a few moments, the server should be running at [http://localhost:3000/](http://localhost:3000/) |
0 commit comments