跨链账户
得益于 AIP-113 可派生账户抽象,可在 Aptos 网络上灵活、安全地管理跨链签名。任何在 Aptos 链上实现认证函数的钱包均可向 Aptos 网络提交交易。这为 dApp 带来多种用例,并改善用户体验和入门流程。
用户进入支持跨链账户的 dApp 时,体验与普通 Aptos 钱包相同。用户连接跨链账户(例如 Solana 的 Phantom),可查看可派生抽象账户(DAA)详细信息、签名消息并向 Aptos 链提交交易。
dApp 使用跨链账户提交交易时,钱包适配器会使用跨链账户标准定义的 signIn 函数进行域验证和安全保护。若钱包不支持 signIn,适配器会回退使用默认 signMessage。钱包批准签名后,交易提交到 Aptos 链并进行签名验证。
DAA 在跨链账户中如何运作?
Section titled “DAA 在跨链账户中如何运作?”跨链账户适配器会计算用户的 DAA 地址,并将跨链账户转换为遵循 Aptos 钱包标准的接口,使开发者和最终用户均可无缝交互。
DAA 地址使用钱包适配器中定义的 authenticationFunction 和 accountIdentity 计算:
authenticationFunction:链上用于验证跨链账户签名的函数。accountIdentity:链上认证函数用于验证签名的账户身份。它基于原跨链账户公钥和 dApp 域名(如 mydomain.com),格式为:${originWalletAddress}${domain}。
如何在 dApp 中集成跨链账户?
Section titled “如何在 dApp 中集成跨链账户?”钱包适配器遵循 Solana Wallet Standard 来发现钱包。已测试支持跨链账户的钱包:
| Aptos Devnet | Aptos Testnet | Aptos Mainnet | |
|---|---|---|---|
| Phantom | ✅ | ✅ | ✅ |
| Solflare | ✅ | ✅ | ✅ |
| Backpack | ✅ | ✅ | ✅ |
| OKX | ✅ | ✅ | ✅ |
跨链账户集成分三步:
-
安装 @aptos-labs/derived-wallet-solana 包
Terminal window npm install @aptos-labs/derived-wallet-solana -
导入 setupAutomaticSolanaWalletDerivation 函数
安装后,在导入其他钱包的同一文件(如
WalletProvider.tsx)中添加:import { setupAutomaticSolanaWalletDerivation } from "@aptos-labs/derived-wallet-solana";setupAutomaticSolanaWalletDerivation({ defaultNetwork: Network.TESTNET }); // this is the Aptos network your dapp is working with...<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,}}>{children}</AptosWalletAdapterProvider> -
将 AptosWalletAdapterProvider 的 crossChainWallets dapp 配置属性设为 true
<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,crossChainWallets: true,}}>{children}</AptosWalletAdapterProvider>
钱包适配器遵循 EIP-1193 来发现钱包。已测试支持跨链账户的钱包:
| Aptos Devnet | Aptos Testnet | Aptos Mainnet | |
|---|---|---|---|
| Metamask | ✅ | ✅ | ✅ |
| Phantom | ✅ | ✅ | ✅ |
| Coinbase | ✅ | ✅ | ✅ |
| OKX | ✅ | ✅ | ✅ |
| Exodus | ✅ | ✅ | ✅ |
| Backpack | ✅ | ✅ | ✅ |
-
安装 @aptos-labs/derived-wallet-ethereum 包
Terminal window npm install @aptos-labs/derived-wallet-ethereum -
导入 setupAutomaticEthereumWalletDerivation 函数,并在
WalletProvider.tsx等文件中使用:import { setupAutomaticEthereumWalletDerivation } from "@aptos-labs/derived-wallet-ethereum";setupAutomaticEthereumWalletDerivation({ defaultNetwork: Network.TESTNET }); // this is the Aptos network your dapp is working with...<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,}}>{children}</AptosWalletAdapterProvider> -
将 crossChainWallets dapp 配置属性设为 true:
<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,crossChainWallets: true,}}>{children}</AptosWalletAdapterProvider>
钱包适配器遵循 Sui Wallet Standard 来发现钱包。已测试支持跨链账户的钱包:
| Aptos Devnet | Aptos Testnet | Aptos Mainnet | |
|---|---|---|---|
| Backpack | ✅ | ✅ | ✅ |
| Bitget | ✅ | ✅ | ✅ |
| Ethos | ✅ | ✅ | ✅ |
| Nightly | ✅ | ✅ | ✅ |
| OKX | ✅ | ✅ | ✅ |
| Phantom | ✅ | ✅ | ✅ |
| Slush | ✅ | ✅ | ✅ |
| Trust Wallet | ✅ | ✅ | ✅ |
-
安装 @aptos-labs/derived-wallet-sui 包
Terminal window npm install @aptos-labs/derived-wallet-sui -
导入 setupAutomaticSuiWalletDerivation 函数,并在
WalletProvider.tsx等文件中使用:import { setupAutomaticSuiWalletDerivation } from "@aptos-labs/derived-wallet-sui";setupAutomaticSuiWalletDerivation({ defaultNetwork: Network.TESTNET }); // this is the Aptos network your dapp is working with...<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,}}>{children}</AptosWalletAdapterProvider> -
将 crossChainWallets dapp 配置属性设为 true:
<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,crossChainWallets: true,}}>{children}</AptosWalletAdapterProvider>
这些步骤会处理将跨链账户作为 Aptos 钱包纳入所需的逻辑和实现。
大多数情况下,允许用户通过跨链账户向 Aptos 链提交交易需要使用赞助交易,因为跨链账户可能没有支付 Gas 的 APT。因此 dApp 应考虑维护赞助账户。
import React from 'react';import { useWallet } from '@aptos-labs/wallet-adapter-react';import { Aptos, AptosConfig, Network, Ed25519PrivateKey, PrivateKey, PrivateKeyVariants, Account } from '@aptos-labs/ts-sdk';
// Initialize an Aptos clientconst config = new AptosConfig({ network: Network.TESTNET });const aptos = new Aptos(config);
// Generate a sponsor account or use an existing accountconst privateKey = new Ed25519PrivateKey( PrivateKey.formatPrivateKey( "0x123", PrivateKeyVariants.Ed25519 ));const sponsor = Account.fromPrivateKey({ privateKey });
const SignAndSubmit = () => { const { account, signTransaction } = useWallet();
const onSignAndSubmitTransaction = async () => { if(!account) { throw new Error("Account is not connected and unable to sign transaction") }
try { // Build the transaction const rawTransaction = await aptos.transaction.build.simple({ data: { function: "0x1::aptos_account::transfer", functionArguments: [account.address.toString(), 1], }, sender: account.address, withFeePayer: true, });
// Send it to the wallet to sign const walletSignedTransaction = await signTransaction({ transactionOrPayload: rawTransaction, });
// Sponsor account signs the transaction to pay for the gas fees const sponsorAuthenticator = aptos.transaction.signAsFeePayer({ signer: sponsor, transaction: rawTransaction, });
// Submit the transaction to chain const txnSubmitted = await aptosClient(network).transaction.submit.simple( { transaction: rawTransaction, senderAuthenticator: walletSignedTransaction.authenticator, feePayerAuthenticator: sponsorAuthenticator, } );
// if you want to wait for transaction await aptos.waitForTransaction({ transactionHash: txnSubmitted.hash }); } catch (error) { console.error(error); } };
return ( <button onClick={onSignAndSubmitTransaction}> Sign and submit transaction </button> );};
export default SignAndSubmit;-
按照 Gas Station 文档设置 Gas Station 服务,以获取 gas station API 密钥。
-
在应用中集成 Gas Station 实例:
import {GasStationClient,GasStationTransactionSubmitter,} from "@aptos-labs/gas-station-client";import { Network } from "@aptos-labs/ts-sdk";const network = Network.TESTNET;const gasStationClient = new GasStationClient({network,apiKey: process.env.GAS_STATION_API_KEY,});const transactionSubmitter = new GasStationTransactionSubmitter(gasStationClient); -
使用
transactionSubmitter仅赞助跨链账户交易:const { signAndSubmitTransaction, wallet } = useWallet();const transactionInput = {data: {function:"0x1::aptos_account::transfer",functionArguments: [account.address, 717],},sender: account.address,transactionSubmitter: null};// If wallet is NOT an Aptos native walletif (!wallet.isAptosNativeWallet) {transactionInput.transactionSubmitter = transactionSubmitter;}const txn = await signAndSubmitTransaction(transactionInput);
- 原始钱包创建跨链账户,且通常未与 Aptos 集成,因此钱包中无法进行模拟。
- 跨链账户优先使用 DAA,但每个账户仍会保留原始钱包,因此开发者仍可使用和与其交互。
- 跨链账户适配器演示应用
- AIP-113 可派生账户抽象