> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Privy 集成

> 使用 LangChain Python 与 Privy 工具集成。

[Privy](https://privy.io) 是为 AI 代理打造的强大钱包基础设施，专为规模化而生。

## 概述

创建能够执行以下操作的代理：

* 自动创建和管理钱包
* 使用多种数字资产（包括稳定币）进行支付
* 签署消息和交易
* 查询钱包余额和地址

### 工作原理

Privy 提供的钱包基础设施消除了区块链交互的复杂性：

1. 为你的代理提供一个强大的钱包
2. 通过交易策略保护代理的资产
3. 快速完成支付

**零摩擦入门**
与传统钱包解决方案不同，Privy 会自动为你的代理创建嵌入式钱包，无需管理私钥、助记词或进行复杂设置。

**生产就绪的基础设施**
Privy 受到领先 Web3 应用的信赖，能够大规模处理安全的密钥生成、多链地址派生、交易签名和合规策略。

### 快速开始

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os
from langchain_privy import PrivyWalletTool
from langchain.agents import create_agent

# 设置凭证
os.environ["PRIVY_APP_ID"] = "your-privy-app-id"
os.environ["PRIVY_APP_SECRET"] = "your-privy-app-secret"

# 初始化钱包工具（自动创建钱包）
privy_tool = PrivyWalletTool()
print(f"钱包已创建！地址：{privy_tool.wallet_address}")

# 创建代理
agent = create_agent(
    model="claude-sonnet-4-6",
    tools=[privy_tool],
)

# 代理现在可以执行钱包操作
agent.invoke({"messages": [{"role": "user", "content": "我在 Base 上的钱包地址是什么？"}]})
```

请参阅完整的[示例](https://github.com/privy-io/langchain-privy/tree/main/examples)以获取完整实现。

## 设置

前往 [Privy 控制面板](https://dashboard.privy.io) 注册并创建一个新应用。你将获得：

* **App ID** - 你的应用标识符
* **App Secret** - 你的服务器端认证密钥

1. 安装包：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install langchain-privy
```

2. 设置你的凭证：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os
import getpass

os.environ["PRIVY_APP_ID"] = getpass.getpass("输入你的 Privy App ID：")
os.environ["PRIVY_APP_SECRET"] = getpass.getpass("输入你的 Privy App Secret：")
```

## 实例化

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_privy import PrivyWalletTool

# 自动创建一个新的以太坊钱包
tool = PrivyWalletTool()

# 或在特定链上创建
base_tool = PrivyWalletTool(chain_type="base")
solana_tool = PrivyWalletTool(chain_type="solana")

# 或重用现有钱包
existing_tool = PrivyWalletTool(wallet_id="wal_abc123...")
```

## 调用

### 可用操作

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 获取任何链的钱包地址
tool.invoke({
    "operation": "get_wallet_address",
    "chain": "base"
})

# 签署消息
tool.invoke({
    "operation": "sign_message",
    "message": "Hello from LangChain!",
    "chain": "ethereum"
})

# 检查余额
tool.invoke({
    "operation": "get_balance",
    "chain": "base"
})

# 发送交易
tool.invoke({
    "operation": "send_transaction",
    "chain": "base",
    "to": "0x1234567890123456789012345678901234567890",
    "value": "0.001",
    "unit": "ether"
})
```

## 在代理中使用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os
from langchain_privy import PrivyWalletTool
from langchain.agents import create_agent

# 设置凭证
os.environ["PRIVY_APP_ID"] = "your-privy-app-id"
os.environ["PRIVY_APP_SECRET"] = "your-privy-app-secret"

# 初始化工具
tools = [PrivyWalletTool()]

# 创建代理
agent = create_agent(
    model="claude-sonnet-4-6",
    tools=tools,
)

# 自然语言钱包操作
agent.invoke({
    "messages": [{"role": "user", "content": "签署消息 'Verified by AI Agent'，然后检查我在 Base 上的余额"}]
})
```

***

## API 参考

* 完整文档，请参阅 [langchain-privy GitHub 仓库](https://github.com/privy-io/langchain-privy)
* 有关 Privy 的更多信息，请参阅 [Privy 文档](https://docs.privy.io)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs) 通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/privy.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
