# Bitcoin Provider

After setting up your wallet, you can have access to the Bitcoin provider:

```javascript
// react
const { providers } = useTomo()
const { bitcoinProvider } = providers;

// pure js
const bitcoinProvider = window.tomo_bitcoin
```

### Get Address and Balance

You can first check and change the address type you want to use

```tsx
// interface
getAddressType(): string
changeAddressType(addressType: string): Promise<void>

export enum AddressType {
  P2PKH,
  P2WPKH,
  P2TR,
  P2SH_P2WPKH,
}

// react
const btcAddressType= await bitcoinProvider.getAddressType()
await bitcoinProvider.changeAddressType("P2SH_P2WPKH")

// pure js
const btcAddress = await window.tomo_bitcoin.getAddressType()
await window.tomo_bitcoin.changeAddressType("P2PKH")
```

Then, get the address. However, if the user uses extension wallet, not all address type may have value.

<pre class="language-tsx"><code class="lang-tsx">// interface
// get current type address, by default it is taproot
getAddress(): Promise&#x3C;string>
getAllAddresses(): Promise&#x3C;AddressInfo[]>
getBalance(address?: string): Promise&#x3C;number>

interface AddressInfo {
  value: AddressType;
  label: string;
  name: string;
  displayName?: string;
  hdPath: string;
  address?: string;
}

// form wallet state
const { walletState, providers } = useTomo()
const { btcAddress } = walletState

// or 
const { bitcoinProvider } = providers;
<strong>const btcAddress = await bitcoinProvider.getAddress()
</strong>const allAddresses= await bitcoinProvider.getAllAddresses()
const balance= await bitcoinProvider.getBalance(btcAddress)

// pure js
const btcAddress = await window.tomo_bitcoin.getAddress()
const allAddresses= await window.tomo_bitcoin.getAllAddresses()
const balance= await window.tomo_bitcoin.getBalance(btcAddress)
</code></pre>

### Get the public key

In addition to the address, you can get the concrete public key from the provider.

```tsx
//interface 
getPublicKey(): Promise<string>

//react
const publicKey = await bitcoinProvider.getPublicKey()

// pure js

const publicKey = await window.tomo_bitcoin.getPublicKey()
```

### Switch Network

We support three networks for different purposes:

```tsx
// interface
getNetwork()： string
switchNetwork(network: Network): Promise<void>
const network = 'signet' | 'testnet' | 'mainnet'

// react
const current = await bitcoinProvider.getNetwork()
const sendRes = await bitcoinProvider.switchNetwork(network)

// pure js
const current = await window.tomo_bitcoin.getNetwork()
const sendRes = await window.tomo_bitcoin.switchNetwork(network)
```

## Provider Functions

`providers.bitcoinProvider` exposes three functions for sending requests to the user's wallet:

### **Signing a Message**

It uses the `bitcoinprovider` to sign a plaintext offchain message, which returns the message's signature.

{% code overflow="wrap" %}

```tsx
// interface
signMessage(message: string, _type: "ecdsa" | "bip322-simple" = "ecdsa"): Promise<string>

const message = 'your message'
const type: "ecdsa" | "bip322-simple" = 'ecdsa'   // default is ecdsa
// react
const signedMessage = await bitcoinProvider.signMessage(message, type)

// pure js
const signedMessage = window.tomo_bitcoin.signMessage(message, type)
```

{% endcode %}

Currently, the `bip32-simple` type does not support taproot addresses. Please select other address types when you need to sign a message with `bip322-simple`.

### **Sign Psbt**

Given a hex raw transaction  you can use `signPsbt`method to get the signed transaction in hex value if the user approves the request.

{% code overflow="wrap" %}

```tsx
// interface
signPsbt(psbtHex: string): Promise<string>
signPsbts(psbtHexs: string[]): Promise<string[]>


const psbtHex = 'hex String'

// react
const signed = await bitcoinProvider.signPsbt(psbtHex)

// pure js
const signed = await window.tomo_bitcoin.signPsbt(psbtHex)
```

{% endcode %}

### **Send Bitcoin**

You can also send the transaction directly through this `sendBitcoin` method. The return value is the transaction signature if signed and sent correctly.

{% code overflow="wrap" %}

```tsx
const toAddress = 'toAddress'
const satAmount = 1 // BTC
const feeRate = 123 // sat/vB


// react
const sendRes = await bitcoinProvider.sendBitcoin(toAddress, satAmount, feeRate)

// pure js
const sendRes = await  window.tomo_bitcoin.sendBitcoin(toAddress, satAmount, feeRate)
```

{% endcode %}
