Add an administrator account
At this point, the election application doesn't restrict who can nominate a candidate. In this tutorial, you'll create an administrative account to control the actions that can be taken in the election application.
Kadena accounts consist of four parts:
- An account name.
- An account balance.
- One or more public keys that can be used to sign transactions.
- A predicate that specifies how many keys are required to sign transactions.
For this tutorial, you'll create an administrative account with only one key and
you'll use the default keys-all
predicate. The keys-all
predicate requires
transactions to be signed by all keys in the account. Because you're creating
this account with only one key, only one signature will be required to sign
transactions. You can use any text string of three to 256 characters for the
account name. However, the convention for accounts that only have one key is to
use the prefix k:
followed by public key of the account.
For a more general introduction to how public and private keys are used to sign transactions and in Kadena accounts, see Beginner’s Guide to Kadena: Accounts + Keysets.
Before you begin
Before you start this tutorial, verify the following basic requirements:
- You have an internet connection and a web browser installed on your local computer.
- You have a code editor, such as Visual Studio Code, access to an interactive terminal shell, and are generally familiar with using command-line programs.
- You have cloned the
[election-dapp](https://github.com/kadena-community/voting-dapp.git
election-dapp) repository as described in
Prepare your workspace
and have checked out the
01-getting-started
branch. - You have the development network running in a Docker container as described in Start a local blockchain.
- You have created a Chainweaver account and are connected to the development network using your local host IP address and port number 8080.
Generate the administrative key pair
There are many tools you can use to create keys, including the Chainweaver application you used in previous tutorial. In fact, if you followed that tutorial, you already have a public and private key pair that you could use as the basis for your administrative account. However, for demonstration purposes in this tutorial, let's create a new key in Chainweaver and use the new key as the basis for your Kadena account.
To create a new key pair and account:
-
Verify the development network is currently running on your local computer.
-
Open Chainweaver.
-
Select devnet from the network list.
-
Click Keys in the Chainweaver navigation panel.
-
Click Generate Key to add a new public key to your list of public keys.
-
Click Add k: Account for the new public key to add a new account to the list of accounts you are watching in Chainweaver.
If you expand the new account, you'll see that no balance exists for the account on any chain and there's no information about the owner or keyset for the account. For example:
In this initial state, the account name acts as a placeholder but the account doesn't exist yet on the development network.
If you change to the
./snippets
folder in the root of your project, you can also verify that your account doesn't exist using the Kadena client. For example, you can run thecoin-details
script for the administrative account you just added with a command similar to the following:npm run coin-details:devnet -- k:<your-public-key>
npm run coin-details:devnet -- k:<your-public-key>
Because your account doesn't exist yet on the development network, the script returns an error similar to the following:
type: 'TxFailure', message: 'with-read: row not found: k:5ec41b89d323398a609ffd54581f2bd6afc706858063e8f3e8bc76dc5c35e2c0',
type: 'TxFailure', message: 'with-read: row not found: k:5ec41b89d323398a609ffd54581f2bd6afc706858063e8f3e8bc76dc5c35e2c0',
Transfer coins to create an account
An account must have funds before it can be used on any Kadena blockchain. To
interact with the Kadena main public network (mainnet
), you typically buy KDA
as a digital asset through an exchange then transfer the KDA to the Kadena
account you want to fund. For the Kadena test network (testnet
), you can
submit a request to transfer 20 KDA to a
specified account for testing purposes.
To interact with the local development network (devnet
), you can either:
- Create your own faucet contract.
- Transfer KDA from a test account with publicly published private keys.
For this tutorial, you can fund the administrative account that governs the election application by transferring KDA from one of the test accounts.
To transfer coins to fund the administrative account:
-
Open the
election-dapp/snippets
folder in a terminal shell on your computer.As you saw in the previous tutorial, the development network includes several smart contracts by default. In this tutorial, you'll use the
transfer-create
function that's defined in thecoin
smart contract to transfer 20 KDA to the administrative account. -
Open the
./transferCreate.ts
script in your code editor.This script uses the Kadena client to call the
transfer-create
function of thecoin
contract to create and fund your administrative account. After importing the dependencies and creating the client with thedevnet
configuration, themain
function is called with information about the sender, the receiver, and the amount.- The sender—
sender00
—is one of the pre-installed Devnet test accounts that holds some KDA on all chains. The public and private key for this account are copied from GitHub. - You specify the receiver—your administrative account name—as an argument when running the script.
- The amount to transfer to the receiver is hardcoded to 20.
Inside the
main
function, the amount to transfer is converted to aPactDecimal
with the format:{ decimal: '20.0' }
.The script then generates the Pact code and creates the transaction for you, based on the typing information from the previous step and the arguments provided to the function.
The third argument of
transfer-create
is a function that returns the guard for the account to be created. To define the guard for the account, the.addData()
function uses the public key of the administrative account as the only key andkeys-all
as the predicate. This guard associates the public key of your administrative account with the private key you hold in Chainweaver to ensure that only you can control the account.The
.addSigner()
function specifies that the sender must sign the transaction to pay the gas fee and to make the transfer with the provided details. In the.setMeta()
function,sender00
is specified as thesenderAccount
. After setting the network id from thedevnet
configuration, the transaction is created. Next, the transaction is signed with the private key of thesender00
account and the transaction is submitted. - The sender—
-
Create and fund your administrative account using the
transfer-create
script by running a command similar to the following with your administrative account name:npm run transfer-create:devnet -- k:<your-public-key>
npm run transfer-create:devnet -- k:<your-public-key>
After a few seconds, you should see a status message:
{ status: 'success', data: 'Write succeeded' }
{ status: 'success', data: 'Write succeeded' }
-
Verify that your account was created using the Kadena client and the
coin-details
script for the administrative account with a command similar to the following:npm run coin-details:devnet -- k:<your-public-key>
npm run coin-details:devnet -- k:<your-public-key>
You should see information about the new account similar to the following:
{ guard: { pred: 'keys-all', keys: [ '5ec41b89d323398a609ffd54581f2bd6afc706858063e8f3e8bc76dc5c35e2c0' ] }, balance: 20, account: 'k:5ec41b89d323398a609ffd54581f2bd6afc706858063e8f3e8bc76dc5c35e2c0'}
{ guard: { pred: 'keys-all', keys: [ '5ec41b89d323398a609ffd54581f2bd6afc706858063e8f3e8bc76dc5c35e2c0' ] }, balance: 20, account: 'k:5ec41b89d323398a609ffd54581f2bd6afc706858063e8f3e8bc76dc5c35e2c0'}
-
Click Accounts in the Chainweaver navigation panel.
-
Expand your administrative account to verify that on chain 1 you are the owner, one keyset is defined, and the balance is 20 KDA.
Next steps
In this tutorial, you learned how to:
- Generate a new public and secret key for a Kadena account using Chainweaver.
- Transfer coins to create and fund a new account using the Kadena client.
- Verify account information using the Kadena client and in Chainweaver.
You'll use the KDA you transferred to the administrative account to pay transaction fees for defining keysets, deploying smart contract modules, and nominating candidates in later tutorials. Before we get there, the next tutorial demonstrates how to create a namespace for a new keyset and smart contracts.