> ## Documentation Index
> Fetch the complete documentation index at: https://docs.li.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Bitcoin 提供方

> Bitcoin Architecture

LI.FI 在原生 Bitcoin、主流 EVM 链和 Solana 之间提供无缝的原生 Bitcoin 桥接与交换。

## 请求报价

Bitcoin 的报价可以使用与 EVM 相同的端点进行请求。唯一的区别在于当源链为 Bitcoin 时的交易数据。

> [!NOTE]
>
> 当源链为 Bitcoin 时，`fromAddress` 支持灵活的格式以实现最大程度的便利：
>
> * **单个 Bitcoin 地址**：`bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh`
> * **多个地址**（以分号分隔）：`address1;address2;address3`
> * **扩展公钥（xpub）**：`xpub6CUG...`
> * **多个 xpub**：`xpub1,xpub2`
> * **xpub 与地址的组合**：`xpub1;address1;address2`
>
> **UTXO 收集**：系统通过从以下来源收集 UTXO 来构建交易输入：
>
> * 直接从提供的地址，或
> * 从提供的 xpub 派生出且包含 UTXO 的地址
>
> `fromAddress` 必须拥有足够的 UTXO 来覆盖所请求的交易金额，否则不会返回任何报价。系统会以最高效的方式检查并组合 UTXO 来构建交易。
>
> **交易签名**：所有为交易贡献 UTXO 的输入地址（钱包）都必须对 PSBT 进行签名。这确保了来自每个参与地址的资金都得到了正确授权。
>
> **退款地址**：PSBT 中的第三个输出是退款/找零输出，将返回到为该交易贡献了最重要输入的地址。

## 执行交易

### 交易数据

在获取报价后，需要将资金连同一条 memo 一起发送到响应中提供的 BTC 保管库地址。

* Memo 功能：与 Thorchain 类似，LI.FI 在 BTC 到 EVM 的交换中使用 memo。BTC 交易中的 memo 指定了交换的目标地址和链。

* 交易处理：离开 BTC 前往 EVM 的交易需要发送到一个 EVM 地址。memo 确保交换详情被验证者正确处理。

> [!NOTE]
>
> 注意：请务必及时发送交易（约 30 分钟内）。始终建议请求最新的报价，以确保获取最新的
> 信息。

> [!WARNING]
>
> **修改 Bitcoin 交易数据的风险**
>
> 修改从我们的 API 收到的 PSBT 或原始 Bitcoin 交易数据（例如
> 移除输出、更改金额，或编辑 opcode/脚本）可能会
> 使签名或花费条件失效，并导致资金不可逆转的损失。
>
> 除非你是专家并且已明确与我们确认你打算进行的
> 修改，否则请不要更改 PSBT。

> [!NOTE]
>
> transactionRequest 对象中的 `data` 是 PSBT（部分签名的 Bitcoin
> 交易），memo 需要通过解码 PSBT 来获取。

### 从 PSBT 中获取 memo

PSBT 可以使用任何库进行解码，例如 [bitcoinjs](https://github.com/bitcoinjs/bitcoinjs-lib) 或 [scure-btc-signer](https://github.com/paulmillr/scure-btc-signer)。

以下是使用 `bitcoinjs` 的示例

```typescript theme={"system"}
const psbtHex = transactionRequest.data;

// Create PSBT object from hex data
const psbt = Psbt.fromHex(psbtHex, { network: networks.bitcoin });

// Find OP_RETURN output in the transaction outputs
const opReturnOutput = psbt.txOutputs.find((output) => {
  if (output?.script) {
    // Convert the output script to hex string for checking
    const scriptHex = Array.from(output.script)
      .map((b) => b.toString(16).padStart(2, "0"))
      .join("");

    // Check if script starts with OP_RETURN opcode (0x6a)
    return scriptHex.startsWith("6a");
  }
  return false;
});

// If an OP_RETURN output exists, decode its script data as UTF-8 text (memo)
const memo = opReturnOutput?.script
  ? new TextDecoder().decode(
      new Uint8Array(Object.values(opReturnOutput.script))
    )
  : undefined;
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.