X-Chain Accounts
Thanks to AIP-113 Derivable Account Abstraction, we can manage x-chain signatures flexibly and securely on the Aptos network. This means that any wallet with an authentication function implementation on the Aptos chain can submit transactions to the Aptos network.
This functionality enables a variety of use cases for dApps, enhancing user experience and onboarding.
High level flow
Section titled “High level flow”When a user enters a dApp that supports x-chain accounts, the interaction and experience feel the same as with any Aptos wallet. The user connects with a x-chain account (e.g., Phantom for Solana) and can view their Derivable Abstracted Account (DAA) details, sign messages, and submit transactions to the Aptos chain.
When a dapp submits a transaction using a x-chain account, the wallet adapter utilizes the signIn
function (defined in the x-chain account standard) for domain verification and security. If a specific wallet does not support the signIn
method, the adapter falls back to using the default signMessage
.
The wallet is requested to sign a message to submit a transaction on the Aptos network. Once the wallet approves the transaction, it is submitted to the Aptos chain, where it undergoes a signature verification process.
How does DAA work in a x-chain account?
Section titled “How does DAA work in a x-chain account?”When a user connects to a dApp using the x-chain account adapter, the adapter computes the user’s Derivable Abstracted Account (DAA) address and converts the x-chain account to follow the Aptos wallet standard interface. This ensures a seamless interaction with the wallet for both developers and end users.
The computation of the DAA address is done using the authenticationFunction
and the accountIdentity
, both of which are defined in the wallet adapter:
authenticationFunction
: This is a function that exists on-chain and is used to verify the signature of the x-chain account.accountIdentity
: This represents the identity of the account used in the on-chain authentication function to verify the signature of the x-chain account. In the Wallet Adapter, theaccountIdentity
is based on the original x-chain account’s public key and the dApp domain (e.g., mydomain.com). The format is:${originWalletAddress}${domain}
How to integrate x-chain accounts in my dApp?
Section titled “How to integrate x-chain accounts in my dApp?”Currently, the adapter supports Solana and EVM chains
The wallet adapter follows the Solana Wallet Standard to discover wallets. Currently, the wallets that have been tested and support cross-chain accounts are:
Aptos Devnet | Aptos Testnet | Aptos Mainnet | |
---|---|---|---|
Phantom | ✅ | ✅ | |
Solflare | ✅ | ✅ | |
Backpack | ✅ | ✅ | |
OKX | ✅ | ✅ |
Supporting x-chain accounts in a dApp requires only a 2-step installation process.
-
Install the @aptos-labs/derived-wallet-solana package
Terminal window npm install @aptos-labs/derived-wallet-solana -
Import the setupAutomaticSolanaWalletDerivation function
Once you have installed the
@aptos-labs/derived-wallet-solana
package, you can import and use it. In the same file where you import the other wallets, such asWalletProvider.tsx
, you can add the following: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/> -
Set crossChainWallets dapp config prop to true fot the AptosWalletAdapterProvider
<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,crossChainWallets: true,}}>{children}<AptosWalletAdapterProvider/>
The wallet adapter follows the EIP-1193 to discover wallets. Currently, the wallets that have been tested and support cross-chain accounts are:
Aptos Devnet | Aptos Testnet | Aptos Mainnet | |
---|---|---|---|
Metamask | ✅ | ✅ | |
Phantom | ✅ | ✅ | |
Coinbase | ✅ | ✅ | |
OKX | ✅ | ✅ | |
Exodus | ✅ | ✅ | |
Backpack | ✅ | ✅ |
Supporting x-chain accounts in a dApp requires only a 2-step installation process.
-
Install the @aptos-labs/derived-wallet-ethereum package
Terminal window npm install @aptos-labs/derived-wallet-ethereum -
Import the setupAutomaticEthereumWalletDerivation function
Once you have installed the
@aptos-labs/derived-wallet-ethereum
package, you can import and use it. In the same file where you import the other wallets, such asWalletProvider.tsx
, you can add the following: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/> -
Set crossChainWallets dapp config prop to true fot the AptosWalletAdapterProvider
<AptosWalletAdapterProviderdappConfig={{network: Network.TESTNET,crossChainWallets: true,}}>{children}<AptosWalletAdapterProvider/>
That will handle the logic and implementation to include the x-chain accounts as if they were Aptos wallets.
Submitting a transaction
Section titled “Submitting a transaction”In most cases, allowing users to submit a transaction with a x-chain account to the Aptos chain requires using a sponsor transaction. This is because the x-chain account might not have APT to pay for gas. Therefore, the dApp should consider maintaining a sponsor account to sponsor the transactions.
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;
Considerations
Section titled “Considerations”- Since the origin wallet creates an x-chain account and is most likely not integrated with Aptos, simulation is not available in the wallet.
- While the x-chain account prioritizes DAA, each account also retains the origin wallet, so developers should be able to use it and interact with it
Resources
Section titled “Resources”- X-Chain Accounts Adapter Demo App
- AIP-113 Derivable Account Abstraction