> 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/solana-provider-api.md).

# Solana Provider API

TokenPocket Extension injects a Solana Provider into the page. Developers can integrate Solana wallet capabilities through the traditional Provider interface or Wallet Standard.

## Provider Injection

The following objects are available on the page:

```ts
window.solana
window.tokenpocket.solana
```

TokenPocket also supports Wallet Standard and can be discovered by standard wallet discovery flows.

{% hint style="info" %}
Wallet Standard is recommended for new integrations. Existing dApps built around `window.solana` can continue using the traditional Provider interface.
{% endhint %}

## Quick Start

### Connect Wallet

```ts
const provider = window.solana;

const res = await provider.connect();

console.log(res.address);
console.log(res.publicKey.toBase58());
console.log(res.accounts);
```

### Sign a Message

```ts
const message = new TextEncoder().encode('hello tokenpocket');
const res = await window.solana.signMessage(message);

console.log(res.signature);
console.log(res.publicKey.toBase58());
```

### Sign a Transaction

```ts
const signedTx = await window.solana.signTransaction(transaction);
```

## API List

### connect

Connects the current site to a Solana account.

```ts
connect(options?: { silent?: boolean }): Promise<{
  accounts: WalletAccount[];
  publicKey: PublicKey;
  address: string;
}>
```

Parameters:

* `silent`: Whether to connect silently. Default is `false`.

Notes:

* When `silent: true`, an account is returned only if the wallet is unlocked and the current site already has an authorized address.
* The call throws an error if no connection is available.

Example:

```ts
const { address, publicKey } = await window.solana.connect();
console.log(address);
console.log(publicKey.toBase58());
```

### disconnect

Disconnects the current session.

```ts
disconnect(): Promise<void>
```

Notes:

* Clears the current account state.
* Triggers the `disconnect` and `accountChanged` events.

Example:

```ts
await window.solana.disconnect();
```

### signTransaction

Signs a single transaction.

```ts
signTransaction(
  transaction: Transaction | VersionedTransaction
): Promise<Transaction | VersionedTransaction>
```

Notes:

* Supports `Transaction`.
* Also handles `VersionedTransaction` compatibility.
* dApps are recommended to use `legacy` transactions.
* Returns the signed transaction object.
* If the wallet returns `unitsConsumed` or `microLamports`, the Provider automatically appends Compute Budget instructions.

Example:

```ts
const signedTx = await window.solana.signTransaction(transaction);
```

### signAllTransactions

Signs multiple transactions.

```ts
signAllTransactions(
  transactions: Array<Transaction | VersionedTransaction>
): Promise<Array<Transaction | VersionedTransaction>>
```

Notes:

* The current implementation signs transactions one by one through `signTransaction`.

Example:

```ts
const signedTxs = await window.solana.signAllTransactions(transactions);
```

### signMessage

Signs a message.

```ts
signMessage(
  message: Uint8Array | { message: Uint8Array }
): Promise<{
  signature: Uint8Array;
  publicKey: PublicKey;
}>
```

Notes:

* Supports a raw `Uint8Array`.
* Also accepts a `{ message }` wrapper.

Example:

```ts
const message = new TextEncoder().encode('hello tokenpocket');
const res = await window.solana.signMessage(message);

console.log(res.signature);
console.log(res.publicKey.toBase58());
```

### signAndSendTransaction

Signs and sends a transaction.

```ts
signAndSendTransaction(params: {
  recentBlockhash?: string;
  feePayer?: string;
  instructions?: Array<{
    keys: Array<{
      pubkey: string;
      isSigner: boolean;
      isWritable: boolean;
    }>;
    programId: string;
    data: number[] | Uint8Array | Buffer;
  }>;
}): Promise<{
  signature: string;
  publicKey: string;
}>
```

Notes:

* Builds a transaction from the provided `instructions`.
* Returns `signature` as a Base58 string.
* Returns `publicKey` as the signing address.

Example:

```ts
const result = await window.solana.signAndSendTransaction({
  recentBlockhash,
  feePayer,
  instructions,
});

console.log(result.signature);
console.log(result.publicKey);
```

## Events

The Provider supports event subscriptions.

### connect

Triggered after a successful connection.

```ts
window.solana.on('connect', (publicKey) => {
  console.log(publicKey?.toBase58?.());
});
```

### disconnect

Triggered after disconnect.

```ts
window.solana.on('disconnect', () => {
  console.log('disconnected');
});
```

### accountChanged

Triggered when the active account changes.

```ts
window.solana.on('accountChanged', (publicKey) => {
  if (!publicKey) {
    console.log('account cleared');
    return;
  }

  console.log(publicKey.toBase58());
});
```

{% hint style="info" %}
When the user disconnects or the active account is cleared, `accountChanged` may receive `null`. Make sure your dApp handles that case.
{% endhint %}

## Wallet Standard

TokenPocket currently supports these Wallet Standard features:

* `standard:connect`
* `standard:events`
* `solana:signTransaction`
* `solana:signAndSendTransaction`
* `solana:signMessage`

Official references:

* [Wallet Standard docs](https://wallet-standard.github.io/wallet-standard/)
* [Wallet Standard extensions](https://github.com/wallet-standard/wallet-standard/blob/master/EXTENSIONS.md)
* [Solana Wallet Adapter ecosystem reference](https://github.com/solana-labs/wallet-adapter)

Supported chains:

```ts
[
  'solana:mainnet',
  'solana:devnet',
  'solana:testnet',
  'solana:localnet',
]
```

Notes:

* The currently declared `supportedTransactionVersions` value is `['legacy']`.
* dApps should integrate against `legacy` transaction support.

### Wallet Standard Example

```ts
const accounts = await wallet.features['standard:connect'].connect();

const outputs =
  await wallet.features['solana:signTransaction'].signTransaction({
    account: accounts.accounts[0],
    chain: 'solana:mainnet',
    transaction,
  });
```

## Error Handling

Use `try/catch` for all Provider calls.

```ts
try {
  await window.solana.connect();
} catch (error) {
  console.error(error);
}
```

Common failure cases:

* User rejects the connection request.
* User rejects a signature request.
* Wallet is locked.
* The current site is not authorized.
* Transaction or message format is invalid.

## Best Practices

* Prefer Wallet Standard for new integrations.
* Existing dApps can keep using `window.solana`.
* Treat transaction support as `legacy`.
* Handle exceptions for all connect and signing flows.
* Handle `null` values from `accountChanged`.

## Compatibility Notes

* TokenPocket exposes both `window.solana` and `window.tokenpocket.solana`.
* Wallet Standard is recommended for new integrations.
* Existing Solana Provider integrations can continue to work directly.
