Skip to content

Commit

Permalink
add Download JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
0xk0stas committed Oct 20, 2023
1 parent 96677c9 commit 238c667
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts


# SC
SmartContract/target/*
SmartContract/Cargo.lock
39 changes: 36 additions & 3 deletions src/views/Home/components/ExistingUser/ExistingUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { createEncryptionKey, decrypt } from '@/utils/functions/cryptography';
import { Box, Button, Text, VStack, HStack, Circle, Icon, List, ListItem } from "@chakra-ui/react";
import { FaBeer, FaCoffee, FaPhabricator, FaEraser, FaDownload, FaCopy, FaExternalLinkAlt } from 'react-icons/fa';
import { addressIsValid } from '@multiversx/sdk-dapp/utils/account';
import { Mnemonic, UserWallet } from '@multiversx/sdk-wallet/out';


const theme = extendTheme({
Expand Down Expand Up @@ -53,9 +54,39 @@ function ExistingUser({ address, email, secretWords, userGid }: { address: strin
const [walletDeletionHash, setWalletDeletionHash] = useState('');
const [balance, setBalance] = useState(null); // Added state for balance
const [price, setPrice] = useState<number | null>(null); // Added state for price
const [clickedDownloadJson, setClickedDownloadJson] = useState(false);

const handleDownload = () => {
setPinModalOpen(true);
setClickedDownloadJson(true);
};

useEffect(() => {
if (encryptionKey != '' && isPinModalOpen == false && clickedDownloadJson) {
let words = secretWords.map((word, index) => (decrypt(word, encryptionKey))).join(' ');

let mnemonic = Mnemonic.fromString(words);

let walletJson = UserWallet.fromSecretKey({
secretKey: mnemonic.deriveKey(0),
password: pin,
}).toJSON();

const jsonContent = JSON.stringify(walletJson, null, 2);
const blob = new Blob([jsonContent], { type: 'application/json' });
const url = window.URL.createObjectURL(blob);

const a = document.createElement('a');
a.href = url;
a.download = `${walletJson.bech32}.json`;

a.click();
window.URL.revokeObjectURL(url);
}
}, [clickedDownloadJson, encryptionKey, isPinModalOpen, pin, secretWords]);

const handleClickWords = () => {
setClickedDownloadJson(false);
if (isWordsVisible) {
setWordsVisible(false);
} else {
Expand All @@ -75,7 +106,9 @@ function ExistingUser({ address, email, secretWords, userGid }: { address: strin

const handlePinSubmit = () => {
setEncryptionKey(createEncryptionKey(pin, userGid));
setWordsVisible(true);
if (!clickedDownloadJson) {
setWordsVisible(true);
}
setPinModalOpen(false);
};

Expand Down Expand Up @@ -172,8 +205,8 @@ function ExistingUser({ address, email, secretWords, userGid }: { address: strin
_hover={{ bg: "transparent" }}
_active={{ bg: "transparent" }}
_focus={{ boxShadow: "none" }}
onClick={handleClickWords}
isDisabled={true} // Disabling the button
onClick={handleDownload}
// isDisabled={true} // Disabling the button
>
<VStack spacing={1}>
<Circle size="50px" bg="blue.500" color="white">
Expand Down
4 changes: 0 additions & 4 deletions src/views/Home/components/NewUserForm/CreateWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ export default function CreateWallet({ pin, email, handleReset, userGid, setClic
setJsonFileContent(jsonPretty);
setJsonPretty(jsonPretty);
onJsonPrettyChange(jsonPretty);


let mnemonic = Mnemonic.fromString(walletInfo.mnemonic.words.join(' '));
console.log(mnemonic);
} catch (error) {
console.error(error);
}
Expand Down

0 comments on commit 238c667

Please sign in to comment.