Facilitate Secret Key Management with KeyStore CLI

Park Juhyung
CodeChain
Published in
4 min readApr 26, 2019

If you build an app based on blockchains, the first thing you would do is create a secret key. You can transfer coins with the secret key you’ve created. If you lose your secret key, not only will you lose your coins, but will also render already revealed public keys useless. Therefore, keeping your secret key safe is critical.

However, managing the secret key is not a seamless task. It doesn’t feel safe to simply convert the key into a string format and store it into a hard drive, but it doesn’t mean that you’d want to go out of your way to create an encryption format. What you want is a tool that can safely store your private key and allow you to take it out in a simple way when you need it.

Photo by Luka Siemionov from Pexels

CodeChain KeyStore CLI’s task

CodeChain’s KeyStore CLI allows you to easily create and manage secret keys using the command line interface. Basic encryption is provided by allowing the user to lock the secret key with a passphrase. You can load the file created in the CLI through the CodeChain SDK and use the secret key easily when you need it.

Let’s practice the following:

  1. Create a platform account and an asset account using the KeyStore CLI.
  2. Within the SDK, by using an account created through the KeyStore CLI, you can create your own assets in CodeChain.

Install KeyStore CLI

You can install the CodeChain KeyStore by either using the JavaScript package manager or downloading the pre-built binary file. You can use the installation method of your choice.

Install with npm

npm install -g codechain-keystore-cli

Install with yarn

yarn global add codechain-keystore-cli

Download binary file

You can install the binary file from this link.

Test the executable file

If the installation was successful, you can use the KeyStore CLI by using the cckey command. The command below is the simplest command that you can type on the terminal. It verifies the version of the KeyStore CLI currently installed.

cckey --version

Create a project

The KeyStore CLI stores the key file in the current directory. Since we plan to use the CodeChain SDK, we set up the nodejs development environment in advance and then create an account.

In the terminal, enter the following command to create the directory and create the nodejs project:

mkdir keystore-example
cd keystore-example
npm init
npm install --save codechain-sdk@"^1.2.0"

Create an account

Here is how to create a platform account and an asset account.

Platform accounts and asset accounts

CodeChain has two types of accounts depending on the purpose. One type of account is called a platform account that pays for the processing fees in order to process the parcels. The other type of account is called an asset account, which enables you to own or trade assets that were originally published on CodeChain. The upcoming examples create and use both types of accounts.

Create a platform account

Create a platform account by using the following command. The newly created account is created in the working directory when the command was executed. If this command succeeds, a keystore.db file is created in the directory.

cckey create -t platform --network-id cc

The -t platform option specifies the creation of a platform account.

The --network-id option specifies the network that the account will be created in.

CodeChain manages different networks depending on the purpose or goal, and the type of accounts used in each network will vary as well. In this example, we will use “cc” since we will create an account for the main network.

After creating an account, you can use the following command to see all of the accounts managed by the KeyStore CLI:

cckey list -t platform --network-id cc

Receive CCC in your platform account

We need more than 100,100 CCC as the processing fee in order to send assets with the platform account created above. You can buy CCC using BTC or ETH, and the instructions are here. If you purchased CCC, then send CCC to the address you created by using the CodeChain KeyStore CLI.

Create an asset account

You need an asset account to own and transfer assets in CodeChain.

Use the following command to create an asset account:

cckey create -t asset --network-id cc

You can check the created accounts with the following command:

cckey list -t asset --network-id cc

Using a file generated from KeyStore CLI in CodeChain SDK

The account you created earlier is easy to use in the CodeChain SDK.

Read

Create the read_example.js file with the contents below and execute it with `node read_keystore.js`.

This is the code that makes the createLocalKeyStore function read the file that was generated by the KeyStore CLI. Enter the path of the file generated by the KeyStore CLI as the argument of the createLocalKeyStore function. In this example, we use the value “keystore.db” since we used the default file name.

Signing

Any value can be signed using the keys created by the KeyStore CLI.

Run sign_example.js as shown in the example below. For the passphrase variable, assign it with the passphrase that you used earlier when creating the platform key.

Publishing assets

The following example shows how to create a new asset with the key of the platform account created earlier in the KeyStore CLI and the key of the asset account.

Conclusion

If you wish to create an application using CodeChain, the simplest way to manage keys is to use to KeyStore CLI. It allows you to generate and manage keys in a simple manner, and easily read from the SDK. If you are interested in CodeChain, you should definitely give the KeyStore CLI a shot.

--

--