Skip to main content

Account

With the project structure setup in the last step, you can create your Attester account.

With KILT, an account is an object that interacts with the blockchain.

KILT Account

A KILT account is a set of cryptographic elements:

  • The address, generated from the public key, is the entity's unique and public on-chain identifier, used to pay fees and deposits.
  • A signing key pair to write transactions on-chain

To create an account, you need a mnemonic.

Mnemonic

In cryptography, a mnemonic consists of a series of 12 or 24 random words.

For example, waste frown beach save hidden bar inmate oil mind member junk famous is a mnemonic.

You use a mnemonic to generate signing key pairs. What's great about a mnemonic is that it's human-readable, and a person could memorize it to later re-generate their key pairs and address. A mnemonic is critical for security, so it's crucial to keep it safe!

Create the Accountโ€‹

To generate an account, use the addFromMnemonic() function on the KiltKeyringPair interface of the SDK. The function uses the underlying polkadot mnemonicGenerate() function to generate a 12-word mnemonic.

polkadot.js

The KILT SDK is built on top of the polkadot.js library, so this workshop uses several functions from the library.

The library provides tools to interact with the KILT blockchain and other Substrate-based blockchains.

In addition, the polkadot.js library offers cryptographic primitives and a serialization framework to encode/decode data sent to and received from the blockchain. Read the API documentation to learn more about the functions available.

Add the following code to the generateAccount file.

attester/generateAccount.ts
import { config as envConfig } from 'dotenv'

import * as Kilt from '@kiltprotocol/sdk-js'

export function generateAccount(
mnemonic = Kilt.Utils.Crypto.mnemonicGenerate()
): {
account: Kilt.KiltKeyringPair & { type: 'ed25519' }
mnemonic: string
} {
return {
account: Kilt.Utils.Crypto.makeKeypairFromUri(mnemonic),
mnemonic
}
}

// Don't execute if this is imported by another file.
if (require.main === module) {
;(async () => {
envConfig()

try {
await Kilt.init()

const { mnemonic, account } = generateAccount()
console.log('save to mnemonic and address to .env to continue!\n\n')
console.log(`ATTESTER_ACCOUNT_MNEMONIC="${mnemonic}"`)
console.log(`ATTESTER_ACCOUNT_ADDRESS="${account.address}"\n\n`)
} catch (e) {
console.log('Error while setting up attester account')
throw e
}
})()
}

The generateAccount method returns an object with the following two properties:

  • A key account with the type Kilt.KiltKeyringPair.
  • A key mnemonic with the type string.

Generating these values takes two steps:

  1. Create the mnemonic value using the mnemonicGenerate() method from the Utils.Crypto package.
  2. The account value first needs a keyring value defined, which is a data structure for defining the key pair type. This example uses ed25519, but sr25519 or ecdsa are also valid.

The function then returns the value using the makeKeypairFromUri() method to create a key pair for the address using the given mnemonic.

The rest of the code runs the generateAccount function and logs the results to the console.

Run codeโ€‹

Run the code above to receive your Attester <address> and <mnenomic>.

yarn ts-node ./attester/generateAccount.ts

The output provides you with an ATTESTER_ACCOUNT_MNEMONIC and ATTESTER_ACCOUNT_ADDRESS. Save both values in your .env file, which should look similar to the below.

.env
WSS_ADDRESS=wss://peregrine.kilt.io

ATTESTER_ACCOUNT_MNEMONIC="warrior icon use cry..."
ATTESTER_ACCOUNT_ADDRESS="4ohMvUHsyeDhMVZF..."
Get PILT coins!

You now have a blockchain account to use to pay fees and deposits. If you haven't already requested PILT, go to the faucet and request tokens for your <address>.