# Bitcoin Integration

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:

```javascript
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 [wallet adaptor interface](https://github.com/babylonchain/simple-staking/blob/main/src/utils/wallet/wallet_provider.ts) defined by [Babylon](https://babylonchain.io/) chain. Any Bitcoin ecosystem dApps can use the following Tomo implementation.

```javascript
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)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tomo.inc/tomo-wallet/extension-wallet/developer-manual/bitcoin-integration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
