Skip to content

Commit

Permalink
Merge pull request #47 from FLock-io/fix/email-registration
Browse files Browse the repository at this point in the history
fix email registration
  • Loading branch information
rodrigopavezi authored Sep 13, 2023
2 parents 5d2ec12 + 2fa8011 commit f5306af
Showing 1 changed file with 21 additions and 40 deletions.
61 changes: 21 additions & 40 deletions src/renderer/components/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
useContractWrite,
useWaitForTransaction,
} from 'wagmi';
import jose from 'node-jose';
import { getPublicCompressed } from '@toruslabs/eccrypto';
import { FLOCK_ABI, FLOCK_ADDRESS } from 'renderer/contracts/flock';
import { ToastContainer, toast } from 'react-toastify';
import { web3AuthInstance } from '../Web3AuthInstance';
Expand All @@ -32,7 +30,8 @@ function Wallet() {
const [isTransferLoading, setIsTransferLoading] = useState(false);
const [isWalletOpen, setIsWalletOpen] = useState(false);
const [showWalletImport, setShowWalletImport] = useState(false);
const { connectAsync, connectors, pendingConnector } = useConnect();
const { connectAsync, connectors, pendingConnector, isSuccess } =
useConnect();
const { disconnect: wagmiDisconnect } = useDisconnect();
const { nativeTokenBalance, flockTokenBalance } = useContext(WalletContext);
const [privateKey, setPrivateKey] = useState('');
Expand All @@ -47,9 +46,9 @@ function Wallet() {
};

const handleImport = async () => {
// @ts-ignore
setIsWalletOpen(false);
try {
// @ts-ignore
await connectors[1].setPrivateKey(`0x${privateKey}`);
await connectAsync({
connector: connectors[1],
Expand All @@ -76,6 +75,7 @@ function Wallet() {
setUserEmail(email);
setShowWalletImport(false);
} else {
setShowWalletImport(false);
setShowEmailImport(true);
}
} catch (error) {
Expand All @@ -85,43 +85,32 @@ function Wallet() {

const importEmail = async () => {
try {
const publicKey = getPublicCompressed(
Buffer.from(privateKey.padStart(64, '0'), 'hex')
).toString('hex');

const payload = {
publicKey,
};

const keyStore = jose.JWK.createKeyStore();

// Create a key object from the private key string
const key = await keyStore.add({
kty: 'oct',
k: privateKey,
});

// Sign the JWT using the key
const jwt = await jose.JWS.createSign({ format: 'compact' }, key)
.update(JSON.stringify(payload))
.final();

const res = await fetch(
const response = await fetch(
'https://us-central1-flock-demo-design.cloudfunctions.net/postEmailToDB',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
body: JSON.stringify({
pubKey: publicKey,
email: userEmail,
wallet: address,
}),
}
);
setShowEmailImport(false);
if (response.ok) {
setShowEmailImport(false);
} else {
const data = await response.json();
console.log(data);
if (
data.error.includes(
'Unique constraint failed on the fields: (`email`)'
)
) {
toast.error('Email already exists');
}
}
} catch (error) {
console.error('Error importing email:', error);
}
Expand Down Expand Up @@ -149,16 +138,9 @@ function Wallet() {
const loadUserInfo = async () => {
try {
if (pendingConnector?.id === 'web3auth') {
const privateKey = (await web3AuthInstance.provider?.request({
method: 'eth_private_key',
})) as string;

const publicKey = getPublicCompressed(
Buffer.from(privateKey.padStart(64, '0'), 'hex')
).toString('hex');
const user = await web3AuthInstance.getUserInfo();

const res = await fetch(
await fetch(
'https://us-central1-flock-demo-design.cloudfunctions.net/postEmailToDB',
{
method: 'POST',
Expand All @@ -167,7 +149,6 @@ function Wallet() {
Authorization: `Bearer ${user.idToken}`,
},
body: JSON.stringify({
pubKey: publicKey,
email: user.email,
wallet: address,
}),
Expand Down Expand Up @@ -217,7 +198,7 @@ function Wallet() {
loadEmail();
loadUserInfo();
}
}, [address]);
}, [address, isSuccess]);

if (showWalletSettings) {
return (
Expand Down Expand Up @@ -356,7 +337,7 @@ function Wallet() {

if (showEmailImport) {
return (
<Layer onEsc={() => setShowWalletImport(false)} full>
<Layer onEsc={() => setShowEmailImport(false)} full>
<Box
align="center"
justify="center"
Expand Down

0 comments on commit f5306af

Please sign in to comment.