laconic-wallet/utils/wallet-connect/WalletConnectUtils.tsx
shreerang6921 276cb3695a
Use existing ethereum accounts while connecting wallet with dapp (#39)
* Use existing accounts while pairing with dapp

* Listen for events emitted from dapp on every render

* Handle review changes
2024-03-06 11:08:02 +05:30

63 lines
1.7 KiB
TypeScript

import '@walletconnect/react-native-compat';
import '@ethersproject/shims';
import { Core } from '@walletconnect/core';
import { ICore } from '@walletconnect/types';
import { Web3Wallet, IWeb3Wallet } from '@walletconnect/web3wallet';
export let web3wallet: IWeb3Wallet;
export let core: ICore;
export let currentETHAddresses: string[];
export let currentCosmosAddresses: string[];
import { useState, useCallback, useEffect } from 'react';
import { retrieveAccounts } from '../accounts';
export async function createWeb3Wallet() {
const { ethLoadedAccounts } = await retrieveAccounts();
currentETHAddresses = ethLoadedAccounts
? ethLoadedAccounts.map(ethAccount => ethAccount.address)
: [];
// TODO: Move to dotenv
const ENV_PROJECT_ID = 'c97365bf9f06d12a7488de36240b0ff4';
const core = new Core({
projectId: ENV_PROJECT_ID,
});
web3wallet = await Web3Wallet.init({
core,
metadata: {
name: 'Web3Wallet React Native Tutorial',
description: 'ReactNative Web3Wallet',
url: 'https://walletconnect.com/',
icons: ['https://avatars.githubusercontent.com/u/37784886'],
},
});
}
export default function useInitialization() {
const [initialized, setInitialized] = useState(false);
const onInitialize = useCallback(async () => {
try {
await createWeb3Wallet();
setInitialized(true);
} catch (err: unknown) {
console.log('Error for initializing', err);
}
}, []);
useEffect(() => {
if (!initialized) {
onInitialize();
}
}, [initialized, onInitialize]);
return initialized;
}
export async function web3WalletPair(params: { uri: string }) {
return await web3wallet.core.pairing.pair({ uri: params.uri });
}