Skip to content

Instantly share code, notes, and snippets.

@andreyluiz
Created June 10, 2024 12:14
Web3Auth initialization
import { CHAIN_NAMESPACES, WALLET_ADAPTERS, WEB3AUTH_NETWORK } from '@web3auth/base';
import { EthereumPrivateKeyProvider } from '@web3auth/ethereum-provider';
import { MetamaskAdapter } from '@web3auth/metamask-adapter';
import { Web3Auth } from '@web3auth/modal';
import { OpenloginAdapter } from '@web3auth/openlogin-adapter';
import { ethers } from 'ethers';
import wallet from './store/wallet';
const clientId = '...';
const TESTNET_RPC_URL = 'https://evm-rpc-arctic-1.sei-apis.com';
const chainConfig = {
chainId: '0xae3f3', // Please use 0x1 for Mainnet
rpcTarget: TESTNET_RPC_URL,
chainNamespace: CHAIN_NAMESPACES.EIP155,
displayName: 'Sei EVM Devnet',
ticker: 'SEI',
tickerName: 'SEI',
blockExplorer: ['https://arctic-explorer.sei-apis.com']
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: { chainConfig }
});
const web3auth = new Web3Auth({
clientId,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
privateKeyProvider
});
const metamaskAdapter = new MetamaskAdapter({
clientId,
sessionTime: 3600, // 1 hour in seconds
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
chainConfig
});
const openloginAdapter = new OpenloginAdapter({
adapterSettings: {
loginConfig: {
facebook: {
verifier: 'facebook-verifier',
typeOfLogin: 'facebook',
clientId: '...'
},
discord: {
verifier: 'discord-verifier',
typeOfLogin: 'discord',
clientId: '...'
}
}
}
});
web3auth.configureAdapter(metamaskAdapter);
web3auth.configureAdapter(openloginAdapter);
(async () => {
await web3auth.initModal({
modalConfig: {
[WALLET_ADAPTERS.METAMASK]: {
label: 'MetaMask'
},
[WALLET_ADAPTERS.OPENLOGIN]: {
label: 'OpenLogin',
loginMethods: {
facebook: {
name: 'Facebook',
showOnModal: true
},
twitter: {
name: 'Twitter',
showOnModal: true
},
discord: {
name: 'Discord',
showOnModal: true
},
google: {
name: 'Google',
showOnModal: false
},
github: {
name: 'GitHub',
showOnModal: false
},
reddit: {
name: 'Reddit',
showOnModal: false
},
linkedin: {
name: 'LinkedIn',
showOnModal: false
},
apple: {
name: 'Apple',
showOnModal: false
},
twitch: {
name: 'Twitch',
showOnModal: false
},
line: {
name: 'Line',
showOnModal: false
},
kakao: {
name: 'Kakao',
showOnModal: false
},
weibo: {
name: 'Weibo',
showOnModal: false
},
wechat: {
name: 'WeChat',
showOnModal: false
},
farcaster: {
name: 'Farcaster',
showOnModal: false
},
email_passwordless: {
name: 'email_passwordless',
showOnModal: false
},
sms_passwordless: {
name: 'sms_passwordless',
showOnModal: false
}
}
}
}
});
})();
// Called when clicking on "Connect wallet" on the UI
export const connectWallet = async () => {
const provider = await web3auth.connect();
const web3 = new ethers.providers.Web3Provider(provider as ethers.providers.ExternalProvider);
const balance = await web3.getBalance(await web3.getSigner().getAddress());
console.log('Balance:', ethers.utils.formatEther(balance));
// Using svelte stores
wallet.set({
provider: web3
});
};
// Called when clicking "Disconnect wallet" on the UI
export const disconnectWallet = async () => {
await web3auth.logout();
wallet.set({
provider: null
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment