Skip to main content

Integrate Web3Auth with the Linea Blockchain in JavaScript

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 Linea 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 here to get you started quickly on that.

Installation

To interact with the Linea blockchain, you can use either library with Web3Auth.

npm install --save viem

Initializing Provider

Using eip155 as chainNamespace while initializing web3auth will provide an EIP1193 compatible provider as web3auth.provider after successful authentication.

Getting the chainConfig

const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0xe708", // hex of 42161
rpcTarget: "https://rpc.linea.build",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Linea Mainnet",
blockExplorer: "https://lineascan.build/",
ticker: "ETH",
tickerName: "ETH",
};

Initializing and instantiating the Web3Auth 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,
});

Getting the Web3Auth provider

After initializing Web3Auth, the next step is to initialize the provider and use it for your operations.

// 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`

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 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!",
},
});