Tomo Docs
Tomo Docs
  • Overview
    • Introducing Tomo
    • Tomo's Key Management
  • Tomo SDK
    • TomoEVMKit
      • Quick Start
      • Use with Ethers.js
      • Migration from RainbowKit
      • Migration from Blocknative
      • Supported Chains
    • Tomo Web SDK
      • Quick Start
      • Solana Provider
      • EVM Provider
      • Bitcoin Provider
      • Tron Provider
      • Movement Provider
      • Sui Provider
    • Tomo Telegram SDK
      • Quick Start
      • Wallet Provider
        • EVM Provider
        • Solana Provider
        • Sui Provider (Beta)
        • TON Provider
      • Partners
    • Tomo Enterprise SDK
      • For Babylon
        • Install the SDK
        • Tomo Wallet Provider
        • Bitcoin Provider
        • Cosmos Provider
        • Multiple Connection Mode
        • Integrate Extension Wallet
          • Submit Wallet PR
          • Extend the SDK
          • Q & A
        • Integrate Mobile Wallet
  • TOMO WALLET
    • Tomo Wallets
    • Mobile Wallet
      • Tomo Keys
        • Bonding Curve Explained
        • How to: Tomo Keys
      • TomoID
        • How to: TomoID
        • How to: Connect Instagram
      • Tomo Socials
      • Tomo Android App
      • Tomo iOS App
    • Extension Wallet
      • Developer Manual
        • EVM Integration
        • Bitcoin Integration
      • Example of User Flows
        • Claiming Signet BTC
        • Staking at Testnet
      • Install Link
    • Telegram Wallet
      • Quick Start
      • Chains/Networks
      • User Manual
        • Account Security
        • Gift feature
        • FAQ
        • Transaction
        • Swap
  • ABOUT US
    • Brand Assets
    • Privacy Policy
Powered by GitBook
On this page
  • Address Type
  • Message Signing
  • PSBT Signing
  1. TOMO WALLET
  2. Extension Wallet
  3. Developer Manual

Bitcoin Integration

PreviousEVM IntegrationNextExample of User Flows

Last updated 4 months ago

Tomo Extension Wallet injects tomo_btc into the window object in your browser. dApp can easily interact with the wallet via window.tomo_btc. The following demo code shows how it works:

const example = async () => {
    // connect to tomo wallet and get accounts
    const accounts = await window.tomo_btc.requestAccounts()
    
    // the first account is the selected account
    const account = accounts[0]
        
    const signature = await window.tomo_btc.signMessage('Hello Tomo', 'bip322-simple')
    // do something about the signature
}

Tomo extension wallet follows the defined by chain. Any Bitcoin ecosystem dApps can use the following Tomo implementation.

import { WalletProvider, Network, Fees, UTXO, WalletInfo } from "./wallet_provider";
import {
  getAddressBalance,
  getTipHeight,
  getFundingUTXOs,
  getNetworkFees,
  pushTx
} from "../mempool_api";

export class TOMOWallet extends WalletProvider {
  private tomoWalletInfo: WalletInfo | undefined;

  constructor() {
    super();
  }

  connectWallet = async (): Promise<this> => {
    const workingVersion = "1.1.1";
    if (!window.tomo_btc) {
      throw new Error("Tomo extension wallet not found");
    }

    const tomowallet = window.tomo_btc;
    let result = null;
    try {
      result = await tomowallet?.requestAccounts();
    } catch (error) {
      throw new Error("Bitcoin is not enabled in Tomo wallet");
    }

    let compressedPublicKey = null;
    try {
      compressedPublicKey = await tomowallet?.getPublicKey();
    } catch (error) {
      throw new Error("Bitcoin is not enabled in Tomo wallet");
    }

    if (compressedPublicKey && result[0]) {
      this.tomoWalletInfo = {
        publicKeyHex: compressedPublicKey,
        address: result[0],
      };
      return this;
    } else {
      throw new Error("Failed to connect to Tomo wallet");
    }
  };

  getWalletProviderName = async (): Promise<string> => {
    return "Tomo";
  };

  getAddress = async (): Promise<string> => {
    if (!this.tomoWalletInfo) {
      throw new Error("Tomo wallet not connected");
    }
    return this.tomoWalletInfo.address;
  };

  getPublicKeyHex = async (): Promise<string> => {
    if (!this.tomoWalletInfo) {
      throw new Error("Tomo wallet not connected");
    }
    return this.tomoWalletInfo.publicKeyHex;
  };

  signPsbt = async (psbtHex: string): Promise<string> => {
    if (!this.tomoWalletInfo) {
      throw new Error("Tomo wallet not connected");
    }
    return await window?.tomo_btc?.signPsbt(psbtHex);
  };

  signPsbts = async (psbtsHexes: string[]): Promise<string[]> => {
    if (!this.tomoWalletInfo) {
      throw new Error("Tomo wallet not connected");
    }
    return await window?.tomo_btc?.signPsbts(psbtsHexes);
  };

  signMessageBIP322 = async (message: string): Promise<string> => {
    if (!this.tomoWalletInfo) {
      throw new Error("TOMO Wallet not connected");
    }
    return await window?.tomo_btc?.signMessage(
      message,
      "bip322-simple",
    );
  };

  getNetwork = async (): Promise<Network> => {
    return "testnet";
  };

  on = (eventName: string, callBack: () => void) => {
    if (!this.tomoWalletInfo) {
      throw new Error("TOMO Wallet not connected");
    }
    if (eventName === "accountChanged") {
      return window.tomo_btc.on(eventName, callBack);
    }
  };

  getBalance = async (): Promise<number> => {
    return await getAddressBalance(await this.getAddress());
  };

  getNetworkFees = async (): Promise<Fees> => {
    return await getNetworkFees();
  };

  pushTx = async (txHex: string): Promise<string> => {
    return await pushTx(txHex);
  };

  getUtxos = async (address: string, amount: number): Promise<UTXO[]> => {
    return await getFundingUTXOs(address, amount);
  };

  getBTCTipHeight = async (): Promise<number> => {
    return await getTipHeight();
  };
}

Address Type

Tomo extension wallet supports the following Bitcoin wallet type:

  • Legacy (P2PKH)

  • Nested SegWit (P2SH)

  • Native SegWit (P2WPKH)

  • Taproot (P2TR)

Message Signing

Tomo extension wallet support the ECDSA signature of the above four wallet types.

PSBT Signing

Tomo extension wallet supports the PSBT signing of the following wallet types:

  • Nested SegWit (P2SH)

  • Native SegWit (P2WPKH)

  • Taproot (P2TR)

wallet adaptor interface
Babylon