Working with wallet

A KardiaChain wallet contains a unique address and a unique private key which will be used to represent you in blockchain world.

A wallet in some way can be understood as an account in traditional system: address as username and private key as password. But keep in mind that wallet's private key can NOT be changed. If you lose your private key, your wallet will be gone. So, keep your credentials carefully, you're the only personal controlling your wallet.

Creating a new wallet

To create a new wallet, use the generateWallet utility from account module

import {KardiaAccount} from 'kardia-js-sdk';

const wallet = KardiaAccount.generateWallet();

The above code will (or should) return

{
    address: "YOUR_WALLET_ADDRESS",
    privateKey: "YOUR_ADDRESS_PRIVATE_KEY",
    mnemonic: {
        phrase: "YOUR_12_WORDS_MNEMONIC_PHRASE",
        path: "m/44'/60'/0'/0/0",
        locale: "en"        
    },
    balace: 0
}

It's recommended to write down your private key and mnemonic phrase. One of them is enough to access your wallet, but just write down both to make sure.

Access your wallet

To access your wallet, KardiaChain JS SDK support 2 methods:

Using private key:

import {KardiaAccount} from 'kardia-js-sdk';

const wallet = KardiaAccount.getWalletFromPK('YOUR_ADDRESS_PRIVATE_KEY');

Using mnemonic phrase:

import {KardiaAccount} from 'kardia-js-sdk';

const wallet = KardiaAccount.getWalletFromMnemonic('YOUR_MNEMONIC_PHRASE');

Get your wallet KAI balance

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

// Account module
const accountModule = kardiaClient.account;

// Get balance
const balance = await accountModule.getBalance('YOUR_WALLET_ADDRESS');

The above code will return your KAI balance, but in HYDRO (similar to wei in ethereum ecosystem). To convert to KAI, use the following util from KAIChain module

import {KAIChain} from 'kardia-js-sdk';

const balanceInKAI = KAIChain.KAIFromHydro(balanceInHYDRO);

Last updated