> For the complete documentation index, see [llms.txt](https://help.tokenpocket.pro/developer-cn/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-cn/extension-wallet/api-reference/xrp-provider-api.md).

# XRP Provider API

TokenPocket Extension 会在页面中注入 XRP Provider，开发者可通过该 Provider 发起地址连接和 XRP 转账请求。

## Provider 注入

页面中可使用以下对象：

```ts
window.tokenpocket.xrp
```

{% hint style="info" %}
当前 XRP 接口较轻量，主要用于连接当前地址和发起 XRP 转账。
{% endhint %}

## 快速开始

### 获取当前地址

```ts
const wallet = await window.tokenpocket.xrp.connect();
console.log(wallet.address);
```

### 发起 XRP 转账

```ts
const res = await window.tokenpocket.xrp.sendXrp({
  from: 'r...',
  to: 'r...',
  amount: '1',
  destinationTag: '10001',
});

console.log(res.txid);
```

## API 列表

### connect

请求连接当前站点，并返回当前 XRP 地址。

```ts
connect(): Promise<{
  address: string;
}>
```

说明：

* 返回当前选中的 XRP 地址
* 若用户拒绝授权，调用会抛出错误

示例：

```ts
const { address } = await window.tokenpocket.xrp.connect();
console.log(address);
```

### sendXrp

发起 XRP 原生转账。

```ts
sendXrp(tx: {
  from: string;
  to: string;
  amount: string | number;
  destinationTag?: string | number;
}): Promise<{
  txid: string;
}>
```

参数：

* `from`：发送地址
* `to`：接收地址
* `amount`：转账数量
* `destinationTag`：可选的目标标签

返回值：

* `txid`：交易哈希

示例：

```ts
const result = await window.tokenpocket.xrp.sendXrp({
  from: 'rSenderAddress',
  to: 'rReceiverAddress',
  amount: '1',
  destinationTag: '10001',
});

console.log(result.txid);
```

### sendToken

当前版本暂不支持 XRP Token 转账。

```ts
sendToken(tx: object): Promise<never>
```

说明：

* 当前调用会直接返回错误：`Not supported!`

## 错误处理

建议所有 Provider 调用都使用 `try/catch`。

```ts
try {
  const wallet = await window.tokenpocket.xrp.connect();
  console.log(wallet.address);
} catch (error) {
  console.error(error);
}
```

常见失败场景：

* 用户拒绝连接
* 用户拒绝转账确认
* 参数格式不正确
* 当前钱包没有可用的 XRP 地址

## 兼容性说明

* 当前仅注入 `window.tokenpocket.xrp`
* 当前主要支持地址连接和 `sendXrp`
* `sendToken` 暂未开放
