衍生钱包
轻量级加载包 @aptos-labs/wallet-adapter-反应 使功能能够使用 EVM 钱包作为本地的 Aptos 钱包
EVM 钱包如何使用钱包适配器 ?
当用户使用支持的EVM钱包连接到dApp时,适配器会计算用户的可衍生摘要账户(DAA)地址并转换EVM账户,以遵循Aptos钱包标准接口. 这确保了开发者和终端用户与钱包的无缝互动.
DAA 地址的计算使用 authenticationFunction 编辑 accountIdentity,两者在本包中定义:
authenticationFunction:这是一个存在于链上并用于验证EVM账户签名的函数. 该函数存在于0x1::ethereum_derivable_account::authenticateaccountIdentity:这是用于验证EVM账户签名的链上认证功能中使用的账户的身份. EVM DAA账户身份格式为:${originWalletAddress}${domain}
如何将EVM钱包整合到我的DApp中?
包裹跟随 EIP-1193 (英语) 以发现钱包. 目前,已测试和支持跨链账户的钱包有:
| 阿普托斯·德文内 | Aptos 测试网 | 阿普托斯主网 | |
|---|---|---|---|
| 元数据 | ✅ | ✅ | ✅ |
| 幽灵 | ✅ | ✅ | ✅ |
| 监狱 | ✅ | ✅ | ✅ |
| OKX 单曲 | ✅ | ✅ | ✅ |
| 出狱 | ✅ | ✅ | ✅ |
| 后包装 | ✅ | ✅ | ✅ |
使用量
备选案文1:自动钱包检测(建议用于dApps)
在用户连接其EVM钱包的dApp构建时使用这种方法.
- 安装软件包
npm install @aptos-labs/wallet-adapter-react @aptos-labs/derived-wallet-ethereum
- 设置自动检测
import { AptosWalletAdapterProvider, Network } from "@aptos-labs/wallet-adapter-react";
import { setupAutomaticEthereumWalletDerivation } from "@aptos-labs/derived-wallet-ethereum";
setupAutomaticEthereumWalletDerivation({ defaultNetwork: Network.TESTNET });
// ...
<AptosWalletAdapterProvider
dappConfig={{
network: Network.TESTNET,
}}
>
{children}
</AptosWalletAdapterProvider>
备选方案2:程序签名(用于节点/脚本)
在服务器侧签名或脚本中直接访问Ethereum私人密钥时使用这种方法.
npm install @aptos-labs/derived-wallet-ethereum
import { Wallet } from "ethers";
import { EIP1193DerivedAccount } from "@aptos-labs/derived-wallet-ethereum";
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
// Create or load an Ethereum wallet
const ethereumWallet = Wallet.createRandom();
// Create the derived account
const account = new EIP1193DerivedAccount({
ethereumWallet,
domain: "my-dapp.com",
});
// Get the derived Aptos address
console.log("Aptos address:", account.accountAddress.toString());
// Sign and submit a transaction
const aptos = new Aptos(new AptosConfig({ network: Network.TESTNET }));
const transaction = await aptos.transaction.build.simple({
sender: account.accountAddress,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [recipientAddress, 100],
},
});
const response = await aptos.signAndSubmitTransaction({
signer: account,
transaction,
});
console.log("Transaction hash:", response.hash);
提交交易(与收费人)
在大多数情况下,允许用户将带有EVM账户的交易提交给Aptos链,需要使用赞助商交易. 这是因为EVM账户可能没有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集成,所以在钱包中无法使用模拟.
- 软件包保留了原产地钱包,因此开发人员应该能够通过:
import { useWallet } from "@aptos-labs/wallet-adapter-react";
const { isEIP1193DerivedWallet } = useWallet();
if (isEIP1193DerivedWallet(wallet)) {
const [activeAccount] = await wallet.eip1193Ethers.listAccounts();
}
资源
- X-Chain 账户调整器 Demo App
- AIP-113 衍生账户摘要
- AIP-122 x- 链 DAA 认证, 使用 Sign- in- With- Ethereum