Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
leoparis89 committed Apr 12, 2023
1 parent 4114218 commit 27b0b44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
31 changes: 6 additions & 25 deletions src/components/sendForm/SendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { TezosNetwork } from "@airgap/tezos";
import { useToast } from "@chakra-ui/react";
import { Estimate } from "@taquito/taquito";
import { useState } from "react";
import { AccountType } from "../../types/Account";
import { UmamiEncrypted } from "../../types/UmamiEncrypted";
import { useAccounts } from "../../utils/hooks/accountHooks";
import { useGetOwnedAccount } from "../../utils/hooks/accountHooks";
import { useSelectedNetwork } from "../../utils/hooks/assetsHooks";
import {
estimateDelegation,
Expand Down Expand Up @@ -54,21 +52,9 @@ const makeSimulation = (
return Promise.reject(`Unrecognized type!`);
};

export const useGetPkAndEsk = () => {
const accounts = useAccounts();

return (pkh: string) => {
const account = accounts.find((a) => a.pkh === pkh);
if (!account) {
throw new Error("No account found");
}

if (account.type === AccountType.MNEMONIC) {
return { esk: account.esk, pk: account.pk };
} else {
return { esk: undefined, pk: account.pk };
}
};
export const useGetPk = () => {
const getAccount = useGetOwnedAccount();
return (pkh: string) => getAccount(pkh).pk;
};

export const SendForm = ({
Expand All @@ -82,14 +68,13 @@ export const SendForm = ({
}) => {
const network = useSelectedNetwork();
const toast = useToast();
const getPkAndEsk = useGetPkAndEsk();
const getPk = useGetPk();

const [isLoading, setIsLoading] = useState(false);

const [transferValues, setTransferValues] = useState<{
transaction: TransactionValues;
estimate: Estimate;
esk: UmamiEncrypted | undefined;
}>();

const [hash, setHash] = useState<string>();
Expand All @@ -99,18 +84,14 @@ export const SendForm = ({
try {
const sender = transaction.values.sender;

const { pk, esk } = getPkAndEsk(sender);
const pk = getPk(sender);

// pk needed for simulation
const estimate = await makeSimulation(transaction, pk, network);

// esk needed for real transfer
// If esk is undefined we are using SSO. Hacky.
// TOOD implement better solution
setTransferValues({
transaction,
estimate,
esk,
});
} catch (error: any) {
toast({ title: "Invalid transaction", description: error.message });
Expand Down
29 changes: 17 additions & 12 deletions src/components/sendForm/steps/SubmitStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import { isValid } from "date-fns";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { GoogleAuth } from "../../../GoogleAuth";
import { UmamiEncrypted } from "../../../types/UmamiEncrypted";
import { AccountType } from "../../../types/Account";
import { decrypt } from "../../../utils/aes";
import { useGetOwnedAccount } from "../../../utils/hooks/accountHooks";
import {
mutezToTezNumber,
prettyTezAmount,
Expand Down Expand Up @@ -61,7 +62,8 @@ const makeTransfer = (
);
}

return Promise.reject(`Unrecognized type!`);
const error: never = t;
throw new Error(error);
};

const renderSubTotal = (t: TransactionValues) => {
Expand All @@ -85,27 +87,29 @@ export const RecapDisplay: React.FC<{
recap: {
transaction: TransactionValues;
estimate: Estimate;
esk: UmamiEncrypted | undefined;
};
onSucces: (hash: string) => void;
}> = ({
recap: { estimate, transaction: transfer, esk },
network,
onSucces,
}) => {
}> = ({ recap: { estimate, transaction: transfer }, network, onSucces }) => {
const isTez = transfer.type === "tez";
const isDelegation = transfer.type === "delegation";
const nft = transfer.type === "nft" ? transfer.data : undefined;
const renderAccountTile = useRenderAccountSmallTile();
const renderBakerTile = useRenderBakerSmallTile();
const getAccount = useGetOwnedAccount();

const { register, handleSubmit } = useForm<{ password: string }>();
const toast = useToast();
const [isLoading, setIsLoading] = useState(false);

const isGoogleSSO = !esk;
const signerAccount = getAccount(transfer.values.sender);

const isGoogleSSO = signerAccount.type === AccountType.SOCIAL;

const onSubmitGoogleSSO = async (sk: string) => {
if (signerAccount.type === AccountType.MNEMONIC) {
throw new Error(`Wrong signing method called`);
}

setIsLoading(true);
try {
const result = await makeTransfer(transfer, sk, network);
Expand All @@ -118,14 +122,15 @@ export const RecapDisplay: React.FC<{
setIsLoading(false);
};

// TODO remove duplication
const onSubmitNominal = async ({ password }: { password: string }) => {
if (!esk) {
throw new Error("esk required");
if (signerAccount.type === AccountType.SOCIAL) {
throw new Error(`Wrong signing method called`);
}

setIsLoading(true);
try {
const sk = await decrypt(esk, password);
const sk = await decrypt(signerAccount.esk, password);
const result = await makeTransfer(transfer, sk, network);

onSucces(result.hash);
Expand Down
11 changes: 11 additions & 0 deletions src/utils/hooks/accountHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ export const useReset = () => {
dispatch(accountsSlice.actions.reset());
};
};

export const useGetOwnedAccount = () => {
const accounts = useAccounts();
return (pkh: string) => {
const account = accounts.find((a) => a.pkh === pkh);
if (!account) {
throw new Error(`You do not ownn account:${pkh}`);
}
return account;
};
};

0 comments on commit 27b0b44

Please sign in to comment.