Legacy
You are viewing legacy documentation. For current SDK docs, see Introduction to Klever Blockchain SDK.
Node.js SDK (@klever/sdk-node)
This package is still functional but no longer the recommended way to integrate with the Klever Blockchain. Use @klever/connect and NodeWallet instead.
Installation
npm install @klever/sdk-node
Quick Start
import { Account, TransactionType } from '@klever/sdk-node'
const account = new Account('yourPrivateKey')
await account.ready
const broadcastRes = await account.quickSend([
{
payload: { amount, receiver, kda },
type: TransactionType.Transfer,
},
])
console.log(broadcastRes)
Changing Network
import { utils, IProvider } from '@klever/sdk-node'
const provider: IProvider = {
api: 'https://api.testnet.klever.org',
node: 'https://node.testnet.klever.org',
}
utils.setProviders(provider)
Account Methods
| Method | Parameters | Return | Description |
|---|---|---|---|
getBalance | () | number | Gets the account's balance |
getNonce | () | number | Gets the account's nonce |
getAddress | () | string | Gets the account's address |
sync | () | Promise<void> | Syncs balance and nonce with node |
buildTransaction | (contracts, txData?, options?) | Promise<ITransaction> | Builds a transaction ready to sign |
signMessage | (message: string) | Promise<string> | Signs any message |
signTransaction | (tx: ITransaction) | Promise<ITransaction> | Signs a transaction |
quickSend | (contracts, txData?, options?) | Promise<IBroadcastResponse> | Build + sign + broadcast |
broadcastTransactions | (txs: ITransaction[]) | Promise<IBroadcastResponse> | Broadcasts transactions |
validateSignature | (message, signature) | Promise<boolean> | Verifies a signature |
downloadAsPem | (path?) | Promise<void> | Saves .pem file |
Utility Methods
| Method | Parameters | Return | Description |
|---|---|---|---|
getAddressFromPrivateKey | (privatekey) | Promise<string> | Derives address from private key |
generateKeyPair | () | Promise<{ privatekey, address }> | Generates new keypair |
setProviders | (providers: IProvider) | void | Sets network endpoints |
getProviders | () | IProvider | Gets current provider |
broadcastTransactions | (txs) | Promise<IBroadcastResponse> | Broadcasts transactions |
decodeTransaction | (tx) | Promise<IDecodedTransaction> | Human-readable JSON decode |
validateSignature | (message, signature, address) | Promise<boolean> | Verifies a signature |
decodeAddress | (address) | Promise<Uint8Array> | Decodes Klever address |
Web SDK (@klever/sdk-web / @klever/sdk-node)
These packages are still functional but no longer the recommended way to integrate with the Klever Blockchain. The new package is @klever/connect.
Installation
Web App
$ npm install @klever/sdk-web
or
$ yarn add @klever/sdk-web
You can also import the script directly from this url, as exemplified here.
NodeJS
NodeJS must be above version 18.0.0
$ npm install @klever/sdk-node
or
$ yarn add @klever/sdk-node
Web App Usage
To use the Klever Blockchain sdk in a web application, use the web object. It manages the window object to use the appropriate account manager (web extension or mobile wallet).
Vanilla JS
import {
web,
TransactionType,
} from 'https://sdk.kleverscan.org/kleverchain-sdk-web-esm-1-0-x.js'
const provider = {
api: 'https://api.testnet.klever.org',
node: 'https://node.testnet.klever.org',
}
web.setProvider(provider)
const connectWallet = async () => {
await web.initialize()
}
const sendTransaction = async () => {
const payload = {
amount: 100 * 10 ** 6,
receiver: 'klv1fpwjz234gy8aaae3gx0e8q9f52vymzzn3z5q0s5h60pvktzx0n0qwvtux5',
kda: 'KLV',
}
try {
const unsignedTx = await web.buildTransaction([
{
payload,
type: TransactionType.Transfer,
},
])
const signedTx = await web.signTransaction(unsignedTx)
const response = await web.broadcastTransactions([signedTx])
console.log(response)
} catch (error) {
console.log(error)
}
}
React TS
import React from 'react'
import { IProvider, web, ITransfer, TransactionType } from '@klever/sdk-web'
const provider: IProvider = {
api: 'https://api.testnet.klever.org',
node: 'https://node.testnet.klever.org',
}
web.setProvider(provider)
const connectWallet = async () => {
await web.initialize()
}
const sendTransaction = async () => {
const payload: ITransfer = {
amount: 100 * 10 ** 6,
receiver: 'klv1fpwjz234gy8aaae3gx0e8q9f52vymzzn3z5q0s5h60pvktzx0n0qwvtux5',
kda: 'KLV',
}
try {
const unsignedTx = await web.buildTransaction([
{ payload, type: TransactionType.Transfer },
])
const signedTx = await web.signTransaction(unsignedTx)
const response = await web.broadcastTransactions([signedTx])
console.log(response)
} catch (error) {
console.log(error)
}
}
Changing Network
import { web, IProvider } from '@klever/sdk-web'
const provider: IProvider = {
api: 'https://api.testnet.klever.org',
node: 'https://node.testnet.klever.org',
}
web.setProvider(provider)
web.initialize()
All web Object Methods
| Method | Parameters | Return | Description |
|---|---|---|---|
| isKleverWebLoaded | () | boolean | Checks if a Klever Web account manager is loaded. |
| isKleverWebActive | () | boolean | Checks if the loaded account manager is initialized. |
| broadcastTransactions | (transactions: ITransaction[]) | Promise<IBroadcastResponse> | Broadcasts transactions to the provided node. |
| signMessage | (message: string, privateKey: string) | Promise<string> | Signs any message using the account manager. |
| signTransaction | (transaction: ITransaction) | Promise<ITransaction> | Signs a transaction using the account manager. |
| validateSignature | (message: string, signature: string, publickey: string) | Promise<boolean> | Checks if the signature and signer are compatible. |
| buildTransaction | (contracts: IContractRequest[], txData?: string[], options?: ITxOptionsRequest) | Promise<ITransaction> | Builds a simplified transaction ready to be signed. |
| initialize | (timeout?: number) | Promise<void> | Initializes the account manager (default timeout: 5000ms). |
| getWalletAddress | () | string | Returns the selected account address. |
| getProvider | () | IProvider | Gets the current provider object. |
| setProvider | (providers: IProvider) | void | Sets the provider object. |
SDK v2 (@klever/sdk)
The oldest SDK version, using a WebAssembly + PEM file model. No longer maintained.
Full v2 documentation has been archived at Legacy / SDK v2.
Web App (@klever/sdk-web)
This package is still functional but no longer the recommended way to build browser-based dApps. Use @klever/connect-react or BrowserWallet instead.
Installation
npm install @klever/sdk-web
Vanilla JS
import { web, TransactionType } from '@klever/sdk-web'
web.setProvider({
api: 'https://api.testnet.klever.org',
node: 'https://node.testnet.klever.org',
})
// Connect
await web.initialize()
// Transfer
const unsignedTx = await web.buildTransaction([{
payload: { amount: 100 * 10 ** 6, receiver: 'klv1receiver...', kda: 'KLV' },
type: TransactionType.Transfer,
}])
const signedTx = await web.signTransaction(unsignedTx)
const response = await web.broadcastTransactions([signedTx])
console.log(response)
React TS
import { web, IProvider, TransactionType } from '@klever/sdk-web'
web.setProvider({
api: 'https://api.testnet.klever.org',
node: 'https://node.testnet.klever.org',
})
function ConnectButton() {
return <button onClick={() => web.initialize()}>Connect</button>
}
function SendTXButton() {
const send = async () => {
const unsignedTx = await web.buildTransaction([{
payload: { amount: 100 * 10 ** 6, receiver: 'klv1receiver...', kda: 'KLV' },
type: TransactionType.Transfer,
}])
const signedTx = await web.signTransaction(unsignedTx)
await web.broadcastTransactions([signedTx])
}
return <button onClick={send}>Send</button>
}
All web object methods
| Method | Parameters | Return | Description |
|---|---|---|---|
initialize | (timeout?) | Promise<void> | Initializes the account manager |
buildTransaction | (contracts, txData?, options?) | Promise<ITransaction> | Builds a transaction |
signTransaction | (tx) | Promise<ITransaction> | Signs a transaction |
broadcastTransactions | (txs) | Promise<IBroadcastResponse> | Broadcasts transactions |
signMessage | (message, privateKey) | Promise<string> | Signs a message |
validateSignature | (message, signature, publickey) | Promise<boolean> | Verifies a signature |
getWalletAddress | () | string | Returns selected account address |
setProvider | (providers) | void | Sets the provider object |
getProvider | () | IProvider | Gets current provider |
isKleverWebLoaded | () | boolean | Checks if wallet is loaded |
isKleverWebActive | () | boolean | Checks if wallet is initialized |
If you want to chain contracts with metadata, use both props, each metadata will be assigned to the respective contract, they are related by the data array index.