BitTap Wallet JSSDK is a powerful tool that allows developers to easily integrate BitTap wallet functionality into their DApps. This document will guide you on how to use the main features of the SDK.
Install BitTap Wallet JSSDK using npm:
npm install @bittap/wallet-sdkFirst, import the SDK and create an instance:
import { WalletSdk, Network } from '@bittap/wallet-sdk';
const walletSdk = new WalletSdk({
network: Network.mainnet,
// Optional parameters:
autoConnection: true/false
});Before using any features of the SDK, you need to connect the wallet first. You can use the following code to connect the wallet:
await walletSdk.connection((res) => {
console.log('Connection successful', res);
});After connecting the wallet, you can specify a network. By default, it is Network.mainnet. If you want to switch to the test network, use the following code:
walletSdk.switchNetwork(Network.testnet);After connecting the wallet, set up a listener which will automatically connect the wallet and listen for account changes. You can use the following code to listen for account changes:
walletSdk.setOnAccountChangeHandler((result) => {
console.log('Account changed', result);
});After connecting the wallet, you can use the following code to get current assets:
async function getCurrentAssets() {
const assets = await walletSdk.getCurrentAssets();
console.log('Current assets', assets);
console.log('BTC assets:', assets.filter((asset) => asset.asset_id === 'Base'));
}After connecting the wallet, you can use the following code to create an invoice for receiving Taproot assets:
async function createInvoice() {
const invoice = await walletSdk.createInvoice({
asset_id: 'your_asset_id',
amount: 100000
});
console.log('Created invoice', invoice);
}After connecting the wallet, you can use the following code to transfer BTC assets:
async function transferBtc() {
const transferOptions = {
recv_addr: 'recipient_address',
amount: 100000,
// Optional parameters:
min_conf: 6,
fee_rate: 1
};
const transferTrx = await walletSdk.transferBtc(transferOptions);
console.log('Transfer transaction', transferTrx);
}async function sendTaprootAssets() {
const sendOptions = {
receive_addr: 'recipient_address',
// Optional parameters:
fee_rate: 1
};
const transferTrx = await walletSdk.sendTaprootAssets(sendOptions);
console.log('Send assets transaction', transferTrx);
}async function signMessage() {
const message = 'Hello, BitTap!';
const signature = await walletSdk.signMessage(message);
console.log('Signature result', signature);
}async function setOnListenTransactionStatusHandler() {
walletSdk.setOnListenTransactionStatusHandler((res) => {
console.log('Transaction status changed', res);
});
}async function addListenTxId() {
const txIds = [
'your_transaction_id_1',
'your_transaction_id_2'
];
walletSdk.addListenTxId(txIds);
}searchAssets() {
const searchOptions = {
// Optional parameters, either asset_id or asset_name must be provided:
asset_id: string,
asset_name: string,
page_num: 6, // Current page number
page_size: 1 // Number of items per page
};
const searchResult = await walletSdk.searchAssets(searchOptions);
console.log('Search result', searchResult);
}Note: Each invoice can be used multiple times.
searchAssets() {
const pageOptions = {
// Optional parameters:
page_num: 6, // Current page number
page_size: 1 // Number of items per page
};
const invoicesResult = await walletSdk.getInvoices(pageOptions);
console.log('Result:', invoicesResult);
}Here's a complete example demonstrating how to use the main features of BitTap Wallet JSSDK:
Example code links:
This example demonstrates how to initialize the SDK, connect the wallet, listen for account changes, and use various main features. You can modify and extend this example according to your needs.
Please note that in actual use, you need to handle errors and exceptions appropriately to ensure the stability and reliability of your application.