Integrate Web3Auth with the Neon 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
,
viem
etc. to make Neon 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 Neon blockchain, you can use either library with Web3Auth.
- 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>
Initializing Provider
Using eip155
as chainNamespace
while initializing web3auth
will provide an
EIP1193
compatible provider as web3auth.provider
after successful authentication.
Getting the chainConfig
- Mainnet
- Testnet
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0xe9ac0d6", // hex of 245022934, Neon mainnet
rpcTarget: "https://neon-proxy-mainnet.solana.p2p.org",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Neon Mainnet",
blockExplorerUrl: "https://neonscan.org/",
ticker: "NEON",
tickerName: "NEON",
logo: "https://docs.neonevm.org/icons/token.svg",
};
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0xe9ac0ce", // hex of 245022926, neon testnet
rpcTarget: "https://devnet.neonevm.org",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Neon Mainnet",
blockExplorerUrl: "https://devnet.neonscan.org/",
ticker: "NEON",
tickerName: "NEON",
logo: "https://docs.neonevm.org/icons/token.svg",
};
Initializing and instantiating the Web3Auth SDK
- 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));
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
- Single Factor Auth JS 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 Single Factor Auth JS SDK
await web3auth.init();
// 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
- 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] });