Getting Started
This guide will walk you through integrating the Turing Wallet SDK into your DApp from scratch.
Prerequisites
- Node.js 16+ and a package manager (npm / pnpm / yarn)
- A front-end project (React, Vue, Vanilla JS, etc.)
- Users of your DApp need the Turing Wallet browser extension or mobile app installed
Installation
npm install turing-wallet-providerpnpm add turing-wallet-provideryarn add turing-wallet-providerTwo Ways to Use the SDK
The SDK provides two access patterns. Both are fully equivalent in functionality.
Option A: useTuringWallet() Hook (Recommended)
Best for React and component-based frameworks. Returns a wallet instance scoped to the current context.
import { useTuringWallet } from "turing-wallet-provider";
const wallet = useTuringWallet();
const addresses = await wallet.connect();Option B: Turing Global Object
Available after the wallet extension injects itself into the page. Works in any environment.
const addresses = await Turing.connect();Which one should I use?
Both provide the same API surface. If you are using React or a similar framework, prefer useTuringWallet() for better lifecycle management. If you are writing vanilla JavaScript or a script, use the Turing global object.
Minimal DApp Example
Here is a complete end-to-end flow — detect wallet → connect → get address → send a transaction:
import { useTuringWallet } from "turing-wallet-provider";
const wallet = useTuringWallet();
// 1. Connect to wallet
const addresses = await wallet.connect();
console.log("Connected:", addresses);
// BVM account: { tbcAddress, btcAddress }
// EVM account: { ethAddress, bnbAddress }
// 2. Check connection status
const connected = await wallet.isConnected();
console.log("Is connected:", connected);
// 3. Get wallet info
const { name, platform, version } = await wallet.getInfo();
console.log(`Wallet: ${name} v${version} on ${platform}`);
// 4. Get public key
const { tbcPubKey } = await wallet.getPubKey();
console.log("Public key:", tbcPubKey);
// 5. Send a simple P2PKH transaction
try {
const { txid, error } = await wallet.sendTransaction([
{
flag: "P2PKH",
address: "recipient_tbc_address_here",
satoshis: 10000,
broadcastEnabled: true,
},
]);
if (txid) {
console.log("Transaction sent! txid:", txid);
}
if (error) {
console.error("Transaction failed:", error);
}
} catch (err) {
console.error("Unexpected error:", err);
}
// 6. Disconnect when done
await wallet.disconnect();Supported Chains
Turing Wallet is a multi-chain wallet. Different API methods target different chains:
| Chain | Methods | Address Format |
|---|---|---|
| TBC (Turing BitChain) | sendTransaction, signTransaction, signAssociatedTransaction, sendBatchRequest | tbcAddress |
| BTC (Bitcoin) | btc.sendTransaction, btc.signTransaction, btc.sendBatchRequest | btcAddress |
| Ethereum | evm.sendTransaction | ethAddress |
| BSC | evm.sendTransaction | bnbAddress |
Error Handling
Most API methods return an error field on failure rather than throwing exceptions. Always check for errors:
const result = await wallet.sendTransaction(params);
if (result.error) {
// Handle the error — display to user, retry, etc.
console.error("Error:", result.error);
return;
}
// Success path
console.log("txid:", result.txid);For methods like signMessage, encrypt, and decrypt, errors are thrown as exceptions. Wrap these in try-catch:
try {
const { sig } = await wallet.signMessage({
message: "hello",
encoding: "utf-8",
});
} catch (error) {
console.error("Signing failed:", error);
}Offline Signing (broadcastEnabled)
Many transaction methods support a broadcastEnabled parameter:
true(default) — The wallet signs and broadcasts the transaction, returningtxid.false— The wallet only signs, returningtxraw(the raw signed transaction hex). You broadcast it yourself.
// Get raw signed transaction without broadcasting
const { txraw } = await wallet.sendTransaction([
{
flag: "P2PKH",
address: "recipient_address",
satoshis: 10000,
broadcastEnabled: false,
},
]);
// Broadcast later via your own node / APINext Steps
Now that you have the basics, explore the full API:
- connect — Connection management
- sendTransaction — TBC transactions (P2PKH, NFT, FT, PoolNFT, Stablecoin)
- evm.sendTransaction — Ethereum & BSC transactions
- btc.sendTransaction — Bitcoin transactions
- signMessage — Message signing & verification
- sendBatchRequest — Batch operations