Skip to main content

Integrate Web3Auth with the Aleph Zero Blockchain in Web Applications

While using the Web3Auth Web SDK, you get a EIP1193 provider, similar to the Metamask Provider. This provider can be used with libraries like web3.js, ethers.js etc. to make Aleph Zero Blockchain calls like getting the user's account, fetching balance, sign transaction, send transaction, read from and write to the smart contract, etc. We have highlighted a few examples to get you started.

Installation

npm install --save viem

Chain Details for Aleph Zero

const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0xA1EF", // hex of 41455, Aleph Zero Mainnet
rpcTarget: "https://rpc.alephzero.raas.gelato.cloud",
displayName: "Aleph Zero Mainnet",
blockExplorerUrl: "https://evm-explorer.alephzero.org",
ticker: "AZERO",
tickerName: "Aleph Zero",
logo: "https://i.ibb.co/cYcFtr4/A0-mark-graphite.png",
};

Initializing Web3Auth

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

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

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

Get Account and Balance

const publicClient = createPublicClient({
chain: mainnet, // for mainnet
transport: custom(this.provider),
});

const walletClient = createWalletClient({
chain: mainnet,
transport: custom(this.provider),
});

// Get user's Ethereum public address
const address = await walletClient.getAddresses();

// Get user's balance in ether
const balance = await publicClient.getBalance({ address: address[0] });

Send Transaction

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

const publicClient = createPublicClient({
chain: mainnet, // for mainnet
transport: custom(this.provider),
});

const walletClient = createWalletClient({
chain: mainnet, // for mainnet
transport: custom(this.provider),
});

// data for the transaction
const destination = "<ADDRESS>";
const amount = parseEther("0.0001");
const address = await this.getAccounts();

const address = await walletClient.getAddresses();

// Submit transaction to the blockchain
const hash = await walletClient.sendTransaction({
account: address[0],
to: destination,
value: amount,
});

const receipt = await publicClient.waitForTransactionReceipt({ hash });

Sign Transaction

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

const walletClient = createWalletClient({
chain: mainnet, // for mainnet
transport: custom(this.provider),
});

// data for the transaction
const destination = "<ADDRESS>";
const amount = parseEther("0.0001");
const address = await this.getAccounts();

const address = await walletClient.getAddresses();

// Prepare transaction
const request = await walletClient.prepareTransactionRequest({
account: address[0],
to: destination,
value: amount,
});

// Sign transaction
const signature = await walletClient.signTransaction(request);

// Submit transaction to the blockchain
const hash = await walletClient.sendRawTransaction(signature);

Sign Message

Personal Sign

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

const publicClient = createPublicClient({
chain: mainnet, // for mainnet
transport: custom(this.provider),
});

const walletClient = createWalletClient({
chain: mainnet, // for mainnet
transport: custom(this.provider),
});

// data for signing
const address = await walletClient.getAddresses();
const originalMessage = "YOUR_MESSAGE";

// Sign the message
const hash = await walletClient.signMessage({
account: address[0],
message: originalMessage,
});

Sign Typed Data V4

// SignTypedData in viem
// https://viem.sh/docs/actions/wallet/signTypedData

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

const walletClient = createWalletClient({
chain: mainnet,
transport: custom(this.provider),
});

const address = await walletClient.getAddresses();

const signature = await walletClient.signTypedData({
account: address[0],
domain: {
name: "Ether Mail",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
types: {
Person: [
{ name: "name", type: "string" },
{ name: "wallet", type: "address" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "string" },
],
},
primaryType: "Mail",
message: {
from: {
name: "Cow",
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
},
to: {
name: "Bob",
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
},
contents: "Hello, Bob!",
},
});