@ aptos- labs/ wallet- 适应器- 核心
Aptos钱包适配器的核心功能. 这个软件包为钱包管理,状态处理,以及与Aptos块链上AIP-62相容钱包的互动提供了基础.
概览
钱包调整器-核心软件包处理器:
- 钱包检测: 自动检测安装在浏览器中的 AIP- 62 标准兼容钱包
- 连接管理: 连接、 断开和管理钱包会话
- 交易签名: 签署信件、交易并提交交易到网络
- 网络管理: 处理网络变化和多网络支持
- 事件系统: 订阅钱包、账户和网络更改事件
- 州管理层: 连接钱包、账户和网络信息的集中状态
安装
npm install @aptos-labs/wallet-adapter-core
# or
pnpm add @aptos-labs/wallet-adapter-core
# or
yarn add @aptos-labs/wallet-adapter-core
同伴依赖关系 :
npm install @aptos-labs/ts-sdk
使用量
基本设置
import { WalletCore } from "@aptos-labs/wallet-adapter-core";
import { Network } from "@aptos-labs/ts-sdk";
// Create wallet core instance
const walletCore = new WalletCore(
[], // Optional: array of wallet names to opt-in (empty = all wallets)
{ network: Network.MAINNET }, // Optional: dapp configuration
false, // Optional: disable telemetry (default: false)
["Petra Web"] // Optional: wallets to hide from display
);
// Get available wallets
const wallets = walletCore.wallets;
const notDetectedWallets = walletCore.notDetectedWallets;
连接到钱包
// Connect to a wallet by name
await walletCore.connect("Petra");
// Check connection status
if (walletCore.isConnected()) {
console.log("Connected account:", walletCore.account);
console.log("Connected network:", walletCore.network);
}
签名信件
const signedMessage = await walletCore.signMessage({
message: "Hello, Aptos!",
nonce: "random-nonce-123",
});
console.log("Signature:", signedMessage.signature);
console.log("Full message:", signedMessage.fullMessage);
签名和提交交易
const result = await walletCore.signAndSubmitTransaction({
data: {
function: "0x1::aptos_account::transfer",
typeArguments: [],
functionArguments: [recipientAddress, amount],
},
});
console.log("Transaction hash:", result.hash);
倾听事件
// Account change event
walletCore.on("accountChange", (account) => {
console.log("Account changed:", account);
});
// Network change event
walletCore.on("networkChange", (network) => {
console.log("Network changed:", network);
});
// Disconnect event
walletCore.on("disconnect", () => {
console.log("Wallet disconnected");
});
断开连接
await walletCore.disconnect();
API 参考
钱包库
管理钱包状态和交互的主要类 .
构造器
new WalletCore(
optInWallets?: ReadonlyArray<AvailableWallets>,
dappConfig?: DappConfig,
disableTelemetry?: boolean,
hideWallets?: ReadonlyArray<AvailableWallets>
)
属性
| 财产 | 类型 | 说明 |
|---|---|---|
wallet | AptosWallet | null | 目前连接的钱包 |
account | AccountInfo | null | 目前连接的账户 |
network | NetworkInfo | null | 目前连接的网络 |
wallets | ReadonlyArray<AptosWallet> | 检测到的钱包列表 |
hiddenWallets | ReadonlyArray<AdapterWallet> | 隐藏钱包列表 |
notDetectedWallets | ReadonlyArray<AdapterNotDetectedWallet> | 未发现钱包列表 |
方法
| 方法 | 说明 |
|---|---|
connect(walletName) | 连接到钱包 |
disconnect() | 从当前钱包断开连接 |
signMessage(input) | 签名信件 |
signTransaction(args) | 签名交易 |
signAndSubmitTransaction(input) | 签名并提交交易 |
changeNetwork(network) | 请求更改网络 |
signIn(args) | 与 SIWA (用相片签名) 签名 |
isConnected() | 检查钱包是否连接 |
活动
| 活动 | 有效载荷 | 说明 |
|---|---|---|
connect | AccountInfo | 钱包连接时退出 |
disconnect | - 说吧 | 钱包断开时退出 |
accountChange | AccountInfo | 账户变动时汇出 |
networkChange | NetworkInfo | 网络改变时发送 |
达普配置
钱包适配器的配置选项 :
interface DappConfig {
network: Network;
transactionSubmitter?: TransactionSubmitter;
aptosApiKeys?: Partial<Record<Network, string>>;
aptosConnectDappId?: string;
aptosConnect?: AptosConnectWalletConfig;
crossChainWallets?: boolean;
}
相关软件包
@aptos-labs/wallet-adapter-react- 反应钩和上下文提供者@aptos-labs/ts-sdk- Aptos 类型脚本 SDK
许可证
阿帕奇 - 2.0