> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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 会自动为您的代理创建嵌入式钱包，无需管理私钥、助记词或进行复杂设置。

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

### 快速开始

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

# Set credentials
os.environ["PRIVY_APP_ID"] = "your-privy-app-id"
os.environ["PRIVY_APP_SECRET"] = "your-privy-app-secret"

# Initialize wallet tool (automatically creates wallet)
privy_tool = PrivyWalletTool()
print(f"Wallet created! Address: {privy_tool.wallet_address}")

# Create agent
agent = create_agent(
    model="claude-sonnet-4-6",
    tools=[privy_tool],
)

# Agent can now perform wallet operations
agent.invoke({"messages": [{"role": "user", "content": "What's my wallet address on Base?"}]})
```

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

## 设置

前往 [Privy 仪表板](https://dashboard.privy.io) 注册并创建新应用。您将收到：

* **应用 ID** - 您的应用程序标识符
* **应用密钥** - 您的服务器端认证密钥

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("Enter your Privy App ID: ")
os.environ["PRIVY_APP_SECRET"] = getpass.getpass("Enter your Privy App Secret: ")
```

## 实例化

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

# Automatically creates a new Ethereum wallet
tool = PrivyWalletTool()

# Or create on a specific chain
base_tool = PrivyWalletTool(chain_type="base")
solana_tool = PrivyWalletTool(chain_type="solana")

# Or reuse an existing wallet
existing_tool = PrivyWalletTool(wallet_id="wal_abc123...")
```

## 调用

### 可用操作

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# Get wallet address for any chain
tool.invoke({
    "operation": "get_wallet_address",
    "chain": "base"
})

# Sign a message
tool.invoke({
    "operation": "sign_message",
    "message": "Hello from LangChain!",
    "chain": "ethereum"
})

# Check balance
tool.invoke({
    "operation": "get_balance",
    "chain": "base"
})

# Send transaction
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

# Set credentials
os.environ["PRIVY_APP_ID"] = "your-privy-app-id"
os.environ["PRIVY_APP_SECRET"] = "your-privy-app-secret"

# Initialize tools
tools = [PrivyWalletTool()]

# Create agent
agent = create_agent(
    model="claude-sonnet-4-6",
    tools=tools,
)

# Natural language wallet operations
agent.invoke({
    "messages": [{"role": "user", "content": "Sign the message 'Verified by AI Agent' and then check my balance on Base"}]
})
```

***

## API 参考

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

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\tools\privy.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
