跳转到内容

跨链账户

得益于 AIP-113 可派生账户抽象,可在 Aptos 网络上灵活、安全地管理跨链签名。任何在 Aptos 链上实现认证函数的钱包均可向 Aptos 网络提交交易。这为 dApp 带来多种用例,并改善用户体验和入门流程。

用户进入支持跨链账户的 dApp 时,体验与普通 Aptos 钱包相同。用户连接跨链账户(例如 Solana 的 Phantom),可查看可派生抽象账户(DAA)详细信息、签名消息并向 Aptos 链提交交易。

dApp 使用跨链账户提交交易时,钱包适配器会使用跨链账户标准定义的 signIn 函数进行域验证和安全保护。若钱包不支持 signIn,适配器会回退使用默认 signMessage。钱包批准签名后,交易提交到 Aptos 链并进行签名验证。

跨链账户适配器会计算用户的 DAA 地址,并将跨链账户转换为遵循 Aptos 钱包标准的接口,使开发者和最终用户均可无缝交互。

DAA 地址使用钱包适配器中定义的 authenticationFunctionaccountIdentity 计算:

  • authenticationFunction:链上用于验证跨链账户签名的函数。
  • accountIdentity:链上认证函数用于验证签名的账户身份。它基于原跨链账户公钥和 dApp 域名(如 mydomain.com),格式为:${originWalletAddress}${domain}

钱包适配器遵循 Solana Wallet Standard 来发现钱包。已测试支持跨链账户的钱包:

Aptos DevnetAptos TestnetAptos Mainnet
Phantom
Solflare
Backpack
OKX

跨链账户集成分三步:

  1. 安装 @aptos-labs/derived-wallet-solana 包

    Terminal window
    npm install @aptos-labs/derived-wallet-solana
  2. 导入 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
    ...
    <AptosWalletAdapterProvider
    dappConfig={{
    network: Network.TESTNET,
    }}
    >
    {children}
    </AptosWalletAdapterProvider>
  3. 将 AptosWalletAdapterProvider 的 crossChainWallets dapp 配置属性设为 true

    <AptosWalletAdapterProvider
    dappConfig={{
    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 client
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);
// Generate a sponsor account or use an existing account
const 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;
  • 原始钱包创建跨链账户,且通常未与 Aptos 集成,因此钱包中无法进行模拟。
  • 跨链账户优先使用 DAA,但每个账户仍会保留原始钱包,因此开发者仍可使用和与其交互。