Skip to main content

Integrate Web3Auth with the Aptos Blockchain

While using the Web3Auth Web SDK for a non-EVM chain like Aptos you get a standard provider from which you can get the private key of the user. Using this private key, you can use the corresponding libraries of the blockchain to make blockchain calls like getting the user's account, fetch balance, sign transaction, send transaction, read from and write to the smart contract, etc. We have highlighted a few methods here to get you started quickly on that.

note

This reference is for the Web, however, you can use Aptos on other Mobile and Gaming Platforms as well. Please follow our reference for EVM Blockchains, and similarly use Aptos libraries that support the platforms to use the private key and make blockchain calls accordingly.

Installation

npm install --save aptos

Initializing Provider

Getting the chainConfig

const chainConfig = {
chainNamespace: "other",
chainId: "0x1", //
rpcTarget: "https://rpc.ankr.com/http/aptos/v1",
// Avoid using public rpcTarget in production.
displayName: "Aptos Mainnet",
blockExplorerUrl: "https://algoexplorer.io",
ticker: "APT",
tickerName: "Aptos",
};

Initializing and instantiating the Web3Auth SDK

import { Web3Auth } from "@web3auth/modal";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK } from "@web3auth/base";

const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig: chainConfig }
});

const web3auth = new Web3Auth({
// Get it from Web3Auth Dashboard
clientId,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
privateKeyProvider: privateKeyProvider,
});

Get User Info

After logging in, the Web3Auth instance will provide you with information regarding the user that is logged in. This information is obtained directly from the JWT token and is not stored by Web3Auth. Therefore, this information can only be accessed through social logins after the user has logged into your application.

const user = await web3auth.getUserInfo(); // web3auth instance

Get Account and Key

Once a user logs in, the Web3Auth SDK returns a provider. Since Web3Auth doesn't have a native provider for Aptos, we need to directly use the private key for making the RPC calls.

Using the function, web3auth.provider.request({method: "private_key"}) from Web3Auth provider, the application can have access to the user's private key. However, we cannot use this key with Aptos EC Curve specific signing functions, hence, we first derive the Aptos Account using the getAptosAccount() function.

On the underhood, it uses the helper functions from the aptos library to derive the key pair.

import { AptosAccount, AptosClient } from "aptos";

/*
Use code from the above Initializing Provider here
*/

// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: "private_key" });
const privateKeyUint8Array = new Uint8Array(privateKey.match(/.{1,2}/g)!.map((byte: any) => parseInt(byte, 16)));
const aptosAccount = new AptosAccount(privateKeyUint8Array);
const aptosAccountAddress = aptosAccount.address();

Get Balance

import { AptosAccount, AptosClient } from "aptos";

/*
Use code from the above Initializing Provider here
*/

// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: "private_key" });
const privateKeyUint8Array = new Uint8Array(privateKey.match(/.{1,2}/g)!.map((byte: any) => parseInt(byte, 16)));
const aptosAccount = new AptosAccount(privateKeyUint8Array);
const aptosAccountAddress = aptosAccount.address();

const NODE_URL = "https://fullnode.devnet.aptoslabs.com";
const aptosCoinStore = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";

const client = new AptosClient(NODE_URL);

let resources = await client.getAccountResources(aptosAccount.address());
let accountResource = resources.find((r) => r.type === aptosCoinStore);
let balance = parseInt((accountResource?.data as any).coin.value);

Send Transaction

import { AptosAccount, FaucetClient, AptosClient } from "aptos";

/*
Use code from the above Initializing Provider here
*/

// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: "private_key" });
const privateKeyUint8Array = new Uint8Array(privateKey.match(/.{1,2}/g)!.map((byte: any) => parseInt(byte, 16)));
const aptosAccount = new AptosAccount(privateKeyUint8Array);
const aptosAccountAddress = aptosAccount.address();

const NODE_URL = "https://fullnode.devnet.aptoslabs.com";
const aptosCoinStore = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";

const client = new AptosClient(NODE_URL);

const payload = {
type: "entry_function_payload",
function: "0x1::coin::transfer",
type_arguments: ["0x1::aptos_coin::AptosCoin"],
arguments: [aptosAccount.address().hex(), 717], // sending funds to self
};
const txnRequest = await client.generateTransaction(aptosAccount.address(), payload);
const signedTxn = await client.signTransaction(aptosAccount, txnRequest);
const transactionRes = await client.submitTransaction(signedTxn);
const hash = transactionRes.hash;