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.
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
- Yarn
- pnpm
npm install --save aptos
yarn add aptos
pnpm add aptos
Initializing Provider
Getting the chainConfig
- Mainnet
- Testnet
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",
};
const chainConfig = {
chainNamespace: "other",
chainId: "0x7E6", //
rpcTarget: "https://rpc.ankr.com/http/aptos_testnet/v1",
// Avoid using public rpcTarget in production.
displayName: "Aptos Testnet",
blockExplorerUrl: "https://testnet.algoexplorer.io",
ticker: "APT",
tickerName: "Aptos",
};
Initializing and instantiating the Web3Auth SDK
- PnP Modal SDK
- PnP NoModal SDK
- CoreKit SFA Web SDK
import { Web3Auth } from "@web3auth/modal";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK, CHAIN_NAMESPACES } 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,
});
import { Web3AuthNoModal } from "@web3auth/no-modal";
import { AuthAdapter } from "@web3auth/auth-adapter";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK, CHAIN_NAMESPACES } from "@web3auth/base";
const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig },
});
const web3auth = new Web3AuthNoModal({
clientId, // Get it from Web3Auth Dashboard
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
});
const authAdapter = new AuthAdapter();
web3auth.configureAdapter(authAdapter);
import { Web3Auth } from "@web3auth/single-factor-auth";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK, CHAIN_NAMESPACES } from "@web3auth/base";
const web3auth = new Web3Auth({
clientId, // Get your Client ID from Web3Auth Dashboard
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
});
const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig },
});
Getting the Web3Auth provider
After initializing Web3Auth, the next step is to initialize the provider and use it for your operations.
- PnP Modal SDK
- PnP NoModal SDK
- CoreKit SFA Web SDK
// Initialize for PnP Modal SDK
await web3auth.initModal();
// Trigger the login
await web3auth.connect();
// Get the provider
const provider = web3auth.provider;
// Continue using the `provider`
// Initialize for PnP NoModal SDK
await web3auth.init();
// Trigger the login
await web3auth.connectTo("auth");
// Get the provider
const provider = web3auth.provider;
// Continue using the `provider`
// Initialize for CoreKit SFA Web SDK
await web3auth.init();
// Trigger the login
await web3auth.connect();
// Get the provider
const provider = web3auth.provider;
// Continue using the `provider`
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 InitializeWeb3Auth and get provider steps
*/
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 InitializeWeb3Auth and get provider steps
*/
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 InitializeWeb3Auth and get provider steps
*/
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;