Installation

Magic interacts with the Aptos blockchain via Magic’s extension NPM package @magic-ext/aptos (beta version 0.5.0). Additionally, the Aptos extension lets you interact with the blockchain using methods from Aptos’ TypeScript SDK and/or @aptos-labs/wallet-adapter-core.
NOTEThis extension requires @magic-ext/aptos version ^0.5.0 , and magic-sdk version ^17.1.6 (latest preferred)
To get started, install the following dependencies for your project:
npm install @aptos-labs/wallet-adapter-core @magic-ext/aptos magic-sdk

Initialization

The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key. You can then use the SDK object to initialize an AptosWallet object that meets the Aptos Wallet standard. You can use any supported login method supported by Magic SDK in the “connect” function.
JavaScript
import { Magic } from 'magic-sdk';
import { AptosExtension } from '@magic-ext/aptos';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new AptosExtension({
      nodeUrl: 'https://fullnode.testnet.aptoslabs.com'
    }),
  ],
});

const magicAptosWallet = new MagicAptosWallet(magic, {
  connect: async () => {
    await magic.auth.loginWithMagicLink({ email });
    const accountInfo = await magic.aptos.getAccountInfo();
    return accountInfo;
  }
});
WARNINGThe nodeUrl you specify will determine whether you are using the mainnet or testnet for Aptos.

Common Methods

Send Transaction

Getting the Aptos Account

The first step will be getting the logged in user’s Aptos account address. Follow any of the login guides on the Authentication tab to get started. You can check if a user is logged in with the following code:
JavaScript
const isLoggedIn = await magic.user.isLoggedIn();
Once the user is logged in, you can get their Aptos account address by running:
JavaScript
const { address } = await magic.aptos.getAccountInfo();
Note: this does not return an actual AptosAccount object, only the address. See the note below for more detail.
Alternatively, you can use MagicAptosWallet’s account() method:
JavaScript
const { address } = await magicAptosAccount.account();

Funding an Account with the Faucet

Next up for our testing, you’ll need to fund the account using the Faucet. This code snippet is taken from the Aptos Tutorial
JavaScript
const DEVNET_NODE_URL = 'https://fullnode.testnet.aptoslabs.com';
const DEVNET_FAUCET_URL = 'https://faucet.testnet.aptoslabs.com';
const faucetClient = new FaucetClient(DEVNET_NODE_URL, DEVNET_FAUCET_URL);
await faucetClient.fundAccount(address, 100_000_000);

Sign and Send Transaction

Note that the Magic Aptos extension follows the method names and conventions of Aptos’ TypeScript SDK. If you are using MagicAptosWallet, you can skip the rest of this section and build your application using the Aptos Wallet Standard. You can also find example apps by Aptos here (MagicAptosWallet is not included in this useWallet, however, due to the flexibility Magic SDK provides - and the complexity of setting it up does not neatly fit into this example’s React hook).
WARNINGThe Beta version of the Magic Aptos Extension only supports Raw transactions. Future versions will include full support for the following: - CoinClient - TokenClient⁠ - AptosAccountIn the meantime, you can find the equivalent RawTransactions in the Aptos source code for any of the object methods above. See the bottom of this page for a detailed example.
JavaScript
// Set the input variables
const MAGIC_WALLET_ADDRESS = '0x906fd65afe31b7237cd4d7c4073d8bf76c61b6a24ec64dd26f0c16de5c2444d5'
const SAMPLE_RAW_TRANSACTION = {
  function: "0x1::coin::transfer",
  type_arguments: ["0x1::aptos_coin::AptosCoin"],
  arguments: [MAGIC_WALLET_ADDRESS, 1000]
}


// Initialize the AptosClient and transaction
const client = new AptosClient(DEVNET_NODE_URL);
const rawTransaction = await client.generateTransaction(address, SAMPLE_RAW_TRANSACTION)

// Sign the transaction using Magic SDK, then send
const signedTransaction = await magic.aptos.signTransaction(rawTransaction)
const transaction = await client.submitTransaction(signedTransaction)
const result = await client.waitForTransactionWithResult(transaction.hash, {
  checkSuccess: true
})

Get Balance

JavaScript
const address = await magic.aptos.getAccountInfo();
const client = new AptosClient(DEVNET_NODE_URL);
const coinClient = new CoinClient(client);

const balance = await coinClient.checkBalance(address);
console.log(balance)

Resources