> For the complete documentation index, see [llms.txt](https://help.tokenpocket.pro/developer-en/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.tokenpocket.pro/developer-en/extension-wallet/api-reference/bitcoin-provider-api.md).

# Bitcoin Provider API

TokenPocket Extension injects a Bitcoin Provider into the page and is compatible with common UniSat-style APIs. Developers can use familiar Bitcoin dApp methods for account access, message signing, PSBT signing, and broadcasting.

## Provider Injection

The following objects are available on the page:

```ts
window.unisat
window.tokenpocket.bitcoin
```

Notes:

* `window.unisat`: UniSat-compatible entry point
* `window.tokenpocket.bitcoin`: Native TokenPocket Bitcoin Provider

After initialization, the page dispatches:

```ts
window.dispatchEvent(new Event('unisat#initialized'));
```

## Quick Start

### Request Accounts

```ts
const accounts = await window.unisat.requestAccounts();
console.log(accounts[0]);
```

### Get Public Key

```ts
const publicKey = await window.unisat.getPublicKey();
console.log(publicKey);
```

### Sign a Message

```ts
const signature = await window.unisat.signMessage('hello tokenpocket');
console.log(signature);
```

## API List

### requestAccounts

Requests connection for the current site and returns the BTC address list.

```ts
requestAccounts(): Promise<string[]>
```

Example:

```ts
const accounts = await window.unisat.requestAccounts();
console.log(accounts[0]);
```

### getAccounts

Returns the BTC address list already authorized for the current site.

```ts
getAccounts(): Promise<string[]>
```

### getPublicKey

Returns the public key for the active BTC address.

```ts
getPublicKey(): Promise<string>
```

### getBalance

Returns BTC balance information.

```ts
getBalance(): Promise<any>
```

Notes:

* The exact response shape follows the wallet implementation.

### getNetwork

Returns the current network identifier.

```ts
getNetwork(): Promise<string>
```

Current version only supports:

```ts
'livenet'
```

### getChain

Returns the current chain information.

```ts
getChain(): Promise<{
  enum: string;
  name: string;
  network: string;
}>
```

Current version returns mainnet information:

```ts
{
  enum: 'BITCOIN_MAINNET',
  name: 'Bitcoin Mainnet',
  network: 'livenet',
}
```

### getVersion

Returns the Provider version.

```ts
getVersion(): Promise<string>
```

### switchNetwork

Switches network.

```ts
switchNetwork(network?: string): Promise<void>
```

Notes:

* Current version only allows mainnet.

### switchChain

Switches chain configuration.

```ts
switchChain(chain?: string): Promise<{
  enum: string;
  name: string;
  network: string;
}>
```

Notes:

* Current version only allows mainnet.

### signMessage

Signs a message.

```ts
signMessage(
  message: string,
  type?: 'ecdsa' | 'bip322-simple'
): Promise<string>
```

Example:

```ts
const signature = await window.unisat.signMessage(
  'hello tokenpocket',
  'ecdsa'
);
```

### signPsbt

Signs a single PSBT.

```ts
signPsbt(psbtHex: string, options?: Record<string, any>): Promise<string>
```

### signPsbts

Signs multiple PSBTs.

```ts
signPsbts(
  psbtHexs: string[],
  options?: Record<string, any>[] | Record<string, any>
): Promise<string[]>
```

### pushPsbt

Finalizes and broadcasts a PSBT.

```ts
pushPsbt(psbtHex: string): Promise<string>
```

### pushTx

Broadcasts a raw transaction.

```ts
pushTx(rawtx: string | { rawtx?: string }): Promise<string>
```

### sendBitcoin

Sends BTC.

```ts
sendBitcoin(
  toAddress: string,
  satoshis: number,
  options?: Record<string, any>
): Promise<string>
```

Example:

```ts
const txid = await window.unisat.sendBitcoin(
  'bc1...',
  1000
);
```

## Events

The Bitcoin Provider supports event listeners.

### accountsChanged

Triggered when the active address changes.

```ts
window.unisat.on('accountsChanged', (accounts) => {
  console.log(accounts);
});
```

### networkChanged

Triggered when the network changes.

```ts
window.unisat.on('networkChanged', (network) => {
  console.log(network);
});
```

### Event Methods

Supported listener methods:

* `on`
* `once`
* `off`
* `addListener`
* `removeListener`
* `removeAllListeners`

## Error Handling

Use `try/catch` for all Provider calls.

```ts
try {
  const accounts = await window.unisat.requestAccounts();
  console.log(accounts[0]);
} catch (error) {
  console.error(error);
}
```

Common failure cases:

* User rejects the connection request.
* User rejects signing or transaction confirmation.
* The current site is not authorized.
* No BTC address is available.
* The requested network is not supported.

## Compatibility Notes

* TokenPocket exposes both `window.unisat` and `window.tokenpocket.bitcoin`.
* The API shape is designed to be compatible with common UniSat integrations.
* The current version only supports Bitcoin Mainnet.
