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
- viem
- web3.js
- ethers.js
- npm
- yarn
- CDN
npm install --save web3
yarn add web3
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
- npm
- yarn
- CDN
npm install --save ethers
yarn add ethers
<script src="https://cdn.ethers.io/scripts/ethers-v4.min.js"></script>
- npm
- yarn
- CDN
npm install --save viem
yarn add viem
<script src="https://cdn.jsdelivr.net/npm/viem@2.9.32/_cjs/index.min.js"></script>
Chain Details for Aleph Zero
- Mainnet
- Testnet
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",
};
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x7F7", // hex of 2039, Aleph Zero Testnet
rpcTarget: "https://rpc.alephzero-testnet.gelato.digital",
displayName: "Aleph Zero Testnet",
blockExplorerUrl: "https://evm-explorer-testnet.alephzero.org",
ticker: "tAZERO",
tickerName: "Aleph Zero Testnet",
logo: "https://i.ibb.co/cYcFtr4/A0-mark-graphite.png",
};
Initializing Web3Auth
- PnP Modal SDK
- PnP NoModal SDK
- Single Factor Auth JS SDK
- MPC CoreKit JS SDK
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,
});
import { Web3AuthNoModal } from "@web3auth/no-modal";
import { AuthAdapter } from "@web3auth/auth-adapter";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import { WEB3AUTH_NETWORK } from "@web3auth/base";
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: { chainConfig },
});
const web3auth = new Web3AuthNoModal({
clientId, // Get it from Web3Auth Dashboard
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
privateKeyProvider,
});
const authAdapter = new AuthAdapter({ privateKeyProvider });
web3auth.configureAdapter(authAdapter);
import { Web3Auth } from "@web3auth/single-factor-auth";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: { chainConfig },
});
const web3auth = new Web3Auth({
clientId, // Get your Client ID from Web3Auth Dashboard
web3AuthNetwork: "sapphire_mainnet",
privateKeyProvider,
});
import { Web3AuthMPCCoreKit, WEB3AUTH_NETWORK, makeEthereumSigner } from "@web3auth/mpc-core-kit";
import { EthereumSigningProvider } from "@web3auth/ethereum-mpc-provider";
import { CHAIN_NAMESPACES } from "@web3auth/base";
import { tssLib } from "@toruslabs/tss-dkls-lib";
const coreKitInstance = new Web3AuthMPCCoreKit({
web3AuthClientId,
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
storage: window.localStorage,
manualSync: true, // This is the recommended approach
tssLib: tssLib,
});
// Setup provider for EVM Chain
const evmProvider = new EthereumSigningProvider({ config: { chainConfig } });
evmProvider.setupProvider(makeEthereumSigner(coreKitInstance));
Get Account and Balance
- viem
- web3.js
- ethers.js
/*
Use code from the above Initializing Provider here
*/
// web3 is const web3 = new Web3(web3Auth.provider); from above.
// Get user's Ethereum public address
const address = (await web3.eth.getAccounts())[0];
// Get user's balance in ether
const balance = web3.utils.fromWei(
await web3.eth.getBalance(address), // Balance is in wei
);
/*
Use code from the above Initializing Provider here
*/
// provider is const provider = new ethers.providers.Web3Provider(web3Auth.provider); from above.
// For ethers v5
// const signer = ethersProvider.getSigner();
const signer = await ethersProvider.getSigner();
// Get user's Ethereum public address
const address = signer.getAddress();
// Get user's balance in ether
// For ethers v5
// const balance = ethers.utils.formatEther(
// await ethersProvider.getBalance(address) // Balance is in wei
// );
const balance = ethers.utils.formatEther(
await provider.getBalance(address), // Balance is in wei
);
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
- viem
- web3.js
- ethers.js
/*
Use code from the above Initializing Provider here
*/
// web3 is const web3 = new Web3(web3Auth.provider); from above.
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const destination = "0x7aFac68875d2841dc16F1730Fba43974060b907A";
const amount = web3.utils.toWei(1); // Convert 1 ether to wei
// Submit transaction to the blockchain and wait for it to be mined
const receipt = await web3.eth.sendTransaction({
from: fromAddress,
to: destination,
value: amount,
maxPriorityFeePerGas: "5000000000", // Max priority fee per gas
maxFeePerGas: "6000000000000", // Max fee per gas
});
/*
Use code from the above Initializing Provider here
*/
// provider is const provider = new ethers.providers.Web3Provider(web3Auth.provider); from above.
const signer = await provider.getSigner();
const destination = "0x7aFac68875d2841dc16F1730Fba43974060b907A";
const amount = ethers.parseEther("1.0"); // Convert 1 ether to wei
// Submit transaction to the blockchain
const tx = await signer.sendTransaction({
to: destination,
value: amount,
});
// Wait for the transaction to be mined
const receipt = await tx.wait();
/*
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
- viem
- web3.js
/*
Use code from the above Initializing Provider here
*/
// web3 is const web3 = new Web3(web3Auth.provider); from above.
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const destination = "0x7aFac68875d2841dc16F1730Fba43974060b907A";
const amount = web3.utils.toWei(1); // Convert 1 ether to wei
// Submit transaction to the blockchain and wait for it to be mined
const receipt = await web3.eth.signTransaction({
from: fromAddress,
to: destination,
value: amount,
maxPriorityFeePerGas: "5000000000", // Max priority fee per gas
maxFeePerGas: "6000000000000", // Max fee per gas
});
/*
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
- viem
- web3.js
- ethers.js
/*
Use code from the above Initializing Provider here
*/
// web3 is const web3 = new Web3(web3Auth.provider); from above.
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage = "YOUR_MESSAGE";
const signedMessage = await web3.eth.personal.sign(originalMessage, fromAddress);
/*
Use code from the above Initializing Provider here
*/
const provider = new ethers.providers.Web3Provider(web3Auth.provider);
const signer = await provider.getSigner();
const originalMessage = "YOUR_MESSAGE";
const signedMessage = await signer.signMessage(originalMessage);
/*
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
- viem
- web3.js
- ethers.js
// 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!",
},
});
import { Eip712TypedData } from "web3/lib/types"
...
/*
Sign Typed Data v4 adds support for
arrays and recursive data types.
Otherwise, it works the same as Sign Typed Data v3.
*/
/*
Use code from the above Initializing Provider here
*/
// web3 is const web3 = new Web3(web3Auth.provider); from above.
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage:Eip712TypedData = {
domain: {
// Defining the same active chain Id
chainId: 11155111, // 1 for mainnet
// Give a user friendly name to the specific contract you are signing for.
name: "Ether Mail",
// If name isn't enough add verifying contract to make sure you are establishing contracts with the proper entity
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
// Just let's you know the latest version. Definitely make sure the field name is correct.
version: "1",
},
// Defining the message signing data content.
message: {
/*
- Anything you want. Just a JSON Blob that encodes the data you want to send
- No required fields
- This is DApp Specific
- Be as explicit as possible when building out the message schema.
*/
contents: "Hello, Bob!",
attachedMoneyInEth: 4.2,
from: {
name: "Cow",
wallets: ["0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"],
},
to: [
{
name: "Bob",
wallets: [
"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
"0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57",
"0xB0B0b0b0b0b0B000000000000000000000000000",
],
},
],
},
// Refers to the keys of the *types* object below.
primaryType: "Mail",
types: {
// TODO: Clarify if EIP712Domain refers to the domain the contract is hosted on
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
// Not an EIP712Domain definition
Group: [
{ name: "name", type: "string" },
{ name: "members", type: "Person[]" },
],
// Refer to PrimaryType
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person[]" },
{ name: "contents", type: "string" },
],
// Not an EIP712Domain definition
Person: [
{ name: "name", type: "string" },
{ name: "wallets", type: "address[]" },
],
},
};
const signedMessage = await web3.eth.signTypedData(fromAddress, originalMessage)
/*
Use code from the above Initializing Provider here
*/
// provider is const provider = new ethers.providers.Web3Provider(web3Auth.provider); from above.
const signer = await provider.getSigner();
// Get user's Ethereum public address
const fromAddress = await signer.getAddress();
const originalMessage = JSON.stringify({
domain: {
// Defining the chain aka Sepolia testnet or Ethereum Main Net
chainId: 11155111,
// Give a user friendly name to the specific contract you are signing for.
name: "Ether Mail",
// If name isn't enough add verifying contract to make sure you are establishing contracts with the proper entity
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
// Just let's you know the latest version. Definitely make sure the field name is correct.
version: "1",
},
// Defining the message signing data content.
message: {
/*
- Anything you want. Just a JSON Blob that encodes the data you want to send
- No required fields
- This is DApp Specific
- Be as explicit as possible when building out the message schema.
*/
contents: "Hello, Bob!",
attachedMoneyInEth: 4.2,
from: {
name: "Cow",
wallets: [
"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF",
],
},
to: [
{
name: "Bob",
wallets: [
"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
"0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57",
"0xB0B0b0b0b0b0B000000000000000000000000000",
],
},
],
},
// Refers to the keys of the *types* object below.
primaryType: "Mail",
types: {
// TODO: Clarify if EIP712Domain refers to the domain the contract is hosted on
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
// Not an EIP712Domain definition
Group: [
{ name: "name", type: "string" },
{ name: "members", type: "Person[]" },
],
// Refer to PrimaryType
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person[]" },
{ name: "contents", type: "string" },
],
// Not an EIP712Domain definition
Person: [
{ name: "name", type: "string" },
{ name: "wallets", type: "address[]" },
],
},
});
const params = [fromAddress, originalMessage];
const method = "eth_signTypedData_v4";
const signedMessage = await signer.provider.send(method, params);