Skip to content

Commit c763d57

Browse files
authored
Feat/send user email to db (#17)
1 parent 1ba176b commit c763d57

File tree

3 files changed

+86
-15
lines changed

3 files changed

+86
-15
lines changed

src/renderer/App.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { WalletContextProvider } from './context/walletContext';
1515
import Faucet from './pages/Faucet';
1616
import { CONFIG } from './config';
1717
import { RunnerContextProvider } from './context/runnerContext';
18+
import { useEffect, useState } from 'react';
19+
import { web3AuthInstance } from './Web3AuthInstance'
1820

1921
export default function App() {
2022
const flockTheme = {
@@ -76,20 +78,6 @@ export default function App() {
7678
blockExplorer: chains[0]?.blockExplorers.default?.url,
7779
};
7880

79-
const web3AuthInstance = new Web3Auth({
80-
clientId: CONFIG.WEB3_AUTH_CLIENT_ID,
81-
web3AuthNetwork: 'cyan',
82-
// @ts-ignore
83-
chainConfig,
84-
authMode: 'WALLET',
85-
uiConfig: {
86-
theme: 'light',
87-
appName: 'FLock Client',
88-
appLogo:
89-
'https://drive.google.com/uc?export=download&id=1Pm_naD3LlamhxkEVv-i2VBVG2RC4DYaZ',
90-
},
91-
});
92-
9381
const privateKeyProvider = new EthereumPrivateKeyProvider({
9482
config: { chainConfig },
9583
});

src/renderer/Web3AuthInstance.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { configureChains } from 'wagmi';
2+
import { polygonMumbai } from 'wagmi/chains';
3+
import { alchemyProvider } from 'wagmi/providers/alchemy';
4+
import { Web3Auth } from '@web3auth/modal';
5+
import { CONFIG } from './config'; // Make sure to import the relevant config
6+
7+
const { chains } = configureChains([polygonMumbai], [
8+
alchemyProvider({ apiKey: 'Qsvi2mE7TTt44pEwkojqyqdRb1s0xAQV' }),
9+
]);
10+
11+
const chainConfig = {
12+
chainNamespace: 'eip155',
13+
chainId: `0x${chains[0].id.toString(16)}`,
14+
// @ts-ignore
15+
rpcTarget: CONFIG.WEB3_AUTH_RPC,
16+
displayName: chains[0].name,
17+
tickerName: chains[0].nativeCurrency?.name,
18+
ticker: chains[0].nativeCurrency?.symbol,
19+
blockExplorer: chains[0]?.blockExplorers.default?.url,
20+
};
21+
22+
export const web3AuthInstance = new Web3Auth({
23+
clientId: CONFIG.WEB3_AUTH_CLIENT_ID,
24+
web3AuthNetwork: 'cyan',
25+
// @ts-ignore
26+
chainConfig,
27+
authMode: 'WALLET',
28+
uiConfig: {
29+
theme: 'light',
30+
appName: 'FLock Client',
31+
appLogo:
32+
'https://drive.google.com/uc?export=download&id=1Pm_naD3LlamhxkEVv-i2VBVG2RC4DYaZ',
33+
},
34+
});

src/renderer/components/Wallet.tsx

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Box, Button, Heading, Layer, Text } from 'grommet';
2-
import { useContext, useState } from 'react';
2+
import { useContext, useEffect, useState } from 'react';
33
import { WalletContext } from 'renderer/context/walletContext';
44
import truncateEthAddress from 'truncate-eth-address';
55
import { useAccount, useConnect, useDisconnect } from 'wagmi';
6+
import { web3AuthInstance } from '../Web3AuthInstance';
7+
import { getPublicCompressed } from "@toruslabs/eccrypto";
68

79
function Wallet() {
810
const { address } = useAccount();
@@ -21,6 +23,53 @@ function Wallet() {
2123
wagmiDisconnect();
2224
};
2325

26+
interface UserInfo {
27+
email: string;
28+
address: string;
29+
name?: string;
30+
profileImage?: string;
31+
aggregateVerifier?: string;
32+
verifier?: string;
33+
verifierId?: string;
34+
typeOfLogin?: string;
35+
dappShare?: string;
36+
idToken?: string; //jwt
37+
oAuthIdToken?: string;
38+
oAuthAccessToken?: string;
39+
}
40+
41+
const loadUserInfo = async () => {
42+
try {
43+
const privateKey = await web3AuthInstance.provider?.request({
44+
method: "eth_private_key",
45+
}) as string;
46+
47+
const publicKey = getPublicCompressed(Buffer.from(privateKey.padStart(64, "0"), "hex")).toString("hex");
48+
const user = await web3AuthInstance.getUserInfo();
49+
50+
const res = await fetch("https://us-central1-flock-demo-design.cloudfunctions.net/postEmailToDB", {
51+
method: "POST",
52+
headers: {
53+
"Content-Type": "application/json",
54+
Authorization: "Bearer " + user.idToken,
55+
},
56+
body: JSON.stringify({ pubKey: publicKey, email: user.email, wallet: address }),
57+
});
58+
59+
console.log(res, "success!")
60+
return res.json();
61+
62+
} catch (error) {
63+
console.error('Error loading user info:', error);
64+
}
65+
};
66+
67+
useEffect(() => {
68+
if (address) {
69+
loadUserInfo();
70+
}
71+
}, [address]);
72+
2473
if (showWalletSettings) {
2574
return (
2675
<Layer onEsc={() => setShowWalletSettings(false)} full>

0 commit comments

Comments
 (0)