NEXT SDK

The NEXT SDK allows you to integrate your dApps with the Bitkub NEXT Wallet, which provides native support for the KUB network. It offers various methods for managing user authentication, retrieving user information, and executing transactions on the blockchain. With the NEXT SDK, your dApps can integrate with the Bitkub NEXT Wallet Authentication services and submit transactions directly on the network.

circle-exclamation

Prerequisites

Before installing or interacting with the NEXT SDK, we highly recommend generating the necessary keys, including the Client ID and Project ID.

Installation

To install the NEXT SDK, you can use npm, pnpm, or yarn.

npm install @bitkub-chain/sdk.js

Initialization

To initialize the SDK, create lib/bitkubchain-sdk/index.ts at the root project with the clientID and projectID.

import { initializeSDK, Network } from '@bitkub-chain/sdk.js';

const clientID = 'your-client-id';
const projectID = 'your-project-id';
const network = Network.BKC_TESTNET;
const initOpts = {
  loginRedirectPath: '/oauth/callback',
};

const sdk = initializeSDK(clientID, projectID, network, initOpts);
circle-info

Please declare your network as Network.BKC_TESTNET for KUB Testnet and Network.BKC_MAINNET for KUB Mainnet.

Authentication

To begin with authentication, you must set up OAuth to allow your dApp to log in with Bitkub NEXT. The first step is to create /oauth/callback page, you can use the following code:

Redirect URI

You can visit the KUB Playgroundarrow-up-right to configure your redirect URI for your dApp.

If you have not specified a loginRedirectPath, the default redirect URI will be /oauth/callback. You should configure it as follows:

If you have specified a custom loginRedirectPath, update your redirect UI as follows:

Login

Opens a login window for Bitkub NEXT.

Logout

Logs out the current user.

Exchange Authorization Code

Exchanges the OAuth code for access and refresh tokens.

User Information

Get User Information

Returns an object containing user details.

Get Wallet Address

Fetches the user's wallet address.

Get Phone Number

Fetches the user's phone number.

Get Email

Fetches the user's email address.

Get User ID

Fetches the user's unique ID.

Balances

Native Balance

Returns the user's native balance in wei.

circle-info

If the network is Network.BKC_MAINNET or Network.BKC_TESTNET, KKUB balance will be returned. Otherwise, the native currency of the network will be returned.

KAP-20 Token Balance

Returns the balance of a specific KAP-20 token in wei.

KAP-721 Token Balance

Returns the number of KAP-721 tokens owned by the user.

KAP-1155 Token Balance

Returns the quantity of a specific KAP-1155 token owned by the user.

Token Approval and Transfers

Users must approve an allowance before transferring KAP-20 to ensure that only authorized smart contracts can spend their tokens, preventing unauthorized access and protecting their assets.

Get Token Allowance

Gets the current token allowance for a given spender in wei.

Approve Token

Approves a spender to use a specified amount of tokens.

Transfer Native Token

Transfers native KUB to a recipient.

Transfer KAP-20 Token

Transfers a specified KAP-20 token to a recipient.

NFT Approval and Transfers

Users must approve before transferring NFTs to ensure that only authorized smart contracts can send their NFTs, preventing unauthorized access and protecting their assets.

NFT Approval

Checks if a given operator is approved for the user's NFT.

Approve NFT

Grants approval to an operator to manage the user's NFT.

Transfer KAP-721 Token

Transfers a KAP-721 token to a recipient.

Transfer KAP-1155 Token

Transfers a quantity of a KAP-1155 token to a recipient.

Custom Transaction

Sends a custom smart contract transaction using human-readable ABI and parameters.

Fetch Transaction Details

Returns the details of a submitted transaction using its queue ID.

Smart Contract Design for NEXT SDK

To allow Bitkub NEXT users to write data on a smart contract, you need to design the smart contract with additional functions. The additional functions are called through the CallHelper smart contract to execute commands on behalf of the Bitkub NEXT users. The function must also use an extra address _bitkubNext parameter at the end of the function.

Example

This function for buying an NFT requires 2 parameters, which are _recipeIndex and _amount. MetaMask or other wallets can call this function to buy an NFT.

The function must use an extra address _bitkubNext parameter at the end of the function to allow Bitkub NEXT users to call this function to buy an NFT.

You can now apply the human-readable ABI when you are calling the NEXT SDK for your custom transaction.

SDK CallHelper Router

When an end user invokes a function through the SDK library, the msg.sender interaction with your smart contract will be sdkCallHelperRouter. To ensure that only authorized calls are made to this function, you must include a modifier to check that msg.sender equals sdkCallHelperRouter.

Example Modifier

circle-exclamation

Last updated