Skip to content

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

sh
npm install turing-wallet-provider
sh
pnpm add turing-wallet-provider
sh
yarn add turing-wallet-provider

Two Ways to Use the SDK

The SDK provides two access patterns. Both are fully equivalent in functionality.

Best for React and component-based frameworks. Returns a wallet instance scoped to the current context.

ts
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.

ts
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:

ts
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:

ChainMethodsAddress Format
TBC (Turing BitChain)sendTransaction, signTransaction, signAssociatedTransaction, sendBatchRequesttbcAddress
BTC (Bitcoin)btc.sendTransaction, btc.signTransaction, btc.sendBatchRequestbtcAddress
Ethereumevm.sendTransactionethAddress
BSCevm.sendTransactionbnbAddress

Error Handling

Most API methods return an error field on failure rather than throwing exceptions. Always check for errors:

ts
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:

ts
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, returning txid.
  • false — The wallet only signs, returning txraw (the raw signed transaction hex). You broadcast it yourself.
ts
// 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 / API

Next Steps

Now that you have the basics, explore the full API: