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.
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.
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.
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.
- Typescript
- Javascript
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
}
})()
}
# loading code...
The generateAccount method returns an object with the following two properties:
- A key
accountwith the typeKilt.KiltKeyringPair. - A key
mnemonicwith the typestring.
Generating these values takes two steps:
- Create the
mnemonicvalue using themnemonicGenerate()method from theUtils.Cryptopackage. - The
accountvalue first needs akeyringvalue defined, which is a data structure for defining the key pair type. This example usesed25519, butsr25519orecdsaare 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>.
- Typescript
- Javascript
yarn ts-node ./attester/generateAccount.ts
node ./attester/generateAccount.js
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.
WSS_ADDRESS=wss://peregrine.kilt.io
ATTESTER_ACCOUNT_MNEMONIC="warrior icon use cry..."
ATTESTER_ACCOUNT_ADDRESS="4ohMvUHsyeDhMVZF..."
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>.